跳转到内容

Toolbar 工具栏 ​

用于组织一组相关的操作控件。

用法 ​

集中常用编辑操作

组件结构 ​

加粗的是必需部件。

data-scope="toolbar":root · group · item · separator

示例 ​

分组 ​

将相关操作收在一起

垂直布局 ​

按纵向排列工具

附着工具面 ​

为悬浮工具条提供完整表面

设计指引 ​

何时使用 ​

  • 文本编辑、表格操作或画布工具。
  • 相关控件需要统一的方向键导航。

何时不用 ​

  • 只有少量独立操作时直接使用按钮。
  • 操作必须连接成一个整体时使用按钮组。

特性 ​

  • 默认 ghost 形态只组织控件,不绘制工具条外框。
  • outline 形态提供带内距、描边与背景的附着式工具面;subtle 为淡底。
  • group 将相关操作连接成连续分段,并以低对比度分隔线区分。
  • 独立条目是接入 Action Control text 档的无描边工具按钮,组内条目使用中性操作面;aria-pressed 表示选中状态。
  • 支持水平、垂直、分组、分隔线与整体禁用。
  • 方向键在条目间移动,禁用项会被跳过。

组合 ​

  • 使用 group 收紧相关操作。
  • 使用 separator 区分操作组。

最佳实践 ​

  • 仅图标条目必须提供 aria-label。
  • 使用 aria-pressed 表示可切换工具的当前状态。

反模式 ​

  • 不要在工具栏中放置文本输入控件。
  • 不要将整页所有操作放入同一工具栏。

API 参考 ​

产物 ​

层值
自定义元素<xh-toolbar>
Vue 组件XhToolbarGroup XhToolbarItem XhToolbarRoot XhToolbarSeparator
组合式函数useToolbar
状态机toolbarMachine
皮肤@xihan-ui/styles/toolbar.css

Props ​

属性类型必填说明
orientationOrientation主轴,默认 horizontal。它决定 root 的 aria-orientation、方向键接管哪一对键 (另一轴原样放行给页面),以及分隔线的朝向(恒与主轴垂直)。
dirDirection文字方向,默认 ltr;只改写水平主轴上左右方向键的语义。
loopboolean方向键到达末尾是否回绕,默认 true。
disabledboolean整条禁用:条目全部为 aria-disabled,方向键不再接管。
variantControlVariant形态:ghost 只组织控件不画面(默认),outline 为附着式工具面,subtle 为淡底。默认 ghost。
sizeSize尺寸:sm / md / lg,同时调整排布与默认条目尺寸。

插槽 ​

仅列出带载荷的插槽。

Vue 组件插槽载荷说明
XhToolbarRootdefaultToolbarRootSlotProps

React 适配器 props ​

只列各组件自己声明的那些:继承自 ComponentPropsWithRef 的 DOM 属性不在其中,根组件上与上面 Props 表同名的也不重复列。Vue 的对应物是上面的插槽表。

React 组件属性类型必填说明
XhToolbarItemvaluestring是
XhToolbarItemdisabledboolean
XhToolbarItemasElementType条目渲染为哪个标签,默认 button;不自动补 type="button",表单内需自行声明。
XhToolbarRootchildrenSlotChildren<ToolbarRootSlotProps>

状态 ​

以下名称仅用于内部状态机。

状态:idle

事件:ITEM.FOCUS · TOOLBAR.BLUR · PRESS.START · PRESS.END

判据:canPress

connect API ​

getXxxProps() 返回对应部件的宿主属性。

成员类型说明
focusedValuestring | null焦点锚点;焦点不在工具条内时为 null。
orientationOrientation生效的主轴。
separatorOrientationOrientation分隔线的朝向:恒与主轴垂直(横向工具条中的分隔线是竖线)。
disabledboolean
getRootProps() => T['element']
getGroupProps() => T['element']
getItemProps(props: ToolbarItemProps) => T['element']
getSeparatorProps() => T['element']

无障碍 ​

键盘 ​

规格出处:W3C APG

按键生效条件行为
Tab / Shift+Tabroving tabindex(恒开)整条只占一个 Tab 位:焦点落到锚点条目,无锚点时先落容器再由它转投给第一个可停留条目
ArrowRight / ArrowDown焦点在条内且未整条禁用;横排收 ArrowRight、竖排收 ArrowDown焦点移到下一个可停留条目(禁用项跳过、尽头按 loop 回绕);dir=rtl 时水平主轴改由 ArrowLeft 承担
ArrowLeft / ArrowUp焦点在条内且未整条禁用;横排收 ArrowLeft、竖排收 ArrowUp焦点移到上一个可停留条目(禁用项跳过、尽头按 loop 回绕);dir=rtl 时水平主轴改由 ArrowRight 承担
Home焦点在条内且未整条禁用焦点移到首个可停留条目
End焦点在条内且未整条禁用焦点移到末个可停留条目
Enter / Spaceheld on item, 整条未禁用且条目未禁用按住期间该条目投影 data-pressed,与指针 :active 同一副按压面;抬起、失焦或转禁用撤下。激活语义仍归条目自身(原生 button 的 click)
交叉轴的两个方向键焦点在条内(横排按上下、竖排按左右)不归工具条管:原样放行给页面滚动与读屏,绝不 preventDefault

ARIA ​

以下属性由 connect 生成。

部件属性值
rootaria-disabled'true' | 'false'
rootaria-orientationprops.orientation
rootrole'toolbar'
grouprole'group'
itemaria-disabled'true' | 'false'
separatoraria-orientation'vertical' | 'horizontal'
separatorrole'separator'

样式参考 ​

皮肤 ​

@xihan-ui/styles/toolbar.css 使用 [data-scope="toolbar"][data-part="root"] 部件选择器,位于 xihan.components 层。覆盖样式使用 xihan.overrides。

forced-colors: active 下另有一套规则:颜色交给系统,边框与状态标记改用系统色关键字。

数据属性 ​

由 connect 生成;条件不成立时不输出无值属性。

部件属性值
rootdata-disabled''(条件成立时才出现)
rootdata-orientationprops.orientation
rootdata-sizeprops.size
rootdata-variantprops.variant
groupdata-disabled''(条件成立时才出现)
groupdata-orientationprops.orientation
itemdata-disabled''(条件成立时才出现)
itemdata-pressed''(条件成立时才出现)
itemdata-xh-action-control''
itemdata-xh-action-display'always'
itemdata-xh-action-profile'text'
itemdata-xh-action-sizeprops.size
itemdata-xh-action-variant'ghost'
separatordata-orientation'vertical' | 'horizontal'

CSS 变量 ​

本组件公开覆盖槽由独立皮肤的实际消费位生成;默认来源、作用部件和状态均与 CSS 同源。

变量部件CSS 属性状态默认来源说明
--xh-toolbar-bgrootbackgrounddefault--xh-_toolbar-root-bgtoolbar 的 root 部件 background 覆盖槽。
--xh-toolbar-bg-disabledrootbackgrounddisabled--xh-_toolbar-root-bg-disabledtoolbar 的 root 部件 background 覆盖槽。
--xh-toolbar-borderrootborderdefault--xh-_toolbar-root-bordertoolbar 的 root 部件 border 覆盖槽。
--xh-toolbar-fgrootcolordefault--xh-fg-defaulttoolbar 的 root 部件 color 覆盖槽。
--xh-toolbar-gaprootgapdefault--xh-_toolbar-gaptoolbar 的 root 部件 gap 覆盖槽。
--xh-toolbar-group-gapgroupgapdefault--xh-space-0toolbar 的 group 部件 gap 覆盖槽。
--xh-toolbar-icon-sizeroot--xh-icon-sizedefault
size=lg
size=sm
--xh-glyph-size-lg
--xh-glyph-size-md
--xh-glyph-size-sm
toolbar 的 root 部件 --xh-icon-size 覆盖槽。
--xh-toolbar-item-bggroup
item
--xh-ink-surface
background-color
default
not([aria-pressed='true'])
xh-ink-surface
--xh-_action-variant-bg-rest
--xh-bg-subtle
toolbar 的 group、item 部件 --xh-ink-surface、background-color 覆盖槽。
--xh-toolbar-item-bg-activeitembackground-colordisabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-_action-variant-bg-pressedtoolbar 的 item 部件 background-color 覆盖槽。
--xh-toolbar-item-bg-disabledgroup
item
--xh-ink-surface
background-color
disabled
xh-ink-surface
--xh-_action-variant-bg-disabled
--xh-bg-subtle
toolbar 的 group、item 部件 --xh-ink-surface、background-color 覆盖槽。
--xh-toolbar-item-bg-hoveritembackground-colordisabled
hover
loading
not([data-disabled])
not([data-loading])
--xh-_action-variant-bg-hovertoolbar 的 item 部件 background-color 覆盖槽。
--xh-toolbar-item-bg-presseditem--xh-ink-surface
background-color
default
xh-ink-surface
--xh-bg-brand-subtletoolbar 的 item 部件 --xh-ink-surface、background-color 覆盖槽。
--xh-toolbar-item-bg-pressed-activeitembackground-colordisabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-bg-brand-subtle-activetoolbar 的 item 部件 background-color 覆盖槽。
--xh-toolbar-item-bg-pressed-hoveritembackground-colordisabled
hover
loading
not([data-disabled])
not([data-loading])
--xh-bg-brand-subtle-hovertoolbar 的 item 部件 background-color 覆盖槽。
--xh-toolbar-item-fgitemcolordefault
disabled
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-_action-variant-fg-hover
--xh-_action-variant-fg-pressed
--xh-_action-variant-fg-rest
toolbar 的 item 部件 color 覆盖槽。
--xh-toolbar-item-fg-presseditemcolordefault
disabled
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-fg-on-brand-subtletoolbar 的 item 部件 color 覆盖槽。
--xh-toolbar-item-font-sizeitemfont-sizedefault--xh-_action-profile-font-sizetoolbar 的 item 部件 font-size 覆盖槽。
--xh-toolbar-item-font-weightitemfont-weightdefault--xh-text-label-weighttoolbar 的 item 部件 font-weight 覆盖槽。
--xh-toolbar-item-gapitemgapdefault--xh-control-gap-smtoolbar 的 item 部件 gap 覆盖槽。
--xh-toolbar-item-hitemblock-sizedefault--xh-_action-profile-visual-sizetoolbar 的 item 部件 block-size 覆盖槽。
--xh-toolbar-item-pxitempadding-inlinedefault--xh-_action-profile-padding-inlinetoolbar 的 item 部件 padding-inline 覆盖槽。
--xh-toolbar-item-radiusgroup
item
border-end-end-radius
border-end-start-radius
border-radius
border-start-end-radius
border-start-start-radius
default
first-of-type
last-of-type
orientation=horizontal
orientation=vertical
--xh-_action-profile-radius
--xh-shape-control
toolbar 的 group、item 部件 border-end-end-radius、border-end-start-radius、border-radius、border-start-end-radius、border-start-start-radius 覆盖槽。
--xh-toolbar-pxrootpadding-inlinedefault--xh-_toolbar-root-ptoolbar 的 root 部件 padding-inline 覆盖槽。
--xh-toolbar-pyrootpadding-blockdefault--xh-_toolbar-root-ptoolbar 的 root 部件 padding-block 覆盖槽。
--xh-toolbar-radiusrootborder-radiusdefault
variant=outline
--xh-shape-surfacetoolbar 的 root 部件 border-radius 覆盖槽。
--xh-toolbar-separator-colorgroup
separator
backgrounddefault--xh-border-default
--xh-fg-default
toolbar 的 group、separator 部件 background 覆盖槽。
--xh-toolbar-separator-gapseparatormargin-block
margin-inline
orientation=horizontal
orientation=vertical
--xh-space-0toolbar 的 separator 部件 margin-block、margin-inline 覆盖槽。
--xh-toolbar-separator-insetseparatormargin-block
margin-inline
orientation=horizontal
orientation=vertical
--xh-space-0toolbar 的 separator 部件 margin-block、margin-inline 覆盖槽。
--xh-toolbar-separator-opacitygroup
separator
opacitydefault--xh-control-separator-opacitytoolbar 的 group、separator 部件 opacity 覆盖槽。
--xh-toolbar-separator-radiusseparatorborder-radiusdefault--xh-shape-pilltoolbar 的 separator 部件 border-radius 覆盖槽。
--xh-toolbar-separator-thicknessgroup
separator
block-size
inline-size
margin-block-start
margin-inline-start
orientation=horizontal
orientation=vertical
--xh-stroke-thintoolbar 的 group、separator 部件 block-size、inline-size、margin-block-start、margin-inline-start 覆盖槽。
--xh-toolbar-shadowrootbox-shadowvariant=outlinenonetoolbar 的 root 部件 box-shadow 覆盖槽。

动效 ​

动效角色:按压 · 状态(见动效规范)。

background-color · color 走 transition 过渡。时长与缓动读动效令牌,改令牌即改全局节奏。

系统开启减弱动效时由令牌层统一收敛,皮肤不另作判断。

RTL ​

皮肤用逻辑属性排布(inline-start 一族),dir="rtl" 下自动镜像。

Released under The MIT License