@betterinternship/components 3.0.3 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/animated-shiny-text.js +12 -0
- package/dist/components/animated-shiny-text.js.map +1 -0
- package/dist/components/autocomplete.js +335 -0
- package/dist/components/autocomplete.js.map +1 -0
- package/dist/components/carousel.js +126 -0
- package/dist/components/carousel.js.map +1 -0
- package/dist/components/chip-select.js +22 -0
- package/dist/components/chip-select.js.map +1 -0
- package/dist/components/collapsible.js +45 -0
- package/dist/components/collapsible.js.map +1 -0
- package/dist/components/context-menu.js +37 -0
- package/dist/components/context-menu.js.map +1 -0
- package/dist/components/dialog.js +10 -10
- package/dist/components/dialog.js.map +1 -1
- package/dist/components/dot-matrix.js +163 -0
- package/dist/components/dot-matrix.js.map +1 -0
- package/dist/components/dropdown.js +262 -0
- package/dist/components/dropdown.js.map +1 -0
- package/dist/components/error.js +19 -0
- package/dist/components/error.js.map +1 -0
- package/dist/components/fab.js +9 -0
- package/dist/components/fab.js.map +1 -0
- package/dist/components/form.js +71 -0
- package/dist/components/form.js.map +1 -0
- package/dist/components/hover-card.js +13 -0
- package/dist/components/hover-card.js.map +1 -0
- package/dist/components/input-otp.js +24 -0
- package/dist/components/input-otp.js.map +1 -0
- package/dist/components/magnetic-button.js +32 -0
- package/dist/components/magnetic-button.js.map +1 -0
- package/dist/components/messages.js +9 -0
- package/dist/components/messages.js.map +1 -0
- package/dist/components/mobile-sheet-portal.js +17 -0
- package/dist/components/mobile-sheet-portal.js.map +1 -0
- package/dist/components/paginator.js +44 -0
- package/dist/components/paginator.js.map +1 -0
- package/dist/components/progress.js +11 -0
- package/dist/components/progress.js.map +1 -0
- package/dist/components/sheet.d.ts +1 -1
- package/dist/components/sheet.js +40 -0
- package/dist/components/sheet.js.map +1 -0
- package/dist/components/slider.js +11 -0
- package/dist/components/slider.js.map +1 -0
- package/dist/components/sonner-toast.js +78 -0
- package/dist/components/sonner-toast.js.map +1 -0
- package/dist/components/timeline.js +16 -0
- package/dist/components/timeline.js.map +1 -0
- package/dist/components/toggle.js +15 -0
- package/dist/components/toggle.js.map +1 -0
- package/dist/hooks/use-detect-click-outside.js +29 -0
- package/dist/hooks/use-detect-click-outside.js.map +1 -0
- package/dist/hooks/use-resource-table.js +284 -0
- package/dist/hooks/use-resource-table.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -1
- package/dist/node_modules/embla-carousel/esm/embla-carousel.esm.js +1670 -0
- package/dist/node_modules/embla-carousel/esm/embla-carousel.esm.js.map +1 -0
- package/dist/node_modules/embla-carousel-react/esm/embla-carousel-react.esm.js +38 -0
- package/dist/node_modules/embla-carousel-react/esm/embla-carousel-react.esm.js.map +1 -0
- package/dist/node_modules/embla-carousel-reactive-utils/esm/embla-carousel-reactive-utils.esm.js +39 -0
- package/dist/node_modules/embla-carousel-reactive-utils/esm/embla-carousel-reactive-utils.esm.js.map +1 -0
- package/dist/node_modules/input-otp/dist/index.js +25 -0
- package/dist/node_modules/input-otp/dist/index.js.map +1 -0
- package/dist/node_modules/react-hook-form/dist/index.esm.js +706 -0
- package/dist/node_modules/react-hook-form/dist/index.esm.js.map +1 -0
- package/package.json +37 -2
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { useReducedMotion, motion } from 'framer-motion';
|
|
4
|
+
import { cn } from '../utils.js';
|
|
5
|
+
|
|
6
|
+
function AnimatedShinyText({ children, className, }) {
|
|
7
|
+
const reduceMotion = useReducedMotion();
|
|
8
|
+
return (jsx(motion.span, { className: cn("inline-block bg-[linear-gradient(110deg,rgba(15,23,42,0.9)_0%,rgba(15,23,42,0.9)_42%,rgba(255,255,255,0.75)_50%,rgba(15,23,42,0.9)_58%,rgba(15,23,42,0.9)_100%)] bg-[length:200%_100%] bg-clip-text align-baseline text-transparent motion-reduce:bg-none motion-reduce:text-slate-900", className), initial: false, animate: reduceMotion ? { backgroundPosition: '0% 0%' } : { backgroundPosition: ['200% 0%', '-200% 0%'] }, transition: reduceMotion ? { duration: 0 } : { duration: 4, ease: 'linear', repeat: Infinity }, children: children }));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { AnimatedShinyText };
|
|
12
|
+
//# sourceMappingURL=animated-shiny-text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"animated-shiny-text.js","sources":["../../src/components/animated-shiny-text.tsx"],"sourcesContent":["'use client';\n\nimport type { ReactNode } from \"react\";\nimport { motion, useReducedMotion } from \"framer-motion\";\nimport { cn } from \"@/utils\";\n\nexport function AnimatedShinyText({\n children,\n className,\n}: {\n children: ReactNode;\n className?: string;\n}) {\n const reduceMotion = useReducedMotion();\n\n return (\n <motion.span\n className={cn(\n \"inline-block bg-[linear-gradient(110deg,rgba(15,23,42,0.9)_0%,rgba(15,23,42,0.9)_42%,rgba(255,255,255,0.75)_50%,rgba(15,23,42,0.9)_58%,rgba(15,23,42,0.9)_100%)] bg-[length:200%_100%] bg-clip-text align-baseline text-transparent motion-reduce:bg-none motion-reduce:text-slate-900\",\n className,\n )}\n initial={false}\n animate={reduceMotion ? { backgroundPosition: '0% 0%' } : { backgroundPosition: ['200% 0%', '-200% 0%'] }}\n transition={reduceMotion ? { duration: 0 } : { duration: 4, ease: 'linear', repeat: Infinity }}\n >\n {children}\n </motion.span>\n );\n}\n"],"names":[],"mappings":";;;;;;AAaE;AAEA;AAaF;;"}
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
|
+
import { useState, useRef, useId, useMemo, useEffect, Fragment } from 'react';
|
|
4
|
+
import { Input } from './input.js';
|
|
5
|
+
import { useDetectClickOutside } from '../hooks/use-detect-click-outside.js';
|
|
6
|
+
import { cn } from '../utils.js';
|
|
7
|
+
import { X, ChevronDown, PlusCircleIcon, Search } from 'lucide-react';
|
|
8
|
+
import { Label } from './label.js';
|
|
9
|
+
import { Button } from './button.js';
|
|
10
|
+
import { CheckboxVisual } from './checkbox.js';
|
|
11
|
+
import { Sheet, SheetContent, SheetTitle } from './sheet.js';
|
|
12
|
+
import { useMobile } from '../hooks/use-mobile.js';
|
|
13
|
+
import { useMobileSheetViewport } from '../hooks/use-mobile-sheet-viewport.js';
|
|
14
|
+
import { useFieldFocus, fieldFocusOutlineClassName, fieldInteractionClassName, FieldChromeContext } from '../hooks/use-field-focus.js';
|
|
15
|
+
|
|
16
|
+
/* -------------------------------------------------------
|
|
17
|
+
* Base component (array-based). Single uses length 0..1.
|
|
18
|
+
* -----------------------------------------------------*/
|
|
19
|
+
function AutocompleteBase({ required, options, value = [], setter, placeholder, className, multiple = true, label, labelAddon, allowCustomValue = false, preserveOptionOrder = false, emptyText = "No matching options", mobileDropdownMode = "sheet", inputIcons = false, ...props }) {
|
|
20
|
+
const [query, setQuery] = useState("");
|
|
21
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
22
|
+
const [dropdownMaxHeight, setDropdownMaxHeight] = useState();
|
|
23
|
+
const isMobile = useMobile();
|
|
24
|
+
const clickOutsideRef = useDetectClickOutside({
|
|
25
|
+
onTriggered: () => {
|
|
26
|
+
// Radix owns outside dismissal for the portalled modal sheet.
|
|
27
|
+
if (isMobile && mobileDropdownMode === 'sheet')
|
|
28
|
+
return;
|
|
29
|
+
setIsOpen(false);
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
const rootRef = useRef(null);
|
|
33
|
+
const inputRef = useRef(null);
|
|
34
|
+
const sheetInputRef = useRef(null);
|
|
35
|
+
const sheetRef = useRef(null);
|
|
36
|
+
const sheetCloseRef = useRef(null);
|
|
37
|
+
const lastSelectionRef = useRef(0);
|
|
38
|
+
// Show the Button focus outline only for keyboard focus, not clicks.
|
|
39
|
+
const { keyboardFocus, focusProps: fieldFocusProps } = useFieldFocus();
|
|
40
|
+
const inputId = useId();
|
|
41
|
+
const flatOptions = useMemo(() => options.flatMap((option) => option.children?.length ? option.children : [option]), [options]);
|
|
42
|
+
const findExactOption = (value) => {
|
|
43
|
+
const normalizedValue = value.trim().toLowerCase();
|
|
44
|
+
return flatOptions.find((o) => o.name?.trim().toLowerCase() === normalizedValue ||
|
|
45
|
+
o.keywords?.some((k) => k === normalizedValue));
|
|
46
|
+
};
|
|
47
|
+
const resolveQuerySelection = (nextQuery, matchKeywords = true) => {
|
|
48
|
+
const text = nextQuery.trim();
|
|
49
|
+
if (!text)
|
|
50
|
+
return false;
|
|
51
|
+
const exact = matchKeywords
|
|
52
|
+
? findExactOption(text)
|
|
53
|
+
: flatOptions.find((o) => o.name?.trim().toLowerCase() === text.toLowerCase());
|
|
54
|
+
if (exact) {
|
|
55
|
+
setter([exact.id]);
|
|
56
|
+
setQuery("");
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
if (allowCustomValue) {
|
|
60
|
+
setter([text]);
|
|
61
|
+
setQuery("");
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
};
|
|
66
|
+
const selectedSet = useMemo(() => new Set(value ?? []), [value]);
|
|
67
|
+
const filtered = useMemo(() => {
|
|
68
|
+
const q = query.trim().toLowerCase();
|
|
69
|
+
const base = q
|
|
70
|
+
? flatOptions.filter((o) => o.name?.toLowerCase().includes(q) ||
|
|
71
|
+
options.some((group) => group.children?.some((child) => child.id === o.id) && group.name.toLowerCase().includes(q)) ||
|
|
72
|
+
o.keywords?.some((k) => k.includes(q)))
|
|
73
|
+
: flatOptions;
|
|
74
|
+
return preserveOptionOrder || options.some((option) => option.children?.length)
|
|
75
|
+
? base
|
|
76
|
+
: base.slice().sort((a, b) => a.name.localeCompare(b.name));
|
|
77
|
+
}, [query, flatOptions, options, preserveOptionOrder]);
|
|
78
|
+
const toggle = (id) => {
|
|
79
|
+
lastSelectionRef.current = Date.now();
|
|
80
|
+
if (!multiple) {
|
|
81
|
+
setter([id]); // single-select
|
|
82
|
+
setIsOpen(false);
|
|
83
|
+
setQuery("");
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const set = new Set(value ?? []);
|
|
87
|
+
if (set.has(id))
|
|
88
|
+
set.delete(id);
|
|
89
|
+
else
|
|
90
|
+
set.add(id);
|
|
91
|
+
setter(Array.from(set));
|
|
92
|
+
};
|
|
93
|
+
const removeAt = (id) => {
|
|
94
|
+
const set = new Set(value ?? []);
|
|
95
|
+
set.delete(id);
|
|
96
|
+
setter(Array.from(set));
|
|
97
|
+
};
|
|
98
|
+
const selectedLabels = useMemo(() => (value ?? [])
|
|
99
|
+
.map((id) => {
|
|
100
|
+
const label = flatOptions.find((o) => o.id === id)?.name;
|
|
101
|
+
return label ?? (allowCustomValue ? String(id) : undefined);
|
|
102
|
+
})
|
|
103
|
+
.filter(Boolean), [value, flatOptions, allowCustomValue]);
|
|
104
|
+
const singleDisplay = !multiple && (selectedLabels[0] ?? "");
|
|
105
|
+
const useMobileSheet = isMobile && mobileDropdownMode === "sheet";
|
|
106
|
+
const sheetViewport = useMobileSheetViewport(isOpen && useMobileSheet);
|
|
107
|
+
const useInlineMobileDropdown = isMobile && mobileDropdownMode === "inline";
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
if (!isOpen || !useInlineMobileDropdown) {
|
|
110
|
+
setDropdownMaxHeight(undefined);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const updateDropdownMaxHeight = () => {
|
|
114
|
+
const rootRect = rootRef.current?.getBoundingClientRect();
|
|
115
|
+
if (!rootRect)
|
|
116
|
+
return;
|
|
117
|
+
const viewportHeight = window.visualViewport?.height ?? window.innerHeight;
|
|
118
|
+
const gap = 8;
|
|
119
|
+
const availableHeight = viewportHeight - rootRect.bottom - gap;
|
|
120
|
+
setDropdownMaxHeight(Math.max(0, Math.min(400, availableHeight)));
|
|
121
|
+
};
|
|
122
|
+
updateDropdownMaxHeight();
|
|
123
|
+
window.addEventListener("resize", updateDropdownMaxHeight);
|
|
124
|
+
window.visualViewport?.addEventListener("resize", updateDropdownMaxHeight);
|
|
125
|
+
window.visualViewport?.addEventListener("scroll", updateDropdownMaxHeight);
|
|
126
|
+
return () => {
|
|
127
|
+
window.removeEventListener("resize", updateDropdownMaxHeight);
|
|
128
|
+
window.visualViewport?.removeEventListener("resize", updateDropdownMaxHeight);
|
|
129
|
+
window.visualViewport?.removeEventListener("scroll", updateDropdownMaxHeight);
|
|
130
|
+
};
|
|
131
|
+
}, [isOpen, useInlineMobileDropdown]);
|
|
132
|
+
const suppressMobileKeyboard = isMobile && (useMobileSheet || (mobileDropdownMode === "inline" && !allowCustomValue));
|
|
133
|
+
// --- keyboard helpers for multi
|
|
134
|
+
const onMultiKeyDown = (e) => {
|
|
135
|
+
if (!multiple)
|
|
136
|
+
return;
|
|
137
|
+
if (allowCustomValue && e.key === "Enter" && query.trim().length > 0 && !findExactOption(query)) {
|
|
138
|
+
const text = query.trim();
|
|
139
|
+
const exact = findExactOption(text);
|
|
140
|
+
const nextId = exact ? exact.id : text;
|
|
141
|
+
const set = new Set(value ?? []);
|
|
142
|
+
set.add(nextId);
|
|
143
|
+
setter(Array.from(set));
|
|
144
|
+
setQuery("");
|
|
145
|
+
setIsOpen(true);
|
|
146
|
+
e.preventDefault();
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (e.key === "Backspace" &&
|
|
150
|
+
query.length === 0 &&
|
|
151
|
+
(value?.length ?? 0) > 0) {
|
|
152
|
+
const last = value[value.length - 1];
|
|
153
|
+
removeAt(last);
|
|
154
|
+
e.preventDefault();
|
|
155
|
+
}
|
|
156
|
+
if (e.key === "Enter") {
|
|
157
|
+
const first = filtered[0];
|
|
158
|
+
if (first) {
|
|
159
|
+
toggle(first.id);
|
|
160
|
+
setQuery("");
|
|
161
|
+
setIsOpen(true);
|
|
162
|
+
e.preventDefault();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (e.key === "ArrowDown")
|
|
166
|
+
setIsOpen(true);
|
|
167
|
+
if (e.key === "Escape")
|
|
168
|
+
setIsOpen(false);
|
|
169
|
+
};
|
|
170
|
+
// Same hover lift and press feedback as the Dropdown trigger.
|
|
171
|
+
const fieldClassName = cn(fieldInteractionClassName, keyboardFocus && fieldFocusOutlineClassName);
|
|
172
|
+
const decorateSingleInput = (input) => inputIcons ? (jsxs("div", { className: cn("relative min-w-0 flex-1", fieldClassName), ...fieldFocusProps, children: [inputIcons !== "search" && inputIcons !== "dropdown" && (jsx(Search, { className: "pointer-events-none absolute left-3 top-1/2 z-20 h-4 w-4 -translate-y-1/2 text-muted-foreground", "aria-hidden": "true" })), jsx(FieldChromeContext, { value: true, children: input }), inputIcons === "search" ? (jsx(Search, { className: "pointer-events-none absolute right-3 top-1/2 z-20 h-4 w-4 -translate-y-1/2 text-muted-foreground", "aria-hidden": "true" })) : (jsx(ChevronDown, { className: cn("pointer-events-none absolute right-3 top-1/2 z-20 h-4 w-4 -translate-y-1/2 text-muted-foreground transition-transform duration-interactive ease-out-strong motion-reduce:transition-none", isOpen && "rotate-180"), "aria-hidden": "true" }))] })) : (jsx("div", { className: cn("relative min-w-0 flex-1", fieldClassName), ...fieldFocusProps, children: jsx(FieldChromeContext, { value: true, children: input }) }));
|
|
173
|
+
const OptionsSurface = useMobileSheet ? SheetContent : 'div';
|
|
174
|
+
const sheetProps = useMobileSheet ? {
|
|
175
|
+
side: 'bottom',
|
|
176
|
+
showCloseButton: false,
|
|
177
|
+
overlayClassName: 'z-[1090] bg-black/25 backdrop-blur-[1px]',
|
|
178
|
+
'aria-describedby': undefined,
|
|
179
|
+
onOpenAutoFocus: (event) => { event.preventDefault(); sheetCloseRef.current?.focus(); },
|
|
180
|
+
onCloseAutoFocus: (event) => { event.preventDefault(); inputRef.current?.focus(); },
|
|
181
|
+
} : {};
|
|
182
|
+
return (jsxs("div", { className: cn("relative w-full overflow-visible", className), ref: (node) => {
|
|
183
|
+
rootRef.current = node;
|
|
184
|
+
clickOutsideRef.current = node;
|
|
185
|
+
}, children: [label ? (jsx(Label, { label: label, required: required, labelAddon: labelAddon })) : null, multiple ? (
|
|
186
|
+
// ---------- MULTI: chips inline inside the box ----------
|
|
187
|
+
jsxs("div", { className: cn("relative flex min-h-form-control w-full flex-wrap items-center gap-1 rounded-control-lg border border-input bg-background px-[0.75em] pr-10 hover:border-input-hover", fieldClassName), ...fieldFocusProps, onClick: () => {
|
|
188
|
+
setIsOpen(true);
|
|
189
|
+
if (!suppressMobileKeyboard) {
|
|
190
|
+
inputRef.current?.focus();
|
|
191
|
+
}
|
|
192
|
+
}, children: [(value ?? []).map((id) => {
|
|
193
|
+
const label = flatOptions.find((o) => o.id === id)?.name ?? String(id);
|
|
194
|
+
return (jsxs("span", { className: "my-0.5 flex items-center gap-1 rounded-control-sm border border-input bg-muted px-2 text-xs", children: [label, jsx(Button, { type: "button", variant: "ghost", size: "xs", className: "ml-1 h-5 min-h-0 w-5 p-0 hover:bg-muted", onClick: (event) => { event.stopPropagation(); removeAt(id); }, "aria-label": `Remove ${label}`, children: jsx(X, { className: "h-3.5 w-3.5" }) })] }, String(id)));
|
|
195
|
+
}), jsx("input", { id: inputId, ref: inputRef, value: query, onChange: (e) => {
|
|
196
|
+
setQuery(e.target.value);
|
|
197
|
+
setIsOpen(true);
|
|
198
|
+
}, onBlur: () => {
|
|
199
|
+
if (useMobileSheet)
|
|
200
|
+
return;
|
|
201
|
+
// Delay slightly to let dropdown option clicks register first
|
|
202
|
+
setTimeout(() => {
|
|
203
|
+
if (Date.now() - lastSelectionRef.current < 250) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (allowCustomValue && query.trim().length > 0) {
|
|
207
|
+
const text = query.trim();
|
|
208
|
+
const exact = findExactOption(text);
|
|
209
|
+
const nextId = exact ? exact.id : text;
|
|
210
|
+
setter(Array.from(new Set([...(value ?? []), nextId])));
|
|
211
|
+
setQuery("");
|
|
212
|
+
}
|
|
213
|
+
}, 150);
|
|
214
|
+
}, onKeyDown: onMultiKeyDown, onFocus: () => { if (!useMobileSheet)
|
|
215
|
+
setIsOpen(true); }, readOnly: suppressMobileKeyboard, placeholder: (value?.length ?? 0) === 0 ? placeholder : "", className: cn("h-[calc(var(--spacing-form-control)-2px)] min-w-[8ch] flex-1 border-none bg-transparent p-0 text-base outline-none focus:ring-0", "placeholder:text-muted-foreground/40"), ...props }), jsx(ChevronDown, { className: cn("pointer-events-none absolute right-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground transition-transform duration-interactive ease-out-strong motion-reduce:transition-none", isOpen && "rotate-180"), "aria-hidden": "true" })] })) : (
|
|
216
|
+
// ---------- SINGLE: regular input ----------
|
|
217
|
+
decorateSingleInput(jsx(Input, { id: inputId, value: singleDisplay || query, className: cn("border-input", inputIcons === "search" || inputIcons === "dropdown" ? "pr-10" : inputIcons && "px-10"), placeholder: placeholder, readOnly: suppressMobileKeyboard, onChange: (e) => {
|
|
218
|
+
const nextQuery = e.target.value;
|
|
219
|
+
if ((value?.length ?? 0) > 0) {
|
|
220
|
+
setter([]);
|
|
221
|
+
}
|
|
222
|
+
const exact = findExactOption(nextQuery);
|
|
223
|
+
// Only auto-fill on an exact NAME match; keyword/acronym matches
|
|
224
|
+
// (e.g. "dlsu") require an explicit Enter, blur, or click.
|
|
225
|
+
if (exact &&
|
|
226
|
+
exact.name.trim().toLowerCase() ===
|
|
227
|
+
nextQuery.trim().toLowerCase()) {
|
|
228
|
+
setter([exact.id]);
|
|
229
|
+
setQuery("");
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
setQuery(nextQuery);
|
|
233
|
+
}
|
|
234
|
+
setIsOpen(true);
|
|
235
|
+
}, onFocus: () => { if (!useMobileSheet)
|
|
236
|
+
setIsOpen(true); }, onClick: (e) => {
|
|
237
|
+
setIsOpen(true);
|
|
238
|
+
if (suppressMobileKeyboard) {
|
|
239
|
+
e.currentTarget.blur();
|
|
240
|
+
}
|
|
241
|
+
}, onKeyDown: (e) => {
|
|
242
|
+
if (e.key === "Enter" && resolveQuerySelection(query, true)) {
|
|
243
|
+
setIsOpen(false);
|
|
244
|
+
e.preventDefault();
|
|
245
|
+
}
|
|
246
|
+
}, onBlur: () => {
|
|
247
|
+
if (useMobileSheet)
|
|
248
|
+
return;
|
|
249
|
+
// slight delay to let dropdown option clicks register first
|
|
250
|
+
setTimeout(() => {
|
|
251
|
+
if (Date.now() - lastSelectionRef.current < 250) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (!resolveQuerySelection(query, false))
|
|
255
|
+
setQuery("");
|
|
256
|
+
}, 150);
|
|
257
|
+
} }))), isOpen && (jsx(Sheet, { open: useMobileSheet && isOpen, onOpenChange: next => { if (!next) {
|
|
258
|
+
setQuery('');
|
|
259
|
+
setIsOpen(false);
|
|
260
|
+
} }, children: jsxs(OptionsSurface, { ...sheetProps, ref: useMobileSheet ? sheetRef : undefined, role: useMobileSheet ? "dialog" : undefined, "aria-modal": useMobileSheet ? true : undefined, "aria-label": useMobileSheet ? (typeof label === "string" ? label : "Choose options") : undefined, className: cn("bg-background text-sm shadow-card border border-border", useMobileSheet
|
|
261
|
+
? "fixed left-0 right-0 z-[1100] flex flex-col gap-0 overflow-hidden rounded-t-control-lg border-b-0 p-0"
|
|
262
|
+
: "absolute left-0 right-0 z-50 mt-1 max-h-[400px] overflow-y-auto overscroll-contain rounded-control-lg"), style: useMobileSheet
|
|
263
|
+
? {
|
|
264
|
+
bottom: sheetViewport?.bottom ?? 0,
|
|
265
|
+
maxHeight: sheetViewport ? Math.min(sheetViewport.height, 640) : "80dvh",
|
|
266
|
+
}
|
|
267
|
+
: undefined, children: [useMobileSheet && (jsxs("div", { className: "shrink-0 px-4 pb-3 pt-4", children: [jsxs("div", { className: "mb-3 flex items-center justify-between gap-4", children: [jsx(SheetTitle, { className: "text-base font-semibold text-foreground", children: label || "Choose options" }), jsx("button", { ref: sheetCloseRef, type: "button", "aria-label": "Close options", className: "-mr-3 flex h-11 w-11 shrink-0 items-center justify-center rounded-control-lg text-muted-foreground hover:bg-muted", onClick: () => { setQuery(""); setIsOpen(false); }, children: jsx(X, { className: "h-5 w-5", "aria-hidden": "true" }) })] }), jsx(Input, { ref: sheetInputRef, "aria-label": typeof label === "string" ? `Search ${label}` : "Search options", value: query, placeholder: placeholder ?? "Search", className: "h-10 min-h-10 max-h-none text-base", onChange: (e) => {
|
|
268
|
+
setQuery(e.target.value);
|
|
269
|
+
setIsOpen(true);
|
|
270
|
+
if ((value?.length ?? 0) > 0 && !multiple) {
|
|
271
|
+
setter([]);
|
|
272
|
+
}
|
|
273
|
+
}, onKeyDown: (e) => {
|
|
274
|
+
if (multiple) {
|
|
275
|
+
onMultiKeyDown(e);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (e.key === "Enter" && resolveQuerySelection(query)) {
|
|
279
|
+
setIsOpen(false);
|
|
280
|
+
e.preventDefault();
|
|
281
|
+
}
|
|
282
|
+
if (e.key === "Escape") {
|
|
283
|
+
setIsOpen(false);
|
|
284
|
+
}
|
|
285
|
+
} })] })), jsxs("ul", { className: cn(useMobileSheet && "min-h-0 flex-1 overflow-y-auto overscroll-contain"), children: [allowCustomValue && (jsx("li", { className: "w-full text-left px-4 py-2 text-sm text-muted-foreground bg-muted pointer-events-none", children: "Suggestions" })), filtered.length ? (filtered.map((option, index) => {
|
|
286
|
+
const active = selectedSet.has(option.id);
|
|
287
|
+
const group = options.find((item) => item.children?.some((child) => child.id === option.id));
|
|
288
|
+
const firstInGroup = group && !filtered.slice(0, index).some((item) => group.children?.some((child) => child.id === item.id));
|
|
289
|
+
return (jsxs(Fragment, { children: [firstInGroup && group && (jsx("li", { children: multiple ? (jsxs("button", { type: "button", "aria-pressed": group.children.every((child) => selectedSet.has(child.id)), className: "flex min-h-11 w-full items-center gap-2 px-4 py-2 text-left text-sm font-medium text-foreground hover:bg-muted", onClick: () => {
|
|
290
|
+
const ids = group.children.map((child) => child.id);
|
|
291
|
+
const next = new Set(value ?? []);
|
|
292
|
+
const allSelected = ids.every((id) => next.has(id));
|
|
293
|
+
ids.forEach((id) => allSelected ? next.delete(id) : next.add(id));
|
|
294
|
+
setter(Array.from(next));
|
|
295
|
+
}, children: [jsx(CheckboxVisual, { checked: group.children.every((child) => selectedSet.has(child.id)) ? true : group.children.some((child) => selectedSet.has(child.id)) ? 'indeterminate' : false }), group.name] })) : jsx("div", { className: "px-4 py-2 text-sm font-medium text-foreground", children: group.name }) })), jsx("li", { children: jsxs("button", { type: "button", "aria-pressed": multiple ? active : undefined, onClick: () => {
|
|
296
|
+
toggle(option.id);
|
|
297
|
+
if (multiple) {
|
|
298
|
+
// keep open for rapid multi-pick
|
|
299
|
+
setQuery("");
|
|
300
|
+
if (!useMobileSheet) {
|
|
301
|
+
inputRef.current?.focus();
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
setQuery("");
|
|
306
|
+
setIsOpen(false);
|
|
307
|
+
}
|
|
308
|
+
}, className: cn("w-full text-left px-4 py-2 text-sm text-muted-foreground transition-colors flex items-center gap-2 cursor-pointer hover:bg-muted", useMobileSheet && "min-h-12", group && "pl-8", active && "bg-primary-tint"), children: [multiple ? (jsx(CheckboxVisual, { checked: active })) : null, option.name] }) })] }, String(option.id)));
|
|
309
|
+
})) : (!allowCustomValue || !query.trim() ? (jsx("li", { className: "w-full text-left px-4 py-2 text-sm text-muted-foreground", children: emptyText })) : null), allowCustomValue && query.trim() && !findExactOption(query) && (jsx("li", { children: jsxs("button", { type: "button", className: "flex min-h-11 w-full items-center gap-2 px-4 py-2 text-left text-sm text-primary hover:bg-muted focus-visible:outline-2 focus-visible:outline-primary", onMouseDown: (event) => event.preventDefault(), onClick: () => {
|
|
310
|
+
const text = query.trim();
|
|
311
|
+
if (multiple) {
|
|
312
|
+
setter(Array.from(new Set([...(value ?? []), text])));
|
|
313
|
+
setQuery("");
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
resolveQuerySelection(text);
|
|
317
|
+
setIsOpen(false);
|
|
318
|
+
}
|
|
319
|
+
}, children: [jsx(PlusCircleIcon, { className: "h-4 w-4", "aria-hidden": "true" }), "Add \u201C", query.trim(), "\u201D"] }) }))] }), useMobileSheet && multiple && (jsx("div", { className: "shrink-0 border-t border-border bg-background px-4 pt-3 pb-[max(0.75rem,env(safe-area-inset-bottom))]", children: jsx(Button, { type: "button", variant: "outline", className: "min-h-11 w-full", onClick: () => {
|
|
320
|
+
setQuery("");
|
|
321
|
+
setIsOpen(false);
|
|
322
|
+
}, children: "Done" }) }))] }) }))] }));
|
|
323
|
+
}
|
|
324
|
+
const Autocomplete = (config) => {
|
|
325
|
+
const { required, options, placeholder, className, label, props, allowCustomValue = false, preserveOptionOrder = false, emptyText, mobileDropdownMode, inputIcons } = config;
|
|
326
|
+
return (jsx(AutocompleteBase, { required: required, options: options, multiple: config.multiple === true, value: config.multiple === true ? config.value ?? [] : config.value != null ? [config.value] : [], setter: (arr) => {
|
|
327
|
+
if (config.multiple === true)
|
|
328
|
+
config.setter(arr);
|
|
329
|
+
else
|
|
330
|
+
config.setter(arr[0] ?? null);
|
|
331
|
+
}, placeholder: placeholder, className: className, label: label, allowCustomValue: allowCustomValue, preserveOptionOrder: preserveOptionOrder, emptyText: emptyText, mobileDropdownMode: mobileDropdownMode, inputIcons: inputIcons, ...props }));
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
export { Autocomplete };
|
|
335
|
+
//# sourceMappingURL=autocomplete.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"autocomplete.js","sources":["../../src/components/autocomplete.tsx"],"sourcesContent":["\"use client\";\n\nimport { Fragment, useEffect, useId, useMemo, useRef, useState } from \"react\";\nimport { Input } from \"./input\";\nimport { useDetectClickOutside } from '@/hooks/use-detect-click-outside';\nimport { cn } from \"@/utils\";\nimport { ChevronDown, PlusCircleIcon, Search, X } from \"lucide-react\";\nimport { Label } from \"./label\";\nimport { Button } from \"./button\";\nimport { CheckboxVisual } from \"./checkbox\";\nimport { Sheet, SheetContent, SheetTitle } from \"./sheet\";\nimport { useMobile } from \"@/hooks/use-mobile\";\nimport { useMobileSheetViewport } from \"@/hooks/use-mobile-sheet-viewport\";\nimport {\n FieldChromeContext,\n fieldFocusOutlineClassName,\n fieldInteractionClassName,\n useFieldFocus,\n} from \"@/hooks/use-field-focus\";\n\nexport interface IAutocompleteOption<ID extends number | string> {\n id: ID;\n name: string;\n keywords?: string[];\n /** Child options; in multiple mode the group can select all its children. */\n children?: IAutocompleteOption<ID>[];\n}\n\ntype MobileDropdownMode = \"sheet\" | \"inline\";\ntype InputIcons = boolean | \"search\" | \"dropdown\";\n\n/* -------------------------------------------------------\n * Base component (array-based). Single uses length 0..1.\n * -----------------------------------------------------*/\nfunction AutocompleteBase<ID extends number | string>({\n required,\n options,\n value = [],\n setter,\n placeholder,\n className,\n multiple = true,\n label,\n labelAddon,\n allowCustomValue = false,\n preserveOptionOrder = false,\n emptyText = \"No matching options\",\n mobileDropdownMode = \"sheet\",\n inputIcons = false,\n ...props\n}: {\n required?: boolean;\n options: IAutocompleteOption<ID>[];\n value?: ID[]; // single => 0..1 items, multi => any length\n setter: (next: ID[]) => void; // set the whole selection\n placeholder?: string;\n className?: string;\n multiple?: boolean;\n label?: React.ReactNode;\n labelAddon?: React.ReactNode;\n allowCustomValue?: boolean;\n preserveOptionOrder?: boolean;\n emptyText?: string;\n mobileDropdownMode?: MobileDropdownMode;\n inputIcons?: InputIcons;\n}) {\n const [query, setQuery] = useState(\"\");\n const [isOpen, setIsOpen] = useState(false);\n const [dropdownMaxHeight, setDropdownMaxHeight] = useState<number>();\n const isMobile = useMobile();\n const clickOutsideRef = useDetectClickOutside({\n onTriggered: () => {\n // Radix owns outside dismissal for the portalled modal sheet.\n if (isMobile && mobileDropdownMode === 'sheet') return;\n setIsOpen(false);\n },\n });\n const rootRef = useRef<HTMLDivElement | null>(null);\n const inputRef = useRef<HTMLInputElement | null>(null);\n const sheetInputRef = useRef<HTMLInputElement | null>(null);\n const sheetRef = useRef<HTMLDivElement | null>(null);\n const sheetCloseRef = useRef<HTMLButtonElement | null>(null);\n const lastSelectionRef = useRef(0);\n // Show the Button focus outline only for keyboard focus, not clicks.\n const { keyboardFocus, focusProps: fieldFocusProps } = useFieldFocus();\n\n const inputId = useId();\n const flatOptions = useMemo(\n () => options.flatMap((option) => option.children?.length ? option.children : [option]),\n [options],\n );\n\n const findExactOption = (value: string) => {\n const normalizedValue = value.trim().toLowerCase();\n return flatOptions.find(\n (o) =>\n o.name?.trim().toLowerCase() === normalizedValue ||\n o.keywords?.some((k) => k === normalizedValue),\n );\n };\n\n const resolveQuerySelection = (nextQuery: string, matchKeywords = true) => {\n const text = nextQuery.trim();\n if (!text) return false;\n\n const exact = matchKeywords\n ? findExactOption(text)\n : flatOptions.find(\n (o) => o.name?.trim().toLowerCase() === text.toLowerCase(),\n );\n if (exact) {\n setter([exact.id]);\n setQuery(\"\");\n return true;\n }\n\n if (allowCustomValue) {\n setter([text as unknown as ID]);\n setQuery(\"\");\n return true;\n }\n\n return false;\n };\n\n const selectedSet = useMemo(() => new Set(value ?? []), [value]);\n\n const filtered = useMemo(() => {\n const q = query.trim().toLowerCase();\n const base = q\n ? flatOptions.filter(\n (o) =>\n o.name?.toLowerCase().includes(q) ||\n options.some((group) => group.children?.some((child) => child.id === o.id) && group.name.toLowerCase().includes(q)) ||\n o.keywords?.some((k) => k.includes(q)),\n )\n : flatOptions;\n return preserveOptionOrder || options.some((option) => option.children?.length)\n ? base\n : base.slice().sort((a, b) => a.name.localeCompare(b.name));\n }, [query, flatOptions, options, preserveOptionOrder]);\n\n const toggle = (id: ID) => {\n lastSelectionRef.current = Date.now();\n if (!multiple) {\n setter([id]); // single-select\n setIsOpen(false);\n setQuery(\"\");\n return;\n }\n const set = new Set(value ?? []);\n if (set.has(id)) set.delete(id);\n else set.add(id);\n setter(Array.from(set));\n };\n\n const removeAt = (id: ID) => {\n const set = new Set(value ?? []);\n set.delete(id);\n setter(Array.from(set));\n };\n\n const selectedLabels = useMemo(\n () =>\n (value ?? [])\n .map((id) => {\n const label = flatOptions.find((o) => o.id === id)?.name;\n return label ?? (allowCustomValue ? String(id) : undefined);\n })\n .filter(Boolean) as string[],\n [value, flatOptions, allowCustomValue],\n );\n\n const singleDisplay = !multiple && (selectedLabels[0] ?? \"\");\n const useMobileSheet = isMobile && mobileDropdownMode === \"sheet\";\n const sheetViewport = useMobileSheetViewport(isOpen && useMobileSheet);\n const useInlineMobileDropdown = isMobile && mobileDropdownMode === \"inline\";\n\n\n useEffect(() => {\n if (!isOpen || !useInlineMobileDropdown) {\n setDropdownMaxHeight(undefined);\n return;\n }\n\n const updateDropdownMaxHeight = () => {\n const rootRect = rootRef.current?.getBoundingClientRect();\n if (!rootRect) return;\n\n const viewportHeight =\n window.visualViewport?.height ?? window.innerHeight;\n const gap = 8;\n const availableHeight = viewportHeight - rootRect.bottom - gap;\n setDropdownMaxHeight(Math.max(0, Math.min(400, availableHeight)));\n };\n\n updateDropdownMaxHeight();\n window.addEventListener(\"resize\", updateDropdownMaxHeight);\n window.visualViewport?.addEventListener(\"resize\", updateDropdownMaxHeight);\n window.visualViewport?.addEventListener(\"scroll\", updateDropdownMaxHeight);\n\n return () => {\n window.removeEventListener(\"resize\", updateDropdownMaxHeight);\n window.visualViewport?.removeEventListener(\n \"resize\",\n updateDropdownMaxHeight,\n );\n window.visualViewport?.removeEventListener(\n \"scroll\",\n updateDropdownMaxHeight,\n );\n };\n }, [isOpen, useInlineMobileDropdown]);\n\n const suppressMobileKeyboard =\n isMobile && (useMobileSheet || (mobileDropdownMode === \"inline\" && !allowCustomValue));\n\n // --- keyboard helpers for multi\n const onMultiKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {\n if (!multiple) return;\n if (allowCustomValue && e.key === \"Enter\" && query.trim().length > 0 && !findExactOption(query)) {\n const text = query.trim();\n const exact = findExactOption(text);\n const nextId = exact ? exact.id : (text as unknown as ID);\n const set = new Set(value ?? []);\n set.add(nextId);\n setter(Array.from(set));\n setQuery(\"\");\n setIsOpen(true);\n e.preventDefault();\n return;\n }\n if (\n e.key === \"Backspace\" &&\n query.length === 0 &&\n (value?.length ?? 0) > 0\n ) {\n const last = value[value.length - 1];\n removeAt(last);\n e.preventDefault();\n }\n if (e.key === \"Enter\") {\n const first = filtered[0];\n if (first) {\n toggle(first.id);\n setQuery(\"\");\n setIsOpen(true);\n e.preventDefault();\n }\n }\n if (e.key === \"ArrowDown\") setIsOpen(true);\n if (e.key === \"Escape\") setIsOpen(false);\n };\n\n // Same hover lift and press feedback as the Dropdown trigger.\n const fieldClassName = cn(\n fieldInteractionClassName,\n keyboardFocus && fieldFocusOutlineClassName,\n );\n\n const decorateSingleInput = (input: React.ReactNode) =>\n inputIcons ? (\n <div className={cn(\"relative min-w-0 flex-1\", fieldClassName)} {...fieldFocusProps}>\n {inputIcons !== \"search\" && inputIcons !== \"dropdown\" && (\n <Search\n className=\"pointer-events-none absolute left-3 top-1/2 z-20 h-4 w-4 -translate-y-1/2 text-muted-foreground\"\n aria-hidden=\"true\"\n />\n )}\n <FieldChromeContext value={true}>{input}</FieldChromeContext>\n {inputIcons === \"search\" ? (\n <Search\n className=\"pointer-events-none absolute right-3 top-1/2 z-20 h-4 w-4 -translate-y-1/2 text-muted-foreground\"\n aria-hidden=\"true\"\n />\n ) : (\n <ChevronDown\n className={cn(\n \"pointer-events-none absolute right-3 top-1/2 z-20 h-4 w-4 -translate-y-1/2 text-muted-foreground transition-transform duration-interactive ease-out-strong motion-reduce:transition-none\",\n isOpen && \"rotate-180\",\n )}\n aria-hidden=\"true\"\n />\n )}\n </div>\n ) : (\n <div className={cn(\"relative min-w-0 flex-1\", fieldClassName)} {...fieldFocusProps}>\n <FieldChromeContext value={true}>{input}</FieldChromeContext>\n </div>\n );\n\n const OptionsSurface = useMobileSheet ? SheetContent : 'div';\n const sheetProps = useMobileSheet ? {\n side: 'bottom' as const,\n showCloseButton: false,\n overlayClassName: 'z-[1090] bg-black/25 backdrop-blur-[1px]',\n 'aria-describedby': undefined,\n onOpenAutoFocus: (event: Event) => { event.preventDefault(); sheetCloseRef.current?.focus(); },\n onCloseAutoFocus: (event: Event) => { event.preventDefault(); inputRef.current?.focus(); },\n } : {};\n return (\n <div\n className={cn(\"relative w-full overflow-visible\", className)}\n ref={(node) => {\n rootRef.current = node;\n (\n clickOutsideRef as React.MutableRefObject<HTMLDivElement | null>\n ).current = node;\n }}\n >\n {label ? (\n <Label\n label={label}\n required={required}\n labelAddon={labelAddon}\n />\n ) : null}\n\n {multiple ? (\n // ---------- MULTI: chips inline inside the box ----------\n <div\n className={cn(\n \"relative flex min-h-form-control w-full flex-wrap items-center gap-1 rounded-control-lg border border-input bg-background px-[0.75em] pr-10 hover:border-input-hover\",\n fieldClassName,\n )}\n {...fieldFocusProps}\n onClick={() => {\n setIsOpen(true);\n if (!suppressMobileKeyboard) {\n inputRef.current?.focus();\n }\n }}\n >\n {(value ?? []).map((id) => {\n const label = flatOptions.find((o) => o.id === id)?.name ?? String(id);\n return (\n <span\n key={String(id)}\n className=\"my-0.5 flex items-center gap-1 rounded-control-sm border border-input bg-muted px-2 text-xs\"\n >\n {label}\n <Button\n type=\"button\"\n variant=\"ghost\"\n size=\"xs\"\n className=\"ml-1 h-5 min-h-0 w-5 p-0 hover:bg-muted\"\n onClick={(event) => { event.stopPropagation(); removeAt(id); }}\n aria-label={`Remove ${label}`}\n >\n <X className=\"h-3.5 w-3.5\" />\n </Button>\n </span>\n );\n })}\n <input\n id={inputId}\n ref={inputRef}\n value={query}\n onChange={(e) => {\n setQuery(e.target.value);\n setIsOpen(true);\n }}\n onBlur={() => {\n if (useMobileSheet) return;\n // Delay slightly to let dropdown option clicks register first\n setTimeout(() => {\n if (Date.now() - lastSelectionRef.current < 250) {\n return;\n }\n if (allowCustomValue && query.trim().length > 0) {\n const text = query.trim();\n const exact = findExactOption(text);\n const nextId = exact ? exact.id : (text as unknown as ID);\n setter(Array.from(new Set([...(value ?? []), nextId])));\n setQuery(\"\");\n }\n }, 150);\n }}\n onKeyDown={onMultiKeyDown}\n onFocus={() => { if (!useMobileSheet) setIsOpen(true); }}\n readOnly={suppressMobileKeyboard}\n placeholder={(value?.length ?? 0) === 0 ? placeholder : \"\"}\n className={cn(\n \"h-[calc(var(--spacing-form-control)-2px)] min-w-[8ch] flex-1 border-none bg-transparent p-0 text-base outline-none focus:ring-0\",\n \"placeholder:text-muted-foreground/40\",\n )}\n {...props}\n />\n <ChevronDown\n className={cn(\n \"pointer-events-none absolute right-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground transition-transform duration-interactive ease-out-strong motion-reduce:transition-none\",\n isOpen && \"rotate-180\",\n )}\n aria-hidden=\"true\"\n />\n </div>\n ) : (\n // ---------- SINGLE: regular input ----------\n decorateSingleInput(\n <Input\n id={inputId}\n value={singleDisplay || query}\n className={cn(\n \"border-input\",\n inputIcons === \"search\" || inputIcons === \"dropdown\" ? \"pr-10\" : inputIcons && \"px-10\",\n )}\n placeholder={placeholder}\n readOnly={suppressMobileKeyboard}\n onChange={(e) => {\n const nextQuery = e.target.value;\n\n if ((value?.length ?? 0) > 0) {\n setter([]);\n }\n\n const exact = findExactOption(nextQuery);\n // Only auto-fill on an exact NAME match; keyword/acronym matches\n // (e.g. \"dlsu\") require an explicit Enter, blur, or click.\n if (\n exact &&\n exact.name.trim().toLowerCase() ===\n nextQuery.trim().toLowerCase()\n ) {\n setter([exact.id]);\n setQuery(\"\");\n } else {\n setQuery(nextQuery);\n }\n\n setIsOpen(true);\n }}\n onFocus={() => { if (!useMobileSheet) setIsOpen(true); }}\n onClick={(e) => {\n setIsOpen(true);\n if (suppressMobileKeyboard) {\n e.currentTarget.blur();\n }\n }}\n onKeyDown={(e) => {\n if (e.key === \"Enter\" && resolveQuerySelection(query, true)) {\n setIsOpen(false);\n e.preventDefault();\n }\n }}\n onBlur={() => {\n if (useMobileSheet) return;\n // slight delay to let dropdown option clicks register first\n setTimeout(() => {\n if (Date.now() - lastSelectionRef.current < 250) {\n return;\n }\n if (!resolveQuerySelection(query, false)) setQuery(\"\");\n }, 150);\n }}\n />,\n )\n )}\n\n {isOpen && (\n <Sheet open={useMobileSheet && isOpen} onOpenChange={next => { if (!next) { setQuery(''); setIsOpen(false); } }}>\n <OptionsSurface\n {...sheetProps}\n ref={useMobileSheet ? sheetRef : undefined}\n role={useMobileSheet ? \"dialog\" : undefined}\n aria-modal={useMobileSheet ? true : undefined}\n aria-label={useMobileSheet ? (typeof label === \"string\" ? label : \"Choose options\") : undefined}\n className={cn(\n \"bg-background text-sm shadow-card border border-border\",\n useMobileSheet\n ? \"fixed left-0 right-0 z-[1100] flex flex-col gap-0 overflow-hidden rounded-t-control-lg border-b-0 p-0\"\n : \"absolute left-0 right-0 z-50 mt-1 max-h-[400px] overflow-y-auto overscroll-contain rounded-control-lg\",\n )}\n style={\n useMobileSheet\n ? {\n bottom: sheetViewport?.bottom ?? 0,\n maxHeight: sheetViewport ? Math.min(sheetViewport.height, 640) : \"80dvh\",\n }\n : undefined\n }\n >\n {useMobileSheet && (\n <div className=\"shrink-0 px-4 pb-3 pt-4\">\n <div className=\"mb-3 flex items-center justify-between gap-4\">\n <SheetTitle className=\"text-base font-semibold text-foreground\">{label || \"Choose options\"}</SheetTitle>\n <button ref={sheetCloseRef} type=\"button\" aria-label=\"Close options\" className=\"-mr-3 flex h-11 w-11 shrink-0 items-center justify-center rounded-control-lg text-muted-foreground hover:bg-muted\" onClick={() => { setQuery(\"\"); setIsOpen(false); }}>\n <X className=\"h-5 w-5\" aria-hidden=\"true\" />\n </button>\n </div>\n <Input\n ref={sheetInputRef}\n aria-label={typeof label === \"string\" ? `Search ${label}` : \"Search options\"}\n value={query}\n placeholder={placeholder ?? \"Search\"}\n className=\"h-10 min-h-10 max-h-none text-base\"\n onChange={(e) => {\n setQuery(e.target.value);\n setIsOpen(true);\n if ((value?.length ?? 0) > 0 && !multiple) {\n setter([]);\n }\n }}\n onKeyDown={(e) => {\n if (multiple) {\n onMultiKeyDown(e);\n return;\n }\n\n if (e.key === \"Enter\" && resolveQuerySelection(query)) {\n setIsOpen(false);\n e.preventDefault();\n }\n\n if (e.key === \"Escape\") {\n setIsOpen(false);\n }\n }}\n />\n </div>\n )}\n <ul className={cn(useMobileSheet && \"min-h-0 flex-1 overflow-y-auto overscroll-contain\")}>\n {allowCustomValue && (\n <li className=\"w-full text-left px-4 py-2 text-sm text-muted-foreground bg-muted pointer-events-none\">\n Suggestions\n </li>\n )}\n {filtered.length ? (\n filtered.map((option, index) => {\n const active = selectedSet.has(option.id);\n const group = options.find((item) => item.children?.some((child) => child.id === option.id));\n const firstInGroup = group && !filtered.slice(0, index).some((item) => group.children?.some((child) => child.id === item.id));\n return (\n <Fragment key={String(option.id)}>\n {firstInGroup && group && (\n <li>\n {multiple ? (\n <button type=\"button\" aria-pressed={group.children!.every((child) => selectedSet.has(child.id))} className=\"flex min-h-11 w-full items-center gap-2 px-4 py-2 text-left text-sm font-medium text-foreground hover:bg-muted\" onClick={() => {\n const ids = group.children!.map((child) => child.id);\n const next = new Set(value ?? []);\n const allSelected = ids.every((id) => next.has(id));\n ids.forEach((id) => allSelected ? next.delete(id) : next.add(id));\n setter(Array.from(next));\n }}>\n <CheckboxVisual checked={group.children!.every((child) => selectedSet.has(child.id)) ? true : group.children!.some((child) => selectedSet.has(child.id)) ? 'indeterminate' : false} />\n {group.name}\n </button>\n ) : <div className=\"px-4 py-2 text-sm font-medium text-foreground\">{group.name}</div>}\n </li>\n )}\n <li>\n <button\n type=\"button\"\n aria-pressed={multiple ? active : undefined}\n onClick={() => {\n toggle(option.id);\n if (multiple) {\n // keep open for rapid multi-pick\n setQuery(\"\");\n if (!useMobileSheet) {\n inputRef.current?.focus();\n }\n } else {\n setQuery(\"\");\n setIsOpen(false);\n }\n }}\n className={cn(\n \"w-full text-left px-4 py-2 text-sm text-muted-foreground transition-colors flex items-center gap-2 cursor-pointer hover:bg-muted\",\n useMobileSheet && \"min-h-12\",\n group && \"pl-8\",\n active && \"bg-primary-tint\",\n )}\n >\n {multiple ? (\n <CheckboxVisual checked={active} />\n ) : null}\n {option.name}\n </button>\n </li>\n </Fragment>\n );\n })\n ) : (\n !allowCustomValue || !query.trim() ? (\n <li className=\"w-full text-left px-4 py-2 text-sm text-muted-foreground\">\n {emptyText}\n </li>\n ) : null\n )}\n {allowCustomValue && query.trim() && !findExactOption(query) && (\n <li>\n <button\n type=\"button\"\n className=\"flex min-h-11 w-full items-center gap-2 px-4 py-2 text-left text-sm text-primary hover:bg-muted focus-visible:outline-2 focus-visible:outline-primary\"\n onMouseDown={(event) => event.preventDefault()}\n onClick={() => {\n const text = query.trim();\n if (multiple) {\n setter(Array.from(new Set([...(value ?? []), text as unknown as ID])));\n setQuery(\"\");\n } else {\n resolveQuerySelection(text);\n setIsOpen(false);\n }\n }}\n >\n <PlusCircleIcon className=\"h-4 w-4\" aria-hidden=\"true\" />\n Add “{query.trim()}”\n </button>\n </li>\n )}\n </ul>\n {useMobileSheet && multiple && (\n <div className=\"shrink-0 border-t border-border bg-background px-4 pt-3 pb-[max(0.75rem,env(safe-area-inset-bottom))]\">\n <Button\n type=\"button\"\n variant=\"outline\"\n className=\"min-h-11 w-full\"\n onClick={() => {\n setQuery(\"\");\n setIsOpen(false);\n }}\n >\n Done\n </Button>\n </div>\n )}\n </OptionsSurface>\n </Sheet>\n )}\n </div>\n );\n}\n\ntype AutocompleteCommonProps<ID extends number | string> = {\n required?: boolean;\n options: IAutocompleteOption<ID>[];\n placeholder?: string;\n className?: string;\n label?: React.ReactNode;\n props?: any[];\n /** Allow a user to add their own text. Custom values are strings. */\n allowCustomValue?: boolean;\n preserveOptionOrder?: boolean;\n emptyText?: string;\n mobileDropdownMode?: MobileDropdownMode;\n inputIcons?: InputIcons;\n};\n\nexport type AutocompleteProps<ID extends number | string> = AutocompleteCommonProps<ID> & (\n | { multiple?: false; value?: ID | null; setter: (value?: ID | null) => void }\n | { multiple: true; value?: ID[]; setter: (value: ID[]) => void }\n);\n\nexport const Autocomplete = <ID extends number | string>(config: AutocompleteProps<ID>) => {\n const { required, options, placeholder, className, label, props, allowCustomValue = false,\n preserveOptionOrder = false, emptyText, mobileDropdownMode, inputIcons } = config;\n return (\n <AutocompleteBase<ID>\n required={required}\n options={options}\n multiple={config.multiple === true}\n value={config.multiple === true ? config.value ?? [] : config.value != null ? [config.value] : []}\n setter={(arr) => {\n if (config.multiple === true) config.setter(arr);\n else config.setter(arr[0] ?? null);\n }}\n placeholder={placeholder}\n className={className}\n label={label}\n allowCustomValue={allowCustomValue}\n preserveOptionOrder={preserveOptionOrder}\n emptyText={emptyText}\n mobileDropdownMode={mobileDropdownMode}\n inputIcons={inputIcons}\n {...props}\n />\n );\n};\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AA+BA;;AAE0D;AAC1D;;;;AAmCE;;;;AAII;;;;AAGH;AACD;AACA;AACA;AACA;AACA;AACA;;;AAIA;AACA;AAKA;;AAEE;AAGI;AAEN;;AAGE;AACA;AAAW;;AAGT;;;AAKA;;AAEA;;;AAIA;;AAEA;;AAGF;AACF;AAEA;AAEA;;;;AAMY;AACA;;AAGV;AACE;;;AAIJ;AACE;;AAEE;;;;;;AAMF;AAAiB;;AACZ;;AAEP;AAEA;;AAEE;;AAEF;;AAKO;AACC;AACA;AACF;AACC;AAIP;AACA;;AAEA;;AAIE;;;;;;AAOE;;;;;AAMA;AACF;AAEA;AACA;;;AAIA;AACE;;;AASF;AACF;AAEA;;AAIA;AACE;;;AAEE;AACA;AACA;;AAEA;;;;;;;AAOF;;;;;;;AASA;AACE;;AAEE;;;;;;AAMJ;;AACA;;AACF;;;;;AAwCA;AACE;AACA;AACA;AACA;;;;AAIF;AAIM;AAEE;AAEJ;;AAYE;;;AASM;;AAEJ;;AAIE;;AAwBE;;AAEF;AAEE;;;;;;;;AAOI;AACA;AACA;;;;;AAKN;AAEsC;;;AA8BpC;;;;AAMA;;;AAGA;AAEE;AACE;AAEF;;;;;;;;AAQkC;;;AAIlC;;AAEJ;AAEE;;;;AAIF;AAEE;;;;;;;AAME;;;;;;AAQoG;AAUnG;AACA;AAID;AACI;AACA;AACD;;AAmBC;;AAEA;;;AAGF;;;;;;;;;AAYE;;;AAGF;;AAaF;AACA;;AAOU;;AAEA;AACA;;;AAcJ;;;;;AAKI;;;;;;;;AAsBZ;AAeM;;;;;;;;;;;;;AAoCpB;AAsBO;AACL;AAEA;AAOM;AAA8B;;;AAEhC;AAYN;;"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import useEmblaCarousel from '../node_modules/embla-carousel-react/esm/embla-carousel-react.esm.js';
|
|
5
|
+
import { ArrowLeft, ArrowRight } from 'lucide-react';
|
|
6
|
+
import { cn } from '../utils.js';
|
|
7
|
+
import { Button } from './button.js';
|
|
8
|
+
|
|
9
|
+
const CarouselContext = React.createContext(null);
|
|
10
|
+
function useCarousel() {
|
|
11
|
+
const context = React.useContext(CarouselContext);
|
|
12
|
+
if (!context) {
|
|
13
|
+
throw new Error("useCarousel must be used within a <Carousel />");
|
|
14
|
+
}
|
|
15
|
+
return context;
|
|
16
|
+
}
|
|
17
|
+
const Carousel = React.forwardRef(({ orientation = "horizontal", opts, setApi, plugins, autoPlay = false, autoPlayInterval = 5000, className, children, ...props }, ref) => {
|
|
18
|
+
const [carouselRef, api] = useEmblaCarousel({
|
|
19
|
+
...opts,
|
|
20
|
+
axis: orientation === "horizontal" ? "x" : "y",
|
|
21
|
+
}, plugins);
|
|
22
|
+
const [canScrollPrev, setCanScrollPrev] = React.useState(false);
|
|
23
|
+
const [canScrollNext, setCanScrollNext] = React.useState(false);
|
|
24
|
+
const [hovered, setHovered] = React.useState(false);
|
|
25
|
+
const [focused, setFocused] = React.useState(false);
|
|
26
|
+
const [reducedMotion, setReducedMotion] = React.useState(true);
|
|
27
|
+
React.useEffect(() => {
|
|
28
|
+
const media = window.matchMedia('(prefers-reduced-motion: reduce)');
|
|
29
|
+
const update = () => setReducedMotion(media.matches);
|
|
30
|
+
update();
|
|
31
|
+
media.addEventListener('change', update);
|
|
32
|
+
return () => media.removeEventListener('change', update);
|
|
33
|
+
}, []);
|
|
34
|
+
React.useEffect(() => {
|
|
35
|
+
if (!api || !autoPlay || hovered || focused || reducedMotion || autoPlayInterval <= 0)
|
|
36
|
+
return;
|
|
37
|
+
const timer = window.setInterval(() => {
|
|
38
|
+
if (api.canScrollNext())
|
|
39
|
+
api.scrollNext();
|
|
40
|
+
else
|
|
41
|
+
api.scrollTo(0);
|
|
42
|
+
}, autoPlayInterval);
|
|
43
|
+
return () => window.clearInterval(timer);
|
|
44
|
+
}, [api, autoPlay, hovered, focused, reducedMotion, autoPlayInterval]);
|
|
45
|
+
const onSelect = React.useCallback((api) => {
|
|
46
|
+
if (!api) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
setCanScrollPrev(api.canScrollPrev());
|
|
50
|
+
setCanScrollNext(api.canScrollNext());
|
|
51
|
+
}, []);
|
|
52
|
+
const scrollPrev = React.useCallback(() => {
|
|
53
|
+
api?.scrollPrev();
|
|
54
|
+
}, [api]);
|
|
55
|
+
const scrollNext = React.useCallback(() => {
|
|
56
|
+
api?.scrollNext();
|
|
57
|
+
}, [api]);
|
|
58
|
+
const handleKeyDown = React.useCallback((event) => {
|
|
59
|
+
if (event.key === "ArrowLeft") {
|
|
60
|
+
event.preventDefault();
|
|
61
|
+
scrollPrev();
|
|
62
|
+
}
|
|
63
|
+
else if (event.key === "ArrowRight") {
|
|
64
|
+
event.preventDefault();
|
|
65
|
+
scrollNext();
|
|
66
|
+
}
|
|
67
|
+
}, [scrollPrev, scrollNext]);
|
|
68
|
+
React.useEffect(() => {
|
|
69
|
+
if (!api || !setApi) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
setApi(api);
|
|
73
|
+
}, [api, setApi]);
|
|
74
|
+
React.useEffect(() => {
|
|
75
|
+
if (!api) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
onSelect(api);
|
|
79
|
+
api.on("reInit", onSelect);
|
|
80
|
+
api.on("select", onSelect);
|
|
81
|
+
return () => {
|
|
82
|
+
api?.off("select", onSelect);
|
|
83
|
+
};
|
|
84
|
+
}, [api, onSelect]);
|
|
85
|
+
return (jsx(CarouselContext.Provider, { value: {
|
|
86
|
+
carouselRef,
|
|
87
|
+
api: api,
|
|
88
|
+
opts,
|
|
89
|
+
orientation: orientation || (opts?.axis === "y" ? "vertical" : "horizontal"),
|
|
90
|
+
scrollPrev,
|
|
91
|
+
scrollNext,
|
|
92
|
+
canScrollPrev,
|
|
93
|
+
canScrollNext,
|
|
94
|
+
}, children: jsx("div", { ref: ref, onKeyDownCapture: handleKeyDown, className: cn("relative", className), role: "region", "aria-roledescription": "carousel", ...props, onPointerEnter: (event) => { if (event.pointerType === 'mouse')
|
|
95
|
+
setHovered(true); props.onPointerEnter?.(event); }, onPointerLeave: (event) => { if (event.pointerType === 'mouse')
|
|
96
|
+
setHovered(false); props.onPointerLeave?.(event); }, onFocusCapture: (event) => { setFocused(true); props.onFocusCapture?.(event); }, onBlurCapture: (event) => { if (!event.currentTarget.contains(event.relatedTarget))
|
|
97
|
+
setFocused(false); props.onBlurCapture?.(event); }, children: children }) }));
|
|
98
|
+
});
|
|
99
|
+
Carousel.displayName = "Carousel";
|
|
100
|
+
const CarouselContent = React.forwardRef(({ className, ...props }, ref) => {
|
|
101
|
+
const { carouselRef, orientation } = useCarousel();
|
|
102
|
+
return (jsx("div", { ref: carouselRef, className: "overflow-hidden", children: jsx("div", { ref: ref, className: cn("flex", orientation === "horizontal" ? "-ml-4" : "-mt-4 flex-col", className), ...props }) }));
|
|
103
|
+
});
|
|
104
|
+
CarouselContent.displayName = "CarouselContent";
|
|
105
|
+
const CarouselItem = React.forwardRef(({ className, ...props }, ref) => {
|
|
106
|
+
const { orientation } = useCarousel();
|
|
107
|
+
return (jsx("div", { ref: ref, role: "group", "aria-roledescription": "slide", className: cn("min-w-0 shrink-0 grow-0 basis-full", orientation === "horizontal" ? "pl-4" : "pt-4", className), ...props }));
|
|
108
|
+
});
|
|
109
|
+
CarouselItem.displayName = "CarouselItem";
|
|
110
|
+
const CarouselPrevious = React.forwardRef(({ className, variant = "outline", size = "xs", ...props }, ref) => {
|
|
111
|
+
const { orientation, scrollPrev, canScrollPrev } = useCarousel();
|
|
112
|
+
return (jsxs(Button, { ref: ref, variant: variant, size: size, className: cn("absolute h-8 w-8 min-h-0 rounded-full border-input bg-background p-0 text-foreground enabled:hover:border-primary/20 enabled:hover:bg-primary-tint enabled:hover:text-primary pointer-fine:hover:shadow-none motion-safe:pointer-fine:hover:translate-y-0 motion-safe:active:scale-100", orientation === "horizontal"
|
|
113
|
+
? "-left-12 top-1/2 -translate-y-1/2 motion-safe:pointer-fine:hover:-translate-y-1/2"
|
|
114
|
+
: "-top-12 left-1/2 -translate-x-1/2 rotate-90", className), disabled: !canScrollPrev, onClick: scrollPrev, ...props, children: [jsx(ArrowLeft, { className: "h-4 w-4" }), jsx("span", { className: "sr-only", children: "Previous slide" })] }));
|
|
115
|
+
});
|
|
116
|
+
CarouselPrevious.displayName = "CarouselPrevious";
|
|
117
|
+
const CarouselNext = React.forwardRef(({ className, variant = "outline", size = "xs", ...props }, ref) => {
|
|
118
|
+
const { orientation, scrollNext, canScrollNext } = useCarousel();
|
|
119
|
+
return (jsxs(Button, { ref: ref, variant: variant, size: size, className: cn("absolute h-8 w-8 min-h-0 rounded-full border-input bg-background p-0 text-foreground enabled:hover:border-primary/20 enabled:hover:bg-primary-tint enabled:hover:text-primary pointer-fine:hover:shadow-none motion-safe:pointer-fine:hover:translate-y-0 motion-safe:active:scale-100", orientation === "horizontal"
|
|
120
|
+
? "-right-12 top-1/2 -translate-y-1/2 motion-safe:pointer-fine:hover:-translate-y-1/2"
|
|
121
|
+
: "-bottom-12 left-1/2 -translate-x-1/2 rotate-90", className), disabled: !canScrollNext, onClick: scrollNext, ...props, children: [jsx(ArrowRight, { className: "h-4 w-4" }), jsx("span", { className: "sr-only", children: "Next slide" })] }));
|
|
122
|
+
});
|
|
123
|
+
CarouselNext.displayName = "CarouselNext";
|
|
124
|
+
|
|
125
|
+
export { Carousel, CarouselContent, CarouselItem, CarouselNext, CarouselPrevious };
|
|
126
|
+
//# sourceMappingURL=carousel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"carousel.js","sources":["../../src/components/carousel.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\nimport useEmblaCarousel, {\n type UseEmblaCarouselType,\n} from \"embla-carousel-react\";\nimport { ArrowLeft, ArrowRight } from \"lucide-react\";\n\nimport { cn } from \"@/utils\";\nimport { Button, type ButtonProps } from \"./button\";\n\ntype CarouselApi = UseEmblaCarouselType[1];\ntype UseCarouselParameters = Parameters<typeof useEmblaCarousel>;\ntype CarouselOptions = UseCarouselParameters[0];\ntype CarouselPlugin = UseCarouselParameters[1];\n\ntype CarouselProps = {\n opts?: CarouselOptions;\n plugins?: CarouselPlugin;\n orientation?: \"horizontal\" | \"vertical\";\n setApi?: (api: CarouselApi) => void;\n /** Automatically advance slides while the carousel is not hovered or focused. */\n autoPlay?: boolean;\n /** Delay between automatic advances in milliseconds. */\n autoPlayInterval?: number;\n};\n\ntype CarouselContextProps = {\n carouselRef: ReturnType<typeof useEmblaCarousel>[0];\n api: ReturnType<typeof useEmblaCarousel>[1];\n scrollPrev: () => void;\n scrollNext: () => void;\n canScrollPrev: boolean;\n canScrollNext: boolean;\n} & CarouselProps;\n\nconst CarouselContext = React.createContext<CarouselContextProps | null>(null);\n\nfunction useCarousel() {\n const context = React.useContext(CarouselContext);\n\n if (!context) {\n throw new Error(\"useCarousel must be used within a <Carousel />\");\n }\n\n return context;\n}\n\nconst Carousel = React.forwardRef<\n HTMLDivElement,\n React.HTMLAttributes<HTMLDivElement> & CarouselProps\n>(\n (\n {\n orientation = \"horizontal\",\n opts,\n setApi,\n plugins,\n autoPlay = false,\n autoPlayInterval = 5000,\n className,\n children,\n ...props\n },\n ref,\n ) => {\n const [carouselRef, api] = useEmblaCarousel(\n {\n ...opts,\n axis: orientation === \"horizontal\" ? \"x\" : \"y\",\n },\n plugins,\n );\n const [canScrollPrev, setCanScrollPrev] = React.useState(false);\n const [canScrollNext, setCanScrollNext] = React.useState(false);\n const [hovered, setHovered] = React.useState(false);\n const [focused, setFocused] = React.useState(false);\n const [reducedMotion, setReducedMotion] = React.useState(true);\n\n React.useEffect(() => {\n const media = window.matchMedia('(prefers-reduced-motion: reduce)');\n const update = () => setReducedMotion(media.matches);\n update();\n media.addEventListener('change', update);\n return () => media.removeEventListener('change', update);\n }, []);\n\n React.useEffect(() => {\n if (!api || !autoPlay || hovered || focused || reducedMotion || autoPlayInterval <= 0) return;\n const timer = window.setInterval(() => {\n if (api.canScrollNext()) api.scrollNext();\n else api.scrollTo(0);\n }, autoPlayInterval);\n return () => window.clearInterval(timer);\n }, [api, autoPlay, hovered, focused, reducedMotion, autoPlayInterval]);\n\n const onSelect = React.useCallback((api: CarouselApi) => {\n if (!api) {\n return;\n }\n\n setCanScrollPrev(api.canScrollPrev());\n setCanScrollNext(api.canScrollNext());\n }, []);\n\n const scrollPrev = React.useCallback(() => {\n api?.scrollPrev();\n }, [api]);\n\n const scrollNext = React.useCallback(() => {\n api?.scrollNext();\n }, [api]);\n\n const handleKeyDown = React.useCallback(\n (event: React.KeyboardEvent<HTMLDivElement>) => {\n if (event.key === \"ArrowLeft\") {\n event.preventDefault();\n scrollPrev();\n } else if (event.key === \"ArrowRight\") {\n event.preventDefault();\n scrollNext();\n }\n },\n [scrollPrev, scrollNext],\n );\n\n React.useEffect(() => {\n if (!api || !setApi) {\n return;\n }\n\n setApi(api);\n }, [api, setApi]);\n\n React.useEffect(() => {\n if (!api) {\n return;\n }\n\n onSelect(api);\n api.on(\"reInit\", onSelect);\n api.on(\"select\", onSelect);\n\n return () => {\n api?.off(\"select\", onSelect);\n };\n }, [api, onSelect]);\n\n return (\n <CarouselContext.Provider\n value={{\n carouselRef,\n api: api,\n opts,\n orientation:\n orientation || (opts?.axis === \"y\" ? \"vertical\" : \"horizontal\"),\n scrollPrev,\n scrollNext,\n canScrollPrev,\n canScrollNext,\n }}\n >\n <div\n ref={ref}\n onKeyDownCapture={handleKeyDown}\n className={cn(\"relative\", className)}\n role=\"region\"\n aria-roledescription=\"carousel\"\n {...props}\n onPointerEnter={(event) => { if (event.pointerType === 'mouse') setHovered(true); props.onPointerEnter?.(event); }}\n onPointerLeave={(event) => { if (event.pointerType === 'mouse') setHovered(false); props.onPointerLeave?.(event); }}\n onFocusCapture={(event) => { setFocused(true); props.onFocusCapture?.(event); }}\n onBlurCapture={(event) => { if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false); props.onBlurCapture?.(event); }}\n >\n {children}\n </div>\n </CarouselContext.Provider>\n );\n },\n);\nCarousel.displayName = \"Carousel\";\n\nconst CarouselContent = React.forwardRef<\n HTMLDivElement,\n React.HTMLAttributes<HTMLDivElement>\n>(({ className, ...props }, ref) => {\n const { carouselRef, orientation } = useCarousel();\n\n return (\n <div ref={carouselRef} className=\"overflow-hidden\">\n <div\n ref={ref}\n className={cn(\n \"flex\",\n orientation === \"horizontal\" ? \"-ml-4\" : \"-mt-4 flex-col\",\n className,\n )}\n {...props}\n />\n </div>\n );\n});\nCarouselContent.displayName = \"CarouselContent\";\n\nconst CarouselItem = React.forwardRef<\n HTMLDivElement,\n React.HTMLAttributes<HTMLDivElement>\n>(({ className, ...props }, ref) => {\n const { orientation } = useCarousel();\n\n return (\n <div\n ref={ref}\n role=\"group\"\n aria-roledescription=\"slide\"\n className={cn(\n \"min-w-0 shrink-0 grow-0 basis-full\",\n orientation === \"horizontal\" ? \"pl-4\" : \"pt-4\",\n className,\n )}\n {...props}\n />\n );\n});\nCarouselItem.displayName = \"CarouselItem\";\n\nconst CarouselPrevious = React.forwardRef<\n HTMLButtonElement,\n ButtonProps\n>(({ className, variant = \"outline\", size = \"xs\", ...props }, ref) => {\n const { orientation, scrollPrev, canScrollPrev } = useCarousel();\n\n return (\n <Button\n ref={ref}\n variant={variant}\n size={size}\n className={cn(\n \"absolute h-8 w-8 min-h-0 rounded-full border-input bg-background p-0 text-foreground enabled:hover:border-primary/20 enabled:hover:bg-primary-tint enabled:hover:text-primary pointer-fine:hover:shadow-none motion-safe:pointer-fine:hover:translate-y-0 motion-safe:active:scale-100\",\n orientation === \"horizontal\"\n ? \"-left-12 top-1/2 -translate-y-1/2 motion-safe:pointer-fine:hover:-translate-y-1/2\"\n : \"-top-12 left-1/2 -translate-x-1/2 rotate-90\",\n className,\n )}\n disabled={!canScrollPrev}\n onClick={scrollPrev}\n {...props}\n >\n <ArrowLeft className=\"h-4 w-4\" />\n <span className=\"sr-only\">Previous slide</span>\n </Button>\n );\n});\nCarouselPrevious.displayName = \"CarouselPrevious\";\n\nconst CarouselNext = React.forwardRef<\n HTMLButtonElement,\n ButtonProps\n>(({ className, variant = \"outline\", size = \"xs\", ...props }, ref) => {\n const { orientation, scrollNext, canScrollNext } = useCarousel();\n\n return (\n <Button\n ref={ref}\n variant={variant}\n size={size}\n className={cn(\n \"absolute h-8 w-8 min-h-0 rounded-full border-input bg-background p-0 text-foreground enabled:hover:border-primary/20 enabled:hover:bg-primary-tint enabled:hover:text-primary pointer-fine:hover:shadow-none motion-safe:pointer-fine:hover:translate-y-0 motion-safe:active:scale-100\",\n orientation === \"horizontal\"\n ? \"-right-12 top-1/2 -translate-y-1/2 motion-safe:pointer-fine:hover:-translate-y-1/2\"\n : \"-bottom-12 left-1/2 -translate-x-1/2 rotate-90\",\n className,\n )}\n disabled={!canScrollNext}\n onClick={scrollNext}\n {...props}\n >\n <ArrowRight className=\"h-4 w-4\" />\n <span className=\"sr-only\">Next slide</span>\n </Button>\n );\n});\nCarouselNext.displayName = \"CarouselNext\";\n\nexport {\n type CarouselApi,\n Carousel,\n CarouselContent,\n CarouselItem,\n CarouselPrevious,\n CarouselNext,\n};\n"],"names":[],"mappings":";;;;;;;;AAoCA;AAEA;;;AAII;;AAGF;AACF;AAEA;AAkBI;AAEI;;;AAKJ;AACA;AACA;AACA;AACA;AAEA;;;AAGE;AACA;;;AAIF;AACE;;AACA;;;;AAEO;;;AAGT;;;;;AAOE;AACA;;AAGF;;AAEA;AAEA;;AAEA;;AAII;;AAEE;;AACK;;AAEL;;AAEJ;AAIF;AACE;;;;AAKF;AAEA;;;;;AAME;AACA;AAEA;AACE;AACF;AACF;AAEA;;AAIM;;AAEA;;;;;AAMD;AASiE;;;AASxE;AAEF;AAEA;;AAME;AAaF;AACA;AAEA;AAIE;AAEA;AAaF;AACA;AAEA;;;AAcU;AACA;AAWV;AACA;AAEA;;;AAcU;AACA;AAWV;AACA;;"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
|
+
import * as ToggleGroupPrimitive from '@radix-ui/react-toggle-group';
|
|
4
|
+
import { CheckboxVisual } from './checkbox.js';
|
|
5
|
+
import { cn } from '../utils.js';
|
|
6
|
+
|
|
7
|
+
const chipClassName = (selected) => cn('inline-flex min-h-11 w-max max-w-full items-center gap-2 rounded-full border border-landing-control bg-background px-3 py-2 text-sm font-medium text-landing-nav transition-colors duration-state enabled:hover:border-landing-control-hover enabled:hover:bg-[rgb(55_65_81_/_6%)] enabled:hover:text-landing-nav-hover focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring motion-reduce:transition-none', selected && 'border-primary bg-primary-tint text-primary enabled:hover:border-primary enabled:hover:bg-primary-tint enabled:hover:text-primary');
|
|
8
|
+
const RadioVisual = ({ checked }) => (jsxs("svg", { "aria-hidden": "true", "data-state": checked ? 'checked' : 'unchecked', className: "size-4 shrink-0", viewBox: "0 0 16 16", children: [jsx("circle", { cx: "8", cy: "8", r: "7", fill: checked ? 'var(--primary)' : 'var(--muted)', stroke: checked ? 'var(--primary)' : 'var(--input)', strokeWidth: "1" }), checked && jsx("circle", { cx: "8", cy: "8", r: "4", fill: "var(--background)" })] }));
|
|
9
|
+
/** A compact form choice group supporting single or multiple selection. */
|
|
10
|
+
function ChipSelect({ value, onChange, options, className, multiple = false }) {
|
|
11
|
+
const groupValue = (multiple ? value : value ?? '');
|
|
12
|
+
const SelectGroup = ToggleGroupPrimitive.Root;
|
|
13
|
+
return (jsx(SelectGroup, { type: multiple ? 'multiple' : 'single', value: groupValue, onValueChange: (next) => multiple
|
|
14
|
+
? onChange(next)
|
|
15
|
+
: onChange(next || null), className: cn('flex flex-wrap justify-start gap-2', className), children: options.map((option) => {
|
|
16
|
+
const selected = multiple ? value.includes(option.value) : value === option.value;
|
|
17
|
+
return jsxs(ToggleGroupPrimitive.Item, { value: option.value, className: chipClassName(selected), children: [multiple ? jsx(CheckboxVisual, { checked: selected }) : jsx(RadioVisual, { checked: selected }), option.label] }, option.value);
|
|
18
|
+
}) }));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { ChipSelect };
|
|
22
|
+
//# sourceMappingURL=chip-select.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chip-select.js","sources":["../../src/components/chip-select.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from 'react';\nimport * as ToggleGroupPrimitive from '@radix-ui/react-toggle-group';\nimport { CheckboxVisual } from './checkbox';\nimport { cn } from '@/utils';\n\nexport type Option = { value: string; label: string };\n\nconst chipClassName = (selected: boolean) => cn(\n 'inline-flex min-h-11 w-max max-w-full items-center gap-2 rounded-full border border-landing-control bg-background px-3 py-2 text-sm font-medium text-landing-nav transition-colors duration-state enabled:hover:border-landing-control-hover enabled:hover:bg-[rgb(55_65_81_/_6%)] enabled:hover:text-landing-nav-hover focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring motion-reduce:transition-none',\n selected && 'border-primary bg-primary-tint text-primary enabled:hover:border-primary enabled:hover:bg-primary-tint enabled:hover:text-primary',\n);\n\nconst RadioVisual = ({ checked }: { checked: boolean }) => (\n <svg\n aria-hidden=\"true\"\n data-state={checked ? 'checked' : 'unchecked'}\n className=\"size-4 shrink-0\"\n viewBox=\"0 0 16 16\"\n >\n <circle\n cx=\"8\"\n cy=\"8\"\n r=\"7\"\n fill={checked ? 'var(--primary)' : 'var(--muted)'}\n stroke={checked ? 'var(--primary)' : 'var(--input)'}\n strokeWidth=\"1\"\n />\n {checked && <circle cx=\"8\" cy=\"8\" r=\"4\" fill=\"var(--background)\" />}\n </svg>\n);\n\nexport type ChipSelectProps = {\n options: Option[];\n className?: string;\n} & ({\n multiple: true;\n value: string[];\n onChange: (value: string[]) => void;\n} | {\n multiple?: false;\n value: string | null;\n onChange: (value: string | null) => void;\n});\n\n/** A compact form choice group supporting single or multiple selection. */\nexport function ChipSelect({ value, onChange, options, className, multiple = false }: ChipSelectProps) {\n const groupValue = (multiple ? value : value ?? '') as string | string[];\n const SelectGroup = ToggleGroupPrimitive.Root as React.ComponentType<any>;\n return (\n <SelectGroup\n type={multiple ? 'multiple' : 'single'}\n value={groupValue}\n onValueChange={(next: string | string[]) => multiple\n ? (onChange as (value: string[]) => void)(next as string[])\n : (onChange as (value: string | null) => void)((next as string) || null)}\n className={cn('flex flex-wrap justify-start gap-2', className)}\n >\n {options.map((option) => {\n const selected = multiple ? (value as string[]).includes(option.value) : value === option.value;\n return <ToggleGroupPrimitive.Item key={option.value} value={option.value} className={chipClassName(selected)}>\n {multiple ? <CheckboxVisual checked={selected} /> : <RadioVisual checked={selected} />}\n {option.label}\n </ToggleGroupPrimitive.Item>;\n })}\n </SelectGroup>\n );\n}\n"],"names":[],"mappings":";;;;;;AASA;AAKA;AAgCA;AACM;AACJ;AACA;;AAMM;;;;;AAaR;;"}
|