# Separator

用一条水平或垂直线分隔各组内容。

在读者应当视为彼此独立的内容组之间使用分隔线，比如个人资料的标题和下方的简介。

```ts title="basic.ts"
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 }
```

用屏幕阅读器阅读这份个人资料。它会在 Hollis Grant 的姓名和邮箱，与下方的简介之间播报一条分隔线。

## 安装
```bash
npm install @ui-sdk/web @ui-sdk/core
```

导入组件会定义它的元素。在服务端，导入什么也不做。

```ts
import "@ui-sdk/web/separator"
```

## 用法
导入分隔线，把它放在两组内容之间：

```html
<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）开始：

```ts title="divider-element.ts"
import { separator } from "@ui-sdk/core/separator"
import { createParts } from "@ui-sdk/web"

createParts(separator, {
  root: { tag: "app-divider", display: "block" },
})
```

```html
<app-divider orientation="vertical"></app-divider>
```

`app-divider` 保留现成 `ui-separator` 的行为、状态和数据属性：`separator` 角色、`aria-orientation` 和 `data-orientation`。更多内容见[渲染](/docs/web-components/rendering)和[自定义部件](/docs/web-components/custom-parts)。

## 垂直
要在一行中的各项之间画线，设置 `orientation="vertical"`。它会占满这一行的高度。

```ts title="vertical.ts"
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 }
```

横向阅读 Privacy、Terms 和 Cookies。屏幕阅读器会在每个链接之间播报一条垂直分隔线。

## 堆叠内容
垂直分隔线会拉伸到这一行中最高的项，因此能跨越一个数字和它的标签。

```ts title="menu.ts"
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 }
```

比较这些线与统计数据。每条线都贯穿 Projects、Followers 和 Following 的整个高度。

## 列表行
在每两行之间放一条分隔线，把明细列表分隔开。

```ts title="list.ts"
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 }
```

用屏幕阅读器阅读套餐详情。它会在 Plan、Seats 和 Renews 之间播报一条分隔线。

## 装饰性
当线条不增加信息时，设置 `decorative`。这里的“or”一词已经说明两种登录方式是分开的，所以屏幕阅读器会跳过这些线条。

```ts title="decorative.ts"
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 }
```

用屏幕阅读器从 Sign In with Passkey 读到 Email Me a Link。它会读出“or”，并跳过这些线条。

装饰性分隔线会得到 `role="none"`，没有 `aria-orientation`。它保留 `data-orientation` 用于设置样式。

## 自定义渲染
设置 `as-child`，把分隔线绑定到它的第一个子元素，比如 `<ui-separator as-child><hr /></ui-separator>`。为部件选择绑定元素的方法见[部件与组合](/docs/web-components/parts-and-composition)。

## 样式
分隔线不带样式。给它一个背景，水平时给它高度，垂直时给它宽度，并用 `data-orientation` 区分两者。垂直的线用 `align-self: stretch` 占满所在行的高度。

分隔线的 `data-slot` 是 `separator`，它的 `data-orientation` 列在[数据属性](https://ui-sdk.cn/docs/web-components/components/separator#data-attributes)下。

## 数据属性
### `<ui-separator>`
`[data-slot="separator"]`

- `data-orientation` (`horizontal`, `vertical`): Which 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.

### 状态
同样的状态会以数据属性的形式显示在元素上。

- `orientation` (`SeparatorOrientation`): Which way the line runs.

## 无障碍
组件遵循 [WAI-ARIA separator 角色](https://www.w3.org/TR/wai-aria-1.2/#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.

Runs in React、Vue 和 web components.
