@ladder-ui/select 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -0
- package/dist/index.js +338 -0
- package/dist/index.mjs +327 -0
- package/dist/select.css +160 -0
- package/dist/select.d.ts +119 -0
- package/dist/select.vars.css +28 -0
- package/package.json +70 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectScrollDownButton, SelectScrollUpButton, SelectSeparator, SelectTrigger, SelectValue, } from "./select";
|
|
2
|
+
export type { SelectProps, SelectItemData, SelectContentProps, SelectGroupProps, SelectItemProps, SelectLabelProps, SelectScrollDownButtonProps, SelectScrollUpButtonProps, SelectSeparatorProps, SelectTriggerProps, SelectValueProps, } from "./select";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var reactDom = require('react-dom');
|
|
6
|
+
var concatClassNames = require('@ladder-ui/core/concatClassNames');
|
|
7
|
+
|
|
8
|
+
// ─── Inline SVG icons ────────────────────────────────────────────────────────
|
|
9
|
+
function ChevronDownIcon({ className }) {
|
|
10
|
+
return (jsxRuntime.jsx("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", className: className, children: jsxRuntime.jsx("path", { d: "m6 9 6 6 6-6" }) }));
|
|
11
|
+
}
|
|
12
|
+
function ChevronUpIcon({ className }) {
|
|
13
|
+
return (jsxRuntime.jsx("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", className: className, children: jsxRuntime.jsx("path", { d: "m18 15-6-6-6 6" }) }));
|
|
14
|
+
}
|
|
15
|
+
function CheckIcon({ className }) {
|
|
16
|
+
return (jsxRuntime.jsx("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2.5, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", className: className, children: jsxRuntime.jsx("path", { d: "M20 6 9 17l-5-5" }) }));
|
|
17
|
+
}
|
|
18
|
+
// ─── Label extraction helpers ────────────────────────────────────────────────
|
|
19
|
+
//
|
|
20
|
+
// We scan the JSX children tree in Select root to build a value→label map.
|
|
21
|
+
// This works even when SelectContent renders null (closed state), because the
|
|
22
|
+
// JSX element tree passed as `children` always contains the full structure.
|
|
23
|
+
function extractTextContent(children) {
|
|
24
|
+
if (typeof children === "string")
|
|
25
|
+
return children;
|
|
26
|
+
if (typeof children === "number")
|
|
27
|
+
return String(children);
|
|
28
|
+
if (Array.isArray(children))
|
|
29
|
+
return children.map(extractTextContent).join("");
|
|
30
|
+
if (react.isValidElement(children)) {
|
|
31
|
+
return extractTextContent(children.props.children);
|
|
32
|
+
}
|
|
33
|
+
return "";
|
|
34
|
+
}
|
|
35
|
+
function scanItemLabels(children, registry) {
|
|
36
|
+
react.Children.forEach(children, (child) => {
|
|
37
|
+
if (!react.isValidElement(child))
|
|
38
|
+
return;
|
|
39
|
+
const props = child.props;
|
|
40
|
+
// Detect SelectItem by displayName on the element type function
|
|
41
|
+
const dn = typeof child.type === "function"
|
|
42
|
+
? child.type.displayName
|
|
43
|
+
: typeof child.type === "object"
|
|
44
|
+
? child.type.displayName
|
|
45
|
+
: undefined;
|
|
46
|
+
if (dn === "SelectItem" && (typeof props.value === "string" || props.value === null)) {
|
|
47
|
+
// null is the Shadcn-style "no selection" sentinel — stored as "" internally
|
|
48
|
+
const key = props.value ?? "";
|
|
49
|
+
const label = extractTextContent(props.children);
|
|
50
|
+
if (label)
|
|
51
|
+
registry.set(key, label);
|
|
52
|
+
}
|
|
53
|
+
// Recurse into children of non-item elements
|
|
54
|
+
if (props.children) {
|
|
55
|
+
scanItemLabels(props.children, registry);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
const SelectContext = react.createContext({
|
|
60
|
+
open: false,
|
|
61
|
+
setOpen: () => { },
|
|
62
|
+
value: undefined,
|
|
63
|
+
displayLabel: "",
|
|
64
|
+
triggerId: "",
|
|
65
|
+
contentId: "",
|
|
66
|
+
triggerRef: { current: null },
|
|
67
|
+
});
|
|
68
|
+
function Select({ value: controlledValue, defaultValue, onValueChange, disabled, children, open: controlledOpen, onOpenChange, items, }) {
|
|
69
|
+
const [internalOpen, setInternalOpen] = react.useState(false);
|
|
70
|
+
const [internalValue, setInternalValue] = react.useState(defaultValue);
|
|
71
|
+
const isOpenControlled = controlledOpen !== undefined;
|
|
72
|
+
const isValueControlled = controlledValue !== undefined;
|
|
73
|
+
const open = isOpenControlled ? controlledOpen : internalOpen;
|
|
74
|
+
const value = isValueControlled ? controlledValue : internalValue;
|
|
75
|
+
const triggerId = react.useId();
|
|
76
|
+
const contentId = react.useId();
|
|
77
|
+
const triggerRef = react.useRef(null);
|
|
78
|
+
// Build label registry synchronously from the JSX children tree.
|
|
79
|
+
// This works even when SelectContent renders null (closed), because the
|
|
80
|
+
// JSX element tree always contains all SelectItem elements.
|
|
81
|
+
const labelsMap = new Map();
|
|
82
|
+
scanItemLabels(children, labelsMap);
|
|
83
|
+
// Merge items prop — supports null values and data-driven usage (.map()).
|
|
84
|
+
// Takes precedence over child-scanned labels for matching keys.
|
|
85
|
+
if (items) {
|
|
86
|
+
for (const item of items) {
|
|
87
|
+
labelsMap.set(item.value ?? "", item.label);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const displayLabel = (value !== undefined ? labelsMap.get(value) : undefined) ?? "";
|
|
91
|
+
const setOpen = react.useCallback((next) => {
|
|
92
|
+
if (!isOpenControlled)
|
|
93
|
+
setInternalOpen(next);
|
|
94
|
+
onOpenChange?.(next);
|
|
95
|
+
}, [isOpenControlled, onOpenChange]);
|
|
96
|
+
const handleValueChange = react.useCallback((newValue) => {
|
|
97
|
+
if (!isValueControlled)
|
|
98
|
+
setInternalValue(newValue);
|
|
99
|
+
onValueChange?.(newValue);
|
|
100
|
+
setOpen(false);
|
|
101
|
+
}, [isValueControlled, onValueChange, setOpen]);
|
|
102
|
+
return (jsxRuntime.jsx(SelectContext, { value: {
|
|
103
|
+
open,
|
|
104
|
+
setOpen,
|
|
105
|
+
value,
|
|
106
|
+
displayLabel,
|
|
107
|
+
onValueChange: handleValueChange,
|
|
108
|
+
disabled,
|
|
109
|
+
triggerId,
|
|
110
|
+
contentId,
|
|
111
|
+
triggerRef,
|
|
112
|
+
}, children: children }));
|
|
113
|
+
}
|
|
114
|
+
Select.displayName = "Select";
|
|
115
|
+
function SelectTrigger({ ref, className, children, size = "default", fullWidth, disabled: propDisabled, onKeyDown, ...props }) {
|
|
116
|
+
const ctx = react.use(SelectContext);
|
|
117
|
+
const isDisabled = propDisabled || ctx.disabled;
|
|
118
|
+
// Merge consumer ref + context triggerRef
|
|
119
|
+
const setRef = (el) => {
|
|
120
|
+
ctx.triggerRef.current = el;
|
|
121
|
+
if (typeof ref === "function") {
|
|
122
|
+
ref(el);
|
|
123
|
+
}
|
|
124
|
+
else if (ref) {
|
|
125
|
+
ref.current = el;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
const handleKeyDown = (e) => {
|
|
129
|
+
if (["ArrowDown", "ArrowUp", "Enter", " "].includes(e.key)) {
|
|
130
|
+
e.preventDefault();
|
|
131
|
+
ctx.setOpen(true);
|
|
132
|
+
}
|
|
133
|
+
onKeyDown?.(e);
|
|
134
|
+
};
|
|
135
|
+
const classes = concatClassNames("lui-select-trigger", size === "sm" ? "lui-select-trigger--sm" : undefined, fullWidth ? "lui-select-trigger--full-width" : undefined, className);
|
|
136
|
+
return (jsxRuntime.jsxs("button", { ref: setRef, type: "button", role: "combobox", id: ctx.triggerId, "aria-expanded": ctx.open, "aria-haspopup": "listbox", "aria-controls": ctx.open ? ctx.contentId : undefined, "aria-disabled": isDisabled || undefined, disabled: isDisabled, "data-slot": "select-trigger", "data-size": size, "data-full-width": fullWidth || undefined, className: classes, onClick: () => ctx.setOpen(!ctx.open), onKeyDown: handleKeyDown, ...props, children: [children, jsxRuntime.jsx(ChevronDownIcon, { className: "lui-select-trigger__chevron" })] }));
|
|
137
|
+
}
|
|
138
|
+
SelectTrigger.displayName = "SelectTrigger";
|
|
139
|
+
function SelectValue({ placeholder, className }) {
|
|
140
|
+
const ctx = react.use(SelectContext);
|
|
141
|
+
const classes = concatClassNames("lui-select-value", className);
|
|
142
|
+
return (jsxRuntime.jsx("span", { "data-slot": "select-value", className: classes, children: ctx.displayLabel || placeholder || "" }));
|
|
143
|
+
}
|
|
144
|
+
SelectValue.displayName = "SelectValue";
|
|
145
|
+
function SelectContent({ ref, className, children, position = "item-aligned", }) {
|
|
146
|
+
const ctx = react.use(SelectContext);
|
|
147
|
+
const contentRef = react.useRef(null);
|
|
148
|
+
// Two-pass positioning:
|
|
149
|
+
// Pass 1 — render with visibility:hidden so browser can measure item offsets.
|
|
150
|
+
// Pass 2 — compute correct position, set isPositioned=true → visibility:visible.
|
|
151
|
+
const [isPositioned, setIsPositioned] = react.useState(false);
|
|
152
|
+
const [coords, setCoords] = react.useState({ top: 0, left: 0, width: 0 });
|
|
153
|
+
// Pass 1→2: measure and position after first render
|
|
154
|
+
react.useEffect(() => {
|
|
155
|
+
if (!ctx.open || !contentRef.current || !ctx.triggerRef.current)
|
|
156
|
+
return;
|
|
157
|
+
const triggerRect = ctx.triggerRef.current.getBoundingClientRect();
|
|
158
|
+
const content = contentRef.current;
|
|
159
|
+
const contentHeight = content.offsetHeight;
|
|
160
|
+
const contentWidth = content.offsetWidth;
|
|
161
|
+
const viewportHeight = window.innerHeight;
|
|
162
|
+
const viewportWidth = window.innerWidth;
|
|
163
|
+
let top;
|
|
164
|
+
if (position === "item-aligned") {
|
|
165
|
+
// Align the selected item (or first item) vertically with the trigger.
|
|
166
|
+
// The item's center should coincide with the trigger's center.
|
|
167
|
+
const selectedItem = content.querySelector('[role="option"][aria-selected="true"]');
|
|
168
|
+
const firstItem = content.querySelector('[role="option"]');
|
|
169
|
+
const targetItem = selectedItem ?? firstItem;
|
|
170
|
+
if (targetItem) {
|
|
171
|
+
// offsetTop is relative to the fixed content div (its offsetParent)
|
|
172
|
+
const itemTop = targetItem.offsetTop;
|
|
173
|
+
const itemHeight = targetItem.offsetHeight;
|
|
174
|
+
// Center item with trigger: move content up so item.center = trigger.center
|
|
175
|
+
top =
|
|
176
|
+
triggerRect.top -
|
|
177
|
+
itemTop +
|
|
178
|
+
(triggerRect.height - itemHeight) / 2;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
top = triggerRect.top;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
// Popper: open below trigger, fall back to above if insufficient space
|
|
186
|
+
const spaceBelow = viewportHeight - triggerRect.bottom;
|
|
187
|
+
if (spaceBelow < contentHeight + 8 && triggerRect.top > contentHeight + 8) {
|
|
188
|
+
top = triggerRect.top - contentHeight - 4;
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
top = triggerRect.bottom + 4;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// Clamp to viewport with 8 px margin
|
|
195
|
+
top = Math.max(8, Math.min(top, viewportHeight - contentHeight - 8));
|
|
196
|
+
const left = Math.max(8, Math.min(triggerRect.left, viewportWidth - contentWidth - 8));
|
|
197
|
+
setCoords({ top, left, width: triggerRect.width });
|
|
198
|
+
setIsPositioned(true);
|
|
199
|
+
// Run after every open (component remounts each time, so state resets automatically)
|
|
200
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
201
|
+
}, [ctx.open]);
|
|
202
|
+
// Focus management — wait until content is visible to avoid jarring jump
|
|
203
|
+
react.useEffect(() => {
|
|
204
|
+
if (!ctx.open || !isPositioned || !contentRef.current)
|
|
205
|
+
return;
|
|
206
|
+
const selected = contentRef.current.querySelector('[role="option"][aria-selected="true"]');
|
|
207
|
+
const first = contentRef.current.querySelector('[role="option"]:not([aria-disabled="true"])');
|
|
208
|
+
(selected ?? first)?.focus();
|
|
209
|
+
}, [ctx.open, isPositioned]);
|
|
210
|
+
// Click-outside to close
|
|
211
|
+
react.useEffect(() => {
|
|
212
|
+
if (!ctx.open)
|
|
213
|
+
return;
|
|
214
|
+
const handlePointerDown = (e) => {
|
|
215
|
+
const target = e.target;
|
|
216
|
+
if (contentRef.current &&
|
|
217
|
+
!contentRef.current.contains(target) &&
|
|
218
|
+
ctx.triggerRef.current &&
|
|
219
|
+
!ctx.triggerRef.current.contains(target)) {
|
|
220
|
+
ctx.setOpen(false);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
document.addEventListener("pointerdown", handlePointerDown);
|
|
224
|
+
return () => document.removeEventListener("pointerdown", handlePointerDown);
|
|
225
|
+
}, [ctx.open, ctx.setOpen, ctx.triggerRef]);
|
|
226
|
+
// Keyboard navigation within the listbox
|
|
227
|
+
const handleKeyDown = (e) => {
|
|
228
|
+
const items = contentRef.current?.querySelectorAll('[role="option"]:not([aria-disabled="true"])');
|
|
229
|
+
if (!items || items.length === 0)
|
|
230
|
+
return;
|
|
231
|
+
const active = document.activeElement;
|
|
232
|
+
const currentIndex = Array.from(items).indexOf(active);
|
|
233
|
+
switch (e.key) {
|
|
234
|
+
case "ArrowDown": {
|
|
235
|
+
e.preventDefault();
|
|
236
|
+
items[Math.min(currentIndex + 1, items.length - 1)]?.focus();
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
case "ArrowUp": {
|
|
240
|
+
e.preventDefault();
|
|
241
|
+
items[Math.max(currentIndex - 1, 0)]?.focus();
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
case "Home": {
|
|
245
|
+
e.preventDefault();
|
|
246
|
+
items[0]?.focus();
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
case "End": {
|
|
250
|
+
e.preventDefault();
|
|
251
|
+
items[items.length - 1]?.focus();
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
case "Escape":
|
|
255
|
+
case "Tab": {
|
|
256
|
+
ctx.setOpen(false);
|
|
257
|
+
ctx.triggerRef.current?.focus();
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
if (!ctx.open)
|
|
263
|
+
return null;
|
|
264
|
+
const style = {
|
|
265
|
+
position: "fixed",
|
|
266
|
+
top: coords.top,
|
|
267
|
+
left: coords.left,
|
|
268
|
+
width: coords.width || undefined,
|
|
269
|
+
minWidth: coords.width || undefined,
|
|
270
|
+
zIndex: 9999,
|
|
271
|
+
// Hidden during pass 1 to allow measurement without visual flash
|
|
272
|
+
visibility: isPositioned ? "visible" : "hidden",
|
|
273
|
+
};
|
|
274
|
+
const setRef = (el) => {
|
|
275
|
+
contentRef.current = el;
|
|
276
|
+
if (typeof ref === "function") {
|
|
277
|
+
ref(el);
|
|
278
|
+
}
|
|
279
|
+
else if (ref) {
|
|
280
|
+
ref.current = el;
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
const classes = concatClassNames("lui-select-content", className);
|
|
284
|
+
return reactDom.createPortal(jsxRuntime.jsx("div", { ref: setRef, id: ctx.contentId, role: "listbox", "aria-labelledby": ctx.triggerId, "data-slot": "select-content", "data-state": "open", className: classes, style: style, onKeyDown: handleKeyDown, children: jsxRuntime.jsx("div", { className: "lui-select-content__viewport", children: children }) }), document.body);
|
|
285
|
+
}
|
|
286
|
+
SelectContent.displayName = "SelectContent";
|
|
287
|
+
function SelectGroup({ ref, className, children, ...props }) {
|
|
288
|
+
return (jsxRuntime.jsx("div", { ref: ref, role: "group", "data-slot": "select-group", className: concatClassNames("lui-select-group", className), ...props, children: children }));
|
|
289
|
+
}
|
|
290
|
+
SelectGroup.displayName = "SelectGroup";
|
|
291
|
+
function SelectLabel({ ref, className, children, ...props }) {
|
|
292
|
+
return (jsxRuntime.jsx("div", { ref: ref, "data-slot": "select-label", className: concatClassNames("lui-select-label", className), ...props, children: children }));
|
|
293
|
+
}
|
|
294
|
+
SelectLabel.displayName = "SelectLabel";
|
|
295
|
+
function SelectItem({ ref, className, children, value, disabled, onClick, onKeyDown, ...props }) {
|
|
296
|
+
const ctx = react.use(SelectContext);
|
|
297
|
+
// null is the Shadcn-style sentinel for "no selection" — normalize to ""
|
|
298
|
+
const normalizedValue = value ?? "";
|
|
299
|
+
const isSelected = ctx.value === normalizedValue;
|
|
300
|
+
const handleSelect = () => {
|
|
301
|
+
if (disabled)
|
|
302
|
+
return;
|
|
303
|
+
ctx.onValueChange?.(normalizedValue);
|
|
304
|
+
};
|
|
305
|
+
const handleKeyDown = (e) => {
|
|
306
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
307
|
+
e.preventDefault();
|
|
308
|
+
handleSelect();
|
|
309
|
+
}
|
|
310
|
+
onKeyDown?.(e);
|
|
311
|
+
};
|
|
312
|
+
const classes = concatClassNames("lui-select-item", isSelected ? "lui-select-item--selected" : undefined, disabled ? "lui-select-item--disabled" : undefined, className);
|
|
313
|
+
return (jsxRuntime.jsxs("div", { ref: ref, role: "option", "aria-selected": isSelected, "aria-disabled": disabled || undefined, tabIndex: disabled ? -1 : 0, "data-slot": "select-item", "data-value": normalizedValue, className: classes, onClick: handleSelect, onKeyDown: handleKeyDown, ...props, children: [jsxRuntime.jsx("span", { className: "lui-select-item__indicator", "aria-hidden": "true", children: isSelected && jsxRuntime.jsx(CheckIcon, { className: "lui-select-item__check" }) }), jsxRuntime.jsx("span", { className: "lui-select-item__text", children: children })] }));
|
|
314
|
+
}
|
|
315
|
+
SelectItem.displayName = "SelectItem";
|
|
316
|
+
function SelectSeparator({ ref, className, ...props }) {
|
|
317
|
+
return (jsxRuntime.jsx("hr", { ref: ref, "data-slot": "select-separator", className: concatClassNames("lui-select-separator", className), ...props }));
|
|
318
|
+
}
|
|
319
|
+
SelectSeparator.displayName = "SelectSeparator";
|
|
320
|
+
function SelectScrollUpButton({ ref, className, ...props }) {
|
|
321
|
+
return (jsxRuntime.jsx("div", { ref: ref, "data-slot": "select-scroll-up-button", className: concatClassNames("lui-select-scroll-button", className), ...props, children: jsxRuntime.jsx(ChevronUpIcon, { className: "lui-select-scroll-button__icon" }) }));
|
|
322
|
+
}
|
|
323
|
+
SelectScrollUpButton.displayName = "SelectScrollUpButton";
|
|
324
|
+
function SelectScrollDownButton({ ref, className, ...props }) {
|
|
325
|
+
return (jsxRuntime.jsx("div", { ref: ref, "data-slot": "select-scroll-down-button", className: concatClassNames("lui-select-scroll-button", className), ...props, children: jsxRuntime.jsx(ChevronDownIcon, { className: "lui-select-scroll-button__icon" }) }));
|
|
326
|
+
}
|
|
327
|
+
SelectScrollDownButton.displayName = "SelectScrollDownButton";
|
|
328
|
+
|
|
329
|
+
exports.Select = Select;
|
|
330
|
+
exports.SelectContent = SelectContent;
|
|
331
|
+
exports.SelectGroup = SelectGroup;
|
|
332
|
+
exports.SelectItem = SelectItem;
|
|
333
|
+
exports.SelectLabel = SelectLabel;
|
|
334
|
+
exports.SelectScrollDownButton = SelectScrollDownButton;
|
|
335
|
+
exports.SelectScrollUpButton = SelectScrollUpButton;
|
|
336
|
+
exports.SelectSeparator = SelectSeparator;
|
|
337
|
+
exports.SelectTrigger = SelectTrigger;
|
|
338
|
+
exports.SelectValue = SelectValue;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
2
|
+
import { createContext, useState, useId, useRef, useCallback, use, useEffect, Children, isValidElement } from 'react';
|
|
3
|
+
import { createPortal } from 'react-dom';
|
|
4
|
+
import concatClassNames from '@ladder-ui/core/concatClassNames';
|
|
5
|
+
|
|
6
|
+
// ─── Inline SVG icons ────────────────────────────────────────────────────────
|
|
7
|
+
function ChevronDownIcon({ className }) {
|
|
8
|
+
return (jsx("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", className: className, children: jsx("path", { d: "m6 9 6 6 6-6" }) }));
|
|
9
|
+
}
|
|
10
|
+
function ChevronUpIcon({ className }) {
|
|
11
|
+
return (jsx("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", className: className, children: jsx("path", { d: "m18 15-6-6-6 6" }) }));
|
|
12
|
+
}
|
|
13
|
+
function CheckIcon({ className }) {
|
|
14
|
+
return (jsx("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 2.5, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", className: className, children: jsx("path", { d: "M20 6 9 17l-5-5" }) }));
|
|
15
|
+
}
|
|
16
|
+
// ─── Label extraction helpers ────────────────────────────────────────────────
|
|
17
|
+
//
|
|
18
|
+
// We scan the JSX children tree in Select root to build a value→label map.
|
|
19
|
+
// This works even when SelectContent renders null (closed state), because the
|
|
20
|
+
// JSX element tree passed as `children` always contains the full structure.
|
|
21
|
+
function extractTextContent(children) {
|
|
22
|
+
if (typeof children === "string")
|
|
23
|
+
return children;
|
|
24
|
+
if (typeof children === "number")
|
|
25
|
+
return String(children);
|
|
26
|
+
if (Array.isArray(children))
|
|
27
|
+
return children.map(extractTextContent).join("");
|
|
28
|
+
if (isValidElement(children)) {
|
|
29
|
+
return extractTextContent(children.props.children);
|
|
30
|
+
}
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
33
|
+
function scanItemLabels(children, registry) {
|
|
34
|
+
Children.forEach(children, (child) => {
|
|
35
|
+
if (!isValidElement(child))
|
|
36
|
+
return;
|
|
37
|
+
const props = child.props;
|
|
38
|
+
// Detect SelectItem by displayName on the element type function
|
|
39
|
+
const dn = typeof child.type === "function"
|
|
40
|
+
? child.type.displayName
|
|
41
|
+
: typeof child.type === "object"
|
|
42
|
+
? child.type.displayName
|
|
43
|
+
: undefined;
|
|
44
|
+
if (dn === "SelectItem" && (typeof props.value === "string" || props.value === null)) {
|
|
45
|
+
// null is the Shadcn-style "no selection" sentinel — stored as "" internally
|
|
46
|
+
const key = props.value ?? "";
|
|
47
|
+
const label = extractTextContent(props.children);
|
|
48
|
+
if (label)
|
|
49
|
+
registry.set(key, label);
|
|
50
|
+
}
|
|
51
|
+
// Recurse into children of non-item elements
|
|
52
|
+
if (props.children) {
|
|
53
|
+
scanItemLabels(props.children, registry);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const SelectContext = createContext({
|
|
58
|
+
open: false,
|
|
59
|
+
setOpen: () => { },
|
|
60
|
+
value: undefined,
|
|
61
|
+
displayLabel: "",
|
|
62
|
+
triggerId: "",
|
|
63
|
+
contentId: "",
|
|
64
|
+
triggerRef: { current: null },
|
|
65
|
+
});
|
|
66
|
+
function Select({ value: controlledValue, defaultValue, onValueChange, disabled, children, open: controlledOpen, onOpenChange, items, }) {
|
|
67
|
+
const [internalOpen, setInternalOpen] = useState(false);
|
|
68
|
+
const [internalValue, setInternalValue] = useState(defaultValue);
|
|
69
|
+
const isOpenControlled = controlledOpen !== undefined;
|
|
70
|
+
const isValueControlled = controlledValue !== undefined;
|
|
71
|
+
const open = isOpenControlled ? controlledOpen : internalOpen;
|
|
72
|
+
const value = isValueControlled ? controlledValue : internalValue;
|
|
73
|
+
const triggerId = useId();
|
|
74
|
+
const contentId = useId();
|
|
75
|
+
const triggerRef = useRef(null);
|
|
76
|
+
// Build label registry synchronously from the JSX children tree.
|
|
77
|
+
// This works even when SelectContent renders null (closed), because the
|
|
78
|
+
// JSX element tree always contains all SelectItem elements.
|
|
79
|
+
const labelsMap = new Map();
|
|
80
|
+
scanItemLabels(children, labelsMap);
|
|
81
|
+
// Merge items prop — supports null values and data-driven usage (.map()).
|
|
82
|
+
// Takes precedence over child-scanned labels for matching keys.
|
|
83
|
+
if (items) {
|
|
84
|
+
for (const item of items) {
|
|
85
|
+
labelsMap.set(item.value ?? "", item.label);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const displayLabel = (value !== undefined ? labelsMap.get(value) : undefined) ?? "";
|
|
89
|
+
const setOpen = useCallback((next) => {
|
|
90
|
+
if (!isOpenControlled)
|
|
91
|
+
setInternalOpen(next);
|
|
92
|
+
onOpenChange?.(next);
|
|
93
|
+
}, [isOpenControlled, onOpenChange]);
|
|
94
|
+
const handleValueChange = useCallback((newValue) => {
|
|
95
|
+
if (!isValueControlled)
|
|
96
|
+
setInternalValue(newValue);
|
|
97
|
+
onValueChange?.(newValue);
|
|
98
|
+
setOpen(false);
|
|
99
|
+
}, [isValueControlled, onValueChange, setOpen]);
|
|
100
|
+
return (jsx(SelectContext, { value: {
|
|
101
|
+
open,
|
|
102
|
+
setOpen,
|
|
103
|
+
value,
|
|
104
|
+
displayLabel,
|
|
105
|
+
onValueChange: handleValueChange,
|
|
106
|
+
disabled,
|
|
107
|
+
triggerId,
|
|
108
|
+
contentId,
|
|
109
|
+
triggerRef,
|
|
110
|
+
}, children: children }));
|
|
111
|
+
}
|
|
112
|
+
Select.displayName = "Select";
|
|
113
|
+
function SelectTrigger({ ref, className, children, size = "default", fullWidth, disabled: propDisabled, onKeyDown, ...props }) {
|
|
114
|
+
const ctx = use(SelectContext);
|
|
115
|
+
const isDisabled = propDisabled || ctx.disabled;
|
|
116
|
+
// Merge consumer ref + context triggerRef
|
|
117
|
+
const setRef = (el) => {
|
|
118
|
+
ctx.triggerRef.current = el;
|
|
119
|
+
if (typeof ref === "function") {
|
|
120
|
+
ref(el);
|
|
121
|
+
}
|
|
122
|
+
else if (ref) {
|
|
123
|
+
ref.current = el;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
const handleKeyDown = (e) => {
|
|
127
|
+
if (["ArrowDown", "ArrowUp", "Enter", " "].includes(e.key)) {
|
|
128
|
+
e.preventDefault();
|
|
129
|
+
ctx.setOpen(true);
|
|
130
|
+
}
|
|
131
|
+
onKeyDown?.(e);
|
|
132
|
+
};
|
|
133
|
+
const classes = concatClassNames("lui-select-trigger", size === "sm" ? "lui-select-trigger--sm" : undefined, fullWidth ? "lui-select-trigger--full-width" : undefined, className);
|
|
134
|
+
return (jsxs("button", { ref: setRef, type: "button", role: "combobox", id: ctx.triggerId, "aria-expanded": ctx.open, "aria-haspopup": "listbox", "aria-controls": ctx.open ? ctx.contentId : undefined, "aria-disabled": isDisabled || undefined, disabled: isDisabled, "data-slot": "select-trigger", "data-size": size, "data-full-width": fullWidth || undefined, className: classes, onClick: () => ctx.setOpen(!ctx.open), onKeyDown: handleKeyDown, ...props, children: [children, jsx(ChevronDownIcon, { className: "lui-select-trigger__chevron" })] }));
|
|
135
|
+
}
|
|
136
|
+
SelectTrigger.displayName = "SelectTrigger";
|
|
137
|
+
function SelectValue({ placeholder, className }) {
|
|
138
|
+
const ctx = use(SelectContext);
|
|
139
|
+
const classes = concatClassNames("lui-select-value", className);
|
|
140
|
+
return (jsx("span", { "data-slot": "select-value", className: classes, children: ctx.displayLabel || placeholder || "" }));
|
|
141
|
+
}
|
|
142
|
+
SelectValue.displayName = "SelectValue";
|
|
143
|
+
function SelectContent({ ref, className, children, position = "item-aligned", }) {
|
|
144
|
+
const ctx = use(SelectContext);
|
|
145
|
+
const contentRef = useRef(null);
|
|
146
|
+
// Two-pass positioning:
|
|
147
|
+
// Pass 1 — render with visibility:hidden so browser can measure item offsets.
|
|
148
|
+
// Pass 2 — compute correct position, set isPositioned=true → visibility:visible.
|
|
149
|
+
const [isPositioned, setIsPositioned] = useState(false);
|
|
150
|
+
const [coords, setCoords] = useState({ top: 0, left: 0, width: 0 });
|
|
151
|
+
// Pass 1→2: measure and position after first render
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
if (!ctx.open || !contentRef.current || !ctx.triggerRef.current)
|
|
154
|
+
return;
|
|
155
|
+
const triggerRect = ctx.triggerRef.current.getBoundingClientRect();
|
|
156
|
+
const content = contentRef.current;
|
|
157
|
+
const contentHeight = content.offsetHeight;
|
|
158
|
+
const contentWidth = content.offsetWidth;
|
|
159
|
+
const viewportHeight = window.innerHeight;
|
|
160
|
+
const viewportWidth = window.innerWidth;
|
|
161
|
+
let top;
|
|
162
|
+
if (position === "item-aligned") {
|
|
163
|
+
// Align the selected item (or first item) vertically with the trigger.
|
|
164
|
+
// The item's center should coincide with the trigger's center.
|
|
165
|
+
const selectedItem = content.querySelector('[role="option"][aria-selected="true"]');
|
|
166
|
+
const firstItem = content.querySelector('[role="option"]');
|
|
167
|
+
const targetItem = selectedItem ?? firstItem;
|
|
168
|
+
if (targetItem) {
|
|
169
|
+
// offsetTop is relative to the fixed content div (its offsetParent)
|
|
170
|
+
const itemTop = targetItem.offsetTop;
|
|
171
|
+
const itemHeight = targetItem.offsetHeight;
|
|
172
|
+
// Center item with trigger: move content up so item.center = trigger.center
|
|
173
|
+
top =
|
|
174
|
+
triggerRect.top -
|
|
175
|
+
itemTop +
|
|
176
|
+
(triggerRect.height - itemHeight) / 2;
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
top = triggerRect.top;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
// Popper: open below trigger, fall back to above if insufficient space
|
|
184
|
+
const spaceBelow = viewportHeight - triggerRect.bottom;
|
|
185
|
+
if (spaceBelow < contentHeight + 8 && triggerRect.top > contentHeight + 8) {
|
|
186
|
+
top = triggerRect.top - contentHeight - 4;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
top = triggerRect.bottom + 4;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// Clamp to viewport with 8 px margin
|
|
193
|
+
top = Math.max(8, Math.min(top, viewportHeight - contentHeight - 8));
|
|
194
|
+
const left = Math.max(8, Math.min(triggerRect.left, viewportWidth - contentWidth - 8));
|
|
195
|
+
setCoords({ top, left, width: triggerRect.width });
|
|
196
|
+
setIsPositioned(true);
|
|
197
|
+
// Run after every open (component remounts each time, so state resets automatically)
|
|
198
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
199
|
+
}, [ctx.open]);
|
|
200
|
+
// Focus management — wait until content is visible to avoid jarring jump
|
|
201
|
+
useEffect(() => {
|
|
202
|
+
if (!ctx.open || !isPositioned || !contentRef.current)
|
|
203
|
+
return;
|
|
204
|
+
const selected = contentRef.current.querySelector('[role="option"][aria-selected="true"]');
|
|
205
|
+
const first = contentRef.current.querySelector('[role="option"]:not([aria-disabled="true"])');
|
|
206
|
+
(selected ?? first)?.focus();
|
|
207
|
+
}, [ctx.open, isPositioned]);
|
|
208
|
+
// Click-outside to close
|
|
209
|
+
useEffect(() => {
|
|
210
|
+
if (!ctx.open)
|
|
211
|
+
return;
|
|
212
|
+
const handlePointerDown = (e) => {
|
|
213
|
+
const target = e.target;
|
|
214
|
+
if (contentRef.current &&
|
|
215
|
+
!contentRef.current.contains(target) &&
|
|
216
|
+
ctx.triggerRef.current &&
|
|
217
|
+
!ctx.triggerRef.current.contains(target)) {
|
|
218
|
+
ctx.setOpen(false);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
document.addEventListener("pointerdown", handlePointerDown);
|
|
222
|
+
return () => document.removeEventListener("pointerdown", handlePointerDown);
|
|
223
|
+
}, [ctx.open, ctx.setOpen, ctx.triggerRef]);
|
|
224
|
+
// Keyboard navigation within the listbox
|
|
225
|
+
const handleKeyDown = (e) => {
|
|
226
|
+
const items = contentRef.current?.querySelectorAll('[role="option"]:not([aria-disabled="true"])');
|
|
227
|
+
if (!items || items.length === 0)
|
|
228
|
+
return;
|
|
229
|
+
const active = document.activeElement;
|
|
230
|
+
const currentIndex = Array.from(items).indexOf(active);
|
|
231
|
+
switch (e.key) {
|
|
232
|
+
case "ArrowDown": {
|
|
233
|
+
e.preventDefault();
|
|
234
|
+
items[Math.min(currentIndex + 1, items.length - 1)]?.focus();
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
case "ArrowUp": {
|
|
238
|
+
e.preventDefault();
|
|
239
|
+
items[Math.max(currentIndex - 1, 0)]?.focus();
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case "Home": {
|
|
243
|
+
e.preventDefault();
|
|
244
|
+
items[0]?.focus();
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
case "End": {
|
|
248
|
+
e.preventDefault();
|
|
249
|
+
items[items.length - 1]?.focus();
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
case "Escape":
|
|
253
|
+
case "Tab": {
|
|
254
|
+
ctx.setOpen(false);
|
|
255
|
+
ctx.triggerRef.current?.focus();
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
if (!ctx.open)
|
|
261
|
+
return null;
|
|
262
|
+
const style = {
|
|
263
|
+
position: "fixed",
|
|
264
|
+
top: coords.top,
|
|
265
|
+
left: coords.left,
|
|
266
|
+
width: coords.width || undefined,
|
|
267
|
+
minWidth: coords.width || undefined,
|
|
268
|
+
zIndex: 9999,
|
|
269
|
+
// Hidden during pass 1 to allow measurement without visual flash
|
|
270
|
+
visibility: isPositioned ? "visible" : "hidden",
|
|
271
|
+
};
|
|
272
|
+
const setRef = (el) => {
|
|
273
|
+
contentRef.current = el;
|
|
274
|
+
if (typeof ref === "function") {
|
|
275
|
+
ref(el);
|
|
276
|
+
}
|
|
277
|
+
else if (ref) {
|
|
278
|
+
ref.current = el;
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
const classes = concatClassNames("lui-select-content", className);
|
|
282
|
+
return createPortal(jsx("div", { ref: setRef, id: ctx.contentId, role: "listbox", "aria-labelledby": ctx.triggerId, "data-slot": "select-content", "data-state": "open", className: classes, style: style, onKeyDown: handleKeyDown, children: jsx("div", { className: "lui-select-content__viewport", children: children }) }), document.body);
|
|
283
|
+
}
|
|
284
|
+
SelectContent.displayName = "SelectContent";
|
|
285
|
+
function SelectGroup({ ref, className, children, ...props }) {
|
|
286
|
+
return (jsx("div", { ref: ref, role: "group", "data-slot": "select-group", className: concatClassNames("lui-select-group", className), ...props, children: children }));
|
|
287
|
+
}
|
|
288
|
+
SelectGroup.displayName = "SelectGroup";
|
|
289
|
+
function SelectLabel({ ref, className, children, ...props }) {
|
|
290
|
+
return (jsx("div", { ref: ref, "data-slot": "select-label", className: concatClassNames("lui-select-label", className), ...props, children: children }));
|
|
291
|
+
}
|
|
292
|
+
SelectLabel.displayName = "SelectLabel";
|
|
293
|
+
function SelectItem({ ref, className, children, value, disabled, onClick, onKeyDown, ...props }) {
|
|
294
|
+
const ctx = use(SelectContext);
|
|
295
|
+
// null is the Shadcn-style sentinel for "no selection" — normalize to ""
|
|
296
|
+
const normalizedValue = value ?? "";
|
|
297
|
+
const isSelected = ctx.value === normalizedValue;
|
|
298
|
+
const handleSelect = () => {
|
|
299
|
+
if (disabled)
|
|
300
|
+
return;
|
|
301
|
+
ctx.onValueChange?.(normalizedValue);
|
|
302
|
+
};
|
|
303
|
+
const handleKeyDown = (e) => {
|
|
304
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
305
|
+
e.preventDefault();
|
|
306
|
+
handleSelect();
|
|
307
|
+
}
|
|
308
|
+
onKeyDown?.(e);
|
|
309
|
+
};
|
|
310
|
+
const classes = concatClassNames("lui-select-item", isSelected ? "lui-select-item--selected" : undefined, disabled ? "lui-select-item--disabled" : undefined, className);
|
|
311
|
+
return (jsxs("div", { ref: ref, role: "option", "aria-selected": isSelected, "aria-disabled": disabled || undefined, tabIndex: disabled ? -1 : 0, "data-slot": "select-item", "data-value": normalizedValue, className: classes, onClick: handleSelect, onKeyDown: handleKeyDown, ...props, children: [jsx("span", { className: "lui-select-item__indicator", "aria-hidden": "true", children: isSelected && jsx(CheckIcon, { className: "lui-select-item__check" }) }), jsx("span", { className: "lui-select-item__text", children: children })] }));
|
|
312
|
+
}
|
|
313
|
+
SelectItem.displayName = "SelectItem";
|
|
314
|
+
function SelectSeparator({ ref, className, ...props }) {
|
|
315
|
+
return (jsx("hr", { ref: ref, "data-slot": "select-separator", className: concatClassNames("lui-select-separator", className), ...props }));
|
|
316
|
+
}
|
|
317
|
+
SelectSeparator.displayName = "SelectSeparator";
|
|
318
|
+
function SelectScrollUpButton({ ref, className, ...props }) {
|
|
319
|
+
return (jsx("div", { ref: ref, "data-slot": "select-scroll-up-button", className: concatClassNames("lui-select-scroll-button", className), ...props, children: jsx(ChevronUpIcon, { className: "lui-select-scroll-button__icon" }) }));
|
|
320
|
+
}
|
|
321
|
+
SelectScrollUpButton.displayName = "SelectScrollUpButton";
|
|
322
|
+
function SelectScrollDownButton({ ref, className, ...props }) {
|
|
323
|
+
return (jsx("div", { ref: ref, "data-slot": "select-scroll-down-button", className: concatClassNames("lui-select-scroll-button", className), ...props, children: jsx(ChevronDownIcon, { className: "lui-select-scroll-button__icon" }) }));
|
|
324
|
+
}
|
|
325
|
+
SelectScrollDownButton.displayName = "SelectScrollDownButton";
|
|
326
|
+
|
|
327
|
+
export { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectScrollDownButton, SelectScrollUpButton, SelectSeparator, SelectTrigger, SelectValue };
|
package/dist/select.css
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
@charset "UTF-8";
|
|
2
|
+
@layer components {
|
|
3
|
+
.lui-select-trigger {
|
|
4
|
+
position: relative;
|
|
5
|
+
display: inline-flex;
|
|
6
|
+
align-items: center;
|
|
7
|
+
justify-content: space-between;
|
|
8
|
+
gap: 0.5rem;
|
|
9
|
+
width: fit-content;
|
|
10
|
+
height: var(--lui-select-trigger-height);
|
|
11
|
+
padding: var(--lui-select-trigger-py) var(--lui-select-trigger-px);
|
|
12
|
+
background-color: var(--lui-select-trigger-bg);
|
|
13
|
+
color: var(--lui-select-trigger-text);
|
|
14
|
+
border: 1px solid var(--lui-select-trigger-border);
|
|
15
|
+
border-radius: var(--lui-select-trigger-radius);
|
|
16
|
+
font-family: var(--lui-font-family);
|
|
17
|
+
font-size: 0.875rem;
|
|
18
|
+
line-height: 1.25;
|
|
19
|
+
white-space: nowrap;
|
|
20
|
+
text-align: left;
|
|
21
|
+
cursor: pointer;
|
|
22
|
+
transition: var(--lui-transition);
|
|
23
|
+
vertical-align: middle;
|
|
24
|
+
outline: none;
|
|
25
|
+
user-select: none;
|
|
26
|
+
}
|
|
27
|
+
.lui-select-trigger:focus-visible {
|
|
28
|
+
border-color: var(--lui-select-focus-border);
|
|
29
|
+
box-shadow: 0 0 0 2px var(--lui-surface), 0 0 0 4px var(--lui-select-focus-ring-color);
|
|
30
|
+
}
|
|
31
|
+
.lui-select-trigger:disabled, .lui-select-trigger[aria-disabled=true] {
|
|
32
|
+
opacity: var(--lui-disabled-opacity);
|
|
33
|
+
cursor: var(--lui-disabled-cursor);
|
|
34
|
+
pointer-events: none;
|
|
35
|
+
}
|
|
36
|
+
.lui-select-trigger--sm {
|
|
37
|
+
height: var(--lui-select-trigger-height-sm);
|
|
38
|
+
font-size: 0.8125rem;
|
|
39
|
+
}
|
|
40
|
+
.lui-select-trigger--full-width {
|
|
41
|
+
width: 100%;
|
|
42
|
+
}
|
|
43
|
+
.lui-select-trigger__chevron {
|
|
44
|
+
width: 1rem;
|
|
45
|
+
height: 1rem;
|
|
46
|
+
opacity: 0.5;
|
|
47
|
+
flex-shrink: 0;
|
|
48
|
+
pointer-events: none;
|
|
49
|
+
}
|
|
50
|
+
.lui-select-value {
|
|
51
|
+
overflow: hidden;
|
|
52
|
+
text-overflow: ellipsis;
|
|
53
|
+
white-space: nowrap;
|
|
54
|
+
flex: 1;
|
|
55
|
+
min-width: 0;
|
|
56
|
+
}
|
|
57
|
+
.lui-select-value:empty::before {
|
|
58
|
+
content: "";
|
|
59
|
+
}
|
|
60
|
+
.lui-select-content {
|
|
61
|
+
background-color: var(--lui-select-content-bg);
|
|
62
|
+
border: 1px solid var(--lui-select-content-border);
|
|
63
|
+
border-radius: var(--lui-select-content-radius);
|
|
64
|
+
box-shadow: var(--lui-select-content-shadow);
|
|
65
|
+
overflow: hidden;
|
|
66
|
+
animation: lui-select-fade-in 0.12s ease-out;
|
|
67
|
+
}
|
|
68
|
+
.lui-select-content__viewport {
|
|
69
|
+
overflow-y: auto;
|
|
70
|
+
max-height: var(--lui-select-content-max-height);
|
|
71
|
+
overscroll-behavior: contain;
|
|
72
|
+
padding: 0.25rem;
|
|
73
|
+
scroll-padding-block: 0.25rem;
|
|
74
|
+
}
|
|
75
|
+
@keyframes lui-select-fade-in {
|
|
76
|
+
from {
|
|
77
|
+
opacity: 0;
|
|
78
|
+
transform: scale(0.97);
|
|
79
|
+
}
|
|
80
|
+
to {
|
|
81
|
+
opacity: 1;
|
|
82
|
+
transform: scale(1);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
.lui-select-label {
|
|
86
|
+
padding: 0.375rem 0.5rem 0.25rem;
|
|
87
|
+
font-size: 0.75rem;
|
|
88
|
+
font-weight: 500;
|
|
89
|
+
color: var(--lui-select-label-text);
|
|
90
|
+
user-select: none;
|
|
91
|
+
font-family: var(--lui-font-family);
|
|
92
|
+
}
|
|
93
|
+
.lui-select-item {
|
|
94
|
+
position: relative;
|
|
95
|
+
display: flex;
|
|
96
|
+
align-items: center;
|
|
97
|
+
gap: 0.375rem;
|
|
98
|
+
padding: var(--lui-select-item-py) var(--lui-select-item-px);
|
|
99
|
+
padding-right: calc(var(--lui-select-item-px) + var(--lui-select-item-indicator-size) + 0.375rem);
|
|
100
|
+
border-radius: var(--lui-select-item-radius);
|
|
101
|
+
font-size: 0.875rem;
|
|
102
|
+
color: var(--lui-select-item-text);
|
|
103
|
+
cursor: pointer;
|
|
104
|
+
user-select: none;
|
|
105
|
+
outline: none;
|
|
106
|
+
transition: background-color 0.1s ease, color 0.1s ease;
|
|
107
|
+
font-family: var(--lui-font-family);
|
|
108
|
+
}
|
|
109
|
+
.lui-select-item:hover:not(.lui-select-item--disabled) {
|
|
110
|
+
background-color: color-mix(in srgb, var(--lui-select-item-hover-bg) 50%, transparent);
|
|
111
|
+
color: var(--lui-select-item-hover-text);
|
|
112
|
+
}
|
|
113
|
+
.lui-select-item:focus-visible:not(.lui-select-item--disabled) {
|
|
114
|
+
background-color: var(--lui-select-item-focus-bg);
|
|
115
|
+
color: var(--lui-select-item-hover-text);
|
|
116
|
+
}
|
|
117
|
+
.lui-select-item--disabled {
|
|
118
|
+
opacity: var(--lui-disabled-opacity);
|
|
119
|
+
cursor: not-allowed;
|
|
120
|
+
pointer-events: none;
|
|
121
|
+
}
|
|
122
|
+
.lui-select-item__indicator {
|
|
123
|
+
position: absolute;
|
|
124
|
+
right: var(--lui-select-item-px);
|
|
125
|
+
display: flex;
|
|
126
|
+
align-items: center;
|
|
127
|
+
justify-content: center;
|
|
128
|
+
width: var(--lui-select-item-indicator-size);
|
|
129
|
+
height: var(--lui-select-item-indicator-size);
|
|
130
|
+
flex-shrink: 0;
|
|
131
|
+
}
|
|
132
|
+
.lui-select-item__check {
|
|
133
|
+
width: 0.875rem;
|
|
134
|
+
height: 0.875rem;
|
|
135
|
+
color: var(--lui-select-item-selected-text);
|
|
136
|
+
}
|
|
137
|
+
.lui-select-item__text {
|
|
138
|
+
overflow: hidden;
|
|
139
|
+
text-overflow: ellipsis;
|
|
140
|
+
white-space: nowrap;
|
|
141
|
+
flex: 1;
|
|
142
|
+
}
|
|
143
|
+
.lui-select-separator {
|
|
144
|
+
border: none;
|
|
145
|
+
border-top: 1px solid var(--lui-select-separator-color);
|
|
146
|
+
margin: 0.25rem -0.25rem;
|
|
147
|
+
}
|
|
148
|
+
.lui-select-scroll-button {
|
|
149
|
+
display: flex;
|
|
150
|
+
align-items: center;
|
|
151
|
+
justify-content: center;
|
|
152
|
+
padding: 0.25rem;
|
|
153
|
+
cursor: default;
|
|
154
|
+
color: var(--lui-select-label-text);
|
|
155
|
+
}
|
|
156
|
+
.lui-select-scroll-button__icon {
|
|
157
|
+
width: 1rem;
|
|
158
|
+
height: 1rem;
|
|
159
|
+
}
|
|
160
|
+
}
|
package/dist/select.d.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { Ref } from "react";
|
|
2
|
+
/** A single item entry for the data-driven `items` prop on `Select`. */
|
|
3
|
+
export interface SelectItemData {
|
|
4
|
+
label: string;
|
|
5
|
+
/** Pass `null` for the "no selection" reset item (equivalent to `value=""`). */
|
|
6
|
+
value: string | null;
|
|
7
|
+
}
|
|
8
|
+
export interface SelectProps {
|
|
9
|
+
/** Controlled selected value. Pair with `onValueChange`. */
|
|
10
|
+
value?: string;
|
|
11
|
+
/** Uncontrolled initial selected value. */
|
|
12
|
+
defaultValue?: string;
|
|
13
|
+
/** Called when the user selects a different item. */
|
|
14
|
+
onValueChange?: (value: string) => void;
|
|
15
|
+
/** Disables the entire select control. */
|
|
16
|
+
disabled?: boolean;
|
|
17
|
+
children?: React.ReactNode;
|
|
18
|
+
/** Controlled open state. Pair with `onOpenChange`. */
|
|
19
|
+
open?: boolean;
|
|
20
|
+
/** Called when open state changes. */
|
|
21
|
+
onOpenChange?: (open: boolean) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Optional flat list of items for label extraction.
|
|
24
|
+
* Useful when items are rendered via `.map()` and the label map
|
|
25
|
+
* cannot be built from static JSX children alone.
|
|
26
|
+
* Supports `value: null` for the "no selection" reset item.
|
|
27
|
+
*/
|
|
28
|
+
items?: SelectItemData[];
|
|
29
|
+
}
|
|
30
|
+
export declare function Select({ value: controlledValue, defaultValue, onValueChange, disabled, children, open: controlledOpen, onOpenChange, items, }: SelectProps): import("react/jsx-runtime").JSX.Element;
|
|
31
|
+
export declare namespace Select {
|
|
32
|
+
var displayName: string;
|
|
33
|
+
}
|
|
34
|
+
export interface SelectTriggerProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
35
|
+
ref?: Ref<HTMLButtonElement>;
|
|
36
|
+
/** Size variant of the trigger button. */
|
|
37
|
+
size?: "sm" | "default";
|
|
38
|
+
/** When true, the trigger expands to fill its container width. */
|
|
39
|
+
fullWidth?: boolean;
|
|
40
|
+
}
|
|
41
|
+
export declare function SelectTrigger({ ref, className, children, size, fullWidth, disabled: propDisabled, onKeyDown, ...props }: SelectTriggerProps): import("react/jsx-runtime").JSX.Element;
|
|
42
|
+
export declare namespace SelectTrigger {
|
|
43
|
+
var displayName: string;
|
|
44
|
+
}
|
|
45
|
+
export interface SelectValueProps {
|
|
46
|
+
/** Text shown when no value is selected. */
|
|
47
|
+
placeholder?: string;
|
|
48
|
+
className?: string;
|
|
49
|
+
}
|
|
50
|
+
export declare function SelectValue({ placeholder, className }: SelectValueProps): import("react/jsx-runtime").JSX.Element;
|
|
51
|
+
export declare namespace SelectValue {
|
|
52
|
+
var displayName: string;
|
|
53
|
+
}
|
|
54
|
+
export interface SelectContentProps {
|
|
55
|
+
ref?: Ref<HTMLDivElement>;
|
|
56
|
+
className?: string;
|
|
57
|
+
children?: React.ReactNode;
|
|
58
|
+
/**
|
|
59
|
+
* Positioning strategy.
|
|
60
|
+
* - `"item-aligned"` (default): the dropdown opens so the selected item
|
|
61
|
+
* is visually aligned with the trigger — matching Shadcn/Radix behaviour.
|
|
62
|
+
* - `"popper"`: the dropdown opens below (or above) the trigger.
|
|
63
|
+
*/
|
|
64
|
+
position?: "item-aligned" | "popper";
|
|
65
|
+
}
|
|
66
|
+
export declare function SelectContent({ ref, className, children, position, }: SelectContentProps): import("react").ReactPortal | null;
|
|
67
|
+
export declare namespace SelectContent {
|
|
68
|
+
var displayName: string;
|
|
69
|
+
}
|
|
70
|
+
export interface SelectGroupProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
71
|
+
ref?: Ref<HTMLDivElement>;
|
|
72
|
+
}
|
|
73
|
+
export declare function SelectGroup({ ref, className, children, ...props }: SelectGroupProps): import("react/jsx-runtime").JSX.Element;
|
|
74
|
+
export declare namespace SelectGroup {
|
|
75
|
+
var displayName: string;
|
|
76
|
+
}
|
|
77
|
+
export interface SelectLabelProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
78
|
+
ref?: Ref<HTMLDivElement>;
|
|
79
|
+
}
|
|
80
|
+
export declare function SelectLabel({ ref, className, children, ...props }: SelectLabelProps): import("react/jsx-runtime").JSX.Element;
|
|
81
|
+
export declare namespace SelectLabel {
|
|
82
|
+
var displayName: string;
|
|
83
|
+
}
|
|
84
|
+
export interface SelectItemProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
85
|
+
ref?: Ref<HTMLDivElement>;
|
|
86
|
+
/**
|
|
87
|
+
* The value this item represents when selected. Required.
|
|
88
|
+
* Pass `null` for the "no selection" reset item (Shadcn-style).
|
|
89
|
+
* Internally treated as `""` — `onValueChange` is called with `""`.
|
|
90
|
+
*/
|
|
91
|
+
value: string | null;
|
|
92
|
+
/** Prevents selection of this item. */
|
|
93
|
+
disabled?: boolean;
|
|
94
|
+
}
|
|
95
|
+
export declare function SelectItem({ ref, className, children, value, disabled, onClick, onKeyDown, ...props }: SelectItemProps): import("react/jsx-runtime").JSX.Element;
|
|
96
|
+
export declare namespace SelectItem {
|
|
97
|
+
var displayName: string;
|
|
98
|
+
}
|
|
99
|
+
export interface SelectSeparatorProps extends React.HTMLAttributes<HTMLHRElement> {
|
|
100
|
+
ref?: Ref<HTMLHRElement>;
|
|
101
|
+
}
|
|
102
|
+
export declare function SelectSeparator({ ref, className, ...props }: SelectSeparatorProps): import("react/jsx-runtime").JSX.Element;
|
|
103
|
+
export declare namespace SelectSeparator {
|
|
104
|
+
var displayName: string;
|
|
105
|
+
}
|
|
106
|
+
export interface SelectScrollUpButtonProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
107
|
+
ref?: Ref<HTMLDivElement>;
|
|
108
|
+
}
|
|
109
|
+
export declare function SelectScrollUpButton({ ref, className, ...props }: SelectScrollUpButtonProps): import("react/jsx-runtime").JSX.Element;
|
|
110
|
+
export declare namespace SelectScrollUpButton {
|
|
111
|
+
var displayName: string;
|
|
112
|
+
}
|
|
113
|
+
export interface SelectScrollDownButtonProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
114
|
+
ref?: Ref<HTMLDivElement>;
|
|
115
|
+
}
|
|
116
|
+
export declare function SelectScrollDownButton({ ref, className, ...props }: SelectScrollDownButtonProps): import("react/jsx-runtime").JSX.Element;
|
|
117
|
+
export declare namespace SelectScrollDownButton {
|
|
118
|
+
var displayName: string;
|
|
119
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
--lui-select-trigger-bg: var(--lui-surface);
|
|
3
|
+
--lui-select-trigger-text: var(--lui-surface-text);
|
|
4
|
+
--lui-select-trigger-border: var(--lui-surface-border);
|
|
5
|
+
--lui-select-trigger-radius: var(--lui-radius-sm);
|
|
6
|
+
--lui-select-trigger-height: 2.25rem;
|
|
7
|
+
--lui-select-trigger-height-sm: 2rem;
|
|
8
|
+
--lui-select-trigger-px: 0.75rem;
|
|
9
|
+
--lui-select-trigger-py: 0.5rem;
|
|
10
|
+
--lui-select-focus-border: var(--lui-primary);
|
|
11
|
+
--lui-select-focus-ring-color: color-mix(in srgb, var(--lui-primary) 40%, transparent);
|
|
12
|
+
--lui-select-content-bg: var(--lui-surface);
|
|
13
|
+
--lui-select-content-border: var(--lui-surface-border);
|
|
14
|
+
--lui-select-content-radius: var(--lui-radius-sm);
|
|
15
|
+
--lui-select-content-shadow: 0 4px 16px rgba(0, 0, 0, 0.12), 0 1px 4px rgba(0, 0, 0, 0.08);
|
|
16
|
+
--lui-select-content-max-height: 20rem;
|
|
17
|
+
--lui-select-item-text: var(--lui-surface-text);
|
|
18
|
+
--lui-select-item-hover-bg: color-mix(in srgb, var(--lui-primary) 10%, transparent);
|
|
19
|
+
--lui-select-item-hover-text: var(--lui-surface-text);
|
|
20
|
+
--lui-select-item-selected-text: var(--lui-primary);
|
|
21
|
+
--lui-select-item-focus-bg: color-mix(in srgb, var(--lui-primary) 10%, transparent);
|
|
22
|
+
--lui-select-item-radius: var(--lui-radius-sm);
|
|
23
|
+
--lui-select-item-px: 0.5rem;
|
|
24
|
+
--lui-select-item-py: 0.375rem;
|
|
25
|
+
--lui-select-item-indicator-size: 1rem;
|
|
26
|
+
--lui-select-label-text: color-mix(in srgb, var(--lui-surface-text) 60%, transparent);
|
|
27
|
+
--lui-select-separator-color: var(--lui-surface-border);
|
|
28
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ladder-ui/select",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": "./dist/index.js",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"./*.css": "./dist/*.css",
|
|
15
|
+
"./styles/*.css": "./dist/*.css"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"keywords": [
|
|
21
|
+
"nodejs",
|
|
22
|
+
"react",
|
|
23
|
+
"ui",
|
|
24
|
+
"components",
|
|
25
|
+
"library",
|
|
26
|
+
"select",
|
|
27
|
+
"dropdown",
|
|
28
|
+
"form"
|
|
29
|
+
],
|
|
30
|
+
"author": "Ivan Avila <ivelaval@gmail.com> - https://www.vennet.dev",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+ssh://git@github.com/ivelaval/ladder-ui.git"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/ivelaval/ladder-ui/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/ivelaval/ladder-ui#readme",
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
45
|
+
"@types/react": "^19.0.0",
|
|
46
|
+
"@types/react-dom": "^19.0.0",
|
|
47
|
+
"rollup": "^4.59.0",
|
|
48
|
+
"rollup-plugin-postcss": "^4.0.2",
|
|
49
|
+
"sass": "^1.90.0",
|
|
50
|
+
"tslib": "^2.6.2",
|
|
51
|
+
"typescript": "^5.3.3",
|
|
52
|
+
"@ladder-ui/core": "0.2.0"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@ladder-ui/core": ">=0.0.0",
|
|
56
|
+
"react": ">=18.0.0",
|
|
57
|
+
"react-dom": ">=18.0.0"
|
|
58
|
+
},
|
|
59
|
+
"sideEffects": [
|
|
60
|
+
"**/*.css"
|
|
61
|
+
],
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "pnpm clean && rollup -c",
|
|
64
|
+
"dev": "rollup -c -w",
|
|
65
|
+
"test": "vitest run",
|
|
66
|
+
"test:watch": "vitest",
|
|
67
|
+
"type-check": "tsc --noEmit",
|
|
68
|
+
"clean": "rm -rf dist"
|
|
69
|
+
}
|
|
70
|
+
}
|