2
1

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.

Elixir Phoenix でのbootstrap iconの使い方

Last updated at Posted at 2021-08-06

PhoneixでBootstrap Iconsを使う

liveviewやlivecomponentでアイコンを使いたくなったのですが、

hexに登録されているライブラリがあまり使いやすくなかったため、別のやり方を探していたら、

↑phoenixでbootstrap iconsの使い方を説明している記事を見つけました。

3ステップでめちゃくちゃ簡単にbootstrap iconsが使えるようになるみたいです。

手順

①mixプロジェクトのままのディレクトリでbootstrap-iconsをインストール

npm --prefix=assets install bootstrap-icons file-loader --save-dev

②スタイルシートにCSSとwebfontsをインポート

assets/css/app.scss
+ @import '../node_modules/bootstrap-icons/font/bootstrap-icons.css';
+
+ @font-face {
+   font-family: 'bootstrap-icons';
+   src: font-url('../node_modules/bootstrap-icons/font/fonts/bootstrap-icons.woff2') format('woff2'),
+     font-url('../node_modules/bootstrap-icons/font/fonts/bootstrap-icons.woff') format('woff');
+ }

③webfontsのfile-loaderを設定する

assets/webpack.config.js
           test: /\.[s]?css$/,
           use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
         },
+        {
+          test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
+          use: [
+            {
+              loader: 'file-loader',
+              options: {
+                name: '[name].[ext]',
+                outputPath: 'fonts/',
+              },
+            },
+          ],
+        },
       ],
     },
     plugins: [

あとは下記のようにすればok

<i class="bi bi-plus-circle"></i>

  
  

他のやり方もあるみたいです↓

注意事項

tailwindcssも使っている場合、bootstrap-iconsとの併用が原因でtailwindcssが動かなくなるエラーがありました。
tailwindcssと併用する際にはご注意ください。

備考

hex packageでsurface_heroiconsであれば、mix.exsのdepsに追加するだけでheroiconsの豊富なiconを使用できます。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?