@ladder-ui/select 0.2.0 → 0.4.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 CHANGED
@@ -1,2 +1,3 @@
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";
1
+ export { Select, SelectTrigger, SelectValue, SelectContent, SelectItem, SelectGroup, SelectLabel, SelectSeparator, } from "./select";
2
+ export type { SelectProps, SelectTriggerProps, SelectValueProps, SelectItemProps, } from "./select";
3
+ export { Select as default } from "./select";
package/dist/index.js CHANGED
@@ -1,25 +1,22 @@
1
1
  'use strict';
2
2
 
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
3
5
  var jsxRuntime = require('react/jsx-runtime');
4
6
  var react = require('react');
5
7
  var reactDom = require('react-dom');
6
- var concatClassNames = require('@ladder-ui/core/concatClassNames');
8
+ var core = require('@ladder-ui/core');
9
+ var primitives = require('@ladder-ui/primitives');
7
10
 
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" }) }));
11
+ const useIsomorphicLayoutEffect = typeof window !== "undefined" ? react.useLayoutEffect : react.useEffect;
12
+ // ─── Icons ──────────────────────────────────────────────────────────────────
13
+ function ChevronDownIcon({ className, style }) {
14
+ 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, style: style, children: jsxRuntime.jsx("path", { d: "m6 9 6 6 6-6" }) }));
14
15
  }
15
16
  function CheckIcon({ className }) {
16
17
  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
  }
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.
19
+ // ─── Helper: Label Extraction ────────────────────────────────────────────────
23
20
  function extractTextContent(children) {
24
21
  if (typeof children === "string")
25
22
  return children;
@@ -27,303 +24,160 @@ function extractTextContent(children) {
27
24
  return String(children);
28
25
  if (Array.isArray(children))
29
26
  return children.map(extractTextContent).join("");
30
- if (react.isValidElement(children)) {
27
+ if (react.isValidElement(children))
31
28
  return extractTextContent(children.props.children);
32
- }
33
29
  return "";
34
30
  }
35
31
  function scanItemLabels(children, registry) {
36
32
  react.Children.forEach(children, (child) => {
37
33
  if (!react.isValidElement(child))
38
34
  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);
35
+ // Check for SelectItem based on type or name
36
+ const type = child.type;
37
+ const dn = type.displayName || type.name;
38
+ if (dn === "SelectItem") {
39
+ const key = child.props.value ?? "";
40
+ const label = extractTextContent(child.props.children);
50
41
  if (label)
51
42
  registry.set(key, label);
52
43
  }
53
- // Recurse into children of non-item elements
54
- if (props.children) {
55
- scanItemLabels(props.children, registry);
44
+ if (child.props.children) {
45
+ scanItemLabels(child.props.children, registry);
56
46
  }
57
47
  });
58
48
  }
59
- const SelectContext = react.createContext({
60
- open: false,
61
- setOpen: () => { },
62
- value: undefined,
63
- displayLabel: "",
64
- triggerId: "",
65
- contentId: "",
66
- triggerRef: { current: null },
49
+ const SelectContext = react.createContext(null);
50
+ // ─── Variants ────────────────────────────────────────────────────────────────
51
+ const selectTriggerVariants = core.cva({
52
+ base: "lui-select-trigger",
53
+ variants: {
54
+ size: { sm: "lui-select-trigger--sm", md: "lui-select-trigger--md" },
55
+ status: { default: "", error: "lui-select-trigger--error" },
56
+ fullWidth: { true: "lui-select-trigger--full-width" },
57
+ },
58
+ defaultVariants: { size: "md", status: "default" },
67
59
  });
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 }));
60
+ function Select({ children, items, ...props }) {
61
+ const select = primitives.useSelect(props);
62
+ const labelsMap = react.useMemo(() => {
63
+ const map = new Map();
64
+ // 1. Scan children (Static JSX)
65
+ scanItemLabels(children, map);
66
+ // 2. Add explicit items (Dynamic/SDUI)
67
+ items?.forEach(item => map.set(item.value, item.label));
68
+ return map;
69
+ }, [children, items]);
70
+ const displayLabel = (select.value !== undefined ? labelsMap.get(select.value) : undefined) ?? "";
71
+ return (jsxRuntime.jsx(SelectContext, { value: { ...select, displayLabel }, children: children }));
113
72
  }
114
73
  Select.displayName = "Select";
115
- function SelectTrigger({ ref, className, children, size = "default", fullWidth, disabled: propDisabled, onKeyDown, ...props }) {
74
+ const SelectTrigger = react.forwardRef(({ className, size, status, fullWidth, children, ...props }, ref) => {
116
75
  const ctx = react.use(SelectContext);
117
- const isDisabled = propDisabled || ctx.disabled;
118
- // Merge consumer ref + context triggerRef
119
- const setRef = (el) => {
76
+ if (!ctx)
77
+ return null;
78
+ const setRefs = (el) => {
120
79
  ctx.triggerRef.current = el;
121
- if (typeof ref === "function") {
80
+ if (typeof ref === "function")
122
81
  ref(el);
123
- }
124
- else if (ref) {
82
+ else if (ref)
125
83
  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
84
  };
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
- }
85
+ return (jsxRuntime.jsxs("button", { ref: setRefs, type: "button", role: "combobox", id: ctx.triggerId, "aria-expanded": ctx.open, "aria-haspopup": "listbox", "aria-controls": ctx.open ? ctx.contentId : undefined, disabled: ctx.disabled, className: selectTriggerVariants({ size, status, fullWidth, className }), onClick: (e) => {
86
+ ctx.setOpen(!ctx.open);
87
+ props.onClick?.(e);
88
+ }, onKeyDown: (e) => {
89
+ if (e.key === "Escape" && ctx.open) {
90
+ e.preventDefault();
91
+ ctx.setOpen(false);
92
+ }
93
+ if ((e.key === "ArrowDown" || e.key === "Enter" || e.key === " ") && !ctx.open) {
94
+ e.preventDefault();
95
+ ctx.setOpen(true);
96
+ }
97
+ props.onKeyDown?.(e);
98
+ }, ...props, children: [children, jsxRuntime.jsx(ChevronDownIcon, { className: "lui-select-trigger__chevron" })] }));
99
+ });
138
100
  SelectTrigger.displayName = "SelectTrigger";
139
- function SelectValue({ placeholder, className }) {
101
+ const SelectValue = ({ placeholder, className }) => {
140
102
  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
- }
103
+ return (jsxRuntime.jsx("span", { className: core.concatClassNames("lui-select-value", className), "data-slot": "select-value", children: ctx?.displayLabel || placeholder }));
104
+ };
144
105
  SelectValue.displayName = "SelectValue";
145
- function SelectContent({ ref, className, children, position = "item-aligned", }) {
106
+ const SelectContent = react.forwardRef(({ children, className, ...props }, ref) => {
146
107
  const ctx = react.use(SelectContext);
147
108
  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
109
  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)
110
+ const [isPositioned, setIsPositioned] = react.useState(false);
111
+ const { addRef } = primitives.useOutsideClick(() => ctx?.setOpen(false), ctx?.open);
112
+ // Two-pass positioning strategy
113
+ useIsomorphicLayoutEffect(() => {
114
+ if (!ctx?.open || !ctx.triggerRef.current || !contentRef.current) {
115
+ setIsPositioned(false);
156
116
  return;
117
+ }
157
118
  const triggerRect = ctx.triggerRef.current.getBoundingClientRect();
158
119
  const content = contentRef.current;
159
120
  const contentHeight = content.offsetHeight;
160
- const contentWidth = content.offsetWidth;
161
121
  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
- }
122
+ // Popper strategy: open below trigger
123
+ let top = triggerRect.bottom + window.scrollY + 4;
124
+ const left = triggerRect.left + window.scrollX;
125
+ // Flip if no space below
126
+ if (triggerRect.bottom + contentHeight + 8 > viewportHeight) {
127
+ top = triggerRect.top + window.scrollY - contentHeight - 4;
183
128
  }
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
129
  setCoords({ top, left, width: triggerRect.width });
198
130
  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": {
131
+ }, [ctx?.open]);
132
+ if (!ctx?.open)
133
+ return null;
134
+ return reactDom.createPortal(jsxRuntime.jsx("div", { ref: (node) => {
135
+ contentRef.current = node;
136
+ if (typeof ref === "function")
137
+ ref(node);
138
+ else if (ref)
139
+ ref.current = node;
140
+ if (node)
141
+ addRef({ current: node });
142
+ addRef(ctx.triggerRef);
143
+ }, id: ctx.contentId, role: "listbox", "aria-labelledby": ctx.triggerId, "data-slot": "select-content", className: core.concatClassNames("lui-select-content", className), style: {
144
+ position: "absolute",
145
+ top: coords.top,
146
+ left: coords.left,
147
+ width: coords.width,
148
+ minWidth: coords.width,
149
+ zIndex: 9999,
150
+ visibility: isPositioned ? "visible" : "hidden",
151
+ }, onKeyDown: (e) => {
152
+ if (e.key === "Escape") {
256
153
  ctx.setOpen(false);
257
154
  ctx.triggerRef.current?.focus();
258
- break;
259
155
  }
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
- }
156
+ props.onKeyDown?.(e);
157
+ }, ...props, children: jsxRuntime.jsx("div", { className: "lui-select-content__viewport", children: children }) }), document.body);
158
+ });
286
159
  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 }) {
160
+ const SelectItem = react.forwardRef(({ value, children, disabled, className, ...props }, ref) => {
296
161
  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
- }
162
+ const isSelected = ctx?.value === value;
163
+ return (jsxRuntime.jsxs("div", { ref: ref, role: "option", "aria-selected": isSelected, "aria-disabled": disabled, className: core.concatClassNames("lui-select-item", isSelected && "lui-select-item--selected", disabled && "lui-select-item--disabled", className), onClick: (e) => {
164
+ if (!disabled) {
165
+ ctx?.onValueChange(value === null ? "" : value);
166
+ ctx?.setOpen(false);
167
+ }
168
+ props.onClick?.(e);
169
+ }, ...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 })] }));
170
+ });
315
171
  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
- }
172
+ const SelectGroup = react.forwardRef(({ className, ...props }, ref) => (jsxRuntime.jsx("div", { ref: ref, role: "group", className: core.concatClassNames("lui-select-group", className), ...props })));
173
+ SelectGroup.displayName = "SelectGroup";
174
+ const SelectLabel = react.forwardRef(({ className, ...props }, ref) => (jsxRuntime.jsx("div", { ref: ref, className: core.concatClassNames("lui-select-label", className), ...props })));
175
+ SelectLabel.displayName = "SelectLabel";
176
+ const SelectSeparator = react.forwardRef(({ className, ...props }, ref) => (jsxRuntime.jsx("hr", { ref: ref, className: core.concatClassNames("lui-select-separator", className), ...props })));
319
177
  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
- }
178
+ const SelectScrollUpButton = react.forwardRef(({ className, ...props }, ref) => (jsxRuntime.jsx("div", { ref: ref, className: core.concatClassNames("lui-select-scroll-button", className), "aria-hidden": "true", ...props, children: jsxRuntime.jsx(ChevronDownIcon, { className: "lui-select-scroll-button__icon", style: { transform: "rotate(180deg)" } }) })));
323
179
  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
- }
180
+ const SelectScrollDownButton = react.forwardRef(({ className, ...props }, ref) => (jsxRuntime.jsx("div", { ref: ref, className: core.concatClassNames("lui-select-scroll-button", className), "aria-hidden": "true", ...props, children: jsxRuntime.jsx(ChevronDownIcon, { className: "lui-select-scroll-button__icon" }) })));
327
181
  SelectScrollDownButton.displayName = "SelectScrollDownButton";
328
182
 
329
183
  exports.Select = Select;
@@ -331,8 +185,7 @@ exports.SelectContent = SelectContent;
331
185
  exports.SelectGroup = SelectGroup;
332
186
  exports.SelectItem = SelectItem;
333
187
  exports.SelectLabel = SelectLabel;
334
- exports.SelectScrollDownButton = SelectScrollDownButton;
335
- exports.SelectScrollUpButton = SelectScrollUpButton;
336
188
  exports.SelectSeparator = SelectSeparator;
337
189
  exports.SelectTrigger = SelectTrigger;
338
190
  exports.SelectValue = SelectValue;
191
+ exports.default = Select;
package/dist/index.mjs CHANGED
@@ -1,23 +1,18 @@
1
- import { jsx, jsxs } from 'react/jsx-runtime';
2
- import { createContext, useState, useId, useRef, useCallback, use, useEffect, Children, isValidElement } from 'react';
1
+ import { jsxs, jsx } from 'react/jsx-runtime';
2
+ import { createContext, forwardRef, use, useRef, useState, useLayoutEffect, useEffect, useMemo, Children, isValidElement } from 'react';
3
3
  import { createPortal } from 'react-dom';
4
- import concatClassNames from '@ladder-ui/core/concatClassNames';
4
+ import { cva, concatClassNames } from '@ladder-ui/core';
5
+ import { useOutsideClick, useSelect } from '@ladder-ui/primitives';
5
6
 
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" }) }));
7
+ const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
8
+ // ─── Icons ──────────────────────────────────────────────────────────────────
9
+ function ChevronDownIcon({ className, style }) {
10
+ 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, style: style, children: jsx("path", { d: "m6 9 6 6 6-6" }) }));
12
11
  }
13
12
  function CheckIcon({ className }) {
14
13
  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
14
  }
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.
15
+ // ─── Helper: Label Extraction ────────────────────────────────────────────────
21
16
  function extractTextContent(children) {
22
17
  if (typeof children === "string")
23
18
  return children;
@@ -25,303 +20,160 @@ function extractTextContent(children) {
25
20
  return String(children);
26
21
  if (Array.isArray(children))
27
22
  return children.map(extractTextContent).join("");
28
- if (isValidElement(children)) {
23
+ if (isValidElement(children))
29
24
  return extractTextContent(children.props.children);
30
- }
31
25
  return "";
32
26
  }
33
27
  function scanItemLabels(children, registry) {
34
28
  Children.forEach(children, (child) => {
35
29
  if (!isValidElement(child))
36
30
  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);
31
+ // Check for SelectItem based on type or name
32
+ const type = child.type;
33
+ const dn = type.displayName || type.name;
34
+ if (dn === "SelectItem") {
35
+ const key = child.props.value ?? "";
36
+ const label = extractTextContent(child.props.children);
48
37
  if (label)
49
38
  registry.set(key, label);
50
39
  }
51
- // Recurse into children of non-item elements
52
- if (props.children) {
53
- scanItemLabels(props.children, registry);
40
+ if (child.props.children) {
41
+ scanItemLabels(child.props.children, registry);
54
42
  }
55
43
  });
56
44
  }
57
- const SelectContext = createContext({
58
- open: false,
59
- setOpen: () => { },
60
- value: undefined,
61
- displayLabel: "",
62
- triggerId: "",
63
- contentId: "",
64
- triggerRef: { current: null },
45
+ const SelectContext = createContext(null);
46
+ // ─── Variants ────────────────────────────────────────────────────────────────
47
+ const selectTriggerVariants = cva({
48
+ base: "lui-select-trigger",
49
+ variants: {
50
+ size: { sm: "lui-select-trigger--sm", md: "lui-select-trigger--md" },
51
+ status: { default: "", error: "lui-select-trigger--error" },
52
+ fullWidth: { true: "lui-select-trigger--full-width" },
53
+ },
54
+ defaultVariants: { size: "md", status: "default" },
65
55
  });
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 }));
56
+ function Select({ children, items, ...props }) {
57
+ const select = useSelect(props);
58
+ const labelsMap = useMemo(() => {
59
+ const map = new Map();
60
+ // 1. Scan children (Static JSX)
61
+ scanItemLabels(children, map);
62
+ // 2. Add explicit items (Dynamic/SDUI)
63
+ items?.forEach(item => map.set(item.value, item.label));
64
+ return map;
65
+ }, [children, items]);
66
+ const displayLabel = (select.value !== undefined ? labelsMap.get(select.value) : undefined) ?? "";
67
+ return (jsx(SelectContext, { value: { ...select, displayLabel }, children: children }));
111
68
  }
112
69
  Select.displayName = "Select";
113
- function SelectTrigger({ ref, className, children, size = "default", fullWidth, disabled: propDisabled, onKeyDown, ...props }) {
70
+ const SelectTrigger = forwardRef(({ className, size, status, fullWidth, children, ...props }, ref) => {
114
71
  const ctx = use(SelectContext);
115
- const isDisabled = propDisabled || ctx.disabled;
116
- // Merge consumer ref + context triggerRef
117
- const setRef = (el) => {
72
+ if (!ctx)
73
+ return null;
74
+ const setRefs = (el) => {
118
75
  ctx.triggerRef.current = el;
119
- if (typeof ref === "function") {
76
+ if (typeof ref === "function")
120
77
  ref(el);
121
- }
122
- else if (ref) {
78
+ else if (ref)
123
79
  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
80
  };
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
- }
81
+ return (jsxs("button", { ref: setRefs, type: "button", role: "combobox", id: ctx.triggerId, "aria-expanded": ctx.open, "aria-haspopup": "listbox", "aria-controls": ctx.open ? ctx.contentId : undefined, disabled: ctx.disabled, className: selectTriggerVariants({ size, status, fullWidth, className }), onClick: (e) => {
82
+ ctx.setOpen(!ctx.open);
83
+ props.onClick?.(e);
84
+ }, onKeyDown: (e) => {
85
+ if (e.key === "Escape" && ctx.open) {
86
+ e.preventDefault();
87
+ ctx.setOpen(false);
88
+ }
89
+ if ((e.key === "ArrowDown" || e.key === "Enter" || e.key === " ") && !ctx.open) {
90
+ e.preventDefault();
91
+ ctx.setOpen(true);
92
+ }
93
+ props.onKeyDown?.(e);
94
+ }, ...props, children: [children, jsx(ChevronDownIcon, { className: "lui-select-trigger__chevron" })] }));
95
+ });
136
96
  SelectTrigger.displayName = "SelectTrigger";
137
- function SelectValue({ placeholder, className }) {
97
+ const SelectValue = ({ placeholder, className }) => {
138
98
  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
- }
99
+ return (jsx("span", { className: concatClassNames("lui-select-value", className), "data-slot": "select-value", children: ctx?.displayLabel || placeholder }));
100
+ };
142
101
  SelectValue.displayName = "SelectValue";
143
- function SelectContent({ ref, className, children, position = "item-aligned", }) {
102
+ const SelectContent = forwardRef(({ children, className, ...props }, ref) => {
144
103
  const ctx = use(SelectContext);
145
104
  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
105
  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)
106
+ const [isPositioned, setIsPositioned] = useState(false);
107
+ const { addRef } = useOutsideClick(() => ctx?.setOpen(false), ctx?.open);
108
+ // Two-pass positioning strategy
109
+ useIsomorphicLayoutEffect(() => {
110
+ if (!ctx?.open || !ctx.triggerRef.current || !contentRef.current) {
111
+ setIsPositioned(false);
154
112
  return;
113
+ }
155
114
  const triggerRect = ctx.triggerRef.current.getBoundingClientRect();
156
115
  const content = contentRef.current;
157
116
  const contentHeight = content.offsetHeight;
158
- const contentWidth = content.offsetWidth;
159
117
  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
- }
118
+ // Popper strategy: open below trigger
119
+ let top = triggerRect.bottom + window.scrollY + 4;
120
+ const left = triggerRect.left + window.scrollX;
121
+ // Flip if no space below
122
+ if (triggerRect.bottom + contentHeight + 8 > viewportHeight) {
123
+ top = triggerRect.top + window.scrollY - contentHeight - 4;
181
124
  }
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
125
  setCoords({ top, left, width: triggerRect.width });
196
126
  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": {
127
+ }, [ctx?.open]);
128
+ if (!ctx?.open)
129
+ return null;
130
+ return createPortal(jsx("div", { ref: (node) => {
131
+ contentRef.current = node;
132
+ if (typeof ref === "function")
133
+ ref(node);
134
+ else if (ref)
135
+ ref.current = node;
136
+ if (node)
137
+ addRef({ current: node });
138
+ addRef(ctx.triggerRef);
139
+ }, id: ctx.contentId, role: "listbox", "aria-labelledby": ctx.triggerId, "data-slot": "select-content", className: concatClassNames("lui-select-content", className), style: {
140
+ position: "absolute",
141
+ top: coords.top,
142
+ left: coords.left,
143
+ width: coords.width,
144
+ minWidth: coords.width,
145
+ zIndex: 9999,
146
+ visibility: isPositioned ? "visible" : "hidden",
147
+ }, onKeyDown: (e) => {
148
+ if (e.key === "Escape") {
254
149
  ctx.setOpen(false);
255
150
  ctx.triggerRef.current?.focus();
256
- break;
257
151
  }
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
- }
152
+ props.onKeyDown?.(e);
153
+ }, ...props, children: jsx("div", { className: "lui-select-content__viewport", children: children }) }), document.body);
154
+ });
284
155
  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 }) {
156
+ const SelectItem = forwardRef(({ value, children, disabled, className, ...props }, ref) => {
294
157
  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
- }
158
+ const isSelected = ctx?.value === value;
159
+ return (jsxs("div", { ref: ref, role: "option", "aria-selected": isSelected, "aria-disabled": disabled, className: concatClassNames("lui-select-item", isSelected && "lui-select-item--selected", disabled && "lui-select-item--disabled", className), onClick: (e) => {
160
+ if (!disabled) {
161
+ ctx?.onValueChange(value === null ? "" : value);
162
+ ctx?.setOpen(false);
163
+ }
164
+ props.onClick?.(e);
165
+ }, ...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 })] }));
166
+ });
313
167
  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
- }
168
+ const SelectGroup = forwardRef(({ className, ...props }, ref) => (jsx("div", { ref: ref, role: "group", className: concatClassNames("lui-select-group", className), ...props })));
169
+ SelectGroup.displayName = "SelectGroup";
170
+ const SelectLabel = forwardRef(({ className, ...props }, ref) => (jsx("div", { ref: ref, className: concatClassNames("lui-select-label", className), ...props })));
171
+ SelectLabel.displayName = "SelectLabel";
172
+ const SelectSeparator = forwardRef(({ className, ...props }, ref) => (jsx("hr", { ref: ref, className: concatClassNames("lui-select-separator", className), ...props })));
317
173
  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
- }
174
+ const SelectScrollUpButton = forwardRef(({ className, ...props }, ref) => (jsx("div", { ref: ref, className: concatClassNames("lui-select-scroll-button", className), "aria-hidden": "true", ...props, children: jsx(ChevronDownIcon, { className: "lui-select-scroll-button__icon", style: { transform: "rotate(180deg)" } }) })));
321
175
  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
- }
176
+ const SelectScrollDownButton = forwardRef(({ className, ...props }, ref) => (jsx("div", { ref: ref, className: concatClassNames("lui-select-scroll-button", className), "aria-hidden": "true", ...props, children: jsx(ChevronDownIcon, { className: "lui-select-scroll-button__icon" }) })));
325
177
  SelectScrollDownButton.displayName = "SelectScrollDownButton";
326
178
 
327
- export { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectScrollDownButton, SelectScrollUpButton, SelectSeparator, SelectTrigger, SelectValue };
179
+ export { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectSeparator, SelectTrigger, SelectValue, Select as default };
package/dist/select.css CHANGED
@@ -26,7 +26,7 @@
26
26
  }
27
27
  .lui-select-trigger:focus-visible {
28
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);
29
+ box-shadow: 0 0 0 2px var(--lui-bg-surface), 0 0 0 4px var(--lui-select-focus-ring-color);
30
30
  }
31
31
  .lui-select-trigger:disabled, .lui-select-trigger[aria-disabled=true] {
32
32
  opacity: var(--lui-disabled-opacity);
@@ -107,12 +107,11 @@
107
107
  font-family: var(--lui-font-family);
108
108
  }
109
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);
110
+ background-color: var(--lui-select-item-hover-bg);
111
111
  color: var(--lui-select-item-hover-text);
112
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);
113
+ .lui-select-item--selected {
114
+ color: var(--lui-select-item-selected-text);
116
115
  }
117
116
  .lui-select-item--disabled {
118
117
  opacity: var(--lui-disabled-opacity);
@@ -145,16 +144,4 @@
145
144
  border-top: 1px solid var(--lui-select-separator-color);
146
145
  margin: 0.25rem -0.25rem;
147
146
  }
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
147
  }
package/dist/select.d.ts CHANGED
@@ -1,119 +1,62 @@
1
- import type { Ref } from "react";
2
- /** A single item entry for the data-driven `items` prop on `Select`. */
1
+ import type { Ref, HTMLAttributes, ReactNode } from "react";
2
+ import { type VariantProps } from "@ladder-ui/core";
3
+ declare const selectTriggerVariants: (props?: (import("packages/core/dist/cva").ConfigVariants<{
4
+ size: {
5
+ sm: string;
6
+ md: string;
7
+ };
8
+ status: {
9
+ default: string;
10
+ error: string;
11
+ };
12
+ fullWidth: {
13
+ true: string;
14
+ };
15
+ }> & {
16
+ className?: string;
17
+ }) | undefined) => string;
3
18
  export interface SelectItemData {
4
19
  label: string;
5
- /** Pass `null` for the "no selection" reset item (equivalent to `value=""`). */
6
- value: string | null;
20
+ value: string;
7
21
  }
8
22
  export interface SelectProps {
9
- /** Controlled selected value. Pair with `onValueChange`. */
10
23
  value?: string;
11
- /** Uncontrolled initial selected value. */
12
24
  defaultValue?: string;
13
- /** Called when the user selects a different item. */
14
25
  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
26
  open?: boolean;
20
- /** Called when open state changes. */
21
27
  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
+ disabled?: boolean;
29
+ children?: ReactNode;
28
30
  items?: SelectItemData[];
29
31
  }
30
- export declare function Select({ value: controlledValue, defaultValue, onValueChange, disabled, children, open: controlledOpen, onOpenChange, items, }: SelectProps): import("react/jsx-runtime").JSX.Element;
32
+ export declare function Select({ children, items, ...props }: SelectProps): import("react/jsx-runtime").JSX.Element;
31
33
  export declare namespace Select {
32
34
  var displayName: string;
33
35
  }
34
- export interface SelectTriggerProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
36
+ export interface SelectTriggerProps extends Omit<HTMLAttributes<HTMLButtonElement>, "size">, VariantProps<typeof selectTriggerVariants> {
35
37
  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. */
38
+ size?: "sm" | "md";
39
+ status?: "default" | "error";
39
40
  fullWidth?: boolean;
40
41
  }
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
- }
42
+ export declare const SelectTrigger: import("react").ForwardRefExoticComponent<Omit<SelectTriggerProps, "ref"> & import("react").RefAttributes<HTMLButtonElement>>;
45
43
  export interface SelectValueProps {
46
- /** Text shown when no value is selected. */
47
44
  placeholder?: string;
48
45
  className?: string;
49
46
  }
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. */
47
+ export declare const SelectValue: {
48
+ ({ placeholder, className }: SelectValueProps): import("react/jsx-runtime").JSX.Element;
49
+ displayName: string;
50
+ };
51
+ export declare const SelectContent: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLDivElement> & import("react").RefAttributes<HTMLDivElement>>;
52
+ export interface SelectItemProps extends Omit<HTMLAttributes<HTMLDivElement>, "value"> {
53
+ value: string;
93
54
  disabled?: boolean;
94
55
  }
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
- }
56
+ export declare const SelectItem: import("react").ForwardRefExoticComponent<SelectItemProps & import("react").RefAttributes<HTMLDivElement>>;
57
+ export declare const SelectGroup: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLDivElement> & import("react").RefAttributes<HTMLDivElement>>;
58
+ export declare const SelectLabel: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLDivElement> & import("react").RefAttributes<HTMLDivElement>>;
59
+ export declare const SelectSeparator: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLHRElement> & import("react").RefAttributes<HTMLHRElement>>;
60
+ export declare const SelectScrollUpButton: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLDivElement> & import("react").RefAttributes<HTMLDivElement>>;
61
+ export declare const SelectScrollDownButton: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLDivElement> & import("react").RefAttributes<HTMLDivElement>>;
62
+ export {};
@@ -1,28 +1,28 @@
1
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);
2
+ --lui-select-trigger-bg: var(--lui-bg-surface);
3
+ --lui-select-trigger-text: var(--lui-text-primary);
4
+ --lui-select-trigger-border: var(--lui-border-default);
5
5
  --lui-select-trigger-radius: var(--lui-radius-sm);
6
6
  --lui-select-trigger-height: 2.25rem;
7
7
  --lui-select-trigger-height-sm: 2rem;
8
8
  --lui-select-trigger-px: 0.75rem;
9
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);
10
+ --lui-select-focus-border: var(--lui-bg-interactive);
11
+ --lui-select-focus-ring-color: color-mix(in srgb, var(--lui-bg-interactive) 40%, transparent);
12
+ --lui-select-content-bg: var(--lui-bg-surface);
13
+ --lui-select-content-border: var(--lui-border-default);
14
14
  --lui-select-content-radius: var(--lui-radius-sm);
15
15
  --lui-select-content-shadow: 0 4px 16px rgba(0, 0, 0, 0.12), 0 1px 4px rgba(0, 0, 0, 0.08);
16
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);
17
+ --lui-select-item-text: var(--lui-text-primary);
18
+ --lui-select-item-hover-bg: color-mix(in srgb, var(--lui-bg-interactive) 10%, transparent);
19
+ --lui-select-item-hover-text: var(--lui-text-primary);
20
+ --lui-select-item-selected-text: var(--lui-bg-interactive);
21
+ --lui-select-item-focus-bg: color-mix(in srgb, var(--lui-bg-interactive) 10%, transparent);
22
22
  --lui-select-item-radius: var(--lui-radius-sm);
23
23
  --lui-select-item-px: 0.5rem;
24
24
  --lui-select-item-py: 0.375rem;
25
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);
26
+ --lui-select-label-text: color-mix(in srgb, var(--lui-text-primary) 60%, transparent);
27
+ --lui-select-separator-color: var(--lui-border-default);
28
28
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ladder-ui/select",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -17,48 +17,31 @@
17
17
  "files": [
18
18
  "dist"
19
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
20
  "devDependencies": {
44
21
  "@rollup/plugin-typescript": "^11.1.6",
45
22
  "@types/react": "^19.0.0",
46
- "@types/react-dom": "^19.0.0",
23
+ "@types/react-dom": "^19.2.3",
47
24
  "rollup": "^4.59.0",
48
25
  "rollup-plugin-postcss": "^4.0.2",
49
26
  "sass": "^1.90.0",
50
27
  "tslib": "^2.6.2",
51
28
  "typescript": "^5.3.3",
52
- "@ladder-ui/core": "0.2.0"
29
+ "@ladder-ui/primitives": "0.4.0",
30
+ "@ladder-ui/layout": "0.4.0",
31
+ "@ladder-ui/core": "0.4.0"
53
32
  },
54
33
  "peerDependencies": {
55
34
  "@ladder-ui/core": ">=0.0.0",
56
- "react": ">=18.0.0",
57
- "react-dom": ">=18.0.0"
35
+ "react": ">=18.0.0"
58
36
  },
59
37
  "sideEffects": [
60
38
  "**/*.css"
61
39
  ],
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "https://github.com/ivelaval/ladder-ui.git",
43
+ "directory": "packages/select"
44
+ },
62
45
  "scripts": {
63
46
  "build": "pnpm clean && rollup -c",
64
47
  "dev": "rollup -c -w",