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

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

import { Separator } from "@ui-sdk/solid/separator"

function Example() {
  return (
    <div data-slot="stack">
      <div data-slot="stack">
        <h3>Hollis Grant</h3>
        <p data-slot="hint">hollis@example.com</p>
      </div>
      <Separator />
      <p>
        Designs the billing and onboarding flows, and reviews every new pricing
        page.
      </p>
    </div>
  )
}

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

安装

npm install @ui-sdk/solid @ui-sdk/core

用法

导入分隔线,把它放在两组内容之间:

import { Separator } from "@ui-sdk/solid/separator"

function Profile() {
  return (
    <section>
      <h2>Hollis Grant</h2>
      <Separator />
      <p>Designs the billing and onboarding flows.</p>
    </section>
  )
}

export { Profile }

自己构建

@ui-sdk/solid/separator 自带一个现成的 Separator。如果你想用自己的名字构建自己的版本,从 @ui-sdk/core 里的 separator 原语(primitive)开始:

divider.ts
import { separator } from "@ui-sdk/core/separator"
import { createParts } from "@ui-sdk/solid/primitive"

const { Root: Divider } = createParts(separator, { root: "div" })

export { Divider }
<Divider orientation="vertical" />

Divider 保留现成 Separator 的行为、状态和数据属性:separator 角色、aria-orientation 和 data-orientation。更多内容见渲染和自定义部件。

垂直

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

import { Separator } from "@ui-sdk/solid/separator"

function Example() {
  return (
    <nav aria-label="Legal" data-slot="row">
      <a href="#">Privacy</a>
      <Separator orientation="vertical" />
      <a href="#">Terms</a>
      <Separator orientation="vertical" />
      <a href="#">Cookies</a>
    </nav>
  )
}

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

堆叠内容

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

import { Separator } from "@ui-sdk/solid/separator"

function Example() {
  return (
    <div data-slot="row">
      <div data-slot="stack">
        <span>24</span>
        <span data-slot="hint">Projects</span>
      </div>
      <Separator orientation="vertical" />
      <div data-slot="stack">
        <span>1,280</span>
        <span data-slot="hint">Followers</span>
      </div>
      <Separator orientation="vertical" />
      <div data-slot="stack">
        <span>312</span>
        <span data-slot="hint">Following</span>
      </div>
    </div>
  )
}

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

列表行

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

import { Separator } from "@ui-sdk/solid/separator"

function Example() {
  return (
    <div data-slot="stack">
      <dl>
        <dt>Plan</dt>
        <dd>Pro</dd>
      </dl>
      <Separator />
      <dl>
        <dt>Seats</dt>
        <dd>8 of 10</dd>
      </dl>
      <Separator />
      <dl>
        <dt>Renews</dt>
        <dd>December 1, 2026</dd>
      </dl>
    </div>
  )
}

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

装饰性

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

import { Separator } from "@ui-sdk/solid/separator"

function Example() {
  return (
    <div data-slot="stack">
      <button type="button" data-variant="solid">
        Sign In with Passkey
      </button>
      <div data-slot="row">
        <Separator decorative />
        <span>or</span>
        <Separator decorative />
      </div>
      <button type="button">Email Me a Link</button>
    </div>
  )
}

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

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

自定义渲染

传入 render 以使用不同的元素,比如 render={(props) => <hr {...props} />}。读取分隔线状态的方法见部件与组合。

样式

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

分隔线的 data-slot 是 separator,它的 data-orientation 列在数据属性下。

数据属性

Separator

[data-slot="separator"]

属性含义
data-orientationhorizontalverticalWhich way the line runs.

API 参考

props 与下面相同,都是 Solid props。

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。