@agile-plus/headless 0.0.0-stage → 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +59 -2
- package/dist/index.cjs +2610 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +2631 -0
- package/dist/index.js.map +1 -0
- package/dist/types-2ROaIfZ9.d.cts +57 -0
- package/dist/types-2ROaIfZ9.d.ts +57 -0
- package/dist/types-C9nl8qeg.d.cts +100 -0
- package/dist/types-C9nl8qeg.d.ts +100 -0
- package/dist/types-DM4s_xZa.d.cts +128 -0
- package/dist/types-DM4s_xZa.d.ts +128 -0
- package/dist/useCheckbox/index.cjs +255 -0
- package/dist/useCheckbox/index.cjs.map +1 -0
- package/dist/useCheckbox/index.d.cts +145 -0
- package/dist/useCheckbox/index.d.ts +145 -0
- package/dist/useCheckbox/index.js +232 -0
- package/dist/useCheckbox/index.js.map +1 -0
- package/dist/useCheckboxGroup/index.cjs +100 -0
- package/dist/useCheckboxGroup/index.cjs.map +1 -0
- package/dist/useCheckboxGroup/index.d.cts +102 -0
- package/dist/useCheckboxGroup/index.d.ts +102 -0
- package/dist/useCheckboxGroup/index.js +73 -0
- package/dist/useCheckboxGroup/index.js.map +1 -0
- package/dist/useCollapsible/index.cjs +151 -0
- package/dist/useCollapsible/index.cjs.map +1 -0
- package/dist/useCollapsible/index.d.cts +106 -0
- package/dist/useCollapsible/index.d.ts +106 -0
- package/dist/useCollapsible/index.js +129 -0
- package/dist/useCollapsible/index.js.map +1 -0
- package/dist/useDialog/index.cjs +294 -0
- package/dist/useDialog/index.cjs.map +1 -0
- package/dist/useDialog/index.d.cts +105 -0
- package/dist/useDialog/index.d.ts +105 -0
- package/dist/useDialog/index.js +267 -0
- package/dist/useDialog/index.js.map +1 -0
- package/dist/useForm/index.cjs +123 -0
- package/dist/useForm/index.cjs.map +1 -0
- package/dist/useForm/index.d.cts +22 -0
- package/dist/useForm/index.d.ts +22 -0
- package/dist/useForm/index.js +96 -0
- package/dist/useForm/index.js.map +1 -0
- package/dist/useFormControl/index.cjs +162 -0
- package/dist/useFormControl/index.cjs.map +1 -0
- package/dist/useFormControl/index.d.cts +116 -0
- package/dist/useFormControl/index.d.ts +116 -0
- package/dist/useFormControl/index.js +143 -0
- package/dist/useFormControl/index.js.map +1 -0
- package/dist/useFormField/index.cjs +45 -0
- package/dist/useFormField/index.cjs.map +1 -0
- package/dist/useFormField/index.d.cts +22 -0
- package/dist/useFormField/index.d.ts +22 -0
- package/dist/useFormField/index.js +18 -0
- package/dist/useFormField/index.js.map +1 -0
- package/dist/useFormMessage/index.cjs +77 -0
- package/dist/useFormMessage/index.cjs.map +1 -0
- package/dist/useFormMessage/index.d.cts +80 -0
- package/dist/useFormMessage/index.d.ts +80 -0
- package/dist/useFormMessage/index.js +54 -0
- package/dist/useFormMessage/index.js.map +1 -0
- package/dist/usePresence/index.cjs +117 -0
- package/dist/usePresence/index.cjs.map +1 -0
- package/dist/usePresence/index.d.cts +52 -0
- package/dist/usePresence/index.d.ts +52 -0
- package/dist/usePresence/index.js +90 -0
- package/dist/usePresence/index.js.map +1 -0
- package/dist/useRadioGroup/index.cjs +197 -0
- package/dist/useRadioGroup/index.cjs.map +1 -0
- package/dist/useRadioGroup/index.d.cts +162 -0
- package/dist/useRadioGroup/index.d.ts +162 -0
- package/dist/useRadioGroup/index.js +170 -0
- package/dist/useRadioGroup/index.js.map +1 -0
- package/dist/useScrollArea/index.cjs +309 -0
- package/dist/useScrollArea/index.cjs.map +1 -0
- package/dist/useScrollArea/index.d.cts +127 -0
- package/dist/useScrollArea/index.d.ts +127 -0
- package/dist/useScrollArea/index.js +287 -0
- package/dist/useScrollArea/index.js.map +1 -0
- package/dist/useSlider/index.cjs +417 -0
- package/dist/useSlider/index.cjs.map +1 -0
- package/dist/useSlider/index.d.cts +215 -0
- package/dist/useSlider/index.d.ts +215 -0
- package/dist/useSlider/index.js +401 -0
- package/dist/useSlider/index.js.map +1 -0
- package/dist/useSwitch/index.cjs +264 -0
- package/dist/useSwitch/index.cjs.map +1 -0
- package/dist/useSwitch/index.d.cts +110 -0
- package/dist/useSwitch/index.d.ts +110 -0
- package/dist/useSwitch/index.js +237 -0
- package/dist/useSwitch/index.js.map +1 -0
- package/dist/useTabs/index.cjs +153 -0
- package/dist/useTabs/index.cjs.map +1 -0
- package/dist/useTabs/index.d.cts +84 -0
- package/dist/useTabs/index.d.ts +84 -0
- package/dist/useTabs/index.js +126 -0
- package/dist/useTabs/index.js.map +1 -0
- package/dist/useToast/index.cjs +375 -0
- package/dist/useToast/index.cjs.map +1 -0
- package/dist/useToast/index.d.cts +148 -0
- package/dist/useToast/index.d.ts +148 -0
- package/dist/useToast/index.js +358 -0
- package/dist/useToast/index.js.map +1 -0
- package/dist/useToastProvider/index.cjs +118 -0
- package/dist/useToastProvider/index.cjs.map +1 -0
- package/dist/useToastProvider/index.d.cts +25 -0
- package/dist/useToastProvider/index.d.ts +25 -0
- package/dist/useToastProvider/index.js +96 -0
- package/dist/useToastProvider/index.js.map +1 -0
- package/dist/useToastViewport/index.cjs +227 -0
- package/dist/useToastViewport/index.cjs.map +1 -0
- package/dist/useToastViewport/index.d.cts +130 -0
- package/dist/useToastViewport/index.d.ts +130 -0
- package/dist/useToastViewport/index.js +207 -0
- package/dist/useToastViewport/index.js.map +1 -0
- package/dist/useToggle/index.cjs +86 -0
- package/dist/useToggle/index.cjs.map +1 -0
- package/dist/useToggle/index.d.cts +66 -0
- package/dist/useToggle/index.d.ts +66 -0
- package/dist/useToggle/index.js +59 -0
- package/dist/useToggle/index.js.map +1 -0
- package/dist/useToggleGroup/index.cjs +227 -0
- package/dist/useToggleGroup/index.cjs.map +1 -0
- package/dist/useToggleGroup/index.d.cts +159 -0
- package/dist/useToggleGroup/index.d.ts +159 -0
- package/dist/useToggleGroup/index.js +207 -0
- package/dist/useToggleGroup/index.js.map +1 -0
- package/package.json +163 -3
- package/src/index.ts +37 -0
- package/src/useCheckbox/index.ts +3 -0
- package/src/useCheckbox/types.ts +150 -0
- package/src/useCheckbox/useCheckbox.ts +141 -0
- package/src/useCheckboxGroup/index.ts +3 -0
- package/src/useCheckboxGroup/types.ts +104 -0
- package/src/useCheckboxGroup/useCheckboxGroup.ts +79 -0
- package/src/useCollapsible/index.ts +3 -0
- package/src/useCollapsible/types.ts +103 -0
- package/src/useCollapsible/useCollapsible.ts +160 -0
- package/src/useDialog/index.ts +3 -0
- package/src/useDialog/types.ts +109 -0
- package/src/useDialog/useDialog.ts +181 -0
- package/src/useForm/index.ts +9 -0
- package/src/useForm/types.ts +149 -0
- package/src/useForm/useForm.ts +156 -0
- package/src/useFormControl/index.ts +3 -0
- package/src/useFormControl/types.ts +101 -0
- package/src/useFormControl/useFormControl.ts +181 -0
- package/src/useFormField/index.ts +3 -0
- package/src/useFormField/types.ts +65 -0
- package/src/useFormField/useFormField.ts +46 -0
- package/src/useFormMessage/index.ts +3 -0
- package/src/useFormMessage/types.ts +64 -0
- package/src/useFormMessage/useFormMessage.ts +116 -0
- package/src/usePresence/index.ts +3 -0
- package/src/usePresence/types.ts +38 -0
- package/src/usePresence/usePresence.ts +168 -0
- package/src/useRadioGroup/index.ts +8 -0
- package/src/useRadioGroup/types.ts +173 -0
- package/src/useRadioGroup/useRadioGroup.ts +204 -0
- package/src/useScrollArea/index.ts +3 -0
- package/src/useScrollArea/types.ts +141 -0
- package/src/useScrollArea/useScrollArea.ts +360 -0
- package/src/useSlider/index.ts +3 -0
- package/src/useSlider/types.ts +231 -0
- package/src/useSlider/useSlider.ts +486 -0
- package/src/useSwitch/index.ts +3 -0
- package/src/useSwitch/types.ts +109 -0
- package/src/useSwitch/useSwitch.ts +144 -0
- package/src/useTabs/index.ts +3 -0
- package/src/useTabs/types.ts +83 -0
- package/src/useTabs/useTabs.ts +196 -0
- package/src/useToast/index.ts +3 -0
- package/src/useToast/types.ts +167 -0
- package/src/useToast/useToast.ts +378 -0
- package/src/useToastProvider/index.ts +3 -0
- package/src/useToastProvider/types.ts +121 -0
- package/src/useToastProvider/useToastProvider.ts +115 -0
- package/src/useToastViewport/index.ts +3 -0
- package/src/useToastViewport/types.ts +131 -0
- package/src/useToastViewport/useToastViewport.ts +319 -0
- package/src/useToggle/index.ts +3 -0
- package/src/useToggle/types.ts +59 -0
- package/src/useToggle/useToggle.ts +57 -0
- package/src/useToggleGroup/index.ts +3 -0
- package/src/useToggleGroup/types.ts +171 -0
- package/src/useToggleGroup/useToggleGroup.ts +260 -0
- package/src/utils/env.ts +26 -0
- package/src/utils/useBubbleInput.ts +168 -0
- package/src/utils/useCollection.ts +92 -0
- package/src/utils/useControllable.ts +96 -0
- package/src/utils/useFocusTrap.ts +160 -0
- package/src/utils/useFormResetListener.ts +80 -0
- package/src/utils/usePointerDrag.ts +101 -0
- package/src/utils/useResizeObserver.ts +60 -0
- package/src/utils/useScrollLock.ts +95 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ToggleGroupDirection,
|
|
3
|
+
ToggleGroupOrientation,
|
|
4
|
+
ToggleGroupType,
|
|
5
|
+
} from '@agile-plus/primitives/toggle';
|
|
6
|
+
|
|
7
|
+
import type { ComputedRef, MaybeRefOrGetter, Ref } from 'vue';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* useToggleGroup 的配置项。
|
|
11
|
+
*/
|
|
12
|
+
export interface UseToggleGroupOptions {
|
|
13
|
+
/**
|
|
14
|
+
* 选择模式,必填。
|
|
15
|
+
*
|
|
16
|
+
* 缺失时直接抛错,与 Radix 的 `Missing prop type expected on ToggleGroup` 一致:
|
|
17
|
+
* 它决定值形状(string 还是 string[])与 ARIA 角色,猜一个默认值会静默改变语义。
|
|
18
|
+
*/
|
|
19
|
+
type: MaybeRefOrGetter<ToggleGroupType>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 受控值,传入后进入受控模式。
|
|
23
|
+
*
|
|
24
|
+
* single 是字符串(空字符串表示未选中),multiple 是字符串数组。类型写成联合是
|
|
25
|
+
* Vue 侧的限制:模板上无法像 React 那样用泛型按 `type` 收窄,具体形状由 `type`
|
|
26
|
+
* 在运行期决定并在文档中约定。
|
|
27
|
+
*/
|
|
28
|
+
value?: MaybeRefOrGetter<string | string[] | undefined>;
|
|
29
|
+
|
|
30
|
+
/** 非受控模式的初始值,形状与 `value` 相同 */
|
|
31
|
+
defaultValue?: MaybeRefOrGetter<string | string[] | undefined>;
|
|
32
|
+
|
|
33
|
+
/** 值变化回调,负载形状跟随 `type`:single 给字符串,multiple 给数组 */
|
|
34
|
+
onValueChange?: (value: string | string[]) => void;
|
|
35
|
+
|
|
36
|
+
/** 整组禁用,项级禁用状态由「组禁用或项禁用」合成 */
|
|
37
|
+
disabled?: MaybeRefOrGetter<boolean | undefined>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 是否维持罗盘焦点,默认 true。
|
|
41
|
+
*
|
|
42
|
+
* 关闭后整组退化成一堆普通按钮:每个都进 Tab 序列、方向键不接管,这与 Radix 的
|
|
43
|
+
* `rovingFocus={false}` 分支一致。
|
|
44
|
+
*/
|
|
45
|
+
rovingFocus?: MaybeRefOrGetter<boolean | undefined>;
|
|
46
|
+
|
|
47
|
+
/** 焦点到达两端后是否绕回,默认 true */
|
|
48
|
+
loop?: MaybeRefOrGetter<boolean | undefined>;
|
|
49
|
+
|
|
50
|
+
/** 排布方向,决定哪一组方向键有效 */
|
|
51
|
+
orientation?: MaybeRefOrGetter<ToggleGroupOrientation | undefined>;
|
|
52
|
+
|
|
53
|
+
/** 文本方向,RTL 下左右方向键互换 */
|
|
54
|
+
dir?: MaybeRefOrGetter<ToggleGroupDirection | undefined>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 登记进罗盘焦点的一个项。
|
|
59
|
+
*
|
|
60
|
+
* 保存 Ref 而不是快照:项可能在任意时刻被禁用或选中,键盘导航发生时必须读到当下
|
|
61
|
+
* 的状态与真实元素。
|
|
62
|
+
*/
|
|
63
|
+
export interface ToggleGroupItemEntry {
|
|
64
|
+
/** 罗盘焦点 id,由项用 useId 生成;与选中值无关,因为值允许为空且可能重复登记 */
|
|
65
|
+
id: string;
|
|
66
|
+
|
|
67
|
+
/** 该项的选中值 */
|
|
68
|
+
value: string;
|
|
69
|
+
|
|
70
|
+
/** 项元素,未挂载时为 null */
|
|
71
|
+
element: Ref<HTMLElement | null>;
|
|
72
|
+
|
|
73
|
+
/** 是否参与罗盘焦点(组禁用或项禁用时为 false) */
|
|
74
|
+
focusable: ComputedRef<boolean>;
|
|
75
|
+
|
|
76
|
+
/** 是否处于选中状态,Tab 进入组时焦点优先落到它 */
|
|
77
|
+
active: ComputedRef<boolean>;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* useToggleGroup 的返回值。
|
|
82
|
+
*/
|
|
83
|
+
export interface UseToggleGroupReturn {
|
|
84
|
+
/** 选择模式 */
|
|
85
|
+
type: ComputedRef<ToggleGroupType>;
|
|
86
|
+
|
|
87
|
+
/** 当前选中值集合(内部统一数组) */
|
|
88
|
+
value: ComputedRef<string[]>;
|
|
89
|
+
|
|
90
|
+
/** 整组是否禁用 */
|
|
91
|
+
disabled: ComputedRef<boolean>;
|
|
92
|
+
|
|
93
|
+
/** 是否启用罗盘焦点 */
|
|
94
|
+
rovingFocus: ComputedRef<boolean>;
|
|
95
|
+
|
|
96
|
+
/** 排布方向 */
|
|
97
|
+
orientation: ComputedRef<ToggleGroupOrientation | undefined>;
|
|
98
|
+
|
|
99
|
+
/** 文本方向 */
|
|
100
|
+
dir: ComputedRef<ToggleGroupDirection>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 判断某项是否选中。
|
|
104
|
+
*
|
|
105
|
+
* @param itemValue 项的值。
|
|
106
|
+
* @returns 是否按下。
|
|
107
|
+
*/
|
|
108
|
+
isItemPressed(itemValue: string): boolean;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 写入某项的按下状态。
|
|
112
|
+
*
|
|
113
|
+
* @param itemValue 项的值。
|
|
114
|
+
* @param pressed 该项点击后的状态。
|
|
115
|
+
*/
|
|
116
|
+
setItemPressed(itemValue: string, pressed: boolean): void;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 登记一个参与罗盘焦点的项。
|
|
120
|
+
*
|
|
121
|
+
* @param entry 项条目。
|
|
122
|
+
* @returns 注销函数。
|
|
123
|
+
*/
|
|
124
|
+
registerItem(entry: ToggleGroupItemEntry): () => void;
|
|
125
|
+
|
|
126
|
+
/** 获取组根元素属性:role、dir,罗盘模式下的 tabIndex 与 outline */
|
|
127
|
+
getGroupProps(): Record<string, unknown>;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 获取项在罗盘模式下的属性。
|
|
131
|
+
*
|
|
132
|
+
* @param id 项的罗盘焦点 id。
|
|
133
|
+
* @returns 含 tabIndex 的属性集合;未启用罗盘焦点时返回空对象。
|
|
134
|
+
*/
|
|
135
|
+
getItemProps(id: string): Record<string, unknown>;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 组获得焦点时把焦点交给选中的项;绑定到 @focus。
|
|
139
|
+
*
|
|
140
|
+
* @param event 原生焦点事件。
|
|
141
|
+
*/
|
|
142
|
+
onGroupFocus(event: FocusEvent): void;
|
|
143
|
+
|
|
144
|
+
/** 组内指针按下,标记本次焦点来自点击而不是键盘;绑定到 @mousedown */
|
|
145
|
+
onGroupMousedown(): void;
|
|
146
|
+
|
|
147
|
+
/** 焦点离开组,重置 shift+Tab 退让状态;绑定到 @blur */
|
|
148
|
+
onGroupBlur(): void;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 项获得焦点,记录当前 Tab 停靠点;绑定到项的 @focus。
|
|
152
|
+
*
|
|
153
|
+
* @param id 项的罗盘焦点 id。
|
|
154
|
+
*/
|
|
155
|
+
onItemFocus(id: string): void;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 项按下时记录停靠点,并在项不可聚焦时阻止获得焦点;绑定到项的 @mousedown。
|
|
159
|
+
*
|
|
160
|
+
* @param event 原生鼠标事件。
|
|
161
|
+
* @param id 项的罗盘焦点 id。
|
|
162
|
+
*/
|
|
163
|
+
onItemMousedown(event: MouseEvent, id: string): void;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 项的键盘导航;绑定到项的 @keydown。
|
|
167
|
+
*
|
|
168
|
+
* @param event 原生键盘事件。
|
|
169
|
+
*/
|
|
170
|
+
onItemKeydown(event: KeyboardEvent): void;
|
|
171
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { computed, ref, toValue } from 'vue';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
isToggleGroupItemPressed,
|
|
5
|
+
normalizeToggleGroupValue,
|
|
6
|
+
resolveGroupValueAfterToggle,
|
|
7
|
+
resolveRovingFocusIntent,
|
|
8
|
+
resolveRovingFocusTargetIndex,
|
|
9
|
+
serializeToggleGroupValue,
|
|
10
|
+
} from '@agile-plus/primitives/toggle';
|
|
11
|
+
|
|
12
|
+
import { focusFirst } from '@agile-plus/primitives/dom';
|
|
13
|
+
|
|
14
|
+
import { useCollection } from '../utils/useCollection';
|
|
15
|
+
import { useControllable } from '../utils/useControllable';
|
|
16
|
+
|
|
17
|
+
import type { ToggleGroupType } from '@agile-plus/primitives/toggle';
|
|
18
|
+
|
|
19
|
+
import type { ToggleGroupItemEntry, UseToggleGroupOptions, UseToggleGroupReturn } from './types';
|
|
20
|
+
|
|
21
|
+
const VALID_TYPES: ToggleGroupType[] = ['single', 'multiple'];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* ToggleGroup 的 Vue Composable 适配。
|
|
25
|
+
*
|
|
26
|
+
* 除了组值本身,这里还就地实现了 Radix 交给 RovingFocusGroup 的那套焦点规则:
|
|
27
|
+
*
|
|
28
|
+
* 1. **只有一个 Tab 停靠点**。整组在 Tab 序列里只占一格:焦点第一次进组时落在
|
|
29
|
+
* 选中项(没有选中项则落在当前停靠点或第一项),之后方向键在项之间移动焦点。
|
|
30
|
+
* 2. **按键语义按方向与文本方向过滤**。纵向组不接管左右、横向组不接管上下,RTL 下
|
|
31
|
+
* 左右互换,带修饰键时交还浏览器。
|
|
32
|
+
* 3. **入口焦点由组元素上的 focus 事件触发**。Radix 之所以要区分点击与键盘,是因为
|
|
33
|
+
* Safari 点击按钮时焦点落在外层容器上,若把它当键盘进入处理会抢走用户刚点的焦点。
|
|
34
|
+
*
|
|
35
|
+
* 没有把它抽成共享 composable:Tabs 与 RadioGroup 已有各自的键盘实现,抽象要等真的
|
|
36
|
+
* 出现迁移需求,否则只是给一次性代码换个名字。
|
|
37
|
+
*
|
|
38
|
+
* @param options 组配置项。
|
|
39
|
+
* @returns 组值读写与罗盘焦点所需的状态、属性与处理器。
|
|
40
|
+
* @throws 当 `type` 缺失或不是 `single` / `multiple` 时抛出。
|
|
41
|
+
*/
|
|
42
|
+
export function useToggleGroup(options: UseToggleGroupOptions): UseToggleGroupReturn {
|
|
43
|
+
const type = computed(() => {
|
|
44
|
+
const value = toValue(options.type);
|
|
45
|
+
|
|
46
|
+
if (!VALID_TYPES.includes(value)) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
'Invalid prop `type` expected on `ToggleGroup`. Expected `single` or `multiple`.'
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return value;
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 组的值统一按数组归一化:single 只是「长度不超过 1」的特例,序列化只发生在出口。
|
|
57
|
+
* 归一化必须在受控判定之后做——直接返回 `[]` 会让「未传 value」被误判成受控空值。
|
|
58
|
+
*/
|
|
59
|
+
const state = useControllable<string[]>({
|
|
60
|
+
value: () => {
|
|
61
|
+
const raw = toValue(options.value);
|
|
62
|
+
|
|
63
|
+
return raw === undefined ? undefined : normalizeToggleGroupValue(type.value, raw);
|
|
64
|
+
},
|
|
65
|
+
defaultValue: normalizeToggleGroupValue(type.value, toValue(options.defaultValue)),
|
|
66
|
+
onChange: (next) => options.onValueChange?.(serializeToggleGroupValue(type.value, next)),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const value = state.value;
|
|
70
|
+
|
|
71
|
+
const disabled = computed(() => toValue(options.disabled) ?? false);
|
|
72
|
+
const rovingFocus = computed(() => toValue(options.rovingFocus) ?? true);
|
|
73
|
+
const loop = computed(() => toValue(options.loop) ?? true);
|
|
74
|
+
const orientation = computed(() => toValue(options.orientation));
|
|
75
|
+
const dir = computed(() => toValue(options.dir) ?? 'ltr');
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 条目表交给 useCollection:条目里带 Ref 与 ComputedRef,深响应会把它们拆包成值,
|
|
79
|
+
* 键盘导航读到的就是过期快照而不是当下的状态。
|
|
80
|
+
*/
|
|
81
|
+
const collection = useCollection<ToggleGroupItemEntry>({ key: (entry) => entry.id });
|
|
82
|
+
|
|
83
|
+
const currentTabStopId = ref<string | null>(null);
|
|
84
|
+
|
|
85
|
+
const isTabbingBackOut = ref(false);
|
|
86
|
+
|
|
87
|
+
let isClickFocus = false;
|
|
88
|
+
|
|
89
|
+
const focusableEntries = computed(() =>
|
|
90
|
+
collection.items.value.filter((entry) => entry.focusable.value)
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
function findEntry(id: string): ToggleGroupItemEntry | undefined {
|
|
94
|
+
return collection.items.value.find((entry) => entry.id === id);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function isItemPressed(itemValue: string): boolean {
|
|
98
|
+
return isToggleGroupItemPressed(value.value, itemValue);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function emitValue(next: string[]): void {
|
|
102
|
+
state.set(next);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function setItemPressed(itemValue: string, pressed: boolean): void {
|
|
106
|
+
emitValue(
|
|
107
|
+
resolveGroupValueAfterToggle({ type: type.value, value: value.value, itemValue, pressed })
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 焦点进入组时的落点选择。
|
|
113
|
+
*
|
|
114
|
+
* 顺序是「选中项 → 当前停靠点 → 第一个可聚焦项」,与 Radix 完全一致:读屏用户
|
|
115
|
+
* Tab 进一组格式按钮时,应该先听到当前生效的那一个,而不是从头报一遍。
|
|
116
|
+
*/
|
|
117
|
+
function handleEntryFocus(): void {
|
|
118
|
+
const candidates = focusableEntries.value;
|
|
119
|
+
|
|
120
|
+
if (candidates.length === 0) return;
|
|
121
|
+
|
|
122
|
+
const active = candidates.find((entry) => entry.active.value);
|
|
123
|
+
const current = candidates.find((entry) => entry.id === currentTabStopId.value);
|
|
124
|
+
|
|
125
|
+
const ordered = [active, current, ...candidates]
|
|
126
|
+
.filter((entry): entry is ToggleGroupItemEntry => Boolean(entry))
|
|
127
|
+
.map((entry) => entry.element.value)
|
|
128
|
+
.filter((element): element is HTMLElement => Boolean(element));
|
|
129
|
+
|
|
130
|
+
focusFirst(ordered);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function getGroupProps(): Record<string, unknown> {
|
|
134
|
+
return {
|
|
135
|
+
role: type.value === 'single' ? 'radiogroup' : 'toolbar',
|
|
136
|
+
dir: dir.value,
|
|
137
|
+
...(rovingFocus.value
|
|
138
|
+
? {
|
|
139
|
+
// 整组只占一个 Tab 停靠点;没有可聚焦项时连这一格也不留。
|
|
140
|
+
tabindex: isTabbingBackOut.value || focusableEntries.value.length === 0 ? -1 : 0,
|
|
141
|
+
'data-orientation': orientation.value,
|
|
142
|
+
style: { outline: 'none' },
|
|
143
|
+
}
|
|
144
|
+
: null),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function getItemProps(id: string): Record<string, unknown> {
|
|
149
|
+
if (!rovingFocus.value) return {};
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
tabindex: currentTabStopId.value === id ? 0 : -1,
|
|
153
|
+
'data-orientation': orientation.value,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function onGroupFocus(event: FocusEvent): void {
|
|
158
|
+
if (!rovingFocus.value) return;
|
|
159
|
+
|
|
160
|
+
// 只处理真正落到组上的焦点:事件冒泡自子元素时 target 不是组本身。
|
|
161
|
+
const isSelfFocus = event.target === event.currentTarget;
|
|
162
|
+
|
|
163
|
+
const isKeyboardFocus = !isClickFocus;
|
|
164
|
+
|
|
165
|
+
if (isSelfFocus && isKeyboardFocus && !isTabbingBackOut.value) handleEntryFocus();
|
|
166
|
+
|
|
167
|
+
isClickFocus = false;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function onGroupMousedown(): void {
|
|
171
|
+
isClickFocus = true;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function onGroupBlur(): void {
|
|
175
|
+
isTabbingBackOut.value = false;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function onItemFocus(id: string): void {
|
|
179
|
+
if (!rovingFocus.value) return;
|
|
180
|
+
|
|
181
|
+
currentTabStopId.value = id;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function onItemMousedown(event: MouseEvent, id: string): void {
|
|
185
|
+
if (!rovingFocus.value) return;
|
|
186
|
+
|
|
187
|
+
const entry = findEntry(id);
|
|
188
|
+
|
|
189
|
+
if (!entry) return;
|
|
190
|
+
|
|
191
|
+
// tabIndex=-1 只是移出 Tab 序列,元素依然能被程序聚焦;不可聚焦的项要在按下时
|
|
192
|
+
// 就阻止默认行为,否则 Safari 等浏览器仍会把焦点交给它。
|
|
193
|
+
if (!entry.focusable.value) {
|
|
194
|
+
event.preventDefault();
|
|
195
|
+
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
currentTabStopId.value = id;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function onItemKeydown(event: KeyboardEvent): void {
|
|
203
|
+
if (!rovingFocus.value) return;
|
|
204
|
+
|
|
205
|
+
if (event.key === 'Tab' && event.shiftKey) {
|
|
206
|
+
// 用户要往回退出整组:把组的停靠点让出来,并把按键交还浏览器。
|
|
207
|
+
isTabbingBackOut.value = true;
|
|
208
|
+
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (event.target !== event.currentTarget) return;
|
|
213
|
+
|
|
214
|
+
const intent = resolveRovingFocusIntent({
|
|
215
|
+
key: event.key,
|
|
216
|
+
orientation: orientation.value,
|
|
217
|
+
dir: dir.value,
|
|
218
|
+
hasModifierKey: event.shiftKey || event.ctrlKey || event.altKey || event.metaKey,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
if (!intent) return;
|
|
222
|
+
|
|
223
|
+
event.preventDefault();
|
|
224
|
+
|
|
225
|
+
const candidates = focusableEntries.value;
|
|
226
|
+
const target = resolveRovingFocusTargetIndex({
|
|
227
|
+
intent,
|
|
228
|
+
currentIndex: candidates.findIndex((entry) => entry.element.value === event.currentTarget),
|
|
229
|
+
count: candidates.length,
|
|
230
|
+
loop: loop.value,
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
if (target === null) return;
|
|
234
|
+
|
|
235
|
+
// Radix 用 setTimeout 推迟焦点,是为了绕开 React 在 keydown 里批处理更新的已知问题
|
|
236
|
+
// (facebook/react#20332);Vue 的响应式不依赖那层批处理,同步聚焦即可,
|
|
237
|
+
// 推迟一帧反而会让连按方向键时焦点跟不上按键。
|
|
238
|
+
candidates[target]?.element.value?.focus();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return {
|
|
242
|
+
type,
|
|
243
|
+
value,
|
|
244
|
+
disabled,
|
|
245
|
+
rovingFocus,
|
|
246
|
+
orientation,
|
|
247
|
+
dir,
|
|
248
|
+
isItemPressed,
|
|
249
|
+
setItemPressed,
|
|
250
|
+
registerItem: collection.add,
|
|
251
|
+
getGroupProps,
|
|
252
|
+
getItemProps,
|
|
253
|
+
onGroupFocus,
|
|
254
|
+
onGroupMousedown,
|
|
255
|
+
onGroupBlur,
|
|
256
|
+
onItemFocus,
|
|
257
|
+
onItemMousedown,
|
|
258
|
+
onItemKeydown,
|
|
259
|
+
};
|
|
260
|
+
}
|
package/src/utils/env.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 运行环境判定。
|
|
3
|
+
*
|
|
4
|
+
* 本库大量 composable 会在 watch 的 immediate 回调里碰 DOM,而 SSR 与非浏览器环境
|
|
5
|
+
* (单元测试的 node 环境)里没有这些全局对象。判据统一放这里,避免每个文件写一份
|
|
6
|
+
* 略有出入的变体(曾经出现过 `typeof window`、`window + document`、
|
|
7
|
+
* `window + HTMLElement` 三种写法),也避免有人用 `typeof document !== 'undefined'`
|
|
8
|
+
* 却去访问 `window` 这类不一致。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** 是否在浏览器环境中运行。 */
|
|
12
|
+
export const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 判断当前环境是否支持某个观察器构造器。
|
|
16
|
+
*
|
|
17
|
+
* jsdom 不实现 ResizeObserver / MutationObserver,直接 new 会抛错而不是静默降级;
|
|
18
|
+
* 各组件的降级路径本来就不同(有的不测量、有的只在事件里测),因此这里只回答
|
|
19
|
+
* 「能不能用」,不替调用方决定怎么办。
|
|
20
|
+
*
|
|
21
|
+
* @param constructor 全局构造器,例如 `globalThis.ResizeObserver`。
|
|
22
|
+
* @returns true 表示可以安全构造。
|
|
23
|
+
*/
|
|
24
|
+
export function isObserverAvailable(constructor: unknown): boolean {
|
|
25
|
+
return typeof constructor === 'function';
|
|
26
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { computed, ref, shallowRef, toValue, watch } from 'vue';
|
|
2
|
+
|
|
3
|
+
import { getHiddenInputStyle, writeNativeChecked } from '@agile-plus/primitives/form';
|
|
4
|
+
|
|
5
|
+
import type { HiddenInputSize } from '@agile-plus/primitives/form';
|
|
6
|
+
|
|
7
|
+
import type { ComputedRef, MaybeRefOrGetter, Ref } from 'vue';
|
|
8
|
+
|
|
9
|
+
import { isBrowser } from './env';
|
|
10
|
+
import { useResizeObserver } from './useResizeObserver';
|
|
11
|
+
import { useFormResetListener } from './useFormResetListener';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* useBubbleInput 的配置项。
|
|
15
|
+
*/
|
|
16
|
+
export interface UseBubbleInputOptions {
|
|
17
|
+
/** 被覆盖的控件元素(按钮本体) */
|
|
18
|
+
control: Ref<HTMLElement | null>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 显式归属的表单 id。
|
|
22
|
+
*
|
|
23
|
+
* 提供时即使控件在 `<form>` 外面也算表单控件;这与原生 input 的 form 属性同义。
|
|
24
|
+
*/
|
|
25
|
+
form?: MaybeRefOrGetter<string | undefined>;
|
|
26
|
+
|
|
27
|
+
/** 当下应该写进原生 input 的选中态 */
|
|
28
|
+
checked: () => boolean;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 表单 reset 时的恢复动作。
|
|
32
|
+
*
|
|
33
|
+
* 由调用方决定恢复到什么值:Checkbox 恢复 defaultChecked,Switch 恢复初次出现的值,
|
|
34
|
+
* 受控模式下它们各自选择「通知外部」还是「不动内部状态」。
|
|
35
|
+
*/
|
|
36
|
+
onReset: () => void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* useBubbleInput 的返回值。
|
|
41
|
+
*/
|
|
42
|
+
export interface UseBubbleInputReturn {
|
|
43
|
+
/** 是否应该渲染隐藏 input(控件在表单内或显式给了 form id) */
|
|
44
|
+
isFormControl: ComputedRef<boolean>;
|
|
45
|
+
|
|
46
|
+
/** 隐藏 input 的内联样式,尺寸跟随控件 */
|
|
47
|
+
style: ComputedRef<Record<string, string>>;
|
|
48
|
+
|
|
49
|
+
/** 隐藏 input 的元素引用,供调用方读写 property(如 Checkbox 的 indeterminate) */
|
|
50
|
+
inputElement: Ref<HTMLInputElement | null>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 绑定隐藏 input 元素,传给模板的 `:ref`。
|
|
54
|
+
*
|
|
55
|
+
* @param node 元素实例,卸载时为 null。
|
|
56
|
+
*/
|
|
57
|
+
setInputElement(node: unknown): void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 隐藏原生 input 的表单接入。
|
|
62
|
+
*
|
|
63
|
+
* 三件事必须一致,否则每个组件都会各自出偏差,所以合并成一份:
|
|
64
|
+
*
|
|
65
|
+
* 1. **尺寸跟随**:input 覆盖在控件上,需要跟着控件尺寸变(ResizeObserver + 首帧测量)。
|
|
66
|
+
* 2. **写值与通知**:用原型上的原始 setter 写 `checked`,再派发一次冒泡 `change`,
|
|
67
|
+
* 这样 FormData、`form.addEventListener('change')` 与依赖 change 的表单库都能看到;
|
|
68
|
+
* 初次挂载只写值不派发,避免页面刚出现就冒出一批事件。
|
|
69
|
+
* 3. **reset 复原**:浏览器 reset 会把 input 恢复到 `defaultChecked`,而组件状态是另一套
|
|
70
|
+
* 真源,因此必须同时监听 reset 并通知调用方(复用 `useFormResetListener`);
|
|
71
|
+
* `defaultChecked` 只在挂载时写一次,语义才是「初次出现的值」而不是「最近一次改动」。
|
|
72
|
+
*
|
|
73
|
+
* 未成为表单控件时(不在 form 内且没有 form id)什么都不做,调用方据此不渲染 input。
|
|
74
|
+
* 控件尚未挂载(含 SSR 首帧)时按表单控件处理,保证无 JS 时提交路径仍然存在。
|
|
75
|
+
*
|
|
76
|
+
* @param options 控件元素、选中态来源、归属 id 与 reset 动作。
|
|
77
|
+
* @returns 渲染判断、样式与元素入口。
|
|
78
|
+
*/
|
|
79
|
+
export function useBubbleInput(options: UseBubbleInputOptions): UseBubbleInputReturn {
|
|
80
|
+
const inputElement = ref<HTMLInputElement | null>(null);
|
|
81
|
+
|
|
82
|
+
const size = shallowRef<HiddenInputSize | null>(null);
|
|
83
|
+
|
|
84
|
+
const formId = computed(() => toValue(options.form));
|
|
85
|
+
|
|
86
|
+
const isFormControl = computed(() => {
|
|
87
|
+
if (formId.value) return true;
|
|
88
|
+
|
|
89
|
+
const element = options.control.value;
|
|
90
|
+
|
|
91
|
+
if (!element) return true;
|
|
92
|
+
|
|
93
|
+
return element.closest('form') !== null;
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const style = computed(() => getHiddenInputStyle(size.value));
|
|
97
|
+
|
|
98
|
+
const resize = useResizeObserver(measure);
|
|
99
|
+
|
|
100
|
+
function measure(): void {
|
|
101
|
+
const element = options.control.value;
|
|
102
|
+
|
|
103
|
+
if (!element) {
|
|
104
|
+
size.value = null;
|
|
105
|
+
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const rect = element.getBoundingClientRect();
|
|
110
|
+
|
|
111
|
+
size.value = { width: rect.width, height: rect.height };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 写值并通知表单。
|
|
116
|
+
*
|
|
117
|
+
* @param notify 是否派发 change;初次挂载传 false。
|
|
118
|
+
*/
|
|
119
|
+
function syncInput(notify: boolean): void {
|
|
120
|
+
const input = inputElement.value;
|
|
121
|
+
|
|
122
|
+
if (!input) return;
|
|
123
|
+
|
|
124
|
+
writeNativeChecked(input, options.checked());
|
|
125
|
+
|
|
126
|
+
if (notify) input.dispatchEvent(new Event('change', { bubbles: true }));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
useFormResetListener(
|
|
130
|
+
options.control,
|
|
131
|
+
() => options.onReset(),
|
|
132
|
+
() => formId.value
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
watch(
|
|
136
|
+
options.control,
|
|
137
|
+
(element) => {
|
|
138
|
+
resize.disconnect();
|
|
139
|
+
|
|
140
|
+
if (!isBrowser) return;
|
|
141
|
+
|
|
142
|
+
measure();
|
|
143
|
+
|
|
144
|
+
resize.observe(element);
|
|
145
|
+
},
|
|
146
|
+
{ flush: 'post' }
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
// input 是条件渲染的,可能出现得比状态变化晚:挂载时先同步一次且不派发事件。
|
|
150
|
+
watch(inputElement, (input) => {
|
|
151
|
+
if (!input) return;
|
|
152
|
+
|
|
153
|
+
input.defaultChecked = options.checked();
|
|
154
|
+
|
|
155
|
+
syncInput(false);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
watch(
|
|
159
|
+
() => options.checked(),
|
|
160
|
+
() => syncInput(true)
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
function setInputElement(node: unknown): void {
|
|
164
|
+
inputElement.value = node instanceof HTMLInputElement ? node : null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return { isFormControl, style, inputElement, setInputElement };
|
|
168
|
+
}
|