Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Breaking Up with SVG-in-JS

Breaking Up with SVG-in-JS

HRBrain Fly High #4 -Frontend-にて使用した資料

wakamaki

July 28, 2023
Tweet

Other Decks in Programming

Transcript

  1. <!-- Icon.svg --> <svg viewBox="0 0 300 300"> <g><path d="M0

    200 v-200 h200 a100,100 90 0,1 0,200 a100,100 90 0,1 -200,0z" /></g> </svg> import React from 'react'; import Icon from "./Icon.svg"; const Test = () => { return <Icon /> }
  2. import * as React from "react" const Svg = (props)

    => ( <svg viewBox="0 0 300 300" width="1em" height="1em" {...props}> <path d="M0 200V0h200a100 100 90 0 1 0 200 100 100 90 0 1-200 0z" /> </svg> ) export default Svg import React from 'react'; import Svg from ".svg"; const Test = () => { return <Svg fill="red" width={20} height={20} /> } SVGR
  3. <!-- Icon.svg --> <svg viewBox="0 0 300 300"> <g><path d="M0

    200 v-200 h200 a100,100 90 0,1 0,200 a100,100 90 0,1 -200,0z" /></g> </svg> import React from 'react'; import Icon from "./Icon.svg"; const Test = () => { return <img src={Icon} /> } Using <img> to load the SVG
  4. SVG sprites – using <use> <svg style="display: none;"> <symbol id="icon-heart"

    viewBox="0 0 32 32"> <title>heart icon</title> <path d="M16 29.414l-13.413-13.415 6.826-6.826 6.587 6.587 6.587-6.587 6.8266.826z"></path> </symbol> </svg> <svg class="icon icon-heart"><use href="#icon-heart"></use></svg> <svg class="icon icon-heart"><use href="#icon-heart"></use></svg>