切换多个相关面板,同一时间只选中一个选项卡。
当多个视图共用同一块空间时,比如设置分区,或者预览和它的源码,就用 Tabs。每个选项卡打开与它 value 相同的面板。
import "@ui-sdk/web/tabs"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-tabs default-value="account">
<ui-tabs-list aria-label="Settings">
<ui-tabs-tab value="account">
<button type="button">Account</button>
</ui-tabs-tab>
<ui-tabs-tab value="password">
<button type="button">Password</button>
</ui-tabs-tab>
</ui-tabs-list>
<ui-tabs-panel value="account">
Update your name, email address and profile photo. Your team sees changes right away.
</ui-tabs-panel>
<ui-tabs-panel value="password">
Choose a new password for hollis@example.com. You stay signed in on this device.
</ui-tabs-panel>
</ui-tabs>
`
}
export { mount }安装
npm install @ui-sdk/web @ui-sdk/core导入组件会定义它的元素。在服务器上,这个导入什么也不做。
import "@ui-sdk/web/tabs"组合
用下面的组合构建一个 ui-tabs:
ui-tabs
├── ui-tabs-list
│ ├── ui-tabs-tab
│ │ └── button
│ └── ui-tabs-tab
│ └── button
├── ui-tabs-panel
└── ui-tabs-panel给每个选项卡和它的面板相同的唯一 value。给列表一个无障碍名称。在每个 ui-tabs-tab 里放一个原生 button。设置 default-value 选择初始选项卡;否则会选中第一个启用的选项卡。
用法
导入各个部件并把它们组合起来:
<script type="module">
import "@ui-sdk/web/tabs"
</script>
<ui-tabs default-value="account">
<ui-tabs-list aria-label="Settings">
<ui-tabs-tab value="account"
><button type="button">Account</button></ui-tabs-tab
>
<ui-tabs-tab value="password"
><button type="button">Password</button></ui-tabs-tab
>
</ui-tabs-list>
<ui-tabs-panel value="account">Manage your profile.</ui-tabs-panel>
<ui-tabs-panel value="password">Choose a new password.</ui-tabs-panel>
</ui-tabs>自己构建
@ui-sdk/web/tabs 自带现成的 ui-tabs 元素。如果你想用自己的元素名构建自己的版本,从 @ui-sdk/core 里的 tabs 原语(primitive)开始:
import { tabs } from "@ui-sdk/core/tabs"
import { createParts, wrappedButton } from "@ui-sdk/web"
createParts(tabs, {
root: { tag: "segment-group", display: "block" },
list: { tag: "segment-list", display: "block" },
tab: { tag: "segment-tab", target: wrappedButton, display: "contents" },
panel: { tag: "segment-panel", display: "block" },
})<segment-group default-value="day">
<segment-list aria-label="Range">
<segment-tab value="day"><button type="button">Day</button></segment-tab>
<segment-tab value="week"><button type="button">Week</button></segment-tab>
</segment-list>
<segment-panel value="day">128 visitors today.</segment-panel>
<segment-panel value="week">1,024 visitors this week.</segment-panel>
</segment-group>wrappedButton 把每个选项卡的行为和属性放在它里面的 button 上。再加上 "tab-icon" 和 indicator 元素,也能得到这些部件。每个元素都保留现成对应元素的行为、状态和数据属性。更多内容见渲染原语和自定义部件。
垂直
设置 orientation="vertical" 改用上、下方向键。把列表样式设成面板旁边的列,与之匹配。
import "@ui-sdk/web/tabs"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-tabs default-value="general" orientation="vertical">
<ui-tabs-list aria-label="Preferences">
<ui-tabs-tab value="general">
<button type="button">General</button>
</ui-tabs-tab>
<ui-tabs-tab value="security">
<button type="button">Security</button>
</ui-tabs-tab>
<ui-tabs-tab value="integrations">
<button type="button">Integrations</button>
</ui-tabs-tab>
</ui-tabs-list>
<ui-tabs-panel value="general">Choose your language, time zone and the page you start on.</ui-tabs-panel>
<ui-tabs-panel value="security">Manage passkeys, signed-in sessions and recovery codes.</ui-tabs-panel>
<ui-tabs-panel value="integrations">Connect your calendar and chat apps to get reminders.</ui-tabs-panel>
</ui-tabs>
`
}
export { mount }要让方向随布局变化,比如宽屏时是面板旁边的列、手机上时是面板上方的行,就从媒体查询里设置 orientation。方向键会跟着它变。
<ui-tabs default-value="account">
<!-- List and panels. -->
</ui-tabs>
<script type="module">
const tabs = document.querySelector("ui-tabs")
const isWide = matchMedia("(min-width: 768px)")
const follow = () =>
tabs.setAttribute("orientation", isWide.matches ? "vertical" : "horizontal")
isWide.addEventListener("change", follow)
follow()
</script>禁用
给选项卡加上 disabled 可以阻止选中。方向键仍然可以聚焦它,让人能发现它。
import "@ui-sdk/web/tabs"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-tabs default-value="overview">
<ui-tabs-list aria-label="Dashboard">
<ui-tabs-tab value="overview">
<button type="button">Overview</button>
</ui-tabs-tab>
<ui-tabs-tab value="analytics">
<button type="button">Analytics</button>
</ui-tabs-tab>
<ui-tabs-tab value="reports" disabled>
<button type="button">Reports</button>
</ui-tabs-tab>
</ui-tabs-list>
<ui-tabs-panel value="overview">Revenue, visitors and sign-ups for the last 30 days.</ui-tabs-panel>
<ui-tabs-panel value="analytics">Where your visitors come from and the pages they read most.</ui-tabs-panel>
<ui-tabs-panel value="reports">Monthly reports are available on the Pro plan.</ui-tabs-panel>
</ui-tabs>
`
}
export { mount }图标
把装饰性图标放进 ui-tabs-tab-icon,这样它们不会进入选项卡的无障碍名称。给有方向的图标,比如 Reply,加上 mirror,让它们在从右到左(RTL)的文本里翻转。只有图标的选项卡仍然需要 aria-label。
import "@ui-sdk/web/tabs"
import { Archive, createElement, Mail, Reply } from "lucide"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-tabs default-value="inbox">
<ui-tabs-list aria-label="Mail">
<ui-tabs-tab value="inbox">
<button type="button">
<ui-tabs-tab-icon>${createElement(Mail, { "aria-hidden": "true" }).outerHTML}</ui-tabs-tab-icon>
Inbox
</button>
</ui-tabs-tab>
<ui-tabs-tab value="replies">
<button type="button">
<ui-tabs-tab-icon mirror>${createElement(Reply, { "aria-hidden": "true" }).outerHTML}</ui-tabs-tab-icon>
Replies
</button>
</ui-tabs-tab>
<ui-tabs-tab value="archive">
<button type="button">
<ui-tabs-tab-icon>${createElement(Archive, { "aria-hidden": "true" }).outerHTML}</ui-tabs-tab-icon>
Archive
</button>
</ui-tabs-tab>
</ui-tabs-list>
<ui-tabs-panel value="inbox">3 new messages from Inès Moreau and Kofi Mensah.</ui-tabs-panel>
<ui-tabs-panel value="replies">Wren Takahashi replied to your message about the launch.</ui-tabs-panel>
<ui-tabs-panel value="archive">Messages you archived stay here and in search.</ui-tabs-panel>
</ui-tabs>
`
}
export { mount }手动激活
打开面板需要时间时,比如视图要获取数据,就设置 activation="manual"。方向键移动焦点;Enter 或 Space 选中获得焦点的选项卡。
<ui-tabs activation="manual"></ui-tabs>只有当面板里没有可聚焦内容时,面板才会成为 Tab 停靠点。面板里如果有输入框,Tab 会直接进入该输入框。
import "@ui-sdk/web/field"
import "@ui-sdk/web/input"
import "@ui-sdk/web/tabs"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-tabs default-value="preview" activation="manual">
<ui-tabs-list aria-label="Snippet">
<ui-tabs-tab value="preview">
<button type="button">Preview</button>
</ui-tabs-tab>
<ui-tabs-tab value="code">
<button type="button">Code</button>
</ui-tabs-tab>
</ui-tabs-list>
<ui-tabs-panel value="preview">Welcome back, Hollis. You have 3 unread messages.</ui-tabs-panel>
<ui-tabs-panel value="code">
<ui-field>
<ui-field-label>Source</ui-field-label>
<ui-input>
<input value='<Welcome name="Hollis" unread={3} />' />
</ui-input>
</ui-field>
</ui-tabs-panel>
</ui-tabs>
`
}
export { mount }循环
方向键通常会在两端环绕。设置 loop="false" 改为停在端点上。
import "@ui-sdk/web/tabs"
const ranges = [
{
value: "day",
label: "Day",
content: "128 visitors today, up 12% from yesterday.",
},
{
value: "week",
label: "Week",
content: "1,024 visitors this week, up 8% from last week.",
},
{
value: "month",
label: "Month",
content: "4,210 visitors this month, down 3% from last month.",
},
{
value: "year",
label: "Year",
content: "48,930 visitors this year, up 21% from last year.",
},
]
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-tabs default-value="day" loop="false">
<ui-tabs-list aria-label="Range">
${ranges
.map(
(range) => `
<ui-tabs-tab value="${range.value}">
<button type="button">${range.label}</button>
</ui-tabs-tab>`
)
.join("")}
</ui-tabs-list>
${ranges.map((range) => `<ui-tabs-panel value="${range.value}">${range.content}</ui-tabs-panel>`).join("")}
</ui-tabs>
`
}
export { mount }指示器
ui-tabs-indicator 测量选中的选项卡。把它放在相对定位的选项卡列表里,用它自己的 CSS 变量做一个跟随选择的标记。选项卡或列表尺寸变化时,它会重新测量。有了位置之后,它会得到 data-visible。在此之前,比如在服务器上渲染、脚本还没运行的页面里,它的变量尚未设置,所以要在它 :not([data-visible]) 时隐藏它。
[data-slot="tabs-list"] {
position: relative;
}
[data-slot="tabs-indicator"] {
position: absolute;
top: var(--active-tab-top);
left: var(--active-tab-left);
width: var(--active-tab-width);
height: var(--active-tab-height);
transition-property: top, left, width, height;
transition-duration: 200ms;
&[data-activation-direction="none"] {
transition: none;
}
}为你做动画的属性添加过渡。在 data-activation-direction="none" 时跳过过渡,避免第一次放置也播放动画。
import "@ui-sdk/web/tabs"
const orientations = [
{ value: "horizontal", label: "Horizontal" },
{ value: "vertical", label: "Vertical" },
]
function mount(root: HTMLElement) {
root.innerHTML = `
<div>
<div data-slot="controls" role="group" aria-label="Orientation">
${orientations
.map(
(option) => `
<button type="button" data-size="sm" value="${option.value}" aria-pressed="${option.value === "horizontal"}">
${option.label}
</button>`
)
.join("")}
</div>
<ui-tabs default-value="music" orientation="horizontal">
<ui-tabs-list aria-label="Library">
<ui-tabs-tab value="music">
<button type="button">Music</button>
</ui-tabs-tab>
<ui-tabs-tab value="podcasts">
<button type="button">Podcasts</button>
</ui-tabs-tab>
<ui-tabs-tab value="audiobooks">
<button type="button">Audiobooks</button>
</ui-tabs-tab>
<ui-tabs-indicator></ui-tabs-indicator>
</ui-tabs-list>
<ui-tabs-panel value="music">312 songs in 24 albums, synced to this device.</ui-tabs-panel>
<ui-tabs-panel value="podcasts">8 shows you follow, with 3 new episodes.</ui-tabs-panel>
<ui-tabs-panel value="audiobooks">2 books in progress, 6 hours left in total.</ui-tabs-panel>
</ui-tabs>
</div>
`
const tabs = root.querySelector("ui-tabs")!
const buttons = root.querySelectorAll<HTMLButtonElement>(
'[data-slot="controls"] button'
)
for (const button of buttons) {
button.addEventListener("click", () => {
tabs.setAttribute("orientation", button.value)
for (const other of buttons) {
other.setAttribute("aria-pressed", String(other === button))
}
})
}
}
export { mount }给它加上 follow-hover,它就会改为跟随指针下方的选项卡,指针离开列表后再回到选中的选项卡。同一个标记就成了悬停高亮。
保持挂载
面板关闭时,Web Components 会保留你提供的标记。加上 keep-mounted,还能保留隐藏面板的 role 及其 aria-controls 关系。当智能体(Agent)选择另一个选项卡时,用户所在的面板也会保留它们,直到用户选中那个选项卡并离开。
import "@ui-sdk/web/field"
import "@ui-sdk/web/textarea"
import "@ui-sdk/web/tabs"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-tabs default-value="reply">
<ui-tabs-list aria-label="Comment">
<ui-tabs-tab value="reply">
<button type="button">Reply</button>
</ui-tabs-tab>
<ui-tabs-tab value="formatting">
<button type="button">Formatting</button>
</ui-tabs-tab>
<ui-tabs-indicator></ui-tabs-indicator>
</ui-tabs-list>
<ui-tabs-panel value="reply" keep-mounted>
<ui-field>
<ui-field-label>Message</ui-field-label>
<ui-textarea>
<textarea rows="3" placeholder="Write a reply…"></textarea>
</ui-textarea>
</ui-field>
</ui-tabs-panel>
<ui-tabs-panel value="formatting">
<p>Use **bold**, _italics_ and \`code\` to format your reply.</p>
<p>Paste a link on its own line to show a preview.</p>
</ui-tabs-panel>
</ui-tabs>
`
}
export { mount }在页面中查找
给选项卡或面板加上 hidden-until-found,让关闭的内容仍能被浏览器的查找命令找到。匹配项会以 find-in-page 原因选中它所在的选项卡。这也会让面板保持挂载。
如果初始隐藏的面板应该在元素定义之前就能被找到,也要在它们上面写上 hidden="until-found"。
import "@ui-sdk/web/tabs"
const policies = [
{
value: "shipping",
label: "Shipping",
text: "Orders leave our warehouse within two working days. Express delivery arrives the next morning.",
},
{
value: "returns",
label: "Returns",
text: "Send anything back within 30 days. Opened electronics carry a restocking fee of 10 percent.",
},
{
value: "warranty",
label: "Warranty",
text: "Every product has a two-year warranty. Batteries are covered for one year from delivery.",
},
]
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-tabs default-value="shipping" hidden-until-found>
<ui-tabs-list aria-label="Policies">
${policies
.map(
(policy) => `
<ui-tabs-tab value="${policy.value}">
<button type="button">${policy.label}</button>
</ui-tabs-tab>`
)
.join("")}
</ui-tabs-list>
${policies
.map(
(policy, index) =>
`<ui-tabs-panel value="${policy.value}"${index > 0 ? ' hidden="until-found"' : ""}>${policy.text}</ui-tabs-panel>`
)
.join("")}
</ui-tabs>
`
}
export { mount }动画
面板进场时暴露 data-starting-style,离场时暴露 data-ending-style。上一个面板会在退场动画期间保持挂载,之后卸载;如果它被保持挂载,则改为隐藏。
用 data-activation-direction 选择滑动方向。把面板放进同一个网格单元,让它们在过渡期间重叠,就像这个示例一样。
import "@ui-sdk/web/tabs"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-tabs default-value="overview">
<ui-tabs-list aria-label="Project">
<ui-tabs-tab value="overview">
<button type="button">Overview</button>
</ui-tabs-tab>
<ui-tabs-tab value="activity">
<button type="button">Activity</button>
</ui-tabs-tab>
<ui-tabs-tab value="settings">
<button type="button">Settings</button>
</ui-tabs-tab>
<ui-tabs-indicator></ui-tabs-indicator>
</ui-tabs-list>
<div data-slot="stack">
<ui-tabs-panel value="overview">The launch is on track for Friday, with 4 of 6 tasks done.</ui-tabs-panel>
<ui-tabs-panel value="activity">Inès Moreau deployed to production and Kofi Mensah left a review.</ui-tabs-panel>
<ui-tabs-panel value="settings">Rename the project, transfer it to another team or archive it.</ui-tabs-panel>
</div>
</ui-tabs>
`
}
export { mount }受控
读取并设置元素的 value property,或调用它的 api.select(value) 方法。两者都会选中一个选项卡并触发 valuechange,就像选择一个选项卡那样。状态与事件介绍了这个通用模式。在 valuechange 上调用 event.preventDefault() 可以拒绝一次变更。
valuechange 在 event.detail 里接收新的值和原因,比如 item-press、list-navigation 或 imperative。初始选择不会触发它。元素的 api还能让另一个控件选择选项卡。
import "@ui-sdk/web/tabs"
import { log } from "./log"
function mount(root: HTMLElement) {
root.innerHTML = `
<div>
<div data-slot="controls">
<button type="button">Show Sent Mail</button>
</div>
<ui-tabs default-value="inbox">
<ui-tabs-list aria-label="Mail">
<ui-tabs-tab value="inbox">
<button type="button">Inbox</button>
</ui-tabs-tab>
<ui-tabs-tab value="drafts">
<button type="button">Drafts</button>
</ui-tabs-tab>
<ui-tabs-tab value="sent">
<button type="button">Sent</button>
</ui-tabs-tab>
<ui-tabs-indicator></ui-tabs-indicator>
</ui-tabs-list>
<ui-tabs-panel value="inbox">3 unread messages from Hollis Grant and Inès Moreau.</ui-tabs-panel>
<ui-tabs-panel value="drafts">2 drafts, the last one edited yesterday at 4:12 PM.</ui-tabs-panel>
<ui-tabs-panel value="sent">12 messages sent this week, all delivered.</ui-tabs-panel>
</ui-tabs>
</div>
`
const tabs = root.querySelector("ui-tabs")!
tabs.addEventListener("valuechange", (event) => {
const { value, reason } = event.detail
log(root, `value: ${value} (${reason})`)
})
root
.querySelector('[data-slot="controls"] button')!
.addEventListener("click", () => tabs.api.select("sent"))
}
export { mount }链接
选中就意味着导航时,把选项卡渲染成链接。它仍然保持 tab role;方向键选中它时会跟随链接。带修饰键的点击,比如 Cmd 点击,保留浏览器原本的行为,不会选中选项卡。
<ui-tabs-tab value="activity">
<a href="#activity">Activity</a>
</ui-tabs-tab>import "@ui-sdk/web/tabs"
const pages = [
{
value: "overview",
href: "#project-overview",
label: "Overview",
content:
"Hollis Grant started this project in March. It has 14 open issues.",
},
{
value: "activity",
href: "#project-activity",
label: "Activity",
content:
"Wren Takahashi merged 3 pull requests and closed 5 issues this week.",
},
{
value: "members",
href: "#project-members",
label: "Members",
content:
"Hollis Grant owns the project. Wren Takahashi and 3 others can edit it.",
},
]
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-tabs default-value="overview">
<ui-tabs-list aria-label="Project">
${pages
.map(
(page) => `
<ui-tabs-tab value="${page.value}">
<a href="${page.href}">${page.label}</a>
</ui-tabs-tab>`
)
.join("")}
<ui-tabs-indicator></ui-tabs-indicator>
</ui-tabs-list>
${pages.map((page) => `<ui-tabs-panel value="${page.value}">${page.content}</ui-tabs-panel>`).join("")}
</ui-tabs>
`
}
export { mount }查询字符串
用 URL 作为选中值的来源,并对照你的选项卡值校验它。尽可能在服务器上读取参数,再把它写进 value 属性。通过你的路由器写入变更,让浏览器导航和渲染的页面保持同步。对于纯客户端页面,更新 URL 时保留其余部分:
function saveSelectedTab(value: string | null) {
const url = new URL(location.href)
if (value === null) {
url.searchParams.delete("tab")
} else {
url.searchParams.set("tab", value)
}
history.replaceState(null, "", url)
}在接受值之后从 valuechange 调用它。如果你的页面直接使用浏览器历史,还要在 popstate 时再读一次;视图卸载时移除这个监听器。
URL 哈希
像 #activity 这样的哈希在客户端渲染的页面上有效。在挂载后以及 hashchange 时读取它,然后选中匹配的选项卡。设置 location.hash 会增加一条历史记录。服务器读不到哈希,所以页面加载时要给它一个初始选项卡。
路由选项卡
当每个选项卡都是一个路由时,渲染链接,并从当前路径设置元素的 value。让导航来更新值。你不需要再写一个导航到同一路由的变更处理函数。如果标记由服务器渲染,还要在那里设置初始 value 并隐藏其他面板。
滚动到可见区域
如果列表可以滚动,选中选项卡时会在该列表内把它滚动到可见区域,无论是点击它、用方向键到达它,还是通过 api、value 或 URL 选中它。
自定义渲染
把原生 button 或链接放进 ui-tabs-tab,就像链接示例那样;它会收到选项卡行为,里面其他的内容都由你决定。部件与组合介绍了共用的渲染 API。每个部件的状态列在状态下。
import "@ui-sdk/web/tabs"
const folders = [
{
value: "inbox",
label: "Inbox",
count: 12,
content: "12 conversations, 3 of them unread.",
},
{
value: "unread",
label: "Unread",
count: 3,
content: "Hollis Grant, Inès Moreau and Kofi Mensah wrote to you.",
},
{
value: "archived",
label: "Archived",
count: 0,
content: "Nothing archived yet. Archived mail stays searchable.",
},
]
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-tabs default-value="inbox">
<ui-tabs-list aria-label="Mail">
${folders
.map(
(folder) => `
<ui-tabs-tab value="${folder.value}">
<button type="button">
${folder.label}
${folder.count > 0 ? `<span data-slot="badge">${folder.count}</span>` : ""}
</button>
</ui-tabs-tab>`
)
.join("")}
</ui-tabs-list>
${folders.map((folder) => `<ui-tabs-panel value="${folder.value}">${folder.content}</ui-tabs-panel>`).join("")}
</ui-tabs>
`
}
export { mount }样式
部件没有样式。用 data-selected 和 data-disabled 为选项卡设置样式,对正在显示的面板则用 data-selected。每个部件上的 data-orientation 让同一份样式表能布局水平与垂直选项卡。图标部件还会在选项卡上设置 data-has-icon、在列表上设置 data-has-icons,这有助于对齐混合了文字和图标的选项卡。
指示器的位置和尺寸会从选项卡列表测量到下面的变量里。用它们来做下划线、背景,或者垂直列表旁边的标记。
每个部件都有一个 data-slot,比如 tabs-tab,它的状态属性列在数据属性下。选项卡的属性会落在它的 button 上。
<ui-tabs-indicator>
| 变量 | 内容 |
|---|---|
--active-tab-left | Distance from the tab list's left edge to the selected tab. |
--active-tab-right | Distance from the tab list's right edge to the selected tab. |
--active-tab-top | Distance from the tab list's top edge to the selected tab. |
--active-tab-bottom | Distance from the tab list's bottom edge to the selected tab. |
--active-tab-width | Width of the selected tab. |
--active-tab-height | Height of the selected tab. |
数据属性
<ui-tabs>
[data-slot="tabs"]
| 属性 | 含义 |
|---|---|
data-orientation | horizontalverticalWhich arrow keys move between tabs. |
data-activation-direction | noneleftrightupdownWhich way the last selection moved on screen. Use it to slide panels in from the right side. |
<ui-tabs-list>
[data-slot="tabs-list"]
| 属性 | 含义 |
|---|---|
data-orientation | horizontalverticalWhich arrow keys move between tabs. |
data-activation-direction | noneleftrightupdownWhich way the last selection moved on screen. Use it to slide panels in from the right side. |
data-has-icons | Present while any tab inside holds an icon part, so items without one can line up. |
<ui-tabs-tab>
[data-slot="tabs-tab"]
| 属性 | 含义 |
|---|---|
data-selected | Present on the selected tab and its panel. |
data-orientation | horizontalverticalWhich arrow keys move between tabs. |
data-activation-direction | noneleftrightupdownWhich way the last selection moved on screen. Use it to slide panels in from the right side. |
data-disabled | Present when the tab can't be selected. |
data-has-icon | Present on a tab while it holds an icon part. |
<ui-tabs-tab-icon>
[data-slot="tabs-tab-icon"]
| 属性 | 含义 |
|---|---|
data-mirror | Present on an icon that flips in right-to-left text, with mirror. |
<ui-tabs-indicator>
[data-slot="tabs-indicator"]
| 属性 | 含义 |
|---|---|
data-orientation | horizontalverticalWhich arrow keys move between tabs. |
data-activation-direction | noneleftrightupdownWhich way the last selection moved on screen. Use it to slide panels in from the right side. |
data-visible | Present once the indicator has a position. Its variables are unset until then. |
<ui-tabs-panel>
[data-slot="tabs-panel"]
| 属性 | 含义 |
|---|---|
data-selected | Present on the selected tab and its panel. |
data-orientation | horizontalverticalWhich arrow keys move between tabs. |
data-activation-direction | noneleftrightupdownWhich way the last selection moved on screen. Use it to slide panels in from the right side. |
data-starting-style | Present for a panel's first frame. Style the state you animate from. |
data-ending-style | Present while the previous panel animates out. Style the state you animate to. |
API 参考
属性(props)采用 kebab 命名(kebab case),比如 default-value、keep-mounted 和 hidden-until-found:"false" 即为 false。回调是 DOM 事件。
Tabs
value?T | null- The selected tab's value. Pass it to control the tabs.
nullselects nothing. defaultValue?T | null- The tab selected at first, when uncontrolled. Defaults to the first enabled tab, which never calls
valuechange. valuechange event- Runs when the selection wants to change.
details.reasonsays why, anddetails.cancel()stops it. orientation?TabsOrientation- Which arrow keys move between tabs: left and right, or up and down.
activation?"automatic" | "manual""automatic"selects a tab as soon as arrow keys focus it."manual"waits for Enter or Space.loop?boolean- Whether arrow keys wrap from the last tab to the first.
hiddenUntilFound?boolean- Keeps hidden panels in the page as
hidden="until-found", so the browser's find in page can match their text and select their tab. ImplieskeepMounted. controller?{ getValue: () => T | null; setValue: (value: T | null, options?: TabsChangeOptions) => boolean; select: (value: T, options?: TabsChangeOptions) => boolean; focus: () => void; }- Selects tabs from anywhere. Create one with
createTabsController(). debug?boolean- Logs every state change with its reason to the console, in development builds.
TabsList
列表本身不接受任何选项,只有你给它的属性。
TabsTab
valuestring- Identifies the tab and pairs it with the panel of the same value.
disabled?boolean- Keeps the tab focusable but stops it from being selected, as the tabs pattern asks.
TabsTabIcon
mirror?boolean- Flips the icon horizontally in right-to-left text, for icons that point a way, like arrows and chevrons. Icons that picture a thing, like a clock or a check, stay as they are.
TabsIndicator
followHover?boolean- Whether it follows the tab under the pointer, and goes back to the selected one when the pointer leaves the list.
它的 CSS 变量保存选中选项卡的盒子。
TabsPanel
valuestring- The value of the tab this panel belongs to.
keepMounted?boolean- Whether the panel stays in the page, hidden, while its tab isn't selected.
hiddenUntilFound?boolean- Whether the panel stays findable with
hidden="until-found"while its tab isn't selected. Overrides the tabs'.
状态
同样的状态会以数据属性显示在每个元素上。
ui-tabs:
valueT | null- The selected tab's value.
orientationTabsOrientation- The tabs'
orientationoption, or"horizontal"when it's unset or invalid. directionDirection- Which way the last selection moved.
ui-tabs-list:
orientationTabsOrientation- The tabs'
orientationoption, or"horizontal"when it's unset or invalid.
ui-tabs-tab:
selectedboolean- Whether this tab's value is the tabs'
value. disabledboolean- Whether this tab can't be selected.
ui-tabs-tab-icon:
mirrorboolean- Whether it flips in right-to-left text.
ui-tabs-panel:
selectedboolean- Whether this panel's tab is selected. False while it animates out.
ui-tabs-indicator:
orientationTabsOrientation- The tabs'
orientationoption, or"horizontal"when it's unset or invalid. directionDirection- Which way the last selection moved.
原因
event.detail.reason 说明选择为什么变化。
item-pressMouseEvent | KeyboardEvent- A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
list-navigationKeyboardEvent- Arrow, Home or End keys moved the selection, typing picked an item, or Ctrl or ⌘ with A selected every item of a listbox.
find-in-pageEvent- The browser's find in page matched hidden content.
item-removednone- The selected item left the page, and another was picked.
imperativenone- A controller or
apicall.
控制器
元素的 api property 有同样的方法。value property 读取并设置选择。
每个写入方法都接受可选的 { reason, event },像一次按压那样触发 valuechange,并在变更被取消时返回 false。
getValue()() => T | null- The selected tab's value, or null when nothing is selected.
setValue(value, options?)(value: T | null, options?: TabsChangeOptions | undefined) => boolean- Selects a tab, or nothing with
null. Disabled tabs never select. Returns false when canceled. select(value, options?)(value: T, options?: TabsChangeOptions | undefined) => boolean- Selects a tab, the same as
setValue. focus()() => void- Moves focus to the selected tab, or the tab that holds the tab stop.
事件
事件在各自的元素上触发,不冒泡,新值在 event.detail.value 里,reason 和 event 就在它旁边。在 valuechange 上调用 preventDefault() 会保持当前选项卡。valuechange 在连接时从不触发。
| 成员 | 所在元素 | 说明 |
|---|---|---|
value property, attribute | ui-tabs | 选中的选项卡。设置其中任意一个都会选中选项卡 |
api property | ui-tabs | 控制器 |
valuechange event | ui-tabs | detail: { value, reason, event, source } |
无障碍
Tabs 遵循 WAI-ARIA tabs 模式。列表是 tablist,每个选项卡的 button 控制它的面板,每个面板的名称来自它的选项卡。
键盘
Tab 从选中的选项卡进入列表,再按一次 Tab 离开。左、右方向键在选项卡之间移动并选中它们;使用 activation="manual" 时只移动焦点,由 Enter 或 Space 选中。在垂直列表里,上、下方向键做同样的事。Home 和 End 移动到第一个和最后一个选项卡。列表从页面或任意祖先元素读取 dir="rtl",左、右方向键跟随阅读方向,不需要 provider。
经过测试的行为
下面每一行都是一条自动化测试。
- The default tab is selected and shows its panel.
- Clicking a tab selects it and shows its panel.
- Clicking a disabled tab does nothing.
- Clicking the selected tab keeps it selected.
- A change can be cancelled.
- Without a default value, the first enabled tab is selected.
- Without a default value, the first render already selects the first enabled tab.
- Tab moves focus to the selected tab.
- ArrowRight and ArrowLeft move focus and select the tab.
- ArrowRight and ArrowLeft skip tabs the page hides.
- Disabled tabs are focusable but never selected.
- Focus wraps at both ends.
- Home and End move to the first and last tabs.
- ArrowUp and ArrowDown do nothing in a horizontal tab list.
- Arrow keys with a modifier are left to the browser.
- Keys pressed in quick succession are never lost.
- Tab moves focus from the tab list into the selected panel.
- Shift+Tab from the panel returns to the selected tab.
- Tab leaves the list from a focused tab before the selected one.
- Shift+Tab leaves the list from a focused tab after the selected one.
- Arrow keys move focus without selecting.
- Enter selects the focused tab.
- Space selects the focused tab.
- Enter on a disabled tab does nothing.
- The tab list reports its orientation.
- ArrowDown and ArrowUp move between tabs.
- ArrowLeft and ArrowRight do nothing in a vertical tab list.
- The selected tab controls its panel, and the panel is labelled by its tab.
- With keepMounted, hidden panels stay in the page.
- With keepMounted, a class that sets display doesn't show hidden panels.
运行于 React、Vue 和 web components。
在模式基线之外:
- In manual mode, re-entering the tab list focuses the selected tab.
- A panel that holds a field is not a tab stop of its own.
- A controller selects a tab from anywhere.
- An infinite exit animation still unmounts the panel.
运行于 React、Vue 和 web components。
在每个测试过的框架里,屏幕阅读器听到的内容相同,由虚拟屏幕阅读器录制:
- Announcements while using the tabs with a keyboard.
运行于 React、Vue 和 web components。