Separator
用一条水平或垂直线分隔各组内容。
在读者应当视为彼此独立的内容组之间使用分隔线,比如个人资料的标题和下方的简介。
import "@ui-sdk/web/separator"
function mount(root: HTMLElement) {
root.innerHTML = `
<div data-slot="stack">
<div data-slot="stack">
<h3>Hollis Grant</h3>
<p data-slot="hint">hollis@example.com</p>
</div>
<ui-separator></ui-separator>
<p>Designs the billing and onboarding flows, and reviews every new pricing page.</p>
</div>
`
}
export { mount }安装
npm install @ui-sdk/web @ui-sdk/core导入组件会定义它的元素。在服务端,导入什么也不做。
import "@ui-sdk/web/separator"用法
导入分隔线,把它放在两组内容之间:
<script type="module">
import "@ui-sdk/web/separator"
</script>
<section>
<h2>Hollis Grant</h2>
<ui-separator></ui-separator>
<p>Designs the billing and onboarding flows.</p>
</section>自己构建
@ui-sdk/web/separator 自带一个现成的 ui-separator 元素。如果你想用自己的元素名构建自己的版本,从 @ui-sdk/core 里的 separator 原语(primitive)开始:
import { separator } from "@ui-sdk/core/separator"
import { createParts } from "@ui-sdk/web"
createParts(separator, {
root: { tag: "app-divider", display: "block" },
})<app-divider orientation="vertical"></app-divider>app-divider 保留现成 ui-separator 的行为、状态和数据属性:separator 角色、aria-orientation 和 data-orientation。更多内容见渲染和自定义部件。
垂直
要在一行中的各项之间画线,设置 orientation="vertical"。它会占满这一行的高度。
import "@ui-sdk/web/separator"
function mount(root: HTMLElement) {
root.innerHTML = `
<nav aria-label="Legal" data-slot="row">
<a href="#">Privacy</a>
<ui-separator orientation="vertical"></ui-separator>
<a href="#">Terms</a>
<ui-separator orientation="vertical"></ui-separator>
<a href="#">Cookies</a>
</nav>
`
}
export { mount }堆叠内容
垂直分隔线会拉伸到这一行中最高的项,因此能跨越一个数字和它的标签。
import "@ui-sdk/web/separator"
function mount(root: HTMLElement) {
root.innerHTML = `
<div data-slot="row">
<div data-slot="stack">
<span>24</span>
<span data-slot="hint">Projects</span>
</div>
<ui-separator orientation="vertical"></ui-separator>
<div data-slot="stack">
<span>1,280</span>
<span data-slot="hint">Followers</span>
</div>
<ui-separator orientation="vertical"></ui-separator>
<div data-slot="stack">
<span>312</span>
<span data-slot="hint">Following</span>
</div>
</div>
`
}
export { mount }列表行
在每两行之间放一条分隔线,把明细列表分隔开。
import "@ui-sdk/web/separator"
function mount(root: HTMLElement) {
root.innerHTML = `
<div data-slot="stack">
<dl>
<dt>Plan</dt>
<dd>Pro</dd>
</dl>
<ui-separator></ui-separator>
<dl>
<dt>Seats</dt>
<dd>8 of 10</dd>
</dl>
<ui-separator></ui-separator>
<dl>
<dt>Renews</dt>
<dd>December 1, 2026</dd>
</dl>
</div>
`
}
export { mount }装饰性
当线条不增加信息时,设置 decorative。这里的“or”一词已经说明两种登录方式是分开的,所以屏幕阅读器会跳过这些线条。
import "@ui-sdk/web/separator"
function mount(root: HTMLElement) {
root.innerHTML = `
<div data-slot="stack">
<button type="button" data-variant="solid">Sign In with Passkey</button>
<div data-slot="row">
<ui-separator decorative></ui-separator>
<span>or</span>
<ui-separator decorative></ui-separator>
</div>
<button type="button">Email Me a Link</button>
</div>
`
}
export { mount }装饰性分隔线会得到 role="none",没有 aria-orientation。它保留 data-orientation 用于设置样式。
自定义渲染
设置 as-child,把分隔线绑定到它的第一个子元素,比如 <ui-separator as-child><hr /></ui-separator>。为部件选择绑定元素的方法见部件与组合。
样式
分隔线不带样式。给它一个背景,水平时给它高度,垂直时给它宽度,并用 data-orientation 区分两者。垂直的线用 align-self: stretch 占满所在行的高度。
分隔线的 data-slot 是 separator,它的 data-orientation 列在数据属性下。
数据属性
<ui-separator>
[data-slot="separator"]
| 属性 | 含义 |
|---|---|
data-orientation | horizontalverticalWhich way the line runs. |
API 参考
props 就是属性,比如 orientation="vertical" 和 decorative。分隔线没有事件,也没有 api。
Separator
orientation?SeparatorOrientation- Which way the line runs. A vertical separator divides items in a row.
decorative?boolean- Hides the separator from assistive technology with
role="none", for a line that's only visual. Leave it off when the separator divides content a screen reader should hear as separate, like groups of menu items. debug?boolean- Logs every state change with its reason to the console, in development builds.
状态
同样的状态会以数据属性的形式显示在元素上。
orientationSeparatorOrientation- Which way the line runs.
无障碍
组件遵循 WAI-ARIA separator 角色。分隔线用来分开屏幕阅读器应当分别读出的内容,它通过 role="separator" 和对应的 aria-orientation 播报。装饰性分隔线只是视觉元素:它得到 role="none",所以屏幕阅读器会跳过它。只有周围内容已经表达了这种分隔时,才使用 decorative。无论哪种情况,分隔线都不会获得焦点,所以 Tab 会越过它。
经过测试的行为
下面每一行都是一项自动化测试。
- A separator is announced with its orientation.
- A separator can't be focused.
- A separator carries its orientation for styling.
运行于 React、Vue 和 web components。