To Keep It Going, Please Show Your Love.Buy Me a Coffee at ko-fi.com

Svelte Google Materialdesign Icons

sponsor npm Created by Shin Okada License npm

2120+ SVG Google Materialdesign icon components for Svelte.

Thank you for considering my open-source package. If you use it in a commercial project, please support me by sponsoring me on GitHub sponsor. Your support helps me maintain and improve this package for the benefit of the community.

Repo #

GitHub Repo

Installation #

pnpm i -D svelte-google-materialdesign-icons

Usages #

In a svelte file:

<script>
  import { Hiking, Double_arrow, Javascript } from 'svelte-google-materialdesign-icons';
</script>

<Hiking />
<Double_arrow />
<Javascript />

Since a component name can’t be started with a number, if a SVG file name starts with a number, A is prefixed. For example, 30fps_select.svg will be named as A30fps_select.svelte.

Faster compiling #

If you need only a few icons from this library in your Svelte app, import them directly. This can optimize compilation speed and improve performance by reducing the amount of code processed during compilation.

<script>
  import Hiking from 'svelte-google-materialdesign-icons/Hiking.svelte';
</script>

<Hiking />

Props #

  • size: string = ctx.size || ‘24’;
  • role: string = ctx.role || ‘img’;
  • color: string = ctx.color || ‘currentColor’;
  • variation: ‘filled’ | ‘outlined’ | ‘round’ | ‘sharp’ | ‘two-tone’ = ctx.variation || ‘outlined’;

IDE support #

If you are using an LSP-compatible editor, such as VSCode, Atom, Sublime Text, or Neovim, hovering over a component name will display a documentation link, props, and events.

Variation #

The default variation value is outline. Use the variation prop to change it to solid.

<Hiking variation="two-tone" />

Size #

Use the size prop to change the size of icons.

<Hiking size="30" />
<Double_arrow size="40" />
<Javascript size="50" />

If you are using Tailwind CSS, you can add a custom size using Tailwind CSS by including the desired classes in the class prop. For example:

<Hiking class="shrink-0 h-20 w-20" />

Using Icon component #

This library provides Icon component that you can use the icon and other props to set an icon.

<script>
  import {Icon, Add_to_photos} from 'svelte-google-materialdesign-icons';
</script>

<Icon
    icon={Add_to_photos}
    on:click={() => console.log('Hello!')}
    size="50"
    variation="outlined"
    ariaLabel="Add to your photo library"
/>

Please see Event section under to see forwarded events.

Setting Global Icon using setContext #

You can establish global icon preferences in your Svelte application using setContext. This allows you to configure icon-related properties once and share them across multiple components. Here’s how you can do it:

<script>
  import { setContext } from 'svelte';

  // Define your global icon settings
  const iconCtx = {
    strokeWidth: '1.5',
    size: '100', // Icon size in pixels
    color: '#ff4488', // Icon color in hexadecimal or CSS color name
    variation: 'filled'
  };
  setContext('iconCtx', iconCtx);
</script>

The size, color, and role properties are optional, allowing you to fine-tune the appearance and accessibility of your icons as needed.

If you set size, icons can be customized with different colors. For example:

<script>
  import { setContext } from 'svelte';
  import { Zoom_in } from 'svelte-google-materialdesign-icons';
  const iconCtx = {
    size: '50'
  };
  setContext('iconCtx', iconCtx);
</script>

<Zoom_in color="#ff4488" />

Remember that you can set only one or two of these properties, allowing you to tailor icon settings to your specific design and accessibility requirements.

Feel free to mix and match these properties as needed to create visually appealing and accessible icons in your Svelte application.

Creating a Default Icon Setting #

You can create a config file, /src/lib/icon.config.json.

The Icon component serves as a wrapper for svelte:component, allowing you to establish a global default setting or expand the capabilities of a component.

To create a default global icon setting, follow these steps:

Configuration File #

Start by creating a configuration file named /src/lib/icon.config.json with the following structure:

{
  "config1": {
    "size": 40,
    "color": "#FF5733"
  },
  "config2": {
    "size": 50,
    "color": "#445533"
  }
}

In this JSON file, you can define different configurations (config1 and config2 in this case) for your icons, specifying attributes like size, variation, and color.

Implementation #

In your Svelte page file, make use of the configurations from the JSON file:

<script lang="ts">
  type IconConfig = {
    config1: {
      size: number;
      color: string;
    };
    config2: {
      size: number;
      color: string;
    };
  };
  import config from '$lib/icon.config.json';
  import { Icon, Add_comment, Arrow_circle_left } from 'svelte-google-materialdesign-icons';

  const iconConfig: IconConfig = config;
  const config1 = iconConfig.config1;
  const config2 = iconConfig.config2;
</script>

<Icon {...config1} icon="{Add_comment}" />
<Icon {...config2} icon="{Arrow_circle_left}" />

We import the configurations from the JSON file and assign them to config1 and config2. We then utilize the Icon component with the spread attributes {...config1} and {...config2} to apply the respective configurations to each icon.

Custom Default Icon #

If you wish to create a custom default icon, you can follow these steps:

Create a Svelte component named src/lib/MyIcon.svelte:

<script lang="ts">
  import type { ComponentType } from 'svelte';
  const config = {
    size: 30,
    color: '#FF5733'
  };
  import { Icon } from 'svelte-google-materialdesign-icons';
  export let icon: ComponentType;
</script>

<Icon {...config} {icon} />

This component, MyIcon.svelte, accepts an icon prop which you can use to pass in the specific icon component you want to display. The default configuration is also applied to the icon.

Implementation in a Page #

To use your custom default icon in a Svelte page, do the following:

<script>
  import MyIcon from '$lib/MyIcon.svelte';
  import { Add_comment } from 'svelte-google-materialdesign-icons';
</script>

<MyIcon icon="{Add_comment}" />

Here, we import the MyIcon component and the Add_comment icon. By passing the Add_comment icon to the icon prop of MyIcon, you apply the default configuration to the icon.

CSS HEX Colors #

Use the color prop to change colors with HEX color code.

<Hiking size="50" color="#ff0000" />
<Double_arrow size="50" color="#3a7cff" />
<Javascript size="50" color="#3fe537" />

CSS framworks suport #

You can apply CSS framework color and other attributes directly to the icon component or its parent tag using the class prop.

Tailwind CSS example:

<Hiking class="h-24 w-24 text-blue-700 mr-4" />

Bootstrap examples:

<Hiking class="position-absolute top-0 px-1" />

Dark mode #

If you are using the dark mode on your website with Tailwind CSS, add your dark mode class to the class prop.

Let’s use dark for the dark mode class as an example.

<Hiking class="text-blue-700 dark:text-red-500" />

aria-label #

All icons have aria-label. For example Hiking has aria-label="hiking". Use ariaLabel prop to modify the aria-label value.

<Hiking ariaLabel="Hiking icon" />

Unfocusable icon #

If you want to make an icon unfocusable, add tabindex="-1".

<Hiking tabindex="-1" />

Events #

All icons have the following events:

  • on:click
  • on:keydown
  • on:keyup
  • on:focus
  • on:blur
  • on:mouseenter
  • on:mouseleave
  • on:mouseover
  • on:mouseout

Passing down other attributes #

You can pass other attibutes as well.

<Hiking tabindex="0" />

Using svelte:component #

<script>
  import { Hiking } from 'svelte-google-materialdesign-icons';
</script>

<svelte:component this="{Hiking}" />

Using onMount #

<script>
  import { Hiking } from 'svelte-google-materialdesign-icons';
  import { onMount } from 'svelte';
  const props = {
    size: '50',
    color: '#ff0000'
  };
  onMount(() => {
    const icon = new Hiking({ target: document.body, props });
  });
</script>

Import all #

Use import * as Icon from 'svelte-google-materialdesign-icons.

<script>
  import * as Icon from 'svelte-google-materialdesign-icons';
</script>

<Icon.Hiking />
<Icon.Double_arrow />

<h1>Size</h1>
<Icon.Hiking size="50" />
<Icon.Double_arrow size="50" />

<h1>CSS HEX color</h1>
<Icon.Hiking size="50" color="#ff0000" />
<Icon.Double_arrow size="50" color="#3a7cff" />

<h1>Tailwind CSS</h1>
<Icon.Hiking size="50" class="text-pink-700" />
<Icon.Double_arrow size="50" class="text-blue-500" />

Icon finder #

You can find icon images at Google Fonts

Original source #

material-design-icons

License #

Svelte-Google-Materialdesign-Icons License

Google Fonts LICENSE

Other icons #

Svelte-Icon-Sets