2.1.2 • Published 1 month ago

google-material-icons v2.1.2

Weekly downloads
-
License
ISC
Repository
-
Last release
1 month ago

Google Material Icons

Implementation of the google material icon library for react applications.

Installation

yarn add google-material-icons

or

npm install google-material-icons

How to use

It's built with ES modules so it's completely tree-shakable. Each icon can be imported as a react component with two different variant Filled and Outlined.

Example

You can pass additional props to adjust the icon.

import { Abc } from "google-material-icons/filled";

const App = () => {
  return <Abc color="red" size={48} />;
};

export default App;
import { Abc } from "google-material-icons/outlined";

const App = () => {
  return <Abc color="red" size={48} />;
};

export default App;

Props

nametypedefault
sizeNumber24
colorStringcurrentColor
strokeWidthNumber2

Custom props

You can also pass custom props that will be added in the svg as attributes.

const App = () => {
  return <Abc fill="red" />;
};

Generic icon component

It is possible to create a generic icon component to load icons.

:warning: The example below is importing all ES modules. This is not recommended when you using a bundler since your application build size will grow substantially.

import { filledIcons } from "google-material-icons";

const Icon = ({ name, color, size }) => {
  const GoogleMaterialIcon = filledIcons[name];

  return <GoogleMaterialIcon color={color} size={size} />;
};

export default Icon;

With Dynamic Imports

Google Material Icons exports a dynamic import map dynamicIconImports. Useful for applications that want to show icons dynamically by icon name. For example when using a content management system with where icon names are stored in a database.

When using client side rendering, it will fetch the icon component when it's needed. This will reduce the initial bundle size.

The keys of the dynamic import map are the Google Material original icon names.

Example with React suspense:

import React, { lazy, Suspense } from "react";
import { GoogleMaterialProps } from "google-material-icons/outlined";
import outlinedDynamicIconImports from "google-material-icons/outlinedDynamicIconImports";

const fallback = <div style={{ background: "#ddd", width: 24, height: 24 }} />;

interface IconProps extends Omit<GoogleMaterialProps, "ref"> {
  name: keyof typeof outlinedDynamicIconImports;
}

const Icon = ({ name, ...props }: IconProps) => {
  const GoogleMaterialIcon = lazy(outlinedDynamicIconImports[name]);

  return (
    <Suspense fallback={fallback}>
      <GoogleMaterialIcon {...props} />
    </Suspense>
  );
};

export default Icon;
NextJS Example

In NextJS, the dynamic function can be used to dynamically load the icon component.

To make dynamic imports work with NextJS, you need to add google-material-icons to the transpilePackages option in your next.config.js like this:

/** @type {import('next').NextConfig} */
const nextConfig = {
  transpilePackages: ["google-material-icons"], // add this
};

module.exports = nextConfig;

You can then start using it:

import dynamic from "next/dynamic";
import { GoogleMaterialProps } from "google-material-icons/filled";
import filledDynamicIconImports from "google-material-icons/filledDynamicIconImports";

interface IconProps extends GoogleMaterialProps {
  name: keyof typeof filledDynamicIconImports;
}

const Icon = ({ name, ...props }: IconProps) => {
  const GoogleMaterialIcon = dynamic(filledDynamicIconImports[name]);

  return <GoogleMaterialIcon {...props} />;
};

export default Icon;
2.1.2

1 month ago

2.1.1

2 months ago

2.0.1

6 months ago

2.0.0

6 months ago

0.1.0

1 year ago

1.0.5

1 year ago