@octanejs/radix 0.1.31 → 0.1.33
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/cjs/AccessibleIcon.cjs +56 -0
- package/dist/cjs/Accordion.cjs +272 -0
- package/dist/cjs/AlertDialog.cjs +144 -0
- package/dist/cjs/Arrow.cjs +43 -0
- package/dist/cjs/AspectRatio.cjs +56 -0
- package/dist/cjs/Avatar.cjs +150 -0
- package/dist/cjs/Checkbox.cjs +281 -0
- package/dist/cjs/Collapsible.cjs +176 -0
- package/dist/cjs/ContextMenu.cjs +315 -0
- package/dist/cjs/Dialog.cjs +291 -0
- package/dist/cjs/DismissableLayer.cjs +382 -0
- package/dist/cjs/DropdownMenu.cjs +296 -0
- package/dist/cjs/FocusScope.cjs +257 -0
- package/dist/cjs/Form.cjs +537 -0
- package/dist/cjs/HoverCard.cjs +309 -0
- package/dist/cjs/Label.cjs +42 -0
- package/dist/cjs/Menu.cjs +925 -0
- package/dist/cjs/Menubar.cjs +475 -0
- package/dist/cjs/NavigationMenu.cjs +942 -0
- package/dist/cjs/OneTimePasswordField.cjs +729 -0
- package/dist/cjs/PasswordToggleField.cjs +377 -0
- package/dist/cjs/Popover.cjs +340 -0
- package/dist/cjs/Popper.cjs +368 -0
- package/dist/cjs/Portal.cjs +40 -0
- package/dist/cjs/Presence.cjs +183 -0
- package/dist/cjs/Primitive.cjs +63 -0
- package/dist/cjs/Progress.cjs +111 -0
- package/dist/cjs/Radio.cjs +240 -0
- package/dist/cjs/RadioGroup.cjs +216 -0
- package/dist/cjs/RovingFocusGroup.cjs +253 -0
- package/dist/cjs/ScrollArea.cjs +828 -0
- package/dist/cjs/Select.cjs +1429 -0
- package/dist/cjs/Separator.cjs +51 -0
- package/dist/cjs/Slider.cjs +634 -0
- package/dist/cjs/Slot.cjs +97 -0
- package/dist/cjs/Switch.cjs +253 -0
- package/dist/cjs/Tabs.cjs +195 -0
- package/dist/cjs/Toast.cjs +733 -0
- package/dist/cjs/Toggle.cjs +54 -0
- package/dist/cjs/ToggleGroup.cjs +174 -0
- package/dist/cjs/Toolbar.cjs +165 -0
- package/dist/cjs/Tooltip.cjs +628 -0
- package/dist/cjs/VisuallyHidden.cjs +52 -0
- package/dist/cjs/collection.cjs +95 -0
- package/dist/cjs/compose-event-handlers.cjs +35 -0
- package/dist/cjs/compose-refs.cjs +68 -0
- package/dist/cjs/context.cjs +96 -0
- package/dist/cjs/direction.cjs +41 -0
- package/dist/cjs/focus-guards.cjs +76 -0
- package/dist/cjs/index.cjs +174 -0
- package/dist/cjs/internal.cjs +51 -0
- package/dist/cjs/scroll-lock.cjs +67 -0
- package/dist/cjs/use-callback-ref.cjs +47 -0
- package/dist/cjs/use-effect-event.cjs +52 -0
- package/dist/cjs/use-is-hydrated.cjs +43 -0
- package/dist/cjs/use-previous.cjs +47 -0
- package/dist/cjs/use-size.cjs +69 -0
- package/dist/cjs/useControllableState.cjs +80 -0
- package/dist/cjs/useId.cjs +36 -0
- package/package.json +14 -7
|
@@ -0,0 +1,1429 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var Select_exports = {};
|
|
30
|
+
__export(Select_exports, {
|
|
31
|
+
Arrow: () => Arrow,
|
|
32
|
+
BubbleInput: () => BubbleInput,
|
|
33
|
+
Content: () => Content,
|
|
34
|
+
Group: () => Group,
|
|
35
|
+
Icon: () => Icon,
|
|
36
|
+
Item: () => Item,
|
|
37
|
+
ItemIndicator: () => ItemIndicator,
|
|
38
|
+
ItemText: () => ItemText,
|
|
39
|
+
Label: () => Label,
|
|
40
|
+
Portal: () => Portal,
|
|
41
|
+
Provider: () => Provider,
|
|
42
|
+
Root: () => Root,
|
|
43
|
+
ScrollDownButton: () => ScrollDownButton,
|
|
44
|
+
ScrollUpButton: () => ScrollUpButton,
|
|
45
|
+
Select: () => Root,
|
|
46
|
+
SelectArrow: () => Arrow,
|
|
47
|
+
SelectBubbleInput: () => BubbleInput,
|
|
48
|
+
SelectContent: () => Content,
|
|
49
|
+
SelectGroup: () => Group,
|
|
50
|
+
SelectIcon: () => Icon,
|
|
51
|
+
SelectItem: () => Item,
|
|
52
|
+
SelectItemIndicator: () => ItemIndicator,
|
|
53
|
+
SelectItemText: () => ItemText,
|
|
54
|
+
SelectLabel: () => Label,
|
|
55
|
+
SelectPortal: () => Portal,
|
|
56
|
+
SelectProvider: () => Provider,
|
|
57
|
+
SelectScrollDownButton: () => ScrollDownButton,
|
|
58
|
+
SelectScrollUpButton: () => ScrollUpButton,
|
|
59
|
+
SelectSeparator: () => Separator,
|
|
60
|
+
SelectTrigger: () => Trigger,
|
|
61
|
+
SelectValue: () => Value,
|
|
62
|
+
SelectViewport: () => Viewport,
|
|
63
|
+
Separator: () => Separator,
|
|
64
|
+
Trigger: () => Trigger,
|
|
65
|
+
Value: () => Value,
|
|
66
|
+
Viewport: () => Viewport,
|
|
67
|
+
createSelectScope: () => createSelectScope
|
|
68
|
+
});
|
|
69
|
+
module.exports = __toCommonJS(Select_exports);
|
|
70
|
+
var import_octane = require("octane");
|
|
71
|
+
var import_aria_hidden = require("aria-hidden");
|
|
72
|
+
var import_collection = require("./collection.cjs");
|
|
73
|
+
var import_compose_event_handlers = require("./compose-event-handlers.cjs");
|
|
74
|
+
var import_compose_refs = require("./compose-refs.cjs");
|
|
75
|
+
var import_context = require("./context.cjs");
|
|
76
|
+
var import_direction = require("./direction.cjs");
|
|
77
|
+
var import_DismissableLayer = require("./DismissableLayer.cjs");
|
|
78
|
+
var import_FocusScope = require("./FocusScope.cjs");
|
|
79
|
+
var import_focus_guards = require("./focus-guards.cjs");
|
|
80
|
+
var import_internal = require("./internal.cjs");
|
|
81
|
+
var PopperPrimitive = __toESM(require("./Popper.cjs"), 1);
|
|
82
|
+
var import_Popper = require("./Popper.cjs");
|
|
83
|
+
var import_Portal = require("./Portal.cjs");
|
|
84
|
+
var import_Presence = require("./Presence.cjs");
|
|
85
|
+
var import_Primitive = require("./Primitive.cjs");
|
|
86
|
+
var import_scroll_lock = require("./scroll-lock.cjs");
|
|
87
|
+
var import_use_callback_ref = require("./use-callback-ref.cjs");
|
|
88
|
+
var import_use_previous = require("./use-previous.cjs");
|
|
89
|
+
var import_useControllableState = require("./useControllableState.cjs");
|
|
90
|
+
var import_useId = require("./useId.cjs");
|
|
91
|
+
var import_VisuallyHidden = require("./VisuallyHidden.cjs");
|
|
92
|
+
const OPEN_KEYS = [" ", "Enter", "ArrowUp", "ArrowDown"];
|
|
93
|
+
const SELECTION_KEYS = [" ", "Enter"];
|
|
94
|
+
const SELECT_NAME = "Select";
|
|
95
|
+
const [Collection, useCollection, createCollectionScope] = (0, import_collection.createCollection)(SELECT_NAME);
|
|
96
|
+
const [createSelectContext, createSelectScope] = (0, import_context.createContextScope)(SELECT_NAME, [
|
|
97
|
+
createCollectionScope,
|
|
98
|
+
import_Popper.createPopperScope
|
|
99
|
+
]);
|
|
100
|
+
const usePopperScope = (0, import_Popper.createPopperScope)();
|
|
101
|
+
const [SelectProviderImpl, useSelectContext] = createSelectContext(SELECT_NAME);
|
|
102
|
+
const [SelectNativeOptionsProvider, useSelectNativeOptionsContext] = createSelectContext(SELECT_NAME);
|
|
103
|
+
const PROVIDER_NAME = "SelectProvider";
|
|
104
|
+
function Provider(props) {
|
|
105
|
+
const slot = (0, import_internal.S)("Select.Provider");
|
|
106
|
+
const {
|
|
107
|
+
__scopeSelect,
|
|
108
|
+
children,
|
|
109
|
+
open: openProp,
|
|
110
|
+
defaultOpen,
|
|
111
|
+
onOpenChange,
|
|
112
|
+
value: valueProp,
|
|
113
|
+
defaultValue,
|
|
114
|
+
onValueChange,
|
|
115
|
+
dir,
|
|
116
|
+
name,
|
|
117
|
+
autoComplete,
|
|
118
|
+
disabled,
|
|
119
|
+
required,
|
|
120
|
+
form,
|
|
121
|
+
// internal render prop used by `Select` (Root) to compose its default parts
|
|
122
|
+
internal_do_not_use_render
|
|
123
|
+
} = props ?? {};
|
|
124
|
+
const popperScope = usePopperScope(__scopeSelect, (0, import_internal.subSlot)(slot, "popper"));
|
|
125
|
+
const [trigger, setTrigger] = (0, import_octane.useState)(null, (0, import_internal.subSlot)(slot, "trigger"));
|
|
126
|
+
const [valueNode, setValueNode] = (0, import_octane.useState)(null, (0, import_internal.subSlot)(slot, "valueNode"));
|
|
127
|
+
const [valueNodeHasChildren, setValueNodeHasChildren] = (0, import_octane.useState)(
|
|
128
|
+
false,
|
|
129
|
+
(0, import_internal.subSlot)(slot, "hasChildren")
|
|
130
|
+
);
|
|
131
|
+
const direction = (0, import_direction.useDirection)(dir);
|
|
132
|
+
const [open, setOpen] = (0, import_useControllableState.useControllableState)(
|
|
133
|
+
{ prop: openProp, defaultProp: defaultOpen ?? false, onChange: onOpenChange },
|
|
134
|
+
(0, import_internal.subSlot)(slot, "open")
|
|
135
|
+
);
|
|
136
|
+
const [value, setValue] = (0, import_useControllableState.useControllableState)(
|
|
137
|
+
{ prop: valueProp, defaultProp: defaultValue, onChange: onValueChange },
|
|
138
|
+
(0, import_internal.subSlot)(slot, "value")
|
|
139
|
+
);
|
|
140
|
+
const triggerPointerDownPosRef = (0, import_octane.useRef)(
|
|
141
|
+
null,
|
|
142
|
+
(0, import_internal.subSlot)(slot, "pointerPos")
|
|
143
|
+
);
|
|
144
|
+
const isFormControl = trigger ? !!form || !!trigger.closest("form") : true;
|
|
145
|
+
const [nativeOptionsSet, setNativeOptionsSet] = (0, import_octane.useState)(
|
|
146
|
+
/* @__PURE__ */ new Set(),
|
|
147
|
+
(0, import_internal.subSlot)(slot, "options")
|
|
148
|
+
);
|
|
149
|
+
const contentId = (0, import_useId.useId)((0, import_internal.subSlot)(slot, "contentId"));
|
|
150
|
+
const handleNativeOptionAdd = (0, import_octane.useCallback)(
|
|
151
|
+
(option) => {
|
|
152
|
+
setNativeOptionsSet((prev) => new Set(prev).add(option));
|
|
153
|
+
},
|
|
154
|
+
[],
|
|
155
|
+
(0, import_internal.subSlot)(slot, "optAdd")
|
|
156
|
+
);
|
|
157
|
+
const handleNativeOptionRemove = (0, import_octane.useCallback)(
|
|
158
|
+
(option) => {
|
|
159
|
+
setNativeOptionsSet((prev) => {
|
|
160
|
+
const optionsSet = new Set(prev);
|
|
161
|
+
optionsSet.delete(option);
|
|
162
|
+
return optionsSet;
|
|
163
|
+
});
|
|
164
|
+
},
|
|
165
|
+
[],
|
|
166
|
+
(0, import_internal.subSlot)(slot, "optRemove")
|
|
167
|
+
);
|
|
168
|
+
const context = {
|
|
169
|
+
required,
|
|
170
|
+
trigger,
|
|
171
|
+
onTriggerChange: setTrigger,
|
|
172
|
+
valueNode,
|
|
173
|
+
onValueNodeChange: setValueNode,
|
|
174
|
+
valueNodeHasChildren,
|
|
175
|
+
onValueNodeHasChildrenChange: setValueNodeHasChildren,
|
|
176
|
+
contentId,
|
|
177
|
+
value,
|
|
178
|
+
onValueChange: setValue,
|
|
179
|
+
open,
|
|
180
|
+
onOpenChange: setOpen,
|
|
181
|
+
dir: direction,
|
|
182
|
+
triggerPointerDownPosRef,
|
|
183
|
+
disabled,
|
|
184
|
+
name,
|
|
185
|
+
autoComplete,
|
|
186
|
+
form,
|
|
187
|
+
nativeOptions: nativeOptionsSet,
|
|
188
|
+
isFormControl
|
|
189
|
+
};
|
|
190
|
+
return (0, import_octane.createElement)(PopperPrimitive.Root, {
|
|
191
|
+
...popperScope,
|
|
192
|
+
children: (0, import_octane.createElement)(SelectProviderImpl, {
|
|
193
|
+
scope: __scopeSelect,
|
|
194
|
+
...context,
|
|
195
|
+
children: (0, import_octane.createElement)(Collection.Provider, {
|
|
196
|
+
scope: __scopeSelect,
|
|
197
|
+
children: (0, import_octane.createElement)(SelectNativeOptionsProvider, {
|
|
198
|
+
scope: __scopeSelect,
|
|
199
|
+
onNativeOptionAdd: handleNativeOptionAdd,
|
|
200
|
+
onNativeOptionRemove: handleNativeOptionRemove,
|
|
201
|
+
children: isFunction(internal_do_not_use_render) ? internal_do_not_use_render(context) : children
|
|
202
|
+
})
|
|
203
|
+
})
|
|
204
|
+
})
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
function Root(props) {
|
|
208
|
+
const { __scopeSelect, children, ...providerProps } = props ?? {};
|
|
209
|
+
return (0, import_octane.createElement)(Provider, {
|
|
210
|
+
__scopeSelect,
|
|
211
|
+
...providerProps,
|
|
212
|
+
// Every array entry is keyed (unkeyed/null entries would trip octane's one-time
|
|
213
|
+
// missing-key warning in the de-opt list renderer); `children` rides through a
|
|
214
|
+
// keyed passthrough component, and the bubble input is simply omitted when the
|
|
215
|
+
// select isn't a form control — these are fixed-position siblings.
|
|
216
|
+
internal_do_not_use_render: (context) => {
|
|
217
|
+
const nodes = [(0, import_octane.createElement)(ValueFragment, { key: "children", children })];
|
|
218
|
+
if (context.isFormControl) {
|
|
219
|
+
nodes.push((0, import_octane.createElement)(BubbleInput, { key: "bubble", __scopeSelect }));
|
|
220
|
+
}
|
|
221
|
+
return nodes;
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
const TRIGGER_NAME = "SelectTrigger";
|
|
226
|
+
function Trigger(props) {
|
|
227
|
+
const slot = (0, import_internal.S)("Select.Trigger");
|
|
228
|
+
const { __scopeSelect, disabled = false, ref: forwardedRef, ...triggerProps } = props ?? {};
|
|
229
|
+
const popperScope = usePopperScope(__scopeSelect, (0, import_internal.subSlot)(slot, "popper"));
|
|
230
|
+
const context = useSelectContext(TRIGGER_NAME, __scopeSelect);
|
|
231
|
+
const isDisabled = context.disabled || disabled;
|
|
232
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(
|
|
233
|
+
forwardedRef,
|
|
234
|
+
context.onTriggerChange,
|
|
235
|
+
(0, import_internal.subSlot)(slot, "refs")
|
|
236
|
+
);
|
|
237
|
+
const getItems = useCollection(__scopeSelect, (0, import_internal.subSlot)(slot, "items"));
|
|
238
|
+
const pointerTypeRef = (0, import_octane.useRef)("touch", (0, import_internal.subSlot)(slot, "pointerType"));
|
|
239
|
+
const [searchRef, handleTypeaheadSearch, resetTypeahead] = useTypeaheadSearch(
|
|
240
|
+
(search) => {
|
|
241
|
+
const enabledItems = getItems().filter((item) => !item.disabled);
|
|
242
|
+
const currentItem = enabledItems.find((item) => item.value === context.value);
|
|
243
|
+
const nextItem = findNextItem(enabledItems, search, currentItem);
|
|
244
|
+
if (nextItem !== void 0) {
|
|
245
|
+
context.onValueChange(nextItem.value);
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
(0, import_internal.subSlot)(slot, "typeahead")
|
|
249
|
+
);
|
|
250
|
+
const handleOpen = (pointerEvent) => {
|
|
251
|
+
if (!isDisabled) {
|
|
252
|
+
context.onOpenChange(true);
|
|
253
|
+
resetTypeahead();
|
|
254
|
+
}
|
|
255
|
+
if (pointerEvent) {
|
|
256
|
+
context.triggerPointerDownPosRef.current = {
|
|
257
|
+
x: Math.round(pointerEvent.pageX),
|
|
258
|
+
y: Math.round(pointerEvent.pageY)
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
return (0, import_octane.createElement)(PopperPrimitive.Anchor, {
|
|
263
|
+
asChild: true,
|
|
264
|
+
...popperScope,
|
|
265
|
+
children: (0, import_octane.createElement)(import_Primitive.Primitive.button, {
|
|
266
|
+
type: "button",
|
|
267
|
+
role: "combobox",
|
|
268
|
+
"aria-controls": context.open ? context.contentId : void 0,
|
|
269
|
+
"aria-expanded": context.open,
|
|
270
|
+
"aria-required": context.required,
|
|
271
|
+
"aria-autocomplete": "none",
|
|
272
|
+
dir: context.dir,
|
|
273
|
+
"data-state": context.open ? "open" : "closed",
|
|
274
|
+
disabled: isDisabled,
|
|
275
|
+
"data-disabled": isDisabled ? "" : void 0,
|
|
276
|
+
"data-placeholder": shouldShowPlaceholder(context.value) ? "" : void 0,
|
|
277
|
+
...triggerProps,
|
|
278
|
+
ref: composedRefs,
|
|
279
|
+
// Enable compatibility with native label or custom `Label` "click" for Safari:
|
|
280
|
+
onClick: (0, import_compose_event_handlers.composeEventHandlers)(triggerProps.onClick, (event) => {
|
|
281
|
+
event.currentTarget.focus();
|
|
282
|
+
if (pointerTypeRef.current !== "mouse") {
|
|
283
|
+
handleOpen(event);
|
|
284
|
+
}
|
|
285
|
+
}),
|
|
286
|
+
onPointerDown: (0, import_compose_event_handlers.composeEventHandlers)(triggerProps.onPointerDown, (event) => {
|
|
287
|
+
pointerTypeRef.current = event.pointerType;
|
|
288
|
+
const target = event.target;
|
|
289
|
+
if (typeof target.hasPointerCapture === "function" && target.hasPointerCapture(event.pointerId)) {
|
|
290
|
+
target.releasePointerCapture(event.pointerId);
|
|
291
|
+
}
|
|
292
|
+
if (event.button === 0 && event.ctrlKey === false && event.pointerType === "mouse") {
|
|
293
|
+
handleOpen(event);
|
|
294
|
+
event.preventDefault();
|
|
295
|
+
}
|
|
296
|
+
}),
|
|
297
|
+
onKeyDown: (0, import_compose_event_handlers.composeEventHandlers)(triggerProps.onKeyDown, (event) => {
|
|
298
|
+
const isTypingAhead = searchRef.current !== "";
|
|
299
|
+
const isModifierKey = event.ctrlKey || event.altKey || event.metaKey;
|
|
300
|
+
if (!isModifierKey && event.key.length === 1) handleTypeaheadSearch(event.key);
|
|
301
|
+
if (isTypingAhead && event.key === " ") return;
|
|
302
|
+
if (OPEN_KEYS.includes(event.key)) {
|
|
303
|
+
handleOpen();
|
|
304
|
+
event.preventDefault();
|
|
305
|
+
}
|
|
306
|
+
})
|
|
307
|
+
})
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
const VALUE_NAME = "SelectValue";
|
|
311
|
+
function Value(props) {
|
|
312
|
+
const slot = (0, import_internal.S)("Select.Value");
|
|
313
|
+
const {
|
|
314
|
+
__scopeSelect,
|
|
315
|
+
className,
|
|
316
|
+
style,
|
|
317
|
+
children,
|
|
318
|
+
placeholder = "",
|
|
319
|
+
ref: forwardedRef,
|
|
320
|
+
...valueProps
|
|
321
|
+
} = props ?? {};
|
|
322
|
+
void className;
|
|
323
|
+
void style;
|
|
324
|
+
const context = useSelectContext(VALUE_NAME, __scopeSelect);
|
|
325
|
+
const { onValueNodeHasChildrenChange } = context;
|
|
326
|
+
const hasChildren = children !== void 0;
|
|
327
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(
|
|
328
|
+
forwardedRef,
|
|
329
|
+
context.onValueNodeChange,
|
|
330
|
+
(0, import_internal.subSlot)(slot, "refs")
|
|
331
|
+
);
|
|
332
|
+
(0, import_octane.useLayoutEffect)(
|
|
333
|
+
() => {
|
|
334
|
+
onValueNodeHasChildrenChange(hasChildren);
|
|
335
|
+
},
|
|
336
|
+
[onValueNodeHasChildrenChange, hasChildren],
|
|
337
|
+
(0, import_internal.subSlot)(slot, "e:hasChildren")
|
|
338
|
+
);
|
|
339
|
+
const showPlaceholder = shouldShowPlaceholder(context.value);
|
|
340
|
+
return (0, import_octane.createElement)(import_Primitive.Primitive.span, {
|
|
341
|
+
...valueProps,
|
|
342
|
+
asChild: showPlaceholder ? false : valueProps.asChild,
|
|
343
|
+
ref: composedRefs,
|
|
344
|
+
// we don't want events from the portalled `SelectValue` children to bubble
|
|
345
|
+
// through the item they came from
|
|
346
|
+
style: { pointerEvents: "none" },
|
|
347
|
+
// The source wraps in `<React.Fragment key={placeholder|value}>`. The component
|
|
348
|
+
// wrapper here doubles as an octane adaptation: it forces the span's children
|
|
349
|
+
// through the BLOCK (marker-delimited) render path, so the ItemText content
|
|
350
|
+
// portal'd into this span from elsewhere isn't swept by the raw host
|
|
351
|
+
// reconciler (see suspected-bug note in the port summary).
|
|
352
|
+
children: (0, import_octane.createElement)(ValueFragment, {
|
|
353
|
+
key: showPlaceholder ? "placeholder" : "value",
|
|
354
|
+
children: showPlaceholder ? placeholder : children
|
|
355
|
+
})
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
function ValueFragment(props) {
|
|
359
|
+
return props?.children ?? null;
|
|
360
|
+
}
|
|
361
|
+
function Icon(props) {
|
|
362
|
+
const { __scopeSelect, children, ...iconProps } = props ?? {};
|
|
363
|
+
return (0, import_octane.createElement)(import_Primitive.Primitive.span, {
|
|
364
|
+
"aria-hidden": true,
|
|
365
|
+
...iconProps,
|
|
366
|
+
children: children || "\u25BC"
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
const PORTAL_NAME = "SelectPortal";
|
|
370
|
+
const [PortalProvider, usePortalContext] = createSelectContext(PORTAL_NAME, {
|
|
371
|
+
forceMount: void 0
|
|
372
|
+
});
|
|
373
|
+
function Portal(props) {
|
|
374
|
+
const { __scopeSelect, forceMount, children, ...portalProps } = props ?? {};
|
|
375
|
+
return (0, import_octane.createElement)(PortalProvider, {
|
|
376
|
+
scope: __scopeSelect,
|
|
377
|
+
forceMount,
|
|
378
|
+
children: (0, import_octane.createElement)(import_Portal.Portal, {
|
|
379
|
+
asChild: typeof children !== "function",
|
|
380
|
+
...portalProps,
|
|
381
|
+
children
|
|
382
|
+
})
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
const CONTENT_NAME = "SelectContent";
|
|
386
|
+
function Content(props) {
|
|
387
|
+
const slot = (0, import_internal.S)("Select.Content");
|
|
388
|
+
const portalContext = usePortalContext(CONTENT_NAME, props?.__scopeSelect);
|
|
389
|
+
const { forceMount = portalContext.forceMount, ref: forwardedRef, ...contentProps } = props ?? {};
|
|
390
|
+
const context = useSelectContext(CONTENT_NAME, props?.__scopeSelect);
|
|
391
|
+
const [fragment, setFragment] = (0, import_octane.useState)(
|
|
392
|
+
void 0,
|
|
393
|
+
(0, import_internal.subSlot)(slot, "fragment")
|
|
394
|
+
);
|
|
395
|
+
(0, import_octane.useLayoutEffect)(
|
|
396
|
+
() => {
|
|
397
|
+
setFragment(new DocumentFragment());
|
|
398
|
+
},
|
|
399
|
+
[],
|
|
400
|
+
(0, import_internal.subSlot)(slot, "e:fragment")
|
|
401
|
+
);
|
|
402
|
+
return (0, import_octane.createElement)(import_Presence.Presence, {
|
|
403
|
+
present: forceMount || context.open,
|
|
404
|
+
children: ({ present }) => present ? (0, import_octane.createElement)(ContentImpl, { ...contentProps, ref: forwardedRef }) : (0, import_octane.createElement)(ContentFragment, { ...contentProps, fragment })
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
function ContentFragment(props) {
|
|
408
|
+
const { __scopeSelect, children, fragment, ref: forwardedRef } = props ?? {};
|
|
409
|
+
if (!fragment) return null;
|
|
410
|
+
return (0, import_octane.createPortal)(
|
|
411
|
+
(0, import_octane.createElement)(SelectContentProvider, {
|
|
412
|
+
scope: __scopeSelect,
|
|
413
|
+
children: (0, import_octane.createElement)(Collection.Slot, {
|
|
414
|
+
scope: __scopeSelect,
|
|
415
|
+
children: (0, import_octane.createElement)("div", { ref: forwardedRef, children })
|
|
416
|
+
})
|
|
417
|
+
}),
|
|
418
|
+
// octane's createPortal target is typed as Element; a detached DocumentFragment
|
|
419
|
+
// works identically (appendChild + EventTarget for delegated listeners).
|
|
420
|
+
fragment
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
const CONTENT_MARGIN = 10;
|
|
424
|
+
const [SelectContentProvider, useSelectContentContext] = createSelectContext(CONTENT_NAME);
|
|
425
|
+
const CONTENT_IMPL_NAME = "SelectContentImpl";
|
|
426
|
+
function ContentImpl(props) {
|
|
427
|
+
const slot = (0, import_internal.S)("Select.ContentImpl");
|
|
428
|
+
const {
|
|
429
|
+
__scopeSelect,
|
|
430
|
+
position = "item-aligned",
|
|
431
|
+
onCloseAutoFocus,
|
|
432
|
+
onEscapeKeyDown,
|
|
433
|
+
onPointerDownOutside,
|
|
434
|
+
//
|
|
435
|
+
// PopperContent props
|
|
436
|
+
side,
|
|
437
|
+
sideOffset,
|
|
438
|
+
align,
|
|
439
|
+
alignOffset,
|
|
440
|
+
arrowPadding,
|
|
441
|
+
collisionBoundary,
|
|
442
|
+
collisionPadding,
|
|
443
|
+
sticky,
|
|
444
|
+
hideWhenDetached,
|
|
445
|
+
avoidCollisions,
|
|
446
|
+
//
|
|
447
|
+
ref: forwardedRef,
|
|
448
|
+
...contentProps
|
|
449
|
+
} = props;
|
|
450
|
+
const context = useSelectContext(CONTENT_NAME, __scopeSelect);
|
|
451
|
+
const [content, setContent] = (0, import_octane.useState)(null, (0, import_internal.subSlot)(slot, "content"));
|
|
452
|
+
const [viewport, setViewport] = (0, import_octane.useState)(null, (0, import_internal.subSlot)(slot, "viewport"));
|
|
453
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, setContent, (0, import_internal.subSlot)(slot, "refs"));
|
|
454
|
+
const [selectedItem, setSelectedItem] = (0, import_octane.useState)(
|
|
455
|
+
null,
|
|
456
|
+
(0, import_internal.subSlot)(slot, "selItem")
|
|
457
|
+
);
|
|
458
|
+
const [selectedItemText, setSelectedItemText] = (0, import_octane.useState)(
|
|
459
|
+
null,
|
|
460
|
+
(0, import_internal.subSlot)(slot, "selText")
|
|
461
|
+
);
|
|
462
|
+
const getItems = useCollection(__scopeSelect, (0, import_internal.subSlot)(slot, "items"));
|
|
463
|
+
const [isPositioned, setIsPositioned] = (0, import_octane.useState)(false, (0, import_internal.subSlot)(slot, "positioned"));
|
|
464
|
+
const firstValidItemFoundRef = (0, import_octane.useRef)(false, (0, import_internal.subSlot)(slot, "firstValid"));
|
|
465
|
+
(0, import_octane.useEffect)(
|
|
466
|
+
() => {
|
|
467
|
+
if (content) return (0, import_aria_hidden.hideOthers)(content);
|
|
468
|
+
},
|
|
469
|
+
[content],
|
|
470
|
+
(0, import_internal.subSlot)(slot, "e:hide")
|
|
471
|
+
);
|
|
472
|
+
(0, import_focus_guards.useFocusGuards)((0, import_internal.subSlot)(slot, "guards"));
|
|
473
|
+
const focusFirst = (0, import_octane.useCallback)(
|
|
474
|
+
(candidates) => {
|
|
475
|
+
const [firstItem, ...restItems] = getItems().map((item) => item.ref.current);
|
|
476
|
+
const [lastItem] = restItems.slice(-1);
|
|
477
|
+
const PREVIOUSLY_FOCUSED_ELEMENT = document.activeElement;
|
|
478
|
+
for (const candidate of candidates) {
|
|
479
|
+
if (candidate === PREVIOUSLY_FOCUSED_ELEMENT) return;
|
|
480
|
+
candidate?.scrollIntoView?.({ block: "nearest" });
|
|
481
|
+
if (candidate === firstItem && viewport) viewport.scrollTop = 0;
|
|
482
|
+
if (candidate === lastItem && viewport) viewport.scrollTop = viewport.scrollHeight;
|
|
483
|
+
candidate?.focus();
|
|
484
|
+
if (document.activeElement !== PREVIOUSLY_FOCUSED_ELEMENT) return;
|
|
485
|
+
}
|
|
486
|
+
},
|
|
487
|
+
[getItems, viewport],
|
|
488
|
+
(0, import_internal.subSlot)(slot, "focusFirst")
|
|
489
|
+
);
|
|
490
|
+
const focusSelectedItem = (0, import_octane.useCallback)(
|
|
491
|
+
() => focusFirst([selectedItem, content]),
|
|
492
|
+
[focusFirst, selectedItem, content],
|
|
493
|
+
(0, import_internal.subSlot)(slot, "focusSel")
|
|
494
|
+
);
|
|
495
|
+
(0, import_octane.useEffect)(
|
|
496
|
+
() => {
|
|
497
|
+
if (isPositioned) {
|
|
498
|
+
focusSelectedItem();
|
|
499
|
+
}
|
|
500
|
+
},
|
|
501
|
+
[isPositioned, focusSelectedItem],
|
|
502
|
+
(0, import_internal.subSlot)(slot, "e:focus")
|
|
503
|
+
);
|
|
504
|
+
const { onOpenChange, triggerPointerDownPosRef } = context;
|
|
505
|
+
(0, import_octane.useEffect)(
|
|
506
|
+
() => {
|
|
507
|
+
if (content) {
|
|
508
|
+
let pointerMoveDelta = { x: 0, y: 0 };
|
|
509
|
+
const handlePointerMove = (event) => {
|
|
510
|
+
pointerMoveDelta = {
|
|
511
|
+
x: Math.abs(Math.round(event.pageX) - (triggerPointerDownPosRef.current?.x ?? 0)),
|
|
512
|
+
y: Math.abs(Math.round(event.pageY) - (triggerPointerDownPosRef.current?.y ?? 0))
|
|
513
|
+
};
|
|
514
|
+
};
|
|
515
|
+
const handlePointerUp = (event) => {
|
|
516
|
+
if (pointerMoveDelta.x <= 10 && pointerMoveDelta.y <= 10) {
|
|
517
|
+
event.preventDefault();
|
|
518
|
+
} else {
|
|
519
|
+
if (!event.composedPath().includes(content)) {
|
|
520
|
+
onOpenChange(false);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
document.removeEventListener("pointermove", handlePointerMove);
|
|
524
|
+
triggerPointerDownPosRef.current = null;
|
|
525
|
+
};
|
|
526
|
+
if (triggerPointerDownPosRef.current !== null) {
|
|
527
|
+
document.addEventListener("pointermove", handlePointerMove);
|
|
528
|
+
document.addEventListener("pointerup", handlePointerUp, { capture: true, once: true });
|
|
529
|
+
}
|
|
530
|
+
return () => {
|
|
531
|
+
document.removeEventListener("pointermove", handlePointerMove);
|
|
532
|
+
document.removeEventListener("pointerup", handlePointerUp, { capture: true });
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
},
|
|
536
|
+
[content, onOpenChange, triggerPointerDownPosRef],
|
|
537
|
+
(0, import_internal.subSlot)(slot, "e:pointer")
|
|
538
|
+
);
|
|
539
|
+
(0, import_octane.useEffect)(
|
|
540
|
+
() => {
|
|
541
|
+
const close = () => onOpenChange(false);
|
|
542
|
+
window.addEventListener("blur", close);
|
|
543
|
+
window.addEventListener("resize", close);
|
|
544
|
+
return () => {
|
|
545
|
+
window.removeEventListener("blur", close);
|
|
546
|
+
window.removeEventListener("resize", close);
|
|
547
|
+
};
|
|
548
|
+
},
|
|
549
|
+
[onOpenChange],
|
|
550
|
+
(0, import_internal.subSlot)(slot, "e:close")
|
|
551
|
+
);
|
|
552
|
+
const [searchRef, handleTypeaheadSearch] = useTypeaheadSearch(
|
|
553
|
+
(search) => {
|
|
554
|
+
const enabledItems = getItems().filter((item) => !item.disabled);
|
|
555
|
+
const currentItem = enabledItems.find(
|
|
556
|
+
(item) => item.ref.current === document.activeElement
|
|
557
|
+
);
|
|
558
|
+
const nextItem = findNextItem(enabledItems, search, currentItem);
|
|
559
|
+
if (nextItem) {
|
|
560
|
+
setTimeout(() => nextItem.ref.current?.focus());
|
|
561
|
+
}
|
|
562
|
+
},
|
|
563
|
+
(0, import_internal.subSlot)(slot, "typeahead")
|
|
564
|
+
);
|
|
565
|
+
const itemRefCallback = (0, import_octane.useCallback)(
|
|
566
|
+
(node, value, disabled) => {
|
|
567
|
+
const isFirstValidItem = !firstValidItemFoundRef.current && !disabled;
|
|
568
|
+
const isSelectedItem = context.value !== void 0 && context.value === value;
|
|
569
|
+
if (isSelectedItem || isFirstValidItem) {
|
|
570
|
+
setSelectedItem(node);
|
|
571
|
+
if (isFirstValidItem) firstValidItemFoundRef.current = true;
|
|
572
|
+
}
|
|
573
|
+
},
|
|
574
|
+
[context.value],
|
|
575
|
+
(0, import_internal.subSlot)(slot, "itemRef")
|
|
576
|
+
);
|
|
577
|
+
const handleItemLeave = (0, import_octane.useCallback)(
|
|
578
|
+
() => content?.focus(),
|
|
579
|
+
[content],
|
|
580
|
+
(0, import_internal.subSlot)(slot, "itemLeave")
|
|
581
|
+
);
|
|
582
|
+
const itemTextRefCallback = (0, import_octane.useCallback)(
|
|
583
|
+
(node, value, disabled) => {
|
|
584
|
+
const isFirstValidItem = !firstValidItemFoundRef.current && !disabled;
|
|
585
|
+
const isSelectedItem = context.value !== void 0 && context.value === value;
|
|
586
|
+
if (isSelectedItem || isFirstValidItem) {
|
|
587
|
+
setSelectedItemText(node);
|
|
588
|
+
}
|
|
589
|
+
},
|
|
590
|
+
[context.value],
|
|
591
|
+
(0, import_internal.subSlot)(slot, "itemTextRef")
|
|
592
|
+
);
|
|
593
|
+
(0, import_scroll_lock.useScrollLock)(true, (0, import_internal.subSlot)(slot, "lock"));
|
|
594
|
+
const SelectPosition = position === "popper" ? PopperPosition : ItemAlignedPosition;
|
|
595
|
+
const popperContentProps = SelectPosition === PopperPosition ? {
|
|
596
|
+
side,
|
|
597
|
+
sideOffset,
|
|
598
|
+
align,
|
|
599
|
+
alignOffset,
|
|
600
|
+
arrowPadding,
|
|
601
|
+
collisionBoundary,
|
|
602
|
+
collisionPadding,
|
|
603
|
+
sticky,
|
|
604
|
+
hideWhenDetached,
|
|
605
|
+
avoidCollisions
|
|
606
|
+
} : {};
|
|
607
|
+
return (0, import_octane.createElement)(SelectContentProvider, {
|
|
608
|
+
scope: __scopeSelect,
|
|
609
|
+
content,
|
|
610
|
+
viewport,
|
|
611
|
+
onViewportChange: setViewport,
|
|
612
|
+
itemRefCallback,
|
|
613
|
+
selectedItem,
|
|
614
|
+
onItemLeave: handleItemLeave,
|
|
615
|
+
itemTextRefCallback,
|
|
616
|
+
focusSelectedItem,
|
|
617
|
+
selectedItemText,
|
|
618
|
+
position,
|
|
619
|
+
isPositioned,
|
|
620
|
+
searchRef,
|
|
621
|
+
children: (0, import_octane.createElement)(import_FocusScope.FocusScope, {
|
|
622
|
+
asChild: true,
|
|
623
|
+
// we make sure we're not trapping once it's been closed
|
|
624
|
+
// (closed !== unmounted when animating out)
|
|
625
|
+
trapped: context.open,
|
|
626
|
+
onMountAutoFocus: (event) => {
|
|
627
|
+
event.preventDefault();
|
|
628
|
+
},
|
|
629
|
+
onUnmountAutoFocus: (0, import_compose_event_handlers.composeEventHandlers)(onCloseAutoFocus, (event) => {
|
|
630
|
+
context.trigger?.focus({ preventScroll: true });
|
|
631
|
+
event.preventDefault();
|
|
632
|
+
}),
|
|
633
|
+
children: (0, import_octane.createElement)(import_DismissableLayer.DismissableLayer, {
|
|
634
|
+
asChild: true,
|
|
635
|
+
disableOutsidePointerEvents: true,
|
|
636
|
+
onEscapeKeyDown,
|
|
637
|
+
onPointerDownOutside,
|
|
638
|
+
// When focus is trapped, a focusout event may still happen.
|
|
639
|
+
// We make sure we don't trigger our `onDismiss` in such case.
|
|
640
|
+
onFocusOutside: (event) => event.preventDefault(),
|
|
641
|
+
onDismiss: () => context.onOpenChange(false),
|
|
642
|
+
children: (0, import_octane.createElement)(SelectPosition, {
|
|
643
|
+
role: "listbox",
|
|
644
|
+
id: context.contentId,
|
|
645
|
+
"data-state": context.open ? "open" : "closed",
|
|
646
|
+
dir: context.dir,
|
|
647
|
+
onContextMenu: (event) => event.preventDefault(),
|
|
648
|
+
...contentProps,
|
|
649
|
+
...popperContentProps,
|
|
650
|
+
__scopeSelect,
|
|
651
|
+
onPlaced: () => setIsPositioned(true),
|
|
652
|
+
ref: composedRefs,
|
|
653
|
+
style: {
|
|
654
|
+
// flex layout so we can place the scroll buttons properly
|
|
655
|
+
display: "flex",
|
|
656
|
+
flexDirection: "column",
|
|
657
|
+
// reset the outline by default as the content MAY get focused
|
|
658
|
+
outline: "none",
|
|
659
|
+
...contentProps.style
|
|
660
|
+
},
|
|
661
|
+
onKeyDown: (0, import_compose_event_handlers.composeEventHandlers)(contentProps.onKeyDown, (event) => {
|
|
662
|
+
const isModifierKey = event.ctrlKey || event.altKey || event.metaKey;
|
|
663
|
+
if (event.key === "Tab") event.preventDefault();
|
|
664
|
+
if (!isModifierKey && event.key.length === 1) handleTypeaheadSearch(event.key);
|
|
665
|
+
if (["ArrowUp", "ArrowDown", "Home", "End"].includes(event.key)) {
|
|
666
|
+
const items = getItems().filter((item) => !item.disabled);
|
|
667
|
+
let candidateNodes = items.map((item) => item.ref.current);
|
|
668
|
+
if (["ArrowUp", "End"].includes(event.key)) {
|
|
669
|
+
candidateNodes = candidateNodes.slice().reverse();
|
|
670
|
+
}
|
|
671
|
+
if (["ArrowUp", "ArrowDown"].includes(event.key)) {
|
|
672
|
+
const currentElement = event.target;
|
|
673
|
+
const currentIndex = candidateNodes.indexOf(currentElement);
|
|
674
|
+
candidateNodes = candidateNodes.slice(currentIndex + 1);
|
|
675
|
+
}
|
|
676
|
+
setTimeout(() => focusFirst(candidateNodes));
|
|
677
|
+
event.preventDefault();
|
|
678
|
+
}
|
|
679
|
+
})
|
|
680
|
+
})
|
|
681
|
+
})
|
|
682
|
+
})
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
const ITEM_ALIGNED_POSITION_NAME = "SelectItemAlignedPosition";
|
|
686
|
+
function ItemAlignedPosition(props) {
|
|
687
|
+
const slot = (0, import_internal.S)("Select.ItemAlignedPosition");
|
|
688
|
+
const { __scopeSelect, onPlaced, ref: forwardedRef, ...popperProps } = props;
|
|
689
|
+
const context = useSelectContext(CONTENT_NAME, __scopeSelect);
|
|
690
|
+
const contentContext = useSelectContentContext(CONTENT_NAME, __scopeSelect);
|
|
691
|
+
const [contentWrapper, setContentWrapper] = (0, import_octane.useState)(
|
|
692
|
+
null,
|
|
693
|
+
(0, import_internal.subSlot)(slot, "wrapper")
|
|
694
|
+
);
|
|
695
|
+
const [content, setContent] = (0, import_octane.useState)(null, (0, import_internal.subSlot)(slot, "content"));
|
|
696
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, setContent, (0, import_internal.subSlot)(slot, "refs"));
|
|
697
|
+
const getItems = useCollection(__scopeSelect, (0, import_internal.subSlot)(slot, "items"));
|
|
698
|
+
const shouldExpandOnScrollRef = (0, import_octane.useRef)(false, (0, import_internal.subSlot)(slot, "expand"));
|
|
699
|
+
const shouldRepositionRef = (0, import_octane.useRef)(true, (0, import_internal.subSlot)(slot, "reposition"));
|
|
700
|
+
const { viewport, selectedItem, selectedItemText, focusSelectedItem } = contentContext;
|
|
701
|
+
const position = (0, import_octane.useCallback)(
|
|
702
|
+
() => {
|
|
703
|
+
if (context.trigger && context.valueNode && contentWrapper && content && viewport && selectedItem && selectedItemText) {
|
|
704
|
+
const triggerRect = context.trigger.getBoundingClientRect();
|
|
705
|
+
const contentRect = content.getBoundingClientRect();
|
|
706
|
+
const valueNodeRect = context.valueNode.getBoundingClientRect();
|
|
707
|
+
const itemTextRect = selectedItemText.getBoundingClientRect();
|
|
708
|
+
if (context.dir !== "rtl") {
|
|
709
|
+
const itemTextOffset = itemTextRect.left - contentRect.left;
|
|
710
|
+
const left = valueNodeRect.left - itemTextOffset;
|
|
711
|
+
const leftDelta = triggerRect.left - left;
|
|
712
|
+
const minContentWidth = triggerRect.width + leftDelta;
|
|
713
|
+
const contentWidth = Math.max(minContentWidth, contentRect.width);
|
|
714
|
+
const rightEdge = window.innerWidth - CONTENT_MARGIN;
|
|
715
|
+
const clampedLeft = clamp(left, [
|
|
716
|
+
CONTENT_MARGIN,
|
|
717
|
+
// Prevents the content from going off the starting edge of the
|
|
718
|
+
// viewport. It may still go off the ending edge, but this can be
|
|
719
|
+
// controlled by the user since they may want to manage overflow in a
|
|
720
|
+
// specific way.
|
|
721
|
+
// https://github.com/radix-ui/primitives/issues/2049
|
|
722
|
+
Math.max(CONTENT_MARGIN, rightEdge - contentWidth)
|
|
723
|
+
]);
|
|
724
|
+
contentWrapper.style.minWidth = minContentWidth + "px";
|
|
725
|
+
contentWrapper.style.left = clampedLeft + "px";
|
|
726
|
+
} else {
|
|
727
|
+
const itemTextOffset = contentRect.right - itemTextRect.right;
|
|
728
|
+
const right = window.innerWidth - valueNodeRect.right - itemTextOffset;
|
|
729
|
+
const rightDelta = window.innerWidth - triggerRect.right - right;
|
|
730
|
+
const minContentWidth = triggerRect.width + rightDelta;
|
|
731
|
+
const contentWidth = Math.max(minContentWidth, contentRect.width);
|
|
732
|
+
const leftEdge = window.innerWidth - CONTENT_MARGIN;
|
|
733
|
+
const clampedRight = clamp(right, [
|
|
734
|
+
CONTENT_MARGIN,
|
|
735
|
+
Math.max(CONTENT_MARGIN, leftEdge - contentWidth)
|
|
736
|
+
]);
|
|
737
|
+
contentWrapper.style.minWidth = minContentWidth + "px";
|
|
738
|
+
contentWrapper.style.right = clampedRight + "px";
|
|
739
|
+
}
|
|
740
|
+
const items = getItems();
|
|
741
|
+
const availableHeight = window.innerHeight - CONTENT_MARGIN * 2;
|
|
742
|
+
const itemsHeight = viewport.scrollHeight;
|
|
743
|
+
const contentStyles = window.getComputedStyle(content);
|
|
744
|
+
const contentBorderTopWidth = parseInt(contentStyles.borderTopWidth, 10);
|
|
745
|
+
const contentPaddingTop = parseInt(contentStyles.paddingTop, 10);
|
|
746
|
+
const contentBorderBottomWidth = parseInt(contentStyles.borderBottomWidth, 10);
|
|
747
|
+
const contentPaddingBottom = parseInt(contentStyles.paddingBottom, 10);
|
|
748
|
+
const fullContentHeight = contentBorderTopWidth + contentPaddingTop + itemsHeight + contentPaddingBottom + contentBorderBottomWidth;
|
|
749
|
+
const minContentHeight = Math.min(selectedItem.offsetHeight * 5, fullContentHeight);
|
|
750
|
+
const viewportStyles = window.getComputedStyle(viewport);
|
|
751
|
+
const viewportPaddingTop = parseInt(viewportStyles.paddingTop, 10);
|
|
752
|
+
const viewportPaddingBottom = parseInt(viewportStyles.paddingBottom, 10);
|
|
753
|
+
const topEdgeToTriggerMiddle = triggerRect.top + triggerRect.height / 2 - CONTENT_MARGIN;
|
|
754
|
+
const triggerMiddleToBottomEdge = availableHeight - topEdgeToTriggerMiddle;
|
|
755
|
+
const selectedItemHalfHeight = selectedItem.offsetHeight / 2;
|
|
756
|
+
const itemOffsetMiddle = selectedItem.offsetTop + selectedItemHalfHeight;
|
|
757
|
+
const contentTopToItemMiddle = contentBorderTopWidth + contentPaddingTop + itemOffsetMiddle;
|
|
758
|
+
const itemMiddleToContentBottom = fullContentHeight - contentTopToItemMiddle;
|
|
759
|
+
const willAlignWithoutTopOverflow = contentTopToItemMiddle <= topEdgeToTriggerMiddle;
|
|
760
|
+
if (willAlignWithoutTopOverflow) {
|
|
761
|
+
const isLastItem = items.length > 0 && selectedItem === items[items.length - 1].ref.current;
|
|
762
|
+
contentWrapper.style.bottom = "0px";
|
|
763
|
+
const viewportOffsetBottom = content.clientHeight - viewport.offsetTop - viewport.offsetHeight;
|
|
764
|
+
const clampedTriggerMiddleToBottomEdge = Math.max(
|
|
765
|
+
triggerMiddleToBottomEdge,
|
|
766
|
+
selectedItemHalfHeight + // viewport might have padding bottom, include it to avoid a scrollable viewport
|
|
767
|
+
(isLastItem ? viewportPaddingBottom : 0) + viewportOffsetBottom + contentBorderBottomWidth
|
|
768
|
+
);
|
|
769
|
+
const height = contentTopToItemMiddle + clampedTriggerMiddleToBottomEdge;
|
|
770
|
+
contentWrapper.style.height = height + "px";
|
|
771
|
+
} else {
|
|
772
|
+
const isFirstItem = items.length > 0 && selectedItem === items[0].ref.current;
|
|
773
|
+
contentWrapper.style.top = "0px";
|
|
774
|
+
const clampedTopEdgeToTriggerMiddle = Math.max(
|
|
775
|
+
topEdgeToTriggerMiddle,
|
|
776
|
+
contentBorderTopWidth + viewport.offsetTop + // viewport might have padding top, include it to avoid a scrollable viewport
|
|
777
|
+
(isFirstItem ? viewportPaddingTop : 0) + selectedItemHalfHeight
|
|
778
|
+
);
|
|
779
|
+
const height = clampedTopEdgeToTriggerMiddle + itemMiddleToContentBottom;
|
|
780
|
+
contentWrapper.style.height = height + "px";
|
|
781
|
+
viewport.scrollTop = contentTopToItemMiddle - topEdgeToTriggerMiddle + viewport.offsetTop;
|
|
782
|
+
}
|
|
783
|
+
contentWrapper.style.margin = `${CONTENT_MARGIN}px 0`;
|
|
784
|
+
contentWrapper.style.minHeight = minContentHeight + "px";
|
|
785
|
+
contentWrapper.style.maxHeight = availableHeight + "px";
|
|
786
|
+
onPlaced?.();
|
|
787
|
+
requestAnimationFrame(() => shouldExpandOnScrollRef.current = true);
|
|
788
|
+
}
|
|
789
|
+
},
|
|
790
|
+
[
|
|
791
|
+
getItems,
|
|
792
|
+
context.trigger,
|
|
793
|
+
context.valueNode,
|
|
794
|
+
contentWrapper,
|
|
795
|
+
content,
|
|
796
|
+
viewport,
|
|
797
|
+
selectedItem,
|
|
798
|
+
selectedItemText,
|
|
799
|
+
context.dir,
|
|
800
|
+
onPlaced
|
|
801
|
+
],
|
|
802
|
+
(0, import_internal.subSlot)(slot, "position")
|
|
803
|
+
);
|
|
804
|
+
(0, import_octane.useLayoutEffect)(() => position(), [position], (0, import_internal.subSlot)(slot, "e:position"));
|
|
805
|
+
const [contentZIndex, setContentZIndex] = (0, import_octane.useState)(
|
|
806
|
+
void 0,
|
|
807
|
+
(0, import_internal.subSlot)(slot, "zIndex")
|
|
808
|
+
);
|
|
809
|
+
(0, import_octane.useLayoutEffect)(
|
|
810
|
+
() => {
|
|
811
|
+
if (content) setContentZIndex(window.getComputedStyle(content).zIndex);
|
|
812
|
+
},
|
|
813
|
+
[content],
|
|
814
|
+
(0, import_internal.subSlot)(slot, "e:zIndex")
|
|
815
|
+
);
|
|
816
|
+
const handleScrollButtonChange = (0, import_octane.useCallback)(
|
|
817
|
+
(node) => {
|
|
818
|
+
if (node && shouldRepositionRef.current === true) {
|
|
819
|
+
position();
|
|
820
|
+
focusSelectedItem?.();
|
|
821
|
+
shouldRepositionRef.current = false;
|
|
822
|
+
}
|
|
823
|
+
},
|
|
824
|
+
[position, focusSelectedItem],
|
|
825
|
+
(0, import_internal.subSlot)(slot, "scrollBtn")
|
|
826
|
+
);
|
|
827
|
+
return (0, import_octane.createElement)(SelectViewportProvider, {
|
|
828
|
+
scope: __scopeSelect,
|
|
829
|
+
contentWrapper,
|
|
830
|
+
shouldExpandOnScrollRef,
|
|
831
|
+
onScrollButtonChange: handleScrollButtonChange,
|
|
832
|
+
children: (0, import_octane.createElement)("div", {
|
|
833
|
+
ref: setContentWrapper,
|
|
834
|
+
style: {
|
|
835
|
+
display: "flex",
|
|
836
|
+
flexDirection: "column",
|
|
837
|
+
position: "fixed",
|
|
838
|
+
zIndex: contentZIndex
|
|
839
|
+
},
|
|
840
|
+
children: (0, import_octane.createElement)(import_Primitive.Primitive.div, {
|
|
841
|
+
...popperProps,
|
|
842
|
+
ref: composedRefs,
|
|
843
|
+
style: {
|
|
844
|
+
// When we get the height of the content, it includes borders. If we were to set
|
|
845
|
+
// the height without having `boxSizing: 'border-box'` it would be too big.
|
|
846
|
+
boxSizing: "border-box",
|
|
847
|
+
// We need to ensure the content doesn't get taller than the wrapper
|
|
848
|
+
maxHeight: "100%",
|
|
849
|
+
...popperProps.style
|
|
850
|
+
}
|
|
851
|
+
})
|
|
852
|
+
})
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
function PopperPosition(props) {
|
|
856
|
+
const slot = (0, import_internal.S)("Select.PopperPosition");
|
|
857
|
+
const {
|
|
858
|
+
__scopeSelect,
|
|
859
|
+
align = "start",
|
|
860
|
+
collisionPadding = CONTENT_MARGIN,
|
|
861
|
+
...popperProps
|
|
862
|
+
} = props ?? {};
|
|
863
|
+
const popperScope = usePopperScope(__scopeSelect, (0, import_internal.subSlot)(slot, "popper"));
|
|
864
|
+
return (0, import_octane.createElement)(PopperPrimitive.Content, {
|
|
865
|
+
...popperScope,
|
|
866
|
+
...popperProps,
|
|
867
|
+
align,
|
|
868
|
+
collisionPadding,
|
|
869
|
+
style: {
|
|
870
|
+
// Ensure border-box for floating-ui calculations
|
|
871
|
+
boxSizing: "border-box",
|
|
872
|
+
...popperProps.style,
|
|
873
|
+
// re-namespace exposed content custom properties
|
|
874
|
+
"--radix-select-content-transform-origin": "var(--radix-popper-transform-origin)",
|
|
875
|
+
"--radix-select-content-available-width": "var(--radix-popper-available-width)",
|
|
876
|
+
"--radix-select-content-available-height": "var(--radix-popper-available-height)",
|
|
877
|
+
"--radix-select-trigger-width": "var(--radix-popper-anchor-width)",
|
|
878
|
+
"--radix-select-trigger-height": "var(--radix-popper-anchor-height)"
|
|
879
|
+
}
|
|
880
|
+
});
|
|
881
|
+
}
|
|
882
|
+
const [SelectViewportProvider, useSelectViewportContext] = createSelectContext(CONTENT_NAME, {});
|
|
883
|
+
const VIEWPORT_NAME = "SelectViewport";
|
|
884
|
+
function Viewport(props) {
|
|
885
|
+
const slot = (0, import_internal.S)("Select.Viewport");
|
|
886
|
+
const { __scopeSelect, nonce, ref: forwardedRef, onScroll, ...viewportProps } = props ?? {};
|
|
887
|
+
const contentContext = useSelectContentContext(VIEWPORT_NAME, __scopeSelect);
|
|
888
|
+
const viewportContext = useSelectViewportContext(VIEWPORT_NAME, __scopeSelect);
|
|
889
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(
|
|
890
|
+
forwardedRef,
|
|
891
|
+
contentContext.onViewportChange,
|
|
892
|
+
(0, import_internal.subSlot)(slot, "refs")
|
|
893
|
+
);
|
|
894
|
+
const prevScrollTopRef = (0, import_octane.useRef)(0, (0, import_internal.subSlot)(slot, "prevScroll"));
|
|
895
|
+
const handleScroll = (0, import_compose_event_handlers.composeEventHandlers)(onScroll, (event) => {
|
|
896
|
+
const viewport = event.currentTarget;
|
|
897
|
+
const { contentWrapper, shouldExpandOnScrollRef } = viewportContext;
|
|
898
|
+
if (shouldExpandOnScrollRef?.current && contentWrapper) {
|
|
899
|
+
const scrolledBy = Math.abs(prevScrollTopRef.current - viewport.scrollTop);
|
|
900
|
+
if (scrolledBy > 0) {
|
|
901
|
+
const availableHeight = window.innerHeight - CONTENT_MARGIN * 2;
|
|
902
|
+
const cssMinHeight = parseFloat(contentWrapper.style.minHeight);
|
|
903
|
+
const cssHeight = parseFloat(contentWrapper.style.height);
|
|
904
|
+
const prevHeight = Math.max(cssMinHeight, cssHeight);
|
|
905
|
+
if (prevHeight < availableHeight) {
|
|
906
|
+
const nextHeight = prevHeight + scrolledBy;
|
|
907
|
+
const clampedNextHeight = Math.min(availableHeight, nextHeight);
|
|
908
|
+
const heightDiff = nextHeight - clampedNextHeight;
|
|
909
|
+
contentWrapper.style.height = clampedNextHeight + "px";
|
|
910
|
+
if (contentWrapper.style.bottom === "0px") {
|
|
911
|
+
viewport.scrollTop = heightDiff > 0 ? heightDiff : 0;
|
|
912
|
+
contentWrapper.style.justifyContent = "flex-end";
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
prevScrollTopRef.current = viewport.scrollTop;
|
|
918
|
+
});
|
|
919
|
+
return [
|
|
920
|
+
// Hide scrollbars cross-browser and enable momentum scroll for touch devices
|
|
921
|
+
(0, import_octane.createElement)("style", {
|
|
922
|
+
key: "style",
|
|
923
|
+
dangerouslySetInnerHTML: {
|
|
924
|
+
__html: `[data-radix-select-viewport]{scrollbar-width:none;-ms-overflow-style:none;-webkit-overflow-scrolling:touch;}[data-radix-select-viewport]::-webkit-scrollbar{display:none}`
|
|
925
|
+
},
|
|
926
|
+
nonce
|
|
927
|
+
}),
|
|
928
|
+
(0, import_octane.createElement)(Collection.Slot, {
|
|
929
|
+
key: "slot",
|
|
930
|
+
scope: __scopeSelect,
|
|
931
|
+
children: (0, import_octane.createElement)(import_Primitive.Primitive.div, {
|
|
932
|
+
"data-radix-select-viewport": "",
|
|
933
|
+
role: "presentation",
|
|
934
|
+
...viewportProps,
|
|
935
|
+
ref: composedRefs,
|
|
936
|
+
onScroll: handleScroll,
|
|
937
|
+
style: {
|
|
938
|
+
// we use position: 'relative' here on the `viewport` so that when we call
|
|
939
|
+
// `selectedItem.offsetTop` in calculations, the offset is relative to the viewport
|
|
940
|
+
// (independent of the scrollUpButton).
|
|
941
|
+
position: "relative",
|
|
942
|
+
flex: 1,
|
|
943
|
+
// Viewport should only be scrollable in the vertical direction.
|
|
944
|
+
// This won't work in vertical writing modes, so we'll need to
|
|
945
|
+
// revisit this if/when that is supported
|
|
946
|
+
// https://developer.chrome.com/blog/vertical-form-controls
|
|
947
|
+
overflow: "hidden auto",
|
|
948
|
+
...viewportProps.style
|
|
949
|
+
}
|
|
950
|
+
})
|
|
951
|
+
})
|
|
952
|
+
];
|
|
953
|
+
}
|
|
954
|
+
const GROUP_NAME = "SelectGroup";
|
|
955
|
+
const [SelectGroupContextProvider, useSelectGroupContext] = createSelectContext(GROUP_NAME);
|
|
956
|
+
function Group(props) {
|
|
957
|
+
const slot = (0, import_internal.S)("Select.Group");
|
|
958
|
+
const { __scopeSelect, ...groupProps } = props ?? {};
|
|
959
|
+
const groupId = (0, import_useId.useId)((0, import_internal.subSlot)(slot, "id"));
|
|
960
|
+
return (0, import_octane.createElement)(SelectGroupContextProvider, {
|
|
961
|
+
scope: __scopeSelect,
|
|
962
|
+
id: groupId,
|
|
963
|
+
children: (0, import_octane.createElement)(import_Primitive.Primitive.div, {
|
|
964
|
+
role: "group",
|
|
965
|
+
"aria-labelledby": groupId,
|
|
966
|
+
...groupProps
|
|
967
|
+
})
|
|
968
|
+
});
|
|
969
|
+
}
|
|
970
|
+
const LABEL_NAME = "SelectLabel";
|
|
971
|
+
function Label(props) {
|
|
972
|
+
const { __scopeSelect, ...labelProps } = props ?? {};
|
|
973
|
+
const groupContext = useSelectGroupContext(LABEL_NAME, __scopeSelect);
|
|
974
|
+
return (0, import_octane.createElement)(import_Primitive.Primitive.div, { id: groupContext.id, ...labelProps });
|
|
975
|
+
}
|
|
976
|
+
const ITEM_NAME = "SelectItem";
|
|
977
|
+
const [SelectItemContextProvider, useSelectItemContext] = createSelectContext(ITEM_NAME);
|
|
978
|
+
function Item(props) {
|
|
979
|
+
const slot = (0, import_internal.S)("Select.Item");
|
|
980
|
+
const {
|
|
981
|
+
__scopeSelect,
|
|
982
|
+
value,
|
|
983
|
+
disabled = false,
|
|
984
|
+
textValue: textValueProp,
|
|
985
|
+
ref: forwardedRef,
|
|
986
|
+
...itemProps
|
|
987
|
+
} = props ?? {};
|
|
988
|
+
const context = useSelectContext(ITEM_NAME, __scopeSelect);
|
|
989
|
+
const contentContext = useSelectContentContext(ITEM_NAME, __scopeSelect);
|
|
990
|
+
const isSelected = context.value === value;
|
|
991
|
+
const [textValue, setTextValue] = (0, import_octane.useState)(textValueProp ?? "", (0, import_internal.subSlot)(slot, "textValue"));
|
|
992
|
+
const [isFocused, setIsFocused] = (0, import_octane.useState)(false, (0, import_internal.subSlot)(slot, "focused"));
|
|
993
|
+
const handleItemRefCallback = (0, import_use_callback_ref.useCallbackRef)(
|
|
994
|
+
(node) => contentContext.itemRefCallback?.(node, value, disabled),
|
|
995
|
+
(0, import_internal.subSlot)(slot, "refCb")
|
|
996
|
+
);
|
|
997
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, handleItemRefCallback, (0, import_internal.subSlot)(slot, "refs"));
|
|
998
|
+
const textId = (0, import_useId.useId)((0, import_internal.subSlot)(slot, "textId"));
|
|
999
|
+
const pointerTypeRef = (0, import_octane.useRef)("touch", (0, import_internal.subSlot)(slot, "pointerType"));
|
|
1000
|
+
const handleSelect = () => {
|
|
1001
|
+
if (!disabled) {
|
|
1002
|
+
context.onValueChange(value);
|
|
1003
|
+
context.onOpenChange(false);
|
|
1004
|
+
}
|
|
1005
|
+
};
|
|
1006
|
+
return (0, import_octane.createElement)(SelectItemContextProvider, {
|
|
1007
|
+
scope: __scopeSelect,
|
|
1008
|
+
value,
|
|
1009
|
+
disabled,
|
|
1010
|
+
textId,
|
|
1011
|
+
isSelected,
|
|
1012
|
+
onItemTextChange: (0, import_octane.useCallback)(
|
|
1013
|
+
(node) => {
|
|
1014
|
+
setTextValue((prevTextValue) => prevTextValue || (node?.textContent ?? "").trim());
|
|
1015
|
+
},
|
|
1016
|
+
[],
|
|
1017
|
+
(0, import_internal.subSlot)(slot, "textChange")
|
|
1018
|
+
),
|
|
1019
|
+
children: (0, import_octane.createElement)(Collection.ItemSlot, {
|
|
1020
|
+
scope: __scopeSelect,
|
|
1021
|
+
value,
|
|
1022
|
+
disabled,
|
|
1023
|
+
textValue,
|
|
1024
|
+
children: (0, import_octane.createElement)(import_Primitive.Primitive.div, {
|
|
1025
|
+
role: "option",
|
|
1026
|
+
"aria-labelledby": textId,
|
|
1027
|
+
"data-highlighted": isFocused ? "" : void 0,
|
|
1028
|
+
// `isFocused` caveat fixes stuttering in VoiceOver
|
|
1029
|
+
"aria-selected": isSelected && isFocused,
|
|
1030
|
+
"data-state": isSelected ? "checked" : "unchecked",
|
|
1031
|
+
"aria-disabled": disabled || void 0,
|
|
1032
|
+
"data-disabled": disabled ? "" : void 0,
|
|
1033
|
+
tabIndex: disabled ? void 0 : -1,
|
|
1034
|
+
...itemProps,
|
|
1035
|
+
ref: composedRefs,
|
|
1036
|
+
onFocus: (0, import_compose_event_handlers.composeEventHandlers)(itemProps.onFocus, () => setIsFocused(true)),
|
|
1037
|
+
onBlur: (0, import_compose_event_handlers.composeEventHandlers)(itemProps.onBlur, () => setIsFocused(false)),
|
|
1038
|
+
onClick: (0, import_compose_event_handlers.composeEventHandlers)(itemProps.onClick, () => {
|
|
1039
|
+
if (pointerTypeRef.current !== "mouse") handleSelect();
|
|
1040
|
+
}),
|
|
1041
|
+
onPointerUp: (0, import_compose_event_handlers.composeEventHandlers)(itemProps.onPointerUp, () => {
|
|
1042
|
+
if (pointerTypeRef.current === "mouse") handleSelect();
|
|
1043
|
+
}),
|
|
1044
|
+
onPointerDown: (0, import_compose_event_handlers.composeEventHandlers)(itemProps.onPointerDown, (event) => {
|
|
1045
|
+
pointerTypeRef.current = event.pointerType;
|
|
1046
|
+
}),
|
|
1047
|
+
onPointerMove: (0, import_compose_event_handlers.composeEventHandlers)(itemProps.onPointerMove, (event) => {
|
|
1048
|
+
pointerTypeRef.current = event.pointerType;
|
|
1049
|
+
if (disabled) {
|
|
1050
|
+
contentContext.onItemLeave?.();
|
|
1051
|
+
} else if (pointerTypeRef.current === "mouse") {
|
|
1052
|
+
event.currentTarget.focus({ preventScroll: true });
|
|
1053
|
+
}
|
|
1054
|
+
}),
|
|
1055
|
+
onPointerLeave: (0, import_compose_event_handlers.composeEventHandlers)(itemProps.onPointerLeave, (event) => {
|
|
1056
|
+
if (event.currentTarget === document.activeElement) {
|
|
1057
|
+
contentContext.onItemLeave?.();
|
|
1058
|
+
}
|
|
1059
|
+
}),
|
|
1060
|
+
onKeyDown: (0, import_compose_event_handlers.composeEventHandlers)(itemProps.onKeyDown, (event) => {
|
|
1061
|
+
const isTypingAhead = contentContext.searchRef?.current !== "";
|
|
1062
|
+
if (isTypingAhead && event.key === " ") return;
|
|
1063
|
+
if (SELECTION_KEYS.includes(event.key)) handleSelect();
|
|
1064
|
+
if (event.key === " ") event.preventDefault();
|
|
1065
|
+
})
|
|
1066
|
+
})
|
|
1067
|
+
})
|
|
1068
|
+
});
|
|
1069
|
+
}
|
|
1070
|
+
const ITEM_TEXT_NAME = "SelectItemText";
|
|
1071
|
+
function ItemText(props) {
|
|
1072
|
+
const slot = (0, import_internal.S)("Select.ItemText");
|
|
1073
|
+
const { __scopeSelect, className, style, ref: forwardedRef, ...itemTextProps } = props ?? {};
|
|
1074
|
+
void className;
|
|
1075
|
+
void style;
|
|
1076
|
+
const context = useSelectContext(ITEM_TEXT_NAME, __scopeSelect);
|
|
1077
|
+
const contentContext = useSelectContentContext(ITEM_TEXT_NAME, __scopeSelect);
|
|
1078
|
+
const itemContext = useSelectItemContext(ITEM_TEXT_NAME, __scopeSelect);
|
|
1079
|
+
const nativeOptionsContext = useSelectNativeOptionsContext(ITEM_TEXT_NAME, __scopeSelect);
|
|
1080
|
+
const [itemTextNode, setItemTextNode] = (0, import_octane.useState)(null, (0, import_internal.subSlot)(slot, "node"));
|
|
1081
|
+
const handleItemTextRefCallback = (0, import_use_callback_ref.useCallbackRef)(
|
|
1082
|
+
(node) => contentContext.itemTextRefCallback?.(node, itemContext.value, itemContext.disabled),
|
|
1083
|
+
(0, import_internal.subSlot)(slot, "textRefCb")
|
|
1084
|
+
);
|
|
1085
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(
|
|
1086
|
+
forwardedRef,
|
|
1087
|
+
setItemTextNode,
|
|
1088
|
+
itemContext.onItemTextChange,
|
|
1089
|
+
handleItemTextRefCallback,
|
|
1090
|
+
(0, import_internal.subSlot)(slot, "refs")
|
|
1091
|
+
);
|
|
1092
|
+
const textContent = itemTextNode?.textContent;
|
|
1093
|
+
const nativeOption = (0, import_octane.useMemo)(
|
|
1094
|
+
() => (0, import_octane.createElement)("option", {
|
|
1095
|
+
key: itemContext.value,
|
|
1096
|
+
value: itemContext.value,
|
|
1097
|
+
disabled: itemContext.disabled,
|
|
1098
|
+
children: textContent
|
|
1099
|
+
}),
|
|
1100
|
+
[itemContext.disabled, itemContext.value, textContent],
|
|
1101
|
+
(0, import_internal.subSlot)(slot, "option")
|
|
1102
|
+
);
|
|
1103
|
+
const { onNativeOptionAdd, onNativeOptionRemove } = nativeOptionsContext;
|
|
1104
|
+
(0, import_octane.useLayoutEffect)(
|
|
1105
|
+
() => {
|
|
1106
|
+
onNativeOptionAdd(nativeOption);
|
|
1107
|
+
return () => onNativeOptionRemove(nativeOption);
|
|
1108
|
+
},
|
|
1109
|
+
[onNativeOptionAdd, onNativeOptionRemove, nativeOption],
|
|
1110
|
+
(0, import_internal.subSlot)(slot, "e:option")
|
|
1111
|
+
);
|
|
1112
|
+
return [
|
|
1113
|
+
(0, import_octane.createElement)(import_Primitive.Primitive.span, {
|
|
1114
|
+
key: "text",
|
|
1115
|
+
id: itemContext.textId,
|
|
1116
|
+
...itemTextProps,
|
|
1117
|
+
ref: composedRefs
|
|
1118
|
+
}),
|
|
1119
|
+
// Portal the select item text into the trigger value node.
|
|
1120
|
+
// When the value is empty we show the placeholder instead, so a
|
|
1121
|
+
// selected "clear" item (empty value) must not portal its text.
|
|
1122
|
+
// Keyed passthrough wrapper: a bare portal/null entry has no `key`, which
|
|
1123
|
+
// would trip octane's one-time missing-key warning for array children.
|
|
1124
|
+
(0, import_octane.createElement)(ValueFragment, {
|
|
1125
|
+
key: "portal",
|
|
1126
|
+
children: itemContext.isSelected && context.valueNode && !context.valueNodeHasChildren && !shouldShowPlaceholder(context.value) ? (0, import_octane.createPortal)(itemTextProps.children, context.valueNode) : null
|
|
1127
|
+
})
|
|
1128
|
+
];
|
|
1129
|
+
}
|
|
1130
|
+
const ITEM_INDICATOR_NAME = "SelectItemIndicator";
|
|
1131
|
+
function ItemIndicator(props) {
|
|
1132
|
+
const { __scopeSelect, ...itemIndicatorProps } = props ?? {};
|
|
1133
|
+
const itemContext = useSelectItemContext(ITEM_INDICATOR_NAME, __scopeSelect);
|
|
1134
|
+
return itemContext.isSelected ? (0, import_octane.createElement)(import_Primitive.Primitive.span, { "aria-hidden": true, ...itemIndicatorProps }) : null;
|
|
1135
|
+
}
|
|
1136
|
+
const SCROLL_UP_BUTTON_NAME = "SelectScrollUpButton";
|
|
1137
|
+
function ScrollUpButton(props) {
|
|
1138
|
+
const slot = (0, import_internal.S)("Select.ScrollUpButton");
|
|
1139
|
+
const contentContext = useSelectContentContext(SCROLL_UP_BUTTON_NAME, props?.__scopeSelect);
|
|
1140
|
+
const viewportContext = useSelectViewportContext(SCROLL_UP_BUTTON_NAME, props?.__scopeSelect);
|
|
1141
|
+
const [canScrollUp, setCanScrollUp] = (0, import_octane.useState)(false, (0, import_internal.subSlot)(slot, "canScroll"));
|
|
1142
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(
|
|
1143
|
+
props?.ref,
|
|
1144
|
+
viewportContext.onScrollButtonChange,
|
|
1145
|
+
(0, import_internal.subSlot)(slot, "refs")
|
|
1146
|
+
);
|
|
1147
|
+
(0, import_octane.useLayoutEffect)(
|
|
1148
|
+
() => {
|
|
1149
|
+
if (contentContext.viewport && contentContext.isPositioned) {
|
|
1150
|
+
let handleScroll2 = function() {
|
|
1151
|
+
const canScrollUp2 = viewport.scrollTop > 0;
|
|
1152
|
+
setCanScrollUp(canScrollUp2);
|
|
1153
|
+
};
|
|
1154
|
+
var handleScroll = handleScroll2;
|
|
1155
|
+
const viewport = contentContext.viewport;
|
|
1156
|
+
handleScroll2();
|
|
1157
|
+
viewport.addEventListener("scroll", handleScroll2);
|
|
1158
|
+
return () => viewport.removeEventListener("scroll", handleScroll2);
|
|
1159
|
+
}
|
|
1160
|
+
},
|
|
1161
|
+
[contentContext.viewport, contentContext.isPositioned],
|
|
1162
|
+
(0, import_internal.subSlot)(slot, "e:scroll")
|
|
1163
|
+
);
|
|
1164
|
+
return canScrollUp ? (0, import_octane.createElement)(ScrollButtonImpl, {
|
|
1165
|
+
...props,
|
|
1166
|
+
ref: composedRefs,
|
|
1167
|
+
onAutoScroll: () => {
|
|
1168
|
+
const { viewport, selectedItem } = contentContext;
|
|
1169
|
+
if (viewport && selectedItem) {
|
|
1170
|
+
viewport.scrollTop = viewport.scrollTop - selectedItem.offsetHeight;
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
}) : null;
|
|
1174
|
+
}
|
|
1175
|
+
const SCROLL_DOWN_BUTTON_NAME = "SelectScrollDownButton";
|
|
1176
|
+
function ScrollDownButton(props) {
|
|
1177
|
+
const slot = (0, import_internal.S)("Select.ScrollDownButton");
|
|
1178
|
+
const contentContext = useSelectContentContext(SCROLL_DOWN_BUTTON_NAME, props?.__scopeSelect);
|
|
1179
|
+
const viewportContext = useSelectViewportContext(SCROLL_DOWN_BUTTON_NAME, props?.__scopeSelect);
|
|
1180
|
+
const [canScrollDown, setCanScrollDown] = (0, import_octane.useState)(false, (0, import_internal.subSlot)(slot, "canScroll"));
|
|
1181
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(
|
|
1182
|
+
props?.ref,
|
|
1183
|
+
viewportContext.onScrollButtonChange,
|
|
1184
|
+
(0, import_internal.subSlot)(slot, "refs")
|
|
1185
|
+
);
|
|
1186
|
+
(0, import_octane.useLayoutEffect)(
|
|
1187
|
+
() => {
|
|
1188
|
+
if (contentContext.viewport && contentContext.isPositioned) {
|
|
1189
|
+
let handleScroll2 = function() {
|
|
1190
|
+
const maxScroll = viewport.scrollHeight - viewport.clientHeight;
|
|
1191
|
+
const canScrollDown2 = Math.ceil(viewport.scrollTop) < maxScroll;
|
|
1192
|
+
setCanScrollDown(canScrollDown2);
|
|
1193
|
+
};
|
|
1194
|
+
var handleScroll = handleScroll2;
|
|
1195
|
+
const viewport = contentContext.viewport;
|
|
1196
|
+
handleScroll2();
|
|
1197
|
+
viewport.addEventListener("scroll", handleScroll2);
|
|
1198
|
+
return () => viewport.removeEventListener("scroll", handleScroll2);
|
|
1199
|
+
}
|
|
1200
|
+
},
|
|
1201
|
+
[contentContext.viewport, contentContext.isPositioned],
|
|
1202
|
+
(0, import_internal.subSlot)(slot, "e:scroll")
|
|
1203
|
+
);
|
|
1204
|
+
return canScrollDown ? (0, import_octane.createElement)(ScrollButtonImpl, {
|
|
1205
|
+
...props,
|
|
1206
|
+
ref: composedRefs,
|
|
1207
|
+
onAutoScroll: () => {
|
|
1208
|
+
const { viewport, selectedItem } = contentContext;
|
|
1209
|
+
if (viewport && selectedItem) {
|
|
1210
|
+
viewport.scrollTop = viewport.scrollTop + selectedItem.offsetHeight;
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
}) : null;
|
|
1214
|
+
}
|
|
1215
|
+
function ScrollButtonImpl(props) {
|
|
1216
|
+
const slot = (0, import_internal.S)("Select.ScrollButtonImpl");
|
|
1217
|
+
const { __scopeSelect, onAutoScroll, ref: forwardedRef, ...scrollIndicatorProps } = props ?? {};
|
|
1218
|
+
const contentContext = useSelectContentContext("SelectScrollButton", __scopeSelect);
|
|
1219
|
+
const autoScrollTimerRef = (0, import_octane.useRef)(null, (0, import_internal.subSlot)(slot, "timer"));
|
|
1220
|
+
const getItems = useCollection(__scopeSelect, (0, import_internal.subSlot)(slot, "items"));
|
|
1221
|
+
const clearAutoScrollTimer = (0, import_octane.useCallback)(
|
|
1222
|
+
() => {
|
|
1223
|
+
if (autoScrollTimerRef.current !== null) {
|
|
1224
|
+
window.clearInterval(autoScrollTimerRef.current);
|
|
1225
|
+
autoScrollTimerRef.current = null;
|
|
1226
|
+
}
|
|
1227
|
+
},
|
|
1228
|
+
[],
|
|
1229
|
+
(0, import_internal.subSlot)(slot, "clear")
|
|
1230
|
+
);
|
|
1231
|
+
(0, import_octane.useEffect)(
|
|
1232
|
+
() => {
|
|
1233
|
+
return () => clearAutoScrollTimer();
|
|
1234
|
+
},
|
|
1235
|
+
[clearAutoScrollTimer],
|
|
1236
|
+
(0, import_internal.subSlot)(slot, "e:clear")
|
|
1237
|
+
);
|
|
1238
|
+
(0, import_octane.useLayoutEffect)(
|
|
1239
|
+
() => {
|
|
1240
|
+
const activeItem = getItems().find(
|
|
1241
|
+
(item) => item.ref.current === document.activeElement
|
|
1242
|
+
);
|
|
1243
|
+
activeItem?.ref.current?.scrollIntoView?.({ block: "nearest" });
|
|
1244
|
+
},
|
|
1245
|
+
[getItems],
|
|
1246
|
+
(0, import_internal.subSlot)(slot, "e:intoView")
|
|
1247
|
+
);
|
|
1248
|
+
return (0, import_octane.createElement)(import_Primitive.Primitive.div, {
|
|
1249
|
+
"aria-hidden": true,
|
|
1250
|
+
...scrollIndicatorProps,
|
|
1251
|
+
ref: forwardedRef,
|
|
1252
|
+
style: { flexShrink: 0, ...scrollIndicatorProps.style },
|
|
1253
|
+
onPointerDown: (0, import_compose_event_handlers.composeEventHandlers)(scrollIndicatorProps.onPointerDown, () => {
|
|
1254
|
+
if (autoScrollTimerRef.current === null) {
|
|
1255
|
+
autoScrollTimerRef.current = window.setInterval(onAutoScroll, 50);
|
|
1256
|
+
}
|
|
1257
|
+
}),
|
|
1258
|
+
onPointerMove: (0, import_compose_event_handlers.composeEventHandlers)(scrollIndicatorProps.onPointerMove, () => {
|
|
1259
|
+
contentContext.onItemLeave?.();
|
|
1260
|
+
if (autoScrollTimerRef.current === null) {
|
|
1261
|
+
autoScrollTimerRef.current = window.setInterval(onAutoScroll, 50);
|
|
1262
|
+
}
|
|
1263
|
+
}),
|
|
1264
|
+
onPointerLeave: (0, import_compose_event_handlers.composeEventHandlers)(scrollIndicatorProps.onPointerLeave, () => {
|
|
1265
|
+
clearAutoScrollTimer();
|
|
1266
|
+
})
|
|
1267
|
+
});
|
|
1268
|
+
}
|
|
1269
|
+
function Separator(props) {
|
|
1270
|
+
const { __scopeSelect, ...separatorProps } = props ?? {};
|
|
1271
|
+
return (0, import_octane.createElement)(import_Primitive.Primitive.div, { "aria-hidden": true, ...separatorProps });
|
|
1272
|
+
}
|
|
1273
|
+
const ARROW_NAME = "SelectArrow";
|
|
1274
|
+
function Arrow(props) {
|
|
1275
|
+
const slot = (0, import_internal.S)("Select.Arrow");
|
|
1276
|
+
const { __scopeSelect, ...arrowProps } = props ?? {};
|
|
1277
|
+
const popperScope = usePopperScope(__scopeSelect, (0, import_internal.subSlot)(slot, "popper"));
|
|
1278
|
+
const contentContext = useSelectContentContext(ARROW_NAME, __scopeSelect);
|
|
1279
|
+
return contentContext.position === "popper" ? (0, import_octane.createElement)(PopperPrimitive.Arrow, { ...popperScope, ...arrowProps }) : null;
|
|
1280
|
+
}
|
|
1281
|
+
const BUBBLE_INPUT_NAME = "SelectBubbleInput";
|
|
1282
|
+
function BubbleInput(props) {
|
|
1283
|
+
const slot = (0, import_internal.S)("Select.BubbleInput");
|
|
1284
|
+
const { __scopeSelect, ref: forwardedRef, ...selectProps } = props ?? {};
|
|
1285
|
+
const context = useSelectContext(BUBBLE_INPUT_NAME, __scopeSelect);
|
|
1286
|
+
const { value, onValueChange, required, disabled, name, autoComplete, form } = context;
|
|
1287
|
+
const { nativeOptions } = context;
|
|
1288
|
+
const ref = (0, import_octane.useRef)(null, (0, import_internal.subSlot)(slot, "ref"));
|
|
1289
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, ref, (0, import_internal.subSlot)(slot, "refs"));
|
|
1290
|
+
const selectValue = value ?? "";
|
|
1291
|
+
const prevValue = (0, import_use_previous.usePrevious)(selectValue, (0, import_internal.subSlot)(slot, "prev"));
|
|
1292
|
+
const hasEmptyValueOption = Array.from(nativeOptions).some(
|
|
1293
|
+
(option) => (option?.props?.value ?? "") === ""
|
|
1294
|
+
);
|
|
1295
|
+
(0, import_octane.useEffect)(
|
|
1296
|
+
() => {
|
|
1297
|
+
const select = ref.current;
|
|
1298
|
+
if (!select) return;
|
|
1299
|
+
if (prevValue !== selectValue) {
|
|
1300
|
+
select.dispatchEvent(new Event("change", { bubbles: true }));
|
|
1301
|
+
}
|
|
1302
|
+
},
|
|
1303
|
+
[prevValue, selectValue],
|
|
1304
|
+
(0, import_internal.subSlot)(slot, "e:bubble")
|
|
1305
|
+
);
|
|
1306
|
+
return (0, import_octane.createElement)(import_Primitive.Primitive.select, {
|
|
1307
|
+
"aria-hidden": true,
|
|
1308
|
+
required,
|
|
1309
|
+
tabIndex: -1,
|
|
1310
|
+
name,
|
|
1311
|
+
autoComplete,
|
|
1312
|
+
disabled,
|
|
1313
|
+
form,
|
|
1314
|
+
value: selectValue,
|
|
1315
|
+
// React's synthetic onChange on a `<select>` is the native `change` event.
|
|
1316
|
+
onChange: (event) => onValueChange(event.target.value),
|
|
1317
|
+
...selectProps,
|
|
1318
|
+
style: { ...import_VisuallyHidden.VISUALLY_HIDDEN_STYLES, ...selectProps.style },
|
|
1319
|
+
ref: composedRefs,
|
|
1320
|
+
// All entries keyed, no null holes (a null entry would trip octane's one-time
|
|
1321
|
+
// missing-key warning for array children): the placeholder option is simply
|
|
1322
|
+
// omitted when not needed.
|
|
1323
|
+
children: [
|
|
1324
|
+
...shouldShowPlaceholder(value) && !hasEmptyValueOption ? [(0, import_octane.createElement)("option", { key: "__placeholder", value: "" })] : [],
|
|
1325
|
+
...Array.from(nativeOptions)
|
|
1326
|
+
]
|
|
1327
|
+
});
|
|
1328
|
+
}
|
|
1329
|
+
function isFunction(value) {
|
|
1330
|
+
return typeof value === "function";
|
|
1331
|
+
}
|
|
1332
|
+
function shouldShowPlaceholder(value) {
|
|
1333
|
+
return value === "" || value === void 0;
|
|
1334
|
+
}
|
|
1335
|
+
function clamp(value, [min, max]) {
|
|
1336
|
+
return Math.min(max, Math.max(min, value));
|
|
1337
|
+
}
|
|
1338
|
+
function useTypeaheadSearch(...args) {
|
|
1339
|
+
const [user, slotArg] = (0, import_internal.splitSlot)(args);
|
|
1340
|
+
const slot = slotArg ?? (0, import_internal.S)("Select.useTypeaheadSearch");
|
|
1341
|
+
const onSearchChange = user[0];
|
|
1342
|
+
const handleSearchChange = (0, import_use_callback_ref.useCallbackRef)(onSearchChange, (0, import_internal.subSlot)(slot, "change"));
|
|
1343
|
+
const searchRef = (0, import_octane.useRef)("", (0, import_internal.subSlot)(slot, "search"));
|
|
1344
|
+
const timerRef = (0, import_octane.useRef)(0, (0, import_internal.subSlot)(slot, "timer"));
|
|
1345
|
+
const handleTypeaheadSearch = (0, import_octane.useCallback)(
|
|
1346
|
+
(key) => {
|
|
1347
|
+
const search = searchRef.current + key;
|
|
1348
|
+
handleSearchChange(search);
|
|
1349
|
+
(function updateSearch(value) {
|
|
1350
|
+
searchRef.current = value;
|
|
1351
|
+
window.clearTimeout(timerRef.current);
|
|
1352
|
+
if (value !== "") timerRef.current = window.setTimeout(() => updateSearch(""), 1e3);
|
|
1353
|
+
})(search);
|
|
1354
|
+
},
|
|
1355
|
+
[handleSearchChange],
|
|
1356
|
+
(0, import_internal.subSlot)(slot, "handle")
|
|
1357
|
+
);
|
|
1358
|
+
const resetTypeahead = (0, import_octane.useCallback)(
|
|
1359
|
+
() => {
|
|
1360
|
+
searchRef.current = "";
|
|
1361
|
+
window.clearTimeout(timerRef.current);
|
|
1362
|
+
},
|
|
1363
|
+
[],
|
|
1364
|
+
(0, import_internal.subSlot)(slot, "reset")
|
|
1365
|
+
);
|
|
1366
|
+
(0, import_octane.useEffect)(
|
|
1367
|
+
() => {
|
|
1368
|
+
return () => window.clearTimeout(timerRef.current);
|
|
1369
|
+
},
|
|
1370
|
+
[],
|
|
1371
|
+
(0, import_internal.subSlot)(slot, "e:timer")
|
|
1372
|
+
);
|
|
1373
|
+
return [searchRef, handleTypeaheadSearch, resetTypeahead];
|
|
1374
|
+
}
|
|
1375
|
+
function findNextItem(items, search, currentItem) {
|
|
1376
|
+
const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
|
|
1377
|
+
const normalizedSearch = isRepeated ? search[0] : search;
|
|
1378
|
+
const currentItemIndex = currentItem ? items.indexOf(currentItem) : -1;
|
|
1379
|
+
let wrappedItems = wrapArray(items, Math.max(currentItemIndex, 0));
|
|
1380
|
+
const excludeCurrentItem = normalizedSearch.length === 1;
|
|
1381
|
+
if (excludeCurrentItem) wrappedItems = wrappedItems.filter((v) => v !== currentItem);
|
|
1382
|
+
const nextItem = wrappedItems.find(
|
|
1383
|
+
(item) => item.textValue.toLowerCase().startsWith(normalizedSearch.toLowerCase())
|
|
1384
|
+
);
|
|
1385
|
+
return nextItem !== currentItem ? nextItem : void 0;
|
|
1386
|
+
}
|
|
1387
|
+
function wrapArray(array, startIndex) {
|
|
1388
|
+
return array.map((_, index) => array[(startIndex + index) % array.length]);
|
|
1389
|
+
}
|
|
1390
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1391
|
+
0 && (module.exports = {
|
|
1392
|
+
Arrow,
|
|
1393
|
+
BubbleInput,
|
|
1394
|
+
Content,
|
|
1395
|
+
Group,
|
|
1396
|
+
Icon,
|
|
1397
|
+
Item,
|
|
1398
|
+
ItemIndicator,
|
|
1399
|
+
ItemText,
|
|
1400
|
+
Label,
|
|
1401
|
+
Portal,
|
|
1402
|
+
Provider,
|
|
1403
|
+
Root,
|
|
1404
|
+
ScrollDownButton,
|
|
1405
|
+
ScrollUpButton,
|
|
1406
|
+
Select,
|
|
1407
|
+
SelectArrow,
|
|
1408
|
+
SelectBubbleInput,
|
|
1409
|
+
SelectContent,
|
|
1410
|
+
SelectGroup,
|
|
1411
|
+
SelectIcon,
|
|
1412
|
+
SelectItem,
|
|
1413
|
+
SelectItemIndicator,
|
|
1414
|
+
SelectItemText,
|
|
1415
|
+
SelectLabel,
|
|
1416
|
+
SelectPortal,
|
|
1417
|
+
SelectProvider,
|
|
1418
|
+
SelectScrollDownButton,
|
|
1419
|
+
SelectScrollUpButton,
|
|
1420
|
+
SelectSeparator,
|
|
1421
|
+
SelectTrigger,
|
|
1422
|
+
SelectValue,
|
|
1423
|
+
SelectViewport,
|
|
1424
|
+
Separator,
|
|
1425
|
+
Trigger,
|
|
1426
|
+
Value,
|
|
1427
|
+
Viewport,
|
|
1428
|
+
createSelectScope
|
|
1429
|
+
});
|