@agile-plus/headless 0.1.1 → 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,601 @@
1
+ // src/useDateRangePicker/useDateRangePicker.ts
2
+ import { computed as computed3, onScopeDispose as onScopeDispose3, ref as ref2, toValue as toValue3, useId, watch as watch2 } from "vue";
3
+ import {
4
+ addMonths,
5
+ emptyRangeDraft,
6
+ formatDate,
7
+ getCalendarGrid,
8
+ getRangeHighlight,
9
+ getWeekdayLabels,
10
+ isSameDay,
11
+ isSameMonth,
12
+ parseDate,
13
+ resolveDateMove,
14
+ resolveDatePickerKeyIntent,
15
+ resolveRangePick,
16
+ startOfDay
17
+ } from "@agile-plus/primitives/date";
18
+
19
+ // src/utils/useControllable.ts
20
+ import { computed, shallowRef, toValue } from "vue";
21
+ function useControllable(options) {
22
+ const controlled = computed(() => toValue(options.value));
23
+ const isControlled = computed(() => controlled.value !== void 0);
24
+ const internal = shallowRef(options.defaultValue);
25
+ const value = computed(() => {
26
+ const current = controlled.value;
27
+ return current === void 0 ? internal.value : current;
28
+ });
29
+ function set(next) {
30
+ if (!isControlled.value) internal.value = next;
31
+ options.onChange?.(next);
32
+ }
33
+ function replace(next) {
34
+ if (isControlled.value) return;
35
+ internal.value = next;
36
+ }
37
+ return { value, isControlled, set, replace };
38
+ }
39
+
40
+ // src/utils/env.ts
41
+ var isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
42
+ function isObserverAvailable(constructor) {
43
+ return typeof constructor === "function";
44
+ }
45
+
46
+ // src/useFloating/useFloating.ts
47
+ import { computed as computed2, nextTick, onScopeDispose as onScopeDispose2, ref, toValue as toValue2, watch } from "vue";
48
+ import { computeAvailableSpace, computeFloatingPosition } from "@agile-plus/primitives/floating";
49
+
50
+ // src/utils/useResizeObserver.ts
51
+ import { onScopeDispose } from "vue";
52
+ function useResizeObserver(onResize) {
53
+ let observer = null;
54
+ function observe(element) {
55
+ if (!element || !isBrowser || !isObserverAvailable(globalThis.ResizeObserver)) return;
56
+ observer ??= new ResizeObserver(() => onResize());
57
+ observer.observe(element);
58
+ }
59
+ function unobserve(element) {
60
+ observer?.unobserve(element);
61
+ }
62
+ function disconnect() {
63
+ observer?.disconnect();
64
+ observer = null;
65
+ }
66
+ onScopeDispose(disconnect);
67
+ return { observe, unobserve, disconnect };
68
+ }
69
+
70
+ // src/useFloating/useFloating.ts
71
+ function useFloating(options = {}) {
72
+ const referenceRef = ref(null);
73
+ const floatingRef = ref(null);
74
+ const x = ref(0);
75
+ const y = ref(0);
76
+ const placement = ref(toValue2(options.placement) ?? "bottom");
77
+ const arrow = ref(null);
78
+ const availableSpace = ref(null);
79
+ const isOpen = computed2(() => toValue2(options.open) ?? true);
80
+ const strategy = computed2(() => options.strategy ?? "absolute");
81
+ let frame = null;
82
+ const resize = useResizeObserver(requestUpdate);
83
+ function requestUpdate() {
84
+ if (!isBrowser || frame !== null) return;
85
+ frame = window.requestAnimationFrame(() => {
86
+ frame = null;
87
+ measure();
88
+ });
89
+ }
90
+ function measure() {
91
+ const reference = referenceRef.value;
92
+ const floating = floatingRef.value;
93
+ if (!reference || !floating) return;
94
+ const referenceRect = reference.getBoundingClientRect();
95
+ const floatingRect = floating.getBoundingClientRect();
96
+ const boundaryRect = toValue2(options.boundary)?.getBoundingClientRect();
97
+ const viewport = { width: window.innerWidth, height: window.innerHeight };
98
+ const scrollX = window.scrollX;
99
+ const scrollY = window.scrollY;
100
+ const snapshot = {
101
+ reference: {
102
+ x: referenceRect.x,
103
+ y: referenceRect.y,
104
+ width: referenceRect.width,
105
+ height: referenceRect.height
106
+ },
107
+ // 浮层自己的 x/y 是上一轮结果,传 0 以免本轮输出依赖历史状态。
108
+ floating: { x: 0, y: 0, width: floatingRect.width, height: floatingRect.height },
109
+ viewport,
110
+ ...boundaryRect ? {
111
+ boundary: {
112
+ x: boundaryRect.x,
113
+ y: boundaryRect.y,
114
+ width: boundaryRect.width,
115
+ height: boundaryRect.height
116
+ }
117
+ } : {}
118
+ };
119
+ const floatingOptions = {
120
+ placement: toValue2(options.placement),
121
+ offset: options.offset,
122
+ flip: options.flip,
123
+ shift: options.shift,
124
+ padding: options.padding,
125
+ arrow: options.arrow
126
+ };
127
+ const result = computeFloatingPosition(snapshot, floatingOptions);
128
+ x.value = strategy.value === "absolute" ? result.x + scrollX : result.x;
129
+ y.value = strategy.value === "absolute" ? result.y + scrollY : result.y;
130
+ placement.value = result.placement;
131
+ arrow.value = result.arrow;
132
+ availableSpace.value = computeAvailableSpace(snapshot, result.placement, floatingOptions);
133
+ }
134
+ const floatingStyle = computed2(() => ({
135
+ position: strategy.value,
136
+ left: 0,
137
+ top: 0,
138
+ transform: `translate(${x.value}px, ${y.value}px)`
139
+ }));
140
+ const arrowStyle = computed2(() => {
141
+ const offset = arrow.value;
142
+ if (!offset) return {};
143
+ return offset.x !== null ? { position: "absolute", left: `${offset.x}px` } : { position: "absolute", top: `${offset.y}px` };
144
+ });
145
+ function attach() {
146
+ window.addEventListener("scroll", requestUpdate, true);
147
+ window.addEventListener("resize", requestUpdate);
148
+ resize.observe(referenceRef.value);
149
+ resize.observe(floatingRef.value);
150
+ }
151
+ function detach() {
152
+ if (!isBrowser) return;
153
+ window.removeEventListener("scroll", requestUpdate, true);
154
+ window.removeEventListener("resize", requestUpdate);
155
+ resize.disconnect();
156
+ }
157
+ watch(
158
+ [isOpen, referenceRef, floatingRef],
159
+ ([open]) => {
160
+ detach();
161
+ if (!isBrowser || !open) return;
162
+ attach();
163
+ void nextTick(measure);
164
+ },
165
+ { immediate: true }
166
+ );
167
+ onScopeDispose2(() => {
168
+ detach();
169
+ if (frame !== null && isBrowser) {
170
+ window.cancelAnimationFrame(frame);
171
+ frame = null;
172
+ }
173
+ });
174
+ return {
175
+ referenceRef,
176
+ floatingRef,
177
+ x: computed2(() => x.value),
178
+ y: computed2(() => y.value),
179
+ placement: computed2(() => placement.value),
180
+ availableSpace: computed2(() => availableSpace.value),
181
+ floatingStyle,
182
+ arrowStyle,
183
+ update: requestUpdate
184
+ };
185
+ }
186
+
187
+ // src/useDateRangePicker/useDateRangePicker.ts
188
+ var DEFAULT_OFFSET = 4;
189
+ var DEFAULT_PADDING = 8;
190
+ var DEFAULT_VALUE_FORMAT = "YYYY-MM-DD";
191
+ var DEFAULT_FIRST_DAY_OF_WEEK = 1;
192
+ var DAYS_PER_WEEK = 7;
193
+ function useDateRangePicker(options = {}) {
194
+ const uid = useId();
195
+ const triggerIds = [`date-range-trigger-${uid}-0`, `date-range-trigger-${uid}-1`];
196
+ const contentId = `date-range-content-${uid}`;
197
+ const valueFormat = options.valueFormat ?? DEFAULT_VALUE_FORMAT;
198
+ const format = options.format ?? DEFAULT_VALUE_FORMAT;
199
+ const valueState = useControllable({
200
+ value: () => toValue3(options.modelValue),
201
+ defaultValue: options.defaultValue ?? null,
202
+ onChange: (next) => options.onValueChange?.(next)
203
+ });
204
+ const openState = useControllable({
205
+ value: () => toValue3(options.open),
206
+ defaultValue: options.defaultOpen ?? false,
207
+ onChange: (next) => options.onOpenChange?.(next)
208
+ });
209
+ const value = valueState.value;
210
+ const isOpen = openState.value;
211
+ const isDisabled = computed3(() => toValue3(options.disabled) === true);
212
+ const firstDayOfWeek = computed3(() => {
213
+ const raw = toValue3(options.firstDayOfWeek) ?? DEFAULT_FIRST_DAY_OF_WEEK;
214
+ return (raw % DAYS_PER_WEEK + DAYS_PER_WEEK) % DAYS_PER_WEEK;
215
+ });
216
+ const selectedRange = computed3(() => {
217
+ const current = value.value;
218
+ if (!current) return null;
219
+ const start = parseDate(current[0], valueFormat);
220
+ const end = parseDate(current[1], valueFormat);
221
+ return start && end ? [start, end] : null;
222
+ });
223
+ const activeIndex = ref2(0);
224
+ const draftRange = ref2(emptyRangeDraft());
225
+ const inputDrafts = ref2([null, null]);
226
+ const hoverDate = ref2(null);
227
+ let draftIsTyped = false;
228
+ const initialBase = selectedRange.value?.[0] ?? /* @__PURE__ */ new Date();
229
+ const panelDate = ref2(startOfMonth(initialBase));
230
+ const focusedDate = ref2(startOfDay(initialBase));
231
+ const triggerElements = [null, null];
232
+ let skipFocusOpen = false;
233
+ const floating = useFloating({
234
+ placement: options.placement ?? "bottom-start",
235
+ offset: options.offset ?? DEFAULT_OFFSET,
236
+ flip: options.flip,
237
+ shift: options.shift,
238
+ padding: options.padding ?? DEFAULT_PADDING,
239
+ strategy: options.strategy,
240
+ open: isOpen
241
+ });
242
+ const rightPanelDate = computed3(() => addMonths(panelDate.value, 1));
243
+ const awaitingEnd = computed3(() => draftRange.value[0] !== null && draftRange.value[1] === null);
244
+ const displayTexts = computed3(() => {
245
+ const picking = draftRange.value[0] !== null;
246
+ const committed = selectedRange.value;
247
+ function text(index) {
248
+ const draft = inputDrafts.value[index];
249
+ if (draft !== null) return draft;
250
+ if (picking) {
251
+ const pending = draftRange.value[index];
252
+ return pending ? formatDate(pending, format) : "";
253
+ }
254
+ return committed ? formatDate(committed[index], format) : "";
255
+ }
256
+ return [text(0), text(1)];
257
+ });
258
+ const leftCells = computed3(
259
+ () => getCalendarGrid({
260
+ year: panelDate.value.getFullYear(),
261
+ month: panelDate.value.getMonth(),
262
+ firstDayOfWeek: firstDayOfWeek.value
263
+ })
264
+ );
265
+ const rightCells = computed3(
266
+ () => getCalendarGrid({
267
+ year: rightPanelDate.value.getFullYear(),
268
+ month: rightPanelDate.value.getMonth(),
269
+ firstDayOfWeek: firstDayOfWeek.value
270
+ })
271
+ );
272
+ const weekdayLabels = computed3(() => getWeekdayLabels(firstDayOfWeek.value));
273
+ function startOfMonth(date) {
274
+ return new Date(date.getFullYear(), date.getMonth(), 1);
275
+ }
276
+ function isDateDisabled(date) {
277
+ if (isDisabled.value) return true;
278
+ return options.disabledDate ? options.disabledDate(startOfDay(date)) : false;
279
+ }
280
+ function getCellState(date, side) {
281
+ const picking = draftRange.value[0] !== null;
282
+ const committed = selectedRange.value;
283
+ const start = picking ? draftRange.value[0] : committed?.[0] ?? null;
284
+ const end = picking ? draftRange.value[1] : committed?.[1] ?? null;
285
+ const hover = awaitingEnd.value ? hoverDate.value : null;
286
+ const highlight = getRangeHighlight({ date, start, end, hover });
287
+ const monthAnchor = side === "left" ? panelDate.value : rightPanelDate.value;
288
+ return {
289
+ selected: highlight.isStart || highlight.isEnd,
290
+ disabled: isDateDisabled(date),
291
+ today: isSameDay(/* @__PURE__ */ new Date(), date),
292
+ inMonth: isSameMonth(monthAnchor, date),
293
+ ...highlight
294
+ };
295
+ }
296
+ function getCellId(date, side) {
297
+ return `date-range-cell-${uid}-${side}-${formatDate(date, DEFAULT_VALUE_FORMAT)}`;
298
+ }
299
+ function open(initialActive = 0) {
300
+ if (isDisabled.value) return;
301
+ if (!draftIsTyped) draftRange.value = emptyRangeDraft();
302
+ inputDrafts.value = [null, null];
303
+ hoverDate.value = null;
304
+ const base = draftRange.value[0] ?? selectedRange.value?.[0] ?? /* @__PURE__ */ new Date();
305
+ panelDate.value = startOfMonth(base);
306
+ focusedDate.value = startOfDay(base);
307
+ activeIndex.value = initialActive;
308
+ openState.set(true);
309
+ }
310
+ function cleanupDrafts() {
311
+ if (!draftIsTyped) draftRange.value = emptyRangeDraft();
312
+ inputDrafts.value = [null, null];
313
+ hoverDate.value = null;
314
+ }
315
+ function close() {
316
+ const content = floating.floatingRef.value;
317
+ const active = isBrowser ? document.activeElement : null;
318
+ cleanupDrafts();
319
+ openState.set(false);
320
+ if (content && active instanceof Node && content.contains(active)) {
321
+ skipFocusOpen = true;
322
+ triggerElements[activeIndex.value]?.focus();
323
+ }
324
+ }
325
+ function selectRange(start, end) {
326
+ if (isDisabled.value) return;
327
+ const [orderedStart, orderedEnd] = start.getTime() <= end.getTime() ? [start, end] : [end, start];
328
+ if (isDateDisabled(orderedStart) || isDateDisabled(orderedEnd)) return;
329
+ draftRange.value = emptyRangeDraft();
330
+ draftIsTyped = false;
331
+ valueState.set([formatDate(orderedStart, valueFormat), formatDate(orderedEnd, valueFormat)]);
332
+ close();
333
+ }
334
+ function selectRangeByValue(start, end) {
335
+ const parsedStart = parseDate(start, valueFormat);
336
+ const parsedEnd = parseDate(end, valueFormat);
337
+ if (parsedStart && parsedEnd) selectRange(parsedStart, parsedEnd);
338
+ }
339
+ function pickDate(date) {
340
+ if (isDateDisabled(date)) return;
341
+ const result = resolveRangePick({ draft: draftRange.value, active: activeIndex.value, date });
342
+ if (result.done && result.draft[0] && result.draft[1]) {
343
+ selectRange(result.draft[0], result.draft[1]);
344
+ return;
345
+ }
346
+ draftRange.value = result.draft;
347
+ draftIsTyped = false;
348
+ hoverDate.value = null;
349
+ setActiveIndex(result.active);
350
+ }
351
+ function clear() {
352
+ draftRange.value = emptyRangeDraft();
353
+ draftIsTyped = false;
354
+ inputDrafts.value = [null, null];
355
+ hoverDate.value = null;
356
+ valueState.set(null);
357
+ }
358
+ function setActiveIndex(index) {
359
+ activeIndex.value = index;
360
+ triggerElements[index]?.focus();
361
+ }
362
+ function shiftView(unit, delta) {
363
+ const months = unit === "month" ? 1 : 12;
364
+ panelDate.value = addMonths(panelDate.value, delta * months);
365
+ focusedDate.value = addMonths(focusedDate.value, delta * months);
366
+ }
367
+ function moveFocus(move) {
368
+ const next = resolveDateMove(focusedDate.value, move);
369
+ focusedDate.value = next;
370
+ panelDate.value = startOfMonth(next);
371
+ }
372
+ function commitInput(index) {
373
+ const text = inputDrafts.value[index];
374
+ if (text === null) return;
375
+ inputDrafts.value = index === 0 ? [null, inputDrafts.value[1]] : [inputDrafts.value[0], null];
376
+ const trimmed = text.trim();
377
+ if (trimmed === "") {
378
+ clear();
379
+ return;
380
+ }
381
+ const parsed = parseDate(trimmed, format);
382
+ if (!parsed || isDateDisabled(parsed)) return;
383
+ const other = draftRange.value[1 - index] ?? selectedRange.value?.[1 - index] ?? null;
384
+ if (other) {
385
+ selectRange(parsed, other);
386
+ return;
387
+ }
388
+ draftRange.value = index === 0 ? [parsed, null] : [null, parsed];
389
+ draftIsTyped = true;
390
+ }
391
+ function onInputInput(index, event) {
392
+ if (isDisabled.value) return;
393
+ const target = event.target;
394
+ if (!(target instanceof HTMLInputElement)) return;
395
+ inputDrafts.value = index === 0 ? [target.value, inputDrafts.value[1]] : [inputDrafts.value[0], target.value];
396
+ }
397
+ function onInputBlur(index) {
398
+ commitInput(index);
399
+ }
400
+ function onInputFocus(index) {
401
+ if (skipFocusOpen) {
402
+ skipFocusOpen = false;
403
+ return;
404
+ }
405
+ activeIndex.value = index;
406
+ if (!isDisabled.value && !isOpen.value) open(index);
407
+ }
408
+ function onInputClick(index) {
409
+ if (!isDisabled.value && !isOpen.value) open(index);
410
+ }
411
+ function onInputKeydown(index, event) {
412
+ if (isDisabled.value) return;
413
+ if (event.key === "Tab" && isOpen.value) {
414
+ if (index === 0 && !event.shiftKey) {
415
+ event.preventDefault();
416
+ setActiveIndex(1);
417
+ return;
418
+ }
419
+ if (index === 1 && event.shiftKey) {
420
+ event.preventDefault();
421
+ setActiveIndex(0);
422
+ return;
423
+ }
424
+ close();
425
+ return;
426
+ }
427
+ if (!isOpen.value && event.key === "Enter" && inputDrafts.value[index] !== null) {
428
+ event.preventDefault();
429
+ commitInput(index);
430
+ return;
431
+ }
432
+ const intent = resolveDatePickerKeyIntent({
433
+ key: event.key,
434
+ open: isOpen.value,
435
+ panel: "date",
436
+ focusedDate: focusedDate.value,
437
+ firstDayOfWeek: firstDayOfWeek.value,
438
+ hasModifierKey: event.ctrlKey || event.altKey || event.metaKey
439
+ });
440
+ if (intent.kind === "none") return;
441
+ if (intent.kind === "tab-out") {
442
+ close();
443
+ return;
444
+ }
445
+ event.preventDefault();
446
+ if (intent.kind === "open") open(index);
447
+ else if (intent.kind === "close") {
448
+ draftIsTyped = false;
449
+ close();
450
+ } else if (intent.kind === "move") moveFocus(intent.move);
451
+ else if (intent.kind === "commit") pickDate(focusedDate.value);
452
+ }
453
+ function onCellPointerDown(event) {
454
+ event.preventDefault();
455
+ }
456
+ function onCellPointerEnter(date) {
457
+ if (awaitingEnd.value) hoverDate.value = date;
458
+ }
459
+ function onCellPointerLeave() {
460
+ hoverDate.value = null;
461
+ }
462
+ function getTriggerProps(index) {
463
+ const labelledby = toValue3(options.ariaLabelledby);
464
+ const describedby = toValue3(options.ariaDescribedby);
465
+ return {
466
+ id: triggerIds[index],
467
+ role: "combobox",
468
+ "aria-haspopup": "dialog",
469
+ "aria-expanded": isOpen.value,
470
+ "aria-controls": contentId,
471
+ "aria-owns": isOpen.value ? contentId : void 0,
472
+ // 焦点格只在活跃输入框上声明,两个 combobox 同时指向一个格子会让读屏重复播报。
473
+ "aria-activedescendant": isOpen.value && activeIndex.value === index ? getCellId(focusedDate.value, "left") : void 0,
474
+ "aria-autocomplete": "none",
475
+ "aria-disabled": isDisabled.value || void 0,
476
+ "aria-labelledby": labelledby || void 0,
477
+ "aria-describedby": describedby || void 0,
478
+ "data-state": isOpen.value ? "open" : "closed",
479
+ "data-disabled": isDisabled.value ? "" : void 0,
480
+ "data-active": isOpen.value && activeIndex.value === index ? "" : void 0,
481
+ disabled: isDisabled.value || void 0,
482
+ autocomplete: "off"
483
+ };
484
+ }
485
+ function getContentProps() {
486
+ return {
487
+ id: contentId,
488
+ role: "dialog",
489
+ "aria-labelledby": toValue3(options.ariaLabelledby) || triggerIds[0],
490
+ tabindex: -1,
491
+ "data-state": isOpen.value ? "open" : "closed"
492
+ };
493
+ }
494
+ function getGridProps(side) {
495
+ const anchor = side === "left" ? panelDate.value : rightPanelDate.value;
496
+ return { role: "grid", "aria-label": formatDate(anchor, "YYYY\u5E74 M\u6708") };
497
+ }
498
+ function getCellProps(date, side) {
499
+ const state = getCellState(date, side);
500
+ return {
501
+ id: getCellId(date, side),
502
+ role: "gridcell",
503
+ "aria-selected": state.selected,
504
+ "aria-disabled": state.disabled || void 0,
505
+ "aria-label": formatDate(date, "YYYY\u5E74M\u6708D\u65E5"),
506
+ "data-focused": isSameDay(focusedDate.value, date) ? "" : void 0,
507
+ "data-selected": state.selected ? "" : void 0,
508
+ "data-range-start": state.isStart ? "" : void 0,
509
+ "data-range-end": state.isEnd ? "" : void 0,
510
+ "data-in-range": state.inRange ? "" : void 0,
511
+ "data-preview": state.inPreview ? "" : void 0,
512
+ "data-hover-end": state.isHoverEnd ? "" : void 0,
513
+ "data-disabled": state.disabled ? "" : void 0,
514
+ "data-today": state.today ? "" : void 0,
515
+ "data-outside": state.inMonth ? void 0 : "",
516
+ "data-value": formatDate(date, DEFAULT_VALUE_FORMAT)
517
+ };
518
+ }
519
+ function setTriggerElement(node, index) {
520
+ triggerElements[index] = node instanceof HTMLElement ? node : null;
521
+ }
522
+ function setControlElement(node) {
523
+ floating.referenceRef.value = node instanceof HTMLElement ? node : null;
524
+ }
525
+ function setContentElement(node) {
526
+ floating.floatingRef.value = node instanceof HTMLElement ? node : null;
527
+ }
528
+ function handleDocumentPointerDown(event) {
529
+ const target = event.target;
530
+ if (!(target instanceof Node)) return;
531
+ const control = floating.referenceRef.value;
532
+ const content = floating.floatingRef.value;
533
+ if (control?.contains(target) || content?.contains(target)) return;
534
+ commitInput(activeIndex.value);
535
+ close();
536
+ }
537
+ watch2(
538
+ isOpen,
539
+ (open2) => {
540
+ if (!isBrowser) return;
541
+ document.removeEventListener("pointerdown", handleDocumentPointerDown, true);
542
+ if (open2) {
543
+ document.addEventListener("pointerdown", handleDocumentPointerDown, true);
544
+ } else {
545
+ cleanupDrafts();
546
+ }
547
+ },
548
+ { immediate: true }
549
+ );
550
+ const contentStyle = computed3(() => ({ ...floating.floatingStyle.value }));
551
+ onScopeDispose3(() => {
552
+ if (isBrowser) document.removeEventListener("pointerdown", handleDocumentPointerDown, true);
553
+ });
554
+ return {
555
+ value,
556
+ setValue: valueState.set,
557
+ selectedRange,
558
+ isOpen,
559
+ open,
560
+ close,
561
+ activeIndex: computed3(() => activeIndex.value),
562
+ setActiveIndex,
563
+ panelDate: computed3(() => panelDate.value),
564
+ rightPanelDate,
565
+ leftCells,
566
+ rightCells,
567
+ weekdayLabels,
568
+ focusedDate: computed3(() => focusedDate.value),
569
+ displayTexts,
570
+ isDateDisabled,
571
+ getCellState,
572
+ pickDate,
573
+ selectRange,
574
+ selectRangeByValue,
575
+ clear,
576
+ shiftView,
577
+ moveFocus,
578
+ onInputInput,
579
+ onInputKeydown,
580
+ onInputBlur,
581
+ onInputFocus,
582
+ onInputClick,
583
+ onCellPointerDown,
584
+ onCellPointerEnter,
585
+ onCellPointerLeave,
586
+ getTriggerProps,
587
+ getContentProps,
588
+ getGridProps,
589
+ getCellProps,
590
+ getCellId,
591
+ setTriggerElement,
592
+ setControlElement,
593
+ setContentElement,
594
+ floating,
595
+ contentStyle
596
+ };
597
+ }
598
+ export {
599
+ useDateRangePicker
600
+ };
601
+ //# sourceMappingURL=index.js.map