# Google Material Icons

Implementation of the google material icon library for react applications.

## Installation

```sh
yarn add google-material-icons
```

or

```sh
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.

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

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

export default App;
```

```js
import { Abc } from "google-material-icons/outlined";

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

export default App;
```

### Props

| name          | type     | default      |
| ------------- | -------- | ------------ |
| `size`        | _Number_ | 24           |
| `color`       | _String_ | currentColor |
| `strokeWidth` | _Number_ | 2            |

### Custom props

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

```js
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.

```js
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:

```tsx
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](https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading#nextdynamic) 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`](https://nextjs.org/docs/app/api-reference/next-config-js/transpilePackages) option in your `next.config.js` like this:

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

module.exports = nextConfig;
```

You can then start using it:

```tsx
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;
```
