导航

v2.1.0
内容

图标

行内图标的尺寸由所在组件设置。

本页内容

将 icon 直接加到 <svg>、<img> 或图标组件本身。组件不按元素类型为子元素设置样式,而是各自设置 --icon-size,由图标采用该值。因此,同一段图标标记在按钮、徽章和标签页中的尺寸可以不同。

同一图标,三种场景

<button type="button" class="btn btn-sm">  <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">    <path d="M3 8L13 8" /><path d="M8 3L8 13" />  </svg> Small</button><button type="button" class="btn">  <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">    <path d="M3 8L13 8" /><path d="M8 3L8 13" />  </svg> Medium</button><button type="button" class="btn btn-lg">  <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">    <path d="M4 10L16 10" /><path d="M10 4L10 16" />  </svg> Large</button>
import PlusIcon16 from '@master/icons-react/native/16/soft/plus'import PlusIcon20 from '@master/icons-react/native/20/soft/plus'<>  <button type="button" className="btn btn-sm"><PlusIcon16 className="icon" /> Small</button>  <button type="button" className="btn"><PlusIcon16 className="icon" /> Medium</button>  <button type="button" className="btn btn-lg"><PlusIcon20 className="icon" /> Large</button></>
<script>  import PlusIcon16 from '@master/icons-svelte/native/16/soft/plus'  import PlusIcon20 from '@master/icons-svelte/native/20/soft/plus'</script><button type="button" class="btn btn-sm"><PlusIcon16 class="icon" /> Small</button><button type="button" class="btn"><PlusIcon16 class="icon" /> Medium</button><button type="button" class="btn btn-lg"><PlusIcon20 class="icon" /> Large</button>
<script setup>import PlusIcon16 from '@master/icons-vue/native/16/soft/plus'import PlusIcon20 from '@master/icons-vue/native/20/soft/plus'</script><template>  <button type="button" class="btn btn-sm"><PlusIcon16 class="icon" /> Small</button>  <button type="button" class="btn"><PlusIcon16 class="icon" /> Medium</button>  <button type="button" class="btn btn-lg"><PlusIcon20 class="icon" /> Large</button></template>

#正文中的图标

在组件之外,图标的尺寸为 1em,位置略低于基线,以便与小写字母对齐。

正文中的图标

<p class="m(0) text-sm fg-text-body">  Press <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" role="img" aria-label="Download">    <path d="M8.5 9L8.4994 1.9755A0.5 0.5 0 0 0 7.5054 1.9266L7.5 2L7.5014 9.0379A0.5007 0.5007 0 0 0 7.5892 9.285A0.7044 0.7044 0 0 0 8.4108 9.285A0.4996 0.4996 0 0 0 8.5 9Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><path d="M5 6L7.8586 8.8586A0.2 0.2 0 0 0 8.1414 8.8586L11 6" /><path d="M2.5 10.5L2.5 12.5A1 1 0 0 0 3.5 13.5L12.5 13.5A1 1 0 0 0 13.5 12.5L13.5 10.5" />  </svg> to download the report.</p>
import DownloadIcon from '@master/icons-react/native/16/soft/download'<p className="m(0) text-sm fg-text-body">  Press <DownloadIcon className="icon" aria-label="Download" /> to download the report.</p>
<script>  import DownloadIcon from '@master/icons-svelte/native/16/soft/download'</script><p class="m(0) text-sm fg-text-body">  Press <DownloadIcon class="icon" aria-label="Download" /> to download the report.</p>
<script setup>import DownloadIcon from '@master/icons-vue/native/16/soft/download'</script><template>  <p class="m(0) text-sm fg-text-body">    Press <DownloadIcon class="icon" aria-label="Download" /> to download the report.  </p></template>

#Master Icons

Master UI 自己的组件和示例使用 Master Icons。每个图标都为 16、20、24px 各绘制一次,颜色使用 currentColor。icon 接受任何 SVG、图片或图标组件,因此其他图标集的用法相同:Master Icons 是默认选择,而不是必要条件。

npm install @master/icons-react --save-exact

请安装对应框架的包:@master/icons-react、-vue、-svelte、-preact 或 -astro;只需要 SVG 文件和纯 JavaScript 时安装 @master/icons。

按组件给图标的尺寸选择图稿,只导入那一张图稿,并加上 icon:

组件设置的尺寸图稿图稿线宽
17px 及以下161
18 到 22px201.25
超过 22px241.5
import SearchIcon from '@master/icons-react/native/16/soft/search'<button type="button" className="btn btn-square" aria-label="Search">  <SearchIcon className="icon" /></button>

在 HTML 中,从图标集复制 SVG 并加上 icon。名称与其他图标集不同,书写前请先查询:

node node_modules/@master/icons/skills/master-icons/scripts/search.mjs "arrow left"

#描边

图标会保留图稿自身的线宽。Master Icons 每个尺寸的线宽都是网格的十六分之一,因此图标的线宽在 16px 时为 1px,并随组件设置的尺寸等比增加。要改变 Master Icons 的粗细,请在固定入口(例如 @master/icons-react/16/soft)选择 strokeWidth:镂空和间隙会按该线宽重新绘制。

描边

<svg class="icon fg-text-strong" style="--icon-size: 1.5rem" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">  <circle cx="12" cy="12" r="9" /><path d="M12 6.5L12 11.8892A0.2 0.2 0 0 0 12.094 12.0588L16 14.5" /></svg><svg class="icon fg-text-strong" style="--icon-size: 1.5rem; --icon-stroke: 1.25" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">  <circle cx="12" cy="12" r="9" /><path d="M12 6.5L12 11.8892A0.2 0.2 0 0 0 12.094 12.0588L16 14.5" /></svg><svg class="icon fg-text-strong" style="--icon-size: 1.5rem; --icon-stroke: 2" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">  <circle cx="12" cy="12" r="9" /><path d="M12 6.5L12 11.8892A0.2 0.2 0 0 0 12.094 12.0588L16 14.5" /></svg>
import ClockIcon from '@master/icons-react/native/24/soft/clock'<>  <ClockIcon className="icon fg-text-strong" style={{ '--icon-size': '1.5rem' } as React.CSSProperties} />  <ClockIcon className="icon fg-text-strong" style={{ '--icon-size': '1.5rem', '--icon-stroke': '1.25' } as React.CSSProperties} />  <ClockIcon className="icon fg-text-strong" style={{ '--icon-size': '1.5rem', '--icon-stroke': '2' } as React.CSSProperties} /></>
<script>  import ClockIcon from '@master/icons-svelte/native/24/soft/clock'</script><ClockIcon class="icon fg-text-strong" style="--icon-size: 1.5rem" /><ClockIcon class="icon fg-text-strong" style="--icon-size: 1.5rem; --icon-stroke: 1.25" /><ClockIcon class="icon fg-text-strong" style="--icon-size: 1.5rem; --icon-stroke: 2" />
<script setup>import ClockIcon from '@master/icons-vue/native/24/soft/clock'</script><template>  <ClockIcon class="icon fg-text-strong" style="--icon-size: 1.5rem" />  <ClockIcon class="icon fg-text-strong" style="--icon-size: 1.5rem; --icon-stroke: 1.25" />  <ClockIcon class="icon fg-text-strong" style="--icon-size: 1.5rem; --icon-stroke: 2" /></template>

--icon-stroke 会替换图稿的线宽,以图稿 viewBox 的单位计算;填充图标不受影响。可用它把一组单纯的线条图标调整到界面的粗细。设置在容器上时,里面的图标都会随之改变。

#无障碍

  • 用 aria-hidden="true" 将装饰性图标从无障碍树中隐藏。
  • 仅含图标的控件应在自身上设置无障碍名称,例如在方形按钮上设置 aria-label。
  • 在句子中代替一个词的图标(例如上面的下载箭头)就是那个词:为它设置 role="img",并用 aria-label 说明它代表什么,而不是它的外观。

#自定义属性

属性默认值说明
--icon-size图标的宽度和高度,由容纳图标的组件设置。没有外层组件时为 1em
--icon-stroke替换图稿的线宽,以图稿 viewBox 的单位计算。未设置时图标保留自身的线宽。填充图标不受影响
--icon-color图标颜色。组件可设置此值来淡化图标或为图标着色;未设置时使用文本颜色

#类名

类名说明
icon由 --icon-size 设置尺寸的行内图标,在组件外尺寸为 1em,位置略低于基线。

#源码

@master/ui/components/icon.css
/* * Icon: sizes an inline icon by the --icon-size its component provides. * Put .icon on the <svg>, <img> or icon component itself. Components never reach into * their children by element type; they set --icon-size and --icon-color instead. * * The drawing keeps its own stroke. An optically sized set such as Master Icons draws each size * at its own width (1 on a 16 grid, 1.25 on 20, 1.5 on 24: a sixteenth of the grid at every size), * so a single width written here would be wrong for two of the three. --icon-stroke is the * exception a set asks for by name: when it is set, it replaces the drawing's width. */@layer components.ui {  .icon {    flex: none;    width: var(--icon-size, 1em);    height: var(--icon-size, 1em);    /* Icons carry internal whitespace; a control pulls them toward its edge by --icon-offset. */    margin-inline: var(--icon-offset, 0);    /*     * Without --icon-stroke the declaration rolls back to the stroke-width the SVG carries, and to     * the initial 1 where it carries none. With it, the value replaces the drawing's; fill icons are unaffected.     */    stroke-width: var(--icon-stroke, revert-layer);    /* A component may quiet its icons next to a strong label; by default an icon takes the text color. */    color: var(--icon-color, inherit);    vertical-align: -.125em;  }}