@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.
Files changed (196) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +59 -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,92 @@
1
+ import { computed, shallowRef } from 'vue';
2
+
3
+ import type { ComputedRef } from 'vue';
4
+
5
+ /**
6
+ * useCollection 的配置项。
7
+ */
8
+ export interface UseCollectionOptions<T> {
9
+ /**
10
+ * 从条目取唯一键。
11
+ *
12
+ * 同键重复登记时覆盖旧条目而不是追加:热更新或项的 value 变化会让 setup/watch 再跑
13
+ * 一次,追加会让同一个条目在遍历顺序里出现两遍。
14
+ *
15
+ * @param item 条目。
16
+ * @returns 唯一键。
17
+ */
18
+ key: (item: T) => string;
19
+ }
20
+
21
+ /**
22
+ * useCollection 的返回值。
23
+ */
24
+ export interface UseCollectionReturn<T> {
25
+ /** 按登记顺序排列的条目;消费方按自己的语义再过滤(可用项、活动项等) */
26
+ items: ComputedRef<T[]>;
27
+
28
+ /**
29
+ * 登记一个条目。
30
+ *
31
+ * @param item 条目。
32
+ * @returns 注销函数,只摘掉这个实例本身。
33
+ */
34
+ add(item: T): () => void;
35
+
36
+ /**
37
+ * 按键注销。
38
+ *
39
+ * @param key 条目键。
40
+ */
41
+ remove(key: string): void;
42
+
43
+ /**
44
+ * 原地替换同键条目。
45
+ *
46
+ * @param item 新条目,键必须与已登记的一致。
47
+ */
48
+ replace(item: T): void;
49
+ }
50
+
51
+ /**
52
+ * 父级组件登记子项的共享集合。
53
+ *
54
+ * 「子组件挂载时把自己登记进父级、卸载时摘掉」在 RadioGroup、Toast、ToggleGroup 里
55
+ * 各写了一遍,而且都踩过同一个坑:**条目里带 Ref 时不能用深响应**——`ref([])` 会拆掉
56
+ * 嵌套 Ref,父级读到的 `entry.element` 变成元素而不是 Ref,键盘导航与焦点回还就会
57
+ * 读到过期快照。这里用 shallowRef 把这个结论只解释一次。
58
+ *
59
+ * 顺序按登记顺序(即挂载顺序,模板里就是 DOM 顺序),需要别的顺序由调用方反转。
60
+ *
61
+ * @param options 取键方式。
62
+ * @returns 条目集合与登记入口。
63
+ */
64
+ export function useCollection<T>(options: UseCollectionOptions<T>): UseCollectionReturn<T> {
65
+ const entries = shallowRef<T[]>([]);
66
+
67
+ const items = computed(() => entries.value);
68
+
69
+ function add(item: T): () => void {
70
+ const key = options.key(item);
71
+
72
+ entries.value = [...entries.value.filter((existing) => options.key(existing) !== key), item];
73
+
74
+ return () => {
75
+ entries.value = entries.value.filter((existing) => existing !== item);
76
+ };
77
+ }
78
+
79
+ function remove(key: string): void {
80
+ entries.value = entries.value.filter((existing) => options.key(existing) !== key);
81
+ }
82
+
83
+ function replace(item: T): void {
84
+ const key = options.key(item);
85
+
86
+ entries.value = entries.value.map((existing) =>
87
+ options.key(existing) === key ? item : existing
88
+ );
89
+ }
90
+
91
+ return { items, add, remove, replace };
92
+ }
@@ -0,0 +1,96 @@
1
+ import { computed, shallowRef, toValue } from 'vue';
2
+
3
+ import type { ComputedRef, MaybeRefOrGetter } from 'vue';
4
+
5
+ /**
6
+ * 受控 / 非受控状态。
7
+ *
8
+ * 这是 Radix `useControllableState` 的对应物,也是本库所有带状态组件的真源判定入口。
9
+ *
10
+ * @template T 值类型。整体替换语义(数组也是换一个新数组而不是就地改),因此内部用
11
+ * shallowRef:深响应会把值里嵌套的 Ref 拆包,而这个 hook 无法保证调用方传进来的形状。
12
+ */
13
+ export interface UseControllableOptions<T> {
14
+ /**
15
+ * 受控值来源。
16
+ *
17
+ * **`undefined` 就是「未受控」的唯一判据**:封装层必须让 Boolean prop 缺省保持
18
+ * undefined,被 cast 成 false 会让组件变成「受控且恒为该值」,非受控用法静默失效。
19
+ */
20
+ value?: MaybeRefOrGetter<T | undefined>;
21
+
22
+ /** 非受控模式的初始值,必须由调用方给出(不提供隐式零值,避免各组件默认值漂移) */
23
+ defaultValue: T;
24
+
25
+ /**
26
+ * 值变化回调。
27
+ *
28
+ * 受控模式下同样会触发,且此时它是新值唯一的落地路径;因此回调不能只在非受控分支调用。
29
+ */
30
+ onChange?: (value: T) => void;
31
+ }
32
+
33
+ /**
34
+ * useControllable 的返回值。
35
+ */
36
+ export interface UseControllableReturn<T> {
37
+ /** 当前值:受控时跟随外部来源,否则是内部状态 */
38
+ value: ComputedRef<T>;
39
+
40
+ /** 是否处于受控模式 */
41
+ isControlled: ComputedRef<boolean>;
42
+
43
+ /**
44
+ * 写入新值。
45
+ *
46
+ * 顺序固定为「先落地内部状态,再通知外部」:这样回调里同步读 `value` 得到的就是新值。
47
+ * 受控模式下不写内部(写了就是越权改写调用方的状态),只通知。
48
+ *
49
+ * @param next 目标值。
50
+ */
51
+ set(next: T): void;
52
+
53
+ /**
54
+ * 只修正内部值,不通知外部。
55
+ *
56
+ * 留给「生命周期兜底修正」这类场景:子项被卸载时要把选中值清掉,但那不是用户操作,
57
+ * 不该冒出一个 change 事件。除这种场合外都应该用 set。
58
+ *
59
+ * @param next 目标值;受控模式下无操作。
60
+ */
61
+ replace(next: T): void;
62
+ }
63
+
64
+ /**
65
+ * 创建受控 / 非受控状态。
66
+ *
67
+ * @param options 受控来源、默认值与变化回调。
68
+ * @returns 值、受控判定与两种写入方式。
69
+ */
70
+ export function useControllable<T>(options: UseControllableOptions<T>): UseControllableReturn<T> {
71
+ const controlled = computed(() => toValue(options.value));
72
+
73
+ const isControlled = computed(() => controlled.value !== undefined);
74
+
75
+ const internal = shallowRef<T>(options.defaultValue);
76
+
77
+ const value = computed<T>(() => {
78
+ const current = controlled.value;
79
+
80
+ return current === undefined ? internal.value : current;
81
+ });
82
+
83
+ function set(next: T): void {
84
+ if (!isControlled.value) internal.value = next;
85
+
86
+ options.onChange?.(next);
87
+ }
88
+
89
+ function replace(next: T): void {
90
+ if (isControlled.value) return;
91
+
92
+ internal.value = next;
93
+ }
94
+
95
+ return { value, isControlled, set, replace };
96
+ }
@@ -0,0 +1,160 @@
1
+ import { onScopeDispose, toValue, watch } from 'vue';
2
+
3
+ import { getTabbableCandidates } from '@agile-plus/primitives/dom';
4
+
5
+ import type { MaybeRefOrGetter, Ref } from 'vue';
6
+
7
+ /**
8
+ * 焦点陷阱配置。
9
+ */
10
+ interface UseFocusTrapOptions {
11
+ /** 陷阱是否启用;关闭时立即移除全局监听。 */
12
+ active: MaybeRefOrGetter<boolean>;
13
+
14
+ /**
15
+ * 焦点允许停留的容器。
16
+ *
17
+ * 容器本身必须可聚焦(通常由 UI 层设置 tabindex="-1"),
18
+ * 用于没有任何可 Tab 元素时承接焦点。
19
+ */
20
+ container: Ref<HTMLElement | null>;
21
+ }
22
+
23
+ /**
24
+ * 已激活的陷阱栈。
25
+ *
26
+ * 嵌套对话框会各自激活一个陷阱;只有栈顶实例响应键盘与 focusin,
27
+ * 否则父子两个陷阱会把同一份焦点来回争抢。
28
+ */
29
+ const trapStack: symbol[] = [];
30
+
31
+ /**
32
+ * 聚焦目标;目标实际不可聚焦时退回容器,保证焦点不留在背景内容上。
33
+ */
34
+ function focusWithin(container: HTMLElement, target: HTMLElement): void {
35
+ target.focus({ preventScroll: true });
36
+
37
+ if (!container.contains(document.activeElement)) {
38
+ container.focus({ preventScroll: true });
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Dialog 的焦点陷阱。
44
+ *
45
+ * 打开期间把 Tab 循环限制在内容容器内,并在程序化 focus 落到容器外时把焦点收回。
46
+ * 关闭、容器卸载或作用域销毁时移除全部监听,不会影响之后的普通页面导航。
47
+ *
48
+ * @param options 陷阱启用状态与焦点容器。
49
+ */
50
+ export function useFocusTrap(options: UseFocusTrapOptions): void {
51
+ const token = Symbol('focus-trap');
52
+
53
+ let isActive = false;
54
+
55
+ function isTopmost(): boolean {
56
+ return trapStack[trapStack.length - 1] === token;
57
+ }
58
+
59
+ function handleKeydown(event: KeyboardEvent): void {
60
+ if (event.defaultPrevented || event.key !== 'Tab') return;
61
+ if (event.altKey || event.ctrlKey || event.metaKey) return;
62
+ if (!isTopmost()) return;
63
+
64
+ const container = options.container.value;
65
+
66
+ if (!container?.isConnected) return;
67
+
68
+ const tabbables = getTabbableCandidates(container);
69
+
70
+ if (tabbables.length === 0) {
71
+ event.preventDefault();
72
+ container.focus({ preventScroll: true });
73
+
74
+ return;
75
+ }
76
+
77
+ const first = tabbables[0];
78
+ const last = tabbables[tabbables.length - 1];
79
+ const current = document.activeElement;
80
+
81
+ if (!(current instanceof HTMLElement) || !container.contains(current)) {
82
+ event.preventDefault();
83
+ focusWithin(container, event.shiftKey ? last : first);
84
+
85
+ return;
86
+ }
87
+
88
+ const atStart = current === container || current === first;
89
+
90
+ if (event.shiftKey && atStart) {
91
+ event.preventDefault();
92
+ focusWithin(container, last);
93
+
94
+ return;
95
+ }
96
+
97
+ if (!event.shiftKey && current === last) {
98
+ event.preventDefault();
99
+ focusWithin(container, first);
100
+ }
101
+ }
102
+
103
+ function handleFocusIn(event: FocusEvent): void {
104
+ if (!isTopmost()) return;
105
+
106
+ const container = options.container.value;
107
+
108
+ if (!container?.isConnected) return;
109
+
110
+ const target = event.target;
111
+
112
+ if (target instanceof Node && container.contains(target)) return;
113
+
114
+ const tabbables = getTabbableCandidates(container);
115
+
116
+ focusWithin(container, tabbables[0] ?? container);
117
+ }
118
+
119
+ function activate(): void {
120
+ if (isActive) return;
121
+
122
+ isActive = true;
123
+ trapStack.push(token);
124
+
125
+ // keydown 走冒泡阶段:子组件先处理 Tab,再由陷阱在边界兜底。
126
+ document.addEventListener('keydown', handleKeydown);
127
+
128
+ // focusin 用捕获阶段,程序化 focus 逃逸也会被立即收回。
129
+ document.addEventListener('focusin', handleFocusIn, true);
130
+ }
131
+
132
+ function deactivate(): void {
133
+ if (!isActive) return;
134
+
135
+ isActive = false;
136
+
137
+ const index = trapStack.indexOf(token);
138
+
139
+ if (index !== -1) trapStack.splice(index, 1);
140
+
141
+ document.removeEventListener('keydown', handleKeydown);
142
+ document.removeEventListener('focusin', handleFocusIn, true);
143
+ }
144
+
145
+ watch(
146
+ [() => toValue(options.active), options.container],
147
+ ([active, container]) => {
148
+ if (active && container) {
149
+ activate();
150
+
151
+ return;
152
+ }
153
+
154
+ deactivate();
155
+ },
156
+ { immediate: true, flush: 'post' }
157
+ );
158
+
159
+ onScopeDispose(deactivate);
160
+ }
@@ -0,0 +1,80 @@
1
+ import { onScopeDispose, toValue, watch } from 'vue';
2
+
3
+ import { isBrowser } from './env';
4
+
5
+ import type { MaybeRefOrGetter, Ref } from 'vue';
6
+
7
+ /**
8
+ * 找到控件所属的表单。
9
+ *
10
+ * 显式 id 优先(控件可以放在 form 外面仍然归属它),否则先看原生 `form` 属性,
11
+ * 再向上找最近的 form 元素。收窄到 HTMLFormElement 是因为要读它的事件能力,
12
+ * closest 只给 Element。
13
+ */
14
+ function resolveForm(
15
+ element: HTMLElement | null,
16
+ formId: string | undefined
17
+ ): HTMLFormElement | null {
18
+ if (!isBrowser) return null;
19
+
20
+ if (formId) {
21
+ const node = document.getElementById(formId);
22
+
23
+ return node instanceof HTMLFormElement ? node : null;
24
+ }
25
+
26
+ // 原生 form 属性是「归属」的第一来源:input/button/select/textarea 都带它,
27
+ // 其余元素没有这个字段,因此这里做结构判断而不是类型断言。
28
+ const native = element && 'form' in element ? element.form : null;
29
+
30
+ if (native instanceof HTMLFormElement) return native;
31
+
32
+ const closest = element?.closest('form');
33
+
34
+ return closest instanceof HTMLFormElement ? closest : null;
35
+ }
36
+
37
+ /**
38
+ * 监听控件所属表单的 reset。
39
+ *
40
+ * `form.reset()` 会把原生值改回默认值,但组件状态是另一套真源:不接这个事件,
41
+ * 控件的值与 `data-invalid`、错误文案就会各说各话。归属关系会随着控件元素或
42
+ * 显式 id 变化,因此监听必须跟着一起迁移而不是在挂载时接一次。
43
+ *
44
+ * @param control 控件元素引用。
45
+ * @param onReset reset 时的动作。
46
+ * @param formId 显式归属的表单 id,优先级高于就近包含。
47
+ */
48
+ export function useFormResetListener(
49
+ control: Ref<HTMLElement | null>,
50
+ onReset: () => void,
51
+ formId?: MaybeRefOrGetter<string | undefined>
52
+ ): void {
53
+ let target: HTMLFormElement | null = null;
54
+
55
+ function detach(): void {
56
+ if (!target) return;
57
+
58
+ target.removeEventListener('reset', onReset);
59
+ target = null;
60
+ }
61
+
62
+ watch(
63
+ [control, () => toValue(formId)],
64
+ ([element, nextFormId]) => {
65
+ detach();
66
+
67
+ if (!isBrowser) return;
68
+
69
+ const form = resolveForm(element, nextFormId);
70
+
71
+ if (!form) return;
72
+
73
+ form.addEventListener('reset', onReset);
74
+ target = form;
75
+ },
76
+ { flush: 'post' }
77
+ );
78
+
79
+ onScopeDispose(detach);
80
+ }
@@ -0,0 +1,101 @@
1
+ import { onScopeDispose } from 'vue';
2
+
3
+ import { isBrowser } from './env';
4
+
5
+ /**
6
+ * 拖拽结束的原因。
7
+ *
8
+ * - `up`:指针正常抬起。
9
+ * - `cancel`:系统中断(触摸被接管、指针丢失),或被新的拖拽与组件卸载强行结束。
10
+ */
11
+ export type PointerDragEndReason = 'up' | 'cancel';
12
+
13
+ /**
14
+ * 一次拖拽的回调。
15
+ */
16
+ export interface UsePointerDragHandlers {
17
+ /** 每次指针移动 */
18
+ onMove(event: PointerEvent): void;
19
+
20
+ /**
21
+ * 拖拽结束。
22
+ *
23
+ * @param reason 结束原因;调用方据此决定是否提交(Slider 只在 `up` 时派发 commit)。
24
+ */
25
+ onEnd(reason: PointerDragEndReason): void;
26
+ }
27
+
28
+ /**
29
+ * usePointerDrag 的返回值。
30
+ */
31
+ export interface UsePointerDragReturn {
32
+ /**
33
+ * 开始一次拖拽。
34
+ *
35
+ * 已有进行中的拖拽会先以 `cancel` 结束:同一时刻只允许一次,否则两次按下会留下
36
+ * 两套互相覆盖的监听器。
37
+ *
38
+ * @param handlers 拖拽回调。
39
+ */
40
+ start(handlers: UsePointerDragHandlers): void;
41
+
42
+ /** 以 `cancel` 结束当前拖拽;没有拖拽时是空操作。 */
43
+ stop(): void;
44
+ }
45
+
46
+ /**
47
+ * 共享的指针拖拽接管。
48
+ *
49
+ * ScrollArea 的 thumb 与 Slider 的 thumb 需要同一套底座,且都刻意**不用**
50
+ * `setPointerCapture`:它在合成指针事件下会抛 InvalidStateError。监听挂在 window 上
51
+ * 同样能做到「指针移出控件后继续拖」,代价是必须自己保证释放——拖拽中途卸载或异常
52
+ * 中断若漏掉清理,状态会永久停在 dragging(滚动条再也不会隐藏、Slider 会一直跟随指针)。
53
+ * 把这段生命周期收口在这里,两个组件就不必各自维护一份 `disposeDragRef`。
54
+ *
55
+ * 只负责监听的登记与释放,不碰业务状态:坐标系、起点读数与提交语义都留在调用方。
56
+ *
57
+ * @returns 拖拽的启动与释放入口。
58
+ */
59
+ export function usePointerDrag(): UsePointerDragReturn {
60
+ /** 当前拖拽的收尾函数;null 表示没有进行中的拖拽。 */
61
+ let current: ((reason: PointerDragEndReason) => void) | null = null;
62
+
63
+ function start(handlers: UsePointerDragHandlers): void {
64
+ if (!isBrowser) return;
65
+
66
+ current?.('cancel');
67
+
68
+ const onMove = (event: PointerEvent): void => {
69
+ handlers.onMove(event);
70
+ };
71
+
72
+ const onUp = (): void => finish('up');
73
+
74
+ const onCancel = (): void => finish('cancel');
75
+
76
+ function finish(reason: PointerDragEndReason): void {
77
+ window.removeEventListener('pointermove', onMove);
78
+ window.removeEventListener('pointerup', onUp);
79
+ window.removeEventListener('pointercancel', onCancel);
80
+
81
+ // 被新拖拽顶替后,旧拖拽迟到的事件不该清掉新的收尾函数。
82
+ if (current === finish) current = null;
83
+
84
+ handlers.onEnd(reason);
85
+ }
86
+
87
+ current = finish;
88
+
89
+ window.addEventListener('pointermove', onMove);
90
+ window.addEventListener('pointerup', onUp);
91
+ window.addEventListener('pointercancel', onCancel);
92
+ }
93
+
94
+ function stop(): void {
95
+ current?.('cancel');
96
+ }
97
+
98
+ onScopeDispose(stop);
99
+
100
+ return { start, stop };
101
+ }
@@ -0,0 +1,60 @@
1
+ import { onScopeDispose } from 'vue';
2
+
3
+ import { isBrowser, isObserverAvailable } from './env';
4
+
5
+ /**
6
+ * useResizeObserver 的返回值。
7
+ */
8
+ export interface UseResizeObserverReturn {
9
+ /**
10
+ * 加入观察。重复观察同一元素是安全的(浏览器会去重)。
11
+ *
12
+ * @param element 目标元素,允许 null/undefined 以便直接传响应式 ref 的值。
13
+ */
14
+ observe(element: HTMLElement | null | undefined): void;
15
+
16
+ /**
17
+ * 停止观察单个元素;元素被替换时用它摘掉旧目标。
18
+ *
19
+ * @param element 目标元素。
20
+ */
21
+ unobserve(element: HTMLElement): void;
22
+
23
+ /** 断开全部观察。作用域销毁时会自动调用,手动调用只用于提前释放。 */
24
+ disconnect(): void;
25
+ }
26
+
27
+ /**
28
+ * 共享的尺寸观察器。
29
+ *
30
+ * ScrollArea(视口 + 内容首子元素)、Slider(多个 thumb)、Switch(按钮本体)都要
31
+ * 监听元素尺寸变化,但**测什么、测完做什么各不相同**,所以这里只统一三件真正相同的
32
+ * 事:环境守卫(jsdom / SSR 没有 ResizeObserver)、懒创建、随作用域释放。把
33
+ * `useElementSize` 这种「测量即结束」的形状强套给前两者,只会逼它们绕开钩子。
34
+ *
35
+ * @param onResize 任一被观察元素尺寸变化时的回调,节流/合并由调用方自己决定。
36
+ * @returns 观察与释放入口。
37
+ */
38
+ export function useResizeObserver(onResize: () => void): UseResizeObserverReturn {
39
+ let observer: ResizeObserver | null = null;
40
+
41
+ function observe(element: HTMLElement | null | undefined): void {
42
+ if (!element || !isBrowser || !isObserverAvailable(globalThis.ResizeObserver)) return;
43
+
44
+ observer ??= new ResizeObserver(() => onResize());
45
+ observer.observe(element);
46
+ }
47
+
48
+ function unobserve(element: HTMLElement): void {
49
+ observer?.unobserve(element);
50
+ }
51
+
52
+ function disconnect(): void {
53
+ observer?.disconnect();
54
+ observer = null;
55
+ }
56
+
57
+ onScopeDispose(disconnect);
58
+
59
+ return { observe, unobserve, disconnect };
60
+ }
@@ -0,0 +1,95 @@
1
+ import { onScopeDispose, toValue, watch } from 'vue';
2
+
3
+ import { isBrowser } from './env';
4
+
5
+ import type { MaybeRefOrGetter } from 'vue';
6
+
7
+ let lockCount = 0;
8
+ let originalOverflow = '';
9
+ let originalPaddingRight = '';
10
+
11
+ /**
12
+ * 读取滚动条占用的宽度。
13
+ *
14
+ * jsdom 不做布局,`documentElement.clientWidth` 为 0;此时如果直接相减会把整个视口
15
+ * 当成滚动条,因此只在布局信息有效时计算。
16
+ */
17
+ function getScrollbarWidth(): number {
18
+ const documentWidth = document.documentElement.clientWidth;
19
+
20
+ if (documentWidth <= 0) return 0;
21
+
22
+ return Math.max(0, window.innerWidth - documentWidth);
23
+ }
24
+
25
+ /**
26
+ * 获取滚动锁,返回释放函数。
27
+ *
28
+ * 用模块级计数支持多个消费者与嵌套对话框:只有最后一个持有者释放时才恢复原样式。
29
+ */
30
+ function acquireScrollLock(): () => void {
31
+ const body = document.body;
32
+
33
+ if (lockCount === 0) {
34
+ originalOverflow = body.style.overflow;
35
+ originalPaddingRight = body.style.paddingRight;
36
+
37
+ const scrollbarWidth = getScrollbarWidth();
38
+
39
+ // 锁定滚动会让滚动条消失;补上等宽内边距,避免页面内容横向跳动。
40
+ if (scrollbarWidth > 0) {
41
+ const currentPadding = Number.parseFloat(window.getComputedStyle(body).paddingRight) || 0;
42
+
43
+ body.style.paddingRight = `${currentPadding + scrollbarWidth}px`;
44
+ }
45
+
46
+ body.style.overflow = 'hidden';
47
+ }
48
+
49
+ lockCount += 1;
50
+
51
+ let released = false;
52
+
53
+ return () => {
54
+ if (released) return;
55
+
56
+ released = true;
57
+ lockCount = Math.max(0, lockCount - 1);
58
+
59
+ if (lockCount > 0) return;
60
+
61
+ body.style.overflow = originalOverflow;
62
+ body.style.paddingRight = originalPaddingRight;
63
+ };
64
+ }
65
+
66
+ /**
67
+ * 锁定 `body` 滚动,支持多个消费者与嵌套对话框。
68
+ *
69
+ * @param active 是否持有滚动锁。
70
+ */
71
+ export function useScrollLock(active: MaybeRefOrGetter<boolean>): void {
72
+ if (!isBrowser) return;
73
+
74
+ let release: (() => void) | null = null;
75
+
76
+ watch(
77
+ () => toValue(active),
78
+ (isActive) => {
79
+ if (isActive) {
80
+ release ??= acquireScrollLock();
81
+
82
+ return;
83
+ }
84
+
85
+ release?.();
86
+ release = null;
87
+ },
88
+ { immediate: true }
89
+ );
90
+
91
+ onScopeDispose(() => {
92
+ release?.();
93
+ release = null;
94
+ });
95
+ }