@agile-plus/headless 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +150 -2
  3. package/dist/index.cjs +2610 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +30 -0
  6. package/dist/index.d.ts +30 -0
  7. package/dist/index.js +2631 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/types-2ROaIfZ9.d.cts +57 -0
  10. package/dist/types-2ROaIfZ9.d.ts +57 -0
  11. package/dist/types-C9nl8qeg.d.cts +100 -0
  12. package/dist/types-C9nl8qeg.d.ts +100 -0
  13. package/dist/types-DM4s_xZa.d.cts +128 -0
  14. package/dist/types-DM4s_xZa.d.ts +128 -0
  15. package/dist/useCheckbox/index.cjs +255 -0
  16. package/dist/useCheckbox/index.cjs.map +1 -0
  17. package/dist/useCheckbox/index.d.cts +145 -0
  18. package/dist/useCheckbox/index.d.ts +145 -0
  19. package/dist/useCheckbox/index.js +232 -0
  20. package/dist/useCheckbox/index.js.map +1 -0
  21. package/dist/useCheckboxGroup/index.cjs +100 -0
  22. package/dist/useCheckboxGroup/index.cjs.map +1 -0
  23. package/dist/useCheckboxGroup/index.d.cts +102 -0
  24. package/dist/useCheckboxGroup/index.d.ts +102 -0
  25. package/dist/useCheckboxGroup/index.js +73 -0
  26. package/dist/useCheckboxGroup/index.js.map +1 -0
  27. package/dist/useCollapsible/index.cjs +151 -0
  28. package/dist/useCollapsible/index.cjs.map +1 -0
  29. package/dist/useCollapsible/index.d.cts +106 -0
  30. package/dist/useCollapsible/index.d.ts +106 -0
  31. package/dist/useCollapsible/index.js +129 -0
  32. package/dist/useCollapsible/index.js.map +1 -0
  33. package/dist/useDialog/index.cjs +294 -0
  34. package/dist/useDialog/index.cjs.map +1 -0
  35. package/dist/useDialog/index.d.cts +105 -0
  36. package/dist/useDialog/index.d.ts +105 -0
  37. package/dist/useDialog/index.js +267 -0
  38. package/dist/useDialog/index.js.map +1 -0
  39. package/dist/useForm/index.cjs +123 -0
  40. package/dist/useForm/index.cjs.map +1 -0
  41. package/dist/useForm/index.d.cts +22 -0
  42. package/dist/useForm/index.d.ts +22 -0
  43. package/dist/useForm/index.js +96 -0
  44. package/dist/useForm/index.js.map +1 -0
  45. package/dist/useFormControl/index.cjs +162 -0
  46. package/dist/useFormControl/index.cjs.map +1 -0
  47. package/dist/useFormControl/index.d.cts +116 -0
  48. package/dist/useFormControl/index.d.ts +116 -0
  49. package/dist/useFormControl/index.js +143 -0
  50. package/dist/useFormControl/index.js.map +1 -0
  51. package/dist/useFormField/index.cjs +45 -0
  52. package/dist/useFormField/index.cjs.map +1 -0
  53. package/dist/useFormField/index.d.cts +22 -0
  54. package/dist/useFormField/index.d.ts +22 -0
  55. package/dist/useFormField/index.js +18 -0
  56. package/dist/useFormField/index.js.map +1 -0
  57. package/dist/useFormMessage/index.cjs +77 -0
  58. package/dist/useFormMessage/index.cjs.map +1 -0
  59. package/dist/useFormMessage/index.d.cts +80 -0
  60. package/dist/useFormMessage/index.d.ts +80 -0
  61. package/dist/useFormMessage/index.js +54 -0
  62. package/dist/useFormMessage/index.js.map +1 -0
  63. package/dist/usePresence/index.cjs +117 -0
  64. package/dist/usePresence/index.cjs.map +1 -0
  65. package/dist/usePresence/index.d.cts +52 -0
  66. package/dist/usePresence/index.d.ts +52 -0
  67. package/dist/usePresence/index.js +90 -0
  68. package/dist/usePresence/index.js.map +1 -0
  69. package/dist/useRadioGroup/index.cjs +197 -0
  70. package/dist/useRadioGroup/index.cjs.map +1 -0
  71. package/dist/useRadioGroup/index.d.cts +162 -0
  72. package/dist/useRadioGroup/index.d.ts +162 -0
  73. package/dist/useRadioGroup/index.js +170 -0
  74. package/dist/useRadioGroup/index.js.map +1 -0
  75. package/dist/useScrollArea/index.cjs +309 -0
  76. package/dist/useScrollArea/index.cjs.map +1 -0
  77. package/dist/useScrollArea/index.d.cts +127 -0
  78. package/dist/useScrollArea/index.d.ts +127 -0
  79. package/dist/useScrollArea/index.js +287 -0
  80. package/dist/useScrollArea/index.js.map +1 -0
  81. package/dist/useSlider/index.cjs +417 -0
  82. package/dist/useSlider/index.cjs.map +1 -0
  83. package/dist/useSlider/index.d.cts +215 -0
  84. package/dist/useSlider/index.d.ts +215 -0
  85. package/dist/useSlider/index.js +401 -0
  86. package/dist/useSlider/index.js.map +1 -0
  87. package/dist/useSwitch/index.cjs +264 -0
  88. package/dist/useSwitch/index.cjs.map +1 -0
  89. package/dist/useSwitch/index.d.cts +110 -0
  90. package/dist/useSwitch/index.d.ts +110 -0
  91. package/dist/useSwitch/index.js +237 -0
  92. package/dist/useSwitch/index.js.map +1 -0
  93. package/dist/useTabs/index.cjs +153 -0
  94. package/dist/useTabs/index.cjs.map +1 -0
  95. package/dist/useTabs/index.d.cts +84 -0
  96. package/dist/useTabs/index.d.ts +84 -0
  97. package/dist/useTabs/index.js +126 -0
  98. package/dist/useTabs/index.js.map +1 -0
  99. package/dist/useToast/index.cjs +375 -0
  100. package/dist/useToast/index.cjs.map +1 -0
  101. package/dist/useToast/index.d.cts +148 -0
  102. package/dist/useToast/index.d.ts +148 -0
  103. package/dist/useToast/index.js +358 -0
  104. package/dist/useToast/index.js.map +1 -0
  105. package/dist/useToastProvider/index.cjs +118 -0
  106. package/dist/useToastProvider/index.cjs.map +1 -0
  107. package/dist/useToastProvider/index.d.cts +25 -0
  108. package/dist/useToastProvider/index.d.ts +25 -0
  109. package/dist/useToastProvider/index.js +96 -0
  110. package/dist/useToastProvider/index.js.map +1 -0
  111. package/dist/useToastViewport/index.cjs +227 -0
  112. package/dist/useToastViewport/index.cjs.map +1 -0
  113. package/dist/useToastViewport/index.d.cts +130 -0
  114. package/dist/useToastViewport/index.d.ts +130 -0
  115. package/dist/useToastViewport/index.js +207 -0
  116. package/dist/useToastViewport/index.js.map +1 -0
  117. package/dist/useToggle/index.cjs +86 -0
  118. package/dist/useToggle/index.cjs.map +1 -0
  119. package/dist/useToggle/index.d.cts +66 -0
  120. package/dist/useToggle/index.d.ts +66 -0
  121. package/dist/useToggle/index.js +59 -0
  122. package/dist/useToggle/index.js.map +1 -0
  123. package/dist/useToggleGroup/index.cjs +227 -0
  124. package/dist/useToggleGroup/index.cjs.map +1 -0
  125. package/dist/useToggleGroup/index.d.cts +159 -0
  126. package/dist/useToggleGroup/index.d.ts +159 -0
  127. package/dist/useToggleGroup/index.js +207 -0
  128. package/dist/useToggleGroup/index.js.map +1 -0
  129. package/package.json +163 -3
  130. package/src/index.ts +37 -0
  131. package/src/useCheckbox/index.ts +3 -0
  132. package/src/useCheckbox/types.ts +150 -0
  133. package/src/useCheckbox/useCheckbox.ts +141 -0
  134. package/src/useCheckboxGroup/index.ts +3 -0
  135. package/src/useCheckboxGroup/types.ts +104 -0
  136. package/src/useCheckboxGroup/useCheckboxGroup.ts +79 -0
  137. package/src/useCollapsible/index.ts +3 -0
  138. package/src/useCollapsible/types.ts +103 -0
  139. package/src/useCollapsible/useCollapsible.ts +160 -0
  140. package/src/useDialog/index.ts +3 -0
  141. package/src/useDialog/types.ts +109 -0
  142. package/src/useDialog/useDialog.ts +181 -0
  143. package/src/useForm/index.ts +9 -0
  144. package/src/useForm/types.ts +149 -0
  145. package/src/useForm/useForm.ts +156 -0
  146. package/src/useFormControl/index.ts +3 -0
  147. package/src/useFormControl/types.ts +101 -0
  148. package/src/useFormControl/useFormControl.ts +181 -0
  149. package/src/useFormField/index.ts +3 -0
  150. package/src/useFormField/types.ts +65 -0
  151. package/src/useFormField/useFormField.ts +46 -0
  152. package/src/useFormMessage/index.ts +3 -0
  153. package/src/useFormMessage/types.ts +64 -0
  154. package/src/useFormMessage/useFormMessage.ts +116 -0
  155. package/src/usePresence/index.ts +3 -0
  156. package/src/usePresence/types.ts +38 -0
  157. package/src/usePresence/usePresence.ts +168 -0
  158. package/src/useRadioGroup/index.ts +8 -0
  159. package/src/useRadioGroup/types.ts +173 -0
  160. package/src/useRadioGroup/useRadioGroup.ts +204 -0
  161. package/src/useScrollArea/index.ts +3 -0
  162. package/src/useScrollArea/types.ts +141 -0
  163. package/src/useScrollArea/useScrollArea.ts +360 -0
  164. package/src/useSlider/index.ts +3 -0
  165. package/src/useSlider/types.ts +231 -0
  166. package/src/useSlider/useSlider.ts +486 -0
  167. package/src/useSwitch/index.ts +3 -0
  168. package/src/useSwitch/types.ts +109 -0
  169. package/src/useSwitch/useSwitch.ts +144 -0
  170. package/src/useTabs/index.ts +3 -0
  171. package/src/useTabs/types.ts +83 -0
  172. package/src/useTabs/useTabs.ts +196 -0
  173. package/src/useToast/index.ts +3 -0
  174. package/src/useToast/types.ts +167 -0
  175. package/src/useToast/useToast.ts +378 -0
  176. package/src/useToastProvider/index.ts +3 -0
  177. package/src/useToastProvider/types.ts +121 -0
  178. package/src/useToastProvider/useToastProvider.ts +115 -0
  179. package/src/useToastViewport/index.ts +3 -0
  180. package/src/useToastViewport/types.ts +131 -0
  181. package/src/useToastViewport/useToastViewport.ts +319 -0
  182. package/src/useToggle/index.ts +3 -0
  183. package/src/useToggle/types.ts +59 -0
  184. package/src/useToggle/useToggle.ts +57 -0
  185. package/src/useToggleGroup/index.ts +3 -0
  186. package/src/useToggleGroup/types.ts +171 -0
  187. package/src/useToggleGroup/useToggleGroup.ts +260 -0
  188. package/src/utils/env.ts +26 -0
  189. package/src/utils/useBubbleInput.ts +168 -0
  190. package/src/utils/useCollection.ts +92 -0
  191. package/src/utils/useControllable.ts +96 -0
  192. package/src/utils/useFocusTrap.ts +160 -0
  193. package/src/utils/useFormResetListener.ts +80 -0
  194. package/src/utils/usePointerDrag.ts +101 -0
  195. package/src/utils/useResizeObserver.ts +60 -0
  196. package/src/utils/useScrollLock.ts +95 -0
@@ -0,0 +1,116 @@
1
+ import { computed, isRef, onScopeDispose, toValue, useId, watchEffect } from 'vue';
2
+
3
+ import {
4
+ BUILT_IN_VALIDITY_MESSAGES,
5
+ DEFAULT_INVALID_MESSAGE,
6
+ getMessageMatch,
7
+ } from '@agile-plus/primitives/form';
8
+
9
+ import type { CustomMatcher, ValidityMatcher } from '@agile-plus/primitives/form';
10
+
11
+ import type { Ref } from 'vue';
12
+
13
+ import type { UseFormReturn } from '../useForm';
14
+
15
+ import type { UseFormFieldReturn } from '../useFormField';
16
+
17
+ import type { UseFormMessageOptions, UseFormMessageReturn } from './types';
18
+
19
+ /**
20
+ * 取出 match 的当前值。
21
+ *
22
+ * 这里不能用 `toValue`:函数本身就是这条 API 的合法取值(自定义匹配器),而 `toValue`
23
+ * 会把函数当成 getter 调用一次,于是匹配器在毫无参数的情况下被执行。因此只接受「值」
24
+ * 或「Ref」两种形态。
25
+ */
26
+ function resolveMatch(
27
+ match:
28
+ ValidityMatcher | CustomMatcher | Ref<ValidityMatcher | CustomMatcher | undefined> | undefined
29
+ ): ValidityMatcher | CustomMatcher | undefined {
30
+ if (match === undefined) return undefined;
31
+
32
+ return isRef(match) ? match.value : match;
33
+ }
34
+
35
+ /**
36
+ * 一条错误文案的命中判定与登记。
37
+ *
38
+ * 两个登记动作决定了这条文案能被控件「看见」:
39
+ *
40
+ * 1. **id 进字段的说明列表**:控件的 `aria-describedby` 只包含当前正在显示的文案,
41
+ * 没显示的不能占位——否则读屏会念出一串空引用。
42
+ * 2. **函数式 match 进字段的匹配器清单**:匹配结果由控件在校验那一刻统一跑,
43
+ * 文案自己不去读控件的值。这样才能拿到跨字段比较所需的同一份 `FormData`,
44
+ * 也保证「原生错误优先」这条规则只实现一遍。
45
+ *
46
+ * 登记随实例的销毁自动撤销,所以文案可以随 `v-if` 反复出现消失,注册表不会积累脏条目。
47
+ *
48
+ * @param options 命中条件、forceMatch 与字段名。
49
+ * @param form 所在表单的注册表。
50
+ * @param field 所在字段的实例,可省略。
51
+ * @returns id、是否显示与缺省文案。
52
+ * @throws 既不在字段内、又没有显式 name 时抛出。
53
+ */
54
+ export function useFormMessage(
55
+ options: UseFormMessageOptions,
56
+ form: UseFormReturn,
57
+ field?: UseFormFieldReturn
58
+ ): UseFormMessageReturn {
59
+ const name = toValue(options.name) ?? field?.name.value;
60
+
61
+ if (!name) {
62
+ throw new Error('useFormMessage 需要 name:请把它放进 useFormField,或显式提供 name。');
63
+ }
64
+
65
+ const uid = useId();
66
+
67
+ const id = `message-${uid}`;
68
+
69
+ const match = computed(() => resolveMatch(options.match));
70
+
71
+ const matches = computed(() =>
72
+ getMessageMatch({
73
+ match: match.value as ValidityMatcher | CustomMatcher | undefined,
74
+ validity: form.getFieldValidity(name),
75
+ customErrors: form.getFieldCustomErrors(name),
76
+ messageId: id,
77
+ forceMatch: toValue(options.forceMatch) === true,
78
+ })
79
+ );
80
+
81
+ // 只有正在显示的文案才进 aria-describedby;隐藏中的 forceMatch 文案同样算显示。
82
+ watchEffect((onCleanup) => {
83
+ if (!matches.value) return;
84
+
85
+ form.addFieldMessageId(name, id);
86
+ onCleanup(() => form.removeFieldMessageId(name, id));
87
+ });
88
+
89
+ // 函数式 match 注册成匹配器。异步与否在这里不区分:区分发生在控件校验的那一刻,
90
+ // 因为只有它拿得到当前值与 FormData。
91
+ watchEffect((onCleanup) => {
92
+ const current = match.value;
93
+
94
+ if (typeof current !== 'function') return;
95
+
96
+ form.addFieldMatcher(name, { id, match: current });
97
+ onCleanup(() => form.removeFieldMatcher(name, id));
98
+ });
99
+
100
+ function getDefaultText(): string {
101
+ const current = match.value;
102
+
103
+ // 函数式与未提供 match 都没有可推导的原文,只能给通用文案;
104
+ // 字符串 match 才有内置文案表。
105
+ if (typeof current !== 'string') return DEFAULT_INVALID_MESSAGE;
106
+
107
+ return BUILT_IN_VALIDITY_MESSAGES[current] ?? DEFAULT_INVALID_MESSAGE;
108
+ }
109
+
110
+ onScopeDispose(() => {
111
+ form.removeFieldMessageId(name, id);
112
+ form.removeFieldMatcher(name, id);
113
+ });
114
+
115
+ return { id, name: computed(() => name), matches, getDefaultText };
116
+ }
@@ -0,0 +1,3 @@
1
+ export { usePresence } from './usePresence';
2
+
3
+ export type { UsePresenceOptions, UsePresenceReturn } from './types';
@@ -0,0 +1,38 @@
1
+ import type { ComputedRef, MaybeRefOrGetter, Ref } from 'vue';
2
+
3
+ /**
4
+ * usePresence 的配置项。
5
+ */
6
+ export interface UsePresenceOptions {
7
+ /**
8
+ * 元素是否应当存在于 DOM 中。
9
+ *
10
+ * 用 MaybeRefOrGetter 而不是裸 boolean:组件封装层传入的是响应式 props,裸值会在
11
+ * 展开时固化成快照,外部再次打开时 Presence 收不到变化、元素永远不会重新挂载。
12
+ */
13
+ present: MaybeRefOrGetter<boolean>;
14
+ }
15
+
16
+ /**
17
+ * usePresence 的返回值。
18
+ *
19
+ * 契约与库内其它 composable 一致:这里不返回可 v-bind 的 ARIA 属性,因为 Presence
20
+ * 不产生任何属性,只决定渲染与否。
21
+ */
22
+ export interface UsePresenceReturn {
23
+ /** 是否应当渲染元素;退出动画期间仍为 true */
24
+ isPresent: ComputedRef<boolean>;
25
+
26
+ /** 当前被观测的元素,未挂载时为 null */
27
+ element: Ref<HTMLElement | null>;
28
+
29
+ /**
30
+ * 绑定元素的入口,传给模板的 `:ref`。
31
+ *
32
+ * 用函数而不是直接把 Ref 交给模板:模板上的 `:ref` 需要同时能写内部状态和
33
+ * 处理卸载时的置空,函数入口是 Vue 明确支持的写法。
34
+ *
35
+ * @param node 元素实例,卸载时为 null。
36
+ */
37
+ setElement(node: unknown): void;
38
+ }
@@ -0,0 +1,168 @@
1
+ import { computed, onScopeDispose, ref, toValue, watch } from 'vue';
2
+
3
+ import { isPresencePresent, sendPresenceEvent } from '@agile-plus/primitives/presence';
4
+
5
+ import type { PresenceEvent, PresenceState } from '@agile-plus/primitives/presence';
6
+
7
+ import type { UsePresenceOptions, UsePresenceReturn } from './types';
8
+
9
+ /**
10
+ * Presence:把「应当存在」翻译成「是否留在 DOM 中」。
11
+ *
12
+ * 状态、事件与转移表都在 primitives/presence,本 composable 只负责两件事:判断
13
+ * 「此刻有没有退出动画在跑」,以及把动画事件桥接回状态机。
14
+ *
15
+ * 判定依赖 `animationstart` / `animationend` / `animationcancel` 与 `animation-name`
16
+ * 的变化,这一点与 Radix 一致。**因此本库组件的进出场必须写成 @keyframes 动画**:
17
+ * transition 被打断时没有对应事件,用它做退出动画会把元素永久留在 DOM 里。
18
+ *
19
+ * @param options Presence 配置。
20
+ * @returns 渲染开关与元素绑定入口。
21
+ */
22
+ export function usePresence(options: UsePresenceOptions): UsePresenceReturn {
23
+ const state = ref<PresenceState>(toValue(options.present) ? 'mounted' : 'unmounted');
24
+ const element = ref<HTMLElement | null>(null);
25
+ const isPresent = computed(() => isPresencePresent(state.value));
26
+
27
+ let previousAnimationName = 'none';
28
+
29
+ let fillModeTimer = 0;
30
+
31
+ /**
32
+ * 即时读取计算样式。
33
+ *
34
+ * Radix 缓存 getComputedStyle 的返回对象,但那是为了绕开 React 兄弟副作用污染
35
+ * body 样式后强制重算的问题;这里只在 present 变化与动画结束时读取,两个时机都
36
+ * 是低频,直接读取更可靠:缓存对象在 jsdom 下不会随内联样式更新,会让退出动画的
37
+ * 判定无法在单元测试里覆盖。
38
+ */
39
+ function readStyles(): CSSStyleDeclaration | null {
40
+ if (!element.value) return null;
41
+
42
+ return element.value.ownerDocument.defaultView?.getComputedStyle(element.value) ?? null;
43
+ }
44
+
45
+ function readAnimationName(): string {
46
+ return readStyles()?.animationName || 'none';
47
+ }
48
+
49
+ /**
50
+ * 推进状态机并同步动画名基线。
51
+ *
52
+ * 进入 mounted 时把基线设为当前动画名,退出时才能比对出差异;其它状态重置为
53
+ * 'none',与 Radix 在 passive effect 里的处理一致。
54
+ */
55
+ function send(event: PresenceEvent): void {
56
+ const next = sendPresenceEvent(state.value, event);
57
+
58
+ if (next === state.value) return;
59
+
60
+ state.value = next;
61
+ previousAnimationName = next === 'mounted' ? readAnimationName() : 'none';
62
+ }
63
+
64
+ function handleAnimationStart(event: AnimationEvent): void {
65
+ if (event.target !== element.value) return;
66
+
67
+ previousAnimationName = readAnimationName();
68
+ }
69
+
70
+ function handleAnimationFinished(event: AnimationEvent): void {
71
+ const node = element.value;
72
+
73
+ if (!node || event.target !== node) return;
74
+
75
+ const currentAnimationName = readAnimationName();
76
+
77
+ // 只认当前生效的动画:进入动画被打断同样派发 animationcancel,
78
+ // 那时不能把挂起中的退出当成结束。
79
+ if (!currentAnimationName.includes(event.animationName)) return;
80
+
81
+ send('ANIMATION_END');
82
+
83
+ if (toValue(options.present)) return;
84
+
85
+ /**
86
+ * 动画结束到节点移除之间仍有一帧可见内容,会闪一下。
87
+ *
88
+ * 把 fill-mode 强制成 forwards 让节点保持最后一个关键帧的样式,之后再还原
89
+ * (有些消费者会保留元素)。时机必须是 setTimeout 而不是 rAF——rAF 仍在同一帧
90
+ * 内,闪屏照旧,这是 Radix 注释里明确写过的坑。
91
+ */
92
+ const previousFillMode = node.style.animationFillMode;
93
+
94
+ node.style.animationFillMode = 'forwards';
95
+
96
+ fillModeTimer = window.setTimeout(() => {
97
+ if (node.style.animationFillMode === 'forwards') {
98
+ node.style.animationFillMode = previousFillMode;
99
+ }
100
+ });
101
+ }
102
+
103
+ function setElement(node: unknown): void {
104
+ element.value = node instanceof HTMLElement ? node : null;
105
+ }
106
+
107
+ watch(element, (node, previousNode) => {
108
+ window.clearTimeout(fillModeTimer);
109
+
110
+ previousNode?.removeEventListener('animationstart', handleAnimationStart);
111
+ previousNode?.removeEventListener('animationcancel', handleAnimationFinished);
112
+ previousNode?.removeEventListener('animationend', handleAnimationFinished);
113
+
114
+ if (!node) {
115
+ // 节点被外部提前移除(父级 v-if 变化等),不会有动画事件回来,
116
+ // 直接按结束处理,避免卡在 unmountSuspended。
117
+ send('ANIMATION_END');
118
+
119
+ return;
120
+ }
121
+
122
+ if (state.value === 'mounted') previousAnimationName = readAnimationName();
123
+
124
+ node.addEventListener('animationstart', handleAnimationStart);
125
+ node.addEventListener('animationcancel', handleAnimationFinished);
126
+ node.addEventListener('animationend', handleAnimationFinished);
127
+ });
128
+
129
+ // flush: 'post' 让回调在 DOM 补丁之后执行,此时 data-state 已经落到元素上,
130
+ // 读到的 animation-name 才反映退出动画是否真的开始。
131
+ watch(
132
+ () => toValue(options.present),
133
+ (present, wasPresent) => {
134
+ if (present) {
135
+ send('MOUNT');
136
+
137
+ return;
138
+ }
139
+
140
+ const currentAnimationName = readAnimationName();
141
+ const hasExitAnimation = currentAnimationName !== 'none' && readStyles()?.display !== 'none';
142
+ const isAnimating = previousAnimationName !== currentAnimationName;
143
+
144
+ if (!wasPresent || !hasExitAnimation || !isAnimating) {
145
+ send('UNMOUNT');
146
+
147
+ return;
148
+ }
149
+
150
+ send('ANIMATION_OUT');
151
+ },
152
+ { flush: 'post', immediate: true }
153
+ );
154
+
155
+ onScopeDispose(() => {
156
+ window.clearTimeout(fillModeTimer);
157
+
158
+ const node = element.value;
159
+
160
+ if (!node) return;
161
+
162
+ node.removeEventListener('animationstart', handleAnimationStart);
163
+ node.removeEventListener('animationcancel', handleAnimationFinished);
164
+ node.removeEventListener('animationend', handleAnimationFinished);
165
+ });
166
+
167
+ return { isPresent, element, setElement };
168
+ }
@@ -0,0 +1,8 @@
1
+ export type {
2
+ RadioGroupItemEntry,
3
+ RadioGroupOrientation,
4
+ UseRadioGroupOptions,
5
+ UseRadioGroupReturn,
6
+ } from './types';
7
+
8
+ export { useRadioGroup } from './useRadioGroup';
@@ -0,0 +1,173 @@
1
+ import type { ComputedRef, MaybeRefOrGetter } from 'vue';
2
+
3
+ /**
4
+ * RadioGroup 的排列方向。
5
+ *
6
+ * 与 Tabs 不同,方向不改变按键映射:radio group 遵循原生行为,
7
+ * 上下左右四个方向键都能导航。该值只用于输出 aria-orientation。
8
+ */
9
+ export type RadioGroupOrientation = 'horizontal' | 'vertical';
10
+
11
+ /**
12
+ * useRadioGroup Composable 的配置选项。
13
+ *
14
+ * 同时支持受控与非受控两种模式:
15
+ * - 非受控:仅传 defaultValue,内部维护选中值。
16
+ * - 受控:传入 modelValue + onValueChange,外部控制选中值。
17
+ *
18
+ * 受控类参数使用 MaybeRefOrGetter 而非裸值:组件封装层传入的是响应式 props,
19
+ * 裸值会在展开时固化为快照,导致外部状态变化后界面不再响应。
20
+ */
21
+ export interface UseRadioGroupOptions {
22
+ /** 受控选中值,传入后进入受控模式;空字符串表示受控地清空选择 */
23
+ modelValue?: MaybeRefOrGetter<string | undefined>;
24
+
25
+ /**
26
+ * 非受控模式的初始选中值。
27
+ *
28
+ * 允许为空,radio 语义支持「未填写」初始态,与 Tabs 必须存在激活项不同。
29
+ *
30
+ * @default ''
31
+ */
32
+ defaultValue?: string;
33
+
34
+ /** 选中值变化回调,受控模式下必须提供 */
35
+ onValueChange?: (value: string) => void;
36
+
37
+ /**
38
+ * 整组禁用。
39
+ *
40
+ * 禁用后所有 item 不可交互、不可通过键盘到达,导航列表变为空。
41
+ *
42
+ * @default false
43
+ */
44
+ disabled?: MaybeRefOrGetter<boolean | undefined>;
45
+
46
+ /**
47
+ * 是否为必选组,输出 aria-required。
48
+ *
49
+ * @default false
50
+ */
51
+ required?: MaybeRefOrGetter<boolean | undefined>;
52
+
53
+ /**
54
+ * 键盘导航到达边界后是否循环到另一端。
55
+ *
56
+ * @default true
57
+ */
58
+ loop?: boolean;
59
+
60
+ /**
61
+ * 排列方向,仅用于输出 aria-orientation。
62
+ *
63
+ * @default 'horizontal'
64
+ */
65
+ orientation?: MaybeRefOrGetter<RadioGroupOrientation | undefined>;
66
+
67
+ /** 指向组标题元素 id 的输出 aria-labelledby,由消费者自行关联标签元素 */
68
+ ariaLabelledby?: MaybeRefOrGetter<string | undefined>;
69
+
70
+ /** 指向组说明元素 id 的输出 aria-describedby,由消费者自行关联说明元素 */
71
+ ariaDescribedby?: MaybeRefOrGetter<string | undefined>;
72
+
73
+ /**
74
+ * 表单提交用的字段名,对应隐藏 input 的 name。
75
+ *
76
+ * 未提供时不渲染隐藏 input:没有提交目标的情况下多出的原生控件只会带来
77
+ * 额外的焦点与分组行为。
78
+ */
79
+ name?: MaybeRefOrGetter<string | undefined>;
80
+ }
81
+
82
+ /**
83
+ * item 注册表条目。
84
+ *
85
+ * 只记录顺序与禁用态:顺序决定键盘导航与首个可聚焦项,
86
+ * 禁用态需要集中可见,否则无法跳过禁用项,也无法确定唯一 tab stop。
87
+ */
88
+ export interface RadioGroupItemEntry {
89
+ /** item 的唯一值 */
90
+ value: string;
91
+
92
+ /**
93
+ * 该 item 是否单独禁用。
94
+ *
95
+ * 不含整组禁用情形,最终可用性由 Composable 合并组级 disabled 得出。
96
+ */
97
+ disabled: boolean;
98
+ }
99
+
100
+ /**
101
+ * useRadioGroup Composable 的返回值。
102
+ *
103
+ * 契约与 Dialog / Tabs 一致:get*Props 只返回可 v-bind 的 ARIA 属性,
104
+ * 事件通过 v-on 单独绑定。
105
+ */
106
+ export interface UseRadioGroupReturn {
107
+ /** 当前选中值,空字符串表示未选中 */
108
+ value: ComputedRef<string>;
109
+
110
+ /** 整组是否被禁用 */
111
+ isGroupDisabled: ComputedRef<boolean>;
112
+
113
+ /** 设置选中值,不校验目标是否禁用,由调用方决定 */
114
+ setValue(value: string): void;
115
+
116
+ /**
117
+ * 注册一个 item。
118
+ *
119
+ * 由 RadioGroupItem 在 setup 阶段调用,重复注册幂等忽略。
120
+ * 注册顺序即键盘导航顺序,必须与视觉顺序一致。
121
+ */
122
+ registerItem(value: string): void;
123
+
124
+ /** 更新 item 的禁用态,由 RadioGroupItem 在 props.disabled 变化时调用 */
125
+ setItemDisabled(value: string, disabled: boolean): void;
126
+
127
+ /** 注销 item,由 RadioGroupItem 卸载时调用;若注销的是选中项则清空选中 */
128
+ unregisterItem(value: string): void;
129
+
130
+ /**
131
+ * 判断 item 是否不可交互。
132
+ *
133
+ * 合并整组禁用与单项禁用,供 UI 层决定 data-disabled 与 tabindex。
134
+ */
135
+ isItemDisabled(value: string): boolean;
136
+
137
+ /** 获取组容器需要绑定的 ARIA 属性 */
138
+ getRootProps(): Record<string, unknown>;
139
+
140
+ /**
141
+ * 获取单个 item 需要绑定的 ARIA 属性。
142
+ *
143
+ * 纯读取函数,可安全放入 computed 随状态重新求值。
144
+ *
145
+ * @param value 该 item 的唯一值。
146
+ */
147
+ getItemProps(value: string): Record<string, unknown>;
148
+
149
+ /**
150
+ * 是否应当渲染隐藏的原生 input。
151
+ *
152
+ * 由是否提供 name 决定,UI 层据此控制 DOM 是否存在。
153
+ */
154
+ hasName: ComputedRef<boolean>;
155
+
156
+ /**
157
+ * 获取隐藏 input 需要绑定的属性。
158
+ *
159
+ * 未提供 name 时返回 null,UI 层应跳过渲染。禁用项同样输出 disabled,
160
+ * 让浏览器在提交时自然忽略它,而不是由组件模拟一套选择规则。
161
+ *
162
+ * @param value 该选项的唯一值。
163
+ * @returns 可 v-bind 的属性,或 null。
164
+ */
165
+ getInputProps(value: string): Record<string, unknown> | null;
166
+
167
+ /**
168
+ * item 键盘导航处理器,绑定到 item 的 @keydown。
169
+ *
170
+ * @param event 原生键盘事件。
171
+ */
172
+ onItemKeydown(event: KeyboardEvent): void;
173
+ }