@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,204 @@
|
|
|
1
|
+
import { computed, toValue, useId } from 'vue';
|
|
2
|
+
|
|
3
|
+
import { handleRadioGroupKeydown } from '@agile-plus/primitives/radio-group';
|
|
4
|
+
|
|
5
|
+
import { useCollection } from '../utils/useCollection';
|
|
6
|
+
|
|
7
|
+
import { useControllable } from '../utils/useControllable';
|
|
8
|
+
|
|
9
|
+
import type { RadioGroupItemEntry, UseRadioGroupOptions, UseRadioGroupReturn } from './types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* RadioGroup 的 Vue Composable 适配。
|
|
13
|
+
*
|
|
14
|
+
* 管理选中值、item 注册表、禁用合并与键盘导航后的焦点移动。
|
|
15
|
+
* 支持受控与非受控两种模式。
|
|
16
|
+
*
|
|
17
|
+
* @param options Composable 配置选项。
|
|
18
|
+
* @returns 响应式状态、注册方法与属性绑定函数集合。
|
|
19
|
+
*/
|
|
20
|
+
export function useRadioGroup(options: UseRadioGroupOptions = {}): UseRadioGroupReturn {
|
|
21
|
+
const { defaultValue = '', loop = true } = options;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 实例级 id 前缀。
|
|
25
|
+
*
|
|
26
|
+
* item 的 id 由前缀加 value 组成,避免同一页面存在多个值相同的组时 id 冲突;
|
|
27
|
+
* useId 在 SSR 下与服务端渲染结果一致,不会触发注水不匹配。
|
|
28
|
+
*/
|
|
29
|
+
const uid = useId();
|
|
30
|
+
|
|
31
|
+
const state = useControllable<string>({
|
|
32
|
+
value: () => toValue(options.modelValue),
|
|
33
|
+
defaultValue: defaultValue,
|
|
34
|
+
onChange: (next) => options.onValueChange?.(next),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const value = state.value;
|
|
38
|
+
|
|
39
|
+
const isGroupDisabled = computed(() => toValue(options.disabled) === true);
|
|
40
|
+
|
|
41
|
+
const hasName = computed(() => !!toValue(options.name));
|
|
42
|
+
|
|
43
|
+
const collection = useCollection<RadioGroupItemEntry>({ key: (item) => item.value });
|
|
44
|
+
|
|
45
|
+
/** 已过滤禁用项的可用列表,顺序即键盘导航顺序。 */
|
|
46
|
+
const enabledValues = computed(() =>
|
|
47
|
+
isGroupDisabled.value
|
|
48
|
+
? []
|
|
49
|
+
: collection.items.value.filter((item) => !item.disabled).map((item) => item.value)
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 组内唯一的 tab stop。
|
|
54
|
+
*
|
|
55
|
+
* 有选中项时是选中项;未选中时是第一个可用项——这是 radio group 的标准语义:
|
|
56
|
+
* Tab 键一次进入整组并落在当前值上,组内移动只能使用方向键。
|
|
57
|
+
*/
|
|
58
|
+
const tabStopValue = computed(() =>
|
|
59
|
+
value.value === '' ? (enabledValues.value[0] ?? '') : value.value
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
function getItemId(itemValue: string): string {
|
|
63
|
+
return `${uid}-radio-${itemValue}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function setValue(next: string): void {
|
|
67
|
+
state.set(next);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 登记一个项。
|
|
72
|
+
*
|
|
73
|
+
* 已存在时**保留**原条目而不是覆盖:项的 disabled 状态由它自己的 watch 增量上报,
|
|
74
|
+
* 重新登记若重置成 false,禁用项会重新出现在 Tab 序列里。
|
|
75
|
+
*/
|
|
76
|
+
function registerItem(itemValue: string): void {
|
|
77
|
+
if (collection.items.value.some((item) => item.value === itemValue)) return;
|
|
78
|
+
|
|
79
|
+
collection.add({ value: itemValue, disabled: false });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function setItemDisabled(itemValue: string, disabled: boolean): void {
|
|
83
|
+
const current = collection.items.value.find((item) => item.value === itemValue);
|
|
84
|
+
|
|
85
|
+
if (!current || current.disabled === disabled) return;
|
|
86
|
+
|
|
87
|
+
collection.replace({ value: itemValue, disabled });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function unregisterItem(itemValue: string): void {
|
|
91
|
+
collection.remove(itemValue);
|
|
92
|
+
|
|
93
|
+
// 受控模式的选中值由外部持有,此处不越权清空,否则会静默改写调用方状态;
|
|
94
|
+
// replace 在受控下本身就是空操作。卸载子项也不是用户操作,不该冒出 change 事件。
|
|
95
|
+
if (value.value === itemValue) state.replace('');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function isItemDisabled(itemValue: string): boolean {
|
|
99
|
+
if (isGroupDisabled.value) return true;
|
|
100
|
+
|
|
101
|
+
return collection.items.value.find((item) => item.value === itemValue)?.disabled === true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 处理组内方向键导航。
|
|
106
|
+
*
|
|
107
|
+
* 选中与移动焦点必须同时发生:radio 语义下方向键既换焦点也换选中值,
|
|
108
|
+
* 只移动焦点会让 aria-checked 与视觉状态分离。
|
|
109
|
+
*/
|
|
110
|
+
function onItemKeydown(event: KeyboardEvent): void {
|
|
111
|
+
const current = event.currentTarget as HTMLElement | null;
|
|
112
|
+
const focusedValue = current?.dataset.value ?? null;
|
|
113
|
+
|
|
114
|
+
const result = handleRadioGroupKeydown(event.key, {
|
|
115
|
+
enabledValues: enabledValues.value,
|
|
116
|
+
focusedValue,
|
|
117
|
+
loop,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
if (!result.handled) return;
|
|
121
|
+
|
|
122
|
+
event.preventDefault();
|
|
123
|
+
|
|
124
|
+
if (result.nextValue === null) return;
|
|
125
|
+
|
|
126
|
+
setValue(result.nextValue);
|
|
127
|
+
|
|
128
|
+
document.getElementById(getItemId(result.nextValue))?.focus();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function getRootProps(): Record<string, unknown> {
|
|
132
|
+
const required = toValue(options.required) === true;
|
|
133
|
+
const labelledby = toValue(options.ariaLabelledby);
|
|
134
|
+
const describedby = toValue(options.ariaDescribedby);
|
|
135
|
+
const orientation = toValue(options.orientation) ?? 'horizontal';
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
role: 'radiogroup' as const,
|
|
139
|
+
'aria-orientation': orientation,
|
|
140
|
+
// 样式层依赖 data-* 而非 aria-*:ARIA 属性面向辅助技术,不应成为视觉契约。
|
|
141
|
+
'data-orientation': orientation,
|
|
142
|
+
'aria-required': required || undefined,
|
|
143
|
+
'aria-labelledby': labelledby || undefined,
|
|
144
|
+
'aria-describedby': describedby || undefined,
|
|
145
|
+
'aria-disabled': isGroupDisabled.value || undefined,
|
|
146
|
+
'data-disabled': isGroupDisabled.value || undefined,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function getItemProps(itemValue: string): Record<string, unknown> {
|
|
151
|
+
const checked = value.value === itemValue;
|
|
152
|
+
const disabled = isItemDisabled(itemValue);
|
|
153
|
+
const focusable = !disabled && (checked || tabStopValue.value === itemValue);
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
id: getItemId(itemValue),
|
|
157
|
+
role: 'radio' as const,
|
|
158
|
+
'aria-checked': checked,
|
|
159
|
+
'aria-disabled': disabled || undefined,
|
|
160
|
+
tabindex: focusable ? 0 : -1,
|
|
161
|
+
'data-state': checked ? 'checked' : 'unchecked',
|
|
162
|
+
'data-disabled': disabled || undefined,
|
|
163
|
+
'data-value': itemValue,
|
|
164
|
+
'data-orientation': toValue(options.orientation) ?? 'horizontal',
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 隐藏原生 input 的属性。
|
|
170
|
+
*
|
|
171
|
+
* checked 直接由选中值决定,浏览器在同一 name 分组内会自然维持互斥;
|
|
172
|
+
* 禁用项输出 disabled 而不是省略字段,提交时由浏览器负责忽略它。
|
|
173
|
+
*/
|
|
174
|
+
function getInputProps(itemValue: string): Record<string, unknown> | null {
|
|
175
|
+
const name = toValue(options.name);
|
|
176
|
+
|
|
177
|
+
if (!name) return null;
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
type: 'radio' as const,
|
|
181
|
+
name,
|
|
182
|
+
value: itemValue,
|
|
183
|
+
checked: value.value === itemValue,
|
|
184
|
+
disabled: isItemDisabled(itemValue),
|
|
185
|
+
tabindex: -1,
|
|
186
|
+
'aria-hidden': true,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
value,
|
|
192
|
+
isGroupDisabled,
|
|
193
|
+
hasName,
|
|
194
|
+
setValue,
|
|
195
|
+
registerItem,
|
|
196
|
+
setItemDisabled,
|
|
197
|
+
unregisterItem,
|
|
198
|
+
isItemDisabled,
|
|
199
|
+
getRootProps,
|
|
200
|
+
getItemProps,
|
|
201
|
+
getInputProps,
|
|
202
|
+
onItemKeydown,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { ComputedRef, CSSProperties, MaybeRefOrGetter, Ref } from 'vue';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
ScrollAreaOrientation,
|
|
5
|
+
ScrollAreaType,
|
|
6
|
+
ScrollbarVisibility,
|
|
7
|
+
} from '@agile-plus/primitives/scroll-area';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* useScrollArea Composable 的配置选项。
|
|
11
|
+
*
|
|
12
|
+
* 属性名与默认值对齐 Radix 的 ScrollArea.Root。
|
|
13
|
+
*/
|
|
14
|
+
export interface UseScrollAreaOptions {
|
|
15
|
+
/**
|
|
16
|
+
* 滚动条出现时机。
|
|
17
|
+
*
|
|
18
|
+
* @default 'hover'
|
|
19
|
+
*/
|
|
20
|
+
type?: MaybeRefOrGetter<ScrollAreaType | undefined>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* `scroll` 与 `hover` 类型下,停止交互到隐藏之间的延迟(毫秒)。
|
|
24
|
+
*
|
|
25
|
+
* @default 600
|
|
26
|
+
*/
|
|
27
|
+
scrollHideDelay?: number;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 文本方向,输出到根元素的 `dir`。
|
|
31
|
+
*
|
|
32
|
+
* 横向滚动偏移在 RTL 下的映射未做处理,详见组件文档的已知限制。
|
|
33
|
+
*
|
|
34
|
+
* @default undefined
|
|
35
|
+
*/
|
|
36
|
+
dir?: MaybeRefOrGetter<'ltr' | 'rtl' | undefined>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* thumb 的最小可点中长度(px),透传给几何计算。
|
|
40
|
+
*
|
|
41
|
+
* @default 20
|
|
42
|
+
*/
|
|
43
|
+
minThumbSize?: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* useScrollArea Composable 的返回值。
|
|
48
|
+
*
|
|
49
|
+
* 五个部件共用同一个上下文:Viewport 负责提供可测量的元素与滚动事件,
|
|
50
|
+
* Scrollbar / Thumb 消费由几何计算得到的样式,Corner 只看两方向的可见性。
|
|
51
|
+
*
|
|
52
|
+
* 契约与其余组件一致:get*Props 只返回可 v-bind 的属性,事件通过 v-on 绑定;
|
|
53
|
+
* 几何结果单独由 get*Style 返回,避免把 style 混进属性对象后被消费者的静态样式覆盖。
|
|
54
|
+
*/
|
|
55
|
+
export interface UseScrollAreaReturn {
|
|
56
|
+
/** Viewport 元素引用,由 ScrollAreaViewport 在挂载时写入 */
|
|
57
|
+
viewportRef: Ref<HTMLElement | null>;
|
|
58
|
+
|
|
59
|
+
/** 当前生效的出现时机,供样式或文档调试读取 */
|
|
60
|
+
type: ComputedRef<ScrollAreaType>;
|
|
61
|
+
|
|
62
|
+
/** 请求一次测量更新,同一帧内的重复请求会被合并 */
|
|
63
|
+
requestMeasure(): void;
|
|
64
|
+
|
|
65
|
+
/** 获取根容器属性 */
|
|
66
|
+
getRootProps(): Record<string, unknown>;
|
|
67
|
+
|
|
68
|
+
/** 获取视口容器属性 */
|
|
69
|
+
getViewportProps(): Record<string, unknown>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 获取滚动条容器属性。
|
|
73
|
+
*
|
|
74
|
+
* @param orientation 滚动条朝向。
|
|
75
|
+
*/
|
|
76
|
+
getScrollbarProps(orientation: ScrollAreaOrientation): Record<string, unknown>;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 获取 thumb 属性。
|
|
80
|
+
*
|
|
81
|
+
* @param orientation 滚动条朝向。
|
|
82
|
+
*/
|
|
83
|
+
getThumbProps(orientation: ScrollAreaOrientation): Record<string, unknown>;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 获取 thumb 的几何样式(长度与位移)。
|
|
87
|
+
*
|
|
88
|
+
* @param orientation 滚动条朝向。
|
|
89
|
+
*/
|
|
90
|
+
getThumbStyle(orientation: ScrollAreaOrientation): CSSProperties;
|
|
91
|
+
|
|
92
|
+
/** 获取角部元素属性 */
|
|
93
|
+
getCornerProps(): Record<string, unknown>;
|
|
94
|
+
|
|
95
|
+
/** 角部是否应存在:仅当两个方向的滚动条同时可见 */
|
|
96
|
+
isCornerVisible: ComputedRef<boolean>;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 查询某朝向的可见性,供部件决定自身是否渲染。
|
|
100
|
+
*
|
|
101
|
+
* @param orientation 滚动条朝向。
|
|
102
|
+
*/
|
|
103
|
+
getVisibility(orientation: ScrollAreaOrientation): ScrollbarVisibility;
|
|
104
|
+
|
|
105
|
+
/** 视口滚动事件处理器,绑定到 `@scroll` */
|
|
106
|
+
onViewportScroll(): void;
|
|
107
|
+
|
|
108
|
+
/** 视口指针进入处理器,绑定到 `@pointerenter` */
|
|
109
|
+
onViewportPointerEnter(): void;
|
|
110
|
+
|
|
111
|
+
/** 视口指针离开处理器,绑定到 `@pointerleave` */
|
|
112
|
+
onViewportPointerLeave(): void;
|
|
113
|
+
|
|
114
|
+
/** 视口内聚焦事件处理器,绑定到 `@focusin`:键盘滚动也需要看到滚动条 */
|
|
115
|
+
onViewportFocusin(): void;
|
|
116
|
+
|
|
117
|
+
/** 视口内失焦处理器,绑定到 `@focusout` */
|
|
118
|
+
onViewportFocusout(): void;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 滚动条指针进入处理器,绑定到 `@pointerenter`。
|
|
122
|
+
*
|
|
123
|
+
* @param orientation 该滚动条朝向。
|
|
124
|
+
*/
|
|
125
|
+
onScrollbarPointerEnter(orientation: ScrollAreaOrientation): void;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 滚动条指针离开处理器,绑定到 `@pointerleave`。
|
|
129
|
+
*
|
|
130
|
+
* @param orientation 该滚动条朝向。
|
|
131
|
+
*/
|
|
132
|
+
onScrollbarPointerLeave(orientation: ScrollAreaOrientation): void;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* thumb 按下处理器,绑定到 `@pointerdown`。
|
|
136
|
+
*
|
|
137
|
+
* @param event 原生指针事件。
|
|
138
|
+
* @param orientation 该 thumb 朝向。
|
|
139
|
+
*/
|
|
140
|
+
onThumbPointerDown(event: PointerEvent, orientation: ScrollAreaOrientation): void;
|
|
141
|
+
}
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { computed, onScopeDispose, ref, toValue, watch } from 'vue';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
clampThumbOffset,
|
|
5
|
+
getScrollOffsetFromThumb,
|
|
6
|
+
getThumbMetrics,
|
|
7
|
+
resolveScrollbarVisibility,
|
|
8
|
+
} from '@agile-plus/primitives/scroll-area';
|
|
9
|
+
|
|
10
|
+
import { isBrowser, isObserverAvailable } from '../utils/env';
|
|
11
|
+
import { usePointerDrag } from '../utils/usePointerDrag';
|
|
12
|
+
import { useResizeObserver } from '../utils/useResizeObserver';
|
|
13
|
+
|
|
14
|
+
import type { CSSProperties } from 'vue';
|
|
15
|
+
|
|
16
|
+
import type {
|
|
17
|
+
ScrollAreaOrientation,
|
|
18
|
+
ScrollAreaType,
|
|
19
|
+
ScrollbarVisibility,
|
|
20
|
+
ThumbMetrics,
|
|
21
|
+
} from '@agile-plus/primitives/scroll-area';
|
|
22
|
+
|
|
23
|
+
import type { UseScrollAreaOptions, UseScrollAreaReturn } from './types';
|
|
24
|
+
|
|
25
|
+
/** Radix 的默认出现时机。 */
|
|
26
|
+
const DEFAULT_TYPE: ScrollAreaType = 'hover';
|
|
27
|
+
|
|
28
|
+
/** Radix 的默认隐藏延迟(毫秒)。 */
|
|
29
|
+
const DEFAULT_SCROLL_HIDE_DELAY = 600;
|
|
30
|
+
|
|
31
|
+
/** 尚未测量时的占位结果,保证首帧不会渲染出 NaN 尺寸。 */
|
|
32
|
+
const EMPTY_METRICS: ThumbMetrics = { size: 0, offset: 0, hasOverflow: false };
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* ScrollArea 的 Vue Composable 适配。
|
|
36
|
+
*
|
|
37
|
+
* 职责边界:几何与可见性判断全部委托给 primitives,本层只做三件事——把 DOM 读成
|
|
38
|
+
* 像素快照、把高频事件合并到一帧、把指针与焦点事件归一化成布尔输入。
|
|
39
|
+
*
|
|
40
|
+
* @param options Composable 配置选项。
|
|
41
|
+
* @returns 各部件需要的属性、样式与事件处理器。
|
|
42
|
+
*/
|
|
43
|
+
export function useScrollArea(options: UseScrollAreaOptions = {}): UseScrollAreaReturn {
|
|
44
|
+
const { scrollHideDelay = DEFAULT_SCROLL_HIDE_DELAY, minThumbSize } = options;
|
|
45
|
+
|
|
46
|
+
const viewportRef = ref<HTMLElement | null>(null);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 测量代号:几何结果由它驱动重算。
|
|
50
|
+
*
|
|
51
|
+
* computed 读取 DOM 不会自动追踪布局变化,没有这个显式版本就会一直拿缓存值,
|
|
52
|
+
* 表现为滚动后 thumb 不动。
|
|
53
|
+
*/
|
|
54
|
+
const measureVersion = ref(0);
|
|
55
|
+
|
|
56
|
+
const hoveringViewport = ref(false);
|
|
57
|
+
const focusingViewport = ref(false);
|
|
58
|
+
const hoveringScrollbar = ref<Record<ScrollAreaOrientation, boolean>>({
|
|
59
|
+
vertical: false,
|
|
60
|
+
horizontal: false,
|
|
61
|
+
});
|
|
62
|
+
const draggingOrientation = ref<ScrollAreaOrientation | null>(null);
|
|
63
|
+
const recentlyScrolled = ref(false);
|
|
64
|
+
|
|
65
|
+
const type = computed(() => toValue(options.type) ?? DEFAULT_TYPE);
|
|
66
|
+
|
|
67
|
+
let measureFrame: number | null = null;
|
|
68
|
+
let hideTimer: ReturnType<typeof setTimeout> | null = null;
|
|
69
|
+
let mutationObserver: MutationObserver | null = null;
|
|
70
|
+
|
|
71
|
+
// 溢出与 thumb 尺寸都由元素尺寸变化推导,观察回调只需要一次合并测量。
|
|
72
|
+
const resize = useResizeObserver(requestMeasure);
|
|
73
|
+
|
|
74
|
+
/** 拖拽监听的生命周期由它统一登记与释放;同一时刻最多只有一个方向在被拖拽。 */
|
|
75
|
+
const drag = usePointerDrag();
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 合并同一帧内的多次测量请求。
|
|
79
|
+
*
|
|
80
|
+
* scroll 与 resize 都是高频事件,逐次读布局会造成强制同步布局;这里保证一帧
|
|
81
|
+
* 一次,并把「推进版本」与「读取 DOM」分开,避免读写交错。
|
|
82
|
+
*/
|
|
83
|
+
function requestMeasure(): void {
|
|
84
|
+
if (typeof window === 'undefined' || typeof window.requestAnimationFrame !== 'function') {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (measureFrame !== null) return;
|
|
89
|
+
|
|
90
|
+
measureFrame = window.requestAnimationFrame(() => {
|
|
91
|
+
measureFrame = null;
|
|
92
|
+
measureVersion.value += 1;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function readTrack(orientation: ScrollAreaOrientation) {
|
|
97
|
+
const el = viewportRef.value;
|
|
98
|
+
|
|
99
|
+
if (!el) return { viewportSize: 0, scrollSize: 0, scrollOffset: 0 };
|
|
100
|
+
|
|
101
|
+
return orientation === 'vertical'
|
|
102
|
+
? {
|
|
103
|
+
viewportSize: el.clientHeight,
|
|
104
|
+
scrollSize: el.scrollHeight,
|
|
105
|
+
scrollOffset: el.scrollTop,
|
|
106
|
+
}
|
|
107
|
+
: {
|
|
108
|
+
viewportSize: el.clientWidth,
|
|
109
|
+
scrollSize: el.scrollWidth,
|
|
110
|
+
scrollOffset: el.scrollLeft,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function measureMetrics(orientation: ScrollAreaOrientation): ThumbMetrics {
|
|
115
|
+
const { viewportSize, scrollSize, scrollOffset } = readTrack(orientation);
|
|
116
|
+
|
|
117
|
+
return getThumbMetrics({ viewportSize, scrollSize, scrollOffset, minThumbSize });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** 依赖 measureVersion 触发重算,否则 Vue 会把它当成纯派生值永久缓存。 */
|
|
121
|
+
const verticalMetrics = computed(() => {
|
|
122
|
+
void measureVersion.value;
|
|
123
|
+
|
|
124
|
+
return typeof window === 'undefined' ? EMPTY_METRICS : measureMetrics('vertical');
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const horizontalMetrics = computed(() => {
|
|
128
|
+
void measureVersion.value;
|
|
129
|
+
|
|
130
|
+
return typeof window === 'undefined' ? EMPTY_METRICS : measureMetrics('horizontal');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
function getMetrics(orientation: ScrollAreaOrientation): ThumbMetrics {
|
|
134
|
+
return orientation === 'vertical' ? verticalMetrics.value : horizontalMetrics.value;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function markRecentlyScrolled(): void {
|
|
138
|
+
recentlyScrolled.value = true;
|
|
139
|
+
|
|
140
|
+
if (hideTimer !== null) clearTimeout(hideTimer);
|
|
141
|
+
|
|
142
|
+
hideTimer = setTimeout(() => {
|
|
143
|
+
hideTimer = null;
|
|
144
|
+
recentlyScrolled.value = false;
|
|
145
|
+
}, scrollHideDelay);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function getVisibility(orientation: ScrollAreaOrientation): ScrollbarVisibility {
|
|
149
|
+
const metrics = getMetrics(orientation);
|
|
150
|
+
|
|
151
|
+
return resolveScrollbarVisibility(
|
|
152
|
+
{
|
|
153
|
+
interacting:
|
|
154
|
+
hoveringViewport.value || focusingViewport.value || hoveringScrollbar.value[orientation],
|
|
155
|
+
dragging: draggingOrientation.value === orientation,
|
|
156
|
+
recentlyScrolled: recentlyScrolled.value,
|
|
157
|
+
hasOverflow: metrics.hasOverflow,
|
|
158
|
+
},
|
|
159
|
+
type.value
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function getRootProps(): Record<string, unknown> {
|
|
164
|
+
return { dir: toValue(options.dir) || undefined };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 视口带 tabindex。
|
|
169
|
+
*
|
|
170
|
+
* 自定义滚动条把原生滚动条隐藏后,键盘用户没有任何方式滚动内容;APG 对可滚动
|
|
171
|
+
* 容器的要求正是让它可聚焦。Radix 未做这件事,本库的这一点差异记录在文档里。
|
|
172
|
+
*/
|
|
173
|
+
function getViewportProps(): Record<string, unknown> {
|
|
174
|
+
return { tabindex: 0 };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function getScrollbarProps(orientation: ScrollAreaOrientation): Record<string, unknown> {
|
|
178
|
+
return {
|
|
179
|
+
'data-orientation': orientation,
|
|
180
|
+
'data-state': getVisibility(orientation),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function getThumbProps(orientation: ScrollAreaOrientation): Record<string, unknown> {
|
|
185
|
+
return {
|
|
186
|
+
'data-state': getVisibility(orientation),
|
|
187
|
+
'data-orientation': orientation,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function getThumbStyle(orientation: ScrollAreaOrientation): CSSProperties {
|
|
192
|
+
const metrics = getMetrics(orientation);
|
|
193
|
+
|
|
194
|
+
// 位移用 transform 而不是 top / left,避免每次滚动都触发布局。
|
|
195
|
+
return orientation === 'vertical'
|
|
196
|
+
? { height: `${metrics.size}px`, transform: `translateY(${metrics.offset}px)` }
|
|
197
|
+
: { width: `${metrics.size}px`, transform: `translateX(${metrics.offset}px)` };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const isCornerVisible = computed(
|
|
201
|
+
() => getVisibility('vertical') === 'visible' && getVisibility('horizontal') === 'visible'
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
function getCornerProps(): Record<string, unknown> {
|
|
205
|
+
return { 'data-state': isCornerVisible.value ? 'visible' : 'hidden' };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function onViewportScroll(): void {
|
|
209
|
+
markRecentlyScrolled();
|
|
210
|
+
|
|
211
|
+
requestMeasure();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function onViewportPointerEnter(): void {
|
|
215
|
+
hoveringViewport.value = true;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function onViewportPointerLeave(): void {
|
|
219
|
+
hoveringViewport.value = false;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function onViewportFocusin(): void {
|
|
223
|
+
focusingViewport.value = true;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function onViewportFocusout(): void {
|
|
227
|
+
focusingViewport.value = false;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function onScrollbarPointerEnter(orientation: ScrollAreaOrientation): void {
|
|
231
|
+
hoveringScrollbar.value = { ...hoveringScrollbar.value, [orientation]: true };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function onScrollbarPointerLeave(orientation: ScrollAreaOrientation): void {
|
|
235
|
+
hoveringScrollbar.value = { ...hoveringScrollbar.value, [orientation]: false };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 按下 thumb 后接管指针。
|
|
240
|
+
*
|
|
241
|
+
* 监听挂在 window 而不是 thumb:指针移出滚动条后仍能继续拖拽,也不需要
|
|
242
|
+
* setPointerCapture —— 它在合成指针事件下会抛 InvalidStateError。
|
|
243
|
+
* 清理集中在 disposeDrag:pointerup、pointercancel 与组件卸载走同一条路径,
|
|
244
|
+
* 否则异常中断会把 dragging 永远留在 true、滚动条再也不会隐藏。
|
|
245
|
+
*/
|
|
246
|
+
function onThumbPointerDown(event: PointerEvent, orientation: ScrollAreaOrientation): void {
|
|
247
|
+
const el = viewportRef.value;
|
|
248
|
+
|
|
249
|
+
if (!el || typeof window === 'undefined') return;
|
|
250
|
+
|
|
251
|
+
const metrics = getMetrics(orientation);
|
|
252
|
+
|
|
253
|
+
if (!metrics.hasOverflow) return;
|
|
254
|
+
|
|
255
|
+
const { viewportSize, scrollSize } = readTrack(orientation);
|
|
256
|
+
const startPointer = orientation === 'vertical' ? event.clientY : event.clientX;
|
|
257
|
+
const startOffset = metrics.offset;
|
|
258
|
+
|
|
259
|
+
draggingOrientation.value = orientation;
|
|
260
|
+
event.preventDefault();
|
|
261
|
+
|
|
262
|
+
drag.start({
|
|
263
|
+
onMove: (moveEvent: PointerEvent): void => {
|
|
264
|
+
const currentPointer = orientation === 'vertical' ? moveEvent.clientY : moveEvent.clientX;
|
|
265
|
+
const nextOffset = clampThumbOffset(
|
|
266
|
+
startOffset + (currentPointer - startPointer),
|
|
267
|
+
viewportSize,
|
|
268
|
+
metrics.size
|
|
269
|
+
);
|
|
270
|
+
const scrollOffset = getScrollOffsetFromThumb({
|
|
271
|
+
thumbOffset: nextOffset,
|
|
272
|
+
viewportSize,
|
|
273
|
+
scrollSize,
|
|
274
|
+
thumbSize: metrics.size,
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// 只写滚动位置;thumb 的新位置由 scroll 事件走同一套测量得到,避免两套真源。
|
|
278
|
+
if (orientation === 'vertical') {
|
|
279
|
+
el.scrollTop = scrollOffset;
|
|
280
|
+
} else {
|
|
281
|
+
el.scrollLeft = scrollOffset;
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
onEnd: () => {
|
|
285
|
+
draggingOrientation.value = null;
|
|
286
|
+
},
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* 观察视口与内容两个目标的尺寸变化。
|
|
292
|
+
*
|
|
293
|
+
* 只读不写视口尺寸(写的是视口之外滚动条上的 transform),因此不会形成
|
|
294
|
+
* 「观察 → 改尺寸 → 再触发」的回调循环。
|
|
295
|
+
*/
|
|
296
|
+
function observeTargets(el: HTMLElement): void {
|
|
297
|
+
resize.observe(el);
|
|
298
|
+
|
|
299
|
+
if (el.firstElementChild instanceof HTMLElement) resize.observe(el.firstElementChild);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function detachObservers(): void {
|
|
303
|
+
resize.disconnect();
|
|
304
|
+
mutationObserver?.disconnect();
|
|
305
|
+
mutationObserver = null;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
watch(viewportRef, (el) => {
|
|
309
|
+
detachObservers();
|
|
310
|
+
|
|
311
|
+
if (!el || !isObserverAvailable(globalThis.ResizeObserver)) return;
|
|
312
|
+
|
|
313
|
+
observeTargets(el);
|
|
314
|
+
|
|
315
|
+
// 内容子元素被整体替换时旧观察目标失效,需要重新挂载。
|
|
316
|
+
if (isObserverAvailable(globalThis.MutationObserver)) {
|
|
317
|
+
mutationObserver = new MutationObserver(() => {
|
|
318
|
+
observeTargets(el);
|
|
319
|
+
requestMeasure();
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
mutationObserver.observe(el, { childList: true });
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
requestMeasure();
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
onScopeDispose(() => {
|
|
329
|
+
detachObservers();
|
|
330
|
+
|
|
331
|
+
if (measureFrame !== null && isBrowser) {
|
|
332
|
+
window.cancelAnimationFrame(measureFrame);
|
|
333
|
+
measureFrame = null;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (hideTimer !== null) clearTimeout(hideTimer);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
return {
|
|
340
|
+
viewportRef,
|
|
341
|
+
type,
|
|
342
|
+
requestMeasure,
|
|
343
|
+
getRootProps,
|
|
344
|
+
getViewportProps,
|
|
345
|
+
getScrollbarProps,
|
|
346
|
+
getThumbProps,
|
|
347
|
+
getThumbStyle,
|
|
348
|
+
getCornerProps,
|
|
349
|
+
isCornerVisible,
|
|
350
|
+
getVisibility,
|
|
351
|
+
onViewportScroll,
|
|
352
|
+
onViewportPointerEnter,
|
|
353
|
+
onViewportPointerLeave,
|
|
354
|
+
onViewportFocusin,
|
|
355
|
+
onViewportFocusout,
|
|
356
|
+
onScrollbarPointerEnter,
|
|
357
|
+
onScrollbarPointerLeave,
|
|
358
|
+
onThumbPointerDown,
|
|
359
|
+
};
|
|
360
|
+
}
|