Web Origamiでアイコンセットを簡単に作成
本文の状態
日本語全文を表示中
詳細モードで約4分の本文を読めます。
同じ出来事の情報源
この情報源を基点に整理
Andrej Karpathy 厳選
ブログで様々なアイコンセットを使用してきた経験から、現在はHeroiconsを活用。Web Origamiを使うことで、アイコンセットの管理と実装が効率化されます。
Source Article
元記事を日本語で読む
本文に関係しない購読案内、埋め込み通知、サイト内プロモーションは除いています。
長年にわたり、私はブログでさまざまなアイコンセットを使用してきました。現在使用しているのは Heroicons です。
推奨される使い方は、ウェブサイトからソースをコピーして直接 HTML に貼り付けることです。非常にシンプルなプロセスです:
ウェブサイトにアクセスする
目的のアイコンを検索する
アイコンにカーソルを合わせる
「Copy SVG」をクリックする
IDE に戻って貼り付ける
React や Vue を使用している場合、npm パッケージをインストールしてアイコンをコンポーネントとしてインポートすることも可能です。しかし、私はどちらのフレームワークも使用していないため、生(raw)の SVG が必要であり、npm install のような方法では対応できません。
過去には、プロジェクト内に個別のファイルとして SVG をコピーするアプローチをとっていました。例えば:
src/icons/
home.svg
about.svg
search.svg
そして、これらのアイコンをディスクから読み取るための「コンポーネント」を作成し、テンプレートファイルで使用して HTML 内で SVG をインライン化していました。例としては以下のようになります。
// 某ページテンプレートファイル
import { Icon } from './Icon.js'
const template = ${Icon('search.svg')} Search
// Icon.js
import fs from 'fs'
import path from 'path'
const __dirname = /* ファイルパスを正しく解決するための処理 */
export const Icon = (name) => fs.readFileSync(
path.join(__dirname, 'icons', name),
'utf8'
).toString();
これは問題なく動作します。ディスクからファイルを読み取るために、多くの Node.js のボイラープレートが必要ですが、アイコンを変更する際には少し手間がかかります。新しい SVG を見つけ、既存のものを上書きし、ソース管理に再度コミットするなどする必要があります。
もしかすると、「npm install heroicons」のように簡単にできるのが理想なのかもしれません。
アイコンパックによって名称が異なるため、切り替えると名称が一致しません。例えば、あるアイコンは「search」という名前で呼ばれているかもしれません。
アイコンパックは非常に大きくなりがちですが、実際にはその一部しか必要としません。npm i icon-pack
そのため、プロジェクトの npm パッケージでは生の SVG ファイルは提供されていません。ウェブサイト上では入手可能ですが、よりプログラム的に必要なアイコンを簡単に取得する方法が欲しいのです。どうすればよいでしょうか?Origami の登場です。
私はブログで Web Origami を使用しており、これにより、Github にホストされている Heroicons 内のアイコンを、私がテンプレート内で使用するアイコンにマッピングすることが容易になります。npm install は不要です。
git submodule add
{ home.svg: https://raw.githubusercontent.com/tailwindlabs/heroicons/refs/heads/master/optimized/24/outline/home.svg, about.svg: https://raw.githubusercontent.com/tailwindlabs/heroicons/refs/heads/master/optimized/24/outline/question-mark-circle.svg, search.svg: https://raw.githubusercontent.com/tailwindlabs/heroicons/refs/heads/master/optimized/24/outline/magnifying-glass.svg }
ご覧の通り、私はアイコンに「search」のような名前を付けています。
美しい不是吗?これは、裸のモジュール指定子を URL にマッピングできる import maps や、Deno の半ば放棄された HTTP インポート(これもまた独自の美しさを持っていました)を連想させます。仕組みについて
折り紙は、ファイルパスを言語の第一級市民にします——「リモート」ファイルパスさえも——ので、コードベース内のアイコン名を、npm 経由でディスクにインストールされるかインターネットから取得されるかにかかわらず、誰かのセットにある他の人のアイコン名にマッピングする単一のファイルを作成するのは非常に簡単です。先ほどの例を簡略化するために、icons.ori というようなファイルを持つことができます。
{ home.svg: https://example.com/path/to/home.svg about.svg: https://example.com/path/to/information-circle.svg search.svg: https://example.com/path/to/magnifying-glass.svg }
その後、テンプレート内で次のようにこれらのアイコンを参照できます:${icons.ori/home.svg} 検索
簡単です!そして、アイコンを変更したいときは、単に icons.ori のエントリを更新するだけです。
そして、もし本当に一歩先を行きたいなら、折り紙のキャッシュ機能を使用できます:Tree.cache( { home.svg: https://raw.github.com/path/to/home.svg about.svg: https://raw.github.com/path/to/information-circle.svg search.svg: https://raw.github.com/path/to/magnifying-glass.svg }, Origami.projectRoot()/cache )
単にファイルをメモリ内にキャッシュするだけでなく、これは以下のようなローカルフォルダにキャッシュします:cache/ home.svg about.svg search.svg
原文を表示
Over the years, I’ve used different icon sets on my blog. Right now I use Heroicons.The recommended way to use them is to copy/paste the source from the website directly into your HTML. It’s a pretty straightforward process:Go to the websiteSearch for the icon you wantHover itClick to “Copy SVG”Go back to your IDE and paste itIf you’re using React or Vue, there are also npm packages you can install so you can import the icons as components.But I’m not using either of those frameworks, so I need the raw SVGs and there’s no npm i
In the past, my approach has been to copy the SVGs into individual files in my project, like:src/ icons/ home.svg about.svg search.svg
Then I have a “component” for reading those icons from disk which I use in my template files to inline the SVGs in my HTML. For example:// Some page template file import { Icon } from './Icon.js' const template = `${Icon('search.svg')} Search
` // Icon.js import fs from 'fs' import path from 'path' const __dirname = /* Do the stuff to properly resolve the file path */; export const Icon = (name) => fs.readFileSync( path.join(__dirname, 'icons', name), 'utf8' ).toString();
It’s fine. It works. It’s a lot of node boilerplate to read files from disk.But changing icons is a bit of a pain. I have to find new SVGs, overwrite my existing ones, re-commit them to source control, etc.I suppose it would be nice if I could just npm i heroicons
Names are different between icon packs, so when you switch, names don’t match. For example, an icon might be named search
magnifying-glass
Icon packs are often quite large and you only need a subset. npm i icon-pack
So the project’s npm packages don’t provide the raw SVGs. The website does, but I want a more programatic way to easily grab the icons I want.How can I do this?Enter Origami
I’m using Web Origami for my blog which makes it easy to map icons I use in my templates to Heroicons hosted on Github. It doesn’t require an npm install
git submodule add
{ home.svg: https://raw.githubusercontent.com/tailwindlabs/heroicons/refs/heads/master/optimized/24/outline/home.svg, about.svg: https://raw.githubusercontent.com/tailwindlabs/heroicons/refs/heads/master/optimized/24/outline/question-mark-circle.svg, search.svg: https://raw.githubusercontent.com/tailwindlabs/heroicons/refs/heads/master/optimized/24/outline/magnifying-glass.svg }
As you can see, I name my icon (e.g. search
Beautiful, isn’t it? It kind of reminds me of import maps where you can map a bare module specifier to a URL (and Deno’s semi-abandoned HTTP imports which were beautiful in their own right).How It Works
Origami makes file paths first-class citizens of the language — even “remote” file paths — so it’s very simple to create a single file that maps your icon names in a codebase to someone else’s icon names from a set, whether those are being installed on disk via npm or fetched over the internet.To simplify my example earlier, I can have a file like icons.ori
{ home.svg: https://example.com/path/to/home.svg about.svg: https://example.com/path/to/information-circle.svg search.svg: https://example.com/path/to/magnifying-glass.svg }
Then I can reference those icons in my templates like this:${icons.ori/home.svg} Search
Easy-peasy! And when I want to change icons, I simply update the entries in icons.ori
And if you really want to go the extra mile, you can use Origami’s caching feature:Tree.cache( { home.svg: https://raw.github.com/path/to/home.svg about.svg: https://raw.github.com/path/to/information-circle.svg search.svg: https://raw.github.com/path/to/magnifying-glass.svg }, Origami.projectRoot()/cache )
Rather than just caching the files in memory, this will cache them to a local folder like this:cache/ home.svg about.svg search.svg
関連記事
今日のまとめ
AIデイリーブリーフで今日の重要ニュースをまとめ読み