@agile-plus/headless 0.1.2 → 0.2.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 (231) hide show
  1. package/README.md +24 -17
  2. package/dist/index.cjs +4163 -223
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +33 -0
  5. package/dist/index.d.ts +33 -0
  6. package/dist/index.js +4258 -216
  7. package/dist/index.js.map +1 -1
  8. package/dist/types-BxsQI7P5.d.cts +93 -0
  9. package/dist/types-BxsQI7P5.d.ts +93 -0
  10. package/dist/types-zpeY0yvD.d.cts +73 -0
  11. package/dist/types-zpeY0yvD.d.ts +73 -0
  12. package/dist/useAccordion/index.cjs +149 -0
  13. package/dist/useAccordion/index.cjs.map +1 -0
  14. package/dist/useAccordion/index.d.cts +119 -0
  15. package/dist/useAccordion/index.d.ts +119 -0
  16. package/dist/useAccordion/index.js +128 -0
  17. package/dist/useAccordion/index.js.map +1 -0
  18. package/dist/useAvatar/index.cjs +135 -0
  19. package/dist/useAvatar/index.cjs.map +1 -0
  20. package/dist/useAvatar/index.d.cts +120 -0
  21. package/dist/useAvatar/index.d.ts +120 -0
  22. package/dist/useAvatar/index.js +106 -0
  23. package/dist/useAvatar/index.js.map +1 -0
  24. package/dist/useCheckbox/index.cjs +3 -1
  25. package/dist/useCheckbox/index.cjs.map +1 -1
  26. package/dist/useCheckbox/index.js +3 -1
  27. package/dist/useCheckbox/index.js.map +1 -1
  28. package/dist/useCollapsible/index.cjs +10 -0
  29. package/dist/useCollapsible/index.cjs.map +1 -1
  30. package/dist/useCollapsible/index.js +11 -1
  31. package/dist/useCollapsible/index.js.map +1 -1
  32. package/dist/useDatePicker/index.cjs +684 -0
  33. package/dist/useDatePicker/index.cjs.map +1 -0
  34. package/dist/useDatePicker/index.d.cts +191 -0
  35. package/dist/useDatePicker/index.d.ts +191 -0
  36. package/dist/useDatePicker/index.js +671 -0
  37. package/dist/useDatePicker/index.js.map +1 -0
  38. package/dist/useDateRangePicker/index.cjs +614 -0
  39. package/dist/useDateRangePicker/index.cjs.map +1 -0
  40. package/dist/useDateRangePicker/index.d.cts +159 -0
  41. package/dist/useDateRangePicker/index.d.ts +159 -0
  42. package/dist/useDateRangePicker/index.js +601 -0
  43. package/dist/useDateRangePicker/index.js.map +1 -0
  44. package/dist/useDialog/index.cjs.map +1 -1
  45. package/dist/useDialog/index.js.map +1 -1
  46. package/dist/useDropdownMenu/index.cjs +468 -0
  47. package/dist/useDropdownMenu/index.cjs.map +1 -0
  48. package/dist/useDropdownMenu/index.d.cts +73 -0
  49. package/dist/useDropdownMenu/index.d.ts +73 -0
  50. package/dist/useDropdownMenu/index.js +441 -0
  51. package/dist/useDropdownMenu/index.js.map +1 -0
  52. package/dist/useEllipsis/index.cjs +181 -0
  53. package/dist/useEllipsis/index.cjs.map +1 -0
  54. package/dist/useEllipsis/index.d.cts +67 -0
  55. package/dist/useEllipsis/index.d.ts +67 -0
  56. package/dist/useEllipsis/index.js +162 -0
  57. package/dist/useEllipsis/index.js.map +1 -0
  58. package/dist/useFloating/index.cjs +177 -0
  59. package/dist/useFloating/index.cjs.map +1 -0
  60. package/dist/useFloating/index.d.cts +21 -0
  61. package/dist/useFloating/index.d.ts +21 -0
  62. package/dist/useFloating/index.js +150 -0
  63. package/dist/useFloating/index.js.map +1 -0
  64. package/dist/useFormControl/index.cjs +5 -0
  65. package/dist/useFormControl/index.cjs.map +1 -1
  66. package/dist/useFormControl/index.js +5 -0
  67. package/dist/useFormControl/index.js.map +1 -1
  68. package/dist/useInput/index.cjs +192 -0
  69. package/dist/useInput/index.cjs.map +1 -0
  70. package/dist/useInput/index.d.cts +41 -0
  71. package/dist/useInput/index.d.ts +41 -0
  72. package/dist/useInput/index.js +165 -0
  73. package/dist/useInput/index.js.map +1 -0
  74. package/dist/useKeepMounted/index.cjs +44 -0
  75. package/dist/useKeepMounted/index.cjs.map +1 -0
  76. package/dist/useKeepMounted/index.d.cts +45 -0
  77. package/dist/useKeepMounted/index.d.ts +45 -0
  78. package/dist/useKeepMounted/index.js +17 -0
  79. package/dist/useKeepMounted/index.js.map +1 -0
  80. package/dist/useMenu/index.cjs +438 -0
  81. package/dist/useMenu/index.cjs.map +1 -0
  82. package/dist/useMenu/index.d.cts +134 -0
  83. package/dist/useMenu/index.d.ts +134 -0
  84. package/dist/useMenu/index.js +419 -0
  85. package/dist/useMenu/index.js.map +1 -0
  86. package/dist/usePagination/index.cjs +113 -0
  87. package/dist/usePagination/index.cjs.map +1 -0
  88. package/dist/usePagination/index.d.cts +139 -0
  89. package/dist/usePagination/index.d.ts +139 -0
  90. package/dist/usePagination/index.js +95 -0
  91. package/dist/usePagination/index.js.map +1 -0
  92. package/dist/usePopover/index.cjs +312 -0
  93. package/dist/usePopover/index.cjs.map +1 -0
  94. package/dist/usePopover/index.d.cts +71 -0
  95. package/dist/usePopover/index.d.ts +71 -0
  96. package/dist/usePopover/index.js +285 -0
  97. package/dist/usePopover/index.js.map +1 -0
  98. package/dist/useSelect/index.cjs +729 -0
  99. package/dist/useSelect/index.cjs.map +1 -0
  100. package/dist/useSelect/index.d.cts +193 -0
  101. package/dist/useSelect/index.d.ts +193 -0
  102. package/dist/useSelect/index.js +710 -0
  103. package/dist/useSelect/index.js.map +1 -0
  104. package/dist/useSplitter/index.cjs +558 -0
  105. package/dist/useSplitter/index.cjs.map +1 -0
  106. package/dist/useSplitter/index.d.cts +307 -0
  107. package/dist/useSplitter/index.d.ts +307 -0
  108. package/dist/useSplitter/index.js +541 -0
  109. package/dist/useSplitter/index.js.map +1 -0
  110. package/dist/useSwitch/index.cjs +4 -1
  111. package/dist/useSwitch/index.cjs.map +1 -1
  112. package/dist/useSwitch/index.js +4 -1
  113. package/dist/useSwitch/index.js.map +1 -1
  114. package/dist/useTable/index.cjs +308 -0
  115. package/dist/useTable/index.cjs.map +1 -0
  116. package/dist/useTable/index.d.cts +242 -0
  117. package/dist/useTable/index.d.ts +242 -0
  118. package/dist/useTable/index.js +301 -0
  119. package/dist/useTable/index.js.map +1 -0
  120. package/dist/useTabs/index.cjs +13 -4
  121. package/dist/useTabs/index.cjs.map +1 -1
  122. package/dist/useTabs/index.d.cts +5 -1
  123. package/dist/useTabs/index.d.ts +5 -1
  124. package/dist/useTabs/index.js +13 -4
  125. package/dist/useTabs/index.js.map +1 -1
  126. package/dist/useTextControl-Dcq0GFmg.d.cts +63 -0
  127. package/dist/useTextControl-Dcq0GFmg.d.ts +63 -0
  128. package/dist/useTextarea/index.cjs +186 -0
  129. package/dist/useTextarea/index.cjs.map +1 -0
  130. package/dist/useTextarea/index.d.cts +30 -0
  131. package/dist/useTextarea/index.d.ts +30 -0
  132. package/dist/useTextarea/index.js +159 -0
  133. package/dist/useTextarea/index.js.map +1 -0
  134. package/dist/useToast/index.cjs +27 -20
  135. package/dist/useToast/index.cjs.map +1 -1
  136. package/dist/useToast/index.js +27 -20
  137. package/dist/useToast/index.js.map +1 -1
  138. package/dist/useToastViewport/index.cjs +1 -3
  139. package/dist/useToastViewport/index.cjs.map +1 -1
  140. package/dist/useToastViewport/index.js +1 -3
  141. package/dist/useToastViewport/index.js.map +1 -1
  142. package/dist/useTooltip/index.cjs +357 -0
  143. package/dist/useTooltip/index.cjs.map +1 -0
  144. package/dist/useTooltip/index.d.cts +81 -0
  145. package/dist/useTooltip/index.d.ts +81 -0
  146. package/dist/useTooltip/index.js +338 -0
  147. package/dist/useTooltip/index.js.map +1 -0
  148. package/dist/useVirtualList/index.cjs +308 -0
  149. package/dist/useVirtualList/index.cjs.map +1 -0
  150. package/dist/useVirtualList/index.d.cts +31 -0
  151. package/dist/useVirtualList/index.d.ts +31 -0
  152. package/dist/useVirtualList/index.js +288 -0
  153. package/dist/useVirtualList/index.js.map +1 -0
  154. package/dist/useVirtualTree/index.cjs +520 -0
  155. package/dist/useVirtualTree/index.cjs.map +1 -0
  156. package/dist/useVirtualTree/index.d.cts +146 -0
  157. package/dist/useVirtualTree/index.d.ts +146 -0
  158. package/dist/useVirtualTree/index.js +511 -0
  159. package/dist/useVirtualTree/index.js.map +1 -0
  160. package/package.json +121 -2
  161. package/src/index.ts +38 -0
  162. package/src/useAccordion/index.ts +7 -0
  163. package/src/useAccordion/types.ts +118 -0
  164. package/src/useAccordion/useAccordion.ts +130 -0
  165. package/src/useAvatar/index.ts +14 -0
  166. package/src/useAvatar/types.ts +90 -0
  167. package/src/useAvatar/useAvatar.ts +39 -0
  168. package/src/useAvatar/useAvatarFallback.ts +62 -0
  169. package/src/useAvatar/useAvatarImage.ts +78 -0
  170. package/src/useCheckbox/useCheckbox.ts +4 -1
  171. package/src/useCollapsible/useCollapsible.ts +22 -1
  172. package/src/useDatePicker/index.ts +9 -0
  173. package/src/useDatePicker/types.ts +177 -0
  174. package/src/useDatePicker/useDatePicker.ts +688 -0
  175. package/src/useDateRangePicker/index.ts +8 -0
  176. package/src/useDateRangePicker/types.ts +146 -0
  177. package/src/useDateRangePicker/useDateRangePicker.ts +618 -0
  178. package/src/useDropdownMenu/index.ts +3 -0
  179. package/src/useDropdownMenu/types.ts +61 -0
  180. package/src/useDropdownMenu/useDropdownMenu.ts +322 -0
  181. package/src/useEllipsis/index.ts +3 -0
  182. package/src/useEllipsis/types.ts +51 -0
  183. package/src/useEllipsis/useEllipsis.ts +169 -0
  184. package/src/useFloating/index.ts +3 -0
  185. package/src/useFloating/types.ts +79 -0
  186. package/src/useFloating/useFloating.ts +199 -0
  187. package/src/useFormControl/useFormControl.ts +13 -0
  188. package/src/useInput/index.ts +3 -0
  189. package/src/useInput/types.ts +29 -0
  190. package/src/useInput/useInput.ts +40 -0
  191. package/src/useKeepMounted/index.ts +3 -0
  192. package/src/useKeepMounted/types.ts +29 -0
  193. package/src/useKeepMounted/useKeepMounted.ts +34 -0
  194. package/src/useMenu/index.ts +9 -0
  195. package/src/useMenu/types.ts +126 -0
  196. package/src/useMenu/useMenu.ts +504 -0
  197. package/src/usePagination/index.ts +3 -0
  198. package/src/usePagination/types.ts +137 -0
  199. package/src/usePagination/usePagination.ts +109 -0
  200. package/src/usePopover/index.ts +3 -0
  201. package/src/usePopover/types.ts +65 -0
  202. package/src/usePopover/usePopover.ts +157 -0
  203. package/src/useSelect/index.ts +9 -0
  204. package/src/useSelect/types.ts +184 -0
  205. package/src/useSelect/useSelect.ts +713 -0
  206. package/src/useSplitter/index.ts +10 -0
  207. package/src/useSplitter/types.ts +336 -0
  208. package/src/useSplitter/useSplitter.ts +648 -0
  209. package/src/useSplitter/useSplitterPanel.ts +106 -0
  210. package/src/useSwitch/useSwitch.ts +6 -1
  211. package/src/useTable/index.ts +3 -0
  212. package/src/useTable/types.ts +282 -0
  213. package/src/useTable/useTable.ts +407 -0
  214. package/src/useTabs/types.ts +5 -1
  215. package/src/useTabs/useTabs.ts +30 -6
  216. package/src/useTextarea/index.ts +3 -0
  217. package/src/useTextarea/types.ts +17 -0
  218. package/src/useTextarea/useTextarea.ts +34 -0
  219. package/src/useToast/useToast.ts +43 -26
  220. package/src/useToastViewport/useToastViewport.ts +10 -9
  221. package/src/useTooltip/index.ts +3 -0
  222. package/src/useTooltip/types.ts +72 -0
  223. package/src/useTooltip/useTooltip.ts +240 -0
  224. package/src/useVirtualList/index.ts +8 -0
  225. package/src/useVirtualList/types.ts +95 -0
  226. package/src/useVirtualList/useVirtualList.ts +478 -0
  227. package/src/useVirtualTree/index.ts +3 -0
  228. package/src/useVirtualTree/types.ts +179 -0
  229. package/src/useVirtualTree/useVirtualTree.ts +342 -0
  230. package/src/utils/useFocusTrap.ts +2 -0
  231. package/src/utils/useTextControl.ts +191 -0
@@ -0,0 +1,729 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/useSelect/index.ts
21
+ var useSelect_exports = {};
22
+ __export(useSelect_exports, {
23
+ useSelect: () => useSelect
24
+ });
25
+ module.exports = __toCommonJS(useSelect_exports);
26
+
27
+ // src/useSelect/useSelect.ts
28
+ var import_vue6 = require("vue");
29
+ var import_form = require("@agile-plus/primitives/form");
30
+ var import_select = require("@agile-plus/primitives/select");
31
+
32
+ // src/utils/useControllable.ts
33
+ var import_vue = require("vue");
34
+ function useControllable(options) {
35
+ const controlled = (0, import_vue.computed)(() => (0, import_vue.toValue)(options.value));
36
+ const isControlled = (0, import_vue.computed)(() => controlled.value !== void 0);
37
+ const internal = (0, import_vue.shallowRef)(options.defaultValue);
38
+ const value = (0, import_vue.computed)(() => {
39
+ const current = controlled.value;
40
+ return current === void 0 ? internal.value : current;
41
+ });
42
+ function set(next) {
43
+ if (!isControlled.value) internal.value = next;
44
+ options.onChange?.(next);
45
+ }
46
+ function replace(next) {
47
+ if (isControlled.value) return;
48
+ internal.value = next;
49
+ }
50
+ return { value, isControlled, set, replace };
51
+ }
52
+
53
+ // src/utils/useCollection.ts
54
+ var import_vue2 = require("vue");
55
+ function useCollection(options) {
56
+ const entries = (0, import_vue2.shallowRef)([]);
57
+ const items = (0, import_vue2.computed)(() => entries.value);
58
+ function add(item) {
59
+ const key = options.key(item);
60
+ entries.value = [...entries.value.filter((existing) => options.key(existing) !== key), item];
61
+ return () => {
62
+ entries.value = entries.value.filter((existing) => existing !== item);
63
+ };
64
+ }
65
+ function remove(key) {
66
+ entries.value = entries.value.filter((existing) => options.key(existing) !== key);
67
+ }
68
+ function replace(item) {
69
+ const key = options.key(item);
70
+ entries.value = entries.value.map(
71
+ (existing) => options.key(existing) === key ? item : existing
72
+ );
73
+ }
74
+ return { items, add, remove, replace };
75
+ }
76
+
77
+ // src/utils/useFormResetListener.ts
78
+ var import_vue3 = require("vue");
79
+
80
+ // src/utils/env.ts
81
+ var isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
82
+ function isObserverAvailable(constructor) {
83
+ return typeof constructor === "function";
84
+ }
85
+
86
+ // src/utils/useFormResetListener.ts
87
+ function resolveForm(element, formId) {
88
+ if (!isBrowser) return null;
89
+ if (formId) {
90
+ const node = document.getElementById(formId);
91
+ return node instanceof HTMLFormElement ? node : null;
92
+ }
93
+ const native = element && "form" in element ? element.form : null;
94
+ if (native instanceof HTMLFormElement) return native;
95
+ const closest = element?.closest("form");
96
+ return closest instanceof HTMLFormElement ? closest : null;
97
+ }
98
+ function useFormResetListener(control, onReset, formId) {
99
+ let target = null;
100
+ function detach() {
101
+ if (!target) return;
102
+ target.removeEventListener("reset", onReset);
103
+ target = null;
104
+ }
105
+ (0, import_vue3.watch)(
106
+ [control, () => (0, import_vue3.toValue)(formId)],
107
+ ([element, nextFormId]) => {
108
+ detach();
109
+ if (!isBrowser) return;
110
+ const form = resolveForm(element, nextFormId);
111
+ if (!form) return;
112
+ form.addEventListener("reset", onReset);
113
+ target = form;
114
+ },
115
+ { flush: "post" }
116
+ );
117
+ (0, import_vue3.onScopeDispose)(detach);
118
+ }
119
+
120
+ // src/utils/useResizeObserver.ts
121
+ var import_vue4 = require("vue");
122
+ function useResizeObserver(onResize) {
123
+ let observer = null;
124
+ function observe(element) {
125
+ if (!element || !isBrowser || !isObserverAvailable(globalThis.ResizeObserver)) return;
126
+ observer ??= new ResizeObserver(() => onResize());
127
+ observer.observe(element);
128
+ }
129
+ function unobserve(element) {
130
+ observer?.unobserve(element);
131
+ }
132
+ function disconnect() {
133
+ observer?.disconnect();
134
+ observer = null;
135
+ }
136
+ (0, import_vue4.onScopeDispose)(disconnect);
137
+ return { observe, unobserve, disconnect };
138
+ }
139
+
140
+ // src/useFloating/useFloating.ts
141
+ var import_vue5 = require("vue");
142
+ var import_floating = require("@agile-plus/primitives/floating");
143
+ function useFloating(options = {}) {
144
+ const referenceRef = (0, import_vue5.ref)(null);
145
+ const floatingRef = (0, import_vue5.ref)(null);
146
+ const x = (0, import_vue5.ref)(0);
147
+ const y = (0, import_vue5.ref)(0);
148
+ const placement = (0, import_vue5.ref)((0, import_vue5.toValue)(options.placement) ?? "bottom");
149
+ const arrow = (0, import_vue5.ref)(null);
150
+ const availableSpace = (0, import_vue5.ref)(null);
151
+ const isOpen = (0, import_vue5.computed)(() => (0, import_vue5.toValue)(options.open) ?? true);
152
+ const strategy = (0, import_vue5.computed)(() => options.strategy ?? "absolute");
153
+ let frame = null;
154
+ const resize = useResizeObserver(requestUpdate);
155
+ function requestUpdate() {
156
+ if (!isBrowser || frame !== null) return;
157
+ frame = window.requestAnimationFrame(() => {
158
+ frame = null;
159
+ measure();
160
+ });
161
+ }
162
+ function measure() {
163
+ const reference = referenceRef.value;
164
+ const floating = floatingRef.value;
165
+ if (!reference || !floating) return;
166
+ const referenceRect = reference.getBoundingClientRect();
167
+ const floatingRect = floating.getBoundingClientRect();
168
+ const boundaryRect = (0, import_vue5.toValue)(options.boundary)?.getBoundingClientRect();
169
+ const viewport = { width: window.innerWidth, height: window.innerHeight };
170
+ const scrollX = window.scrollX;
171
+ const scrollY = window.scrollY;
172
+ const snapshot = {
173
+ reference: {
174
+ x: referenceRect.x,
175
+ y: referenceRect.y,
176
+ width: referenceRect.width,
177
+ height: referenceRect.height
178
+ },
179
+ // 浮层自己的 x/y 是上一轮结果,传 0 以免本轮输出依赖历史状态。
180
+ floating: { x: 0, y: 0, width: floatingRect.width, height: floatingRect.height },
181
+ viewport,
182
+ ...boundaryRect ? {
183
+ boundary: {
184
+ x: boundaryRect.x,
185
+ y: boundaryRect.y,
186
+ width: boundaryRect.width,
187
+ height: boundaryRect.height
188
+ }
189
+ } : {}
190
+ };
191
+ const floatingOptions = {
192
+ placement: (0, import_vue5.toValue)(options.placement),
193
+ offset: options.offset,
194
+ flip: options.flip,
195
+ shift: options.shift,
196
+ padding: options.padding,
197
+ arrow: options.arrow
198
+ };
199
+ const result = (0, import_floating.computeFloatingPosition)(snapshot, floatingOptions);
200
+ x.value = strategy.value === "absolute" ? result.x + scrollX : result.x;
201
+ y.value = strategy.value === "absolute" ? result.y + scrollY : result.y;
202
+ placement.value = result.placement;
203
+ arrow.value = result.arrow;
204
+ availableSpace.value = (0, import_floating.computeAvailableSpace)(snapshot, result.placement, floatingOptions);
205
+ }
206
+ const floatingStyle = (0, import_vue5.computed)(() => ({
207
+ position: strategy.value,
208
+ left: 0,
209
+ top: 0,
210
+ transform: `translate(${x.value}px, ${y.value}px)`
211
+ }));
212
+ const arrowStyle = (0, import_vue5.computed)(() => {
213
+ const offset = arrow.value;
214
+ if (!offset) return {};
215
+ return offset.x !== null ? { position: "absolute", left: `${offset.x}px` } : { position: "absolute", top: `${offset.y}px` };
216
+ });
217
+ function attach() {
218
+ window.addEventListener("scroll", requestUpdate, true);
219
+ window.addEventListener("resize", requestUpdate);
220
+ resize.observe(referenceRef.value);
221
+ resize.observe(floatingRef.value);
222
+ }
223
+ function detach() {
224
+ if (!isBrowser) return;
225
+ window.removeEventListener("scroll", requestUpdate, true);
226
+ window.removeEventListener("resize", requestUpdate);
227
+ resize.disconnect();
228
+ }
229
+ (0, import_vue5.watch)(
230
+ [isOpen, referenceRef, floatingRef],
231
+ ([open]) => {
232
+ detach();
233
+ if (!isBrowser || !open) return;
234
+ attach();
235
+ void (0, import_vue5.nextTick)(measure);
236
+ },
237
+ { immediate: true }
238
+ );
239
+ (0, import_vue5.onScopeDispose)(() => {
240
+ detach();
241
+ if (frame !== null && isBrowser) {
242
+ window.cancelAnimationFrame(frame);
243
+ frame = null;
244
+ }
245
+ });
246
+ return {
247
+ referenceRef,
248
+ floatingRef,
249
+ x: (0, import_vue5.computed)(() => x.value),
250
+ y: (0, import_vue5.computed)(() => y.value),
251
+ placement: (0, import_vue5.computed)(() => placement.value),
252
+ availableSpace: (0, import_vue5.computed)(() => availableSpace.value),
253
+ floatingStyle,
254
+ arrowStyle,
255
+ update: requestUpdate
256
+ };
257
+ }
258
+
259
+ // src/useSelect/useSelect.ts
260
+ var DEFAULT_OFFSET = 4;
261
+ var DEFAULT_PADDING = 8;
262
+ function useSelect(options = {}) {
263
+ const uid = (0, import_vue6.useId)();
264
+ const triggerId = `select-trigger-${uid}`;
265
+ const contentId = `select-content-${uid}`;
266
+ const valueState = useControllable({
267
+ value: () => (0, import_vue6.toValue)(options.modelValue),
268
+ defaultValue: options.defaultValue ?? "",
269
+ onChange: (next) => options.onValueChange?.(next)
270
+ });
271
+ const openState = useControllable({
272
+ value: () => (0, import_vue6.toValue)(options.open),
273
+ defaultValue: options.defaultOpen ?? false,
274
+ onChange: (next) => options.onOpenChange?.(next)
275
+ });
276
+ const value = valueState.value;
277
+ const isOpen = openState.value;
278
+ const isDisabled = (0, import_vue6.computed)(() => (0, import_vue6.toValue)(options.disabled) === true);
279
+ const isRequired = (0, import_vue6.computed)(() => (0, import_vue6.toValue)(options.required) === true);
280
+ const loop = options.loop ?? true;
281
+ const highlightedValue = (0, import_vue6.ref)(null);
282
+ const collection = useCollection({ key: (entry) => entry.id });
283
+ const floating = useFloating({
284
+ placement: options.placement ?? "bottom-start",
285
+ offset: options.offset ?? DEFAULT_OFFSET,
286
+ flip: options.flip,
287
+ shift: options.shift,
288
+ padding: options.padding ?? DEFAULT_PADDING,
289
+ strategy: options.strategy,
290
+ open: isOpen
291
+ });
292
+ const viewportElement = (0, import_vue6.ref)(null);
293
+ const nativeSelectElement = (0, import_vue6.ref)(null);
294
+ const triggerSize = (0, import_vue6.shallowRef)(null);
295
+ const optionList = (0, import_vue6.computed)(
296
+ () => collection.items.value.map((entry) => ({
297
+ value: entry.value.value,
298
+ textValue: entry.textValue.value,
299
+ disabled: isDisabled.value || entry.disabled.value
300
+ }))
301
+ );
302
+ const hasValue = (0, import_vue6.computed)(() => value.value !== "");
303
+ const optionCache = (0, import_vue6.shallowRef)(/* @__PURE__ */ new Map());
304
+ (0, import_vue6.watch)(
305
+ () => (0, import_vue6.toValue)(options.options) ?? [],
306
+ (list) => {
307
+ if (list.length === 0) return;
308
+ const next = new Map(optionCache.value);
309
+ for (const option of list) {
310
+ next.set(option.value, {
311
+ value: option.value,
312
+ textValue: option.textValue ?? option.label ?? option.value,
313
+ disabled: option.disabled === true
314
+ });
315
+ }
316
+ optionCache.value = next;
317
+ },
318
+ { immediate: true }
319
+ );
320
+ (0, import_vue6.watch)(
321
+ optionList,
322
+ (list) => {
323
+ if (list.length === 0) return;
324
+ const next = new Map(optionCache.value);
325
+ for (const option of list) next.set(option.value, option);
326
+ optionCache.value = next;
327
+ },
328
+ { immediate: true }
329
+ );
330
+ const cachedOptions = (0, import_vue6.computed)(() => [...optionCache.value.values()]);
331
+ const typeaheadOptions = (0, import_vue6.computed)(
332
+ () => optionList.value.length > 0 ? optionList.value : cachedOptions.value
333
+ );
334
+ const selectedText = (0, import_vue6.computed)(
335
+ () => optionList.value.find((option) => option.value === value.value)?.textValue ?? optionCache.value.get(value.value)?.textValue ?? ""
336
+ );
337
+ const activeDescendantId = (0, import_vue6.computed)(() => {
338
+ if (!isOpen.value || highlightedValue.value === null) return void 0;
339
+ return collection.items.value.find((entry) => entry.value.value === highlightedValue.value)?.id;
340
+ });
341
+ const isFormControl = (0, import_vue6.computed)(() => {
342
+ if ((0, import_vue6.toValue)(options.name) || (0, import_vue6.toValue)(options.form)) return true;
343
+ const trigger = floating.referenceRef.value;
344
+ if (!trigger) return true;
345
+ return trigger.closest("form") !== null;
346
+ });
347
+ const resize = useResizeObserver(measureTrigger);
348
+ let typeaheadSearch = "";
349
+ let typeaheadAt = null;
350
+ function getItemId(localId) {
351
+ return `${contentId}-item-${localId}`;
352
+ }
353
+ function findEntry(itemValue) {
354
+ return collection.items.value.find((entry) => entry.value.value === itemValue);
355
+ }
356
+ function setValue(next) {
357
+ valueState.set(next);
358
+ }
359
+ function open() {
360
+ if (isDisabled.value) return;
361
+ highlightedValue.value = (0, import_select.resolveOpenHighlight)({
362
+ options: optionList.value,
363
+ selectedValue: value.value,
364
+ edge: "first"
365
+ });
366
+ openState.set(true);
367
+ }
368
+ function close() {
369
+ highlightedValue.value = null;
370
+ typeaheadSearch = "";
371
+ typeaheadAt = null;
372
+ openState.set(false);
373
+ }
374
+ function toggle() {
375
+ if (isOpen.value) close();
376
+ else open();
377
+ }
378
+ function commitHighlighted() {
379
+ const current = highlightedValue.value;
380
+ if (current !== null) setValue(current);
381
+ close();
382
+ }
383
+ function moveHighlight(intent) {
384
+ const next = (0, import_select.resolveSelectHighlight)({
385
+ intent,
386
+ options: optionList.value,
387
+ currentValue: highlightedValue.value,
388
+ loop
389
+ });
390
+ if (next !== null) highlightedValue.value = next;
391
+ }
392
+ function runTypeahead(char) {
393
+ const now = Date.now();
394
+ const timeout = (0, import_vue6.toValue)(options.typeaheadTimeout) ?? import_select.DEFAULT_TYPEAHEAD_TIMEOUT;
395
+ typeaheadSearch = (0, import_select.appendTypeaheadSearch)({
396
+ previous: typeaheadSearch,
397
+ char,
398
+ elapsed: typeaheadAt === null ? timeout : now - typeaheadAt,
399
+ timeout
400
+ });
401
+ typeaheadAt = now;
402
+ const match = (0, import_select.findTypeaheadMatch)({
403
+ options: typeaheadOptions.value,
404
+ search: typeaheadSearch,
405
+ currentValue: isOpen.value ? highlightedValue.value : value.value || null
406
+ });
407
+ if (match === null) return;
408
+ if (isOpen.value) highlightedValue.value = match;
409
+ else setValue(match);
410
+ }
411
+ function onTriggerClick() {
412
+ if (isDisabled.value) return;
413
+ toggle();
414
+ }
415
+ function onTriggerKeydown(event) {
416
+ if (isDisabled.value) return;
417
+ const intent = (0, import_select.resolveSelectKeyIntent)({
418
+ key: event.key,
419
+ open: isOpen.value,
420
+ hasModifierKey: event.ctrlKey || event.altKey || event.metaKey
421
+ });
422
+ if (intent.kind === "none") return;
423
+ if (intent.kind !== "tab-out") event.preventDefault();
424
+ switch (intent.kind) {
425
+ case "open":
426
+ highlightedValue.value = (0, import_select.resolveOpenHighlight)({
427
+ options: optionList.value,
428
+ selectedValue: value.value,
429
+ edge: intent.edge
430
+ });
431
+ openState.set(true);
432
+ break;
433
+ case "close":
434
+ close();
435
+ break;
436
+ case "tab-out":
437
+ close();
438
+ break;
439
+ case "commit":
440
+ commitHighlighted();
441
+ break;
442
+ case "highlight":
443
+ moveHighlight(intent.intent);
444
+ break;
445
+ case "typeahead":
446
+ runTypeahead(intent.char);
447
+ break;
448
+ }
449
+ }
450
+ function onTriggerBlur(event) {
451
+ const next = event.relatedTarget;
452
+ if (next instanceof Node) {
453
+ const trigger = floating.referenceRef.value;
454
+ const content = floating.floatingRef.value;
455
+ if (trigger?.contains(next) || content?.contains(next)) return;
456
+ }
457
+ close();
458
+ }
459
+ function onContentPointerDown(event) {
460
+ event.preventDefault();
461
+ }
462
+ function onContentPointerLeave() {
463
+ highlightedValue.value = null;
464
+ }
465
+ function onItemPointerMove(itemValue) {
466
+ const option = optionList.value.find((entry) => entry.value === itemValue);
467
+ if (!option || option.disabled) return;
468
+ highlightedValue.value = itemValue;
469
+ }
470
+ function onItemClick(itemValue) {
471
+ const option = optionList.value.find((entry) => entry.value === itemValue);
472
+ if (!option || option.disabled) return;
473
+ setValue(itemValue);
474
+ close();
475
+ floating.referenceRef.value?.focus();
476
+ }
477
+ function getTriggerProps() {
478
+ const labelledby = (0, import_vue6.toValue)(options.ariaLabelledby);
479
+ const describedby = (0, import_vue6.toValue)(options.ariaDescribedby);
480
+ return {
481
+ id: triggerId,
482
+ role: "combobox",
483
+ "aria-expanded": isOpen.value,
484
+ "aria-controls": contentId,
485
+ "aria-haspopup": "listbox",
486
+ // 列表被 portal 到 body,不是触发元素的 DOM 后代。aria-activedescendant 要求
487
+ // 被指向的元素在自身子树内或经 aria-owns 建立归属,因此打开时显式声明所有权,
488
+ // 否则读屏会拒绝播报高亮项。关闭时不声明:那会把一个 hidden 的子树挂进来。
489
+ "aria-owns": isOpen.value ? contentId : void 0,
490
+ "aria-activedescendant": activeDescendantId.value,
491
+ "aria-required": isRequired.value || void 0,
492
+ "aria-disabled": isDisabled.value || void 0,
493
+ "aria-labelledby": labelledby || void 0,
494
+ "aria-describedby": describedby || void 0,
495
+ "data-state": isOpen.value ? "open" : "closed",
496
+ "data-disabled": isDisabled.value ? "" : void 0,
497
+ // 占位态交给样式层决定文字颜色,逻辑层不输出 class。
498
+ "data-placeholder": hasValue.value ? void 0 : "",
499
+ disabled: isDisabled.value || void 0
500
+ };
501
+ }
502
+ function getContentProps() {
503
+ return {
504
+ id: contentId,
505
+ role: "listbox",
506
+ "aria-labelledby": (0, import_vue6.toValue)(options.ariaLabelledby) || triggerId,
507
+ "aria-orientation": "vertical",
508
+ "data-state": isOpen.value ? "open" : "closed"
509
+ };
510
+ }
511
+ function getViewportProps() {
512
+ return {
513
+ role: "presentation",
514
+ "data-part": "viewport",
515
+ // 视口是滚动容器,必须可聚焦才能让键盘用户用滚轮/方向键之外的方式滚动;
516
+ // 但焦点模型要求焦点留在触发元素,因此这里只给 -1,不进 Tab 序列。
517
+ tabindex: -1
518
+ };
519
+ }
520
+ function getItemProps(itemValue) {
521
+ const option = optionList.value.find((entry) => entry.value === itemValue);
522
+ const disabled = option?.disabled === true;
523
+ const selected = value.value === itemValue;
524
+ return {
525
+ id: findEntry(itemValue)?.id,
526
+ role: "option",
527
+ "aria-selected": selected,
528
+ "aria-disabled": disabled || void 0,
529
+ "data-state": selected ? "checked" : "unchecked",
530
+ "data-highlighted": highlightedValue.value === itemValue ? "" : void 0,
531
+ "data-disabled": disabled ? "" : void 0,
532
+ "data-value": itemValue
533
+ };
534
+ }
535
+ function getGroupProps(labelId) {
536
+ return { role: "group", "aria-labelledby": labelId };
537
+ }
538
+ function getLabelProps(id) {
539
+ return { id, "data-part": "label" };
540
+ }
541
+ function getSeparatorProps() {
542
+ return { role: "separator", "aria-orientation": "horizontal" };
543
+ }
544
+ function getValueProps() {
545
+ return { "data-part": "value", "data-placeholder": hasValue.value ? void 0 : "" };
546
+ }
547
+ function getIconProps() {
548
+ return { "aria-hidden": "true", "data-part": "icon" };
549
+ }
550
+ function getNativeSelectProps() {
551
+ if (!isFormControl.value) return null;
552
+ const name = (0, import_vue6.toValue)(options.name);
553
+ const form = (0, import_vue6.toValue)(options.form);
554
+ return {
555
+ name: name || void 0,
556
+ form: form || void 0,
557
+ required: isRequired.value,
558
+ disabled: isDisabled.value,
559
+ tabindex: -1,
560
+ "aria-hidden": "true",
561
+ style: (0, import_form.getHiddenSelectStyle)(triggerSize.value)
562
+ };
563
+ }
564
+ function setTriggerElement(node) {
565
+ floating.referenceRef.value = node instanceof HTMLElement ? node : null;
566
+ }
567
+ function setContentElement(node) {
568
+ floating.floatingRef.value = node instanceof HTMLElement ? node : null;
569
+ }
570
+ function setViewportElement(node) {
571
+ viewportElement.value = node instanceof HTMLElement ? node : null;
572
+ }
573
+ function setNativeSelectElement(node) {
574
+ nativeSelectElement.value = node instanceof HTMLSelectElement ? node : null;
575
+ }
576
+ function measureTrigger() {
577
+ const trigger = floating.referenceRef.value;
578
+ if (!trigger || !isBrowser) {
579
+ triggerSize.value = null;
580
+ return;
581
+ }
582
+ const rect = trigger.getBoundingClientRect();
583
+ const current = triggerSize.value;
584
+ if (!current || current.width !== rect.width || current.height !== rect.height) {
585
+ triggerSize.value = { width: rect.width, height: rect.height };
586
+ }
587
+ }
588
+ const contentStyle = (0, import_vue6.computed)(() => {
589
+ const available = floating.availableSpace.value;
590
+ return {
591
+ ...floating.floatingStyle.value,
592
+ ...triggerSize.value ? { "--y-select-trigger-width": `${triggerSize.value.width}px` } : null,
593
+ ...available ? { "--y-select-content-available-height": `${available.height}px` } : null
594
+ };
595
+ });
596
+ function scrollHighlightedIntoView() {
597
+ const viewport = viewportElement.value;
598
+ const current = highlightedValue.value;
599
+ if (!viewport || current === null) return;
600
+ const item = findEntry(current)?.element.value;
601
+ if (!item) return;
602
+ const itemRect = item.getBoundingClientRect();
603
+ const viewportRect = viewport.getBoundingClientRect();
604
+ viewport.scrollTop = (0, import_select.resolveScrollTop)({
605
+ scrollTop: viewport.scrollTop,
606
+ itemOffset: itemRect.top - viewportRect.top,
607
+ itemSize: itemRect.height,
608
+ viewportSize: viewportRect.height
609
+ });
610
+ }
611
+ function handleDocumentPointerDown(event) {
612
+ const target = event.target;
613
+ if (!(target instanceof Node)) return;
614
+ const trigger = floating.referenceRef.value;
615
+ const content = floating.floatingRef.value;
616
+ if (trigger?.contains(target) || content?.contains(target)) return;
617
+ close();
618
+ }
619
+ function attachDismiss() {
620
+ document.addEventListener("pointerdown", handleDocumentPointerDown, true);
621
+ }
622
+ function detachDismiss() {
623
+ document.removeEventListener("pointerdown", handleDocumentPointerDown, true);
624
+ }
625
+ (0, import_vue6.watch)(
626
+ isOpen,
627
+ (open2) => {
628
+ if (!isBrowser) return;
629
+ detachDismiss();
630
+ if (open2) attachDismiss();
631
+ },
632
+ { immediate: true }
633
+ );
634
+ (0, import_vue6.watch)(
635
+ () => floating.referenceRef.value,
636
+ (trigger) => {
637
+ resize.disconnect();
638
+ if (!isBrowser) return;
639
+ measureTrigger();
640
+ resize.observe(trigger);
641
+ },
642
+ { flush: "post" }
643
+ );
644
+ (0, import_vue6.watch)(highlightedValue, () => scrollHighlightedIntoView(), { flush: "post" });
645
+ (0, import_vue6.watch)(optionList, (list) => {
646
+ const current = highlightedValue.value;
647
+ if (current === null) {
648
+ if (isOpen.value && list.length > 0) {
649
+ highlightedValue.value = (0, import_select.resolveOpenHighlight)({
650
+ options: list,
651
+ selectedValue: value.value,
652
+ edge: "first"
653
+ });
654
+ }
655
+ return;
656
+ }
657
+ const option = list.find((entry) => entry.value === current);
658
+ if (!option || option.disabled) highlightedValue.value = null;
659
+ });
660
+ const initialValue = value.value;
661
+ useFormResetListener(
662
+ floating.referenceRef,
663
+ () => setValue(initialValue),
664
+ () => (0, import_vue6.toValue)(options.form)
665
+ );
666
+ function syncNativeSelect(notify) {
667
+ const select = nativeSelectElement.value;
668
+ if (!select) return;
669
+ (0, import_form.writeNativeSelectValue)(select, value.value);
670
+ if (notify) select.dispatchEvent(new Event("change", { bubbles: true }));
671
+ }
672
+ (0, import_vue6.watch)(nativeSelectElement, (select) => {
673
+ if (!select) return;
674
+ syncNativeSelect(false);
675
+ });
676
+ (0, import_vue6.watch)(value, () => syncNativeSelect(true));
677
+ (0, import_vue6.watch)(optionList, () => syncNativeSelect(false));
678
+ (0, import_vue6.onScopeDispose)(() => {
679
+ detachDismiss();
680
+ resize.disconnect();
681
+ });
682
+ return {
683
+ value,
684
+ isOpen,
685
+ isDisabled,
686
+ highlightedValue: (0, import_vue6.computed)(() => highlightedValue.value),
687
+ selectedText,
688
+ hasValue,
689
+ options: optionList,
690
+ nativeOptions: cachedOptions,
691
+ triggerId,
692
+ contentId,
693
+ floating,
694
+ contentStyle,
695
+ setValue,
696
+ open,
697
+ close,
698
+ toggle,
699
+ getItemId,
700
+ registerItem: collection.add,
701
+ getTriggerProps,
702
+ getContentProps,
703
+ getViewportProps,
704
+ getItemProps,
705
+ getGroupProps,
706
+ getLabelProps,
707
+ getSeparatorProps,
708
+ getValueProps,
709
+ getIconProps,
710
+ isFormControl,
711
+ getNativeSelectProps,
712
+ setTriggerElement,
713
+ setContentElement,
714
+ setViewportElement,
715
+ setNativeSelectElement,
716
+ onTriggerClick,
717
+ onTriggerKeydown,
718
+ onTriggerBlur,
719
+ onContentPointerDown,
720
+ onContentPointerLeave,
721
+ onItemPointerMove,
722
+ onItemClick
723
+ };
724
+ }
725
+ // Annotate the CommonJS export names for ESM import in node:
726
+ 0 && (module.exports = {
727
+ useSelect
728
+ });
729
+ //# sourceMappingURL=index.cjs.map