简体   繁体   English

如何获取 Material UI Icon SVG 路径? 反应图标选择器

[英]How to get Material UI Icon SVG path? React Icon Picker

I have a react application using Material UI's Icons.我有一个使用 Material UI 图标的 React 应用程序。 The main objective is to have an Icon Picker, where a user can select a MUI Icon and that selected icon's SVG path is saved within the state.主要目标是拥有一个图标选择器,用户可以在其中选择一个 MUI 图标,并且所选图标的 SVG 路径保存在状态中。

This svg path will then be saved out to an API where it can be displayed in various places.这个 svg 路径将被保存到一个 API 中,在那里它可以显示在不同的地方。 etc.等等。

I've searched through documentation on MUI's site regarding icons, but it's all about implementation, which I can do just fine.我已经在 MUI 网站上搜索了有关图标的文档,但这都是关于实现的,我可以做得很好。 I've looked for an npm package, without much luck.我一直在寻找一个 npm 包,但运气不佳。

I did come across this package ( https://github.com/DMDc0de/material-ui-icon-picker ), which is essentially what I'd like the picker to be - but it outputs the icon's name for an icon component <i /> .我确实遇到过这个包( https://github.com/DMDc0de/material-ui-icon-picker ),这基本上是我想要的选择器 - 但它输出图标组件的图标名称<i /> Not what I want.不是我想要的。 I need the source of the SVG path.我需要 SVG 路径的来源。

Any direction towards this would be super helpful.对此的任何方向都会非常有帮助。

  1. Go to the icon site: https://material.io/tools/icons/ 转到图标站点: https//material.io/tools/icons/
  2. Click on an icon 单击图标
  3. Click on "Selected icon" button (bottom left) 点击“选中图标”按钮(左下角)
  4. Click on the "SVG" button to download the SVG version 单击“SVG”按钮下载SVG版本

Alternatively, go to the GitHub repo and download the SVGs there. 或者,转到GitHub仓库并在那里下载SVG。

One way of doing that programmatically is to load the component and to render it in a string.以编程方式执行此操作的一种方法是加载组件并将其呈现在字符串中。 Then to extract the path from the string.然后从字符串中提取路径。

To do so, we can use the renderToString or renderToMarkupString method of ReactDomServer.为此,我们可以使用ReactDomServer 的 renderToStringrenderToMarkupString方法。

Than we can extract the path from the generated string.然后我们可以从生成的字符串中提取路径。 We can either parse the svg XML with the DOMParser or with a regexp.我们可以使用DOMParser或正则表达式解析 svg XML。

Here's an example in TypeScript:这是 TypeScript 中的一个示例:

import EditIcon from '@material-ui/icons/Edit';
import ReactDOMServer from 'react-dom/server';

export function getEditIconPath(): string {
  const iconString = ReactDOMServer.renderToString(<EditIcon />);
  const parser = new DOMParser();
  const svgDoc = parser.parseFromString(iconString, 'image/svg+xml');
  const iconPath = svgDoc.querySelector('path')?.getAttribute('d') as string;

  return iconPath;
}

Another way to achieve that would be to use the React Test Renderer , thus we can get directly a json including the different properties (including the path).另一种实现方式是使用React Test Renderer ,因此我们可以直接获得一个包含不同属性(包括路径)的 json。 However, it looks like this method is around 10 times slower than the previous method.但是,看起来这种方法比以前的方法慢 10 倍左右。

Here's an example with the second method:这是第二种方法的示例:

import EditIcon from '@material-ui/icons/Edit';
import TestRenderer from 'react-test-renderer'; // ES6

export function getEditIconPath(): string {
  const iconComponent = TestRenderer.create(<EditIcon />);
  const iconJson = iconComponent.toJSON();
  const path = iconJson.children[0].props.d;

  return path;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM