Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offline Icon Support #964

Open
mpaxson opened this issue Feb 22, 2024 · 1 comment
Open

Offline Icon Support #964

mpaxson opened this issue Feb 22, 2024 · 1 comment
Assignees
Labels
b-enhancement New feature or request

Comments

@mpaxson
Copy link

mpaxson commented Feb 22, 2024

Description
Support for a full offline icon library would be much appreciated (Like Material Design Icons or font awesome community)

Love your library but my use case is in an airgapped environment and the ICONES support won't work.

As such, I have to import many SVGs for each scene by hand and greatly decreases the template/example time.

Thanks!

@mpaxson mpaxson added the b-enhancement New feature or request label Feb 22, 2024
@aarthificial
Copy link
Contributor

You should be able to achieve this with a simple component.

  1. Using a third-party library
    You can install something like @material-design-icons/svg in your project:

    npm i @material-design-icons/svg

    Then leverage glob imports to implement the component:

    const ICONS_PATH =
      '../../node_modules/@material-design-icons/svg/filled/';
    const ICONS = import.meta.glob<true, string, {default: string}>(
      `../../node_modules/@material-design-icons/svg/filled/*.svg`,
      {eager: true},
    );
    
    export function Icon({name, ...props}: ImgProps & {name: string}) {
      const module = ICONS[`${ICONS_PATH}${name}.svg`];
      return <Img src={module.default} {...props} />;
    }

    This will import the local urls of all icons and allow you to reference them by name without importing them individually:

    view.add(<Icon name={'face'} />);

    Note that ICONS_PATH is relative to the current file.

  2. Using static assets.
    You can create a public directory in your project and copy all the icons you want to use over there.
    Vite will then resolve absolute paths to that directory:

    export function Icon({name, ...props}: ImgProps & {name: string}) {
      return <Img src={`/icons/${name}.svg`} {...props} />;
    }

    The above example assumes that you put your icons in the /public/icons/ directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
b-enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants