Toggle Group
支持键盘焦点漫游、多种样式预设、尺寸与间距控制的 Shadcn 切换按钮组组件。
Toggle Group 将一组可切换的双态按钮组织为支持方向键漫游焦点的选择组,支持单选与多选模式。本页示例使用 Shadcn Rhea 风格。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<wc-shadcn-toggle-group-root
id="shadcn-tg-hero"
style-profile="rhea"
variant="outline"
multiple
a11y-label="Text formatting"
>
<wc-shadcn-toggle-group-item style-profile="rhea" value="bold">
<wc-lucide-bold-icon aria-hidden="true"></wc-lucide-bold-icon>
<span class="sr-only">Toggle bold</span>
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="italic">
<wc-lucide-italic-icon
aria-hidden="true"
></wc-lucide-italic-icon>
<span class="sr-only">Toggle italic</span>
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item
style-profile="rhea"
value="underline"
>
<wc-lucide-underline-icon
aria-hidden="true"
></wc-lucide-underline-icon>
<span class="sr-only">Toggle underline</span>
</wc-shadcn-toggle-group-item>
</wc-shadcn-toggle-group-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#shadcn-tg-hero'), {
styleProfile: 'rhea',
variant: 'outline',
multiple: true,
defaultValue: ['bold'],
});
</script>将于 0.3.0-rc.1 及后续版本中提供。可直接从 @proto.ui/prototypes-shadcn/toggle-group 导入组件原型并通过对应宿主 Adapter 适配。
import { ShadcnToggleGroupItem, ShadcnToggleGroupRoot } from '../proto-ui/components/react';
export function Example() { return ( <ShadcnToggleGroupRoot variant="outline" defaultValue={['a']}> <ShadcnToggleGroupItem value="a">A</ShadcnToggleGroupItem> <ShadcnToggleGroupItem value="b">B</ShadcnToggleGroupItem> <ShadcnToggleGroupItem value="c">C</ShadcnToggleGroupItem> </ShadcnToggleGroupRoot> );}ToggleGroupRoot├── ToggleGroupItem└── ToggleGroupItem- 值与模式:
value与defaultValue均为字符串数组(readonly string[])。当multiple={false}(默认)时,组内最多保留一个选中值,再次激活已选中项可清空选择;设为multiple={true}允许同时选中多项。 - 受控更新:传入
value进入受控模式,交互通过valueChange(载荷{ value: readonly string[] })提交候选值,由外部更新状态。 - 键盘与无障碍:方向键根据
orientation在选项间移动焦点而不自动切换选中态,按Space或Enter切换当前聚焦项;纯图标按钮应搭配视觉隐藏文本(如sr-only)提供可访问名称。 - 风格预设:支持通过
styleProfile或外层 Style Scope 切换vega、nova、maia、lyra、mira、luma、sera、rhea与new-york-v4。
描边单选(Outline)
Section titled “描边单选(Outline)”使用 variant="outline" 渲染描边风格,默认单选模式下再次点击当前项可取消选择。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<wc-shadcn-toggle-group-root
id="shadcn-tg-outline"
style-profile="rhea"
variant="outline"
a11y-label="Message filter"
>
<wc-shadcn-toggle-group-item style-profile="rhea" value="all">
All
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="missed">
Missed
</wc-shadcn-toggle-group-item>
</wc-shadcn-toggle-group-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#shadcn-tg-outline'), {
styleProfile: 'rhea',
variant: 'outline',
defaultValue: ['all'],
});
</script>尺寸(Size)
Section titled “尺寸(Size)”通过 size 统一控制整组按钮的尺寸(sm、default、lg),单项也可独立覆写。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div class="flex flex-col items-start gap-4">
<wc-shadcn-toggle-group-root
style-profile="rhea"
size="sm"
variant="outline"
a11y-label="Small position selector"
>
<wc-shadcn-toggle-group-item style-profile="rhea" value="top">
Top
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item
style-profile="rhea"
value="bottom"
>
Bottom
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="left">
Left
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="right">
Right
</wc-shadcn-toggle-group-item>
</wc-shadcn-toggle-group-root>
<wc-shadcn-toggle-group-root
style-profile="rhea"
size="default"
variant="outline"
a11y-label="Default position selector"
>
<wc-shadcn-toggle-group-item style-profile="rhea" value="top">
Top
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item
style-profile="rhea"
value="bottom"
>
Bottom
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="left">
Left
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="right">
Right
</wc-shadcn-toggle-group-item>
</wc-shadcn-toggle-group-root>
<wc-shadcn-toggle-group-root
style-profile="rhea"
size="lg"
variant="outline"
a11y-label="Large position selector"
>
<wc-shadcn-toggle-group-item style-profile="rhea" value="top">
Top
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item
style-profile="rhea"
value="bottom"
>
Bottom
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="left">
Left
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="right">
Right
</wc-shadcn-toggle-group-item>
</wc-shadcn-toggle-group-root>
</div>间距与拼接(Spacing)
Section titled “间距与拼接(Spacing)”使用 spacing 控制按钮间距:现代预设默认为 2,设为 0 时自动折叠内边框并保留外侧圆角,拼接为连续按钮条。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div class="flex flex-col items-start gap-4">
<wc-shadcn-toggle-group-root
style-profile="rhea"
size="sm"
variant="outline"
spacing="2"
a11y-label="Spaced position selector"
>
<wc-shadcn-toggle-group-item style-profile="rhea" value="top">
Top
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item
style-profile="rhea"
value="bottom"
>
Bottom
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="left">
Left
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="right">
Right
</wc-shadcn-toggle-group-item>
</wc-shadcn-toggle-group-root>
<wc-shadcn-toggle-group-root
style-profile="rhea"
size="sm"
variant="outline"
spacing="0"
a11y-label="Joined position selector"
>
<wc-shadcn-toggle-group-item style-profile="rhea" value="top">
Top
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item
style-profile="rhea"
value="bottom"
>
Bottom
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="left">
Left
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="right">
Right
</wc-shadcn-toggle-group-item>
</wc-shadcn-toggle-group-root>
</div>垂直排列(Vertical)
Section titled “垂直排列(Vertical)”设置 orientation="vertical" 将布局与上下方向键漫游切换为垂直方向。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<wc-shadcn-toggle-group-root
id="shadcn-tg-vertical"
style-profile="rhea"
orientation="vertical"
spacing="1"
multiple
a11y-label="Vertical text formatting"
>
<wc-shadcn-toggle-group-item style-profile="rhea" value="bold">
<wc-lucide-bold-icon aria-hidden="true"></wc-lucide-bold-icon>
<span class="sr-only">Toggle bold</span>
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="italic">
<wc-lucide-italic-icon
aria-hidden="true"
></wc-lucide-italic-icon>
<span class="sr-only">Toggle italic</span>
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item
style-profile="rhea"
value="underline"
>
<wc-lucide-underline-icon
aria-hidden="true"
></wc-lucide-underline-icon>
<span class="sr-only">Toggle underline</span>
</wc-shadcn-toggle-group-item>
</wc-shadcn-toggle-group-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#shadcn-tg-vertical'), {
styleProfile: 'rhea',
orientation: 'vertical',
spacing: 1,
multiple: true,
defaultValue: ['bold', 'italic'],
});
</script>禁用(Disabled)
Section titled “禁用(Disabled)”在 Root 上设置 disabled 禁用整组交互,或在单个 Item 上禁用特定选项。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<wc-shadcn-toggle-group-root
style-profile="rhea"
variant="outline"
disabled
a11y-label="Disabled formatting controls"
>
<wc-shadcn-toggle-group-item style-profile="rhea" value="bold">
<wc-lucide-bold-icon aria-hidden="true"></wc-lucide-bold-icon>
<span class="sr-only">Toggle bold</span>
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item style-profile="rhea" value="italic">
<wc-lucide-italic-icon
aria-hidden="true"
></wc-lucide-italic-icon>
<span class="sr-only">Toggle italic</span>
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item
style-profile="rhea"
value="underline"
>
<wc-lucide-underline-icon
aria-hidden="true"
></wc-lucide-underline-icon>
<span class="sr-only">Toggle underline</span>
</wc-shadcn-toggle-group-item>
</wc-shadcn-toggle-group-root>受控与自定义组合(Custom)
Section titled “受控与自定义组合(Custom)”结合 Field、value 与 valueChange 构建自定义受控字重选择器。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<wc-shadcn-field-root style-profile="rhea" class="w-fit">
<wc-shadcn-field-label style-profile="rhea">
Font Weight
</wc-shadcn-field-label>
<wc-shadcn-toggle-group-root
id="font-weight-group"
style-profile="rhea"
variant="outline"
spacing="2"
size="lg"
a11y-label="Font weight"
>
<wc-shadcn-toggle-group-item
style-profile="rhea"
value="light"
class="flex size-16 flex-col items-center justify-center gap-0.5 rounded-xl"
>
<span class="text-2xl leading-none font-light">Aa</span>
<span class="text-xs text-muted-foreground">Light</span>
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item
style-profile="rhea"
value="normal"
class="flex size-16 flex-col items-center justify-center gap-0.5 rounded-xl"
>
<span class="text-2xl leading-none font-normal">Aa</span>
<span class="text-xs text-muted-foreground">Normal</span>
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item
style-profile="rhea"
value="medium"
class="flex size-16 flex-col items-center justify-center gap-0.5 rounded-xl"
>
<span class="text-2xl leading-none font-medium">Aa</span>
<span class="text-xs text-muted-foreground">Medium</span>
</wc-shadcn-toggle-group-item>
<wc-shadcn-toggle-group-item
style-profile="rhea"
value="bold"
class="flex size-16 flex-col items-center justify-center gap-0.5 rounded-xl"
>
<span class="text-2xl leading-none font-bold">Aa</span>
<span class="text-xs text-muted-foreground">Bold</span>
</wc-shadcn-toggle-group-item>
</wc-shadcn-toggle-group-root>
<wc-shadcn-field-description style-profile="rhea">
Use
<span
id="weight-token"
class="rounded-md bg-muted px-1 py-0.5 font-mono text-xs"
>
font-normal
</span>
to set the font weight.
</wc-shadcn-field-description>
</wc-shadcn-field-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const group = document.querySelector('#font-weight-group');
const token = document.querySelector('#weight-token');
let selected = 'normal';
setElementProps(group, {
styleProfile: 'rhea',
value: [selected],
});
group.addEventListener('valueChange', (event) => {
const next = event.detail.value[0];
if (!next) return;
selected = next;
setElementProps(group, {
styleProfile: 'rhea',
value: [selected],
});
token.textContent = 'font-' + selected;
});
</script>| 属性 | 类型 | 默认值 |
|---|---|---|
value | readonly string[] | —(受控) |
defaultValue | readonly string[] | [] |
multiple | boolean | false |
variant | 'default' 'outline' | 'default' |
size | 'default' 'sm' 'lg' | 'default' |
spacing | 0 1 2 | 现代预设 2 / new-york-v4: 0 |
orientation | 'horizontal' 'vertical' | 'horizontal' |
disabled | boolean | false |
readOnly | boolean | false |
loop | boolean | true |
a11yLabel | string | '' |
styleProfile | ShadcnStyleChoice | 'inherit' → scope → 'nova' |
valueChange | Event<{ value: readonly string[] }> | — |
| 属性 | 类型 | 默认值 |
|---|---|---|
value | string | ''(组内唯一) |
disabled | boolean | false |
variant | 'default' 'outline' | 继承 Root |
size | 'default' 'sm' 'lg' | 继承 Root |
styleProfile | ShadcnStyleChoice | 'inherit' → scope → 'nova' |