-
Notifications
You must be signed in to change notification settings - Fork 20
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
How do you dynamically load an Icon? #34
Comments
Yeah, we need to document this process and how it should work. It unfortunately requires a server-side component if one wants to use all 5k icons or it can become a performance issue for pages with lots of icons. Do you want to provide all ~5k icons in the icon picker or a subset? We/community need to make some demos of both examples. |
I was just looking to create a subset...we've picked out about 800 icons to use. For now, what I did was create a custom object with array of icons, including a tag, name of icon and path. Like this where I insert the 800 icons. It's lazy loaded on icon picker page..but when a user selects the icon, I simply store the svg path in db/cache. Then, in the rest of my application, I can simply use the svgpath and display, so it's very fast. It would be super helpeful to have someway of picking icons you want and then generating this type of object which can be used to display them on icon selection page. |
I am trying to use dynamic import from this article |
@AmirHosseinKarimi You could use the const data = body.match(/d="([^"]+)"/)[1]; Obviously write a light cache layer into your component so you don't make unnecessary requests for the path data. |
The |
Sorry I did not understand |
@AmirHosseinKarimi https://github.com/Templarian/MaterialDesign-SVG contains individual SVG files you can deploy. These could be requested by their name in a wrapping component. You only need the SVG path data aka |
So I have created a package (materialdesign-js) to dynamically load an icon. Currently if you need a dynamically load icon, you can create the following component: import React from "react";
import { Icon as MDIcon } from "@mdi/react";
class Icon extends React.Component {
render() {
let icon = require(`materialdesign-js/icons/${this.props.icon}`).default;
if (!icon) {
throw Error(`Could not find materialdesign-js/icons/${icon}`);
}
return <MDIcon path={icon} />;
}
}
export default Icon; and use like this: import Icon from "./Icon";
...
<Icon icon="mdiAccount" /> If this package is helped you please give a star: Also if you like to contribute, just look at the MaterialDesign-JS repository. |
We recommend using a peer dependency for
Yours will be more lightweight than the We will work to open source the IndexedDB solution as it will be more ideal performance wise. Also makes it easier in cases where one may need to search all icons in a list. |
Good, I create this package because I need dynamic load icons in my current project. |
Finally I have find a way which can use dynamic load MDI and also do not decrease the performance of app in development and runtime. |
You're requiring the entire @mdi/js library here, so any development or production build of the app would have to include the whole package (around 2.3MB). |
@craigrileyuk Yea you're right. It's a little bit old. But I think the dynamic load example in the MaterialDesign-JS repository readme is correct and working. |
maybe a little late but this works for me fine:
|
So, in my app, I want to provide an Icon picker...let the user choose their icon. Once saved, I would use it on other pages in the app.
How do I dynamically import this specific icon on those pages?
The text was updated successfully, but these errors were encountered: