SamuelTJackson
SamuelTJackson

Reputation: 1537

Create icon with inkscape and import it with material ui SvgIcon

I just created an icon in inkscape. Like mentioned here I set the viewport to 24x24 and I saved it as a svg file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   sodipodi:docname="drawing.svg"
   inkscape:version="1.0 (4035a4fb49, 2020-05-01)"
   id="svg16"
   version="1.1"
   viewBox="0 0 6.3499999 6.3499997"
   height="24"
   width="24">
  <defs
     id="defs10" />

  <sodipodi:namedview
     lock-margins="true"
     fit-margin-bottom="0"
     fit-margin-right="0"
     fit-margin-left="0"
     fit-margin-top="0"
     units="px"
     showborder="true"
     inkscape:snap-nodes="true"
     inkscape:snap-others="true"
     inkscape:object-nodes="false"
     inkscape:snap-object-midpoints="false"
     inkscape:snap-midpoints="false"
     inkscape:guide-bbox="true"
     showguides="true"
     inkscape:window-maximized="0"
     inkscape:window-y="37"
     inkscape:window-x="1920"
     inkscape:window-height="1041"
     inkscape:window-width="1916"
     inkscape:object-paths="false"
     showgrid="false"
     inkscape:document-rotation="0"
     inkscape:current-layer="layer1"
     inkscape:document-units="px"
     inkscape:cy="15.424759"
     inkscape:cx="14.537256"
     inkscape:zoom="22.627417"
     inkscape:pageshadow="2"
     inkscape:pageopacity="0.0"
     borderopacity="1.0"
     bordercolor="#666666"
     pagecolor="#ffffff"
     id="base" />
  <metadata
     id="metadata13">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
    <g
         transform="translate(74.493643,27.309445)"
         style="opacity:0.954874"
         id="layer1"
         inkscape:groupmode="layer"
         inkscape:label="Layer 1">
        <path
           d="m -74.493643,-24.6269 v 3.667168 h 3.166867 V -24.6269 Z m 1.366019,0.339834 h 0.434813 v 0.257505 h -0.434813 z"
           style="opacity:1;fill:#000000;stroke:none;stroke-width:0.0185523;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none"
           id="rect1057" />
        <path
           sodipodi:nodetypes="ccccc"
           id="path1065"
           style="opacity:1;fill:#000000;stroke:none;stroke-width:0.0185523;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none"
           d="m -71.310514,-24.626608 3.166871,-2.639198 v 3.667163 l -3.166871,2.639198 z" />
        <path
           d="m -71.357846,-27.309445 -3.115172,2.657264 h 3.166868 l 3.158511,-2.629412 z m 0.0046,0.125321 2.897601,0.02523 -2.850926,2.381387 h -2.858487 z"
           style="opacity:1;fill:#000000;stroke:none;stroke-width:0.0185523;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none"
           id="path1067" />
      </g>
    </svg>

Now I want to use this Icon like I use a material ui icon. So I created a new file called logo and just pasted the whole svg file between <SvgIcon>. This didn't work because sodipodi:nodetypes for example throws an error. In addition the style should be an object and so on. So I cleaned the whole entries and only the paths are left. But no Icon is shown. So I deleted all paths but one to see at least something but nothing is shown.

import React from "react";
import SvgIcon from "@material-ui/core/SvgIcon";

const Logo = (props) => {
  return (
    <SvgIcon {...props}>
      <path
        d="m 4.493643,-24.00 v 3.667168 h 3.166867 V -24.000 Z m 1.366019,0.339834 h 0.434813 v 0.257505 h -0.434813 z"
        id="rect1057" />
    </SvgIcon>
  )
}
export default Logo;

Maybe someone can describe the whole process how this is done correctly. And what I'm doing wrong.

Upvotes: 5

Views: 4743

Answers (1)

SamuelTJackson
SamuelTJackson

Reputation: 1537

I could solve it by myself:

  1. Create your icon inkscape
  2. Save as "Optimized SVG and check Export as SVG 1.1 per settings in Preference Dialog
  3. check the following at the options:
  • Shorten color values
  • Convert CSS attributes to XML attributes
  • Collapse groups
  • Create groups for similar attributes
  1. SVG Output:
  • Remove the XML declaration
  • Remove metadata
  • Remove comments
  • Format output with line-breaks and indentation
  1. Save the file and open it with for example nvim
  2. Copy everything between the <svg> tags
  3. create an file for the icon (icon.js)

code:

import React from "react";
import SvgIcon from "@material-ui/core/SvgIcon";

const MyIcon = ({ color }) => {
  const memorizedIcon = React.useMemo(() => {
    return (
      <SvgIcon viewBox={"0 0 52.916665 52.91667"}>
        // YOUR ICON
      </SvgIcon>
    );
  }, [color]);

  return memorizedIcon;
};

export default MyIcon;
  1. Copy the viewbox from the svg file to the code
  2. transform the style annotations from style="something-tag: something" to style={{somethingTag: "something"}}

Done

Upvotes: 13

Related Questions