Skip to content

Headless UI 参考 ​

本文为 Headless UI v2(React)+ v1(Vue) 的 API 速查文档——16 个组件清单、关键 props、anchor 配置、data-* 属性、键盘快捷键、CSS 变量、TypeScript 类型一站式查询。

完整概念与示例见 入门 与 指南。

1. 16 个组件清单 ​

1.1 React v2 全部组件 ​

组件子组件分类
MenuMenuButton / MenuItems / MenuItem / MenuSection / MenuHeading / MenuSeparatorMenu
ListboxListboxButton / ListboxOptions / ListboxOption / ListboxSelectedOptionMenu
ComboboxComboboxInput / ComboboxButton / ComboboxOptions / ComboboxOptionMenu
Select-Form
DialogDialogBackdrop / DialogPanel / DialogTitle / DialogDescriptionOverlay
PopoverPopoverButton / PopoverPanel / PopoverGroup / PopoverBackdropOverlay
DisclosureDisclosureButton / DisclosurePanelDisclosure
TabGroupTabList / Tab / TabPanels / TabPanelDisclosure
Switch-Form
Checkbox-Form (v2 新)
RadioGroupRadioForm
Input-Form (v2 新)
Textarea-Form (v2 新)
FieldsetLegendForm (v2 新)
Field-Utility (v2 新)
Label / Description-Utility
Button-Utility (v2 新)
TransitionTransitionChildUtility
CloseButton-Utility

1.2 React v2 导入 ​

tsx
import {
  Menu, MenuButton, MenuItems, MenuItem,
  MenuSection, MenuHeading, MenuSeparator,
  Listbox, ListboxButton, ListboxOptions, ListboxOption, ListboxSelectedOption,
  Combobox, ComboboxInput, ComboboxButton, ComboboxOptions, ComboboxOption,
  Dialog, DialogBackdrop, DialogPanel, DialogTitle, DialogDescription,
  Popover, PopoverButton, PopoverPanel, PopoverGroup, PopoverBackdrop,
  Disclosure, DisclosureButton, DisclosurePanel,
  TabGroup, TabList, Tab, TabPanels, TabPanel,
  Switch, Checkbox,
  RadioGroup, Radio,
  Input, Textarea, Select,
  Fieldset, Legend, Field, Label, Description,
  Button, CloseButton,
  Transition, TransitionChild,
} from "@headlessui/react";

1.3 Vue v1 全部组件 ​

组件子组件备注
MenuMenuButton / MenuItems / MenuItemMenu
ListboxListboxButton / ListboxOptions / ListboxOption / ListboxLabelMenu
ComboboxComboboxInput / ComboboxButton / ComboboxOptions / ComboboxOption / ComboboxLabelMenu
DialogDialogPanel / DialogTitle / DialogDescription / DialogOverlayOverlay
PopoverPopoverButton / PopoverPanel / PopoverGroup / PopoverOverlayOverlay
DisclosureDisclosureButton / DisclosurePanelDisclosure
TabGroupTabList / Tab / TabPanels / TabPanelDisclosure
Switch / SwitchGroup / SwitchLabel / SwitchDescription-Form
RadioGroupRadioGroupOption / RadioGroupLabel / RadioGroupDescriptionForm
TransitionRoot / TransitionChild-Utility

Vue v1 缺:Checkbox / Input / Textarea / Select / Field / Fieldset / Legend / Button / 内置 anchor。

1.4 Vue v1 导入 ​

ts
import {
  Menu, MenuButton, MenuItems, MenuItem,
  Listbox, ListboxButton, ListboxOptions, ListboxOption, ListboxLabel,
  Combobox, ComboboxInput, ComboboxButton, ComboboxOptions, ComboboxOption, ComboboxLabel,
  Dialog, DialogPanel, DialogTitle, DialogDescription, DialogOverlay,
  Popover, PopoverButton, PopoverPanel, PopoverGroup, PopoverOverlay,
  Disclosure, DisclosureButton, DisclosurePanel,
  TabGroup, TabList, Tab, TabPanels, TabPanel,
  Switch, SwitchGroup, SwitchLabel, SwitchDescription,
  RadioGroup, RadioGroupOption, RadioGroupLabel, RadioGroupDescription,
  TransitionRoot, TransitionChild,
} from "@headlessui/vue";

2. Menu API 速查 ​

tsx
import { Menu, MenuButton, MenuItems, MenuItem } from "@headlessui/react";

2.1 Menu ​

Prop类型默认说明
asElementTypeFragment渲染元素
__demoModeboolean-demo 模式

Render prop:{ open, close }

2.2 MenuButton ​

Prop类型默认说明
asElementType'button'渲染元素
disabledbooleanfalse禁用
autoFocusboolean-初始聚焦

Data- 属性*:data-open / data-focus / data-hover / data-active / data-autofocusRender prop:{ open, focus, hover, active, autofocus }

2.3 MenuItems ​

Prop类型默认说明
asElementType'div'渲染元素
anchorAnchorProps-锚定配置
staticbooleanfalse始终渲染
unmountbooleantrue关闭时卸载
portalboolean自动是否 portal
modalbooleantrue是否 modal
transitionbooleanfalse启用 transition

Data- 属性*:data-open / data-closed / data-enter / data-leave / data-transitionRender prop:{ open }

2.4 MenuItem ​

Prop类型默认说明
asElementTypeFragment渲染元素
disabledbooleanfalse禁用

Data- 属性*:data-disabled / data-focusRender prop:{ disabled, focus, close }

2.5 MenuSection / MenuHeading / MenuSeparator ​

组件默认 as说明
MenuSection'div'分组容器
MenuHeading'header'分组标题
MenuSeparator'div'分隔线

3. Listbox API 速查 ​

tsx
import { Listbox, ListboxButton, ListboxOptions, ListboxOption } from "@headlessui/react";

3.1 Listbox ​

Prop类型默认说明
valueT | T[]-受控值
defaultValueT | T[]-非受控初始值
onChange(v: T | T[]) => void-值变化
multiplebooleanfalse多选
disabledbooleanfalse禁用
namestring-表单字段名
formstring-关联表单 ID
bykeyof T | (a, b) => boolean-对象比较
horizontalbooleanfalse横向布局

Render prop:{ open, disabled, value }

3.2 ListboxButton ​

与 MenuButton 相同 props。Data- 属性*额外:data-active / data-hover。Render prop 额外:{ value, disabled }。

3.3 ListboxOptions ​

与 MenuItems 相同 props。

3.4 ListboxOption ​

Prop类型说明
valueT当前选项值(必须)
asElementTypeFragment
disabledboolean禁用
ordernumber排序(虚拟滚动场景)

Data- 属性*:data-selected / data-focus / data-disabledRender prop:{ focus, selected, disabled }

3.5 ListboxSelectedOption ​

tsx
<ListboxSelectedOption>
  {(value: typeof people[number] | null) => value?.name ?? "请选择"}
</ListboxSelectedOption>

在 ListboxButton 内显示当前选中项的便捷组件。

4. Combobox API 速查 ​

tsx
import { Combobox, ComboboxInput, ComboboxButton, ComboboxOptions, ComboboxOption } from "@headlessui/react";

4.1 Combobox ​

Prop类型默认说明
valueT | T[] | null-受控值
defaultValueT | T[] | null-非受控
onChange(v: T | T[] | null) => void-值变化
onClose() => void-关闭回调
multiplebooleanfalse多选
disabledbooleanfalse禁用
nullableboolean-(v1 用) v2 默认允许 null
namestring-表单字段名
formstring-关联表单
bykeyof T | (a, b) => boolean-对象比较
immediatebooleanfalse聚焦立即打开
virtual{ options: T[], disabled?: (t: T) => boolean }-虚拟滚动

Render prop:{ open, disabled, value, activeIndex, activeOption }

4.2 ComboboxInput ​

Prop类型说明
displayValue(value: T) => string控制 input 显示文本
onChangeChangeEventHandler输入变化
asElementType'input'
autoFocusboolean自动聚焦

Data- 属性*:data-open / data-focus / data-disabled / data-hover

4.3 ComboboxButton ​

类似 ListboxButton。

4.4 ComboboxOptions ​

Prop类型默认说明
asElementType'div'渲染元素
anchorAnchorProps-锚定
transitionbooleanfalse启用 transition
staticbooleanfalse始终渲染
unmountbooleantrue关闭卸载
portalboolean自动是否 portal
modalbooleantrue是否 modal
holdbooleanfalse阻止滚动事件冒泡

4.5 ComboboxOption ​

类似 ListboxOption。

5. Dialog API 速查 ​

tsx
import { Dialog, DialogBackdrop, DialogPanel, DialogTitle, DialogDescription } from "@headlessui/react";

5.1 Dialog ​

Prop类型默认说明
openboolean-受控(必须)
onClose(value: false) => void-关闭回调(必须)
role'dialog' | 'alertdialog''dialog'a11y role
transitionbooleanfalse启用 transition
staticbooleanfalse不内置 mount/unmount
unmountbooleantrue关闭卸载
autoFocusbooleantrue自动聚焦
asElementType'div'渲染元素
__demoModeboolean-demo 模式

Render prop:{ open }

5.2 DialogBackdrop ​

Prop类型说明
asElementType'div'
transitionboolean启用 transition

5.3 DialogPanel ​

Prop类型说明
asElementType'div'
transitionboolean启用 transition

点击 Panel 外触发 onClose。

5.4 DialogTitle / DialogDescription ​

组件默认 as说明
DialogTitle'h2'a11y title(必需)
DialogDescription'p'a11y description

6. Popover API 速查 ​

tsx
import { Popover, PopoverButton, PopoverPanel, PopoverGroup, PopoverBackdrop } from "@headlessui/react";

6.1 Popover ​

Prop类型默认说明
asElementType'div'渲染元素

Render prop:{ open, close }

6.2 PopoverButton ​

类似 MenuButton。

6.3 PopoverPanel ​

Prop类型默认说明
asElementType'div'渲染元素
anchorAnchorProps-锚定
transitionbooleanfalse启用 transition
staticbooleanfalse始终渲染
unmountbooleantrue关闭卸载
portalboolean自动是否 portal
modalbooleanfalse是否 modal(默认 false,不锁滚动)
focusbooleanfalse强制聚焦内部

6.4 PopoverGroup ​

协调多个 Popover 的焦点流。

6.5 PopoverBackdrop ​

Prop类型说明
asElementType'div'
transitionboolean启用 transition

点击 backdrop 关闭 Popover。

7. Disclosure API 速查 ​

tsx
import { Disclosure, DisclosureButton, DisclosurePanel } from "@headlessui/react";

7.1 Disclosure ​

Prop类型默认说明
asElementTypeFragment渲染元素
defaultOpenbooleanfalse非受控初始 open

Render prop:{ open, close }

7.2 DisclosureButton ​

Prop类型说明
asElementType'button'
autoFocusboolean自动聚焦
disabledboolean禁用

Data- 属性*:data-open / data-focus / data-hover / data-active

7.3 DisclosurePanel ​

Prop类型默认说明
asElementType'div'渲染元素
transitionbooleanfalse启用 transition
staticbooleanfalse始终渲染
unmountbooleantrue关闭卸载

8. TabGroup API 速查 ​

tsx
import { TabGroup, TabList, Tab, TabPanels, TabPanel } from "@headlessui/react";

8.1 TabGroup ​

Prop类型默认说明
asElementType'div'渲染元素
defaultIndexnumber0非受控 index
selectedIndexnumber-受控 index
onChange(index: number) => void-切换回调
verticalbooleanfalse垂直布局
manualbooleanfalse键盘不自动切换

8.2 TabList ​

Prop类型说明
asElementType'div'

8.3 Tab ​

Prop类型说明
asElementType'button'
disabledboolean禁用
autoFocusboolean自动聚焦

Data- 属性*:data-selected / data-focus / data-hover / data-disabled / data-autofocusRender prop:{ selected, focus, hover, autofocus, disabled }

8.4 TabPanels / TabPanel ​

组件默认 as说明
TabPanels'div'容器
TabPanel'div'单个 panel

TabPanel props:

Prop类型说明
staticboolean始终渲染
unmountboolean切换时卸载(默认 true)

9. Switch API 速查 ​

tsx
import { Switch } from "@headlessui/react";
Prop类型默认说明
checkedboolean-受控
defaultCheckedbooleanfalse非受控
onChange(checked: boolean) => void-切换
asElementType'button'渲染元素
namestring-表单字段
formstring-关联表单
valuestring'on'提交值
disabledbooleanfalse禁用
autoFocusboolean-自动聚焦

Data- 属性*:data-checked / data-focus / data-hover / data-active / data-disabledRender prop:{ checked, focus, hover, autofocus, disabled, changing }

10. Checkbox API 速查(v2 新) ​

tsx
import { Checkbox } from "@headlessui/react";
Prop类型默认说明
checkedboolean-受控
defaultCheckedbooleanfalse非受控
onChange(checked: boolean) => void-切换
indeterminatebooleanfalseindeterminate 态
asElementType'span'渲染元素
namestring-表单字段
valuestring'on'提交值
disabledbooleanfalse禁用
autoFocusboolean-自动聚焦

Data- 属性*:data-checked / data-indeterminate / data-focus / data-hover / data-active / data-disabledRender prop:{ checked, focus, hover, indeterminate, disabled }

11. RadioGroup API 速查 ​

tsx
import { RadioGroup, Radio } from "@headlessui/react";

11.1 RadioGroup ​

Prop类型默认说明
valueT-受控
defaultValueT-非受控
onChange(value: T) => void-切换
disabledbooleanfalse禁用整组
namestring-表单字段
formstring-关联表单
bykeyof T | (a, b) => boolean-对象比较
asElementType'div'渲染元素

11.2 Radio ​

Prop类型说明
valueT选项值(必须)
disabledboolean禁用
asElementType'span'
autoFocusboolean自动聚焦

Data- 属性*:data-checked / data-focus / data-hover / data-disabledRender prop:{ checked, focus, hover, disabled }

12. Input / Textarea / Select API 速查(v2 新) ​

tsx
import { Input, Textarea, Select } from "@headlessui/react";
Prop类型说明
invalidboolean错误态
disabledboolean禁用
autoFocusboolean自动聚焦
asElementType'input' / 'textarea' / 'select'
所有原生 HTML 属性-type / placeholder / required / pattern...

Data- 属性*:data-focus / data-hover / data-invalid / data-disabled

13. Fieldset / Legend / Field / Label / Description API 速查(v2 新) ​

tsx
import { Fieldset, Legend, Field, Label, Description } from "@headlessui/react";

13.1 Fieldset ​

Prop类型说明
disabledboolean级联禁用所有内部表单控件
asElementType'fieldset'

Data- 属性*:data-disabledRender prop:{ disabled }

13.2 Legend ​

Prop类型说明
asElementType'div'(注意不是原生 <legend>)

13.3 Field ​

Prop类型说明
disabledboolean级联禁用
asElementType'div'

Data- 属性*:data-disabledRender prop:{ disabled }

13.4 Label / Description ​

组件默认 as说明
Label'label'自动 wire id 和 control 的 aria-labelledby
Description'p'自动 wire id 和 control 的 aria-describedby

Label 额外 prop:

Prop类型说明
passiveboolean点击 Label 不切换 control(默认 false)

14. Button API 速查(v2 新) ​

tsx
import { Button } from "@headlessui/react";
Prop类型说明
asElementType'button'
disabledboolean禁用
autoFocusboolean自动聚焦
所有 <button> HTML 属性-type / onClick / form...

Data- 属性*:data-focus / data-hover / data-active / data-disabled / data-autofocus

15. Transition API 速查 ​

tsx
import { Transition, TransitionChild } from "@headlessui/react";

15.1 Transition ​

Prop类型默认说明
showboolean-是否显示
appearbooleanfalse首次挂载播放 enter
asElementTypeFragment渲染元素
unmountbooleantrueleave 后卸载
enterstring-enter 阶段 className
enterFromstring-enter 起始 className
enterTostring-enter 终点 className
leavestring-leave 阶段 className
leaveFromstring-leave 起始 className
leaveTostring-leave 终点 className
beforeEnter() => void-enter 开始前
afterEnter() => void-enter 结束后
beforeLeave() => void-leave 开始前
afterLeave() => void-leave 结束后

Data- 属性*:data-closed / data-enter / data-leave / data-transition

15.2 TransitionChild ​

与 Transition 相同但不需要 show prop(继承父级)。

15.3 transition prop(其他组件用) ​

tsx
<MenuItems
  transition
  className="
    transition duration-200 ease-out
    data-[closed]:scale-95 data-[closed]:opacity-0
    data-[enter]:duration-200
    data-[leave]:duration-100
  "
>

适用组件:MenuItems / ListboxOptions / ComboboxOptions / PopoverPanel / DisclosurePanel / Dialog / DialogBackdrop / DialogPanel。

16. CloseButton API 速查 ​

tsx
import { CloseButton } from "@headlessui/react";
Prop类型说明
asElementType'button'

调用上下文最近的 close 函数(Dialog / Popover / Disclosure)。

tsx
<DisclosurePanel>
  <CloseButton>关闭</CloseButton>
</DisclosurePanel>

17. useClose Hook ​

tsx
import { useClose } from "@headlessui/react";

function CustomCloseButton() {
  const close = useClose();
  return <button onClick={close}>关闭</button>;
}

调用上下文最近的 close 函数(Dialog / Popover / Disclosure / Menu)。

18. Anchor Positioning 完整配置 ​

18.1 字符串语法 ​

值含义
"top"top center
"top start"top 左对齐
"top end"top 右对齐
"bottom"bottom center
"bottom start"bottom 左对齐
"bottom end"bottom 右对齐
"left"left center
"left start"left 顶部对齐
"left end"left 底部对齐
"right"right center
"right start"right 顶部对齐
"right end"right 底部对齐

18.2 对象语法 ​

ts
type AnchorProps = string | {
  to?: AnchorString;          // 同字符串语法
  gap?: number | string;       // 距 Trigger 距离
  offset?: number | string;    // 沿对齐方向偏移
  padding?: number | string;   // 视窗最小距离
};

18.3 CSS 变量 ​

变量含义
--anchor-gap距 Trigger 距离
--anchor-offset偏移
--anchor-padding视窗最小距离
--button-widthTrigger 宽度(只读)
--input-widthComboboxInput 宽度(只读)

18.4 适用组件 ​

MenuItems / ListboxOptions / ComboboxOptions / PopoverPanel。

19. Data-* 属性全表 ​

属性含义适用
data-open当前 openMenu / Listbox / Combobox / Popover / Disclosure / Dialog (Panel)
data-closedtransition closed 态任何 transition 组件
data-entertransition enter 阶段任何 transition 组件
data-leavetransition leave 阶段任何 transition 组件
data-transitiontransition 期间任何 transition 组件
data-focus键盘 focus(等价 :focus-visible)Trigger / Item / Input
data-hover鼠标 hover(触摸设备自动忽略)Trigger / Item
data-active鼠标按下(拖出自动消失)Trigger / Item
data-disabled禁用任何组件
data-selected当前选中Listbox/Combobox Option / Tab
data-checked选中态Checkbox / Switch / Radio
data-indeterminateindeterminate 态Checkbox
data-invalid错误态Input / Textarea / Select
data-autofocusautoFocus prop 设置Trigger / Input
data-headlessui-state综合状态(debug)任何组件

20. 键盘快捷键全表 ​

20.1 Menu ​

键上下文行为
Enter / SpaceButton focused打开菜单,聚焦首项
Arrow Down/UpButton focused打开菜单,聚焦首/末项
Esc菜单打开关闭菜单
Arrow Down/Up菜单打开上下移动
Home / PageUp菜单打开跳到首项
End / PageDown菜单打开跳到末项
Enter / Space菜单打开选中当前项
A-Z / a-z菜单打开typeahead 搜索
Tab菜单打开关闭菜单并跳到下一焦点

20.2 Listbox ​

同 Menu,且:

键行为
Arrow Up/Down移动选项
Enter/Space选中
Esc关闭
Home/End跳到首/末

20.3 Combobox ​

键行为
Arrow Down/Up移动选项
Enter选中当前 + 关闭
Esc清空 query 或关闭
Home/End跳到首/末
任意字符输入到 query

20.4 Dialog ​

键行为
Esc触发 onClose
Tab / Shift+Tab在 Dialog 内循环焦点

20.5 Tabs ​

键行为
Arrow Left/Right(水平)/ Up/Down(垂直)切换 tab
Home / End跳到首/末 tab
Enter / Space在 manual 模式下激活 tab
Tab跳到 panel

20.6 Disclosure ​

键行为
Enter / Space切换 panel

20.7 Switch / Checkbox ​

键行为
Space切换
Enter(在表单中)提交

20.8 RadioGroup ​

键行为
Arrow Up/Down / Left/Right切换选项
Space选中

20.9 Popover ​

键行为
Esc关闭
Tab / Shift+Tab离开 popover 后正常焦点流

21. TypeScript 核心类型 ​

21.1 Anchor ​

ts
type AnchorTo =
  | "top"
  | "top start"
  | "top end"
  | "bottom"
  | "bottom start"
  | "bottom end"
  | "left"
  | "left start"
  | "left end"
  | "right"
  | "right start"
  | "right end";

type AnchorProps =
  | AnchorTo
  | {
      to?: AnchorTo;
      gap?: number | string;
      offset?: number | string;
      padding?: number | string;
    };

21.2 组件 Props 类型 ​

ts
import type {
  MenuProps,
  MenuButtonProps,
  MenuItemsProps,
  MenuItemProps,
  ListboxProps,
  ComboboxProps,
  DialogProps,
  DialogPanelProps,
  PopoverProps,
  DisclosureProps,
  SwitchProps,
  CheckboxProps,
  RadioGroupProps,
  TabGroupProps,
  TransitionProps,
} from "@headlessui/react";

21.3 受控组件泛型 ​

tsx
{/* 单选 */}
type Person = { id: number; name: string };
<Listbox<Person> value={selected} onChange={setSelected}>...</Listbox>

{/* 多选 */}
<Listbox<Person[]> value={selectedList} onChange={setSelectedList} multiple>...</Listbox>

21.4 displayValue 类型 ​

tsx
<ComboboxInput<Person | null>
  displayValue={(p) => p?.name ?? ""}
  onChange={(e) => setQuery(e.target.value)}
/>

22. Vue v1 API 简要速查 ​

22.1 Menu ​

vue
<Menu as="div">
  <MenuButton>选项</MenuButton>
  <MenuItems>
    <MenuItem v-slot="{ active, disabled }">
      <a :class="active ? 'bg-blue-500' : ''">设置</a>
    </MenuItem>
  </MenuItems>
</Menu>

slot props:active(高亮)/ disabled

22.2 Listbox ​

vue
<Listbox v-model="selected">
  <ListboxButton>{{ selected?.name }}</ListboxButton>
  <ListboxOptions>
    <ListboxOption
      v-for="p in people"
      :key="p.id"
      :value="p"
      v-slot="{ active, selected }"
    >
      <li :class="active ? 'bg-blue-500' : ''">
        {{ p.name }}
        <span v-if="selected">✓</span>
      </li>
    </ListboxOption>
  </ListboxOptions>
</Listbox>

22.3 Dialog ​

vue
<TransitionRoot :show="isOpen" as="template">
  <Dialog @close="closeModal">
    <DialogOverlay class="fixed inset-0 bg-black/30" />
    <DialogPanel class="...">
      <DialogTitle>标题</DialogTitle>
      <DialogDescription>描述</DialogDescription>
    </DialogPanel>
  </Dialog>
</TransitionRoot>

注意:Vue 版的遮罩叫 DialogOverlay,React v2 叫 DialogBackdrop。

22.4 Combobox ​

vue
<Combobox v-model="selected">
  <ComboboxInput
    :display-value="(p) => p?.name"
    @change="query = $event.target.value"
  />
  <ComboboxOptions>
    <ComboboxOption
      v-for="p in filtered"
      :key="p.id"
      :value="p"
      v-slot="{ active, selected }"
    >
      <li :class="active ? 'bg-blue-500' : ''">{{ p.name }}</li>
    </ComboboxOption>
  </ComboboxOptions>
</Combobox>

22.5 Switch ​

vue
<Switch v-model="enabled" :class="enabled ? 'bg-blue-600' : 'bg-gray-200'">
  <span :class="enabled ? 'translate-x-6' : 'translate-x-1'" />
</Switch>

22.6 RadioGroup ​

vue
<RadioGroup v-model="selected">
  <RadioGroupOption
    v-for="plan in plans"
    :key="plan.id"
    :value="plan"
    v-slot="{ active, checked }"
  >
    <div :class="checked ? 'border-blue-500' : 'border-gray-300'">
      <RadioGroupLabel>{{ plan.name }}</RadioGroupLabel>
      <RadioGroupDescription>{{ plan.price }}</RadioGroupDescription>
    </div>
  </RadioGroupOption>
</RadioGroup>

22.7 Tabs ​

vue
<TabGroup>
  <TabList>
    <Tab v-slot="{ selected }">
      <button :class="selected ? 'border-b-2 border-blue-500' : ''">
        最新
      </button>
    </Tab>
  </TabList>
  <TabPanels>
    <TabPanel>内容 1</TabPanel>
  </TabPanels>
</TabGroup>

22.8 Vue v1 与 React v2 命名差异 ​

React v2Vue v1备注
DialogBackdropDialogOverlay遮罩
TransitionTransitionRoot动画包装
RadioRadioGroupOption单选项
Label(在 Listbox 内)ListboxLabel关联 label
Label(在 Combobox 内)ComboboxLabel关联 label
数据属性 (data-*)slot props (v-slot)状态消费
anchor prop无(用 Floating UI Vue 手动)定位
transition prop无(用 Vue <transition> 包装)动画
Checkbox / Input / 等不存在表单 v2 新组件

23. 包尺寸参考 ​

包gzipped 大小(约)
@headlessui/react v2~30 KB
@headlessui/vue v1~15 KB

依赖(仅 React v2):

  • @floating-ui/react ~6 KB
  • @tanstack/react-virtual ~5 KB
  • @react-aria/interactions ~3 KB

24. 与同类库 API 对比 ​

概念Headless UI v2Radix UIArk UI
包结构单包30+ 独立包 + 1 聚合包单包
命名扁平(MenuButton)命名空间(Menu.Trigger)命名空间
状态属性data-*data-state="open"data-state="open"
Slot 模式as={Fragment}asChildasChild
内置定位anchor prop(v2)无(Popover.Content side+align)positioning prop
内置 transitiontransition propdata-state + CSS 变量present API
虚拟滚动virtual.options(Combobox)无无
Compound Component浅(2-3 层)深(4-6 层)中等
Form Primitives完整 8 组件4 组件 + Form root类似
Vue 支持v1(部分)无(社区 Reka UI)有(完整)

25. 官方资源链接 ​