跳到內容

Symfony UX 圖示

編輯此頁面

symfony/ux-icons 套件提供簡單直覺的方式,在您的 Symfony 應用程式中渲染 SVG 圖示。它提供了一個 Twig 函式,可以從您的模板中包含任何本機或遠端圖示。

UX 圖示讓您可以直接存取超過 200,000 個來自熱門圖示集的向量圖示,例如 FontAwesome、Bootstrap Icons、Tabler Icons、Google Material Design Icons 等。

安裝

1
$ composer require symfony/ux-icons

SVG 圖示

SVG (可縮放向量圖形) 是一種基於 XML 的向量圖像格式,允許解析度獨立的圖形,同時保持檔案大小較小。SVG 圖示可以嵌入在 HTML 程式碼中,使用 CSS 設定樣式,並使用 JavaScript 動畫化。

UX 圖示讓您可以使用來自最熱門圖示集的圖示,同時也讓您能夠彈性地組合自己的集合,混合來自不同集合和您自己的圖示。

圖示名稱

圖示使用唯一的識別碼來參照,該識別碼遵循以下語法之一

  • prefix:name (例如 mdi:checkbi:checkeditor:align-left)
  • name 僅限 (例如 checkclosemenu)

圖示 name 與不含副檔名的檔案名稱相同 (例如 check.svg -> check)。

注意

名稱必須符合標準的 slug 格式:[a-z0-9-]+(-[0-9a-z])+

根據您的設定,prefix 可以是圖示集的名稱、圖示所在目錄的名稱,或兩者的組合。

例如,bi 前綴指的是 Bootstrap Icons 集,而 header 前綴指的是位於 header 目錄中的圖示。

載入圖示

1
2
3
4
5
6
7
8
9
10
{# includes the contents of the 'assets/icons/user-profile.svg' file in the template #}
{{ ux_icon('user-profile') }}

{# icons stored in subdirectories must use the 'subdirectory_name:file_name' syntax
   (e.g. this includes 'assets/icons/admin/user-profile.svg') #}
{{ ux_icon('admin:user-profile') }}

{# this downloads the 'user-solid.svg' icon from the 'Flowbite' icon set via ux.symfony.com
   and embeds the downloaded SVG contents in the template #}
{{ ux_icon('flowbite:user-solid') }}

注意

若要透過 ux.symfony.com/icons 搜尋和下載圖示,您的應用程式中必須安裝 symfony/http-client 套件

1
$ composer require symfony/http-client

ux_icon() 函式定義了第二個選用引數,您可以在其中定義新增至 <svg> 元素的 HTML 屬性

1
2
3
4
5
{{ ux_icon('user-profile', {class: 'w-4 h-4'}) }}
{# renders <svg class="w-4 h-4"> ... </svg> #}

{{ ux_icon('user-profile', {height: '16px', width: '16px', 'aria-hidden': true}) }}
{# renders <svg height="16" width="16" aria-hidden="true"> ... </svg> #}

圖示集

有許多可用的圖示集,每個圖示集都有自己獨特的樣式和圖示集,為不同的用途提供各種圖示,同時在您的應用程式中保持一致的外觀和風格。以下是一些最受歡迎的可用圖示集

圖示集 圖示 授權 前綴 範例
Bootstrap Icons 2000 MIT bi bi:check
Boxicons 1700 MIT bx bx:check
Flowbite 1000 MIT flowbite flowbite:check
FontAwesome Icons 7000 CC BY 4.0 fa6-regular fa6-regular:check
Heroicons 300 MIT heroicons heroicons:check
Iconoir 1500 MIT iconoir iconoir:check
Ionicons 1300 MIT ion ion:check
Lucide Icons 1500 MIT lucide lucide:check
Material Design Icons 5000 Apache 2 mdi mdi:check
Octicons 600 MIT octicon octicon:check
Phosphor Icons 7000 MIT ph ph:check
Tabler Icons 5200 MIT tabler tabler:check

若要查看可用圖示集的完整清單,請造訪 ux.symfony.com/icons

搜尋圖示集

您可以使用 ux:icons:search 命令來搜尋圖示集,或尋找特定圖示集的前綴

1
2
3
4
5
6
7
8
9
10
11
$ php bin/console ux:icons:search tabler

 -------------- ------- --------- -------- --------------
  Icon set       Icons   License   Prefix   Example
 -------------- ------- --------- -------- --------------
  Tabler Icons    5219   MIT       tabler   tabler:alien
 -------------- ------- --------- -------- --------------

Search "arrow" in Tabler Icons icons:

 php bin/console ux:icons:search tabler arrow

搜尋圖示

您也可以在特定的圖示集中搜尋圖示。若要在 "Tabler Icons" 集中搜尋 "arrow" 圖示,請使用以下命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ php bin/console ux:icons:search tabler arrow

Searching Tabler Icons icons "arrow"...
Found 64 icons.
 ------------------------------------------ ------------------------------------------
  tabler:archery-arrow                       tabler:arrow-autofit-up
  tabler:arrow-back                          tabler:arrow-back-up
  tabler:arrow-badge-down                    tabler:arrow-badge-up
  tabler:arrow-badge-up-filled               tabler:arrow-bar-both
  tabler:arrow-bar-down                      tabler:arrow-bar-left
  tabler:arrow-bar-right                     tabler:arrow-bar-to-up
  tabler:arrow-bar-up                        tabler:arrow-bear-left
  tabler:arrow-big-down                      tabler:arrow-big-down-filled
  tabler:arrow-big-down-line                 tabler:arrow-big-left
  tabler:arrow-big-left-filled               tabler:arrow-big-left-line
  tabler:arrow-big-right                     tabler:arrow-big-right-filled
  tabler:arrow-big-right-line                tabler:arrow-big-up
 ------------------------------------------ ------------------------------------------

 Page 1/3. Continue? (yes/no) [yes]:
 >

HTML 語法

除了先前章節中說明的 ux_icon() 函式之外,此套件也支援基於 <twig:ux:icon> 標籤的替代 HTML 語法

1
2
3
4
5
6
7
8
9
<!-- renders "user-profile.svg" -->
<twig:ux:icon name="user-profile" class="w-4 h-4" />
<!-- renders "admin/user-profile.svg" -->
<twig:ux:icon name="admin:user-profile" class="w-4 h-4" />
<!-- renders 'user-solid.svg' icon from 'Flowbite' icon set via ux.symfony.com -->
<twig:ux:icon name="flowbite:user-solid" />

<!-- you can also add any HTML attributes -->
<twig:ux:icon name="user-profile" height="16" width="16" aria-hidden="true" />

提示

若要使用 HTML 語法,您的專案中必須安裝 symfony/ux-twig-component 套件。

下載圖示

此套件不包含任何圖示,但提供超過 200,000 個開放原始碼圖示的存取權。

本機 SVG 圖示

如果您已經有要在專案中使用的 SVG 圖示檔案,請將它們儲存在 assets/icons/ 目錄中並提交。檔案名稱會用作圖示的名稱 (icon_name.svg 將命名為 icon_name)。如果位於子目錄中,名稱將會是 subdirectory:icon_name

1
2
3
4
5
6
7
8
9
10
11
12
13
your-project/
├─ assets/
│  └─ icons/
│     ├─ bi/
│     │  └─ pause-circle.svg
│     │  └─ play-circle.svg
│     ├─ header/
│     │  ├─ logo.svg
│     │  └─ menu.svg
│     ├─ close.svg
│     ├─ menu.svg
│     └─ ...
└─ ...

隨需圖示

ux.symfony.com/icons 有一個龐大的可搜尋圖示儲存庫,其中包含來自許多不同圖示集的圖示。此套件提供一種隨需包含此網站上找到的任何圖示的方法

  1. 造訪 ux.symfony.com/icons 並搜尋您想要使用的圖示。找到您喜歡的圖示後,複製提供的程式碼片段之一。
  2. 將程式碼片段貼到您的 Twig 模板中,圖示將會自動擷取 (並快取)。

就這樣。這透過使用 Iconify API (其中 ux.symfony.com/icons 是它的前端) 來擷取圖示並就地渲染。然後快取此圖示以供未來對相同圖示的請求使用。

注意

相同名稱的本機 SVG 圖示將優先於隨需圖示。

匯入圖示

雖然隨需圖示在開發期間很棒,但它們需要 HTTP 請求來擷取圖示,並且總是使用圖示的最新版本。圖示將來可能會變更或移除。此外,如果使用許多隨需圖示,快取預熱程序將會花費更長的時間。

這就是為什麼此套件提供一個命令,將開放原始碼圖示下載到 assets/icons/ 目錄中。您可以將匯入圖示視為鎖定它 (類似於 composer.lock 如何鎖定您的相依性)

1
2
3
4
5
6
# icon will be saved in `assets/icons/flowbite/user-solid.svg` and you can
# use it with the name: `flowbite:user-solid`
$ php bin/console ux:icons:import flowbite:user-solid

# it's also possible to import several icons at once
$ php bin/console ux:icons:import flowbite:user-solid flowbite:home-solid

注意

匯入的圖示必須提交到您的儲存庫。

鎖定隨需圖示

您可以透過執行以下命令,鎖定 (匯入) 您在專案中使用的所有隨需圖示

1
$ php bin/console ux:icons:lock

此命令僅匯入本機尚不存在的圖示。您可以使用 --force 選項強制報告覆寫現有圖示

1
$ php bin/console ux:icons:lock --force

渲染圖示

1
2
3
4
5
6
7
8
9
10
{# includes the contents of the 'assets/icons/user-profile.svg' file in the template #}
{{ ux_icon('user-profile') }}

{# icons stored in subdirectories must use the 'subdirectory_name:file_name' syntax
   (e.g. this includes 'assets/icons/admin/user-profile.svg') #}
{{ ux_icon('admin:user-profile') }}

{# this downloads the 'user-solid.svg' icon from the 'Flowbite' icon set via ux.symfony.com
   and embeds the downloaded SVG contents in the template #}
{{ ux_icon('flowbite:user-solid') }}

HTML 語法

1
2
3
4
5
6
7
8
9
10
<twig:ux:icon name="user-profile" />

{# Renders "user-profile.svg" #}
<twig:ux:icon name="user-profile" class="w-4 h-4" />

{# Renders "sub-dir/user-profile.svg" (sub-directory) #}
<twig:ux:icon name="sub-dir:user-profile" class="w-4 h-4" />

{# Renders "flowbite:user-solid" from ux.symfony.com #}
<twig:ux:icon name="flowbite:user-solid" />

注意

使用 HTML 語法需要 symfony/ux-twig-component

預設屬性

您可以在設定中設定所有圖示的預設屬性。這些屬性將會新增至所有圖示,除非被 ux_icon 函式的第二個引數覆寫。

1
2
3
4
# config/packages/ux_icons.yaml
ux_icons:
    default_icon_attributes:
        fill: currentColor

現在,所有圖示的 fill 屬性預設都會設定為 currentColor

1
2
3
4
5
# renders "user-profile.svg" with fill="currentColor"
{{ ux_icon('user-profile') }}

# renders "user-profile.svg" with fill="red"
{{ ux_icon('user-profile', {fill: 'red'}) }}

圖示別名

2.20

圖示別名功能已在 2.20 中新增。

別名是您可以定義的自訂名稱,用來參照任何圖示。它們對於建立您在模板中經常使用的圖示捷徑非常有用

1
2
3
4
5
# config/packages/ux_icons.yaml
ux_icons:
    # ...
    aliases:
        dots: 'clarity:ellipsis-horizontal-line'

現在,您可以在模板中使用 dots 別名

1
2
3
4
5
6
7
8
{{ ux_icon('dots') }}
{# with the previous configuration, this is the same as: #}
{{ ux_icon('clarity:ellipsis-horizontal-line') }}

{# using the HTML syntax #}
<twig:ux:icon name="dots" />
{# same as: #}
<twig:ux:icon name="clarity:ellipsis-horizontal-line" />

錯誤

如果找不到圖示,則會擲回例外。這在開發期間很有用,但在生產環境中,您可能想要改為渲染錯誤訊息。您可以透過將 ignore_not_found 設定選項設定為 true 來執行此操作

1
2
3
# config/packages/ux_icons.yaml
ux_icons:
    ignore_not_found: true

協助工具

圖示為您的網站新增視覺元素,它們對於協助工具可能是一項挑戰。根據 W3C 關於 SVG 圖示協助工具的指南,有三種方法可以改善圖示的協助工具,具體取決於上下文。

資訊型圖示

它們傳達資訊或功能。它們應定義文字替代方案,透過螢幕閱讀器和其他輔助技術使用的 aria-label 屬性呈現相同的內容或功能

1
2
Today's weather:
{{ ux_icon('cloud-rain', {'aria-label': 'Rainy weather'}) }}
功能型圖示

它們是互動式的並執行功能。它們應定義文字替代方案,透過螢幕閱讀器和其他輔助技術使用的 aria-label 屬性呈現相同的內容或功能

1
{{ ux_icon('user-profile', {class: 'w-4 h-4', 'aria-label': 'User Profile'}) }}
裝飾型圖示

它們純粹是裝飾性的,不傳達任何意義或功能。它們應使用 aria-hidden 屬性從螢幕閱讀器中隱藏。

1
2
3
4
5
6
<a href="/profile">
    <svg viewBox="0 0 24 24" class="w-4 h-4" aria-hidden="true">
        <!-- ... -->
    </svg>
    Back to profile
</a>

這就是為什麼 ux_icon() 函式和 <twig:ux:icon> 元件會自動aria-hidden="true" 屬性新增至未至少具有以下屬性之一的圖示:aria-labelaria-labelledbytitle

注意

如果您不想為特定圖示設定 aria-hidden="true",您可以明確地將 aria-hidden 屬性設定為 false

1
<twig:ux:icon name="user-profile" aria-hidden="false" />

效能

UX 圖示元件旨在快速。以下是一些為確保最佳效能而進行的最佳化。

快取

隨需 VS 匯入

雖然隨需圖示在開發期間很棒,但它們需要 HTTP 請求來擷取圖示,並且總是使用圖示的最新版本。圖示將來可能會變更或移除。此外,如果使用許多 _隨需_ 圖示,快取預熱程序將會花費更長的時間。您可以將匯入圖示視為鎖定它 (類似於 composer.lock _鎖定_您的相依性)。

圖示快取

為了避免在每次請求時都必須剖析圖示檔案,圖示會被快取。在生產環境中,您可以執行以下命令來預熱快取

1
$ php bin/console ux:icons:warm-cache

此命令會在您的所有 Twig 模板中尋找 ux_icon() 呼叫和 <twig:ux:icon> 標籤,並快取找到的圖示。

注意

名稱動態建立的圖示將不會被快取。建議在您的模板中將圖示名稱設為字串文字。

1
2
3
4
5
6
7
8
9
{# This will be cached #}
{{ ux_icon('flag-fr') }}

{# This will NOT be cached #}
{{ ux_icon('flag-' ~ locale) }}

{# in this example, both "flag-fr" and "flag-de" will be cached #}
{% set flags = {fr: 'flag-fr', de: 'flag-de'} %}
{{ ux_icon(flags[locale]) }}

注意

在開發期間,如果您修改圖示,您需要清除快取 (bin/console cache:clear) 才能看到變更。

提示

如果使用 symfony/asset-mapper,則在執行 asset-map:compile 時會自動預熱快取。

TwigComponent

ux_icon() 函式已最佳化為盡可能快速。為了在使用 HTML 語法 (<twig:ux:icon name="..." />) 時提供相同的效能水準,TwigComponent 額外負荷會透過立即呼叫 IconRenderer 並傳回 HTML 輸出而降低。

警告

<twig:ux:icon> 元件不支援嵌入內容。

1
2
3
4
5
6
7
{# The 🧸 will be ignored in the HTML output #}
<twig:ux:icon name="user-profile" class="w-4 h-4">🧸</twig:ux:icon>

{# Renders "user-profile.svg" #}
<svg viewBox="0 0 24 24" class="w-4 h-4">
    <path fill="currentColor" d="M21 7L9 19l-5.5-5.5l1.41-1.41L9 16.17L19.59 5.59z"/>
</svg>

設定

UX 圖示與 Symfony 應用程式無縫整合。所有這些選項都在您的應用程式設定中的 ux_icons 金鑰下設定。

1
2
3
# config/packages/ux_icons.yaml
ux_icons:
    {# ... #}

偵錯設定

1
2
3
4
5
# Displays the default config values
$ php bin/console config:dump-reference ux_icons

# Displays the actual config values used by your application
$ php bin/console debug:config ux_icons

完整設定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# config/packages/ux_icons.yaml
ux_icons:
    # The local directory where icons are stored
    icon_dir: '%kernel.project_dir%/assets/icons'

    # Default attributes to add to all icons
    default_icon_attributes:
        fill: currentColor
        'font-size': '1.25em'

    # Icon aliases (alias => icon name)
    aliases:
        dots: 'clarity:ellipsis-horizontal-line'
        'tabler:save': 'tabler:device-floppy'

    # Configuration for the "on demand" icons powered by Iconify.design
    iconify:
       enabled: true

       # Whether to use the "on demand" icons powered by Iconify.design
       on_demand: true

       # The endpoint for the Iconify API
       endpoint: 'https://api.iconify.design'

    # Whether to ignore errors when an icon is not found
    ignore_not_found: false
這項作品,包括程式碼範例,根據 Creative Commons BY-SA 3.0 授權條款授權。
TOC
    版本