@canonical/svelte-icons

中文文档
0.1.0发布于 2026/10/23LGPL-3.0TypeScriptES Module零依赖构建来源已验证近 30 天下载 2397

Canonical's icons as Svelte components

原始使用文档
文档目录9 节
  1. Installation
  2. Usage
  3. Basic Usage
  4. Styling Icons
  5. Performance Optimization
  6. Custom Icons
  7. Icon Source
  8. Development
  9. Generating icon metadata

@canonical/svelte-icons

Canonical's design system icons packaged as Svelte 5 components. This library provides all icons from Canonical's Pragma design system as easy-to-use Svelte components.

[!WARNING] This package is it's experimental stage. It may undergo significant changes before reaching a stable release. It is not recommended for production use at this time.

Installation

bun add @canonical/svelte-icons

Usage

Basic Usage

Import icons individually and use them as components:

<script>
  import { HomeIcon, SettingsIcon, UserIcon } from "@canonical/svelte-icons";
</script>

<HomeIcon />
<SettingsIcon class="custom-class" />
<UserIcon />

Styling Icons

Icons by default inherit the current text color and size.

<script>
  import { SearchIcon } from "@canonical/svelte-icons";
</script>

<SearchIcon style="color: red; width: 2em; height: 2em;" />

Performance Optimization

When using the same icon multiple times, you may use IconsOptimizationProvider to deduplicate SVG definitions in the generated markup:

<script>
  import { IconsOptimizationProvider, HomeIcon } from "@canonical/svelte-icons";
</script>

<IconsOptimizationProvider>
  <nav>
    <HomeIcon />
    <!-- All instances share the same SVG definition -->
    <HomeIcon />
    <HomeIcon />
  </nav>
</IconsOptimizationProvider>

This is especially useful when rendering the same icon many times.

Check out the Node tree size comparison story to see the DOM size difference between optimized and non-optimized rendering.

Custom Icons

You can also create custom icon components using the IconBase component:

<script>
  import { IconBase } from "@canonical/svelte-icons";
</script>

<IconBase iconName="my-custom-icon">
  <svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
    <circle cx="8" cy="8" r="6" stroke="black" stroke-width="1" fill="red" />
  </svg>
</IconBase>

[!IMPORTANT] When using custom icons with IconsOptimizationProvider:

  • ensure that the iconName prop is unique for each custom icon to avoid collisions,
  • modifying the SVG content inside IconBase during runtime may lead to unexpected behaviors.

Check out the Reactivity playground story to learn more.

Icon Source

All SVG icons are sourced from the @canonical/ds-assets package, which is part of Canonical's Pragma design system.

Development

# Install dependencies
bun install

# Build icon components from source
bun run build

# Run Storybook for development
bun run storybook

# Lint, type check and verify metadata.json is up to date
bun run check

Generating icon metadata

metadata.json holds search tags for every icon, generated by a vision model from the rendered icon and its name. Each entry also stores a hash of the icon's SVG, so the generator only calls the model for icons that are new, have changed, or have no tags yet; all other tags are reused. Entries for icons that no longer exist are removed.

bun run check (and CI) fails when metadata.json is out of sync with @canonical/ds-assets, so run the generator after bumping that package:

# Set OPENROUTER_API_KEY in your environment or ignored .env file.
bun run build:metadata

The generator accepts any endpoint supporting OpenAI-compatible Chat Completions. The model must support image input. By default, it uses OpenRouter with google/gemma-4-26b-a4b-it:

# Override the OpenRouter model.
bun run build:metadata --model google/gemini-3.1-flash-lite

For a custom endpoint, pass its API base URL and an explicit model:

# Ollama: start the server and download the chosen model beforehand.
bun run build:metadata --base-url http://localhost:11434/v1 --model gemma4:26b

# llama.cpp: start llama-server with your model; use its served model ID.
bun run build:metadata --base-url http://localhost:8080/v1 --model your-served-model-id

Custom endpoints use only METADATA_API_KEY when supplied, otherwise no authentication header is sent.

Icons are processed 6 at a time by default; change this with --concurrency (or -j), e.g. -j 1 for slow local servers. Requests time out after 120 seconds and are retried up to 2 times, with backoff, on timeouts, network errors, rate limits, and server errors. Increase the timeout for slow local models with --timeout <seconds>. Icons that still fail are reported at the end, the remaining results are written, and the command exits with a non-zero status.

Each generated icon is logged on a single line with its tags. Add --verbose (or -v) to also log the model's description of the drawing and its meaning.

Reused tags are normalized (lowercased, deduplicated, filler words removed). Add --clean (or -c) to regenerate tags for all icons.

版本与发布记录

@canonical/svelte-icons 共发布 10 个版本,首次发布于 2025/11/26;过去 12 个月发布了 10 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.0latest2026/10/2317 天前
0.0.92026/01/099 个月前
0.0.82025/12/0511 个月前
0.0.72025/12/0511 个月前
0.0.62025/12/0211 个月前
0.0.52025/12/0211 个月前

关于 @canonical/svelte-icons 的常见问题

@canonical/svelte-icons 是什么?

Canonical's icons as Svelte components

如何安装 @canonical/svelte-icons?

在项目目录运行 npm install @canonical/svelte-icons,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:svelte(^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@canonical/svelte-icons 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@canonical/svelte-icons 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@canonical/svelte-icons 可以商用吗?

@canonical/svelte-icons 使用 LGPL-3.0 许可。弱 Copyleft:作为库被调用时通常不要求开源你自己的代码,但修改库本身需以 LGPL 开源;前端打包合并后的适用边界存在不同理解。以上为概括,不构成法律意见,请以许可原文为准。

@canonical/svelte-icons 的下载量有多少?

根据 npm 官方下载统计,@canonical/svelte-icons 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,397 次,日均约 80 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@canonical/svelte-icons 还在维护吗?

最新版本 0.1.0 发布于 2026/10/23(17 天前);至今共发布 10 个版本,其中过去 12 个月发布了 10 个;首次发布于 2025/11/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。