@kumix/ui 0.3.3 → 0.3.5
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/dist/components/agents/agent-activity/activity-row.d.ts +1 -1
- package/dist/components/agents/agent-activity/index.d.ts +2 -2
- package/dist/components/agents/agent-activity/index.d.ts.map +1 -1
- package/dist/components/agents/agent-activity/index.js +9 -3
- package/dist/components/agents/agent-activity/index.js.map +1 -1
- package/dist/components/agents/loading-states/reasoning-text.d.ts.map +1 -1
- package/dist/components/agents/loading-states/reasoning-text.js +7 -39
- package/dist/components/agents/loading-states/reasoning-text.js.map +1 -1
- package/dist/components/motion/checkbox.d.ts +3 -1
- package/dist/components/motion/checkbox.d.ts.map +1 -1
- package/dist/components/motion/checkbox.js +2 -1
- package/dist/components/motion/checkbox.js.map +1 -1
- package/dist/components/motion/combobox/content.d.ts +16 -0
- package/dist/components/motion/combobox/content.d.ts.map +1 -0
- package/dist/components/motion/combobox/content.js +91 -0
- package/dist/components/motion/combobox/content.js.map +1 -0
- package/dist/components/motion/combobox/context.d.ts +63 -0
- package/dist/components/motion/combobox/context.d.ts.map +1 -0
- package/dist/components/motion/combobox/context.js +235 -0
- package/dist/components/motion/combobox/context.js.map +1 -0
- package/dist/components/motion/combobox/list.d.ts +40 -0
- package/dist/components/motion/combobox/list.d.ts.map +1 -0
- package/dist/components/motion/combobox/list.js +143 -0
- package/dist/components/motion/combobox/list.js.map +1 -0
- package/dist/components/motion/combobox/trigger.d.ts +21 -0
- package/dist/components/motion/combobox/trigger.d.ts.map +1 -0
- package/dist/components/motion/combobox/trigger.js +115 -0
- package/dist/components/motion/combobox/trigger.js.map +1 -0
- package/dist/components/motion/combobox.d.ts +5 -0
- package/dist/components/motion/combobox.js +6 -0
- package/dist/components/motion/popover.d.ts.map +1 -1
- package/dist/components/motion/popover.js +22 -25
- package/dist/components/motion/popover.js.map +1 -1
- package/dist/components/motion/signup-form.d.ts +56 -0
- package/dist/components/motion/signup-form.d.ts.map +1 -0
- package/dist/components/motion/signup-form.js +337 -0
- package/dist/components/motion/signup-form.js.map +1 -0
- package/dist/components/motion/text-scramble.d.ts +17 -0
- package/dist/components/motion/text-scramble.d.ts.map +1 -0
- package/dist/components/motion/text-scramble.js +64 -0
- package/dist/components/motion/text-scramble.js.map +1 -0
- package/dist/components/reui/alert.d.ts +1 -1
- package/dist/components/reui/badge.d.ts +1 -1
- package/dist/components/reui/frame.d.ts +1 -1
- package/dist/components/reui/icon-tile.d.ts +1 -1
- package/dist/components/ui/attachment.d.ts +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-group.d.ts +1 -1
- package/dist/components/ui/button.d.ts +2 -2
- package/dist/components/ui/field.d.ts +1 -1
- package/dist/components/ui/item.d.ts +1 -1
- package/dist/components/ui/sidebar.d.ts +1 -1
- package/dist/components/ui/toggle.d.ts +1 -1
- package/dist/{types-j36vVGtZ.d.ts → types-kSZH1k8K.d.ts} +11 -1
- package/dist/{types-j36vVGtZ.d.ts.map → types-kSZH1k8K.d.ts.map} +1 -1
- package/package.json +8 -8
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, useCallback, useContext, useEffect, useId, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { cn } from "@kumix/utils";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
import { useReducedMotion } from "motion/react";
|
|
6
|
+
//#region src/components/motion/combobox/context.tsx
|
|
7
|
+
const defaultFilter = (value, query, keywords) => {
|
|
8
|
+
const needle = query.trim().toLocaleLowerCase();
|
|
9
|
+
if (!needle) return true;
|
|
10
|
+
const haystack = [value, ...keywords].join(" ").toLocaleLowerCase();
|
|
11
|
+
let queryIndex = 0;
|
|
12
|
+
for (const character of haystack) {
|
|
13
|
+
if (character === needle[queryIndex]) queryIndex += 1;
|
|
14
|
+
if (queryIndex === needle.length) return true;
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
};
|
|
18
|
+
const ComboboxContext = createContext(null);
|
|
19
|
+
const ComboboxGroupContext = createContext(null);
|
|
20
|
+
function useComboboxContext(component) {
|
|
21
|
+
const context = useContext(ComboboxContext);
|
|
22
|
+
if (!context) throw new Error(`${component} must be used within <Combobox>`);
|
|
23
|
+
return context;
|
|
24
|
+
}
|
|
25
|
+
function mergeRefs(...refs) {
|
|
26
|
+
return (node) => {
|
|
27
|
+
for (const ref of refs) if (typeof ref === "function") ref(node);
|
|
28
|
+
else if (ref && typeof ref === "object") ref.current = node;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function Combobox({ children, value: controlledValue, defaultValue, onValueChange, open: controlledOpen, defaultOpen = false, onOpenChange, query: controlledQuery, defaultQuery = "", onQueryChange, filter = defaultFilter, disabled = false, className }) {
|
|
32
|
+
const reduce = useReducedMotion() ?? false;
|
|
33
|
+
const baseId = useId();
|
|
34
|
+
const rootRef = useRef(null);
|
|
35
|
+
const triggerRef = useRef(null);
|
|
36
|
+
const contentRef = useRef(null);
|
|
37
|
+
const inputRef = useRef(null);
|
|
38
|
+
const [internalValue, setInternalValue] = useState(defaultValue);
|
|
39
|
+
const [internalOpen, setInternalOpen] = useState(defaultOpen);
|
|
40
|
+
const [internalQuery, setInternalQuery] = useState(defaultQuery);
|
|
41
|
+
const [activeValue, setActiveValue] = useState(null);
|
|
42
|
+
const [items, setItems] = useState(/* @__PURE__ */ new Map());
|
|
43
|
+
const valueControlled = controlledValue !== void 0;
|
|
44
|
+
const openControlled = controlledOpen !== void 0;
|
|
45
|
+
const queryControlled = controlledQuery !== void 0;
|
|
46
|
+
const value = valueControlled ? controlledValue : internalValue;
|
|
47
|
+
const open = openControlled ? controlledOpen : internalOpen;
|
|
48
|
+
const query = queryControlled ? controlledQuery : internalQuery;
|
|
49
|
+
const updateQuery = useCallback((next) => {
|
|
50
|
+
if (!queryControlled) setInternalQuery(next);
|
|
51
|
+
onQueryChange?.(next);
|
|
52
|
+
}, [onQueryChange, queryControlled]);
|
|
53
|
+
const updateOpen = useCallback((next, restoreFocus = false) => {
|
|
54
|
+
if (disabled && next) return;
|
|
55
|
+
if (!openControlled) setInternalOpen(next);
|
|
56
|
+
onOpenChange?.(next);
|
|
57
|
+
if (!next) updateQuery("");
|
|
58
|
+
if (restoreFocus) requestAnimationFrame(() => inputRef.current?.focus({ preventScroll: true }));
|
|
59
|
+
}, [
|
|
60
|
+
disabled,
|
|
61
|
+
onOpenChange,
|
|
62
|
+
openControlled,
|
|
63
|
+
updateQuery
|
|
64
|
+
]);
|
|
65
|
+
const registerItem = useCallback((item) => {
|
|
66
|
+
setItems((current) => {
|
|
67
|
+
const existing = current.get(item.value);
|
|
68
|
+
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;
|
|
69
|
+
const next = new Map(current);
|
|
70
|
+
next.set(item.value, item);
|
|
71
|
+
return next;
|
|
72
|
+
});
|
|
73
|
+
}, []);
|
|
74
|
+
const unregisterItem = useCallback((itemValue) => {
|
|
75
|
+
setItems((current) => {
|
|
76
|
+
if (!current.has(itemValue)) return current;
|
|
77
|
+
const next = new Map(current);
|
|
78
|
+
next.delete(itemValue);
|
|
79
|
+
return next;
|
|
80
|
+
});
|
|
81
|
+
}, []);
|
|
82
|
+
const visibleItems = useMemo(() => Array.from(items.values()).filter((item) => filter(item.value, query, [item.label, ...item.keywords])), [
|
|
83
|
+
filter,
|
|
84
|
+
items,
|
|
85
|
+
query
|
|
86
|
+
]);
|
|
87
|
+
const enabledVisibleItems = useMemo(() => visibleItems.filter((item) => !item.disabled), [visibleItems]);
|
|
88
|
+
const visibleValues = useMemo(() => new Set(visibleItems.map((item) => item.value)), [visibleItems]);
|
|
89
|
+
const visibleGroupIds = useMemo(() => new Set(visibleItems.map((item) => item.groupId)), [visibleItems]);
|
|
90
|
+
const select = useCallback((next) => {
|
|
91
|
+
if (items.get(next)?.disabled) return;
|
|
92
|
+
if (!valueControlled) setInternalValue(next);
|
|
93
|
+
onValueChange?.(next);
|
|
94
|
+
updateOpen(false, true);
|
|
95
|
+
}, [
|
|
96
|
+
items,
|
|
97
|
+
onValueChange,
|
|
98
|
+
updateOpen,
|
|
99
|
+
valueControlled
|
|
100
|
+
]);
|
|
101
|
+
const moveActive = useCallback((direction) => {
|
|
102
|
+
if (!enabledVisibleItems.length) {
|
|
103
|
+
setActiveValue(null);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (direction === "first") {
|
|
107
|
+
setActiveValue(enabledVisibleItems[0].value);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (direction === "last") {
|
|
111
|
+
setActiveValue(enabledVisibleItems.at(-1)?.value ?? null);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const currentIndex = enabledVisibleItems.findIndex((item) => item.value === activeValue);
|
|
115
|
+
const nextIndex = currentIndex < 0 ? direction === 1 ? 0 : enabledVisibleItems.length - 1 : (currentIndex + direction + enabledVisibleItems.length) % enabledVisibleItems.length;
|
|
116
|
+
setActiveValue(enabledVisibleItems[nextIndex].value);
|
|
117
|
+
}, [activeValue, enabledVisibleItems]);
|
|
118
|
+
const selectActive = useCallback(() => {
|
|
119
|
+
if (activeValue) select(activeValue);
|
|
120
|
+
}, [activeValue, select]);
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
if (!open) return;
|
|
123
|
+
const selectedVisible = value && visibleValues.has(value) ? value : null;
|
|
124
|
+
const activeVisible = activeValue && visibleValues.has(activeValue) ? activeValue : null;
|
|
125
|
+
setActiveValue(activeVisible ?? selectedVisible ?? enabledVisibleItems[0]?.value ?? null);
|
|
126
|
+
}, [
|
|
127
|
+
activeValue,
|
|
128
|
+
enabledVisibleItems,
|
|
129
|
+
open,
|
|
130
|
+
value,
|
|
131
|
+
visibleValues
|
|
132
|
+
]);
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
if (!open) return;
|
|
135
|
+
requestAnimationFrame(() => inputRef.current?.focus({ preventScroll: true }));
|
|
136
|
+
}, [open]);
|
|
137
|
+
useEffect(() => {
|
|
138
|
+
if (!activeValue || !open) return;
|
|
139
|
+
const item = items.get(activeValue)?.ref.current;
|
|
140
|
+
const list = item?.closest("[role='listbox']");
|
|
141
|
+
if (!item || !list) return;
|
|
142
|
+
const itemRect = item.getBoundingClientRect();
|
|
143
|
+
const listRect = list.getBoundingClientRect();
|
|
144
|
+
if (itemRect.top < listRect.top) list.scrollTop -= listRect.top - itemRect.top;
|
|
145
|
+
else if (itemRect.bottom > listRect.bottom) list.scrollTop += itemRect.bottom - listRect.bottom;
|
|
146
|
+
}, [
|
|
147
|
+
activeValue,
|
|
148
|
+
items,
|
|
149
|
+
open
|
|
150
|
+
]);
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
if (!open) return;
|
|
153
|
+
const isInside = (target) => rootRef.current?.contains(target) || contentRef.current?.contains(target);
|
|
154
|
+
const onPointerDown = (event) => {
|
|
155
|
+
if (!isInside(event.target)) updateOpen(false);
|
|
156
|
+
};
|
|
157
|
+
const onFocusIn = (event) => {
|
|
158
|
+
if (!isInside(event.target)) updateOpen(false);
|
|
159
|
+
};
|
|
160
|
+
const onKeyDown = (event) => {
|
|
161
|
+
if (event.key !== "Escape") return;
|
|
162
|
+
event.preventDefault();
|
|
163
|
+
updateOpen(false, true);
|
|
164
|
+
};
|
|
165
|
+
window.addEventListener("pointerdown", onPointerDown);
|
|
166
|
+
window.addEventListener("focusin", onFocusIn);
|
|
167
|
+
window.addEventListener("keydown", onKeyDown);
|
|
168
|
+
return () => {
|
|
169
|
+
window.removeEventListener("pointerdown", onPointerDown);
|
|
170
|
+
window.removeEventListener("focusin", onFocusIn);
|
|
171
|
+
window.removeEventListener("keydown", onKeyDown);
|
|
172
|
+
};
|
|
173
|
+
}, [open, updateOpen]);
|
|
174
|
+
const activeItem = activeValue ? items.get(activeValue) : void 0;
|
|
175
|
+
const context = useMemo(() => ({
|
|
176
|
+
open,
|
|
177
|
+
setOpen: updateOpen,
|
|
178
|
+
value,
|
|
179
|
+
select,
|
|
180
|
+
query,
|
|
181
|
+
setQuery: updateQuery,
|
|
182
|
+
activeValue,
|
|
183
|
+
setActiveValue,
|
|
184
|
+
moveActive,
|
|
185
|
+
selectActive,
|
|
186
|
+
registerItem,
|
|
187
|
+
unregisterItem,
|
|
188
|
+
labelFor: (itemValue) => itemValue === void 0 ? void 0 : items.get(itemValue)?.label,
|
|
189
|
+
isVisible: (itemValue) => !query.trim() || visibleValues.has(itemValue),
|
|
190
|
+
hasVisibleItems: (groupId) => visibleGroupIds.has(groupId),
|
|
191
|
+
visibleCount: visibleItems.length,
|
|
192
|
+
activeItemId: activeItem?.id,
|
|
193
|
+
triggerId: `${baseId}-trigger`,
|
|
194
|
+
listId: `${baseId}-list`,
|
|
195
|
+
inputId: `${baseId}-input`,
|
|
196
|
+
disabled,
|
|
197
|
+
reduce,
|
|
198
|
+
triggerRef,
|
|
199
|
+
contentRef,
|
|
200
|
+
inputRef,
|
|
201
|
+
activeLayoutId: `${baseId}-active`
|
|
202
|
+
}), [
|
|
203
|
+
activeItem?.id,
|
|
204
|
+
activeValue,
|
|
205
|
+
baseId,
|
|
206
|
+
disabled,
|
|
207
|
+
items,
|
|
208
|
+
moveActive,
|
|
209
|
+
open,
|
|
210
|
+
query,
|
|
211
|
+
reduce,
|
|
212
|
+
registerItem,
|
|
213
|
+
select,
|
|
214
|
+
selectActive,
|
|
215
|
+
unregisterItem,
|
|
216
|
+
updateOpen,
|
|
217
|
+
updateQuery,
|
|
218
|
+
value,
|
|
219
|
+
visibleItems.length,
|
|
220
|
+
visibleGroupIds,
|
|
221
|
+
visibleValues
|
|
222
|
+
]);
|
|
223
|
+
return /* @__PURE__ */ jsx(ComboboxContext.Provider, {
|
|
224
|
+
value: context,
|
|
225
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
226
|
+
ref: rootRef,
|
|
227
|
+
className: cn("relative w-full", className),
|
|
228
|
+
children
|
|
229
|
+
})
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
//#endregion
|
|
233
|
+
export { Combobox, ComboboxContext, ComboboxGroupContext, mergeRefs, useComboboxContext };
|
|
234
|
+
|
|
235
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","names":[],"sources":["../../../../src/components/motion/combobox/context.tsx"],"sourcesContent":["\"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\";\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 activeValue: string | null;\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 [activeValue, setActiveValue] = useState<string | null>(null);\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 const visibleItems = useMemo(\n () =>\n Array.from(items.values()).filter((item) =>\n filter(item.value, query, [item.label, ...item.keywords]),\n ),\n [filter, items, query],\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 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 moveActive = useCallback(\n (direction: 1 | -1 | \"first\" | \"last\") => {\n if (!enabledVisibleItems.length) {\n setActiveValue(null);\n return;\n }\n if (direction === \"first\") {\n setActiveValue(enabledVisibleItems[0].value);\n return;\n }\n if (direction === \"last\") {\n setActiveValue(enabledVisibleItems.at(-1)?.value ?? null);\n return;\n }\n\n const currentIndex = enabledVisibleItems.findIndex((item) => item.value === activeValue);\n const nextIndex =\n currentIndex < 0\n ? direction === 1\n ? 0\n : enabledVisibleItems.length - 1\n : (currentIndex + direction + enabledVisibleItems.length) % enabledVisibleItems.length;\n setActiveValue(enabledVisibleItems[nextIndex].value);\n },\n [activeValue, enabledVisibleItems],\n );\n\n const selectActive = useCallback(() => {\n if (activeValue) select(activeValue);\n }, [activeValue, select]);\n\n useEffect(() => {\n if (!open) return;\n const selectedVisible = value && visibleValues.has(value) ? value : null;\n const activeVisible = activeValue && visibleValues.has(activeValue) ? activeValue : null;\n setActiveValue(activeVisible ?? selectedVisible ?? enabledVisibleItems[0]?.value ?? null);\n }, [activeValue, enabledVisibleItems, open, value, visibleValues]);\n\n useEffect(() => {\n if (!open) return;\n requestAnimationFrame(() => inputRef.current?.focus({ preventScroll: true }));\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) => !query.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 moveActive,\n open,\n query,\n reduce,\n registerItem,\n select,\n selectActive,\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 </ComboboxContext.Provider>\n );\n}\n"],"mappings":";;;;;;AA+BA,MAAM,iBAAiC,OAAO,OAAO,aAAa;CAChE,MAAM,SAAS,MAAM,KAAK,CAAC,CAAC,kBAAkB;CAC9C,IAAI,CAAC,QAAQ,OAAO;CAEpB,MAAM,WAAW,CAAC,OAAO,GAAG,QAAQ,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,kBAAkB;CA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,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,MAAM,KAAK,KAAK,cAAc,IAAI,SAAS;EACtE,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,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"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
//#region src/components/motion/combobox/list.d.ts
|
|
3
|
+
interface ComboboxListProps {
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
ariaLabel?: string;
|
|
6
|
+
className?: string;
|
|
7
|
+
}
|
|
8
|
+
declare function ComboboxList({ children, ariaLabel, className }: ComboboxListProps): import("react").JSX.Element;
|
|
9
|
+
interface ComboboxGroupProps {
|
|
10
|
+
children: ReactNode;
|
|
11
|
+
className?: string;
|
|
12
|
+
}
|
|
13
|
+
declare function ComboboxGroup({ children, className }: ComboboxGroupProps): import("react").JSX.Element;
|
|
14
|
+
interface ComboboxLabelProps {
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
className?: string;
|
|
17
|
+
}
|
|
18
|
+
declare function ComboboxLabel({ children, className }: ComboboxLabelProps): import("react").JSX.Element;
|
|
19
|
+
interface ComboboxItemProps {
|
|
20
|
+
value: string;
|
|
21
|
+
children: ReactNode;
|
|
22
|
+
textValue?: string;
|
|
23
|
+
keywords?: string[];
|
|
24
|
+
disabled?: boolean;
|
|
25
|
+
onSelect?: (value: string) => void;
|
|
26
|
+
className?: string;
|
|
27
|
+
}
|
|
28
|
+
declare function ComboboxItem({ value, children, textValue, keywords, disabled, onSelect, className }: ComboboxItemProps): import("react").JSX.Element | null;
|
|
29
|
+
interface ComboboxEmptyProps {
|
|
30
|
+
children?: ReactNode;
|
|
31
|
+
className?: string;
|
|
32
|
+
}
|
|
33
|
+
declare function ComboboxEmpty({ children, className }: ComboboxEmptyProps): import("react").JSX.Element | null;
|
|
34
|
+
interface ComboboxSeparatorProps {
|
|
35
|
+
className?: string;
|
|
36
|
+
}
|
|
37
|
+
declare function ComboboxSeparator({ className }: ComboboxSeparatorProps): import("react").JSX.Element;
|
|
38
|
+
//#endregion
|
|
39
|
+
export { ComboboxEmpty, ComboboxEmptyProps, ComboboxGroup, ComboboxGroupProps, ComboboxItem, ComboboxItemProps, ComboboxLabel, ComboboxLabelProps, ComboboxList, ComboboxListProps, ComboboxSeparator, ComboboxSeparatorProps };
|
|
40
|
+
//# sourceMappingURL=list.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"list.d.ts","names":[],"sources":["../../../../src/components/motion/combobox/list.tsx"],"mappings":";;UAUiB;EACf,UAAU;EACV;EACA;;iBAGc,eAAe,UAAU,WAAuB,aAAa,oCAAiB,IAAA;UAiB7E;EACf,UAAU;EACV;;iBAGc,gBAAgB,UAAU,aAAa,qCAAkB,IAAA;UAexD;EACf,UAAU;EACV;;iBAGc,gBAAgB,UAAU,aAAa,qCAAkB,IAAA;UAaxD;EACf;EACA,UAAU;EACV;EACA;EACA;EACA,YAAY;EACZ;;iBAGc,eACd,OACA,UACA,WACA,UACA,UACA,UACA,aACC,oCAAiB,IAAA;UAoFH;EACf,WAAW;EACX;;iBAGc,gBAAgB,UAAgC,aAAa,qCAAkB,IAAA;UAa9E;EACf;;iBAGc,oBAAoB,aAAa,yCAAsB,IAAA"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { EASE_OUT, SPRING_LAYOUT } from "../../../lib/ease.js";
|
|
3
|
+
import { ComboboxGroupContext, useComboboxContext } from "./context.js";
|
|
4
|
+
import { useContext, useId, useLayoutEffect, useMemo, useRef } from "react";
|
|
5
|
+
import { cn } from "@kumix/utils";
|
|
6
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
import { motion } from "motion/react";
|
|
8
|
+
import { Check } from "lucide-react";
|
|
9
|
+
//#region src/components/motion/combobox/list.tsx
|
|
10
|
+
function ComboboxList({ children, ariaLabel = "Options", className }) {
|
|
11
|
+
const context = useComboboxContext("ComboboxList");
|
|
12
|
+
return /* @__PURE__ */ jsx("div", {
|
|
13
|
+
id: context.listId,
|
|
14
|
+
role: "listbox",
|
|
15
|
+
"aria-label": ariaLabel,
|
|
16
|
+
className: cn("max-h-64 overflow-y-auto overscroll-contain p-1.5 [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden", className),
|
|
17
|
+
children
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function ComboboxGroup({ children, className }) {
|
|
21
|
+
const context = useComboboxContext("ComboboxGroup");
|
|
22
|
+
const groupId = useId();
|
|
23
|
+
return /* @__PURE__ */ jsx(ComboboxGroupContext.Provider, {
|
|
24
|
+
value: groupId,
|
|
25
|
+
children: /* @__PURE__ */ jsx("fieldset", {
|
|
26
|
+
hidden: !context.hasVisibleItems(groupId),
|
|
27
|
+
className: cn("m-0 min-w-0 border-0 p-0 py-0.5", className),
|
|
28
|
+
children
|
|
29
|
+
})
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function ComboboxLabel({ children, className }) {
|
|
33
|
+
const groupId = useContext(ComboboxGroupContext);
|
|
34
|
+
const labelClassName = cn("w-full px-2 py-1.5 font-medium text-[0.68rem] text-muted-foreground uppercase tracking-[0.12em]", className);
|
|
35
|
+
return groupId ? /* @__PURE__ */ jsx("legend", {
|
|
36
|
+
className: labelClassName,
|
|
37
|
+
children
|
|
38
|
+
}) : /* @__PURE__ */ jsx("div", {
|
|
39
|
+
className: labelClassName,
|
|
40
|
+
children
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function ComboboxItem({ value, children, textValue, keywords = [], disabled = false, onSelect, className }) {
|
|
44
|
+
const context = useComboboxContext("ComboboxItem");
|
|
45
|
+
const groupId = useContext(ComboboxGroupContext);
|
|
46
|
+
const id = useId();
|
|
47
|
+
const itemRef = useRef(null);
|
|
48
|
+
const label = textValue ?? (typeof children === "string" ? children : value);
|
|
49
|
+
const visible = context.isVisible(value);
|
|
50
|
+
const active = context.activeValue === value;
|
|
51
|
+
const selected = context.value === value;
|
|
52
|
+
const keywordKey = keywords.join("\0");
|
|
53
|
+
const normalizedKeywords = useMemo(() => keywordKey ? keywordKey.split("\0") : [], [keywordKey]);
|
|
54
|
+
const { registerItem, unregisterItem } = context;
|
|
55
|
+
useLayoutEffect(() => {
|
|
56
|
+
registerItem({
|
|
57
|
+
value,
|
|
58
|
+
label,
|
|
59
|
+
keywords: normalizedKeywords,
|
|
60
|
+
disabled,
|
|
61
|
+
groupId,
|
|
62
|
+
id,
|
|
63
|
+
ref: itemRef
|
|
64
|
+
});
|
|
65
|
+
return () => unregisterItem(value);
|
|
66
|
+
}, [
|
|
67
|
+
disabled,
|
|
68
|
+
groupId,
|
|
69
|
+
id,
|
|
70
|
+
label,
|
|
71
|
+
normalizedKeywords,
|
|
72
|
+
registerItem,
|
|
73
|
+
unregisterItem,
|
|
74
|
+
value
|
|
75
|
+
]);
|
|
76
|
+
if (!visible) return null;
|
|
77
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
78
|
+
ref: itemRef,
|
|
79
|
+
id,
|
|
80
|
+
type: "button",
|
|
81
|
+
role: "option",
|
|
82
|
+
"aria-selected": selected,
|
|
83
|
+
disabled,
|
|
84
|
+
tabIndex: -1,
|
|
85
|
+
"data-combobox-item": "",
|
|
86
|
+
"data-active": active || void 0,
|
|
87
|
+
"data-selected": selected || void 0,
|
|
88
|
+
onPointerMove: () => {
|
|
89
|
+
if (!disabled) context.setActiveValue(value);
|
|
90
|
+
},
|
|
91
|
+
onPointerDown: (event) => event.preventDefault(),
|
|
92
|
+
onClick: () => {
|
|
93
|
+
if (disabled) return;
|
|
94
|
+
onSelect?.(value);
|
|
95
|
+
context.select(value);
|
|
96
|
+
},
|
|
97
|
+
className: cn("relative isolate flex w-full items-center gap-2 rounded-lg px-2 py-2 text-left text-sm outline-none transition-colors duration-150", active ? "text-foreground" : "text-muted-foreground", "disabled:pointer-events-none disabled:opacity-45", className),
|
|
98
|
+
children: [
|
|
99
|
+
active ? /* @__PURE__ */ jsx(motion.span, {
|
|
100
|
+
"aria-hidden": true,
|
|
101
|
+
layoutId: context.activeLayoutId,
|
|
102
|
+
className: "absolute inset-0 -z-10 rounded-lg bg-muted",
|
|
103
|
+
transition: context.reduce ? { duration: 0 } : SPRING_LAYOUT
|
|
104
|
+
}) : null,
|
|
105
|
+
/* @__PURE__ */ jsx("span", {
|
|
106
|
+
className: "min-w-0 flex-1",
|
|
107
|
+
children
|
|
108
|
+
}),
|
|
109
|
+
/* @__PURE__ */ jsx(motion.span, {
|
|
110
|
+
"aria-hidden": true,
|
|
111
|
+
initial: false,
|
|
112
|
+
animate: {
|
|
113
|
+
opacity: selected ? 1 : 0,
|
|
114
|
+
transform: selected ? "scale(1)" : "scale(0.82)"
|
|
115
|
+
},
|
|
116
|
+
transition: context.reduce ? { duration: 0 } : {
|
|
117
|
+
duration: .14,
|
|
118
|
+
ease: EASE_OUT
|
|
119
|
+
},
|
|
120
|
+
className: "grid size-5 shrink-0 place-items-center text-foreground",
|
|
121
|
+
children: /* @__PURE__ */ jsx(Check, { className: "size-4" })
|
|
122
|
+
})
|
|
123
|
+
]
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function ComboboxEmpty({ children = "No options found.", className }) {
|
|
127
|
+
if (useComboboxContext("ComboboxEmpty").visibleCount > 0) return null;
|
|
128
|
+
return /* @__PURE__ */ jsx("div", {
|
|
129
|
+
role: "status",
|
|
130
|
+
className: cn("px-3 py-8 text-center text-muted-foreground text-sm", className),
|
|
131
|
+
children
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
function ComboboxSeparator({ className }) {
|
|
135
|
+
return /* @__PURE__ */ jsx("div", {
|
|
136
|
+
"aria-hidden": true,
|
|
137
|
+
className: cn("-mx-1 my-1 h-px bg-border", className)
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
//#endregion
|
|
141
|
+
export { ComboboxEmpty, ComboboxGroup, ComboboxItem, ComboboxLabel, ComboboxList, ComboboxSeparator };
|
|
142
|
+
|
|
143
|
+
//# sourceMappingURL=list.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"list.js","names":[],"sources":["../../../../src/components/motion/combobox/list.tsx"],"sourcesContent":["\"use client\";\n\nimport { type ReactNode, useContext, useId, useLayoutEffect, useMemo, useRef } from \"react\";\nimport { Check } from \"lucide-react\";\nimport { motion } from \"motion/react\";\n\nimport { cn } from \"@kumix/utils\";\nimport { EASE_OUT, SPRING_LAYOUT } from \"../../../lib/ease\";\nimport { ComboboxGroupContext, useComboboxContext } from \"./context\";\n\nexport interface ComboboxListProps {\n children: ReactNode;\n ariaLabel?: string;\n className?: string;\n}\n\nexport function ComboboxList({ children, ariaLabel = \"Options\", className }: ComboboxListProps) {\n const context = useComboboxContext(\"ComboboxList\");\n return (\n <div\n id={context.listId}\n role=\"listbox\"\n aria-label={ariaLabel}\n className={cn(\n \"max-h-64 overflow-y-auto overscroll-contain p-1.5 [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden\",\n className,\n )}\n >\n {children}\n </div>\n );\n}\n\nexport interface ComboboxGroupProps {\n children: ReactNode;\n className?: string;\n}\n\nexport function ComboboxGroup({ children, className }: ComboboxGroupProps) {\n const context = useComboboxContext(\"ComboboxGroup\");\n const groupId = useId();\n return (\n <ComboboxGroupContext.Provider value={groupId}>\n <fieldset\n hidden={!context.hasVisibleItems(groupId)}\n className={cn(\"m-0 min-w-0 border-0 p-0 py-0.5\", className)}\n >\n {children}\n </fieldset>\n </ComboboxGroupContext.Provider>\n );\n}\n\nexport interface ComboboxLabelProps {\n children: ReactNode;\n className?: string;\n}\n\nexport function ComboboxLabel({ children, className }: ComboboxLabelProps) {\n const groupId = useContext(ComboboxGroupContext);\n const labelClassName = cn(\n \"w-full px-2 py-1.5 font-medium text-[0.68rem] text-muted-foreground uppercase tracking-[0.12em]\",\n className,\n );\n return groupId ? (\n <legend className={labelClassName}>{children}</legend>\n ) : (\n <div className={labelClassName}>{children}</div>\n );\n}\n\nexport interface ComboboxItemProps {\n value: string;\n children: ReactNode;\n textValue?: string;\n keywords?: string[];\n disabled?: boolean;\n onSelect?: (value: string) => void;\n className?: string;\n}\n\nexport function ComboboxItem({\n value,\n children,\n textValue,\n keywords = [],\n disabled = false,\n onSelect,\n className,\n}: ComboboxItemProps) {\n const context = useComboboxContext(\"ComboboxItem\");\n const groupId = useContext(ComboboxGroupContext);\n const id = useId();\n const itemRef = useRef<HTMLButtonElement>(null);\n const label = textValue ?? (typeof children === \"string\" ? children : value);\n const visible = context.isVisible(value);\n const active = context.activeValue === value;\n const selected = context.value === value;\n const keywordKey = keywords.join(\"\\u0000\");\n const normalizedKeywords = useMemo(\n () => (keywordKey ? keywordKey.split(\"\\u0000\") : []),\n [keywordKey],\n );\n const { registerItem, unregisterItem } = context;\n\n useLayoutEffect(() => {\n registerItem({\n value,\n label,\n keywords: normalizedKeywords,\n disabled,\n groupId,\n id,\n ref: itemRef,\n });\n return () => unregisterItem(value);\n }, [disabled, groupId, id, label, normalizedKeywords, registerItem, unregisterItem, value]);\n\n if (!visible) return null;\n\n return (\n <button\n ref={itemRef}\n id={id}\n type=\"button\"\n role=\"option\"\n aria-selected={selected}\n disabled={disabled}\n tabIndex={-1}\n data-combobox-item=\"\"\n data-active={active || undefined}\n data-selected={selected || undefined}\n onPointerMove={() => {\n if (!disabled) context.setActiveValue(value);\n }}\n onPointerDown={(event) => event.preventDefault()}\n onClick={() => {\n if (disabled) return;\n onSelect?.(value);\n context.select(value);\n }}\n className={cn(\n \"relative isolate flex w-full items-center gap-2 rounded-lg px-2 py-2 text-left text-sm outline-none transition-colors duration-150\",\n active ? \"text-foreground\" : \"text-muted-foreground\",\n \"disabled:pointer-events-none disabled:opacity-45\",\n className,\n )}\n >\n {active ? (\n <motion.span\n aria-hidden\n layoutId={context.activeLayoutId}\n className=\"absolute inset-0 -z-10 rounded-lg bg-muted\"\n transition={context.reduce ? { duration: 0 } : SPRING_LAYOUT}\n />\n ) : null}\n <span className=\"min-w-0 flex-1\">{children}</span>\n <motion.span\n aria-hidden\n initial={false}\n animate={{\n opacity: selected ? 1 : 0,\n transform: selected ? \"scale(1)\" : \"scale(0.82)\",\n }}\n transition={context.reduce ? { duration: 0 } : { duration: 0.14, ease: EASE_OUT }}\n className=\"grid size-5 shrink-0 place-items-center text-foreground\"\n >\n <Check className=\"size-4\" />\n </motion.span>\n </button>\n );\n}\n\nexport interface ComboboxEmptyProps {\n children?: ReactNode;\n className?: string;\n}\n\nexport function ComboboxEmpty({ children = \"No options found.\", className }: ComboboxEmptyProps) {\n const context = useComboboxContext(\"ComboboxEmpty\");\n if (context.visibleCount > 0) return null;\n return (\n <div\n role=\"status\"\n className={cn(\"px-3 py-8 text-center text-muted-foreground text-sm\", className)}\n >\n {children}\n </div>\n );\n}\n\nexport interface ComboboxSeparatorProps {\n className?: string;\n}\n\nexport function ComboboxSeparator({ className }: ComboboxSeparatorProps) {\n return <div aria-hidden className={cn(\"-mx-1 my-1 h-px bg-border\", className)} />;\n}\n"],"mappings":";;;;;;;;;AAgBA,SAAgB,aAAa,EAAE,UAAU,YAAY,WAAW,aAAgC;CAC9F,MAAM,UAAU,mBAAmB,cAAc;CACjD,OACE,oBAAC,OAAD;EACE,IAAI,QAAQ;EACZ,MAAK;EACL,cAAY;EACZ,WAAW,GACT,oIACA,SACF;EAEC;CACE,CAAA;AAET;AAOA,SAAgB,cAAc,EAAE,UAAU,aAAiC;CACzE,MAAM,UAAU,mBAAmB,eAAe;CAClD,MAAM,UAAU,MAAM;CACtB,OACE,oBAAC,qBAAqB,UAAtB;EAA+B,OAAO;EACpC,UAAA,oBAAC,YAAD;GACE,QAAQ,CAAC,QAAQ,gBAAgB,OAAO;GACxC,WAAW,GAAG,mCAAmC,SAAS;GAEzD;EACO,CAAA;CACmB,CAAA;AAEnC;AAOA,SAAgB,cAAc,EAAE,UAAU,aAAiC;CACzE,MAAM,UAAU,WAAW,oBAAoB;CAC/C,MAAM,iBAAiB,GACrB,mGACA,SACF;CACA,OAAO,UACL,oBAAC,UAAD;EAAQ,WAAW;EAAiB;CAAiB,CAAA,IAErD,oBAAC,OAAD;EAAK,WAAW;EAAiB;CAAc,CAAA;AAEnD;AAYA,SAAgB,aAAa,EAC3B,OACA,UACA,WACA,WAAW,CAAC,GACZ,WAAW,OACX,UACA,aACoB;CACpB,MAAM,UAAU,mBAAmB,cAAc;CACjD,MAAM,UAAU,WAAW,oBAAoB;CAC/C,MAAM,KAAK,MAAM;CACjB,MAAM,UAAU,OAA0B,IAAI;CAC9C,MAAM,QAAQ,cAAc,OAAO,aAAa,WAAW,WAAW;CACtE,MAAM,UAAU,QAAQ,UAAU,KAAK;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BAAC,OAAD,EAAO,WAAU,SAAU,CAAA;GAChB,CAAA;EACP;;AAEZ;AAOA,SAAgB,cAAc,EAAE,WAAW,qBAAqB,aAAiC;CAE/F,IADgB,mBAAmB,eACzB,CAAC,CAAC,eAAe,GAAG,OAAO;CACrC,OACE,oBAAC,OAAD;EACE,MAAK;EACL,WAAW,GAAG,uDAAuD,SAAS;EAE7E;CACE,CAAA;AAET;AAMA,SAAgB,kBAAkB,EAAE,aAAqC;CACvE,OAAO,oBAAC,OAAD;EAAK,eAAA;EAAY,WAAW,GAAG,6BAA6B,SAAS;CAAI,CAAA;AAClF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { InputHTMLAttributes, ReactNode, Ref } from "react";
|
|
2
|
+
//#region src/components/motion/combobox/trigger.d.ts
|
|
3
|
+
interface ComboboxTriggerProps {
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
className?: string;
|
|
6
|
+
}
|
|
7
|
+
declare function ComboboxTrigger({ children, className }: ComboboxTriggerProps): import("react").JSX.Element;
|
|
8
|
+
interface ComboboxValueProps {
|
|
9
|
+
placeholder?: ReactNode;
|
|
10
|
+
children?: ReactNode | ((value: string | undefined, label: string | undefined) => ReactNode);
|
|
11
|
+
className?: string;
|
|
12
|
+
}
|
|
13
|
+
declare function ComboboxValue({ placeholder, children, className }: ComboboxValueProps): import("react").JSX.Element;
|
|
14
|
+
interface ComboboxInputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "defaultValue" | "value"> {
|
|
15
|
+
ref?: Ref<HTMLInputElement>;
|
|
16
|
+
wrapperClassName?: string;
|
|
17
|
+
}
|
|
18
|
+
declare function ComboboxInput({ ref, className, wrapperClassName, "aria-label": ariaLabel, onChange, onClick, onFocus, onKeyDown, onPointerDown, placeholder, ...props }: ComboboxInputProps): import("react").JSX.Element;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { ComboboxInput, ComboboxInputProps, ComboboxTrigger, ComboboxTriggerProps, ComboboxValue, ComboboxValueProps };
|
|
21
|
+
//# sourceMappingURL=trigger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trigger.d.ts","names":[],"sources":["../../../../src/components/motion/combobox/trigger.tsx"],"mappings":";;UAaiB;EACf,UAAU;EACV;;iBAGc,kBAAkB,UAAU,aAAa,uCAAoB,IAAA;UA6B5D;EACf,cAAc;EACd,WAAW,cAAc,2BAA2B,8BAA8B;EAClF;;iBAGc,gBACd,aACA,UACA,aACC,qCAAkB,IAAA;UAqBJ,2BACP,KAAK,oBAAoB;EACjC,MAAM,IAAI;EACV;;iBAGc,gBACd,KACA,WACA,gCACc,WACd,UACA,SACA,SACA,WACA,eACA,gBACG,SACF,qCAAkB,IAAA"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { mergeRefs, useComboboxContext } from "./context.js";
|
|
3
|
+
import { cn } from "@kumix/utils";
|
|
4
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
import { ChevronsUpDown, Search } from "lucide-react";
|
|
6
|
+
//#region src/components/motion/combobox/trigger.tsx
|
|
7
|
+
function ComboboxTrigger({ children, className }) {
|
|
8
|
+
const context = useComboboxContext("ComboboxTrigger");
|
|
9
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
10
|
+
ref: context.triggerRef,
|
|
11
|
+
id: context.triggerId,
|
|
12
|
+
"data-state": context.open ? "open" : "closed",
|
|
13
|
+
onPointerDown: (event) => {
|
|
14
|
+
if (context.disabled || event.target === context.inputRef.current) return;
|
|
15
|
+
event.preventDefault();
|
|
16
|
+
context.inputRef.current?.focus({ preventScroll: true });
|
|
17
|
+
context.setOpen(true);
|
|
18
|
+
},
|
|
19
|
+
className: cn("relative z-20 flex h-10 w-full min-w-52 cursor-text items-center justify-between gap-3 rounded-xl border border-border bg-transparent px-3 text-foreground text-sm transition-[border-color] hover:border-(--color-border-strong)", "focus-within:ring-2 focus-within:ring-foreground/20", context.disabled && "pointer-events-none opacity-50", className),
|
|
20
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
21
|
+
className: "min-w-0 flex-1 text-left",
|
|
22
|
+
children
|
|
23
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
24
|
+
"aria-hidden": true,
|
|
25
|
+
className: "shrink-0 text-muted-foreground",
|
|
26
|
+
children: /* @__PURE__ */ jsx(ChevronsUpDown, { className: "size-4" })
|
|
27
|
+
})]
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function ComboboxValue({ placeholder = "Select an option", children, className }) {
|
|
31
|
+
const context = useComboboxContext("ComboboxValue");
|
|
32
|
+
const label = context.labelFor(context.value);
|
|
33
|
+
const content = typeof children === "function" ? children(context.value, label) : children ?? label ?? placeholder;
|
|
34
|
+
return /* @__PURE__ */ jsx("span", {
|
|
35
|
+
className: cn("block truncate", context.value === void 0 ? "text-muted-foreground" : "text-foreground", className),
|
|
36
|
+
children: content
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function ComboboxInput({ ref, className, wrapperClassName, "aria-label": ariaLabel = "Search options", onChange, onClick, onFocus, onKeyDown, onPointerDown, placeholder = "Search…", ...props }) {
|
|
40
|
+
const context = useComboboxContext("ComboboxInput");
|
|
41
|
+
const selectedLabel = context.labelFor(context.value);
|
|
42
|
+
const handleKeyDown = (event) => {
|
|
43
|
+
onKeyDown?.(event);
|
|
44
|
+
if (event.defaultPrevented) return;
|
|
45
|
+
if (event.key === "ArrowDown") {
|
|
46
|
+
event.preventDefault();
|
|
47
|
+
context.setOpen(true);
|
|
48
|
+
context.moveActive(1);
|
|
49
|
+
} else if (event.key === "ArrowUp") {
|
|
50
|
+
event.preventDefault();
|
|
51
|
+
context.setOpen(true);
|
|
52
|
+
context.moveActive(-1);
|
|
53
|
+
} else if (event.key === "Home" && context.open) {
|
|
54
|
+
event.preventDefault();
|
|
55
|
+
context.moveActive("first");
|
|
56
|
+
} else if (event.key === "End" && context.open) {
|
|
57
|
+
event.preventDefault();
|
|
58
|
+
context.moveActive("last");
|
|
59
|
+
} else if (event.key === "Enter") {
|
|
60
|
+
event.preventDefault();
|
|
61
|
+
if (context.open) context.selectActive();
|
|
62
|
+
else context.setOpen(true);
|
|
63
|
+
} else if (event.key === "Escape" && context.open) {
|
|
64
|
+
event.preventDefault();
|
|
65
|
+
context.setOpen(false, true);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
69
|
+
className: cn("flex min-w-0 flex-1 items-center gap-2", wrapperClassName),
|
|
70
|
+
children: [/* @__PURE__ */ jsx(Search, {
|
|
71
|
+
"aria-hidden": true,
|
|
72
|
+
className: "size-4 shrink-0 text-muted-foreground"
|
|
73
|
+
}), /* @__PURE__ */ jsx("input", {
|
|
74
|
+
...props,
|
|
75
|
+
ref: mergeRefs(ref, context.inputRef),
|
|
76
|
+
id: context.inputId,
|
|
77
|
+
role: "combobox",
|
|
78
|
+
"aria-label": ariaLabel,
|
|
79
|
+
"aria-autocomplete": "list",
|
|
80
|
+
"aria-expanded": context.open,
|
|
81
|
+
"aria-controls": context.listId,
|
|
82
|
+
"aria-activedescendant": context.open ? context.activeItemId : void 0,
|
|
83
|
+
autoComplete: "off",
|
|
84
|
+
disabled: context.disabled,
|
|
85
|
+
value: context.open ? context.query : selectedLabel ?? "",
|
|
86
|
+
placeholder,
|
|
87
|
+
onPointerDown: (event) => {
|
|
88
|
+
onPointerDown?.(event);
|
|
89
|
+
if (event.defaultPrevented || context.open) return;
|
|
90
|
+
event.preventDefault();
|
|
91
|
+
context.inputRef.current?.focus({ preventScroll: true });
|
|
92
|
+
context.setOpen(true);
|
|
93
|
+
},
|
|
94
|
+
onFocus: (event) => {
|
|
95
|
+
context.setOpen(true);
|
|
96
|
+
onFocus?.(event);
|
|
97
|
+
},
|
|
98
|
+
onClick: (event) => {
|
|
99
|
+
context.setOpen(true);
|
|
100
|
+
onClick?.(event);
|
|
101
|
+
},
|
|
102
|
+
onChange: (event) => {
|
|
103
|
+
context.setOpen(true);
|
|
104
|
+
context.setQuery(event.target.value);
|
|
105
|
+
onChange?.(event);
|
|
106
|
+
},
|
|
107
|
+
onKeyDown: handleKeyDown,
|
|
108
|
+
className: cn("h-10 min-w-0 flex-1 bg-transparent text-foreground text-sm outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed", className)
|
|
109
|
+
})]
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
//#endregion
|
|
113
|
+
export { ComboboxInput, ComboboxTrigger, ComboboxValue };
|
|
114
|
+
|
|
115
|
+
//# sourceMappingURL=trigger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trigger.js","names":[],"sources":["../../../../src/components/motion/combobox/trigger.tsx"],"sourcesContent":["\"use client\";\n\nimport type {\n InputHTMLAttributes,\n KeyboardEvent as ReactKeyboardEvent,\n ReactNode,\n Ref,\n} from \"react\";\nimport { ChevronsUpDown, Search } from \"lucide-react\";\n\nimport { cn } from \"@kumix/utils\";\nimport { mergeRefs, useComboboxContext } from \"./context\";\n\nexport interface ComboboxTriggerProps {\n children: ReactNode;\n className?: string;\n}\n\nexport function ComboboxTrigger({ children, className }: ComboboxTriggerProps) {\n const context = useComboboxContext(\"ComboboxTrigger\");\n\n return (\n <div\n ref={context.triggerRef}\n id={context.triggerId}\n data-state={context.open ? \"open\" : \"closed\"}\n onPointerDown={(event) => {\n if (context.disabled || event.target === context.inputRef.current) return;\n event.preventDefault();\n context.inputRef.current?.focus({ preventScroll: true });\n context.setOpen(true);\n }}\n className={cn(\n \"relative z-20 flex h-10 w-full min-w-52 cursor-text items-center justify-between gap-3 rounded-xl border border-border bg-transparent px-3 text-foreground text-sm transition-[border-color] hover:border-(--color-border-strong)\",\n \"focus-within:ring-2 focus-within:ring-foreground/20\",\n context.disabled && \"pointer-events-none opacity-50\",\n className,\n )}\n >\n <span className=\"min-w-0 flex-1 text-left\">{children}</span>\n <span aria-hidden className=\"shrink-0 text-muted-foreground\">\n <ChevronsUpDown className=\"size-4\" />\n </span>\n </div>\n );\n}\n\nexport interface ComboboxValueProps {\n placeholder?: ReactNode;\n children?: ReactNode | ((value: string | undefined, label: string | undefined) => ReactNode);\n className?: string;\n}\n\nexport function ComboboxValue({\n placeholder = \"Select an option\",\n children,\n className,\n}: ComboboxValueProps) {\n const context = useComboboxContext(\"ComboboxValue\");\n const label = context.labelFor(context.value);\n const content =\n typeof children === \"function\"\n ? children(context.value, label)\n : (children ?? label ?? placeholder);\n\n return (\n <span\n className={cn(\n \"block truncate\",\n context.value === undefined ? \"text-muted-foreground\" : \"text-foreground\",\n className,\n )}\n >\n {content}\n </span>\n );\n}\n\nexport interface ComboboxInputProps\n extends Omit<InputHTMLAttributes<HTMLInputElement>, \"defaultValue\" | \"value\"> {\n ref?: Ref<HTMLInputElement>;\n wrapperClassName?: string;\n}\n\nexport function ComboboxInput({\n ref,\n className,\n wrapperClassName,\n \"aria-label\": ariaLabel = \"Search options\",\n onChange,\n onClick,\n onFocus,\n onKeyDown,\n onPointerDown,\n placeholder = \"Search…\",\n ...props\n}: ComboboxInputProps) {\n const context = useComboboxContext(\"ComboboxInput\");\n const selectedLabel = context.labelFor(context.value);\n\n const handleKeyDown = (event: ReactKeyboardEvent<HTMLInputElement>) => {\n onKeyDown?.(event);\n if (event.defaultPrevented) return;\n\n if (event.key === \"ArrowDown\") {\n event.preventDefault();\n context.setOpen(true);\n context.moveActive(1);\n } else if (event.key === \"ArrowUp\") {\n event.preventDefault();\n context.setOpen(true);\n context.moveActive(-1);\n } else if (event.key === \"Home\" && context.open) {\n event.preventDefault();\n context.moveActive(\"first\");\n } else if (event.key === \"End\" && context.open) {\n event.preventDefault();\n context.moveActive(\"last\");\n } else if (event.key === \"Enter\") {\n event.preventDefault();\n if (context.open) context.selectActive();\n else context.setOpen(true);\n } else if (event.key === \"Escape\" && context.open) {\n event.preventDefault();\n context.setOpen(false, true);\n }\n };\n\n return (\n <div className={cn(\"flex min-w-0 flex-1 items-center gap-2\", wrapperClassName)}>\n <Search aria-hidden className=\"size-4 shrink-0 text-muted-foreground\" />\n <input\n {...props}\n ref={mergeRefs(ref, context.inputRef)}\n id={context.inputId}\n role=\"combobox\"\n aria-label={ariaLabel}\n aria-autocomplete=\"list\"\n aria-expanded={context.open}\n aria-controls={context.listId}\n aria-activedescendant={context.open ? context.activeItemId : undefined}\n autoComplete=\"off\"\n disabled={context.disabled}\n value={context.open ? context.query : (selectedLabel ?? \"\")}\n placeholder={placeholder}\n onPointerDown={(event) => {\n onPointerDown?.(event);\n if (event.defaultPrevented || context.open) return;\n event.preventDefault();\n context.inputRef.current?.focus({ preventScroll: true });\n context.setOpen(true);\n }}\n onFocus={(event) => {\n context.setOpen(true);\n onFocus?.(event);\n }}\n onClick={(event) => {\n context.setOpen(true);\n onClick?.(event);\n }}\n onChange={(event) => {\n context.setOpen(true);\n context.setQuery(event.target.value);\n onChange?.(event);\n }}\n onKeyDown={handleKeyDown}\n className={cn(\n \"h-10 min-w-0 flex-1 bg-transparent text-foreground text-sm outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed\",\n className,\n )}\n />\n </div>\n );\n}\n"],"mappings":";;;;;;AAkBA,SAAgB,gBAAgB,EAAE,UAAU,aAAmC;CAC7E,MAAM,UAAU,mBAAmB,iBAAiB;CAEpD,OACE,qBAAC,OAAD;EACE,KAAK,QAAQ;EACb,IAAI,QAAQ;EACZ,cAAY,QAAQ,OAAO,SAAS;EACpC,gBAAgB,UAAU;GACxB,IAAI,QAAQ,YAAY,MAAM,WAAW,QAAQ,SAAS,SAAS;GACnE,MAAM,eAAe;GACrB,QAAQ,SAAS,SAAS,MAAM,EAAE,eAAe,KAAK,CAAC;GACvD,QAAQ,QAAQ,IAAI;EACtB;EACA,WAAW,GACT,qOACA,uDACA,QAAQ,YAAY,kCACpB,SACF;EAfF,UAAA,CAiBE,oBAAC,QAAD;GAAM,WAAU;GAA4B;EAAe,CAAA,GAC3D,oBAAC,QAAD;GAAM,eAAA;GAAY,WAAU;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AAQ,MAAM;GACjD,MAAM,eAAe;GACrB,QAAQ,QAAQ,OAAO,IAAI;EAC7B;CACF;CAEA,OACE,qBAAC,OAAD;EAAK,WAAW,GAAG,0CAA0C,gBAAgB;EAA7E,UAAA,CACE,oBAAC,QAAD;GAAQ,eAAA;GAAY,WAAU;EAAyC,CAAA,GACvE,oBAAC,SAAD;GACE,GAAI;GACJ,KAAK,UAAU,KAAK,QAAQ,QAAQ;GACpC,IAAI,QAAQ;GACZ,MAAK;GACL,cAAY;GACZ,qBAAkB;GAClB,iBAAe,QAAQ;GACvB,iBAAe,QAAQ;GACvB,yBAAuB,QAAQ,OAAO,QAAQ,eAAe,KAAA;GAC7D,cAAa;GACb,UAAU,QAAQ;GAClB,OAAO,QAAQ,OAAO,QAAQ,QAAS,iBAAiB;GAC3C;GACb,gBAAgB,UAAU;IACxB,gBAAgB,KAAK;IACrB,IAAI,MAAM,oBAAoB,QAAQ,MAAM;IAC5C,MAAM,eAAe;IACrB,QAAQ,SAAS,SAAS,MAAM,EAAE,eAAe,KAAK,CAAC;IACvD,QAAQ,QAAQ,IAAI;GACtB;GACA,UAAU,UAAU;IAClB,QAAQ,QAAQ,IAAI;IACpB,UAAU,KAAK;GACjB;GACA,UAAU,UAAU;IAClB,QAAQ,QAAQ,IAAI;IACpB,UAAU,KAAK;GACjB;GACA,WAAW,UAAU;IACnB,QAAQ,QAAQ,IAAI;IACpB,QAAQ,SAAS,MAAM,OAAO,KAAK;IACnC,WAAW,KAAK;GAClB;GACA,WAAW;GACX,WAAW,GACT,yIACA,SACF;EACD,CAAA,CACE;;AAET"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ComboboxContent, ComboboxContentProps } from "./combobox/content.js";
|
|
2
|
+
import { Combobox, ComboboxFilter, ComboboxProps } from "./combobox/context.js";
|
|
3
|
+
import { ComboboxEmpty, ComboboxEmptyProps, ComboboxGroup, ComboboxGroupProps, ComboboxItem, ComboboxItemProps, ComboboxLabel, ComboboxLabelProps, ComboboxList, ComboboxListProps, ComboboxSeparator, ComboboxSeparatorProps } from "./combobox/list.js";
|
|
4
|
+
import { ComboboxInput, ComboboxInputProps, ComboboxTrigger, ComboboxTriggerProps, ComboboxValue, ComboboxValueProps } from "./combobox/trigger.js";
|
|
5
|
+
export { Combobox, ComboboxContent, type ComboboxContentProps, ComboboxEmpty, type ComboboxEmptyProps, type ComboboxFilter, ComboboxGroup, type ComboboxGroupProps, ComboboxInput, type ComboboxInputProps, ComboboxItem, type ComboboxItemProps, ComboboxLabel, type ComboboxLabelProps, ComboboxList, type ComboboxListProps, type ComboboxProps, ComboboxSeparator, type ComboboxSeparatorProps, ComboboxTrigger, type ComboboxTriggerProps, ComboboxValue, type ComboboxValueProps };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Combobox } from "./combobox/context.js";
|
|
3
|
+
import { ComboboxContent } from "./combobox/content.js";
|
|
4
|
+
import { ComboboxEmpty, ComboboxGroup, ComboboxItem, ComboboxLabel, ComboboxList, ComboboxSeparator } from "./combobox/list.js";
|
|
5
|
+
import { ComboboxInput, ComboboxTrigger, ComboboxValue } from "./combobox/trigger.js";
|
|
6
|
+
export { Combobox, ComboboxContent, ComboboxEmpty, ComboboxGroup, ComboboxInput, ComboboxItem, ComboboxLabel, ComboboxList, ComboboxSeparator, ComboboxTrigger, ComboboxValue };
|