google-material-icons2
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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/dist/esm/filled";

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

export default App;
import { Abc } from "google-material-icons/dist/esm/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.

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

Generic icon component

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

⚠️ 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 { icons } from "google-material-icons/dist/esm/outlined";

const Icon = ({ name, color, size }) => {
  const GoogleMaterialIcon = icons[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/dist/esm/outlined";
import dynamicIconImports from "google-material-icons/outlinedDynamicIconImports";

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

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

const Icon = ({ name, ...props }: IconProps) => {
  const GoogleMaterialIcon = lazy(dynamicIconImports[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/dist/esm/filled";
import dynamicIconImports from "google-material-icons/filledDynamicIconImports";

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

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

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

export default Icon;

Package Sidebar

Install

npm i google-material-icons2

Weekly Downloads

2

Version

1.0.0

License

ISC

Unpacked Size

64.7 MB

Total Files

8516

Last publish

Collaborators

  • engmrms