0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GatsbyでFeatherIconsを使う方法

Posted at

Gatsbyでアイコンフォントを使うときのメモ。

使い方

アイコンフォントをインストールする。

npm install feather-icons --save

yarnの場合

yarn add feather-icons

コンポーネント内でインポート。

{feather.icons.facebook.toSvg()}だけだとHTMLがエスケープされて表示されるので注意。

// src/components/header.js
import React from "react"
import { Link } from "gatsby"
import feather from 'feather-icons'

const Header = () => {
  return (
<header id="header">
  <div className="container">
    <h1 className="logo">サイトタイトル</h1>
    <nav className="header-menu">
      <ul className="nav">
        <li><Link to="/about">メニュー</Link></li>
        <li>
          <Link to="/facebook-url">
            <div dangerouslySetInnerHTML={
              { __html: feather.icons.facebook.toSvg({width: 15,height: 13}) }
            } />
          </Link>
        </li>
      </ul>
    </nav>
  </div>
</header>
  )
}

export default Header

utilを使う場合

utilにコード入れて関数呼び出しの方が便利

// src/utils/feather.js
import React from 'react';
import feather from 'feather-icons';

export default (name, sizeArray) => {
  const featherString = feather.icons[name].toSvg({
    width: sizeArray[0],
    height: sizeArray[1]
  });
  return <div dangerouslySetInnerHTML={{ __html: featherString }} />;
};

コンポーネントで呼び出し

// src/components/header.js
import React from "react"
import feather from '../utils/feather';

const Header = () => {
  return (
<header id="header">
  {feather('facebook', ['30', '30'])}
</header>
  )
}
export default Header
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?