React 下最輕量級(jí)的 Vectorized Icon Component

以下方法來(lái)自 http://jxnblk.com/react-geomicons/ 。

鑒于 React 構(gòu)造組件的方便性,實(shí)現(xiàn)一個(gè)基于 SVG 的 Icon Component 只要下面區(qū)區(qū)幾行:

const paths = {
  bookmark: 'M6 2 L26 2 L26 30 L16 24 L6 30 Z  ',
  calendar: 'M2 4 L6 4 L6 2 A2 2 0 0 1 10 2 L10 4 L22 4 L22 2 A2 2 0 0 1 26 2 L26 4 L30 4 L30 10 L2 10 M2 12 L30 12 L30 30 L2 30  ',
  camera: 'M0 6 L8 6 L10 2 L22 2 L24 6 L32 6 L32 28 L0 28 z M9 17 A7 7 0 0 0 23 17 A7 7 0 0 0 9 17  ',
  // ... 根據(jù)你自己的需要添加 SVG path
}

const Icon = ({ name, ...props }) => {
  const path = paths[name] || false
  return (
    <svg
      {...props}
      viewBox="0 0 32 32"
    >
      <path d={path} />
    </svg>
  )
}

Icon.defaultProps = {
  name: 'warning',
  width: '1em',
  height: '1em',
  fill: 'black',
}

用法就是:

React.render(<Icon name='bookmark' />, document.querySelector('#container'))

你不需要引入像 Font-Awesome 這樣完整的 Icon Font/Set 到你的項(xiàng)目中,只需要放入你自己需要的 Icon Path。還有更輕更快速的方法嗎?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容