@agile-plus/headless 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +59 -2
- package/dist/index.cjs +2610 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +2631 -0
- package/dist/index.js.map +1 -0
- package/dist/types-2ROaIfZ9.d.cts +57 -0
- package/dist/types-2ROaIfZ9.d.ts +57 -0
- package/dist/types-C9nl8qeg.d.cts +100 -0
- package/dist/types-C9nl8qeg.d.ts +100 -0
- package/dist/types-DM4s_xZa.d.cts +128 -0
- package/dist/types-DM4s_xZa.d.ts +128 -0
- package/dist/useCheckbox/index.cjs +255 -0
- package/dist/useCheckbox/index.cjs.map +1 -0
- package/dist/useCheckbox/index.d.cts +145 -0
- package/dist/useCheckbox/index.d.ts +145 -0
- package/dist/useCheckbox/index.js +232 -0
- package/dist/useCheckbox/index.js.map +1 -0
- package/dist/useCheckboxGroup/index.cjs +100 -0
- package/dist/useCheckboxGroup/index.cjs.map +1 -0
- package/dist/useCheckboxGroup/index.d.cts +102 -0
- package/dist/useCheckboxGroup/index.d.ts +102 -0
- package/dist/useCheckboxGroup/index.js +73 -0
- package/dist/useCheckboxGroup/index.js.map +1 -0
- package/dist/useCollapsible/index.cjs +151 -0
- package/dist/useCollapsible/index.cjs.map +1 -0
- package/dist/useCollapsible/index.d.cts +106 -0
- package/dist/useCollapsible/index.d.ts +106 -0
- package/dist/useCollapsible/index.js +129 -0
- package/dist/useCollapsible/index.js.map +1 -0
- package/dist/useDialog/index.cjs +294 -0
- package/dist/useDialog/index.cjs.map +1 -0
- package/dist/useDialog/index.d.cts +105 -0
- package/dist/useDialog/index.d.ts +105 -0
- package/dist/useDialog/index.js +267 -0
- package/dist/useDialog/index.js.map +1 -0
- package/dist/useForm/index.cjs +123 -0
- package/dist/useForm/index.cjs.map +1 -0
- package/dist/useForm/index.d.cts +22 -0
- package/dist/useForm/index.d.ts +22 -0
- package/dist/useForm/index.js +96 -0
- package/dist/useForm/index.js.map +1 -0
- package/dist/useFormControl/index.cjs +162 -0
- package/dist/useFormControl/index.cjs.map +1 -0
- package/dist/useFormControl/index.d.cts +116 -0
- package/dist/useFormControl/index.d.ts +116 -0
- package/dist/useFormControl/index.js +143 -0
- package/dist/useFormControl/index.js.map +1 -0
- package/dist/useFormField/index.cjs +45 -0
- package/dist/useFormField/index.cjs.map +1 -0
- package/dist/useFormField/index.d.cts +22 -0
- package/dist/useFormField/index.d.ts +22 -0
- package/dist/useFormField/index.js +18 -0
- package/dist/useFormField/index.js.map +1 -0
- package/dist/useFormMessage/index.cjs +77 -0
- package/dist/useFormMessage/index.cjs.map +1 -0
- package/dist/useFormMessage/index.d.cts +80 -0
- package/dist/useFormMessage/index.d.ts +80 -0
- package/dist/useFormMessage/index.js +54 -0
- package/dist/useFormMessage/index.js.map +1 -0
- package/dist/usePresence/index.cjs +117 -0
- package/dist/usePresence/index.cjs.map +1 -0
- package/dist/usePresence/index.d.cts +52 -0
- package/dist/usePresence/index.d.ts +52 -0
- package/dist/usePresence/index.js +90 -0
- package/dist/usePresence/index.js.map +1 -0
- package/dist/useRadioGroup/index.cjs +197 -0
- package/dist/useRadioGroup/index.cjs.map +1 -0
- package/dist/useRadioGroup/index.d.cts +162 -0
- package/dist/useRadioGroup/index.d.ts +162 -0
- package/dist/useRadioGroup/index.js +170 -0
- package/dist/useRadioGroup/index.js.map +1 -0
- package/dist/useScrollArea/index.cjs +309 -0
- package/dist/useScrollArea/index.cjs.map +1 -0
- package/dist/useScrollArea/index.d.cts +127 -0
- package/dist/useScrollArea/index.d.ts +127 -0
- package/dist/useScrollArea/index.js +287 -0
- package/dist/useScrollArea/index.js.map +1 -0
- package/dist/useSlider/index.cjs +417 -0
- package/dist/useSlider/index.cjs.map +1 -0
- package/dist/useSlider/index.d.cts +215 -0
- package/dist/useSlider/index.d.ts +215 -0
- package/dist/useSlider/index.js +401 -0
- package/dist/useSlider/index.js.map +1 -0
- package/dist/useSwitch/index.cjs +264 -0
- package/dist/useSwitch/index.cjs.map +1 -0
- package/dist/useSwitch/index.d.cts +110 -0
- package/dist/useSwitch/index.d.ts +110 -0
- package/dist/useSwitch/index.js +237 -0
- package/dist/useSwitch/index.js.map +1 -0
- package/dist/useTabs/index.cjs +153 -0
- package/dist/useTabs/index.cjs.map +1 -0
- package/dist/useTabs/index.d.cts +84 -0
- package/dist/useTabs/index.d.ts +84 -0
- package/dist/useTabs/index.js +126 -0
- package/dist/useTabs/index.js.map +1 -0
- package/dist/useToast/index.cjs +375 -0
- package/dist/useToast/index.cjs.map +1 -0
- package/dist/useToast/index.d.cts +148 -0
- package/dist/useToast/index.d.ts +148 -0
- package/dist/useToast/index.js +358 -0
- package/dist/useToast/index.js.map +1 -0
- package/dist/useToastProvider/index.cjs +118 -0
- package/dist/useToastProvider/index.cjs.map +1 -0
- package/dist/useToastProvider/index.d.cts +25 -0
- package/dist/useToastProvider/index.d.ts +25 -0
- package/dist/useToastProvider/index.js +96 -0
- package/dist/useToastProvider/index.js.map +1 -0
- package/dist/useToastViewport/index.cjs +227 -0
- package/dist/useToastViewport/index.cjs.map +1 -0
- package/dist/useToastViewport/index.d.cts +130 -0
- package/dist/useToastViewport/index.d.ts +130 -0
- package/dist/useToastViewport/index.js +207 -0
- package/dist/useToastViewport/index.js.map +1 -0
- package/dist/useToggle/index.cjs +86 -0
- package/dist/useToggle/index.cjs.map +1 -0
- package/dist/useToggle/index.d.cts +66 -0
- package/dist/useToggle/index.d.ts +66 -0
- package/dist/useToggle/index.js +59 -0
- package/dist/useToggle/index.js.map +1 -0
- package/dist/useToggleGroup/index.cjs +227 -0
- package/dist/useToggleGroup/index.cjs.map +1 -0
- package/dist/useToggleGroup/index.d.cts +159 -0
- package/dist/useToggleGroup/index.d.ts +159 -0
- package/dist/useToggleGroup/index.js +207 -0
- package/dist/useToggleGroup/index.js.map +1 -0
- package/package.json +163 -3
- package/src/index.ts +37 -0
- package/src/useCheckbox/index.ts +3 -0
- package/src/useCheckbox/types.ts +150 -0
- package/src/useCheckbox/useCheckbox.ts +141 -0
- package/src/useCheckboxGroup/index.ts +3 -0
- package/src/useCheckboxGroup/types.ts +104 -0
- package/src/useCheckboxGroup/useCheckboxGroup.ts +79 -0
- package/src/useCollapsible/index.ts +3 -0
- package/src/useCollapsible/types.ts +103 -0
- package/src/useCollapsible/useCollapsible.ts +160 -0
- package/src/useDialog/index.ts +3 -0
- package/src/useDialog/types.ts +109 -0
- package/src/useDialog/useDialog.ts +181 -0
- package/src/useForm/index.ts +9 -0
- package/src/useForm/types.ts +149 -0
- package/src/useForm/useForm.ts +156 -0
- package/src/useFormControl/index.ts +3 -0
- package/src/useFormControl/types.ts +101 -0
- package/src/useFormControl/useFormControl.ts +181 -0
- package/src/useFormField/index.ts +3 -0
- package/src/useFormField/types.ts +65 -0
- package/src/useFormField/useFormField.ts +46 -0
- package/src/useFormMessage/index.ts +3 -0
- package/src/useFormMessage/types.ts +64 -0
- package/src/useFormMessage/useFormMessage.ts +116 -0
- package/src/usePresence/index.ts +3 -0
- package/src/usePresence/types.ts +38 -0
- package/src/usePresence/usePresence.ts +168 -0
- package/src/useRadioGroup/index.ts +8 -0
- package/src/useRadioGroup/types.ts +173 -0
- package/src/useRadioGroup/useRadioGroup.ts +204 -0
- package/src/useScrollArea/index.ts +3 -0
- package/src/useScrollArea/types.ts +141 -0
- package/src/useScrollArea/useScrollArea.ts +360 -0
- package/src/useSlider/index.ts +3 -0
- package/src/useSlider/types.ts +231 -0
- package/src/useSlider/useSlider.ts +486 -0
- package/src/useSwitch/index.ts +3 -0
- package/src/useSwitch/types.ts +109 -0
- package/src/useSwitch/useSwitch.ts +144 -0
- package/src/useTabs/index.ts +3 -0
- package/src/useTabs/types.ts +83 -0
- package/src/useTabs/useTabs.ts +196 -0
- package/src/useToast/index.ts +3 -0
- package/src/useToast/types.ts +167 -0
- package/src/useToast/useToast.ts +378 -0
- package/src/useToastProvider/index.ts +3 -0
- package/src/useToastProvider/types.ts +121 -0
- package/src/useToastProvider/useToastProvider.ts +115 -0
- package/src/useToastViewport/index.ts +3 -0
- package/src/useToastViewport/types.ts +131 -0
- package/src/useToastViewport/useToastViewport.ts +319 -0
- package/src/useToggle/index.ts +3 -0
- package/src/useToggle/types.ts +59 -0
- package/src/useToggle/useToggle.ts +57 -0
- package/src/useToggleGroup/index.ts +3 -0
- package/src/useToggleGroup/types.ts +171 -0
- package/src/useToggleGroup/useToggleGroup.ts +260 -0
- package/src/utils/env.ts +26 -0
- package/src/utils/useBubbleInput.ts +168 -0
- package/src/utils/useCollection.ts +92 -0
- package/src/utils/useControllable.ts +96 -0
- package/src/utils/useFocusTrap.ts +160 -0
- package/src/utils/useFormResetListener.ts +80 -0
- package/src/utils/usePointerDrag.ts +101 -0
- package/src/utils/useResizeObserver.ts +60 -0
- package/src/utils/useScrollLock.ts +95 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,2631 @@
|
|
|
1
|
+
// src/useDialog/useDialog.ts
|
|
2
|
+
import { computed, onScopeDispose as onScopeDispose3, ref, toValue as toValue3, watch as watch3 } from "vue";
|
|
3
|
+
import { createDialogMachine, handleDialogKeydown } from "@agile-plus/primitives/dialog";
|
|
4
|
+
|
|
5
|
+
// src/utils/useFocusTrap.ts
|
|
6
|
+
import { onScopeDispose, toValue, watch } from "vue";
|
|
7
|
+
import { getTabbableCandidates } from "@agile-plus/primitives/dom";
|
|
8
|
+
var trapStack = [];
|
|
9
|
+
function focusWithin(container, target) {
|
|
10
|
+
target.focus({ preventScroll: true });
|
|
11
|
+
if (!container.contains(document.activeElement)) {
|
|
12
|
+
container.focus({ preventScroll: true });
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function useFocusTrap(options) {
|
|
16
|
+
const token = /* @__PURE__ */ Symbol("focus-trap");
|
|
17
|
+
let isActive = false;
|
|
18
|
+
function isTopmost() {
|
|
19
|
+
return trapStack[trapStack.length - 1] === token;
|
|
20
|
+
}
|
|
21
|
+
function handleKeydown(event) {
|
|
22
|
+
if (event.defaultPrevented || event.key !== "Tab") return;
|
|
23
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
24
|
+
if (!isTopmost()) return;
|
|
25
|
+
const container = options.container.value;
|
|
26
|
+
if (!container?.isConnected) return;
|
|
27
|
+
const tabbables = getTabbableCandidates(container);
|
|
28
|
+
if (tabbables.length === 0) {
|
|
29
|
+
event.preventDefault();
|
|
30
|
+
container.focus({ preventScroll: true });
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const first = tabbables[0];
|
|
34
|
+
const last = tabbables[tabbables.length - 1];
|
|
35
|
+
const current = document.activeElement;
|
|
36
|
+
if (!(current instanceof HTMLElement) || !container.contains(current)) {
|
|
37
|
+
event.preventDefault();
|
|
38
|
+
focusWithin(container, event.shiftKey ? last : first);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const atStart = current === container || current === first;
|
|
42
|
+
if (event.shiftKey && atStart) {
|
|
43
|
+
event.preventDefault();
|
|
44
|
+
focusWithin(container, last);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (!event.shiftKey && current === last) {
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
focusWithin(container, first);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function handleFocusIn(event) {
|
|
53
|
+
if (!isTopmost()) return;
|
|
54
|
+
const container = options.container.value;
|
|
55
|
+
if (!container?.isConnected) return;
|
|
56
|
+
const target = event.target;
|
|
57
|
+
if (target instanceof Node && container.contains(target)) return;
|
|
58
|
+
const tabbables = getTabbableCandidates(container);
|
|
59
|
+
focusWithin(container, tabbables[0] ?? container);
|
|
60
|
+
}
|
|
61
|
+
function activate() {
|
|
62
|
+
if (isActive) return;
|
|
63
|
+
isActive = true;
|
|
64
|
+
trapStack.push(token);
|
|
65
|
+
document.addEventListener("keydown", handleKeydown);
|
|
66
|
+
document.addEventListener("focusin", handleFocusIn, true);
|
|
67
|
+
}
|
|
68
|
+
function deactivate() {
|
|
69
|
+
if (!isActive) return;
|
|
70
|
+
isActive = false;
|
|
71
|
+
const index = trapStack.indexOf(token);
|
|
72
|
+
if (index !== -1) trapStack.splice(index, 1);
|
|
73
|
+
document.removeEventListener("keydown", handleKeydown);
|
|
74
|
+
document.removeEventListener("focusin", handleFocusIn, true);
|
|
75
|
+
}
|
|
76
|
+
watch(
|
|
77
|
+
[() => toValue(options.active), options.container],
|
|
78
|
+
([active, container]) => {
|
|
79
|
+
if (active && container) {
|
|
80
|
+
activate();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
deactivate();
|
|
84
|
+
},
|
|
85
|
+
{ immediate: true, flush: "post" }
|
|
86
|
+
);
|
|
87
|
+
onScopeDispose(deactivate);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/utils/useScrollLock.ts
|
|
91
|
+
import { onScopeDispose as onScopeDispose2, toValue as toValue2, watch as watch2 } from "vue";
|
|
92
|
+
|
|
93
|
+
// src/utils/env.ts
|
|
94
|
+
var isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
|
|
95
|
+
function isObserverAvailable(constructor) {
|
|
96
|
+
return typeof constructor === "function";
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// src/utils/useScrollLock.ts
|
|
100
|
+
var lockCount = 0;
|
|
101
|
+
var originalOverflow = "";
|
|
102
|
+
var originalPaddingRight = "";
|
|
103
|
+
function getScrollbarWidth() {
|
|
104
|
+
const documentWidth = document.documentElement.clientWidth;
|
|
105
|
+
if (documentWidth <= 0) return 0;
|
|
106
|
+
return Math.max(0, window.innerWidth - documentWidth);
|
|
107
|
+
}
|
|
108
|
+
function acquireScrollLock() {
|
|
109
|
+
const body = document.body;
|
|
110
|
+
if (lockCount === 0) {
|
|
111
|
+
originalOverflow = body.style.overflow;
|
|
112
|
+
originalPaddingRight = body.style.paddingRight;
|
|
113
|
+
const scrollbarWidth = getScrollbarWidth();
|
|
114
|
+
if (scrollbarWidth > 0) {
|
|
115
|
+
const currentPadding = Number.parseFloat(window.getComputedStyle(body).paddingRight) || 0;
|
|
116
|
+
body.style.paddingRight = `${currentPadding + scrollbarWidth}px`;
|
|
117
|
+
}
|
|
118
|
+
body.style.overflow = "hidden";
|
|
119
|
+
}
|
|
120
|
+
lockCount += 1;
|
|
121
|
+
let released = false;
|
|
122
|
+
return () => {
|
|
123
|
+
if (released) return;
|
|
124
|
+
released = true;
|
|
125
|
+
lockCount = Math.max(0, lockCount - 1);
|
|
126
|
+
if (lockCount > 0) return;
|
|
127
|
+
body.style.overflow = originalOverflow;
|
|
128
|
+
body.style.paddingRight = originalPaddingRight;
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function useScrollLock(active) {
|
|
132
|
+
if (!isBrowser) return;
|
|
133
|
+
let release = null;
|
|
134
|
+
watch2(
|
|
135
|
+
() => toValue2(active),
|
|
136
|
+
(isActive) => {
|
|
137
|
+
if (isActive) {
|
|
138
|
+
release ??= acquireScrollLock();
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
release?.();
|
|
142
|
+
release = null;
|
|
143
|
+
},
|
|
144
|
+
{ immediate: true }
|
|
145
|
+
);
|
|
146
|
+
onScopeDispose2(() => {
|
|
147
|
+
release?.();
|
|
148
|
+
release = null;
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// src/useDialog/useDialog.ts
|
|
153
|
+
function useDialog(options = {}) {
|
|
154
|
+
const { defaultOpen = false, closeOnEscape = true, closeOnOverlayClick = true } = options;
|
|
155
|
+
const controlledOpen = computed(() => toValue3(options.open));
|
|
156
|
+
const isControlled = computed(() => controlledOpen.value !== void 0);
|
|
157
|
+
const machine = createDialogMachine({
|
|
158
|
+
defaultOpen,
|
|
159
|
+
closeOnEscape,
|
|
160
|
+
closeOnOverlayClick
|
|
161
|
+
});
|
|
162
|
+
const internalOpen = ref(machine.getState().open);
|
|
163
|
+
const isOpen = computed(() => isControlled.value ? controlledOpen.value : internalOpen.value);
|
|
164
|
+
const contentElement = ref(null);
|
|
165
|
+
const portalTarget = computed(() => toValue3(options.container) ?? "body");
|
|
166
|
+
function setContentElement(node) {
|
|
167
|
+
contentElement.value = typeof HTMLElement !== "undefined" && node instanceof HTMLElement ? node : null;
|
|
168
|
+
}
|
|
169
|
+
useFocusTrap({ active: isOpen, container: contentElement });
|
|
170
|
+
useScrollLock(isOpen);
|
|
171
|
+
const unsubscribe = machine.subscribe((state) => {
|
|
172
|
+
if (!isControlled.value) {
|
|
173
|
+
internalOpen.value = state.open;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
watch3(
|
|
177
|
+
controlledOpen,
|
|
178
|
+
(value) => {
|
|
179
|
+
if (value !== void 0) {
|
|
180
|
+
machine.send({ type: "SET_OPEN", payload: value });
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
{ immediate: true }
|
|
184
|
+
);
|
|
185
|
+
function setOpen(value) {
|
|
186
|
+
options.onOpenChange?.(value);
|
|
187
|
+
if (!isControlled.value) {
|
|
188
|
+
machine.send({ type: "SET_OPEN", payload: value });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function open() {
|
|
192
|
+
setOpen(true);
|
|
193
|
+
}
|
|
194
|
+
function close() {
|
|
195
|
+
setOpen(false);
|
|
196
|
+
}
|
|
197
|
+
function toggle() {
|
|
198
|
+
setOpen(!isOpen.value);
|
|
199
|
+
}
|
|
200
|
+
function onContentKeydown(event) {
|
|
201
|
+
const result = handleDialogKeydown(event.key, {
|
|
202
|
+
isOpen: isOpen.value,
|
|
203
|
+
canCloseOnEscape: closeOnEscape
|
|
204
|
+
});
|
|
205
|
+
if (result.handled) {
|
|
206
|
+
event.preventDefault();
|
|
207
|
+
}
|
|
208
|
+
if (result.shouldClose) {
|
|
209
|
+
close();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
function onOverlayClick() {
|
|
213
|
+
if (closeOnOverlayClick) {
|
|
214
|
+
close();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
onScopeDispose3(() => {
|
|
218
|
+
unsubscribe();
|
|
219
|
+
machine.destroy();
|
|
220
|
+
});
|
|
221
|
+
const ids = machine.getState();
|
|
222
|
+
function getTriggerProps() {
|
|
223
|
+
return {
|
|
224
|
+
id: ids.triggerId,
|
|
225
|
+
"aria-haspopup": "dialog",
|
|
226
|
+
"aria-expanded": isOpen.value,
|
|
227
|
+
"aria-controls": isOpen.value ? ids.contentId : void 0
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
function getContentProps() {
|
|
231
|
+
return {
|
|
232
|
+
id: ids.contentId,
|
|
233
|
+
role: "dialog",
|
|
234
|
+
"aria-modal": true,
|
|
235
|
+
"aria-labelledby": ids.titleId,
|
|
236
|
+
"aria-describedby": ids.descriptionId,
|
|
237
|
+
tabindex: -1
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function getOverlayProps() {
|
|
241
|
+
return {
|
|
242
|
+
"aria-hidden": true
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
function getTitleProps() {
|
|
246
|
+
return { id: ids.titleId };
|
|
247
|
+
}
|
|
248
|
+
function getDescriptionProps() {
|
|
249
|
+
return { id: ids.descriptionId };
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
isOpen,
|
|
253
|
+
open,
|
|
254
|
+
close,
|
|
255
|
+
toggle,
|
|
256
|
+
portalTarget,
|
|
257
|
+
getTriggerProps,
|
|
258
|
+
getContentProps,
|
|
259
|
+
getOverlayProps,
|
|
260
|
+
getTitleProps,
|
|
261
|
+
getDescriptionProps,
|
|
262
|
+
setContentElement,
|
|
263
|
+
onContentKeydown,
|
|
264
|
+
onOverlayClick
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/useTabs/useTabs.ts
|
|
269
|
+
import { computed as computed2, onUnmounted, ref as ref2, toValue as toValue4, useId, watch as watch4 } from "vue";
|
|
270
|
+
import { createTabsMachine, handleTabsKeydown } from "@agile-plus/primitives/tabs";
|
|
271
|
+
function useTabs(options = {}) {
|
|
272
|
+
const { defaultValue = "", activationMode = "automatic", orientation = "horizontal" } = options;
|
|
273
|
+
const uid = useId();
|
|
274
|
+
function getTriggerId(tabValue) {
|
|
275
|
+
return `${uid}-tab-${tabValue}`;
|
|
276
|
+
}
|
|
277
|
+
function getPanelId(tabValue) {
|
|
278
|
+
return `${uid}-tabpanel-${tabValue}`;
|
|
279
|
+
}
|
|
280
|
+
const controlledActive = computed2(() => toValue4(options.modelValue));
|
|
281
|
+
const isControlled = computed2(() => controlledActive.value !== void 0);
|
|
282
|
+
const machine = createTabsMachine({
|
|
283
|
+
defaultValue,
|
|
284
|
+
activationMode,
|
|
285
|
+
orientation
|
|
286
|
+
});
|
|
287
|
+
const internalActive = ref2(machine.getState().activeValue);
|
|
288
|
+
const activeValue = computed2(
|
|
289
|
+
() => isControlled.value ? controlledActive.value : internalActive.value
|
|
290
|
+
);
|
|
291
|
+
const unsubscribe = machine.subscribe((state) => {
|
|
292
|
+
const changed = state.activeValue !== activeValue.value;
|
|
293
|
+
if (!isControlled.value) {
|
|
294
|
+
internalActive.value = state.activeValue;
|
|
295
|
+
}
|
|
296
|
+
if (changed) {
|
|
297
|
+
options.onValueChange?.(state.activeValue);
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
watch4(
|
|
301
|
+
controlledActive,
|
|
302
|
+
(value) => {
|
|
303
|
+
if (value !== void 0) {
|
|
304
|
+
machine.send({ type: "SET_ACTIVE", payload: value });
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
{ immediate: true }
|
|
308
|
+
);
|
|
309
|
+
function setActive(value) {
|
|
310
|
+
if (isControlled.value) {
|
|
311
|
+
options.onValueChange?.(value);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
machine.send({ type: "SET_ACTIVE", payload: value });
|
|
315
|
+
}
|
|
316
|
+
function registerTab(value) {
|
|
317
|
+
machine.send({ type: "REGISTER_TAB", payload: value });
|
|
318
|
+
}
|
|
319
|
+
function unregisterTab(value) {
|
|
320
|
+
machine.send({ type: "UNREGISTER_TAB", payload: value });
|
|
321
|
+
}
|
|
322
|
+
function onTriggerKeydown(event) {
|
|
323
|
+
const state = machine.getState();
|
|
324
|
+
const result = handleTabsKeydown(event.key, {
|
|
325
|
+
tabValues: state.tabValues,
|
|
326
|
+
focusedValue: state.focusedValue,
|
|
327
|
+
orientation: state.orientation,
|
|
328
|
+
loop: true
|
|
329
|
+
});
|
|
330
|
+
if (result.handled) {
|
|
331
|
+
event.preventDefault();
|
|
332
|
+
}
|
|
333
|
+
if (result.nextFocusedValue !== null) {
|
|
334
|
+
machine.send({ type: "FOCUS_TAB", payload: result.nextFocusedValue });
|
|
335
|
+
document.getElementById(getTriggerId(result.nextFocusedValue))?.focus();
|
|
336
|
+
}
|
|
337
|
+
if (event.key === "Enter" || event.key === " ") {
|
|
338
|
+
const focused = machine.getState().focusedValue;
|
|
339
|
+
if (focused !== null && activationMode === "manual") {
|
|
340
|
+
event.preventDefault();
|
|
341
|
+
setActive(focused);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
onUnmounted(() => {
|
|
346
|
+
unsubscribe();
|
|
347
|
+
machine.destroy();
|
|
348
|
+
});
|
|
349
|
+
function getTabListProps() {
|
|
350
|
+
return {
|
|
351
|
+
role: "tablist",
|
|
352
|
+
"aria-orientation": orientation
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
function getTriggerProps(value) {
|
|
356
|
+
const isActive = activeValue.value === value;
|
|
357
|
+
return {
|
|
358
|
+
role: "tab",
|
|
359
|
+
"aria-selected": isActive,
|
|
360
|
+
"aria-controls": getPanelId(value),
|
|
361
|
+
id: getTriggerId(value),
|
|
362
|
+
tabindex: isActive ? 0 : -1,
|
|
363
|
+
"data-state": isActive ? "active" : "inactive",
|
|
364
|
+
"data-value": value
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function getContentProps(value) {
|
|
368
|
+
const isActive = activeValue.value === value;
|
|
369
|
+
return {
|
|
370
|
+
role: "tabpanel",
|
|
371
|
+
"aria-labelledby": getTriggerId(value),
|
|
372
|
+
id: getPanelId(value),
|
|
373
|
+
tabindex: 0,
|
|
374
|
+
"data-state": isActive ? "active" : "inactive",
|
|
375
|
+
"data-value": value,
|
|
376
|
+
hidden: !isActive
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
return {
|
|
380
|
+
activeValue,
|
|
381
|
+
setActive,
|
|
382
|
+
registerTab,
|
|
383
|
+
unregisterTab,
|
|
384
|
+
getTabListProps,
|
|
385
|
+
getTriggerProps,
|
|
386
|
+
getContentProps,
|
|
387
|
+
onTriggerKeydown
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// src/useRadioGroup/useRadioGroup.ts
|
|
392
|
+
import { computed as computed5, toValue as toValue6, useId as useId2 } from "vue";
|
|
393
|
+
import { handleRadioGroupKeydown } from "@agile-plus/primitives/radio-group";
|
|
394
|
+
|
|
395
|
+
// src/utils/useCollection.ts
|
|
396
|
+
import { computed as computed3, shallowRef } from "vue";
|
|
397
|
+
function useCollection(options) {
|
|
398
|
+
const entries = shallowRef([]);
|
|
399
|
+
const items = computed3(() => entries.value);
|
|
400
|
+
function add(item) {
|
|
401
|
+
const key = options.key(item);
|
|
402
|
+
entries.value = [...entries.value.filter((existing) => options.key(existing) !== key), item];
|
|
403
|
+
return () => {
|
|
404
|
+
entries.value = entries.value.filter((existing) => existing !== item);
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
function remove(key) {
|
|
408
|
+
entries.value = entries.value.filter((existing) => options.key(existing) !== key);
|
|
409
|
+
}
|
|
410
|
+
function replace(item) {
|
|
411
|
+
const key = options.key(item);
|
|
412
|
+
entries.value = entries.value.map(
|
|
413
|
+
(existing) => options.key(existing) === key ? item : existing
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
return { items, add, remove, replace };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// src/utils/useControllable.ts
|
|
420
|
+
import { computed as computed4, shallowRef as shallowRef2, toValue as toValue5 } from "vue";
|
|
421
|
+
function useControllable(options) {
|
|
422
|
+
const controlled = computed4(() => toValue5(options.value));
|
|
423
|
+
const isControlled = computed4(() => controlled.value !== void 0);
|
|
424
|
+
const internal = shallowRef2(options.defaultValue);
|
|
425
|
+
const value = computed4(() => {
|
|
426
|
+
const current = controlled.value;
|
|
427
|
+
return current === void 0 ? internal.value : current;
|
|
428
|
+
});
|
|
429
|
+
function set(next) {
|
|
430
|
+
if (!isControlled.value) internal.value = next;
|
|
431
|
+
options.onChange?.(next);
|
|
432
|
+
}
|
|
433
|
+
function replace(next) {
|
|
434
|
+
if (isControlled.value) return;
|
|
435
|
+
internal.value = next;
|
|
436
|
+
}
|
|
437
|
+
return { value, isControlled, set, replace };
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// src/useRadioGroup/useRadioGroup.ts
|
|
441
|
+
function useRadioGroup(options = {}) {
|
|
442
|
+
const { defaultValue = "", loop = true } = options;
|
|
443
|
+
const uid = useId2();
|
|
444
|
+
const state = useControllable({
|
|
445
|
+
value: () => toValue6(options.modelValue),
|
|
446
|
+
defaultValue,
|
|
447
|
+
onChange: (next) => options.onValueChange?.(next)
|
|
448
|
+
});
|
|
449
|
+
const value = state.value;
|
|
450
|
+
const isGroupDisabled = computed5(() => toValue6(options.disabled) === true);
|
|
451
|
+
const hasName = computed5(() => !!toValue6(options.name));
|
|
452
|
+
const collection = useCollection({ key: (item) => item.value });
|
|
453
|
+
const enabledValues = computed5(
|
|
454
|
+
() => isGroupDisabled.value ? [] : collection.items.value.filter((item) => !item.disabled).map((item) => item.value)
|
|
455
|
+
);
|
|
456
|
+
const tabStopValue = computed5(
|
|
457
|
+
() => value.value === "" ? enabledValues.value[0] ?? "" : value.value
|
|
458
|
+
);
|
|
459
|
+
function getItemId(itemValue) {
|
|
460
|
+
return `${uid}-radio-${itemValue}`;
|
|
461
|
+
}
|
|
462
|
+
function setValue(next) {
|
|
463
|
+
state.set(next);
|
|
464
|
+
}
|
|
465
|
+
function registerItem(itemValue) {
|
|
466
|
+
if (collection.items.value.some((item) => item.value === itemValue)) return;
|
|
467
|
+
collection.add({ value: itemValue, disabled: false });
|
|
468
|
+
}
|
|
469
|
+
function setItemDisabled(itemValue, disabled) {
|
|
470
|
+
const current = collection.items.value.find((item) => item.value === itemValue);
|
|
471
|
+
if (!current || current.disabled === disabled) return;
|
|
472
|
+
collection.replace({ value: itemValue, disabled });
|
|
473
|
+
}
|
|
474
|
+
function unregisterItem(itemValue) {
|
|
475
|
+
collection.remove(itemValue);
|
|
476
|
+
if (value.value === itemValue) state.replace("");
|
|
477
|
+
}
|
|
478
|
+
function isItemDisabled(itemValue) {
|
|
479
|
+
if (isGroupDisabled.value) return true;
|
|
480
|
+
return collection.items.value.find((item) => item.value === itemValue)?.disabled === true;
|
|
481
|
+
}
|
|
482
|
+
function onItemKeydown(event) {
|
|
483
|
+
const current = event.currentTarget;
|
|
484
|
+
const focusedValue = current?.dataset.value ?? null;
|
|
485
|
+
const result = handleRadioGroupKeydown(event.key, {
|
|
486
|
+
enabledValues: enabledValues.value,
|
|
487
|
+
focusedValue,
|
|
488
|
+
loop
|
|
489
|
+
});
|
|
490
|
+
if (!result.handled) return;
|
|
491
|
+
event.preventDefault();
|
|
492
|
+
if (result.nextValue === null) return;
|
|
493
|
+
setValue(result.nextValue);
|
|
494
|
+
document.getElementById(getItemId(result.nextValue))?.focus();
|
|
495
|
+
}
|
|
496
|
+
function getRootProps() {
|
|
497
|
+
const required = toValue6(options.required) === true;
|
|
498
|
+
const labelledby = toValue6(options.ariaLabelledby);
|
|
499
|
+
const describedby = toValue6(options.ariaDescribedby);
|
|
500
|
+
const orientation = toValue6(options.orientation) ?? "horizontal";
|
|
501
|
+
return {
|
|
502
|
+
role: "radiogroup",
|
|
503
|
+
"aria-orientation": orientation,
|
|
504
|
+
// 样式层依赖 data-* 而非 aria-*:ARIA 属性面向辅助技术,不应成为视觉契约。
|
|
505
|
+
"data-orientation": orientation,
|
|
506
|
+
"aria-required": required || void 0,
|
|
507
|
+
"aria-labelledby": labelledby || void 0,
|
|
508
|
+
"aria-describedby": describedby || void 0,
|
|
509
|
+
"aria-disabled": isGroupDisabled.value || void 0,
|
|
510
|
+
"data-disabled": isGroupDisabled.value || void 0
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
function getItemProps(itemValue) {
|
|
514
|
+
const checked = value.value === itemValue;
|
|
515
|
+
const disabled = isItemDisabled(itemValue);
|
|
516
|
+
const focusable = !disabled && (checked || tabStopValue.value === itemValue);
|
|
517
|
+
return {
|
|
518
|
+
id: getItemId(itemValue),
|
|
519
|
+
role: "radio",
|
|
520
|
+
"aria-checked": checked,
|
|
521
|
+
"aria-disabled": disabled || void 0,
|
|
522
|
+
tabindex: focusable ? 0 : -1,
|
|
523
|
+
"data-state": checked ? "checked" : "unchecked",
|
|
524
|
+
"data-disabled": disabled || void 0,
|
|
525
|
+
"data-value": itemValue,
|
|
526
|
+
"data-orientation": toValue6(options.orientation) ?? "horizontal"
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
function getInputProps(itemValue) {
|
|
530
|
+
const name = toValue6(options.name);
|
|
531
|
+
if (!name) return null;
|
|
532
|
+
return {
|
|
533
|
+
type: "radio",
|
|
534
|
+
name,
|
|
535
|
+
value: itemValue,
|
|
536
|
+
checked: value.value === itemValue,
|
|
537
|
+
disabled: isItemDisabled(itemValue),
|
|
538
|
+
tabindex: -1,
|
|
539
|
+
"aria-hidden": true
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
return {
|
|
543
|
+
value,
|
|
544
|
+
isGroupDisabled,
|
|
545
|
+
hasName,
|
|
546
|
+
setValue,
|
|
547
|
+
registerItem,
|
|
548
|
+
setItemDisabled,
|
|
549
|
+
unregisterItem,
|
|
550
|
+
isItemDisabled,
|
|
551
|
+
getRootProps,
|
|
552
|
+
getItemProps,
|
|
553
|
+
getInputProps,
|
|
554
|
+
onItemKeydown
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// src/useCheckbox/useCheckbox.ts
|
|
559
|
+
import { computed as computed7, ref as ref4, toValue as toValue9 } from "vue";
|
|
560
|
+
import {
|
|
561
|
+
getCheckboxAttrs,
|
|
562
|
+
isCheckboxIndicatorPresent,
|
|
563
|
+
resolveNextChecked
|
|
564
|
+
} from "@agile-plus/primitives/checkbox";
|
|
565
|
+
import { getHiddenInputAttrs } from "@agile-plus/primitives/form";
|
|
566
|
+
|
|
567
|
+
// src/utils/useBubbleInput.ts
|
|
568
|
+
import { computed as computed6, ref as ref3, shallowRef as shallowRef3, toValue as toValue8, watch as watch6 } from "vue";
|
|
569
|
+
import { getHiddenInputStyle, writeNativeChecked } from "@agile-plus/primitives/form";
|
|
570
|
+
|
|
571
|
+
// src/utils/useResizeObserver.ts
|
|
572
|
+
import { onScopeDispose as onScopeDispose4 } from "vue";
|
|
573
|
+
function useResizeObserver(onResize) {
|
|
574
|
+
let observer = null;
|
|
575
|
+
function observe(element) {
|
|
576
|
+
if (!element || !isBrowser || !isObserverAvailable(globalThis.ResizeObserver)) return;
|
|
577
|
+
observer ??= new ResizeObserver(() => onResize());
|
|
578
|
+
observer.observe(element);
|
|
579
|
+
}
|
|
580
|
+
function unobserve(element) {
|
|
581
|
+
observer?.unobserve(element);
|
|
582
|
+
}
|
|
583
|
+
function disconnect() {
|
|
584
|
+
observer?.disconnect();
|
|
585
|
+
observer = null;
|
|
586
|
+
}
|
|
587
|
+
onScopeDispose4(disconnect);
|
|
588
|
+
return { observe, unobserve, disconnect };
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// src/utils/useFormResetListener.ts
|
|
592
|
+
import { onScopeDispose as onScopeDispose5, toValue as toValue7, watch as watch5 } from "vue";
|
|
593
|
+
function resolveForm(element, formId) {
|
|
594
|
+
if (!isBrowser) return null;
|
|
595
|
+
if (formId) {
|
|
596
|
+
const node = document.getElementById(formId);
|
|
597
|
+
return node instanceof HTMLFormElement ? node : null;
|
|
598
|
+
}
|
|
599
|
+
const native = element && "form" in element ? element.form : null;
|
|
600
|
+
if (native instanceof HTMLFormElement) return native;
|
|
601
|
+
const closest = element?.closest("form");
|
|
602
|
+
return closest instanceof HTMLFormElement ? closest : null;
|
|
603
|
+
}
|
|
604
|
+
function useFormResetListener(control, onReset, formId) {
|
|
605
|
+
let target = null;
|
|
606
|
+
function detach() {
|
|
607
|
+
if (!target) return;
|
|
608
|
+
target.removeEventListener("reset", onReset);
|
|
609
|
+
target = null;
|
|
610
|
+
}
|
|
611
|
+
watch5(
|
|
612
|
+
[control, () => toValue7(formId)],
|
|
613
|
+
([element, nextFormId]) => {
|
|
614
|
+
detach();
|
|
615
|
+
if (!isBrowser) return;
|
|
616
|
+
const form = resolveForm(element, nextFormId);
|
|
617
|
+
if (!form) return;
|
|
618
|
+
form.addEventListener("reset", onReset);
|
|
619
|
+
target = form;
|
|
620
|
+
},
|
|
621
|
+
{ flush: "post" }
|
|
622
|
+
);
|
|
623
|
+
onScopeDispose5(detach);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
// src/utils/useBubbleInput.ts
|
|
627
|
+
function useBubbleInput(options) {
|
|
628
|
+
const inputElement = ref3(null);
|
|
629
|
+
const size = shallowRef3(null);
|
|
630
|
+
const formId = computed6(() => toValue8(options.form));
|
|
631
|
+
const isFormControl = computed6(() => {
|
|
632
|
+
if (formId.value) return true;
|
|
633
|
+
const element = options.control.value;
|
|
634
|
+
if (!element) return true;
|
|
635
|
+
return element.closest("form") !== null;
|
|
636
|
+
});
|
|
637
|
+
const style = computed6(() => getHiddenInputStyle(size.value));
|
|
638
|
+
const resize = useResizeObserver(measure);
|
|
639
|
+
function measure() {
|
|
640
|
+
const element = options.control.value;
|
|
641
|
+
if (!element) {
|
|
642
|
+
size.value = null;
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
const rect = element.getBoundingClientRect();
|
|
646
|
+
size.value = { width: rect.width, height: rect.height };
|
|
647
|
+
}
|
|
648
|
+
function syncInput(notify) {
|
|
649
|
+
const input = inputElement.value;
|
|
650
|
+
if (!input) return;
|
|
651
|
+
writeNativeChecked(input, options.checked());
|
|
652
|
+
if (notify) input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
653
|
+
}
|
|
654
|
+
useFormResetListener(
|
|
655
|
+
options.control,
|
|
656
|
+
() => options.onReset(),
|
|
657
|
+
() => formId.value
|
|
658
|
+
);
|
|
659
|
+
watch6(
|
|
660
|
+
options.control,
|
|
661
|
+
(element) => {
|
|
662
|
+
resize.disconnect();
|
|
663
|
+
if (!isBrowser) return;
|
|
664
|
+
measure();
|
|
665
|
+
resize.observe(element);
|
|
666
|
+
},
|
|
667
|
+
{ flush: "post" }
|
|
668
|
+
);
|
|
669
|
+
watch6(inputElement, (input) => {
|
|
670
|
+
if (!input) return;
|
|
671
|
+
input.defaultChecked = options.checked();
|
|
672
|
+
syncInput(false);
|
|
673
|
+
});
|
|
674
|
+
watch6(
|
|
675
|
+
() => options.checked(),
|
|
676
|
+
() => syncInput(true)
|
|
677
|
+
);
|
|
678
|
+
function setInputElement(node) {
|
|
679
|
+
inputElement.value = node instanceof HTMLInputElement ? node : null;
|
|
680
|
+
}
|
|
681
|
+
return { isFormControl, style, inputElement, setInputElement };
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// src/useCheckbox/useCheckbox.ts
|
|
685
|
+
function useCheckbox(options = {}) {
|
|
686
|
+
const { defaultChecked = false } = options;
|
|
687
|
+
const state = useControllable({
|
|
688
|
+
value: () => toValue9(options.checked),
|
|
689
|
+
defaultValue: defaultChecked,
|
|
690
|
+
onChange: (next) => options.onCheckedChange?.(next)
|
|
691
|
+
});
|
|
692
|
+
const checked = state.value;
|
|
693
|
+
const isDisabled = computed7(() => toValue9(options.disabled) === true);
|
|
694
|
+
const isRequired = computed7(() => toValue9(options.required) === true);
|
|
695
|
+
const isIndicatorPresent = computed7(() => isCheckboxIndicatorPresent(checked.value));
|
|
696
|
+
const control = ref4(null);
|
|
697
|
+
const initialChecked = checked.value;
|
|
698
|
+
const bubble = useBubbleInput({
|
|
699
|
+
control,
|
|
700
|
+
checked: () => checked.value === true,
|
|
701
|
+
onReset: () => setChecked(initialChecked)
|
|
702
|
+
});
|
|
703
|
+
function setChecked(next) {
|
|
704
|
+
state.set(next);
|
|
705
|
+
}
|
|
706
|
+
function toggle() {
|
|
707
|
+
if (isDisabled.value) return;
|
|
708
|
+
setChecked(resolveNextChecked(checked.value));
|
|
709
|
+
}
|
|
710
|
+
function setControl(node) {
|
|
711
|
+
control.value = node instanceof HTMLElement ? node : null;
|
|
712
|
+
}
|
|
713
|
+
function getRootProps() {
|
|
714
|
+
const id = toValue9(options.id);
|
|
715
|
+
return {
|
|
716
|
+
type: "button",
|
|
717
|
+
role: "checkbox",
|
|
718
|
+
id: id || void 0,
|
|
719
|
+
...getCheckboxAttrs({
|
|
720
|
+
checked: checked.value,
|
|
721
|
+
disabled: isDisabled.value,
|
|
722
|
+
required: isRequired.value
|
|
723
|
+
}),
|
|
724
|
+
disabled: isDisabled.value || void 0
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
function getInputProps() {
|
|
728
|
+
if (!bubble.isFormControl.value) return null;
|
|
729
|
+
return {
|
|
730
|
+
...getHiddenInputAttrs({
|
|
731
|
+
type: "checkbox",
|
|
732
|
+
name: toValue9(options.name),
|
|
733
|
+
value: toValue9(options.value) ?? "on",
|
|
734
|
+
disabled: isDisabled.value,
|
|
735
|
+
required: isRequired.value
|
|
736
|
+
}),
|
|
737
|
+
style: bubble.style.value
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
function onRootClick() {
|
|
741
|
+
toggle();
|
|
742
|
+
}
|
|
743
|
+
return {
|
|
744
|
+
checked,
|
|
745
|
+
isDisabled,
|
|
746
|
+
isFormControl: bubble.isFormControl,
|
|
747
|
+
isIndicatorPresent,
|
|
748
|
+
control,
|
|
749
|
+
setControl,
|
|
750
|
+
inputElement: bubble.inputElement,
|
|
751
|
+
setInputElement: bubble.setInputElement,
|
|
752
|
+
setChecked,
|
|
753
|
+
toggle,
|
|
754
|
+
getRootProps,
|
|
755
|
+
getInputProps,
|
|
756
|
+
onRootClick
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
// src/useCheckboxGroup/useCheckboxGroup.ts
|
|
761
|
+
import { computed as computed8, toValue as toValue10 } from "vue";
|
|
762
|
+
import { isGroupValueChecked, toggleGroupValue } from "@agile-plus/primitives/checkbox";
|
|
763
|
+
function useCheckboxGroup(options = {}) {
|
|
764
|
+
const { defaultValue = [] } = options;
|
|
765
|
+
const state = useControllable({
|
|
766
|
+
value: () => toValue10(options.modelValue),
|
|
767
|
+
// 拷贝一份:调用方传入的数组不该被当成内部状态持有,否则外部改写会穿透进来。
|
|
768
|
+
defaultValue: [...defaultValue],
|
|
769
|
+
onChange: (next) => options.onValueChange?.(next)
|
|
770
|
+
});
|
|
771
|
+
const values = state.value;
|
|
772
|
+
const isDisabled = computed8(() => toValue10(options.disabled) === true);
|
|
773
|
+
const isRequired = computed8(() => toValue10(options.required) === true);
|
|
774
|
+
const name = computed8(() => toValue10(options.name));
|
|
775
|
+
function setValues(next) {
|
|
776
|
+
state.set(next);
|
|
777
|
+
}
|
|
778
|
+
function setItemChecked(value, checked) {
|
|
779
|
+
setValues(toggleGroupValue(values.value, value, checked));
|
|
780
|
+
}
|
|
781
|
+
function isValueChecked(value) {
|
|
782
|
+
return isGroupValueChecked(values.value, value);
|
|
783
|
+
}
|
|
784
|
+
function getRootProps() {
|
|
785
|
+
const labelledby = toValue10(options.ariaLabelledby);
|
|
786
|
+
const describedby = toValue10(options.ariaDescribedby);
|
|
787
|
+
return {
|
|
788
|
+
role: "group",
|
|
789
|
+
"aria-labelledby": labelledby || void 0,
|
|
790
|
+
"aria-describedby": describedby || void 0,
|
|
791
|
+
"aria-required": isRequired.value ? "true" : void 0,
|
|
792
|
+
"aria-disabled": isDisabled.value || void 0,
|
|
793
|
+
"data-disabled": isDisabled.value || void 0
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
return {
|
|
797
|
+
values,
|
|
798
|
+
isDisabled,
|
|
799
|
+
name,
|
|
800
|
+
setValues,
|
|
801
|
+
setItemChecked,
|
|
802
|
+
isValueChecked,
|
|
803
|
+
getRootProps
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// src/useForm/useForm.ts
|
|
808
|
+
import { ref as ref5 } from "vue";
|
|
809
|
+
import { getFirstInvalidControl } from "@agile-plus/primitives/form";
|
|
810
|
+
function useForm(options = {}) {
|
|
811
|
+
const validityMap = ref5({});
|
|
812
|
+
const customErrorsMap = ref5({});
|
|
813
|
+
const messageIdsMap = ref5({});
|
|
814
|
+
const matcherEntries = /* @__PURE__ */ new Map();
|
|
815
|
+
function getFieldValidity(name) {
|
|
816
|
+
return validityMap.value[name];
|
|
817
|
+
}
|
|
818
|
+
function setFieldValidity(name, validity) {
|
|
819
|
+
validityMap.value = { ...validityMap.value, [name]: validity };
|
|
820
|
+
}
|
|
821
|
+
function clearFieldValidation(name) {
|
|
822
|
+
validityMap.value = { ...validityMap.value, [name]: void 0 };
|
|
823
|
+
customErrorsMap.value = { ...customErrorsMap.value, [name]: {} };
|
|
824
|
+
}
|
|
825
|
+
function getFieldCustomErrors(name) {
|
|
826
|
+
return customErrorsMap.value[name] ?? {};
|
|
827
|
+
}
|
|
828
|
+
function setFieldCustomErrors(name, errors) {
|
|
829
|
+
customErrorsMap.value = {
|
|
830
|
+
...customErrorsMap.value,
|
|
831
|
+
[name]: { ...customErrorsMap.value[name], ...errors }
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
function getFieldMatchers(name) {
|
|
835
|
+
return matcherEntries.get(name) ?? [];
|
|
836
|
+
}
|
|
837
|
+
function addFieldMatcher(name, entry) {
|
|
838
|
+
const entries = matcherEntries.get(name) ?? [];
|
|
839
|
+
const next = entries.filter((item) => item.id !== entry.id);
|
|
840
|
+
matcherEntries.set(name, [...next, entry]);
|
|
841
|
+
}
|
|
842
|
+
function removeFieldMatcher(name, id) {
|
|
843
|
+
const entries = matcherEntries.get(name);
|
|
844
|
+
if (!entries) return;
|
|
845
|
+
const next = entries.filter((item) => item.id !== id);
|
|
846
|
+
if (next.length === 0) matcherEntries.delete(name);
|
|
847
|
+
else matcherEntries.set(name, next);
|
|
848
|
+
}
|
|
849
|
+
function getFieldDescription(name) {
|
|
850
|
+
const ids = messageIdsMap.value[name] ?? [];
|
|
851
|
+
return ids.length > 0 ? ids.join(" ") : void 0;
|
|
852
|
+
}
|
|
853
|
+
function addFieldMessageId(name, id) {
|
|
854
|
+
const ids = messageIdsMap.value[name] ?? [];
|
|
855
|
+
if (ids.includes(id)) return;
|
|
856
|
+
messageIdsMap.value = { ...messageIdsMap.value, [name]: [...ids, id] };
|
|
857
|
+
}
|
|
858
|
+
function removeFieldMessageId(name, id) {
|
|
859
|
+
const ids = messageIdsMap.value[name] ?? [];
|
|
860
|
+
const next = ids.filter((item) => item !== id);
|
|
861
|
+
if (next.length === ids.length) return;
|
|
862
|
+
messageIdsMap.value = { ...messageIdsMap.value, [name]: next };
|
|
863
|
+
}
|
|
864
|
+
function onFormInvalid(event) {
|
|
865
|
+
const form = event.currentTarget;
|
|
866
|
+
if (!(form instanceof HTMLFormElement)) return;
|
|
867
|
+
const controls = Array.from(form.elements).filter(
|
|
868
|
+
(element) => element instanceof HTMLElement
|
|
869
|
+
);
|
|
870
|
+
const firstInvalid = getFirstInvalidControl(controls);
|
|
871
|
+
if (firstInvalid && firstInvalid === event.target) {
|
|
872
|
+
event.target.focus();
|
|
873
|
+
}
|
|
874
|
+
event.preventDefault();
|
|
875
|
+
}
|
|
876
|
+
function onFormSubmit() {
|
|
877
|
+
options.onClearServerErrors?.();
|
|
878
|
+
}
|
|
879
|
+
function onFormReset() {
|
|
880
|
+
options.onClearServerErrors?.();
|
|
881
|
+
}
|
|
882
|
+
return {
|
|
883
|
+
getFieldValidity,
|
|
884
|
+
setFieldValidity,
|
|
885
|
+
clearFieldValidation,
|
|
886
|
+
getFieldCustomErrors,
|
|
887
|
+
setFieldCustomErrors,
|
|
888
|
+
getFieldMatchers,
|
|
889
|
+
addFieldMatcher,
|
|
890
|
+
removeFieldMatcher,
|
|
891
|
+
getFieldDescription,
|
|
892
|
+
addFieldMessageId,
|
|
893
|
+
removeFieldMessageId,
|
|
894
|
+
onFormInvalid,
|
|
895
|
+
onFormSubmit,
|
|
896
|
+
onFormReset
|
|
897
|
+
};
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// src/useFormField/useFormField.ts
|
|
901
|
+
import { computed as computed9, toValue as toValue11, useId as useId3 } from "vue";
|
|
902
|
+
import { getFieldStateAttrs } from "@agile-plus/primitives/form";
|
|
903
|
+
function useFormField(options, form) {
|
|
904
|
+
const uid = useId3();
|
|
905
|
+
const name = computed9(() => toValue11(options.name));
|
|
906
|
+
const id = computed9(() => toValue11(options.id) ?? `field-${uid}`);
|
|
907
|
+
const serverInvalid = computed9(() => toValue11(options.serverInvalid) === true);
|
|
908
|
+
const validity = computed9(() => form.getFieldValidity(name.value));
|
|
909
|
+
function getStateAttrs() {
|
|
910
|
+
return getFieldStateAttrs({ validity: validity.value, serverInvalid: serverInvalid.value });
|
|
911
|
+
}
|
|
912
|
+
return { id, name, serverInvalid, validity, getStateAttrs };
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
// src/useFormControl/useFormControl.ts
|
|
916
|
+
import { computed as computed10, ref as ref6, toValue as toValue12 } from "vue";
|
|
917
|
+
import {
|
|
918
|
+
DEFAULT_INVALID_MESSAGE,
|
|
919
|
+
concatAriaDescribedby,
|
|
920
|
+
getFieldStateAttrs as getFieldStateAttrs2,
|
|
921
|
+
hasBuiltInError,
|
|
922
|
+
isAsyncCustomMatcher,
|
|
923
|
+
isSyncCustomMatcher,
|
|
924
|
+
readValiditySnapshot
|
|
925
|
+
} from "@agile-plus/primitives/form";
|
|
926
|
+
function useFormControl(options, form, field) {
|
|
927
|
+
const name = toValue12(options.name) ?? field?.name.value ?? "";
|
|
928
|
+
if (!name) {
|
|
929
|
+
throw new Error("useFormControl \u9700\u8981 name\uFF1A\u8BF7\u628A\u5B83\u653E\u8FDB useFormField\uFF0C\u6216\u663E\u5F0F\u63D0\u4F9B name\u3002");
|
|
930
|
+
}
|
|
931
|
+
const id = toValue12(options.id) ?? field?.id.value;
|
|
932
|
+
const validity = computed10(() => form.getFieldValidity(name));
|
|
933
|
+
const serverInvalid = computed10(() => field?.serverInvalid.value === true);
|
|
934
|
+
const controlElement = ref6(null);
|
|
935
|
+
function setControlElement(node) {
|
|
936
|
+
controlElement.value = node instanceof HTMLElement && "validity" in node && "setCustomValidity" in node ? node : null;
|
|
937
|
+
}
|
|
938
|
+
function getAttrs() {
|
|
939
|
+
return {
|
|
940
|
+
id,
|
|
941
|
+
name,
|
|
942
|
+
...getFieldStateAttrs2({ validity: validity.value, serverInvalid: serverInvalid.value }),
|
|
943
|
+
// 原生无效时不写 aria-invalid:浏览器自己会报告状态,重复声明会让读屏播两遍。
|
|
944
|
+
// 服务端错误没有原生状态可依赖,只能由我们显式声明。
|
|
945
|
+
"aria-invalid": serverInvalid.value ? "true" : void 0,
|
|
946
|
+
"aria-describedby": concatAriaDescribedby(
|
|
947
|
+
toValue12(options.describedby),
|
|
948
|
+
form.getFieldDescription(name)
|
|
949
|
+
),
|
|
950
|
+
// 关掉浏览器在悬停时显示的原生错误提示:文案已经由 Message 呈现。
|
|
951
|
+
title: ""
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
async function validate() {
|
|
955
|
+
const control = controlElement.value;
|
|
956
|
+
if (!control) return;
|
|
957
|
+
const snapshot = readValiditySnapshot(control.validity);
|
|
958
|
+
if (hasBuiltInError(snapshot)) {
|
|
959
|
+
form.setFieldValidity(name, snapshot);
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
const formData = control.form ? new FormData(control.form) : new FormData();
|
|
963
|
+
const entries = form.getFieldMatchers(name);
|
|
964
|
+
const syncEntries = entries.filter(
|
|
965
|
+
(entry) => isSyncCustomMatcher(entry.match)
|
|
966
|
+
);
|
|
967
|
+
const asyncEntries = entries.filter(
|
|
968
|
+
(entry) => isAsyncCustomMatcher(entry.match)
|
|
969
|
+
);
|
|
970
|
+
const syncErrors = Object.fromEntries(
|
|
971
|
+
syncEntries.map((entry) => [entry.id, entry.match(control.value, formData)])
|
|
972
|
+
);
|
|
973
|
+
applyErrors(control, syncErrors);
|
|
974
|
+
if (Object.values(syncErrors).some(Boolean) || asyncEntries.length === 0) return;
|
|
975
|
+
const asyncErrors = Object.fromEntries(
|
|
976
|
+
await Promise.all(
|
|
977
|
+
asyncEntries.map(
|
|
978
|
+
async (entry) => [entry.id, await entry.match(control.value, formData)]
|
|
979
|
+
)
|
|
980
|
+
)
|
|
981
|
+
);
|
|
982
|
+
applyErrors(control, asyncErrors);
|
|
983
|
+
}
|
|
984
|
+
function applyErrors(control, errors) {
|
|
985
|
+
const hasError = Object.values(errors).some(Boolean);
|
|
986
|
+
control.setCustomValidity(hasError ? DEFAULT_INVALID_MESSAGE : "");
|
|
987
|
+
form.setFieldValidity(name, readValiditySnapshot(control.validity));
|
|
988
|
+
form.setFieldCustomErrors(name, errors);
|
|
989
|
+
}
|
|
990
|
+
function resetValidation() {
|
|
991
|
+
controlElement.value?.setCustomValidity("");
|
|
992
|
+
form.clearFieldValidation(name);
|
|
993
|
+
}
|
|
994
|
+
function onControlInput() {
|
|
995
|
+
resetValidation();
|
|
996
|
+
}
|
|
997
|
+
function onControlInvalid() {
|
|
998
|
+
void validate();
|
|
999
|
+
}
|
|
1000
|
+
useFormResetListener(controlElement, resetValidation);
|
|
1001
|
+
return {
|
|
1002
|
+
controlElement,
|
|
1003
|
+
setControlElement,
|
|
1004
|
+
validity,
|
|
1005
|
+
getAttrs,
|
|
1006
|
+
validate,
|
|
1007
|
+
onControlInput,
|
|
1008
|
+
onControlInvalid,
|
|
1009
|
+
resetValidation
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
// src/useFormMessage/useFormMessage.ts
|
|
1014
|
+
import { computed as computed11, isRef, onScopeDispose as onScopeDispose6, toValue as toValue13, useId as useId4, watchEffect } from "vue";
|
|
1015
|
+
import {
|
|
1016
|
+
BUILT_IN_VALIDITY_MESSAGES,
|
|
1017
|
+
DEFAULT_INVALID_MESSAGE as DEFAULT_INVALID_MESSAGE2,
|
|
1018
|
+
getMessageMatch
|
|
1019
|
+
} from "@agile-plus/primitives/form";
|
|
1020
|
+
function resolveMatch(match) {
|
|
1021
|
+
if (match === void 0) return void 0;
|
|
1022
|
+
return isRef(match) ? match.value : match;
|
|
1023
|
+
}
|
|
1024
|
+
function useFormMessage(options, form, field) {
|
|
1025
|
+
const name = toValue13(options.name) ?? field?.name.value;
|
|
1026
|
+
if (!name) {
|
|
1027
|
+
throw new Error("useFormMessage \u9700\u8981 name\uFF1A\u8BF7\u628A\u5B83\u653E\u8FDB useFormField\uFF0C\u6216\u663E\u5F0F\u63D0\u4F9B name\u3002");
|
|
1028
|
+
}
|
|
1029
|
+
const uid = useId4();
|
|
1030
|
+
const id = `message-${uid}`;
|
|
1031
|
+
const match = computed11(() => resolveMatch(options.match));
|
|
1032
|
+
const matches = computed11(
|
|
1033
|
+
() => getMessageMatch({
|
|
1034
|
+
match: match.value,
|
|
1035
|
+
validity: form.getFieldValidity(name),
|
|
1036
|
+
customErrors: form.getFieldCustomErrors(name),
|
|
1037
|
+
messageId: id,
|
|
1038
|
+
forceMatch: toValue13(options.forceMatch) === true
|
|
1039
|
+
})
|
|
1040
|
+
);
|
|
1041
|
+
watchEffect((onCleanup) => {
|
|
1042
|
+
if (!matches.value) return;
|
|
1043
|
+
form.addFieldMessageId(name, id);
|
|
1044
|
+
onCleanup(() => form.removeFieldMessageId(name, id));
|
|
1045
|
+
});
|
|
1046
|
+
watchEffect((onCleanup) => {
|
|
1047
|
+
const current = match.value;
|
|
1048
|
+
if (typeof current !== "function") return;
|
|
1049
|
+
form.addFieldMatcher(name, { id, match: current });
|
|
1050
|
+
onCleanup(() => form.removeFieldMatcher(name, id));
|
|
1051
|
+
});
|
|
1052
|
+
function getDefaultText() {
|
|
1053
|
+
const current = match.value;
|
|
1054
|
+
if (typeof current !== "string") return DEFAULT_INVALID_MESSAGE2;
|
|
1055
|
+
return BUILT_IN_VALIDITY_MESSAGES[current] ?? DEFAULT_INVALID_MESSAGE2;
|
|
1056
|
+
}
|
|
1057
|
+
onScopeDispose6(() => {
|
|
1058
|
+
form.removeFieldMessageId(name, id);
|
|
1059
|
+
form.removeFieldMatcher(name, id);
|
|
1060
|
+
});
|
|
1061
|
+
return { id, name: computed11(() => name), matches, getDefaultText };
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
// src/useCollapsible/useCollapsible.ts
|
|
1065
|
+
import { computed as computed12, onScopeDispose as onScopeDispose7, ref as ref7, shallowRef as shallowRef4, toValue as toValue14, useId as useId5 } from "vue";
|
|
1066
|
+
import {
|
|
1067
|
+
getCollapsibleContentAttrs,
|
|
1068
|
+
getCollapsibleContentVars,
|
|
1069
|
+
getCollapsibleRootAttrs,
|
|
1070
|
+
getCollapsibleTriggerAttrs
|
|
1071
|
+
} from "@agile-plus/primitives/collapsible";
|
|
1072
|
+
function useCollapsible(options = {}) {
|
|
1073
|
+
const state = useControllable({
|
|
1074
|
+
value: () => toValue14(options.open),
|
|
1075
|
+
defaultValue: options.defaultOpen ?? false,
|
|
1076
|
+
onChange: (value) => options.onOpenChange?.(value)
|
|
1077
|
+
});
|
|
1078
|
+
const isOpen = state.value;
|
|
1079
|
+
const isDisabled = computed12(() => toValue14(options.disabled) === true);
|
|
1080
|
+
const contentId = `collapsible-${useId5()}`;
|
|
1081
|
+
const contentElement = ref7(null);
|
|
1082
|
+
const size = shallowRef4(null);
|
|
1083
|
+
const preventMountAnimation = ref7(isOpen.value);
|
|
1084
|
+
let originalStyles = null;
|
|
1085
|
+
let frame = 0;
|
|
1086
|
+
if (isBrowser) {
|
|
1087
|
+
frame = requestAnimationFrame(() => {
|
|
1088
|
+
preventMountAnimation.value = false;
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
onScopeDispose7(() => {
|
|
1092
|
+
if (isBrowser) cancelAnimationFrame(frame);
|
|
1093
|
+
});
|
|
1094
|
+
function setOpen(value) {
|
|
1095
|
+
state.set(value);
|
|
1096
|
+
}
|
|
1097
|
+
function open() {
|
|
1098
|
+
setOpen(true);
|
|
1099
|
+
}
|
|
1100
|
+
function close() {
|
|
1101
|
+
setOpen(false);
|
|
1102
|
+
}
|
|
1103
|
+
function toggle() {
|
|
1104
|
+
if (isDisabled.value) return;
|
|
1105
|
+
setOpen(!isOpen.value);
|
|
1106
|
+
}
|
|
1107
|
+
function setContentElement(node) {
|
|
1108
|
+
contentElement.value = node instanceof HTMLElement ? node : null;
|
|
1109
|
+
}
|
|
1110
|
+
function measureContent() {
|
|
1111
|
+
const node = contentElement.value;
|
|
1112
|
+
if (!node || !isBrowser) return;
|
|
1113
|
+
originalStyles = originalStyles ?? {
|
|
1114
|
+
transitionDuration: node.style.transitionDuration,
|
|
1115
|
+
animationName: node.style.animationName
|
|
1116
|
+
};
|
|
1117
|
+
node.style.transitionDuration = "0s";
|
|
1118
|
+
node.style.animationName = "none";
|
|
1119
|
+
const rect = node.getBoundingClientRect();
|
|
1120
|
+
size.value = { height: rect.height, width: rect.width };
|
|
1121
|
+
if (!preventMountAnimation.value) {
|
|
1122
|
+
node.style.transitionDuration = originalStyles.transitionDuration;
|
|
1123
|
+
node.style.animationName = originalStyles.animationName;
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
function getRootAttrs() {
|
|
1127
|
+
return getCollapsibleRootAttrs({ open: isOpen.value, disabled: isDisabled.value });
|
|
1128
|
+
}
|
|
1129
|
+
function getTriggerAttrs() {
|
|
1130
|
+
return getCollapsibleTriggerAttrs({
|
|
1131
|
+
open: isOpen.value,
|
|
1132
|
+
disabled: isDisabled.value,
|
|
1133
|
+
contentId
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1136
|
+
function getContentAttrs(isPresent) {
|
|
1137
|
+
return getCollapsibleContentAttrs({
|
|
1138
|
+
open: isOpen.value,
|
|
1139
|
+
disabled: isDisabled.value,
|
|
1140
|
+
contentId,
|
|
1141
|
+
isPresent
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
function getContentVars() {
|
|
1145
|
+
return getCollapsibleContentVars(size.value);
|
|
1146
|
+
}
|
|
1147
|
+
return {
|
|
1148
|
+
isOpen,
|
|
1149
|
+
isDisabled,
|
|
1150
|
+
contentId,
|
|
1151
|
+
open,
|
|
1152
|
+
close,
|
|
1153
|
+
toggle,
|
|
1154
|
+
getRootAttrs,
|
|
1155
|
+
getTriggerAttrs,
|
|
1156
|
+
getContentAttrs,
|
|
1157
|
+
contentElement,
|
|
1158
|
+
setContentElement,
|
|
1159
|
+
measureContent,
|
|
1160
|
+
getContentVars
|
|
1161
|
+
};
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
// src/useScrollArea/useScrollArea.ts
|
|
1165
|
+
import { computed as computed13, onScopeDispose as onScopeDispose9, ref as ref8, toValue as toValue15, watch as watch7 } from "vue";
|
|
1166
|
+
import {
|
|
1167
|
+
clampThumbOffset,
|
|
1168
|
+
getScrollOffsetFromThumb,
|
|
1169
|
+
getThumbMetrics,
|
|
1170
|
+
resolveScrollbarVisibility
|
|
1171
|
+
} from "@agile-plus/primitives/scroll-area";
|
|
1172
|
+
|
|
1173
|
+
// src/utils/usePointerDrag.ts
|
|
1174
|
+
import { onScopeDispose as onScopeDispose8 } from "vue";
|
|
1175
|
+
function usePointerDrag() {
|
|
1176
|
+
let current = null;
|
|
1177
|
+
function start(handlers) {
|
|
1178
|
+
if (!isBrowser) return;
|
|
1179
|
+
current?.("cancel");
|
|
1180
|
+
const onMove = (event) => {
|
|
1181
|
+
handlers.onMove(event);
|
|
1182
|
+
};
|
|
1183
|
+
const onUp = () => finish("up");
|
|
1184
|
+
const onCancel = () => finish("cancel");
|
|
1185
|
+
function finish(reason) {
|
|
1186
|
+
window.removeEventListener("pointermove", onMove);
|
|
1187
|
+
window.removeEventListener("pointerup", onUp);
|
|
1188
|
+
window.removeEventListener("pointercancel", onCancel);
|
|
1189
|
+
if (current === finish) current = null;
|
|
1190
|
+
handlers.onEnd(reason);
|
|
1191
|
+
}
|
|
1192
|
+
current = finish;
|
|
1193
|
+
window.addEventListener("pointermove", onMove);
|
|
1194
|
+
window.addEventListener("pointerup", onUp);
|
|
1195
|
+
window.addEventListener("pointercancel", onCancel);
|
|
1196
|
+
}
|
|
1197
|
+
function stop() {
|
|
1198
|
+
current?.("cancel");
|
|
1199
|
+
}
|
|
1200
|
+
onScopeDispose8(stop);
|
|
1201
|
+
return { start, stop };
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
// src/useScrollArea/useScrollArea.ts
|
|
1205
|
+
var DEFAULT_TYPE = "hover";
|
|
1206
|
+
var DEFAULT_SCROLL_HIDE_DELAY = 600;
|
|
1207
|
+
var EMPTY_METRICS = { size: 0, offset: 0, hasOverflow: false };
|
|
1208
|
+
function useScrollArea(options = {}) {
|
|
1209
|
+
const { scrollHideDelay = DEFAULT_SCROLL_HIDE_DELAY, minThumbSize } = options;
|
|
1210
|
+
const viewportRef = ref8(null);
|
|
1211
|
+
const measureVersion = ref8(0);
|
|
1212
|
+
const hoveringViewport = ref8(false);
|
|
1213
|
+
const focusingViewport = ref8(false);
|
|
1214
|
+
const hoveringScrollbar = ref8({
|
|
1215
|
+
vertical: false,
|
|
1216
|
+
horizontal: false
|
|
1217
|
+
});
|
|
1218
|
+
const draggingOrientation = ref8(null);
|
|
1219
|
+
const recentlyScrolled = ref8(false);
|
|
1220
|
+
const type = computed13(() => toValue15(options.type) ?? DEFAULT_TYPE);
|
|
1221
|
+
let measureFrame = null;
|
|
1222
|
+
let hideTimer = null;
|
|
1223
|
+
let mutationObserver = null;
|
|
1224
|
+
const resize = useResizeObserver(requestMeasure);
|
|
1225
|
+
const drag = usePointerDrag();
|
|
1226
|
+
function requestMeasure() {
|
|
1227
|
+
if (typeof window === "undefined" || typeof window.requestAnimationFrame !== "function") {
|
|
1228
|
+
return;
|
|
1229
|
+
}
|
|
1230
|
+
if (measureFrame !== null) return;
|
|
1231
|
+
measureFrame = window.requestAnimationFrame(() => {
|
|
1232
|
+
measureFrame = null;
|
|
1233
|
+
measureVersion.value += 1;
|
|
1234
|
+
});
|
|
1235
|
+
}
|
|
1236
|
+
function readTrack(orientation) {
|
|
1237
|
+
const el = viewportRef.value;
|
|
1238
|
+
if (!el) return { viewportSize: 0, scrollSize: 0, scrollOffset: 0 };
|
|
1239
|
+
return orientation === "vertical" ? {
|
|
1240
|
+
viewportSize: el.clientHeight,
|
|
1241
|
+
scrollSize: el.scrollHeight,
|
|
1242
|
+
scrollOffset: el.scrollTop
|
|
1243
|
+
} : {
|
|
1244
|
+
viewportSize: el.clientWidth,
|
|
1245
|
+
scrollSize: el.scrollWidth,
|
|
1246
|
+
scrollOffset: el.scrollLeft
|
|
1247
|
+
};
|
|
1248
|
+
}
|
|
1249
|
+
function measureMetrics(orientation) {
|
|
1250
|
+
const { viewportSize, scrollSize, scrollOffset } = readTrack(orientation);
|
|
1251
|
+
return getThumbMetrics({ viewportSize, scrollSize, scrollOffset, minThumbSize });
|
|
1252
|
+
}
|
|
1253
|
+
const verticalMetrics = computed13(() => {
|
|
1254
|
+
void measureVersion.value;
|
|
1255
|
+
return typeof window === "undefined" ? EMPTY_METRICS : measureMetrics("vertical");
|
|
1256
|
+
});
|
|
1257
|
+
const horizontalMetrics = computed13(() => {
|
|
1258
|
+
void measureVersion.value;
|
|
1259
|
+
return typeof window === "undefined" ? EMPTY_METRICS : measureMetrics("horizontal");
|
|
1260
|
+
});
|
|
1261
|
+
function getMetrics(orientation) {
|
|
1262
|
+
return orientation === "vertical" ? verticalMetrics.value : horizontalMetrics.value;
|
|
1263
|
+
}
|
|
1264
|
+
function markRecentlyScrolled() {
|
|
1265
|
+
recentlyScrolled.value = true;
|
|
1266
|
+
if (hideTimer !== null) clearTimeout(hideTimer);
|
|
1267
|
+
hideTimer = setTimeout(() => {
|
|
1268
|
+
hideTimer = null;
|
|
1269
|
+
recentlyScrolled.value = false;
|
|
1270
|
+
}, scrollHideDelay);
|
|
1271
|
+
}
|
|
1272
|
+
function getVisibility(orientation) {
|
|
1273
|
+
const metrics = getMetrics(orientation);
|
|
1274
|
+
return resolveScrollbarVisibility(
|
|
1275
|
+
{
|
|
1276
|
+
interacting: hoveringViewport.value || focusingViewport.value || hoveringScrollbar.value[orientation],
|
|
1277
|
+
dragging: draggingOrientation.value === orientation,
|
|
1278
|
+
recentlyScrolled: recentlyScrolled.value,
|
|
1279
|
+
hasOverflow: metrics.hasOverflow
|
|
1280
|
+
},
|
|
1281
|
+
type.value
|
|
1282
|
+
);
|
|
1283
|
+
}
|
|
1284
|
+
function getRootProps() {
|
|
1285
|
+
return { dir: toValue15(options.dir) || void 0 };
|
|
1286
|
+
}
|
|
1287
|
+
function getViewportProps() {
|
|
1288
|
+
return { tabindex: 0 };
|
|
1289
|
+
}
|
|
1290
|
+
function getScrollbarProps(orientation) {
|
|
1291
|
+
return {
|
|
1292
|
+
"data-orientation": orientation,
|
|
1293
|
+
"data-state": getVisibility(orientation)
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
function getThumbProps(orientation) {
|
|
1297
|
+
return {
|
|
1298
|
+
"data-state": getVisibility(orientation),
|
|
1299
|
+
"data-orientation": orientation
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
function getThumbStyle(orientation) {
|
|
1303
|
+
const metrics = getMetrics(orientation);
|
|
1304
|
+
return orientation === "vertical" ? { height: `${metrics.size}px`, transform: `translateY(${metrics.offset}px)` } : { width: `${metrics.size}px`, transform: `translateX(${metrics.offset}px)` };
|
|
1305
|
+
}
|
|
1306
|
+
const isCornerVisible = computed13(
|
|
1307
|
+
() => getVisibility("vertical") === "visible" && getVisibility("horizontal") === "visible"
|
|
1308
|
+
);
|
|
1309
|
+
function getCornerProps() {
|
|
1310
|
+
return { "data-state": isCornerVisible.value ? "visible" : "hidden" };
|
|
1311
|
+
}
|
|
1312
|
+
function onViewportScroll() {
|
|
1313
|
+
markRecentlyScrolled();
|
|
1314
|
+
requestMeasure();
|
|
1315
|
+
}
|
|
1316
|
+
function onViewportPointerEnter() {
|
|
1317
|
+
hoveringViewport.value = true;
|
|
1318
|
+
}
|
|
1319
|
+
function onViewportPointerLeave() {
|
|
1320
|
+
hoveringViewport.value = false;
|
|
1321
|
+
}
|
|
1322
|
+
function onViewportFocusin() {
|
|
1323
|
+
focusingViewport.value = true;
|
|
1324
|
+
}
|
|
1325
|
+
function onViewportFocusout() {
|
|
1326
|
+
focusingViewport.value = false;
|
|
1327
|
+
}
|
|
1328
|
+
function onScrollbarPointerEnter(orientation) {
|
|
1329
|
+
hoveringScrollbar.value = { ...hoveringScrollbar.value, [orientation]: true };
|
|
1330
|
+
}
|
|
1331
|
+
function onScrollbarPointerLeave(orientation) {
|
|
1332
|
+
hoveringScrollbar.value = { ...hoveringScrollbar.value, [orientation]: false };
|
|
1333
|
+
}
|
|
1334
|
+
function onThumbPointerDown(event, orientation) {
|
|
1335
|
+
const el = viewportRef.value;
|
|
1336
|
+
if (!el || typeof window === "undefined") return;
|
|
1337
|
+
const metrics = getMetrics(orientation);
|
|
1338
|
+
if (!metrics.hasOverflow) return;
|
|
1339
|
+
const { viewportSize, scrollSize } = readTrack(orientation);
|
|
1340
|
+
const startPointer = orientation === "vertical" ? event.clientY : event.clientX;
|
|
1341
|
+
const startOffset = metrics.offset;
|
|
1342
|
+
draggingOrientation.value = orientation;
|
|
1343
|
+
event.preventDefault();
|
|
1344
|
+
drag.start({
|
|
1345
|
+
onMove: (moveEvent) => {
|
|
1346
|
+
const currentPointer = orientation === "vertical" ? moveEvent.clientY : moveEvent.clientX;
|
|
1347
|
+
const nextOffset = clampThumbOffset(
|
|
1348
|
+
startOffset + (currentPointer - startPointer),
|
|
1349
|
+
viewportSize,
|
|
1350
|
+
metrics.size
|
|
1351
|
+
);
|
|
1352
|
+
const scrollOffset = getScrollOffsetFromThumb({
|
|
1353
|
+
thumbOffset: nextOffset,
|
|
1354
|
+
viewportSize,
|
|
1355
|
+
scrollSize,
|
|
1356
|
+
thumbSize: metrics.size
|
|
1357
|
+
});
|
|
1358
|
+
if (orientation === "vertical") {
|
|
1359
|
+
el.scrollTop = scrollOffset;
|
|
1360
|
+
} else {
|
|
1361
|
+
el.scrollLeft = scrollOffset;
|
|
1362
|
+
}
|
|
1363
|
+
},
|
|
1364
|
+
onEnd: () => {
|
|
1365
|
+
draggingOrientation.value = null;
|
|
1366
|
+
}
|
|
1367
|
+
});
|
|
1368
|
+
}
|
|
1369
|
+
function observeTargets(el) {
|
|
1370
|
+
resize.observe(el);
|
|
1371
|
+
if (el.firstElementChild instanceof HTMLElement) resize.observe(el.firstElementChild);
|
|
1372
|
+
}
|
|
1373
|
+
function detachObservers() {
|
|
1374
|
+
resize.disconnect();
|
|
1375
|
+
mutationObserver?.disconnect();
|
|
1376
|
+
mutationObserver = null;
|
|
1377
|
+
}
|
|
1378
|
+
watch7(viewportRef, (el) => {
|
|
1379
|
+
detachObservers();
|
|
1380
|
+
if (!el || !isObserverAvailable(globalThis.ResizeObserver)) return;
|
|
1381
|
+
observeTargets(el);
|
|
1382
|
+
if (isObserverAvailable(globalThis.MutationObserver)) {
|
|
1383
|
+
mutationObserver = new MutationObserver(() => {
|
|
1384
|
+
observeTargets(el);
|
|
1385
|
+
requestMeasure();
|
|
1386
|
+
});
|
|
1387
|
+
mutationObserver.observe(el, { childList: true });
|
|
1388
|
+
}
|
|
1389
|
+
requestMeasure();
|
|
1390
|
+
});
|
|
1391
|
+
onScopeDispose9(() => {
|
|
1392
|
+
detachObservers();
|
|
1393
|
+
if (measureFrame !== null && isBrowser) {
|
|
1394
|
+
window.cancelAnimationFrame(measureFrame);
|
|
1395
|
+
measureFrame = null;
|
|
1396
|
+
}
|
|
1397
|
+
if (hideTimer !== null) clearTimeout(hideTimer);
|
|
1398
|
+
});
|
|
1399
|
+
return {
|
|
1400
|
+
viewportRef,
|
|
1401
|
+
type,
|
|
1402
|
+
requestMeasure,
|
|
1403
|
+
getRootProps,
|
|
1404
|
+
getViewportProps,
|
|
1405
|
+
getScrollbarProps,
|
|
1406
|
+
getThumbProps,
|
|
1407
|
+
getThumbStyle,
|
|
1408
|
+
getCornerProps,
|
|
1409
|
+
isCornerVisible,
|
|
1410
|
+
getVisibility,
|
|
1411
|
+
onViewportScroll,
|
|
1412
|
+
onViewportPointerEnter,
|
|
1413
|
+
onViewportPointerLeave,
|
|
1414
|
+
onViewportFocusin,
|
|
1415
|
+
onViewportFocusout,
|
|
1416
|
+
onScrollbarPointerEnter,
|
|
1417
|
+
onScrollbarPointerLeave,
|
|
1418
|
+
onThumbPointerDown
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
// src/usePresence/usePresence.ts
|
|
1423
|
+
import { computed as computed14, onScopeDispose as onScopeDispose10, ref as ref9, toValue as toValue16, watch as watch8 } from "vue";
|
|
1424
|
+
import { isPresencePresent, sendPresenceEvent } from "@agile-plus/primitives/presence";
|
|
1425
|
+
function usePresence(options) {
|
|
1426
|
+
const state = ref9(toValue16(options.present) ? "mounted" : "unmounted");
|
|
1427
|
+
const element = ref9(null);
|
|
1428
|
+
const isPresent = computed14(() => isPresencePresent(state.value));
|
|
1429
|
+
let previousAnimationName = "none";
|
|
1430
|
+
let fillModeTimer = 0;
|
|
1431
|
+
function readStyles() {
|
|
1432
|
+
if (!element.value) return null;
|
|
1433
|
+
return element.value.ownerDocument.defaultView?.getComputedStyle(element.value) ?? null;
|
|
1434
|
+
}
|
|
1435
|
+
function readAnimationName() {
|
|
1436
|
+
return readStyles()?.animationName || "none";
|
|
1437
|
+
}
|
|
1438
|
+
function send(event) {
|
|
1439
|
+
const next = sendPresenceEvent(state.value, event);
|
|
1440
|
+
if (next === state.value) return;
|
|
1441
|
+
state.value = next;
|
|
1442
|
+
previousAnimationName = next === "mounted" ? readAnimationName() : "none";
|
|
1443
|
+
}
|
|
1444
|
+
function handleAnimationStart(event) {
|
|
1445
|
+
if (event.target !== element.value) return;
|
|
1446
|
+
previousAnimationName = readAnimationName();
|
|
1447
|
+
}
|
|
1448
|
+
function handleAnimationFinished(event) {
|
|
1449
|
+
const node = element.value;
|
|
1450
|
+
if (!node || event.target !== node) return;
|
|
1451
|
+
const currentAnimationName = readAnimationName();
|
|
1452
|
+
if (!currentAnimationName.includes(event.animationName)) return;
|
|
1453
|
+
send("ANIMATION_END");
|
|
1454
|
+
if (toValue16(options.present)) return;
|
|
1455
|
+
const previousFillMode = node.style.animationFillMode;
|
|
1456
|
+
node.style.animationFillMode = "forwards";
|
|
1457
|
+
fillModeTimer = window.setTimeout(() => {
|
|
1458
|
+
if (node.style.animationFillMode === "forwards") {
|
|
1459
|
+
node.style.animationFillMode = previousFillMode;
|
|
1460
|
+
}
|
|
1461
|
+
});
|
|
1462
|
+
}
|
|
1463
|
+
function setElement(node) {
|
|
1464
|
+
element.value = node instanceof HTMLElement ? node : null;
|
|
1465
|
+
}
|
|
1466
|
+
watch8(element, (node, previousNode) => {
|
|
1467
|
+
window.clearTimeout(fillModeTimer);
|
|
1468
|
+
previousNode?.removeEventListener("animationstart", handleAnimationStart);
|
|
1469
|
+
previousNode?.removeEventListener("animationcancel", handleAnimationFinished);
|
|
1470
|
+
previousNode?.removeEventListener("animationend", handleAnimationFinished);
|
|
1471
|
+
if (!node) {
|
|
1472
|
+
send("ANIMATION_END");
|
|
1473
|
+
return;
|
|
1474
|
+
}
|
|
1475
|
+
if (state.value === "mounted") previousAnimationName = readAnimationName();
|
|
1476
|
+
node.addEventListener("animationstart", handleAnimationStart);
|
|
1477
|
+
node.addEventListener("animationcancel", handleAnimationFinished);
|
|
1478
|
+
node.addEventListener("animationend", handleAnimationFinished);
|
|
1479
|
+
});
|
|
1480
|
+
watch8(
|
|
1481
|
+
() => toValue16(options.present),
|
|
1482
|
+
(present, wasPresent) => {
|
|
1483
|
+
if (present) {
|
|
1484
|
+
send("MOUNT");
|
|
1485
|
+
return;
|
|
1486
|
+
}
|
|
1487
|
+
const currentAnimationName = readAnimationName();
|
|
1488
|
+
const hasExitAnimation = currentAnimationName !== "none" && readStyles()?.display !== "none";
|
|
1489
|
+
const isAnimating = previousAnimationName !== currentAnimationName;
|
|
1490
|
+
if (!wasPresent || !hasExitAnimation || !isAnimating) {
|
|
1491
|
+
send("UNMOUNT");
|
|
1492
|
+
return;
|
|
1493
|
+
}
|
|
1494
|
+
send("ANIMATION_OUT");
|
|
1495
|
+
},
|
|
1496
|
+
{ flush: "post", immediate: true }
|
|
1497
|
+
);
|
|
1498
|
+
onScopeDispose10(() => {
|
|
1499
|
+
window.clearTimeout(fillModeTimer);
|
|
1500
|
+
const node = element.value;
|
|
1501
|
+
if (!node) return;
|
|
1502
|
+
node.removeEventListener("animationstart", handleAnimationStart);
|
|
1503
|
+
node.removeEventListener("animationcancel", handleAnimationFinished);
|
|
1504
|
+
node.removeEventListener("animationend", handleAnimationFinished);
|
|
1505
|
+
});
|
|
1506
|
+
return { isPresent, element, setElement };
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
// src/useToastProvider/useToastProvider.ts
|
|
1510
|
+
import { computed as computed15, ref as ref10, toValue as toValue17 } from "vue";
|
|
1511
|
+
import {
|
|
1512
|
+
DEFAULT_TOAST_DURATION,
|
|
1513
|
+
DEFAULT_TOAST_LABEL,
|
|
1514
|
+
DEFAULT_TOAST_SWIPE_DIRECTION,
|
|
1515
|
+
DEFAULT_TOAST_SWIPE_THRESHOLD
|
|
1516
|
+
} from "@agile-plus/primitives/toast";
|
|
1517
|
+
function useToastProvider(options = {}) {
|
|
1518
|
+
const label = computed15(() => toValue17(options.label) ?? DEFAULT_TOAST_LABEL);
|
|
1519
|
+
const duration = computed15(() => toValue17(options.duration) ?? DEFAULT_TOAST_DURATION);
|
|
1520
|
+
const swipeDirection = computed15(
|
|
1521
|
+
() => toValue17(options.swipeDirection) ?? DEFAULT_TOAST_SWIPE_DIRECTION
|
|
1522
|
+
);
|
|
1523
|
+
const swipeThreshold = computed15(
|
|
1524
|
+
() => toValue17(options.swipeThreshold) ?? DEFAULT_TOAST_SWIPE_THRESHOLD
|
|
1525
|
+
);
|
|
1526
|
+
const announceContainer = computed15(() => toValue17(options.announceContainer) ?? null);
|
|
1527
|
+
const collection = useCollection({ key: (entry) => entry.id });
|
|
1528
|
+
const toasts = collection.items;
|
|
1529
|
+
const toastCount = computed15(() => collection.items.value.length);
|
|
1530
|
+
const hasToasts = computed15(() => collection.items.value.length > 0);
|
|
1531
|
+
const viewport = ref10(null);
|
|
1532
|
+
const isPaused = ref10(false);
|
|
1533
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
1534
|
+
if (!label.value.trim()) {
|
|
1535
|
+
console.error("Invalid prop `label` supplied to `ToastProvider`. Expected non-empty `string`.");
|
|
1536
|
+
}
|
|
1537
|
+
function setViewport(node) {
|
|
1538
|
+
viewport.value = node;
|
|
1539
|
+
}
|
|
1540
|
+
function setPaused(next) {
|
|
1541
|
+
if (isPaused.value === next) return;
|
|
1542
|
+
isPaused.value = next;
|
|
1543
|
+
listeners.forEach((listener) => listener(next));
|
|
1544
|
+
}
|
|
1545
|
+
function pauseClosing() {
|
|
1546
|
+
setPaused(true);
|
|
1547
|
+
}
|
|
1548
|
+
function resumeClosing() {
|
|
1549
|
+
setPaused(false);
|
|
1550
|
+
}
|
|
1551
|
+
function subscribeClosing(listener) {
|
|
1552
|
+
listeners.add(listener);
|
|
1553
|
+
return () => {
|
|
1554
|
+
listeners.delete(listener);
|
|
1555
|
+
};
|
|
1556
|
+
}
|
|
1557
|
+
return {
|
|
1558
|
+
label,
|
|
1559
|
+
duration,
|
|
1560
|
+
swipeDirection,
|
|
1561
|
+
swipeThreshold,
|
|
1562
|
+
announceContainer,
|
|
1563
|
+
toasts,
|
|
1564
|
+
toastCount,
|
|
1565
|
+
hasToasts,
|
|
1566
|
+
viewport,
|
|
1567
|
+
setViewport,
|
|
1568
|
+
registerToast: collection.add,
|
|
1569
|
+
isClosePaused: computed15(() => isPaused.value),
|
|
1570
|
+
pauseClosing,
|
|
1571
|
+
resumeClosing,
|
|
1572
|
+
subscribeClosing
|
|
1573
|
+
};
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
// src/useToastViewport/useToastViewport.ts
|
|
1577
|
+
import { computed as computed16, onMounted, onScopeDispose as onScopeDispose11, ref as ref11, toValue as toValue18, watch as watch9 } from "vue";
|
|
1578
|
+
import {
|
|
1579
|
+
DEFAULT_TOAST_VIEWPORT_HOTKEY,
|
|
1580
|
+
DEFAULT_TOAST_VIEWPORT_LABEL,
|
|
1581
|
+
VISUALLY_HIDDEN_STYLE,
|
|
1582
|
+
formatToastHotkey,
|
|
1583
|
+
isToastHotkeyPressed,
|
|
1584
|
+
resolveToastViewportLabel
|
|
1585
|
+
} from "@agile-plus/primitives/toast";
|
|
1586
|
+
import { focusFirst, getTabbableCandidates as getTabbableCandidates2 } from "@agile-plus/primitives/dom";
|
|
1587
|
+
function asElement(node) {
|
|
1588
|
+
return node instanceof HTMLElement ? node : null;
|
|
1589
|
+
}
|
|
1590
|
+
function useToastViewport(options) {
|
|
1591
|
+
const { provider } = options;
|
|
1592
|
+
const wrapper = ref11(null);
|
|
1593
|
+
const listElement = ref11(null);
|
|
1594
|
+
const headProxy = ref11(null);
|
|
1595
|
+
const tailProxy = ref11(null);
|
|
1596
|
+
const hotkey = computed16(() => toValue18(options.hotkey) ?? DEFAULT_TOAST_VIEWPORT_HOTKEY);
|
|
1597
|
+
const hotkeyLabel = computed16(() => formatToastHotkey(hotkey.value));
|
|
1598
|
+
const ariaLabel = computed16(
|
|
1599
|
+
() => resolveToastViewportLabel(toValue18(options.label) ?? DEFAULT_TOAST_VIEWPORT_LABEL, hotkey.value)
|
|
1600
|
+
);
|
|
1601
|
+
function getSortedCandidates(direction) {
|
|
1602
|
+
const perToast = provider.toasts.value.map((entry) => {
|
|
1603
|
+
const node = entry.element.value;
|
|
1604
|
+
if (!node) return [];
|
|
1605
|
+
const candidates = [node, ...getTabbableCandidates2(node)];
|
|
1606
|
+
return direction === "forwards" ? candidates : candidates.reverse();
|
|
1607
|
+
});
|
|
1608
|
+
const ordered = direction === "forwards" ? perToast.reverse() : perToast;
|
|
1609
|
+
return ordered.flat();
|
|
1610
|
+
}
|
|
1611
|
+
function focusProxy(position) {
|
|
1612
|
+
(position === "head" ? headProxy.value : tailProxy.value)?.focus();
|
|
1613
|
+
}
|
|
1614
|
+
function handleProxyFocus(event, position) {
|
|
1615
|
+
const previous = event.relatedTarget;
|
|
1616
|
+
if (previous instanceof Node && listElement.value?.contains(previous)) return;
|
|
1617
|
+
focusFirst(getSortedCandidates(position === "head" ? "forwards" : "backwards"));
|
|
1618
|
+
}
|
|
1619
|
+
function handleEscape(event) {
|
|
1620
|
+
if (!provider.hasToasts.value) return;
|
|
1621
|
+
const activeElement = document.activeElement;
|
|
1622
|
+
const isFocusInsideToast = provider.toasts.value.some(
|
|
1623
|
+
(entry) => entry.element.value?.contains(activeElement)
|
|
1624
|
+
);
|
|
1625
|
+
if (isFocusInsideToast) return;
|
|
1626
|
+
const modal = document.querySelector('[aria-modal="true"]');
|
|
1627
|
+
if (modal?.contains(activeElement)) return;
|
|
1628
|
+
const topmost = provider.toasts.value.at(-1);
|
|
1629
|
+
if (!topmost) return;
|
|
1630
|
+
event.preventDefault();
|
|
1631
|
+
topmost.close();
|
|
1632
|
+
}
|
|
1633
|
+
function handleDocumentKeydown(event) {
|
|
1634
|
+
if (isToastHotkeyPressed(event, hotkey.value)) {
|
|
1635
|
+
listElement.value?.focus();
|
|
1636
|
+
return;
|
|
1637
|
+
}
|
|
1638
|
+
if (event.key === "Escape") handleEscape(event);
|
|
1639
|
+
}
|
|
1640
|
+
function handleWindowBlur() {
|
|
1641
|
+
provider.pauseClosing();
|
|
1642
|
+
}
|
|
1643
|
+
function handleWindowFocus() {
|
|
1644
|
+
provider.resumeClosing();
|
|
1645
|
+
}
|
|
1646
|
+
let isWindowListening = false;
|
|
1647
|
+
watch9(
|
|
1648
|
+
provider.hasToasts,
|
|
1649
|
+
(hasToasts) => {
|
|
1650
|
+
if (!isBrowser || hasToasts && isWindowListening) return;
|
|
1651
|
+
if (hasToasts) {
|
|
1652
|
+
window.addEventListener("blur", handleWindowBlur);
|
|
1653
|
+
window.addEventListener("focus", handleWindowFocus);
|
|
1654
|
+
isWindowListening = true;
|
|
1655
|
+
return;
|
|
1656
|
+
}
|
|
1657
|
+
window.removeEventListener("blur", handleWindowBlur);
|
|
1658
|
+
window.removeEventListener("focus", handleWindowFocus);
|
|
1659
|
+
isWindowListening = false;
|
|
1660
|
+
},
|
|
1661
|
+
{ immediate: true }
|
|
1662
|
+
);
|
|
1663
|
+
onMounted(() => {
|
|
1664
|
+
if (!isBrowser) return;
|
|
1665
|
+
document.addEventListener("keydown", handleDocumentKeydown);
|
|
1666
|
+
});
|
|
1667
|
+
onScopeDispose11(() => {
|
|
1668
|
+
if (!isBrowser) return;
|
|
1669
|
+
document.removeEventListener("keydown", handleDocumentKeydown);
|
|
1670
|
+
window.removeEventListener("blur", handleWindowBlur);
|
|
1671
|
+
window.removeEventListener("focus", handleWindowFocus);
|
|
1672
|
+
isWindowListening = false;
|
|
1673
|
+
});
|
|
1674
|
+
function bindWrapper(node) {
|
|
1675
|
+
wrapper.value = asElement(node);
|
|
1676
|
+
}
|
|
1677
|
+
function bindList(node) {
|
|
1678
|
+
listElement.value = asElement(node);
|
|
1679
|
+
provider.setViewport(listElement.value);
|
|
1680
|
+
}
|
|
1681
|
+
function bindHeadProxy(node) {
|
|
1682
|
+
headProxy.value = asElement(node);
|
|
1683
|
+
}
|
|
1684
|
+
function bindTailProxy(node) {
|
|
1685
|
+
tailProxy.value = asElement(node);
|
|
1686
|
+
}
|
|
1687
|
+
function getWrapperProps() {
|
|
1688
|
+
return {
|
|
1689
|
+
role: "region",
|
|
1690
|
+
"aria-label": ariaLabel.value,
|
|
1691
|
+
// 列表本身可聚焦:移除条目后焦点要有落脚点,读屏才能播报剩余数量。
|
|
1692
|
+
tabindex: -1,
|
|
1693
|
+
// 空列表也可能有 padding,不关掉 pointer-events 会挡住它覆盖的页面交互。
|
|
1694
|
+
style: { pointerEvents: provider.hasToasts.value ? "auto" : "none" }
|
|
1695
|
+
};
|
|
1696
|
+
}
|
|
1697
|
+
function getListProps() {
|
|
1698
|
+
return { tabindex: -1 };
|
|
1699
|
+
}
|
|
1700
|
+
function getProxyProps(position) {
|
|
1701
|
+
return {
|
|
1702
|
+
tabindex: 0,
|
|
1703
|
+
// position: fixed 防止 Tab 到代理时页面滚动。
|
|
1704
|
+
style: { ...VISUALLY_HIDDEN_STYLE, position: "fixed" },
|
|
1705
|
+
"data-toast-focus-proxy": position
|
|
1706
|
+
};
|
|
1707
|
+
}
|
|
1708
|
+
function onWrapperFocusin() {
|
|
1709
|
+
if (!provider.hasToasts.value) return;
|
|
1710
|
+
provider.pauseClosing();
|
|
1711
|
+
}
|
|
1712
|
+
function onWrapperFocusout(event) {
|
|
1713
|
+
const isMovingOutside = !(event.relatedTarget instanceof Node) || !wrapper.value?.contains(event.relatedTarget);
|
|
1714
|
+
if (isMovingOutside) provider.resumeClosing();
|
|
1715
|
+
}
|
|
1716
|
+
function onWrapperPointermove() {
|
|
1717
|
+
if (!provider.hasToasts.value) return;
|
|
1718
|
+
provider.pauseClosing();
|
|
1719
|
+
}
|
|
1720
|
+
function onWrapperPointerleave() {
|
|
1721
|
+
if (!wrapper.value) return;
|
|
1722
|
+
const isFocusInside = wrapper.value.contains(document.activeElement);
|
|
1723
|
+
if (!isFocusInside) provider.resumeClosing();
|
|
1724
|
+
}
|
|
1725
|
+
function onListKeydown(event) {
|
|
1726
|
+
const list = listElement.value;
|
|
1727
|
+
if (!list) return;
|
|
1728
|
+
const isMetaKey = event.altKey || event.ctrlKey || event.metaKey;
|
|
1729
|
+
if (event.key !== "Tab" || isMetaKey) return;
|
|
1730
|
+
const isTabbingBackwards = event.shiftKey;
|
|
1731
|
+
if (event.target === list && isTabbingBackwards) {
|
|
1732
|
+
focusProxy("head");
|
|
1733
|
+
return;
|
|
1734
|
+
}
|
|
1735
|
+
const direction = isTabbingBackwards ? "backwards" : "forwards";
|
|
1736
|
+
const sorted = getSortedCandidates(direction);
|
|
1737
|
+
const focusedElement = document.activeElement;
|
|
1738
|
+
const index = sorted.findIndex((candidate) => candidate === focusedElement);
|
|
1739
|
+
if (focusFirst(sorted.slice(index + 1))) {
|
|
1740
|
+
event.preventDefault();
|
|
1741
|
+
return;
|
|
1742
|
+
}
|
|
1743
|
+
focusProxy(isTabbingBackwards ? "head" : "tail");
|
|
1744
|
+
}
|
|
1745
|
+
function onHeadProxyFocus(event) {
|
|
1746
|
+
handleProxyFocus(event, "head");
|
|
1747
|
+
}
|
|
1748
|
+
function onTailProxyFocus(event) {
|
|
1749
|
+
handleProxyFocus(event, "tail");
|
|
1750
|
+
}
|
|
1751
|
+
return {
|
|
1752
|
+
hasToasts: provider.hasToasts,
|
|
1753
|
+
toastCount: provider.toastCount,
|
|
1754
|
+
ariaLabel,
|
|
1755
|
+
hotkeyLabel,
|
|
1756
|
+
listElement,
|
|
1757
|
+
bindWrapper,
|
|
1758
|
+
bindList,
|
|
1759
|
+
bindHeadProxy,
|
|
1760
|
+
bindTailProxy,
|
|
1761
|
+
getWrapperProps,
|
|
1762
|
+
getListProps,
|
|
1763
|
+
getHeadProxyProps: () => getProxyProps("head"),
|
|
1764
|
+
getTailProxyProps: () => getProxyProps("tail"),
|
|
1765
|
+
onWrapperFocusin,
|
|
1766
|
+
onWrapperFocusout,
|
|
1767
|
+
onWrapperPointermove,
|
|
1768
|
+
onWrapperPointerleave,
|
|
1769
|
+
onListKeydown,
|
|
1770
|
+
onHeadProxyFocus,
|
|
1771
|
+
onTailProxyFocus
|
|
1772
|
+
};
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
// src/useToast/useToast.ts
|
|
1776
|
+
import { computed as computed17, onMounted as onMounted2, onScopeDispose as onScopeDispose12, ref as ref12, toValue as toValue19, useId as useId6, watch as watch10 } from "vue";
|
|
1777
|
+
import {
|
|
1778
|
+
TOAST_ANNOUNCE_LIFETIME,
|
|
1779
|
+
VISUALLY_HIDDEN_STYLE as VISUALLY_HIDDEN_STYLE2,
|
|
1780
|
+
createToastTimerSnapshot,
|
|
1781
|
+
getSwipeEndPhase,
|
|
1782
|
+
getToastAnnounceTextContent,
|
|
1783
|
+
isToastTimerDisabled,
|
|
1784
|
+
pauseToastTimer,
|
|
1785
|
+
resolveSwipeMove,
|
|
1786
|
+
resumeToastTimer
|
|
1787
|
+
} from "@agile-plus/primitives/toast";
|
|
1788
|
+
var SWIPE_VARS = {
|
|
1789
|
+
moveX: "--y-toast-swipe-move-x",
|
|
1790
|
+
moveY: "--y-toast-swipe-move-y",
|
|
1791
|
+
endX: "--y-toast-swipe-end-x",
|
|
1792
|
+
endY: "--y-toast-swipe-end-y"
|
|
1793
|
+
};
|
|
1794
|
+
function useToast(options) {
|
|
1795
|
+
const { provider } = options;
|
|
1796
|
+
const id = useId6();
|
|
1797
|
+
const state = useControllable({
|
|
1798
|
+
value: () => toValue19(options.open),
|
|
1799
|
+
defaultValue: options.defaultOpen ?? true,
|
|
1800
|
+
onChange: (value) => options.onOpenChange?.(value)
|
|
1801
|
+
});
|
|
1802
|
+
const isOpen = state.value;
|
|
1803
|
+
const duration = computed17(() => toValue19(options.duration) ?? provider.duration.value);
|
|
1804
|
+
const presence = usePresence({
|
|
1805
|
+
present: () => Boolean(toValue19(options.forceMount)) || isOpen.value
|
|
1806
|
+
});
|
|
1807
|
+
const swipePhase = ref12(null);
|
|
1808
|
+
const swipeMove = ref12(null);
|
|
1809
|
+
const swipeEnd = ref12(null);
|
|
1810
|
+
let pointerStart = null;
|
|
1811
|
+
let gestureDelta = null;
|
|
1812
|
+
let timerId = 0;
|
|
1813
|
+
let snapshot = createToastTimerSnapshot(duration.value, isBrowser ? Date.now() : 0);
|
|
1814
|
+
function setOpen(value) {
|
|
1815
|
+
state.set(value);
|
|
1816
|
+
}
|
|
1817
|
+
function close() {
|
|
1818
|
+
setOpen(false);
|
|
1819
|
+
}
|
|
1820
|
+
function handleClose() {
|
|
1821
|
+
const isFocusInside = presence.element.value?.contains(document.activeElement);
|
|
1822
|
+
if (isFocusInside) provider.viewport.value?.focus();
|
|
1823
|
+
close();
|
|
1824
|
+
}
|
|
1825
|
+
function startTimer(delay) {
|
|
1826
|
+
if (isToastTimerDisabled(delay)) return;
|
|
1827
|
+
window.clearTimeout(timerId);
|
|
1828
|
+
snapshot = createToastTimerSnapshot(delay, Date.now());
|
|
1829
|
+
timerId = window.setTimeout(handleClose, delay);
|
|
1830
|
+
}
|
|
1831
|
+
function pauseTimer() {
|
|
1832
|
+
snapshot = pauseToastTimer(snapshot, Date.now());
|
|
1833
|
+
window.clearTimeout(timerId);
|
|
1834
|
+
options.onPause?.();
|
|
1835
|
+
}
|
|
1836
|
+
function resumeTimer() {
|
|
1837
|
+
snapshot = resumeToastTimer(snapshot, Date.now());
|
|
1838
|
+
startTimer(snapshot.remaining);
|
|
1839
|
+
options.onResume?.();
|
|
1840
|
+
}
|
|
1841
|
+
const unsubscribeClosing = provider.subscribeClosing((paused) => {
|
|
1842
|
+
if (!isOpen.value) return;
|
|
1843
|
+
if (paused) pauseTimer();
|
|
1844
|
+
else resumeTimer();
|
|
1845
|
+
});
|
|
1846
|
+
watch10(
|
|
1847
|
+
[isOpen, duration],
|
|
1848
|
+
([open, nextDuration]) => {
|
|
1849
|
+
snapshot = createToastTimerSnapshot(nextDuration, Date.now());
|
|
1850
|
+
if (!isBrowser) return;
|
|
1851
|
+
if (open && !provider.isClosePaused.value) startTimer(nextDuration);
|
|
1852
|
+
else window.clearTimeout(timerId);
|
|
1853
|
+
},
|
|
1854
|
+
{ immediate: true }
|
|
1855
|
+
);
|
|
1856
|
+
let unregister = null;
|
|
1857
|
+
watch10(
|
|
1858
|
+
presence.isPresent,
|
|
1859
|
+
(present) => {
|
|
1860
|
+
if (present) unregister = provider.registerToast({ id, element: presence.element, close });
|
|
1861
|
+
else {
|
|
1862
|
+
unregister?.();
|
|
1863
|
+
unregister = null;
|
|
1864
|
+
}
|
|
1865
|
+
},
|
|
1866
|
+
{ immediate: true }
|
|
1867
|
+
);
|
|
1868
|
+
const announcePhase = ref12("pending");
|
|
1869
|
+
let nextFrameId = 0;
|
|
1870
|
+
let lifetimeId = 0;
|
|
1871
|
+
onMounted2(() => {
|
|
1872
|
+
if (!isBrowser) return;
|
|
1873
|
+
nextFrameId = window.requestAnimationFrame(() => {
|
|
1874
|
+
nextFrameId = window.requestAnimationFrame(() => {
|
|
1875
|
+
announcePhase.value = "ready";
|
|
1876
|
+
lifetimeId = window.setTimeout(() => {
|
|
1877
|
+
announcePhase.value = "expired";
|
|
1878
|
+
}, TOAST_ANNOUNCE_LIFETIME);
|
|
1879
|
+
});
|
|
1880
|
+
});
|
|
1881
|
+
});
|
|
1882
|
+
const announceText = computed17(() => {
|
|
1883
|
+
if (announcePhase.value !== "ready" || !presence.element.value) return null;
|
|
1884
|
+
return getToastAnnounceTextContent(presence.element.value);
|
|
1885
|
+
});
|
|
1886
|
+
const ariaLive = computed17(
|
|
1887
|
+
() => (toValue19(options.type) ?? "foreground") === "foreground" ? "assertive" : "polite"
|
|
1888
|
+
);
|
|
1889
|
+
function emitSwipe(phase, detail) {
|
|
1890
|
+
swipePhase.value = phase;
|
|
1891
|
+
if (phase === "start") {
|
|
1892
|
+
options.onSwipeStart?.(detail);
|
|
1893
|
+
return;
|
|
1894
|
+
}
|
|
1895
|
+
if (phase === "move") options.onSwipeMove?.(detail);
|
|
1896
|
+
if (phase === "cancel") options.onSwipeCancel?.(detail);
|
|
1897
|
+
if (phase === "end") options.onSwipeEnd?.(detail);
|
|
1898
|
+
}
|
|
1899
|
+
function onRootKeydown(event) {
|
|
1900
|
+
if (event.key !== "Escape") return;
|
|
1901
|
+
options.onEscapeKeyDown?.(event);
|
|
1902
|
+
if (!event.defaultPrevented) close();
|
|
1903
|
+
}
|
|
1904
|
+
function onRootPointerdown(event) {
|
|
1905
|
+
if (event.button !== 0) return;
|
|
1906
|
+
pointerStart = { x: event.clientX, y: event.clientY };
|
|
1907
|
+
}
|
|
1908
|
+
function onRootPointermove(event) {
|
|
1909
|
+
if (!pointerStart) return;
|
|
1910
|
+
const result = resolveSwipeMove({
|
|
1911
|
+
delta: { x: event.clientX - pointerStart.x, y: event.clientY - pointerStart.y },
|
|
1912
|
+
direction: provider.swipeDirection.value,
|
|
1913
|
+
pointerType: event.pointerType,
|
|
1914
|
+
started: gestureDelta !== null
|
|
1915
|
+
});
|
|
1916
|
+
if (result.phase === "idle") return;
|
|
1917
|
+
if (result.phase === "reset") {
|
|
1918
|
+
pointerStart = null;
|
|
1919
|
+
return;
|
|
1920
|
+
}
|
|
1921
|
+
const detail = { originalEvent: event, delta: result.clamped };
|
|
1922
|
+
gestureDelta = result.clamped;
|
|
1923
|
+
if (result.phase === "start") {
|
|
1924
|
+
emitSwipe("start", detail);
|
|
1925
|
+
if (event.target instanceof HTMLElement) event.target.setPointerCapture(event.pointerId);
|
|
1926
|
+
return;
|
|
1927
|
+
}
|
|
1928
|
+
swipeMove.value = result.clamped;
|
|
1929
|
+
emitSwipe("move", detail);
|
|
1930
|
+
}
|
|
1931
|
+
function onRootPointerup(event) {
|
|
1932
|
+
const delta = gestureDelta;
|
|
1933
|
+
const target = event.target instanceof HTMLElement ? event.target : null;
|
|
1934
|
+
if (target?.hasPointerCapture(event.pointerId)) target.releasePointerCapture(event.pointerId);
|
|
1935
|
+
gestureDelta = null;
|
|
1936
|
+
pointerStart = null;
|
|
1937
|
+
if (!delta) return;
|
|
1938
|
+
const detail = { originalEvent: event, delta };
|
|
1939
|
+
const phase = getSwipeEndPhase(
|
|
1940
|
+
delta,
|
|
1941
|
+
provider.swipeDirection.value,
|
|
1942
|
+
provider.swipeThreshold.value
|
|
1943
|
+
);
|
|
1944
|
+
if (phase === "end") {
|
|
1945
|
+
swipeMove.value = null;
|
|
1946
|
+
swipeEnd.value = delta;
|
|
1947
|
+
emitSwipe("end", detail);
|
|
1948
|
+
close();
|
|
1949
|
+
} else {
|
|
1950
|
+
swipeMove.value = null;
|
|
1951
|
+
swipeEnd.value = null;
|
|
1952
|
+
emitSwipe("cancel", detail);
|
|
1953
|
+
}
|
|
1954
|
+
if (event.currentTarget instanceof HTMLElement) {
|
|
1955
|
+
event.currentTarget.addEventListener("click", (clickEvent) => clickEvent.preventDefault(), {
|
|
1956
|
+
once: true
|
|
1957
|
+
});
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
function getRootProps() {
|
|
1961
|
+
return {
|
|
1962
|
+
tabindex: 0,
|
|
1963
|
+
"data-state": isOpen.value ? "open" : "closed",
|
|
1964
|
+
"data-swipe-direction": provider.swipeDirection.value,
|
|
1965
|
+
...swipePhase.value ? { "data-swipe": swipePhase.value } : {},
|
|
1966
|
+
style: {
|
|
1967
|
+
// 内联而不是写在样式里:滑动手势必须生效,而不依赖消费者有没有引入样式表。
|
|
1968
|
+
userSelect: "none",
|
|
1969
|
+
touchAction: "none",
|
|
1970
|
+
...swipeMove.value ? { [SWIPE_VARS.moveX]: `${swipeMove.value.x}px` } : null,
|
|
1971
|
+
...swipeMove.value ? { [SWIPE_VARS.moveY]: `${swipeMove.value.y}px` } : null,
|
|
1972
|
+
...swipeEnd.value ? { [SWIPE_VARS.endX]: `${swipeEnd.value.x}px` } : null,
|
|
1973
|
+
...swipeEnd.value ? { [SWIPE_VARS.endY]: `${swipeEnd.value.y}px` } : null
|
|
1974
|
+
}
|
|
1975
|
+
};
|
|
1976
|
+
}
|
|
1977
|
+
function getAnnounceProps() {
|
|
1978
|
+
return {
|
|
1979
|
+
// 永远用 status 而不是 alert:alert 在部分读屏里会造成逐字卡顿。
|
|
1980
|
+
role: "status",
|
|
1981
|
+
"aria-live": ariaLive.value,
|
|
1982
|
+
style: VISUALLY_HIDDEN_STYLE2
|
|
1983
|
+
};
|
|
1984
|
+
}
|
|
1985
|
+
function bindElement(node) {
|
|
1986
|
+
presence.setElement(node);
|
|
1987
|
+
}
|
|
1988
|
+
onScopeDispose12(() => {
|
|
1989
|
+
unsubscribeClosing();
|
|
1990
|
+
unregister?.();
|
|
1991
|
+
if (!isBrowser) return;
|
|
1992
|
+
window.clearTimeout(timerId);
|
|
1993
|
+
window.clearTimeout(lifetimeId);
|
|
1994
|
+
if (nextFrameId) window.cancelAnimationFrame(nextFrameId);
|
|
1995
|
+
});
|
|
1996
|
+
return {
|
|
1997
|
+
isOpen,
|
|
1998
|
+
isPresent: presence.isPresent,
|
|
1999
|
+
element: presence.element,
|
|
2000
|
+
bindElement,
|
|
2001
|
+
close,
|
|
2002
|
+
swipePhase: computed17(() => swipePhase.value),
|
|
2003
|
+
swipeDirection: provider.swipeDirection,
|
|
2004
|
+
getRootProps,
|
|
2005
|
+
isAnnounceVisible: computed17(() => announcePhase.value !== "expired"),
|
|
2006
|
+
announceText,
|
|
2007
|
+
announceLabel: provider.label,
|
|
2008
|
+
announceTarget: computed17(() => provider.announceContainer.value ?? "body"),
|
|
2009
|
+
getAnnounceProps,
|
|
2010
|
+
onRootKeydown,
|
|
2011
|
+
onRootPointerdown,
|
|
2012
|
+
onRootPointermove,
|
|
2013
|
+
onRootPointerup
|
|
2014
|
+
};
|
|
2015
|
+
}
|
|
2016
|
+
|
|
2017
|
+
// src/useToggle/useToggle.ts
|
|
2018
|
+
import { computed as computed18, toValue as toValue20 } from "vue";
|
|
2019
|
+
import { getToggleDataState, resolveNextTogglePressed } from "@agile-plus/primitives/toggle";
|
|
2020
|
+
function useToggle(options = {}) {
|
|
2021
|
+
const state = useControllable({
|
|
2022
|
+
value: () => toValue20(options.pressed),
|
|
2023
|
+
defaultValue: options.defaultPressed ?? false,
|
|
2024
|
+
onChange: (value) => options.onPressedChange?.(value)
|
|
2025
|
+
});
|
|
2026
|
+
const pressed = state.value;
|
|
2027
|
+
const disabled = computed18(() => toValue20(options.disabled) ?? false);
|
|
2028
|
+
function setPressed(value) {
|
|
2029
|
+
state.set(value);
|
|
2030
|
+
}
|
|
2031
|
+
function toggle() {
|
|
2032
|
+
setPressed(!pressed.value);
|
|
2033
|
+
}
|
|
2034
|
+
function onClick() {
|
|
2035
|
+
const next = resolveNextTogglePressed({ pressed: pressed.value, disabled: disabled.value });
|
|
2036
|
+
if (next !== null) setPressed(next);
|
|
2037
|
+
}
|
|
2038
|
+
function getRootProps() {
|
|
2039
|
+
return {
|
|
2040
|
+
type: "button",
|
|
2041
|
+
// aria-pressed 始终输出:开关的存在本身要靠它表达,缺席会被读成普通按钮。
|
|
2042
|
+
"aria-pressed": pressed.value,
|
|
2043
|
+
"data-state": getToggleDataState(pressed.value),
|
|
2044
|
+
...disabled.value ? { disabled: true, "data-disabled": "" } : null
|
|
2045
|
+
};
|
|
2046
|
+
}
|
|
2047
|
+
return { pressed, disabled, setPressed, toggle, getRootProps, onClick };
|
|
2048
|
+
}
|
|
2049
|
+
|
|
2050
|
+
// src/useToggleGroup/useToggleGroup.ts
|
|
2051
|
+
import { computed as computed19, ref as ref13, toValue as toValue21 } from "vue";
|
|
2052
|
+
import {
|
|
2053
|
+
isToggleGroupItemPressed,
|
|
2054
|
+
normalizeToggleGroupValue,
|
|
2055
|
+
resolveGroupValueAfterToggle,
|
|
2056
|
+
resolveRovingFocusIntent,
|
|
2057
|
+
resolveRovingFocusTargetIndex,
|
|
2058
|
+
serializeToggleGroupValue
|
|
2059
|
+
} from "@agile-plus/primitives/toggle";
|
|
2060
|
+
import { focusFirst as focusFirst2 } from "@agile-plus/primitives/dom";
|
|
2061
|
+
var VALID_TYPES = ["single", "multiple"];
|
|
2062
|
+
function useToggleGroup(options) {
|
|
2063
|
+
const type = computed19(() => {
|
|
2064
|
+
const value2 = toValue21(options.type);
|
|
2065
|
+
if (!VALID_TYPES.includes(value2)) {
|
|
2066
|
+
throw new Error(
|
|
2067
|
+
"Invalid prop `type` expected on `ToggleGroup`. Expected `single` or `multiple`."
|
|
2068
|
+
);
|
|
2069
|
+
}
|
|
2070
|
+
return value2;
|
|
2071
|
+
});
|
|
2072
|
+
const state = useControllable({
|
|
2073
|
+
value: () => {
|
|
2074
|
+
const raw = toValue21(options.value);
|
|
2075
|
+
return raw === void 0 ? void 0 : normalizeToggleGroupValue(type.value, raw);
|
|
2076
|
+
},
|
|
2077
|
+
defaultValue: normalizeToggleGroupValue(type.value, toValue21(options.defaultValue)),
|
|
2078
|
+
onChange: (next) => options.onValueChange?.(serializeToggleGroupValue(type.value, next))
|
|
2079
|
+
});
|
|
2080
|
+
const value = state.value;
|
|
2081
|
+
const disabled = computed19(() => toValue21(options.disabled) ?? false);
|
|
2082
|
+
const rovingFocus = computed19(() => toValue21(options.rovingFocus) ?? true);
|
|
2083
|
+
const loop = computed19(() => toValue21(options.loop) ?? true);
|
|
2084
|
+
const orientation = computed19(() => toValue21(options.orientation));
|
|
2085
|
+
const dir = computed19(() => toValue21(options.dir) ?? "ltr");
|
|
2086
|
+
const collection = useCollection({ key: (entry) => entry.id });
|
|
2087
|
+
const currentTabStopId = ref13(null);
|
|
2088
|
+
const isTabbingBackOut = ref13(false);
|
|
2089
|
+
let isClickFocus = false;
|
|
2090
|
+
const focusableEntries = computed19(
|
|
2091
|
+
() => collection.items.value.filter((entry) => entry.focusable.value)
|
|
2092
|
+
);
|
|
2093
|
+
function findEntry(id) {
|
|
2094
|
+
return collection.items.value.find((entry) => entry.id === id);
|
|
2095
|
+
}
|
|
2096
|
+
function isItemPressed(itemValue) {
|
|
2097
|
+
return isToggleGroupItemPressed(value.value, itemValue);
|
|
2098
|
+
}
|
|
2099
|
+
function emitValue(next) {
|
|
2100
|
+
state.set(next);
|
|
2101
|
+
}
|
|
2102
|
+
function setItemPressed(itemValue, pressed) {
|
|
2103
|
+
emitValue(
|
|
2104
|
+
resolveGroupValueAfterToggle({ type: type.value, value: value.value, itemValue, pressed })
|
|
2105
|
+
);
|
|
2106
|
+
}
|
|
2107
|
+
function handleEntryFocus() {
|
|
2108
|
+
const candidates = focusableEntries.value;
|
|
2109
|
+
if (candidates.length === 0) return;
|
|
2110
|
+
const active = candidates.find((entry) => entry.active.value);
|
|
2111
|
+
const current = candidates.find((entry) => entry.id === currentTabStopId.value);
|
|
2112
|
+
const ordered = [active, current, ...candidates].filter((entry) => Boolean(entry)).map((entry) => entry.element.value).filter((element) => Boolean(element));
|
|
2113
|
+
focusFirst2(ordered);
|
|
2114
|
+
}
|
|
2115
|
+
function getGroupProps() {
|
|
2116
|
+
return {
|
|
2117
|
+
role: type.value === "single" ? "radiogroup" : "toolbar",
|
|
2118
|
+
dir: dir.value,
|
|
2119
|
+
...rovingFocus.value ? {
|
|
2120
|
+
// 整组只占一个 Tab 停靠点;没有可聚焦项时连这一格也不留。
|
|
2121
|
+
tabindex: isTabbingBackOut.value || focusableEntries.value.length === 0 ? -1 : 0,
|
|
2122
|
+
"data-orientation": orientation.value,
|
|
2123
|
+
style: { outline: "none" }
|
|
2124
|
+
} : null
|
|
2125
|
+
};
|
|
2126
|
+
}
|
|
2127
|
+
function getItemProps(id) {
|
|
2128
|
+
if (!rovingFocus.value) return {};
|
|
2129
|
+
return {
|
|
2130
|
+
tabindex: currentTabStopId.value === id ? 0 : -1,
|
|
2131
|
+
"data-orientation": orientation.value
|
|
2132
|
+
};
|
|
2133
|
+
}
|
|
2134
|
+
function onGroupFocus(event) {
|
|
2135
|
+
if (!rovingFocus.value) return;
|
|
2136
|
+
const isSelfFocus = event.target === event.currentTarget;
|
|
2137
|
+
const isKeyboardFocus = !isClickFocus;
|
|
2138
|
+
if (isSelfFocus && isKeyboardFocus && !isTabbingBackOut.value) handleEntryFocus();
|
|
2139
|
+
isClickFocus = false;
|
|
2140
|
+
}
|
|
2141
|
+
function onGroupMousedown() {
|
|
2142
|
+
isClickFocus = true;
|
|
2143
|
+
}
|
|
2144
|
+
function onGroupBlur() {
|
|
2145
|
+
isTabbingBackOut.value = false;
|
|
2146
|
+
}
|
|
2147
|
+
function onItemFocus(id) {
|
|
2148
|
+
if (!rovingFocus.value) return;
|
|
2149
|
+
currentTabStopId.value = id;
|
|
2150
|
+
}
|
|
2151
|
+
function onItemMousedown(event, id) {
|
|
2152
|
+
if (!rovingFocus.value) return;
|
|
2153
|
+
const entry = findEntry(id);
|
|
2154
|
+
if (!entry) return;
|
|
2155
|
+
if (!entry.focusable.value) {
|
|
2156
|
+
event.preventDefault();
|
|
2157
|
+
return;
|
|
2158
|
+
}
|
|
2159
|
+
currentTabStopId.value = id;
|
|
2160
|
+
}
|
|
2161
|
+
function onItemKeydown(event) {
|
|
2162
|
+
if (!rovingFocus.value) return;
|
|
2163
|
+
if (event.key === "Tab" && event.shiftKey) {
|
|
2164
|
+
isTabbingBackOut.value = true;
|
|
2165
|
+
return;
|
|
2166
|
+
}
|
|
2167
|
+
if (event.target !== event.currentTarget) return;
|
|
2168
|
+
const intent = resolveRovingFocusIntent({
|
|
2169
|
+
key: event.key,
|
|
2170
|
+
orientation: orientation.value,
|
|
2171
|
+
dir: dir.value,
|
|
2172
|
+
hasModifierKey: event.shiftKey || event.ctrlKey || event.altKey || event.metaKey
|
|
2173
|
+
});
|
|
2174
|
+
if (!intent) return;
|
|
2175
|
+
event.preventDefault();
|
|
2176
|
+
const candidates = focusableEntries.value;
|
|
2177
|
+
const target = resolveRovingFocusTargetIndex({
|
|
2178
|
+
intent,
|
|
2179
|
+
currentIndex: candidates.findIndex((entry) => entry.element.value === event.currentTarget),
|
|
2180
|
+
count: candidates.length,
|
|
2181
|
+
loop: loop.value
|
|
2182
|
+
});
|
|
2183
|
+
if (target === null) return;
|
|
2184
|
+
candidates[target]?.element.value?.focus();
|
|
2185
|
+
}
|
|
2186
|
+
return {
|
|
2187
|
+
type,
|
|
2188
|
+
value,
|
|
2189
|
+
disabled,
|
|
2190
|
+
rovingFocus,
|
|
2191
|
+
orientation,
|
|
2192
|
+
dir,
|
|
2193
|
+
isItemPressed,
|
|
2194
|
+
setItemPressed,
|
|
2195
|
+
registerItem: collection.add,
|
|
2196
|
+
getGroupProps,
|
|
2197
|
+
getItemProps,
|
|
2198
|
+
onGroupFocus,
|
|
2199
|
+
onGroupMousedown,
|
|
2200
|
+
onGroupBlur,
|
|
2201
|
+
onItemFocus,
|
|
2202
|
+
onItemMousedown,
|
|
2203
|
+
onItemKeydown
|
|
2204
|
+
};
|
|
2205
|
+
}
|
|
2206
|
+
|
|
2207
|
+
// src/useSwitch/useSwitch.ts
|
|
2208
|
+
import { computed as computed20, ref as ref14, toValue as toValue22 } from "vue";
|
|
2209
|
+
import { getSwitchAttrs, getSwitchDataState } from "@agile-plus/primitives/switch";
|
|
2210
|
+
import { getHiddenInputAttrs as getHiddenInputAttrs2 } from "@agile-plus/primitives/form";
|
|
2211
|
+
function useSwitch(options = {}) {
|
|
2212
|
+
const state = useControllable({
|
|
2213
|
+
value: () => toValue22(options.checked),
|
|
2214
|
+
defaultValue: options.defaultChecked ?? false,
|
|
2215
|
+
onChange: (value) => options.onCheckedChange?.(value)
|
|
2216
|
+
});
|
|
2217
|
+
const checked = state.value;
|
|
2218
|
+
const isDisabled = computed20(() => toValue22(options.disabled) === true);
|
|
2219
|
+
const isRequired = computed20(() => toValue22(options.required) === true);
|
|
2220
|
+
const formId = computed20(() => toValue22(options.form));
|
|
2221
|
+
const submitValue = computed20(() => toValue22(options.value) ?? "on");
|
|
2222
|
+
const id = computed20(() => toValue22(options.id));
|
|
2223
|
+
const control = ref14(null);
|
|
2224
|
+
const initialChecked = checked.value;
|
|
2225
|
+
const bubble = useBubbleInput({
|
|
2226
|
+
control,
|
|
2227
|
+
form: () => formId.value,
|
|
2228
|
+
checked: () => checked.value,
|
|
2229
|
+
onReset: () => setChecked(initialChecked)
|
|
2230
|
+
});
|
|
2231
|
+
function setChecked(value) {
|
|
2232
|
+
state.set(value);
|
|
2233
|
+
}
|
|
2234
|
+
function toggle() {
|
|
2235
|
+
if (isDisabled.value) return;
|
|
2236
|
+
setChecked(!checked.value);
|
|
2237
|
+
}
|
|
2238
|
+
function onRootClick() {
|
|
2239
|
+
toggle();
|
|
2240
|
+
}
|
|
2241
|
+
function setControl(node) {
|
|
2242
|
+
control.value = node instanceof HTMLElement ? node : null;
|
|
2243
|
+
}
|
|
2244
|
+
function getRootProps() {
|
|
2245
|
+
return {
|
|
2246
|
+
type: "button",
|
|
2247
|
+
id: id.value || void 0,
|
|
2248
|
+
...getSwitchAttrs({
|
|
2249
|
+
checked: checked.value,
|
|
2250
|
+
disabled: isDisabled.value,
|
|
2251
|
+
required: isRequired.value
|
|
2252
|
+
}),
|
|
2253
|
+
disabled: isDisabled.value || void 0,
|
|
2254
|
+
// Radix 也把提交值写到按钮上:<button name value> 本身就是表单数据。
|
|
2255
|
+
// 本库的按钮不带 name,因此不会与隐藏 input 重复提交。
|
|
2256
|
+
value: submitValue.value
|
|
2257
|
+
};
|
|
2258
|
+
}
|
|
2259
|
+
function getThumbProps() {
|
|
2260
|
+
return {
|
|
2261
|
+
"data-state": getSwitchDataState(checked.value),
|
|
2262
|
+
"data-disabled": isDisabled.value ? "" : void 0
|
|
2263
|
+
};
|
|
2264
|
+
}
|
|
2265
|
+
function getInputProps() {
|
|
2266
|
+
if (!bubble.isFormControl.value) return null;
|
|
2267
|
+
return {
|
|
2268
|
+
...getHiddenInputAttrs2({
|
|
2269
|
+
type: "checkbox",
|
|
2270
|
+
name: toValue22(options.name),
|
|
2271
|
+
value: submitValue.value,
|
|
2272
|
+
disabled: isDisabled.value,
|
|
2273
|
+
required: isRequired.value,
|
|
2274
|
+
form: formId.value
|
|
2275
|
+
}),
|
|
2276
|
+
style: bubble.style.value
|
|
2277
|
+
};
|
|
2278
|
+
}
|
|
2279
|
+
return {
|
|
2280
|
+
checked,
|
|
2281
|
+
isDisabled,
|
|
2282
|
+
isFormControl: bubble.isFormControl,
|
|
2283
|
+
control,
|
|
2284
|
+
setControl,
|
|
2285
|
+
setInputElement: bubble.setInputElement,
|
|
2286
|
+
getRootProps,
|
|
2287
|
+
getThumbProps,
|
|
2288
|
+
getInputProps,
|
|
2289
|
+
setChecked,
|
|
2290
|
+
toggle,
|
|
2291
|
+
onRootClick
|
|
2292
|
+
};
|
|
2293
|
+
}
|
|
2294
|
+
|
|
2295
|
+
// src/useSlider/useSlider.ts
|
|
2296
|
+
import { computed as computed21, onScopeDispose as onScopeDispose13, ref as ref15, toValue as toValue23 } from "vue";
|
|
2297
|
+
import {
|
|
2298
|
+
getSliderClosestValueIndex,
|
|
2299
|
+
getSliderKeyboardIntent,
|
|
2300
|
+
getSliderNextStepValue,
|
|
2301
|
+
getSliderNextValues,
|
|
2302
|
+
getSliderRangeStyle,
|
|
2303
|
+
getSliderThumbInBoundsOffset,
|
|
2304
|
+
getSliderThumbLabel,
|
|
2305
|
+
getSliderValueFromPointer,
|
|
2306
|
+
getSliderVisualPercent,
|
|
2307
|
+
normalizeSliderValues
|
|
2308
|
+
} from "@agile-plus/primitives/slider";
|
|
2309
|
+
function formatThumbPosition(percent, offset) {
|
|
2310
|
+
if (offset === 0) return `${percent}%`;
|
|
2311
|
+
const operator = offset > 0 ? "+" : "-";
|
|
2312
|
+
return `calc(${percent}% ${operator} ${Math.abs(offset)}px)`;
|
|
2313
|
+
}
|
|
2314
|
+
function useSlider(options = {}) {
|
|
2315
|
+
const min = computed21(() => toValue23(options.min) ?? 0);
|
|
2316
|
+
const max = computed21(() => toValue23(options.max) ?? 100);
|
|
2317
|
+
const step = computed21(() => toValue23(options.step) ?? 1);
|
|
2318
|
+
const minStepsBetweenThumbs = computed21(() => toValue23(options.minStepsBetweenThumbs) ?? 0);
|
|
2319
|
+
const preserveThumbOrder = computed21(() => toValue23(options.preserveThumbOrder) ?? false);
|
|
2320
|
+
const orientation = computed21(() => toValue23(options.orientation) ?? "horizontal");
|
|
2321
|
+
const dir = computed21(() => toValue23(options.dir) ?? "ltr");
|
|
2322
|
+
const inverted = computed21(() => toValue23(options.inverted) ?? false);
|
|
2323
|
+
const disabled = computed21(() => toValue23(options.disabled) ?? false);
|
|
2324
|
+
function assertRange() {
|
|
2325
|
+
if (!Number.isFinite(min.value) || !Number.isFinite(max.value) || max.value <= min.value) {
|
|
2326
|
+
throw new Error("Slider \u7684 `max` \u5FC5\u987B\u662F\u5927\u4E8E `min` \u7684\u6709\u9650\u6570\u503C\u3002");
|
|
2327
|
+
}
|
|
2328
|
+
if (!Number.isFinite(step.value) || step.value <= 0) {
|
|
2329
|
+
throw new Error("Slider \u7684 `step` \u5FC5\u987B\u662F\u5927\u4E8E 0 \u7684\u6709\u9650\u6570\u503C\u3002");
|
|
2330
|
+
}
|
|
2331
|
+
if (!Number.isFinite(minStepsBetweenThumbs.value) || minStepsBetweenThumbs.value < 0) {
|
|
2332
|
+
throw new Error("Slider \u7684 `minStepsBetweenThumbs` \u4E0D\u80FD\u4E3A\u8D1F\u6570\u3002");
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
assertRange();
|
|
2336
|
+
const state = useControllable({
|
|
2337
|
+
value: () => toValue23(options.value),
|
|
2338
|
+
defaultValue: normalizeSliderValues(options.defaultValue, min.value, max.value, step.value),
|
|
2339
|
+
// 回调拿到副本:内部数组之后可能被拖动改写,不能把引用交出去。
|
|
2340
|
+
onChange: (next) => options.onValueChange?.([...next])
|
|
2341
|
+
});
|
|
2342
|
+
const values = computed21(() => {
|
|
2343
|
+
assertRange();
|
|
2344
|
+
return normalizeSliderValues(state.value.value, min.value, max.value, step.value);
|
|
2345
|
+
});
|
|
2346
|
+
const activeIndex = ref15(0);
|
|
2347
|
+
const rootElement = ref15(null);
|
|
2348
|
+
const thumbElements = /* @__PURE__ */ new Map();
|
|
2349
|
+
const thumbSizes = ref15({});
|
|
2350
|
+
const resize = useResizeObserver(() => {
|
|
2351
|
+
for (const index of thumbElements.keys()) {
|
|
2352
|
+
measureThumb(index);
|
|
2353
|
+
}
|
|
2354
|
+
});
|
|
2355
|
+
const drag = usePointerDrag();
|
|
2356
|
+
let valuesBeforeDrag = [];
|
|
2357
|
+
function emitValues(next) {
|
|
2358
|
+
state.set(next);
|
|
2359
|
+
}
|
|
2360
|
+
function applyValue(nextValue, index, commit) {
|
|
2361
|
+
if (disabled.value) return;
|
|
2362
|
+
const result = getSliderNextValues({
|
|
2363
|
+
values: values.value,
|
|
2364
|
+
index,
|
|
2365
|
+
nextValue,
|
|
2366
|
+
min: min.value,
|
|
2367
|
+
max: max.value,
|
|
2368
|
+
step: step.value,
|
|
2369
|
+
minStepsBetweenThumbs: minStepsBetweenThumbs.value,
|
|
2370
|
+
preserveThumbOrder: preserveThumbOrder.value
|
|
2371
|
+
});
|
|
2372
|
+
if (!result.changed) return;
|
|
2373
|
+
const previousIndex = activeIndex.value;
|
|
2374
|
+
activeIndex.value = result.changedIndex;
|
|
2375
|
+
emitValues(result.values);
|
|
2376
|
+
if (result.changedIndex !== previousIndex) {
|
|
2377
|
+
thumbElements.get(result.changedIndex)?.focus({ preventScroll: true });
|
|
2378
|
+
}
|
|
2379
|
+
if (commit) {
|
|
2380
|
+
options.onValueCommit?.([...result.values]);
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
function readRootRect() {
|
|
2384
|
+
return rootElement.value?.getBoundingClientRect() ?? null;
|
|
2385
|
+
}
|
|
2386
|
+
function getPointerValue(event, rect) {
|
|
2387
|
+
const isHorizontal = orientation.value === "horizontal";
|
|
2388
|
+
return getSliderValueFromPointer({
|
|
2389
|
+
pointerPosition: isHorizontal ? event.clientX : event.clientY,
|
|
2390
|
+
trackStart: isHorizontal ? rect.left : rect.top,
|
|
2391
|
+
trackLength: isHorizontal ? rect.width : rect.height,
|
|
2392
|
+
min: min.value,
|
|
2393
|
+
max: max.value,
|
|
2394
|
+
step: step.value,
|
|
2395
|
+
orientation: orientation.value,
|
|
2396
|
+
dir: dir.value,
|
|
2397
|
+
inverted: inverted.value
|
|
2398
|
+
});
|
|
2399
|
+
}
|
|
2400
|
+
function beginDrag(event, index, rect) {
|
|
2401
|
+
if (!isBrowser) return;
|
|
2402
|
+
event.preventDefault();
|
|
2403
|
+
activeIndex.value = index;
|
|
2404
|
+
valuesBeforeDrag = [...values.value];
|
|
2405
|
+
thumbElements.get(index)?.focus({ preventScroll: true });
|
|
2406
|
+
drag.start({
|
|
2407
|
+
onMove: (moveEvent) => {
|
|
2408
|
+
applyValue(getPointerValue(moveEvent, rect), activeIndex.value, false);
|
|
2409
|
+
},
|
|
2410
|
+
// commit 只在正常抬起时上报:被新拖拽顶替、系统中断与组件卸载都算取消,
|
|
2411
|
+
// 与 Radix 的 onValueCommit 语义一致。
|
|
2412
|
+
onEnd: (reason) => {
|
|
2413
|
+
if (reason !== "up") return;
|
|
2414
|
+
const changed = valuesBeforeDrag.length !== values.value.length || valuesBeforeDrag.some((value, valueIndex) => value !== values.value[valueIndex]);
|
|
2415
|
+
if (changed) options.onValueCommit?.([...values.value]);
|
|
2416
|
+
}
|
|
2417
|
+
});
|
|
2418
|
+
}
|
|
2419
|
+
function onRootPointerDown(event) {
|
|
2420
|
+
if (disabled.value || event.button !== 0) return;
|
|
2421
|
+
const rect = readRootRect();
|
|
2422
|
+
if (!rect) return;
|
|
2423
|
+
const nextValue = getPointerValue(event, rect);
|
|
2424
|
+
const index = getSliderClosestValueIndex(values.value, nextValue);
|
|
2425
|
+
beginDrag(event, index, rect);
|
|
2426
|
+
applyValue(nextValue, index, false);
|
|
2427
|
+
}
|
|
2428
|
+
function onThumbPointerDown(event, index) {
|
|
2429
|
+
event.stopPropagation();
|
|
2430
|
+
if (disabled.value || event.button !== 0) return;
|
|
2431
|
+
const rect = readRootRect();
|
|
2432
|
+
if (!rect) return;
|
|
2433
|
+
beginDrag(event, index, rect);
|
|
2434
|
+
}
|
|
2435
|
+
function onThumbFocus(index) {
|
|
2436
|
+
activeIndex.value = index;
|
|
2437
|
+
}
|
|
2438
|
+
function onThumbKeydown(event, index) {
|
|
2439
|
+
if (disabled.value) return;
|
|
2440
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
2441
|
+
const intent = getSliderKeyboardIntent({
|
|
2442
|
+
key: event.key,
|
|
2443
|
+
shiftKey: event.shiftKey,
|
|
2444
|
+
orientation: orientation.value,
|
|
2445
|
+
dir: dir.value,
|
|
2446
|
+
inverted: inverted.value
|
|
2447
|
+
});
|
|
2448
|
+
if (!intent) return;
|
|
2449
|
+
event.preventDefault();
|
|
2450
|
+
if (intent.type === "min") {
|
|
2451
|
+
applyValue(min.value, 0, true);
|
|
2452
|
+
return;
|
|
2453
|
+
}
|
|
2454
|
+
if (intent.type === "max") {
|
|
2455
|
+
applyValue(max.value, values.value.length - 1, true);
|
|
2456
|
+
return;
|
|
2457
|
+
}
|
|
2458
|
+
const currentValue = values.value[index] ?? min.value;
|
|
2459
|
+
const nextValue = getSliderNextStepValue({
|
|
2460
|
+
value: currentValue,
|
|
2461
|
+
min: min.value,
|
|
2462
|
+
step: step.value,
|
|
2463
|
+
direction: intent.direction,
|
|
2464
|
+
multiplier: intent.multiplier
|
|
2465
|
+
});
|
|
2466
|
+
applyValue(nextValue, index, true);
|
|
2467
|
+
}
|
|
2468
|
+
function getHiddenInputs() {
|
|
2469
|
+
const name = toValue23(options.name);
|
|
2470
|
+
if (!name) return [];
|
|
2471
|
+
const resolvedName = values.value.length > 1 ? `${name}[]` : name;
|
|
2472
|
+
const form = toValue23(options.form);
|
|
2473
|
+
return values.value.map((value, index) => ({
|
|
2474
|
+
index,
|
|
2475
|
+
name: resolvedName,
|
|
2476
|
+
value: String(value),
|
|
2477
|
+
form
|
|
2478
|
+
}));
|
|
2479
|
+
}
|
|
2480
|
+
function getRootProps() {
|
|
2481
|
+
return {
|
|
2482
|
+
dir: dir.value,
|
|
2483
|
+
"data-part": "root",
|
|
2484
|
+
"data-orientation": orientation.value,
|
|
2485
|
+
"data-disabled": disabled.value ? "" : void 0,
|
|
2486
|
+
"aria-disabled": disabled.value || void 0
|
|
2487
|
+
};
|
|
2488
|
+
}
|
|
2489
|
+
function getTrackProps() {
|
|
2490
|
+
return {
|
|
2491
|
+
"data-part": "track",
|
|
2492
|
+
"data-orientation": orientation.value,
|
|
2493
|
+
"data-disabled": disabled.value ? "" : void 0
|
|
2494
|
+
};
|
|
2495
|
+
}
|
|
2496
|
+
function getRangeProps() {
|
|
2497
|
+
return {
|
|
2498
|
+
"data-part": "range",
|
|
2499
|
+
"data-orientation": orientation.value,
|
|
2500
|
+
"data-disabled": disabled.value ? "" : void 0
|
|
2501
|
+
};
|
|
2502
|
+
}
|
|
2503
|
+
function getRangeStyle() {
|
|
2504
|
+
const style = getSliderRangeStyle(values.value, {
|
|
2505
|
+
min: min.value,
|
|
2506
|
+
max: max.value,
|
|
2507
|
+
step: step.value,
|
|
2508
|
+
orientation: orientation.value,
|
|
2509
|
+
dir: dir.value,
|
|
2510
|
+
inverted: inverted.value
|
|
2511
|
+
});
|
|
2512
|
+
return {
|
|
2513
|
+
left: style.left,
|
|
2514
|
+
right: style.right,
|
|
2515
|
+
top: style.top,
|
|
2516
|
+
bottom: style.bottom
|
|
2517
|
+
};
|
|
2518
|
+
}
|
|
2519
|
+
function getThumbProps(index) {
|
|
2520
|
+
const value = values.value[index] ?? min.value;
|
|
2521
|
+
return {
|
|
2522
|
+
role: "slider",
|
|
2523
|
+
"aria-valuemin": min.value,
|
|
2524
|
+
"aria-valuemax": max.value,
|
|
2525
|
+
"aria-valuenow": value,
|
|
2526
|
+
"aria-orientation": orientation.value,
|
|
2527
|
+
"aria-label": getSliderThumbLabel(index, values.value.length),
|
|
2528
|
+
"aria-disabled": disabled.value || void 0,
|
|
2529
|
+
"data-part": "thumb",
|
|
2530
|
+
"data-orientation": orientation.value,
|
|
2531
|
+
"data-disabled": disabled.value ? "" : void 0,
|
|
2532
|
+
"data-index": index,
|
|
2533
|
+
tabindex: disabled.value ? void 0 : 0
|
|
2534
|
+
};
|
|
2535
|
+
}
|
|
2536
|
+
function getThumbStyle(index) {
|
|
2537
|
+
const value = values.value[index] ?? min.value;
|
|
2538
|
+
const percent = getSliderVisualPercent(value, {
|
|
2539
|
+
min: min.value,
|
|
2540
|
+
max: max.value,
|
|
2541
|
+
step: step.value,
|
|
2542
|
+
orientation: orientation.value,
|
|
2543
|
+
dir: dir.value,
|
|
2544
|
+
inverted: inverted.value
|
|
2545
|
+
});
|
|
2546
|
+
const size = thumbSizes.value[index];
|
|
2547
|
+
const dimension = orientation.value === "horizontal" ? size?.width ?? 0 : size?.height ?? 0;
|
|
2548
|
+
const offset = dimension > 0 ? Math.round(getSliderThumbInBoundsOffset(dimension, percent) * 100) / 100 : 0;
|
|
2549
|
+
return orientation.value === "horizontal" ? {
|
|
2550
|
+
position: "absolute",
|
|
2551
|
+
left: formatThumbPosition(percent, offset),
|
|
2552
|
+
transform: "translateX(-50%)"
|
|
2553
|
+
} : {
|
|
2554
|
+
position: "absolute",
|
|
2555
|
+
top: formatThumbPosition(percent, offset),
|
|
2556
|
+
transform: "translateY(-50%)"
|
|
2557
|
+
};
|
|
2558
|
+
}
|
|
2559
|
+
function setRootElement(node) {
|
|
2560
|
+
rootElement.value = isBrowser && node instanceof HTMLElement ? node : null;
|
|
2561
|
+
}
|
|
2562
|
+
function measureThumb(index) {
|
|
2563
|
+
const element = thumbElements.get(index);
|
|
2564
|
+
if (!element) return;
|
|
2565
|
+
const next = { width: element.offsetWidth, height: element.offsetHeight };
|
|
2566
|
+
const current = thumbSizes.value[index];
|
|
2567
|
+
if (current?.width === next.width && current.height === next.height) return;
|
|
2568
|
+
thumbSizes.value = { ...thumbSizes.value, [index]: next };
|
|
2569
|
+
}
|
|
2570
|
+
function setThumbElement(index, node) {
|
|
2571
|
+
const element = isBrowser && node instanceof HTMLElement ? node : null;
|
|
2572
|
+
const previous = thumbElements.get(index);
|
|
2573
|
+
if (previous === element) return;
|
|
2574
|
+
if (previous) {
|
|
2575
|
+
resize.unobserve(previous);
|
|
2576
|
+
thumbElements.delete(index);
|
|
2577
|
+
}
|
|
2578
|
+
if (element) {
|
|
2579
|
+
thumbElements.set(index, element);
|
|
2580
|
+
resize.observe(element);
|
|
2581
|
+
measureThumb(index);
|
|
2582
|
+
return;
|
|
2583
|
+
}
|
|
2584
|
+
if (!(index in thumbSizes.value)) return;
|
|
2585
|
+
const nextSizes = { ...thumbSizes.value };
|
|
2586
|
+
delete nextSizes[index];
|
|
2587
|
+
thumbSizes.value = nextSizes;
|
|
2588
|
+
}
|
|
2589
|
+
onScopeDispose13(() => {
|
|
2590
|
+
thumbElements.clear();
|
|
2591
|
+
});
|
|
2592
|
+
return {
|
|
2593
|
+
values,
|
|
2594
|
+
isDisabled: disabled,
|
|
2595
|
+
getRootProps,
|
|
2596
|
+
getTrackProps,
|
|
2597
|
+
getRangeProps,
|
|
2598
|
+
getRangeStyle,
|
|
2599
|
+
getThumbProps,
|
|
2600
|
+
getThumbStyle,
|
|
2601
|
+
getHiddenInputs,
|
|
2602
|
+
setRootElement,
|
|
2603
|
+
setThumbElement,
|
|
2604
|
+
onRootPointerDown,
|
|
2605
|
+
onThumbPointerDown,
|
|
2606
|
+
onThumbKeydown,
|
|
2607
|
+
onThumbFocus
|
|
2608
|
+
};
|
|
2609
|
+
}
|
|
2610
|
+
export {
|
|
2611
|
+
useCheckbox,
|
|
2612
|
+
useCheckboxGroup,
|
|
2613
|
+
useCollapsible,
|
|
2614
|
+
useDialog,
|
|
2615
|
+
useForm,
|
|
2616
|
+
useFormControl,
|
|
2617
|
+
useFormField,
|
|
2618
|
+
useFormMessage,
|
|
2619
|
+
usePresence,
|
|
2620
|
+
useRadioGroup,
|
|
2621
|
+
useScrollArea,
|
|
2622
|
+
useSlider,
|
|
2623
|
+
useSwitch,
|
|
2624
|
+
useTabs,
|
|
2625
|
+
useToast,
|
|
2626
|
+
useToastProvider,
|
|
2627
|
+
useToastViewport,
|
|
2628
|
+
useToggle,
|
|
2629
|
+
useToggleGroup
|
|
2630
|
+
};
|
|
2631
|
+
//# sourceMappingURL=index.js.map
|