@kumix/ui 0.3.8 → 0.3.10
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/README.md +16 -15
- package/dist/components/agents/ai-sidebar.js +3 -5
- package/dist/components/agents/ai-sidebar.js.map +1 -1
- package/dist/components/motion/animated-sidebar.d.ts.map +1 -1
- package/dist/components/motion/animated-sidebar.js.map +1 -1
- package/dist/components/motion/availability-scheduler/copy-menu.js +1 -1
- package/dist/components/motion/availability-scheduler/day-row.d.ts.map +1 -1
- package/dist/components/motion/availability-scheduler/day-row.js +15 -7
- package/dist/components/motion/availability-scheduler/day-row.js.map +1 -1
- package/dist/components/motion/availability-scheduler/index.js +1 -1
- package/dist/components/motion/button/index.d.ts +2 -1
- package/dist/components/motion/button/index.js +2 -1
- package/dist/components/motion/button/metallic.d.ts +10 -0
- package/dist/components/motion/button/metallic.d.ts.map +1 -0
- package/dist/components/motion/button/metallic.js +71 -0
- package/dist/components/motion/button/metallic.js.map +1 -0
- package/dist/components/motion/combobox/content.js +1 -1
- package/dist/components/motion/combobox/context.d.ts +6 -0
- package/dist/components/motion/combobox/context.d.ts.map +1 -1
- package/dist/components/motion/combobox/context.js +1 -233
- package/dist/components/motion/combobox/list.js +1 -1
- package/dist/components/motion/combobox/trigger.js +7 -8
- package/dist/components/motion/combobox/trigger.js.map +1 -1
- package/dist/components/motion/combobox.js +1 -1
- package/dist/components/motion/command-palette.d.ts.map +1 -1
- package/dist/components/motion/command-palette.js +21 -26
- package/dist/components/motion/command-palette.js.map +1 -1
- package/dist/components/motion/expandable-control.d.ts +28 -0
- package/dist/components/motion/expandable-control.d.ts.map +1 -0
- package/dist/components/motion/expandable-control.js +149 -0
- package/dist/components/motion/expandable-control.js.map +1 -0
- package/dist/components/motion/expanding-arrow-button.d.ts +12 -0
- package/dist/components/motion/expanding-arrow-button.d.ts.map +1 -0
- package/dist/components/motion/expanding-arrow-button.js +142 -0
- package/dist/components/motion/expanding-arrow-button.js.map +1 -0
- package/dist/components/motion/file-tree.d.ts +42 -0
- package/dist/components/motion/file-tree.d.ts.map +1 -0
- package/dist/components/motion/file-tree.js +272 -0
- package/dist/components/motion/file-tree.js.map +1 -0
- package/dist/components/motion/morphing-search.d.ts.map +1 -1
- package/dist/components/motion/morphing-search.js +27 -23
- package/dist/components/motion/morphing-search.js.map +1 -1
- package/dist/components/reui/alert.d.ts +1 -1
- package/dist/components/reui/badge.d.ts +1 -1
- package/dist/components/reui/code-block/code-block-highlight.d.ts +216 -0
- package/dist/components/reui/code-block/code-block-highlight.d.ts.map +1 -0
- package/dist/components/reui/code-block/code-block-highlight.js +792 -0
- package/dist/components/reui/code-block/code-block-highlight.js.map +1 -0
- package/dist/components/reui/code-block/code-block.d.ts +197 -0
- package/dist/components/reui/code-block/code-block.d.ts.map +1 -0
- package/dist/components/reui/code-block/code-block.js +1150 -0
- package/dist/components/reui/code-block/code-block.js.map +1 -0
- package/dist/components/reui/filters/filters-builder.js +1 -1
- package/dist/components/reui/filters/filters-chip.js.map +1 -1
- package/dist/components/reui/filters/filters-editors.js +2 -2
- package/dist/components/reui/filters/filters-editors.js.map +1 -1
- package/dist/components/reui/gantt/gantt-bar.js +42 -12
- package/dist/components/reui/gantt/gantt-bar.js.map +1 -1
- package/dist/components/reui/gantt/gantt-dnd.d.ts.map +1 -1
- package/dist/components/reui/gantt/gantt-dnd.js +22 -7
- package/dist/components/reui/gantt/gantt-dnd.js.map +1 -1
- package/dist/components/reui/gantt/gantt-i18n.d.ts +8 -0
- package/dist/components/reui/gantt/gantt-i18n.d.ts.map +1 -1
- package/dist/components/reui/gantt/gantt-i18n.js +6 -1
- package/dist/components/reui/gantt/gantt-i18n.js.map +1 -1
- package/dist/components/reui/gantt/gantt-lib.d.ts +59 -2
- package/dist/components/reui/gantt/gantt-lib.d.ts.map +1 -1
- package/dist/components/reui/gantt/gantt-lib.js +67 -2
- package/dist/components/reui/gantt/gantt-lib.js.map +1 -1
- package/dist/components/reui/gantt/gantt-recurrence.js +3 -3
- package/dist/components/reui/gantt/gantt-recurrence.js.map +1 -1
- package/dist/components/reui/gantt/gantt-types.d.ts +32 -1
- package/dist/components/reui/gantt/gantt-types.d.ts.map +1 -1
- package/dist/components/reui/gantt/gantt-view.d.ts.map +1 -1
- package/dist/components/reui/gantt/gantt-view.js +324 -35
- package/dist/components/reui/gantt/gantt-view.js.map +1 -1
- package/dist/components/reui/gantt/gantt.d.ts +71 -2
- package/dist/components/reui/gantt/gantt.d.ts.map +1 -1
- package/dist/components/reui/gantt/gantt.js +8 -0
- package/dist/components/reui/gantt/gantt.js.map +1 -1
- package/dist/components/ui/badge.d.ts +1 -1
- package/dist/components/ui/bubble.d.ts +1 -1
- package/dist/components/ui/button.d.ts +1 -1
- package/dist/components/ui/item.d.ts +1 -1
- package/dist/components/ui/marker.d.ts +1 -1
- package/dist/components/ui/tabs.d.ts +1 -1
- package/dist/context-BizvUNZv.js +298 -0
- package/dist/context-BizvUNZv.js.map +1 -0
- package/dist/{copy-menu-1FKoMc9o.js → copy-menu-C5GaT4Z-.js} +67 -2
- package/dist/copy-menu-C5GaT4Z-.js.map +1 -0
- package/dist/hooks/use-on-open.d.ts +18 -0
- package/dist/hooks/use-on-open.d.ts.map +1 -0
- package/dist/hooks/use-on-open.js +27 -0
- package/dist/hooks/use-on-open.js.map +1 -0
- package/dist/hooks/use-row-cursor.d.ts +39 -0
- package/dist/hooks/use-row-cursor.d.ts.map +1 -0
- package/dist/hooks/use-row-cursor.js +77 -0
- package/dist/hooks/use-row-cursor.js.map +1 -0
- package/dist/types-smkWN_7x.d.ts.map +1 -1
- package/package.json +7 -13
- package/dist/components/motion/combobox/context.js.map +0 -1
- package/dist/copy-menu-1FKoMc9o.js.map +0 -1
|
@@ -3,7 +3,7 @@ import { VariantProps } from "class-variance-authority";
|
|
|
3
3
|
import { useRender } from "@base-ui/react/use-render";
|
|
4
4
|
//#region src/components/ui/marker.d.ts
|
|
5
5
|
declare const markerVariants: (props?: ({
|
|
6
|
-
variant?: "
|
|
6
|
+
variant?: "separator" | "default" | "border" | null | undefined;
|
|
7
7
|
} & import("class-variance-authority/types").ClassProp) | undefined) => string;
|
|
8
8
|
declare function Marker({ className, variant, render, ...props }: useRender.ComponentProps<"div"> & VariantProps<typeof markerVariants>): React$1.ReactElement<unknown, string | React$1.JSXElementConstructor<any>>;
|
|
9
9
|
declare function MarkerIcon({ className, ...props }: React$1.ComponentProps<"span">): React$1.JSX.Element;
|
|
@@ -3,7 +3,7 @@ import { Tabs as Tabs$1 } from "@base-ui/react/tabs";
|
|
|
3
3
|
//#region src/components/ui/tabs.d.ts
|
|
4
4
|
declare function Tabs({ className, orientation, ...props }: Tabs$1.Root.Props): import("react").JSX.Element;
|
|
5
5
|
declare const tabsListVariants: (props?: ({
|
|
6
|
-
variant?: "
|
|
6
|
+
variant?: "line" | "default" | null | undefined;
|
|
7
7
|
} & import("class-variance-authority/types").ClassProp) | undefined) => string;
|
|
8
8
|
declare function TabsList({ className, variant, ...props }: Tabs$1.List.Props & VariantProps<typeof tabsListVariants>): import("react").JSX.Element;
|
|
9
9
|
declare function TabsTrigger({ className, ...props }: Tabs$1.Tab.Props): import("react").JSX.Element;
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { createContext, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { cn } from "@kumix/utils";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
import { useReducedMotion } from "motion/react";
|
|
5
|
+
//#region src/components/motion/combobox/use-active-option.ts
|
|
6
|
+
const isEnabled = (enabledItems, candidate) => candidate !== void 0 && enabledItems.some((i) => i.value === candidate);
|
|
7
|
+
/**
|
|
8
|
+
* The cursor's option, or null once the query or the result set it was placed
|
|
9
|
+
* in has changed. A cursor that outlived either would steal Enter from the row
|
|
10
|
+
* the user is aiming at. The result-set half costs something: a live search
|
|
11
|
+
* that blanks its rows while fetching and returns the same ones loses the moved
|
|
12
|
+
* highlight. That is deliberate — a highlight visibly back at the top beats one
|
|
13
|
+
* silently in the wrong place.
|
|
14
|
+
*
|
|
15
|
+
* It is stamped with the query rather than with the identity of the visible
|
|
16
|
+
* list because callers routinely pass an inline `filter`, which makes that list
|
|
17
|
+
* a fresh array on every render.
|
|
18
|
+
*/
|
|
19
|
+
function liveCursorValue(cursor, options) {
|
|
20
|
+
if (cursor === null || cursor.query !== options.query) return null;
|
|
21
|
+
return isEnabled(options.enabledItems, cursor.value) ? cursor.value : null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Where the highlight sits with no live cursor: the selection if it can be
|
|
25
|
+
* selected, otherwise the first option that can. Only enabled options qualify —
|
|
26
|
+
* an active disabled option would point `aria-activedescendant` at a row Enter
|
|
27
|
+
* then refuses to select.
|
|
28
|
+
*/
|
|
29
|
+
function fallbackActive({ value, enabledItems }) {
|
|
30
|
+
return isEnabled(enabledItems, value) ? value : enabledItems[0]?.value ?? null;
|
|
31
|
+
}
|
|
32
|
+
/** The active option, from a cursor that may or may not still be live. */
|
|
33
|
+
const resolveActive = (cursor, options) => liveCursorValue(cursor, options) ?? fallbackActive(options);
|
|
34
|
+
function useActiveOption({ open, ...options }) {
|
|
35
|
+
const { query, value, enabledItems } = options;
|
|
36
|
+
const [cursor, setCursor] = useState(null);
|
|
37
|
+
const live = liveCursorValue(cursor, options);
|
|
38
|
+
if (cursor !== null && live === null) setCursor(null);
|
|
39
|
+
const derived = live ?? fallbackActive(options);
|
|
40
|
+
const [opened, setOpened] = useState(open);
|
|
41
|
+
if (open && !opened) setOpened(true);
|
|
42
|
+
const activeValue = opened ? derived : null;
|
|
43
|
+
const latest = useRef({
|
|
44
|
+
open,
|
|
45
|
+
query,
|
|
46
|
+
value,
|
|
47
|
+
enabledItems
|
|
48
|
+
});
|
|
49
|
+
useLayoutEffect(() => {
|
|
50
|
+
latest.current = {
|
|
51
|
+
open,
|
|
52
|
+
query,
|
|
53
|
+
value,
|
|
54
|
+
enabledItems
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
return {
|
|
58
|
+
activeValue,
|
|
59
|
+
setActiveValue: useCallback((next) => {
|
|
60
|
+
setCursor(next === null ? null : {
|
|
61
|
+
value: next,
|
|
62
|
+
query: latest.current.query
|
|
63
|
+
});
|
|
64
|
+
}, []),
|
|
65
|
+
moveActive: useCallback((direction) => {
|
|
66
|
+
const options = latest.current;
|
|
67
|
+
if (!options.open) return;
|
|
68
|
+
const rows = options.enabledItems;
|
|
69
|
+
const last = rows.length - 1;
|
|
70
|
+
if (last < 0) {
|
|
71
|
+
setCursor(null);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
setCursor((current) => {
|
|
75
|
+
const from = resolveActive(current, options);
|
|
76
|
+
const at = rows.findIndex((item) => item.value === from);
|
|
77
|
+
const index = direction === "first" ? 0 : direction === "last" ? last : (at + direction + rows.length) % rows.length;
|
|
78
|
+
return {
|
|
79
|
+
value: rows[index].value,
|
|
80
|
+
query: options.query
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
}, [])
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/components/motion/combobox/context.tsx
|
|
88
|
+
const defaultFilter = (value, query, keywords) => {
|
|
89
|
+
const needle = query.trim().toLocaleLowerCase();
|
|
90
|
+
if (!needle) return true;
|
|
91
|
+
const haystack = [value, ...keywords].join(" ").toLocaleLowerCase();
|
|
92
|
+
let queryIndex = 0;
|
|
93
|
+
for (const character of haystack) {
|
|
94
|
+
if (character === needle[queryIndex]) queryIndex += 1;
|
|
95
|
+
if (queryIndex === needle.length) return true;
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
};
|
|
99
|
+
const ComboboxContext = createContext(null);
|
|
100
|
+
const ComboboxGroupContext = createContext(null);
|
|
101
|
+
function useComboboxContext(component) {
|
|
102
|
+
const context = useContext(ComboboxContext);
|
|
103
|
+
if (!context) throw new Error(`${component} must be used within <Combobox>`);
|
|
104
|
+
return context;
|
|
105
|
+
}
|
|
106
|
+
function mergeRefs(...refs) {
|
|
107
|
+
return (node) => {
|
|
108
|
+
for (const ref of refs) if (typeof ref === "function") ref(node);
|
|
109
|
+
else if (ref && typeof ref === "object") ref.current = node;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function Combobox({ children, value: controlledValue, defaultValue, onValueChange, open: controlledOpen, defaultOpen = false, onOpenChange, query: controlledQuery, defaultQuery = "", onQueryChange, filter = defaultFilter, disabled = false, className }) {
|
|
113
|
+
const reduce = useReducedMotion() ?? false;
|
|
114
|
+
const baseId = useId();
|
|
115
|
+
const rootRef = useRef(null);
|
|
116
|
+
const triggerRef = useRef(null);
|
|
117
|
+
const contentRef = useRef(null);
|
|
118
|
+
const inputRef = useRef(null);
|
|
119
|
+
const [internalValue, setInternalValue] = useState(defaultValue);
|
|
120
|
+
const [internalOpen, setInternalOpen] = useState(defaultOpen);
|
|
121
|
+
const [internalQuery, setInternalQuery] = useState(defaultQuery);
|
|
122
|
+
const [items, setItems] = useState(/* @__PURE__ */ new Map());
|
|
123
|
+
const valueControlled = controlledValue !== void 0;
|
|
124
|
+
const openControlled = controlledOpen !== void 0;
|
|
125
|
+
const queryControlled = controlledQuery !== void 0;
|
|
126
|
+
const value = valueControlled ? controlledValue : internalValue;
|
|
127
|
+
const open = openControlled ? controlledOpen : internalOpen;
|
|
128
|
+
const query = queryControlled ? controlledQuery : internalQuery;
|
|
129
|
+
const updateQuery = useCallback((next) => {
|
|
130
|
+
if (!queryControlled) setInternalQuery(next);
|
|
131
|
+
onQueryChange?.(next);
|
|
132
|
+
}, [onQueryChange, queryControlled]);
|
|
133
|
+
const updateOpen = useCallback((next, restoreFocus = false) => {
|
|
134
|
+
if (disabled && next) return;
|
|
135
|
+
if (!openControlled) setInternalOpen(next);
|
|
136
|
+
onOpenChange?.(next);
|
|
137
|
+
if (!next) updateQuery("");
|
|
138
|
+
if (restoreFocus) requestAnimationFrame(() => inputRef.current?.focus({ preventScroll: true }));
|
|
139
|
+
}, [
|
|
140
|
+
disabled,
|
|
141
|
+
onOpenChange,
|
|
142
|
+
openControlled,
|
|
143
|
+
updateQuery
|
|
144
|
+
]);
|
|
145
|
+
const registerItem = useCallback((item) => {
|
|
146
|
+
setItems((current) => {
|
|
147
|
+
const existing = current.get(item.value);
|
|
148
|
+
if (existing?.label === item.label && existing.disabled === item.disabled && existing.id === item.id && existing.ref === item.ref && existing.groupId === item.groupId && existing.keywords.join("\0") === item.keywords.join("\0")) return current;
|
|
149
|
+
const next = new Map(current);
|
|
150
|
+
next.set(item.value, item);
|
|
151
|
+
return next;
|
|
152
|
+
});
|
|
153
|
+
}, []);
|
|
154
|
+
const unregisterItem = useCallback((itemValue) => {
|
|
155
|
+
setItems((current) => {
|
|
156
|
+
if (!current.has(itemValue)) return current;
|
|
157
|
+
const next = new Map(current);
|
|
158
|
+
next.delete(itemValue);
|
|
159
|
+
return next;
|
|
160
|
+
});
|
|
161
|
+
}, []);
|
|
162
|
+
const [openQuery, setOpenQuery] = useState(query);
|
|
163
|
+
if (open && openQuery !== query) setOpenQuery(query);
|
|
164
|
+
const listQuery = open ? query : openQuery;
|
|
165
|
+
const visibleItems = useMemo(() => Array.from(items.values()).filter((item) => filter(item.value, listQuery, [item.label, ...item.keywords])), [
|
|
166
|
+
filter,
|
|
167
|
+
items,
|
|
168
|
+
listQuery
|
|
169
|
+
]);
|
|
170
|
+
const enabledVisibleItems = useMemo(() => visibleItems.filter((item) => !item.disabled), [visibleItems]);
|
|
171
|
+
const visibleValues = useMemo(() => new Set(visibleItems.map((item) => item.value)), [visibleItems]);
|
|
172
|
+
const visibleGroupIds = useMemo(() => new Set(visibleItems.map((item) => item.groupId)), [visibleItems]);
|
|
173
|
+
const { activeValue, setActiveValue, moveActive } = useActiveOption({
|
|
174
|
+
open,
|
|
175
|
+
query: listQuery,
|
|
176
|
+
value,
|
|
177
|
+
enabledItems: enabledVisibleItems
|
|
178
|
+
});
|
|
179
|
+
const select = useCallback((next) => {
|
|
180
|
+
if (items.get(next)?.disabled) return;
|
|
181
|
+
if (!valueControlled) setInternalValue(next);
|
|
182
|
+
onValueChange?.(next);
|
|
183
|
+
updateOpen(false, true);
|
|
184
|
+
}, [
|
|
185
|
+
items,
|
|
186
|
+
onValueChange,
|
|
187
|
+
updateOpen,
|
|
188
|
+
valueControlled
|
|
189
|
+
]);
|
|
190
|
+
const selectActive = useCallback(() => {
|
|
191
|
+
if (activeValue) select(activeValue);
|
|
192
|
+
}, [activeValue, select]);
|
|
193
|
+
useEffect(() => {
|
|
194
|
+
if (!open) return;
|
|
195
|
+
const frame = requestAnimationFrame(() => inputRef.current?.focus({ preventScroll: true }));
|
|
196
|
+
return () => cancelAnimationFrame(frame);
|
|
197
|
+
}, [open]);
|
|
198
|
+
useEffect(() => {
|
|
199
|
+
if (!activeValue || !open) return;
|
|
200
|
+
const item = items.get(activeValue)?.ref.current;
|
|
201
|
+
const list = item?.closest("[role='listbox']");
|
|
202
|
+
if (!item || !list) return;
|
|
203
|
+
const itemRect = item.getBoundingClientRect();
|
|
204
|
+
const listRect = list.getBoundingClientRect();
|
|
205
|
+
if (itemRect.top < listRect.top) list.scrollTop -= listRect.top - itemRect.top;
|
|
206
|
+
else if (itemRect.bottom > listRect.bottom) list.scrollTop += itemRect.bottom - listRect.bottom;
|
|
207
|
+
}, [
|
|
208
|
+
activeValue,
|
|
209
|
+
items,
|
|
210
|
+
open
|
|
211
|
+
]);
|
|
212
|
+
useEffect(() => {
|
|
213
|
+
if (!open) return;
|
|
214
|
+
const isInside = (target) => rootRef.current?.contains(target) || contentRef.current?.contains(target);
|
|
215
|
+
const onPointerDown = (event) => {
|
|
216
|
+
if (!isInside(event.target)) updateOpen(false);
|
|
217
|
+
};
|
|
218
|
+
const onFocusIn = (event) => {
|
|
219
|
+
if (!isInside(event.target)) updateOpen(false);
|
|
220
|
+
};
|
|
221
|
+
const onKeyDown = (event) => {
|
|
222
|
+
if (event.key !== "Escape") return;
|
|
223
|
+
event.preventDefault();
|
|
224
|
+
updateOpen(false, true);
|
|
225
|
+
};
|
|
226
|
+
window.addEventListener("pointerdown", onPointerDown);
|
|
227
|
+
window.addEventListener("focusin", onFocusIn);
|
|
228
|
+
window.addEventListener("keydown", onKeyDown);
|
|
229
|
+
return () => {
|
|
230
|
+
window.removeEventListener("pointerdown", onPointerDown);
|
|
231
|
+
window.removeEventListener("focusin", onFocusIn);
|
|
232
|
+
window.removeEventListener("keydown", onKeyDown);
|
|
233
|
+
};
|
|
234
|
+
}, [open, updateOpen]);
|
|
235
|
+
const activeItem = activeValue ? items.get(activeValue) : void 0;
|
|
236
|
+
const context = useMemo(() => ({
|
|
237
|
+
open,
|
|
238
|
+
setOpen: updateOpen,
|
|
239
|
+
value,
|
|
240
|
+
select,
|
|
241
|
+
query,
|
|
242
|
+
setQuery: updateQuery,
|
|
243
|
+
activeValue,
|
|
244
|
+
setActiveValue,
|
|
245
|
+
moveActive,
|
|
246
|
+
selectActive,
|
|
247
|
+
registerItem,
|
|
248
|
+
unregisterItem,
|
|
249
|
+
labelFor: (itemValue) => itemValue === void 0 ? void 0 : items.get(itemValue)?.label,
|
|
250
|
+
isVisible: (itemValue) => !listQuery.trim() || visibleValues.has(itemValue),
|
|
251
|
+
hasVisibleItems: (groupId) => visibleGroupIds.has(groupId),
|
|
252
|
+
visibleCount: visibleItems.length,
|
|
253
|
+
activeItemId: activeItem?.id,
|
|
254
|
+
triggerId: `${baseId}-trigger`,
|
|
255
|
+
listId: `${baseId}-list`,
|
|
256
|
+
inputId: `${baseId}-input`,
|
|
257
|
+
disabled,
|
|
258
|
+
reduce,
|
|
259
|
+
triggerRef,
|
|
260
|
+
contentRef,
|
|
261
|
+
inputRef,
|
|
262
|
+
activeLayoutId: `${baseId}-active`
|
|
263
|
+
}), [
|
|
264
|
+
activeItem?.id,
|
|
265
|
+
activeValue,
|
|
266
|
+
baseId,
|
|
267
|
+
disabled,
|
|
268
|
+
items,
|
|
269
|
+
listQuery,
|
|
270
|
+
moveActive,
|
|
271
|
+
open,
|
|
272
|
+
query,
|
|
273
|
+
reduce,
|
|
274
|
+
registerItem,
|
|
275
|
+
select,
|
|
276
|
+
selectActive,
|
|
277
|
+
setActiveValue,
|
|
278
|
+
unregisterItem,
|
|
279
|
+
updateOpen,
|
|
280
|
+
updateQuery,
|
|
281
|
+
value,
|
|
282
|
+
visibleItems.length,
|
|
283
|
+
visibleGroupIds,
|
|
284
|
+
visibleValues
|
|
285
|
+
]);
|
|
286
|
+
return /* @__PURE__ */ jsx(ComboboxContext.Provider, {
|
|
287
|
+
value: context,
|
|
288
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
289
|
+
ref: rootRef,
|
|
290
|
+
className: cn("relative w-full", className),
|
|
291
|
+
children
|
|
292
|
+
})
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
//#endregion
|
|
296
|
+
export { useComboboxContext as a, mergeRefs as i, ComboboxContext as n, ComboboxGroupContext as r, Combobox as t };
|
|
297
|
+
|
|
298
|
+
//# sourceMappingURL=context-BizvUNZv.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-BizvUNZv.js","names":[],"sources":["../src/components/motion/combobox/use-active-option.ts","../src/components/motion/combobox/context.tsx"],"sourcesContent":["\"use client\";\n\nimport { useCallback, useLayoutEffect, useRef, useState } from \"react\";\n\n/**\n * Where the keyboard or the pointer last moved to, stamped with the query it\n * was placed under. Which option is *active* is resolved from this during\n * render, never in an effect: a passive effect runs after the commit, so a list\n * would briefly have none of its options active, and a key arriving in that\n * window would move from nowhere onto the row it was already about to\n * highlight.\n */\ntype ActiveCursor = { value: string; query: string };\n\ntype Options = {\n query: string;\n value: string | undefined;\n /** The enabled, visible options in list order — all this hook reads of them. */\n enabledItems: readonly { value: string }[];\n};\n\nconst isEnabled = (\n enabledItems: Options[\"enabledItems\"],\n candidate: string | undefined,\n): candidate is string =>\n candidate !== undefined && enabledItems.some((i) => i.value === candidate);\n\n/**\n * The cursor's option, or null once the query or the result set it was placed\n * in has changed. A cursor that outlived either would steal Enter from the row\n * the user is aiming at. The result-set half costs something: a live search\n * that blanks its rows while fetching and returns the same ones loses the moved\n * highlight. That is deliberate — a highlight visibly back at the top beats one\n * silently in the wrong place.\n *\n * It is stamped with the query rather than with the identity of the visible\n * list because callers routinely pass an inline `filter`, which makes that list\n * a fresh array on every render.\n */\nfunction liveCursorValue(cursor: ActiveCursor | null, options: Options) {\n if (cursor === null || cursor.query !== options.query) return null;\n return isEnabled(options.enabledItems, cursor.value) ? cursor.value : null;\n}\n\n/**\n * Where the highlight sits with no live cursor: the selection if it can be\n * selected, otherwise the first option that can. Only enabled options qualify —\n * an active disabled option would point `aria-activedescendant` at a row Enter\n * then refuses to select.\n */\nfunction fallbackActive({ value, enabledItems }: Options) {\n return isEnabled(enabledItems, value) ? value : (enabledItems[0]?.value ?? null);\n}\n\n/** The active option, from a cursor that may or may not still be live. */\nconst resolveActive = (cursor: ActiveCursor | null, options: Options) =>\n liveCursorValue(cursor, options) ?? fallbackActive(options);\n\nexport function useActiveOption({ open, ...options }: Options & { open: boolean }) {\n const { query, value, enabledItems } = options;\n const [cursor, setCursor] = useState<ActiveCursor | null>(null);\n\n const live = liveCursorValue(cursor, options);\n // Cleared rather than ignored: React re-runs this render with the cursor\n // gone, so a value that reappears later cannot revive it.\n if (cursor !== null && live === null) setCursor(null);\n const derived = live ?? fallbackActive(options);\n\n // Nothing is active until the list has been opened once. After that the\n // resolution above is already stable across a close — the list keeps\n // filtering by the query it was open with — so the highlight holds its row\n // through the exit without being frozen separately.\n const [opened, setOpened] = useState(open);\n if (open && !opened) setOpened(true);\n const activeValue = opened ? derived : null;\n\n // Both callbacks keep one identity for the life of the component, and read\n // the list through a ref to do it. A caller will put them in a `useMemo` or\n // an effect's dependencies — the exhaustive-deps rule makes it — and\n // `enabledItems` is a fresh array on every render for any consumer passing an\n // inline `filter`, so a callback keyed to it would be rebuilt every render.\n // Written after commit rather than during render: a render React discards\n // still runs the component body, and a handler reading this in that window\n // would step against a list the committed tree does not have.\n const latest = useRef({ open, query, value, enabledItems });\n useLayoutEffect(() => {\n latest.current = { open, query, value, enabledItems };\n });\n\n const setActiveValue = useCallback((next: string | null) => {\n setCursor(next === null ? null : { value: next, query: latest.current.query });\n }, []);\n\n // Steps from the option the cursor really resolves to, inside the update, so\n // that two keys landing in one batch move two rows rather than one.\n const moveActive = useCallback((direction: 1 | -1 | \"first\" | \"last\") => {\n const options = latest.current;\n // While closed the list is still filtering by the query it was open\n // with, so a step taken now would be measured against rows the next\n // render replaces. Opening is the caller's job; stepping waits for it.\n if (!options.open) return;\n const rows = options.enabledItems;\n const last = rows.length - 1;\n if (last < 0) {\n setCursor(null);\n return;\n }\n setCursor((current) => {\n // `resolveActive` always lands on a member of `enabledItems` once the\n // list is non-empty, which the early return above guarantees, so there\n // is always a row to step from.\n const from = resolveActive(current, options);\n const at = rows.findIndex((item) => item.value === from);\n const index =\n direction === \"first\"\n ? 0\n : direction === \"last\"\n ? last\n : (at + direction + rows.length) % rows.length;\n return { value: rows[index].value, query: options.query };\n });\n }, []);\n\n return { activeValue, setActiveValue, moveActive };\n}\n","\"use client\";\n\nimport {\n createContext,\n type MutableRefObject,\n type ReactNode,\n type Ref,\n useCallback,\n useContext,\n useEffect,\n useId,\n useMemo,\n useRef,\n useState,\n} from \"react\";\nimport { useReducedMotion } from \"motion/react\";\n\nimport { cn } from \"@kumix/utils\";\nimport { useActiveOption } from \"./use-active-option\";\n\nexport type RegisteredItem = {\n value: string;\n label: string;\n keywords: string[];\n disabled: boolean;\n groupId: string | null;\n id: string;\n ref: MutableRefObject<HTMLButtonElement | null>;\n};\n\nexport type ComboboxFilter = (value: string, query: string, keywords: string[]) => boolean;\n\nconst defaultFilter: ComboboxFilter = (value, query, keywords) => {\n const needle = query.trim().toLocaleLowerCase();\n if (!needle) return true;\n\n const haystack = [value, ...keywords].join(\" \").toLocaleLowerCase();\n let queryIndex = 0;\n for (const character of haystack) {\n if (character === needle[queryIndex]) queryIndex += 1;\n if (queryIndex === needle.length) return true;\n }\n return false;\n};\n\nexport type ComboboxContextValue = {\n open: boolean;\n setOpen: (open: boolean, restoreFocus?: boolean) => void;\n value: string | undefined;\n select: (value: string) => void;\n query: string;\n setQuery: (query: string) => void;\n /** The option the list is currently pointing at, resolved during render. */\n activeValue: string | null;\n /**\n * Records where the pointer or keyboard moved to. The recorded value is\n * dropped when the query or the result set changes, so it does not always\n * survive to the next `activeValue`.\n */\n setActiveValue: (value: string | null) => void;\n moveActive: (direction: 1 | -1 | \"first\" | \"last\") => void;\n selectActive: () => void;\n registerItem: (item: RegisteredItem) => void;\n unregisterItem: (value: string) => void;\n labelFor: (value: string | undefined) => string | undefined;\n isVisible: (value: string) => boolean;\n hasVisibleItems: (groupId: string) => boolean;\n visibleCount: number;\n activeItemId: string | undefined;\n triggerId: string;\n listId: string;\n inputId: string;\n disabled: boolean;\n reduce: boolean;\n triggerRef: MutableRefObject<HTMLDivElement | null>;\n contentRef: MutableRefObject<HTMLDivElement | null>;\n inputRef: MutableRefObject<HTMLInputElement | null>;\n activeLayoutId: string;\n};\n\nexport const ComboboxContext = createContext<ComboboxContextValue | null>(null);\nexport const ComboboxGroupContext = createContext<string | null>(null);\n\nexport function useComboboxContext(component: string) {\n const context = useContext(ComboboxContext);\n if (!context) throw new Error(`${component} must be used within <Combobox>`);\n return context;\n}\n\nexport function mergeRefs<T>(...refs: Array<Ref<T> | undefined>) {\n return (node: T | null) => {\n for (const ref of refs) {\n if (typeof ref === \"function\") ref(node);\n else if (ref && typeof ref === \"object\") (ref as MutableRefObject<T | null>).current = node;\n }\n };\n}\n\nexport interface ComboboxProps {\n children: ReactNode;\n value?: string;\n defaultValue?: string;\n onValueChange?: (value: string) => void;\n open?: boolean;\n defaultOpen?: boolean;\n onOpenChange?: (open: boolean) => void;\n query?: string;\n defaultQuery?: string;\n onQueryChange?: (query: string) => void;\n filter?: ComboboxFilter;\n disabled?: boolean;\n className?: string;\n}\n\nexport function Combobox({\n children,\n value: controlledValue,\n defaultValue,\n onValueChange,\n open: controlledOpen,\n defaultOpen = false,\n onOpenChange,\n query: controlledQuery,\n defaultQuery = \"\",\n onQueryChange,\n filter = defaultFilter,\n disabled = false,\n className,\n}: ComboboxProps) {\n const reduce = useReducedMotion() ?? false;\n const baseId = useId();\n const rootRef = useRef<HTMLDivElement>(null);\n const triggerRef = useRef<HTMLDivElement>(null);\n const contentRef = useRef<HTMLDivElement>(null);\n const inputRef = useRef<HTMLInputElement>(null);\n const [internalValue, setInternalValue] = useState(defaultValue);\n const [internalOpen, setInternalOpen] = useState(defaultOpen);\n const [internalQuery, setInternalQuery] = useState(defaultQuery);\n const [items, setItems] = useState<Map<string, RegisteredItem>>(new Map());\n\n const valueControlled = controlledValue !== undefined;\n const openControlled = controlledOpen !== undefined;\n const queryControlled = controlledQuery !== undefined;\n const value = valueControlled ? controlledValue : internalValue;\n const open = openControlled ? controlledOpen : internalOpen;\n const query = queryControlled ? controlledQuery : internalQuery;\n\n const updateQuery = useCallback(\n (next: string) => {\n if (!queryControlled) setInternalQuery(next);\n onQueryChange?.(next);\n },\n [onQueryChange, queryControlled],\n );\n\n const updateOpen = useCallback(\n (next: boolean, restoreFocus = false) => {\n if (disabled && next) return;\n if (!openControlled) setInternalOpen(next);\n onOpenChange?.(next);\n if (!next) updateQuery(\"\");\n if (restoreFocus)\n requestAnimationFrame(() => inputRef.current?.focus({ preventScroll: true }));\n },\n [disabled, onOpenChange, openControlled, updateQuery],\n );\n\n const registerItem = useCallback((item: RegisteredItem) => {\n setItems((current) => {\n const existing = current.get(item.value);\n if (\n existing?.label === item.label &&\n existing.disabled === item.disabled &&\n existing.id === item.id &&\n existing.ref === item.ref &&\n existing.groupId === item.groupId &&\n existing.keywords.join(\"\\u0000\") === item.keywords.join(\"\\u0000\")\n ) {\n return current;\n }\n const next = new Map(current);\n next.set(item.value, item);\n return next;\n });\n }, []);\n\n const unregisterItem = useCallback((itemValue: string) => {\n setItems((current) => {\n if (!current.has(itemValue)) return current;\n const next = new Map(current);\n next.delete(itemValue);\n return next;\n });\n }, []);\n\n // The query the list filters by. Closing clears `query`, but the panel is\n // still on screen for its exit, so it keeps filtering by the query it was\n // open with rather than repopulating mid-collapse. The input already reads\n // `query` only while open, so nothing the user can see reads the other one.\n const [openQuery, setOpenQuery] = useState(query);\n if (open && openQuery !== query) setOpenQuery(query);\n const listQuery = open ? query : openQuery;\n\n const visibleItems = useMemo(\n () =>\n Array.from(items.values()).filter((item) =>\n filter(item.value, listQuery, [item.label, ...item.keywords]),\n ),\n [filter, items, listQuery],\n );\n const enabledVisibleItems = useMemo(\n () => visibleItems.filter((item) => !item.disabled),\n [visibleItems],\n );\n const visibleValues = useMemo(\n () => new Set(visibleItems.map((item) => item.value)),\n [visibleItems],\n );\n const visibleGroupIds = useMemo(\n () => new Set(visibleItems.map((item) => item.groupId)),\n [visibleItems],\n );\n\n const { activeValue, setActiveValue, moveActive } = useActiveOption({\n open,\n query: listQuery,\n value,\n enabledItems: enabledVisibleItems,\n });\n\n const select = useCallback(\n (next: string) => {\n if (items.get(next)?.disabled) return;\n if (!valueControlled) setInternalValue(next);\n onValueChange?.(next);\n updateOpen(false, true);\n },\n [items, onValueChange, updateOpen, valueControlled],\n );\n\n const selectActive = useCallback(() => {\n if (activeValue) select(activeValue);\n }, [activeValue, select]);\n\n useEffect(() => {\n if (!open) return;\n const frame = requestAnimationFrame(() => inputRef.current?.focus({ preventScroll: true }));\n return () => cancelAnimationFrame(frame);\n }, [open]);\n\n useEffect(() => {\n if (!activeValue || !open) return;\n const item = items.get(activeValue)?.ref.current;\n const list = item?.closest<HTMLElement>(\"[role='listbox']\");\n if (!item || !list) return;\n const itemRect = item.getBoundingClientRect();\n const listRect = list.getBoundingClientRect();\n if (itemRect.top < listRect.top) list.scrollTop -= listRect.top - itemRect.top;\n else if (itemRect.bottom > listRect.bottom) list.scrollTop += itemRect.bottom - listRect.bottom;\n }, [activeValue, items, open]);\n\n useEffect(() => {\n if (!open) return;\n const isInside = (target: Node) =>\n rootRef.current?.contains(target) || contentRef.current?.contains(target);\n const onPointerDown = (event: PointerEvent) => {\n if (!isInside(event.target as Node)) updateOpen(false);\n };\n const onFocusIn = (event: FocusEvent) => {\n if (!isInside(event.target as Node)) updateOpen(false);\n };\n const onKeyDown = (event: KeyboardEvent) => {\n if (event.key !== \"Escape\") return;\n event.preventDefault();\n updateOpen(false, true);\n };\n window.addEventListener(\"pointerdown\", onPointerDown);\n window.addEventListener(\"focusin\", onFocusIn);\n window.addEventListener(\"keydown\", onKeyDown);\n return () => {\n window.removeEventListener(\"pointerdown\", onPointerDown);\n window.removeEventListener(\"focusin\", onFocusIn);\n window.removeEventListener(\"keydown\", onKeyDown);\n };\n }, [open, updateOpen]);\n\n const activeItem = activeValue ? items.get(activeValue) : undefined;\n const context = useMemo<ComboboxContextValue>(\n () => ({\n open,\n setOpen: updateOpen,\n value,\n select,\n query,\n setQuery: updateQuery,\n activeValue,\n setActiveValue,\n moveActive,\n selectActive,\n registerItem,\n unregisterItem,\n labelFor: (itemValue) => (itemValue === undefined ? undefined : items.get(itemValue)?.label),\n isVisible: (itemValue) => !listQuery.trim() || visibleValues.has(itemValue),\n hasVisibleItems: (groupId) => visibleGroupIds.has(groupId),\n visibleCount: visibleItems.length,\n activeItemId: activeItem?.id,\n triggerId: `${baseId}-trigger`,\n listId: `${baseId}-list`,\n inputId: `${baseId}-input`,\n disabled,\n reduce,\n triggerRef,\n contentRef,\n inputRef,\n activeLayoutId: `${baseId}-active`,\n }),\n [\n activeItem?.id,\n activeValue,\n baseId,\n disabled,\n items,\n listQuery,\n moveActive,\n open,\n query,\n reduce,\n registerItem,\n select,\n selectActive,\n setActiveValue,\n unregisterItem,\n updateOpen,\n updateQuery,\n value,\n visibleItems.length,\n visibleGroupIds,\n visibleValues,\n ],\n );\n\n return (\n <ComboboxContext.Provider value={context}>\n <div ref={rootRef} className={cn(\"relative w-full\", className)}>\n {children}\n </div>\n </Combobox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EACA,OAAO,iBAAiB,eAAe,aAAa;EACpD,OAAO,iBAAiB,WAAW,SAAS;EAC5C,OAAO,iBAAiB,WAAW,SAAS;EAC5C,aAAa;GACX,OAAO,oBAAoB,eAAe,aAAa;GACvD,OAAO,oBAAoB,WAAW,SAAS;GAC/C,OAAO,oBAAoB,WAAW,SAAS;EACjD;CACF,GAAG,CAAC,MAAM,UAAU,CAAC;CAErB,MAAM,aAAa,cAAc,MAAM,IAAI,WAAW,IAAI,KAAA;CAC1D,MAAM,UAAU,eACP;EACL;EACA,SAAS;EACT;EACA;EACA;EACA,UAAU;EACV;EACA;EACA;EACA;EACA;EACA;EACA,WAAW,cAAe,cAAc,KAAA,IAAY,KAAA,IAAY,MAAM,IAAI,SAAS,CAAC,EAAE;EACtF,YAAY,cAAc,CAAC,UAAU,KAAK,KAAK,cAAc,IAAI,SAAS;EAC1E,kBAAkB,YAAY,gBAAgB,IAAI,OAAO;EACzD,cAAc,aAAa;EAC3B,cAAc,YAAY;EAC1B,WAAW,GAAG,OAAO;EACrB,QAAQ,GAAG,OAAO;EAClB,SAAS,GAAG,OAAO;EACnB;EACA;EACA;EACA;EACA;EACA,gBAAgB,GAAG,OAAO;CAC5B,IACA;EACE,YAAY;EACZ;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,aAAa;EACb;EACA;CACF,CACF;CAEA,OACE,oBAAC,gBAAgB,UAAjB;EAA0B,OAAO;EAC/B,UAAA,oBAAC,OAAD;GAAK,KAAK;GAAS,WAAW,GAAG,mBAAmB,SAAS;GAC1D;EACE,CAAA;CACmB,CAAA;AAE9B"}
|
|
@@ -71,6 +71,71 @@ function buildOptions(step) {
|
|
|
71
71
|
}
|
|
72
72
|
return out;
|
|
73
73
|
}
|
|
74
|
+
function snapToOption(mins, slots) {
|
|
75
|
+
let best = slots[0];
|
|
76
|
+
for (const slot of slots) if (Math.abs(slot - mins) < Math.abs(best - mins)) best = slot;
|
|
77
|
+
return best;
|
|
78
|
+
}
|
|
79
|
+
function withCurrentOption(filtered, all, current) {
|
|
80
|
+
if (!current || filtered.some((o) => o.value === current)) return filtered;
|
|
81
|
+
const extra = all.find((o) => o.value === current) ?? {
|
|
82
|
+
value: current,
|
|
83
|
+
label: label12(current)
|
|
84
|
+
};
|
|
85
|
+
return [...filtered, extra].sort((a, b) => toMinutes(a.value) - toMinutes(b.value));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Same-day ranges only. A valid pair is left alone so an off-grid persisted
|
|
89
|
+
* end (17:00 with `step={720}`) is not rewritten. When the invariant fails,
|
|
90
|
+
* the just-selected endpoint stays and only the opposite side moves onto a
|
|
91
|
+
* neighboring generated option.
|
|
92
|
+
*/
|
|
93
|
+
function clampRange(start, end, options, changed = "start") {
|
|
94
|
+
const slots = options.map((o) => toMinutes(o.value));
|
|
95
|
+
if (slots.length === 0) return {
|
|
96
|
+
start,
|
|
97
|
+
end
|
|
98
|
+
};
|
|
99
|
+
const startM = toMinutes(start);
|
|
100
|
+
if (toMinutes(end) > startM) return {
|
|
101
|
+
start,
|
|
102
|
+
end
|
|
103
|
+
};
|
|
104
|
+
const keepOrSnap = (value) => {
|
|
105
|
+
const mins = toMinutes(value);
|
|
106
|
+
return slots.includes(mins) ? mins : snapToOption(mins, slots);
|
|
107
|
+
};
|
|
108
|
+
if (changed === "end") {
|
|
109
|
+
const e = keepOrSnap(end);
|
|
110
|
+
const earlier = [...slots].reverse().find((slot) => slot < e);
|
|
111
|
+
if (earlier === void 0 && slots.length > 1) return {
|
|
112
|
+
start: toValue(slots[0]),
|
|
113
|
+
end: toValue(slots[1])
|
|
114
|
+
};
|
|
115
|
+
return {
|
|
116
|
+
start: toValue(earlier ?? keepOrSnap(start)),
|
|
117
|
+
end: toValue(e)
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const s = keepOrSnap(start);
|
|
121
|
+
const later = slots.find((slot) => slot > s);
|
|
122
|
+
if (later === void 0 && slots.length > 1) return {
|
|
123
|
+
start: toValue(slots[slots.length - 2]),
|
|
124
|
+
end: toValue(slots[slots.length - 1])
|
|
125
|
+
};
|
|
126
|
+
return {
|
|
127
|
+
start: toValue(s),
|
|
128
|
+
end: toValue(later ?? keepOrSnap(end))
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function startOptions(options, end, current) {
|
|
132
|
+
const filtered = options.filter((o) => toMinutes(o.value) < toMinutes(end));
|
|
133
|
+
return withCurrentOption(filtered.length === 0 ? options.slice(0, 1) : filtered, options, current);
|
|
134
|
+
}
|
|
135
|
+
function endOptions(options, start, current) {
|
|
136
|
+
const filtered = options.filter((o) => toMinutes(o.value) > toMinutes(start));
|
|
137
|
+
return withCurrentOption(filtered.length === 0 ? options.slice(-1) : filtered, options, current);
|
|
138
|
+
}
|
|
74
139
|
function defaultWeek() {
|
|
75
140
|
const workday = (day) => ({
|
|
76
141
|
enabled: true,
|
|
@@ -203,6 +268,6 @@ function CopyMenu({ fromLabel, reduce, onApply }) {
|
|
|
203
268
|
});
|
|
204
269
|
}
|
|
205
270
|
//#endregion
|
|
206
|
-
export {
|
|
271
|
+
export { defaultWeek as a, startOptions as c, clampRange as i, toMinutes as l, WEEKDAYS as n, endOptions as o, buildOptions as r, panelKey as s, CopyMenu as t, toValue as u };
|
|
207
272
|
|
|
208
|
-
//# sourceMappingURL=copy-menu-
|
|
273
|
+
//# sourceMappingURL=copy-menu-C5GaT4Z-.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"copy-menu-C5GaT4Z-.js","names":[],"sources":["../src/components/motion/availability-scheduler/types.ts","../src/components/motion/availability-scheduler/copy-menu.tsx"],"sourcesContent":["export type DayKey = \"mon\" | \"tue\" | \"wed\" | \"thu\" | \"fri\" | \"sat\" | \"sun\";\n\nexport type TimeRange = { id: string; start: string; end: string };\nexport type DayAvailability = { enabled: boolean; ranges: TimeRange[] };\nexport type WeekAvailability = Record<DayKey, DayAvailability>;\nexport type TimeOption = { value: string; label: string };\n\nexport const WEEKDAYS: { key: DayKey; label: string }[] = [\n { key: \"mon\", label: \"Monday\" },\n { key: \"tue\", label: \"Tuesday\" },\n { key: \"wed\", label: \"Wednesday\" },\n { key: \"thu\", label: \"Thursday\" },\n { key: \"fri\", label: \"Friday\" },\n { key: \"sat\", label: \"Saturday\" },\n { key: \"sun\", label: \"Sunday\" },\n];\n\n// ─── panels ──────────────────────────────────────────────────────────────────\n\n/**\n * Names one time field week-wide, because the scheduler holds a single open\n * panel for the whole week. Range ids belong to the value and are only unique\n * within a day, so the day is part of the name — and the scheduler builds the\n * same keys to ask whether the panel it is holding is still on screen.\n */\nexport const panelKey = (day: DayKey, rangeId: string, edge: \"start\" | \"end\") =>\n `${day}:${rangeId}:${edge}`;\n\n// ─── time helpers ────────────────────────────────────────────────────────────\n\nexport const toMinutes = (v: string) => {\n const [h, m] = v.split(\":\").map(Number);\n return h * 60 + m;\n};\n\nexport const toValue = (mins: number) => {\n const clamped = Math.max(0, Math.min(24 * 60 - 1, mins));\n const h = Math.floor(clamped / 60);\n const m = clamped % 60;\n return `${String(h).padStart(2, \"0\")}:${String(m).padStart(2, \"0\")}`;\n};\n\nexport const label12 = (v: string) => {\n const [h, m] = v.split(\":\").map(Number);\n const ap = h < 12 ? \"AM\" : \"PM\";\n const h12 = h % 12 === 0 ? 12 : h % 12;\n return `${h12}:${String(m).padStart(2, \"0\")} ${ap}`;\n};\n\nexport function buildOptions(step: number): TimeOption[] {\n const out: TimeOption[] = [];\n for (let m = 0; m < 24 * 60; m += step) {\n const value = toValue(m);\n out.push({ value, label: label12(value) });\n }\n return out;\n}\n\nfunction snapToOption(mins: number, slots: number[]) {\n let best = slots[0];\n for (const slot of slots) {\n if (Math.abs(slot - mins) < Math.abs(best - mins)) best = slot;\n }\n return best;\n}\n\nfunction withCurrentOption(filtered: TimeOption[], all: TimeOption[], current?: string) {\n if (!current || filtered.some((o) => o.value === current)) return filtered;\n const extra =\n all.find((o) => o.value === current) ??\n ({ value: current, label: label12(current) } satisfies TimeOption);\n return [...filtered, extra].sort((a, b) => toMinutes(a.value) - toMinutes(b.value));\n}\n\n/**\n * Same-day ranges only. A valid pair is left alone so an off-grid persisted\n * end (17:00 with `step={720}`) is not rewritten. When the invariant fails,\n * the just-selected endpoint stays and only the opposite side moves onto a\n * neighboring generated option.\n */\nexport function clampRange(\n start: string,\n end: string,\n options: TimeOption[],\n changed: \"start\" | \"end\" = \"start\",\n): { start: string; end: string } {\n const slots = options.map((o) => toMinutes(o.value));\n if (slots.length === 0) return { start, end };\n\n const startM = toMinutes(start);\n const endM = toMinutes(end);\n if (endM > startM) return { start, end };\n\n const keepOrSnap = (value: string) => {\n const mins = toMinutes(value);\n return slots.includes(mins) ? mins : snapToOption(mins, slots);\n };\n\n if (changed === \"end\") {\n const e = keepOrSnap(end);\n const earlier = [...slots].reverse().find((slot) => slot < e);\n if (earlier === undefined && slots.length > 1) {\n // Midnight cannot end a positive same-day range, so use the first pair.\n return { start: toValue(slots[0]), end: toValue(slots[1]) };\n }\n return {\n start: toValue(earlier ?? keepOrSnap(start)),\n end: toValue(e),\n };\n }\n\n const s = keepOrSnap(start);\n const later = slots.find((slot) => slot > s);\n if (later === undefined && slots.length > 1) {\n // The last slot cannot start a positive range, so use the final pair.\n return {\n start: toValue(slots[slots.length - 2]),\n end: toValue(slots[slots.length - 1]),\n };\n }\n return {\n start: toValue(s),\n end: toValue(later ?? keepOrSnap(end)),\n };\n}\n\nexport function startOptions(options: TimeOption[], end: string, current?: string) {\n const filtered = options.filter((o) => toMinutes(o.value) < toMinutes(end));\n // An invalid midnight end has no earlier option; expose midnight so choosing\n // it can move the end forward through clampRange instead of trapping the row.\n const recovery = filtered.length === 0 ? options.slice(0, 1) : filtered;\n return withCurrentOption(recovery, options, current);\n}\n\nexport function endOptions(options: TimeOption[], start: string, current?: string) {\n const filtered = options.filter((o) => toMinutes(o.value) > toMinutes(start));\n // An invalid last-slot start has no later option; expose the last slot so\n // choosing it can move the start backward through clampRange.\n const recovery = filtered.length === 0 ? options.slice(-1) : filtered;\n return withCurrentOption(recovery, options, current);\n}\n\n// Default: Mon–Fri 9–5, weekend off. Fixed ids so SSR and first client render\n// agree (new ranges get counter ids afterwards).\nexport function defaultWeek(): WeekAvailability {\n const workday = (day: DayKey): DayAvailability => ({\n enabled: true,\n ranges: [{ id: `${day}-0`, start: \"09:00\", end: \"17:00\" }],\n });\n const off = (day: DayKey): DayAvailability => ({\n enabled: false,\n ranges: [{ id: `${day}-0`, start: \"09:00\", end: \"17:00\" }],\n });\n return {\n mon: workday(\"mon\"),\n tue: workday(\"tue\"),\n wed: workday(\"wed\"),\n thu: workday(\"thu\"),\n fri: workday(\"fri\"),\n sat: off(\"sat\"),\n sun: off(\"sun\"),\n };\n}\n","\"use client\";\n\nimport { useState } from \"react\";\nimport { Check, Copy } from \"lucide-react\";\nimport { AnimatePresence, motion } from \"motion/react\";\n\nimport { SPRING_PRESS } from \"../../../lib/ease\";\nimport { Checkbox } from \"../checkbox\";\nimport { MorphPopover, MorphPopoverContent } from \"../popover-morph\";\nimport { Tooltip } from \"../tooltip\";\nimport { IconButton } from \"./icon-button\";\nimport { type DayKey, WEEKDAYS } from \"./types\";\n\n// Copy this day's hours to other days: a morph popover with a day picker.\nexport function CopyMenu({\n fromLabel,\n reduce,\n onApply,\n}: {\n fromLabel: string;\n reduce: boolean;\n onApply: (targets: DayKey[]) => void;\n}) {\n const [open, setOpen] = useState(false);\n const [copied, setCopied] = useState(false);\n const [picked, setPicked] = useState<Set<DayKey>>(new Set());\n const others = WEEKDAYS.filter((d) => d.label !== fromLabel);\n\n const toggle = (k: DayKey) =>\n setPicked((prev) => {\n const next = new Set(prev);\n if (next.has(k)) next.delete(k);\n else next.add(k);\n return next;\n });\n\n const apply = (targets: DayKey[]) => {\n if (!targets.length) return;\n onApply(targets);\n setOpen(false);\n setPicked(new Set());\n setCopied(true);\n window.setTimeout(() => setCopied(false), 1200);\n };\n\n return (\n <MorphPopover open={open} onOpenChange={setOpen}>\n <Tooltip content=\"Copy times\">\n <IconButton\n label={`Copy ${fromLabel} hours to other days`}\n reduce={reduce}\n expanded={open}\n onClick={() => setOpen(!open)}\n >\n <AnimatePresence mode=\"popLayout\" initial={false}>\n {copied ? (\n <motion.span\n key=\"done\"\n initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.5 }}\n animate={{ opacity: 1, scale: 1 }}\n exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.5 }}\n transition={SPRING_PRESS}\n className=\"text-foreground\"\n >\n <Check className=\"h-4 w-4\" />\n </motion.span>\n ) : (\n <motion.span\n key=\"copy\"\n initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.5 }}\n animate={{ opacity: 1, scale: 1 }}\n exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.5 }}\n transition={SPRING_PRESS}\n >\n <Copy className=\"h-4 w-4\" />\n </motion.span>\n )}\n </AnimatePresence>\n </IconButton>\n </Tooltip>\n\n <MorphPopoverContent align=\"end\" className=\"w-52 p-2\">\n <p className=\"px-2 py-1.5 font-medium text-muted-foreground text-xs\">Copy times to</p>\n <div className=\"flex flex-col\">\n {others.map((d) => (\n <Checkbox\n key={d.key}\n checked={picked.has(d.key)}\n onCheckedChange={() => toggle(d.key)}\n label={d.label}\n className=\"w-full flex-row-reverse justify-between rounded-lg px-2 py-1.5 transition-colors hover:bg-muted [&_button[data-state=unchecked]]:border-border-strong [&_button]:size-4 [&_button]:rounded-[5px] [&_button]:border\"\n />\n ))}\n </div>\n <div className=\"mt-1 flex items-center gap-2 border-border border-t px-1 pt-2\">\n <button\n type=\"button\"\n onClick={() => apply(others.map((d) => d.key))}\n className=\"flex-1 rounded-lg px-2 py-1.5 font-medium text-muted-foreground text-xs outline-none transition-colors hover:bg-muted hover:text-foreground focus-visible:bg-muted\"\n >\n Every day\n </button>\n <button\n type=\"button\"\n onClick={() => apply([...picked])}\n disabled={picked.size === 0}\n className=\"flex-1 rounded-lg bg-primary px-2 py-1.5 font-semibold text-primary-foreground text-xs outline-none transition-opacity hover:opacity-90 focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-40\"\n >\n Apply\n </button>\n </div>\n </MorphPopoverContent>\n </MorphPopover>\n );\n}\n"],"mappings":";;;;;;;;;;AAOA,MAAa,WAA6C;CACxD;EAAE,KAAK;EAAO,OAAO;CAAS;CAC9B;EAAE,KAAK;EAAO,OAAO;CAAU;CAC/B;EAAE,KAAK;EAAO,OAAO;CAAY;CACjC;EAAE,KAAK;EAAO,OAAO;CAAW;CAChC;EAAE,KAAK;EAAO,OAAO;CAAS;CAC9B;EAAE,KAAK;EAAO,OAAO;CAAW;CAChC;EAAE,KAAK;EAAO,OAAO;CAAS;AAChC;;;;;;;AAUA,MAAa,YAAY,KAAa,SAAiB,SACrD,GAAG,IAAI,GAAG,QAAQ,GAAG;AAIvB,MAAa,aAAa,MAAc;CACtC,MAAM,CAAC,GAAG,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,MAAM;CACtC,OAAO,IAAI,KAAK;AAClB;AAEA,MAAa,WAAW,SAAiB;CACvC,MAAM,UAAU,KAAK,IAAI,GAAG,KAAK,IAAI,MAAa,IAAI,CAAC;CACvD,MAAM,IAAI,KAAK,MAAM,UAAU,EAAE;CACjC,MAAM,IAAI,UAAU;CACpB,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,SAAS,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC,CAAC,CAAC,SAAS,GAAG,GAAG;AACnE;AAEA,MAAa,WAAW,MAAc;CACpC,MAAM,CAAC,GAAG,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,MAAM;CACtC,MAAM,KAAK,IAAI,KAAK,OAAO;CAE3B,OAAO,GADK,IAAI,OAAO,IAAI,KAAK,IAAI,GACtB,GAAG,OAAO,CAAC,CAAC,CAAC,SAAS,GAAG,GAAG,EAAE,GAAG;AACjD;AAEA,SAAgB,aAAa,MAA4B;CACvD,MAA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
|
+
* Opening a list starts a fresh session — an empty query, the highlight back at
|
|
6
|
+
* the top — and that is a resolution, not a side effect: a passive effect runs
|
|
7
|
+
* after the commit, so the new session would carry the last one's state for a
|
|
8
|
+
* window in which a key press can land.
|
|
9
|
+
*
|
|
10
|
+
* `start` may only set state belonging to the calling component. Anything that
|
|
11
|
+
* reaches outside it — a consumer's callback, a DOM write, moving focus — is a
|
|
12
|
+
* side effect, and React rejects a callback that sets another component's state
|
|
13
|
+
* during this one's render. Put those in an effect keyed to `open`.
|
|
14
|
+
*/
|
|
15
|
+
declare function useOnOpen(open: boolean, start: () => void): void;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { useOnOpen };
|
|
18
|
+
//# sourceMappingURL=use-on-open.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-on-open.d.ts","names":[],"sources":["../../src/hooks/use-on-open.ts"],"mappings":";;;;;;;;;;;;;;iBAiBgB,UAAU,eAAe"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
//#region src/hooks/use-on-open.ts
|
|
4
|
+
/**
|
|
5
|
+
* Runs `start` during the render in which `open` becomes true.
|
|
6
|
+
*
|
|
7
|
+
* Opening a list starts a fresh session — an empty query, the highlight back at
|
|
8
|
+
* the top — and that is a resolution, not a side effect: a passive effect runs
|
|
9
|
+
* after the commit, so the new session would carry the last one's state for a
|
|
10
|
+
* window in which a key press can land.
|
|
11
|
+
*
|
|
12
|
+
* `start` may only set state belonging to the calling component. Anything that
|
|
13
|
+
* reaches outside it — a consumer's callback, a DOM write, moving focus — is a
|
|
14
|
+
* side effect, and React rejects a callback that sets another component's state
|
|
15
|
+
* during this one's render. Put those in an effect keyed to `open`.
|
|
16
|
+
*/
|
|
17
|
+
function useOnOpen(open, start) {
|
|
18
|
+
const [wasOpen, setWasOpen] = useState(open);
|
|
19
|
+
if (open !== wasOpen) {
|
|
20
|
+
setWasOpen(open);
|
|
21
|
+
if (open) start();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { useOnOpen };
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=use-on-open.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-on-open.js","names":[],"sources":["../../src/hooks/use-on-open.ts"],"sourcesContent":["\"use client\";\n\nimport { useState } from \"react\";\n\n/**\n * Runs `start` during the render in which `open` becomes true.\n *\n * Opening a list starts a fresh session — an empty query, the highlight back at\n * the top — and that is a resolution, not a side effect: a passive effect runs\n * after the commit, so the new session would carry the last one's state for a\n * window in which a key press can land.\n *\n * `start` may only set state belonging to the calling component. Anything that\n * reaches outside it — a consumer's callback, a DOM write, moving focus — is a\n * side effect, and React rejects a callback that sets another component's state\n * during this one's render. Put those in an effect keyed to `open`.\n */\nexport function useOnOpen(open: boolean, start: () => void) {\n const [wasOpen, setWasOpen] = useState(open);\n if (open !== wasOpen) {\n setWasOpen(open);\n if (open) start();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAiBA,SAAgB,UAAU,MAAe,OAAmB;CAC1D,MAAM,CAAC,SAAS,cAAc,SAAS,IAAI;CAC3C,IAAI,SAAS,SAAS;EACpB,WAAW,IAAI;EACf,IAAI,MAAM,MAAM;CAClB;AACF"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
//#region src/hooks/use-row-cursor.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The highlighted row of a list whose rows can change under it.
|
|
4
|
+
*
|
|
5
|
+
* Which row is highlighted is resolved during render, never in a passive
|
|
6
|
+
* effect: a passive effect runs after the commit, so a list that had just
|
|
7
|
+
* changed would carry an `aria-activedescendant` naming a row that has left it,
|
|
8
|
+
* and a key pressed in that window would commit the wrong row or nothing at
|
|
9
|
+
* all.
|
|
10
|
+
*
|
|
11
|
+
* The cursor holds the row's id, not its position. A position alone cannot tell
|
|
12
|
+
* a list that shrank from one that swapped its rows for a different set of the
|
|
13
|
+
* same length, and the second case is the one that silently hands Enter to a
|
|
14
|
+
* row the user never chose. A cursor whose row has left the list returns the
|
|
15
|
+
* highlight to the first row rather than to the nearest surviving one: the row
|
|
16
|
+
* the user aimed at is gone, and the first row is where a new query already
|
|
17
|
+
* puts the highlight.
|
|
18
|
+
*
|
|
19
|
+
* Pass the query the list is filtered by. The cursor is stamped with it and
|
|
20
|
+
* dropped when it changes, so a caller cannot forget to clear it — including a
|
|
21
|
+
* caller whose query arrives as a prop and so never runs its own handler. Rows
|
|
22
|
+
* that come back under a query that has moved on cannot revive it either.
|
|
23
|
+
* `moveTo(null)` is for deliberate resets, such as reopening the list.
|
|
24
|
+
*
|
|
25
|
+
* Both callbacks keep one identity for the life of the component, and read the
|
|
26
|
+
* rows and the query through a ref to do it. A caller will put them in an
|
|
27
|
+
* effect's dependencies — the exhaustive-deps rule makes it — and a `moveTo`
|
|
28
|
+
* rebuilt on every keystroke would re-run that effect on every keystroke.
|
|
29
|
+
*/
|
|
30
|
+
declare function useRowCursor(rows: readonly {
|
|
31
|
+
id: string;
|
|
32
|
+
}[], query: string): {
|
|
33
|
+
activeIndex: number;
|
|
34
|
+
moveTo: (id: string | null) => void;
|
|
35
|
+
moveActive: (direction: 1 | -1) => void;
|
|
36
|
+
};
|
|
37
|
+
//#endregion
|
|
38
|
+
export { useRowCursor };
|
|
39
|
+
//# sourceMappingURL=use-row-cursor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-row-cursor.d.ts","names":[],"sources":["../../src/hooks/use-row-cursor.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA4CgB,aAAa;EAAiB;KAAgB;;EAkBrD,SAAA;EAIoC,aAAA"}
|