@box/blueprint-web 19.6.0 → 20.0.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/THIRD_PARTY_LICENSES +119 -0
- package/dist/lib-esm/accordion/accordion.js +6 -6
- package/dist/lib-esm/accordion/types.d.ts +1 -1
- package/dist/lib-esm/checkbox/checkbox-item.js +3 -3
- package/dist/lib-esm/checkbox/types.d.ts +1 -1
- package/dist/lib-esm/collapsible/collapsible-section.js +5 -5
- package/dist/lib-esm/collapsible/types.d.ts +1 -1
- package/dist/lib-esm/combobox-group/combobox-group-combobox.js +1 -1
- package/dist/lib-esm/date-picker/date-picker.js +8 -7
- package/dist/lib-esm/guided-tooltip/guided-tooltip.js +7 -7
- package/dist/lib-esm/guided-tooltip/types.d.ts +1 -1
- package/dist/lib-esm/modal/alert-modal.types.d.ts +1 -1
- package/dist/lib-esm/modal/modal-close.js +2 -2
- package/dist/lib-esm/modal/modal-content.d.ts +1 -1
- package/dist/lib-esm/modal/modal-content.js +4 -4
- package/dist/lib-esm/modal/modal-description.js +2 -2
- package/dist/lib-esm/modal/modal-title.js +2 -2
- package/dist/lib-esm/modal/modal-trigger.js +2 -2
- package/dist/lib-esm/modal/modal.d.ts +1 -1
- package/dist/lib-esm/modal/modal.js +1 -1
- package/dist/lib-esm/modal/types.d.ts +1 -1
- package/dist/lib-esm/navigation-menu/index.d.ts +4 -2
- package/dist/lib-esm/navigation-menu/navigation-menu-item.d.ts +1 -1
- package/dist/lib-esm/navigation-menu/navigation-menu-trigger.d.ts +3 -1
- package/dist/lib-esm/primitives/chips/chip.d.ts +1 -1
- package/dist/lib-esm/primitives/chips/chip.js +2 -2
- package/dist/lib-esm/primitives/chips/chips-group.js +2 -2
- package/dist/lib-esm/primitives/chips/filter-chip/filter-chip-button.d.ts +1 -1
- package/dist/lib-esm/primitives/chips/filter-chip/filter-chip.d.ts +1 -1
- package/dist/lib-esm/primitives/chips/filter-chip/filter-combo-chip.d.ts +2 -2
- package/dist/lib-esm/primitives/chips/filter-chip/filter-trigger-chip.d.ts +1 -1
- package/dist/lib-esm/primitives/chips/filter-chip/index.d.ts +5 -5
- package/dist/lib-esm/primitives/chips/types.d.ts +1 -1
- package/dist/lib-esm/primitives/context-menu/context-menu-item.d.ts +2 -2
- package/dist/lib-esm/primitives/context-menu/context-menu-item.js +2 -2
- package/dist/lib-esm/primitives/context-menu/context-menu-menu-content.d.ts +1 -1
- package/dist/lib-esm/primitives/context-menu/context-menu-menu-content.js +5 -5
- package/dist/lib-esm/primitives/context-menu/context-menu-menu-header.js +3 -10
- package/dist/lib-esm/primitives/context-menu/context-menu-menu-root.d.ts +1 -1
- package/dist/lib-esm/primitives/context-menu/context-menu-menu-root.js +1 -1
- package/dist/lib-esm/primitives/context-menu/context-menu-menu-trigger.d.ts +1 -1
- package/dist/lib-esm/primitives/context-menu/context-menu-menu-trigger.js +1 -1
- package/dist/lib-esm/primitives/context-menu/context-menu-sub-menu-content.d.ts +1 -1
- package/dist/lib-esm/primitives/context-menu/context-menu-sub-menu-content.js +5 -5
- package/dist/lib-esm/primitives/context-menu/context-menu-sub-menu-root.d.ts +1 -1
- package/dist/lib-esm/primitives/context-menu/context-menu-sub-menu-root.js +1 -1
- package/dist/lib-esm/primitives/context-menu/context-menu-sub-menu-trigger.d.ts +2 -2
- package/dist/lib-esm/primitives/context-menu/context-menu-sub-menu-trigger.js +1 -1
- package/dist/lib-esm/primitives/context-menu/index.d.ts +3 -3
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-checkbox-item.d.ts +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-checkbox-item.js +3 -3
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-content.d.ts +1 -1
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-content.js +5 -5
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-group.d.ts +4 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-group.js +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-item.d.ts +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-item.js +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-radio-group.d.ts +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-radio-group.js +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-radio-select-item.d.ts +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-radio-select-item.js +3 -3
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-root.d.ts +1 -1
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-root.js +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-sub-header.d.ts +4 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-sub-header.js +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-sub-menu-content.d.ts +1 -1
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-sub-menu-content.js +5 -5
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-sub-menu-root.d.ts +1 -1
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-sub-menu-root.js +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-sub-menu-trigger.d.ts +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-sub-menu-trigger.js +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-trigger.d.ts +4 -2
- package/dist/lib-esm/primitives/dropdown-menu/dropdown-menu-trigger.js +2 -2
- package/dist/lib-esm/primitives/dropdown-menu/index.d.ts +14 -8
- package/dist/lib-esm/primitives/notification/hooks/use-notification-layer.d.ts +15 -0
- package/dist/lib-esm/primitives/notification/hooks/use-notification-layer.js +49 -0
- package/dist/lib-esm/primitives/notification/notification-provider.js +2 -2
- package/dist/lib-esm/primitives/notification/notification.viewport-animated.js +12 -8
- package/dist/lib-esm/primitives/notification/notification.viewport.js +5 -1
- package/dist/lib-esm/primitives/notification/private-notification-element-animated.js +44 -6
- package/dist/lib-esm/primitives/notification/private-notification-element.js +34 -4
- package/dist/lib-esm/primitives/popover/index.d.ts +11 -5
- package/dist/lib-esm/primitives/popover/popover-anchor.d.ts +4 -2
- package/dist/lib-esm/primitives/popover/popover-anchor.js +2 -2
- package/dist/lib-esm/primitives/popover/popover-close.d.ts +4 -2
- package/dist/lib-esm/primitives/popover/popover-close.js +2 -2
- package/dist/lib-esm/primitives/popover/popover-content-container.d.ts +1 -1
- package/dist/lib-esm/primitives/popover/popover-content-container.js +3 -3
- package/dist/lib-esm/primitives/popover/popover-footer.d.ts +1 -1
- package/dist/lib-esm/primitives/popover/popover-main-content.d.ts +1 -1
- package/dist/lib-esm/primitives/popover/popover-root.d.ts +1 -1
- package/dist/lib-esm/primitives/popover/popover-root.js +2 -2
- package/dist/lib-esm/primitives/popover/popover-trigger.d.ts +4 -2
- package/dist/lib-esm/primitives/popover/popover-trigger.js +2 -2
- package/dist/lib-esm/radio-group/index.d.ts +1 -1
- package/dist/lib-esm/radio-group/radio-group.d.ts +1 -1
- package/dist/lib-esm/radio-group/radio-group.js +2 -2
- package/dist/lib-esm/radio-group/radio-item.js +3 -3
- package/dist/lib-esm/radio-group/types.d.ts +1 -1
- package/dist/lib-esm/select/select.js +12 -12
- package/dist/lib-esm/select/types.d.ts +1 -1
- package/dist/lib-esm/select/utils/use-select-exit-animation.d.ts +3 -3
- package/dist/lib-esm/select/utils/use-select-exit-animation.js +3 -3
- package/dist/lib-esm/side-panel/side-panel-close.js +2 -2
- package/dist/lib-esm/side-panel/side-panel-content.js +3 -3
- package/dist/lib-esm/side-panel/side-panel-header.js +2 -2
- package/dist/lib-esm/side-panel/side-panel-overlay.js +3 -3
- package/dist/lib-esm/side-panel/side-panel-trigger.js +2 -2
- package/dist/lib-esm/side-panel/side-panel.js +1 -1
- package/dist/lib-esm/side-panel/types.d.ts +1 -1
- package/dist/lib-esm/switch/switch-item.js +3 -3
- package/dist/lib-esm/switch/types.d.ts +1 -1
- package/dist/lib-esm/toolbar/index.d.ts +2 -2
- package/dist/lib-esm/toolbar/toolbar-button.js +2 -2
- package/dist/lib-esm/toolbar/toolbar-input.js +2 -2
- package/dist/lib-esm/toolbar/toolbar-link.js +2 -2
- package/dist/lib-esm/toolbar/toolbar-root.js +3 -3
- package/dist/lib-esm/toolbar/toolbar-separator.d.ts +1 -1
- package/dist/lib-esm/toolbar/toolbar-separator.js +2 -2
- package/dist/lib-esm/toolbar/toolbar-text-toggle-item.js +2 -2
- package/dist/lib-esm/toolbar/toolbar-toggle-group.d.ts +1 -2
- package/dist/lib-esm/toolbar/toolbar-toggle-group.js +2 -2
- package/dist/lib-esm/toolbar/toolbar-toggle-item.js +2 -2
- package/dist/lib-esm/toolbar/types.d.ts +1 -1
- package/dist/lib-esm/tooltip/tooltip.d.ts +1 -1
- package/dist/lib-esm/tooltip/tooltip.js +6 -6
- package/dist/lib-esm/tooltip/types.d.ts +1 -1
- package/dist/lib-esm/utils/radix-compat/accordion.d.ts +45 -0
- package/dist/lib-esm/utils/radix-compat/accordion.js +299 -0
- package/dist/lib-esm/utils/radix-compat/allow-auto-focus.d.ts +9 -0
- package/dist/lib-esm/utils/radix-compat/allow-auto-focus.js +16 -0
- package/dist/lib-esm/utils/radix-compat/bubble-input.d.ts +14 -0
- package/dist/lib-esm/utils/radix-compat/bubble-input.js +70 -0
- package/dist/lib-esm/utils/radix-compat/button-controls.d.ts +9 -0
- package/dist/lib-esm/utils/radix-compat/button-controls.js +6 -0
- package/dist/lib-esm/utils/radix-compat/checkbox.d.ts +18 -0
- package/dist/lib-esm/utils/radix-compat/checkbox.js +131 -0
- package/dist/lib-esm/utils/radix-compat/collapsible.d.ts +33 -0
- package/dist/lib-esm/utils/radix-compat/collapsible.js +133 -0
- package/dist/lib-esm/utils/radix-compat/collection.d.ts +21 -0
- package/dist/lib-esm/utils/radix-compat/collection.js +82 -0
- package/dist/lib-esm/utils/radix-compat/compose-refs.d.ts +8 -0
- package/dist/lib-esm/utils/radix-compat/compose-refs.js +44 -0
- package/dist/lib-esm/utils/radix-compat/content-size.d.ts +15 -0
- package/dist/lib-esm/utils/radix-compat/content-size.js +61 -0
- package/dist/lib-esm/utils/radix-compat/context-menu.d.ts +56 -0
- package/dist/lib-esm/utils/radix-compat/context-menu.js +191 -0
- package/dist/lib-esm/utils/radix-compat/dialog.d.ts +50 -0
- package/dist/lib-esm/utils/radix-compat/dialog.js +367 -0
- package/dist/lib-esm/utils/radix-compat/dismiss-marker.d.ts +5 -0
- package/dist/lib-esm/utils/radix-compat/dismiss-marker.js +12 -0
- package/dist/lib-esm/utils/radix-compat/dismissable.d.ts +34 -0
- package/dist/lib-esm/utils/radix-compat/dismissable.js +205 -0
- package/dist/lib-esm/utils/radix-compat/dropdown-menu.d.ts +57 -0
- package/dist/lib-esm/utils/radix-compat/dropdown-menu.js +187 -0
- package/dist/lib-esm/utils/radix-compat/focus-scope-component.d.ts +16 -0
- package/dist/lib-esm/utils/radix-compat/focus-scope-component.js +41 -0
- package/dist/lib-esm/utils/radix-compat/focus-scope.d.ts +37 -0
- package/dist/lib-esm/utils/radix-compat/focus-scope.js +288 -0
- package/dist/lib-esm/utils/radix-compat/hide-others.d.ts +6 -0
- package/dist/lib-esm/utils/radix-compat/hide-others.js +18 -0
- package/dist/lib-esm/utils/radix-compat/index.d.ts +14 -0
- package/dist/lib-esm/utils/radix-compat/layers.d.ts +25 -0
- package/dist/lib-esm/utils/radix-compat/layers.js +61 -0
- package/dist/lib-esm/utils/radix-compat/menu.d.ts +98 -0
- package/dist/lib-esm/utils/radix-compat/menu.js +1041 -0
- package/dist/lib-esm/utils/radix-compat/popover.d.ts +44 -0
- package/dist/lib-esm/utils/radix-compat/popover.js +386 -0
- package/dist/lib-esm/utils/radix-compat/popper.d.ts +60 -0
- package/dist/lib-esm/utils/radix-compat/popper.js +416 -0
- package/dist/lib-esm/utils/radix-compat/portal.d.ts +11 -0
- package/dist/lib-esm/utils/radix-compat/portal.js +20 -0
- package/dist/lib-esm/utils/radix-compat/presence.d.ts +20 -0
- package/dist/lib-esm/utils/radix-compat/presence.js +110 -0
- package/dist/lib-esm/utils/radix-compat/radio-group.d.ts +27 -0
- package/dist/lib-esm/utils/radix-compat/radio-group.js +189 -0
- package/dist/lib-esm/utils/radix-compat/roving-focus.d.ts +35 -0
- package/dist/lib-esm/utils/radix-compat/roving-focus.js +237 -0
- package/dist/lib-esm/utils/radix-compat/select.d.ts +82 -0
- package/dist/lib-esm/utils/radix-compat/select.js +901 -0
- package/dist/lib-esm/utils/radix-compat/slot.d.ts +31 -0
- package/dist/lib-esm/utils/radix-compat/slot.js +119 -0
- package/dist/lib-esm/utils/radix-compat/store-options.d.ts +8 -0
- package/dist/lib-esm/utils/radix-compat/store-options.js +10 -0
- package/dist/lib-esm/utils/radix-compat/switch.d.ts +16 -0
- package/dist/lib-esm/utils/radix-compat/switch.js +97 -0
- package/dist/lib-esm/utils/radix-compat/toggle-group.d.ts +41 -0
- package/dist/lib-esm/utils/radix-compat/toggle-group.js +195 -0
- package/dist/lib-esm/utils/radix-compat/toolbar.d.ts +32 -0
- package/dist/lib-esm/utils/radix-compat/toolbar.js +136 -0
- package/dist/lib-esm/utils/radix-compat/tooltip.d.ts +59 -0
- package/dist/lib-esm/utils/radix-compat/tooltip.js +636 -0
- package/dist/lib-esm/utils/radix-compat/typeahead.d.ts +22 -0
- package/dist/lib-esm/utils/radix-compat/typeahead.js +64 -0
- package/dist/lib-esm/utils/radix-compat/types.d.ts +53 -0
- package/dist/lib-esm/utils/radix-compat/viewport-height.d.ts +5 -0
- package/dist/lib-esm/utils/radix-compat/viewport-height.js +28 -0
- package/dist/lib-esm/utils/radix-compat/visually-hidden.d.ts +2 -0
- package/dist/lib-esm/utils/radix-compat/visually-hidden.js +24 -0
- package/package.json +8 -19
|
@@ -0,0 +1,901 @@
|
|
|
1
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
|
+
import { useStoreState, Popover, usePopoverStore } from '@ariakit/react';
|
|
3
|
+
import { forwardRef, useState, useContext, useId, useRef, useCallback, useMemo, useEffect, createContext } from 'react';
|
|
4
|
+
import { createPortal } from 'react-dom';
|
|
5
|
+
import { RemoveScroll } from 'react-remove-scroll';
|
|
6
|
+
import { composeEventHandlers } from '../composeEventHandlers.js';
|
|
7
|
+
import { useControllableState } from '../useControllableState.js';
|
|
8
|
+
import { useEnhancedEffect } from '../useEnhancedEffect.js';
|
|
9
|
+
import { AllowAutoFocus } from './allow-auto-focus.js';
|
|
10
|
+
import { createCollection } from './collection.js';
|
|
11
|
+
import { useComposedRefs } from './compose-refs.js';
|
|
12
|
+
import { DismissMarker } from './dismiss-marker.js';
|
|
13
|
+
import { useDismissable } from './dismissable.js';
|
|
14
|
+
import { useFocusScope } from './focus-scope.js';
|
|
15
|
+
import { useHideOthers } from './hide-others.js';
|
|
16
|
+
import { getPersistentElements } from './layers.js';
|
|
17
|
+
import { PopperArrow, usePopper, PopperArrowProvider, getPopperCssVariables } from './popper.js';
|
|
18
|
+
import { Portal as Portal$1 } from './portal.js';
|
|
19
|
+
import { Slot, getAsChildRender } from './slot.js';
|
|
20
|
+
import { overlayStoreOptions } from './store-options.js';
|
|
21
|
+
import { useTypeaheadSearch, findNextItem } from './typeahead.js';
|
|
22
|
+
import { useStripViewportHeight } from './viewport-height.js';
|
|
23
|
+
|
|
24
|
+
const OPEN_KEYS = [' ', 'Enter', 'ArrowUp', 'ArrowDown'];
|
|
25
|
+
const SELECTION_KEYS = [' ', 'Enter'];
|
|
26
|
+
const CONTENT_MARGIN = 10;
|
|
27
|
+
const shouldShowPlaceholder = value => value === '' || value === undefined;
|
|
28
|
+
const noopUpdatePosition = () => undefined;
|
|
29
|
+
const preventContextMenu = event => event.preventDefault();
|
|
30
|
+
const {
|
|
31
|
+
CollectionProvider,
|
|
32
|
+
useCollection,
|
|
33
|
+
useCollectionItem,
|
|
34
|
+
useCollectionRef
|
|
35
|
+
} = createCollection();
|
|
36
|
+
const SelectContext = /*#__PURE__*/createContext(null);
|
|
37
|
+
const NativeOptionsContext = /*#__PURE__*/createContext(null);
|
|
38
|
+
function useSelectContext(consumerName) {
|
|
39
|
+
const context = useContext(SelectContext);
|
|
40
|
+
if (!context) {
|
|
41
|
+
throw new Error(`\`${consumerName}\` must be used within \`Select\``);
|
|
42
|
+
}
|
|
43
|
+
return context;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Select with the Radix API, DOM and keyboard model; its listbox is an Ariakit popover. Inside a form, a hidden
|
|
47
|
+
* native `<select>` mirrors the value.
|
|
48
|
+
*/
|
|
49
|
+
const Root = props => {
|
|
50
|
+
const {
|
|
51
|
+
children,
|
|
52
|
+
open: openProp,
|
|
53
|
+
defaultOpen,
|
|
54
|
+
onOpenChange,
|
|
55
|
+
value: valueProp,
|
|
56
|
+
defaultValue,
|
|
57
|
+
onValueChange,
|
|
58
|
+
dir,
|
|
59
|
+
name,
|
|
60
|
+
autoComplete,
|
|
61
|
+
disabled,
|
|
62
|
+
required
|
|
63
|
+
} = props;
|
|
64
|
+
const [trigger, setTrigger] = useState(null);
|
|
65
|
+
const [valueNode, setValueNode] = useState(null);
|
|
66
|
+
const [valueNodeHasChildren, setValueNodeHasChildren] = useState(false);
|
|
67
|
+
const contentId = useId();
|
|
68
|
+
const [open = false, setOpen] = useControllableState({
|
|
69
|
+
prop: openProp,
|
|
70
|
+
defaultProp: defaultOpen,
|
|
71
|
+
onChange: onOpenChange
|
|
72
|
+
});
|
|
73
|
+
const [value, setValue] = useControllableState({
|
|
74
|
+
prop: valueProp,
|
|
75
|
+
defaultProp: defaultValue,
|
|
76
|
+
onChange: onValueChange
|
|
77
|
+
});
|
|
78
|
+
const triggerPointerDownPosRef = useRef(null);
|
|
79
|
+
// Until the trigger mounts, the native select renders so forms work without JavaScript, like in Radix.
|
|
80
|
+
const isFormControl = trigger ? Boolean(trigger.closest('form')) : true;
|
|
81
|
+
const [nativeOptionsSet, setNativeOptionsSet] = useState(new Set());
|
|
82
|
+
// The native select only picks the default value if the matching option renders at the same time, so it's
|
|
83
|
+
// rebuilt whenever the options change.
|
|
84
|
+
const nativeSelectKey = Array.from(nativeOptionsSet).map(option => option.props.value).join(';');
|
|
85
|
+
const onOpenChangeCallback = useCallback(nextOpen => setOpen(nextOpen), [setOpen]);
|
|
86
|
+
const onValueChangeCallback = useCallback(nextValue => setValue(nextValue), [setValue]);
|
|
87
|
+
const store = usePopoverStore({
|
|
88
|
+
...overlayStoreOptions,
|
|
89
|
+
open,
|
|
90
|
+
setOpen: nextOpen => {
|
|
91
|
+
if (!nextOpen) {
|
|
92
|
+
setOpen(false);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
store.setAnchorElement(trigger);
|
|
98
|
+
}, [store, trigger]);
|
|
99
|
+
const contextValue = useMemo(() => ({
|
|
100
|
+
required,
|
|
101
|
+
trigger,
|
|
102
|
+
onTriggerChange: setTrigger,
|
|
103
|
+
valueNode,
|
|
104
|
+
onValueNodeChange: setValueNode,
|
|
105
|
+
valueNodeHasChildren,
|
|
106
|
+
onValueNodeHasChildrenChange: setValueNodeHasChildren,
|
|
107
|
+
contentId,
|
|
108
|
+
value,
|
|
109
|
+
onValueChange: onValueChangeCallback,
|
|
110
|
+
open,
|
|
111
|
+
onOpenChange: onOpenChangeCallback,
|
|
112
|
+
dir: dir ?? 'ltr',
|
|
113
|
+
triggerPointerDownPosRef,
|
|
114
|
+
disabled,
|
|
115
|
+
store
|
|
116
|
+
}), [required, trigger, valueNode, valueNodeHasChildren, contentId, value, onValueChangeCallback, open, onOpenChangeCallback, dir, disabled, store]);
|
|
117
|
+
const nativeOptionsValue = useMemo(() => ({
|
|
118
|
+
onNativeOptionAdd: option => setNativeOptionsSet(prev => new Set(prev).add(option)),
|
|
119
|
+
onNativeOptionRemove: option => setNativeOptionsSet(prev => {
|
|
120
|
+
const optionsSet = new Set(prev);
|
|
121
|
+
optionsSet.delete(option);
|
|
122
|
+
return optionsSet;
|
|
123
|
+
})
|
|
124
|
+
}), []);
|
|
125
|
+
const handleNativeSelectChange = useCallback(event => setValue(event.target.value), [setValue]);
|
|
126
|
+
return jsxs(SelectContext.Provider, {
|
|
127
|
+
value: contextValue,
|
|
128
|
+
children: [jsx(CollectionProvider, {
|
|
129
|
+
children: jsx(NativeOptionsContext.Provider, {
|
|
130
|
+
value: nativeOptionsValue,
|
|
131
|
+
children: children
|
|
132
|
+
})
|
|
133
|
+
}), isFormControl ? jsxs(BubbleSelect, {
|
|
134
|
+
"aria-hidden": true,
|
|
135
|
+
autoComplete: autoComplete,
|
|
136
|
+
disabled: disabled,
|
|
137
|
+
name: name,
|
|
138
|
+
required: required,
|
|
139
|
+
tabIndex: -1,
|
|
140
|
+
value: value,
|
|
141
|
+
// Enables form autofill.
|
|
142
|
+
onChange: handleNativeSelectChange,
|
|
143
|
+
children: [value === undefined ? jsx("option", {
|
|
144
|
+
value: ""
|
|
145
|
+
}) : null, Array.from(nativeOptionsSet)]
|
|
146
|
+
}, nativeSelectKey) : null]
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
Root.displayName = 'Select';
|
|
150
|
+
const Trigger = /*#__PURE__*/forwardRef((props, forwardedRef) => {
|
|
151
|
+
const {
|
|
152
|
+
disabled = false,
|
|
153
|
+
asChild,
|
|
154
|
+
...triggerProps
|
|
155
|
+
} = props;
|
|
156
|
+
const context = useSelectContext('SelectTrigger');
|
|
157
|
+
const isDisabled = context.disabled || disabled;
|
|
158
|
+
const composedRefs = useComposedRefs(forwardedRef, context.onTriggerChange);
|
|
159
|
+
const getItems = useCollection();
|
|
160
|
+
const [searchRef, handleTypeaheadSearch, resetTypeahead] = useTypeaheadSearch(search => {
|
|
161
|
+
const enabledItems = getItems().filter(item => !item.disabled);
|
|
162
|
+
const currentItem = enabledItems.find(item => item.value === context.value);
|
|
163
|
+
const nextItem = findNextItem(enabledItems, search, currentItem);
|
|
164
|
+
if (nextItem !== undefined) {
|
|
165
|
+
context.onValueChange(nextItem.value);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
const handleOpen = () => {
|
|
169
|
+
if (!isDisabled) {
|
|
170
|
+
context.onOpenChange(true);
|
|
171
|
+
// Typeahead starts over when the select opens.
|
|
172
|
+
resetTypeahead();
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
const Comp = asChild ? Slot : 'button';
|
|
176
|
+
return jsx(Comp, {
|
|
177
|
+
"aria-autocomplete": "none",
|
|
178
|
+
"aria-controls": context.contentId,
|
|
179
|
+
"aria-expanded": context.open,
|
|
180
|
+
"aria-required": context.required,
|
|
181
|
+
"data-disabled": isDisabled ? '' : undefined,
|
|
182
|
+
"data-placeholder": shouldShowPlaceholder(context.value) ? '' : undefined,
|
|
183
|
+
"data-state": context.open ? 'open' : 'closed',
|
|
184
|
+
dir: context.dir,
|
|
185
|
+
disabled: isDisabled,
|
|
186
|
+
role: "combobox",
|
|
187
|
+
type: "button",
|
|
188
|
+
...triggerProps,
|
|
189
|
+
ref: composedRefs,
|
|
190
|
+
// Safari doesn't focus the trigger when its label is clicked; pointer downs are prevented below.
|
|
191
|
+
onClick: composeEventHandlers(triggerProps.onClick, event => {
|
|
192
|
+
event.currentTarget.focus();
|
|
193
|
+
}),
|
|
194
|
+
onKeyDown: composeEventHandlers(triggerProps.onKeyDown, event => {
|
|
195
|
+
const isTypingAhead = searchRef.current !== '';
|
|
196
|
+
const isModifierKey = event.ctrlKey || event.altKey || event.metaKey;
|
|
197
|
+
if (!isModifierKey && event.key.length === 1) {
|
|
198
|
+
handleTypeaheadSearch(event.key);
|
|
199
|
+
}
|
|
200
|
+
if (isTypingAhead && event.key === ' ') {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (OPEN_KEYS.includes(event.key)) {
|
|
204
|
+
handleOpen();
|
|
205
|
+
event.preventDefault();
|
|
206
|
+
}
|
|
207
|
+
}),
|
|
208
|
+
onPointerDown: composeEventHandlers(triggerProps.onPointerDown, event => {
|
|
209
|
+
// Prevent implicit pointer capture: https://www.w3.org/TR/pointerevents3/#implicit-pointer-capture
|
|
210
|
+
const target = event.target;
|
|
211
|
+
if (target.hasPointerCapture(event.pointerId)) {
|
|
212
|
+
target.releasePointerCapture(event.pointerId);
|
|
213
|
+
}
|
|
214
|
+
// Only the primary button opens the select, and not with Ctrl (macOS right click).
|
|
215
|
+
if (event.button === 0 && event.ctrlKey === false) {
|
|
216
|
+
handleOpen();
|
|
217
|
+
context.triggerPointerDownPosRef.current = {
|
|
218
|
+
x: Math.round(event.pageX),
|
|
219
|
+
y: Math.round(event.pageY)
|
|
220
|
+
};
|
|
221
|
+
// The trigger doesn't steal focus from the active item after opening.
|
|
222
|
+
event.preventDefault();
|
|
223
|
+
}
|
|
224
|
+
})
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
Trigger.displayName = 'SelectTrigger';
|
|
228
|
+
const Value = /*#__PURE__*/forwardRef((props, forwardedRef) => {
|
|
229
|
+
// `className` and `style` are ignored, as this part isn't meant to be styled.
|
|
230
|
+
const {
|
|
231
|
+
className: _className,
|
|
232
|
+
style: _style,
|
|
233
|
+
children,
|
|
234
|
+
placeholder = '',
|
|
235
|
+
asChild,
|
|
236
|
+
...valueProps
|
|
237
|
+
} = props;
|
|
238
|
+
const context = useSelectContext('SelectValue');
|
|
239
|
+
const {
|
|
240
|
+
onValueNodeHasChildrenChange
|
|
241
|
+
} = context;
|
|
242
|
+
const hasChildren = children !== undefined;
|
|
243
|
+
const composedRefs = useComposedRefs(forwardedRef, context.onValueNodeChange);
|
|
244
|
+
useEnhancedEffect(() => {
|
|
245
|
+
onValueNodeHasChildrenChange(hasChildren);
|
|
246
|
+
}, [onValueNodeHasChildrenChange, hasChildren]);
|
|
247
|
+
const Comp = asChild ? Slot : 'span';
|
|
248
|
+
return (
|
|
249
|
+
// Events from the portalled children of the value don't bubble.
|
|
250
|
+
jsx(Comp, {
|
|
251
|
+
...valueProps,
|
|
252
|
+
ref: composedRefs,
|
|
253
|
+
style: {
|
|
254
|
+
pointerEvents: 'none'
|
|
255
|
+
},
|
|
256
|
+
children: shouldShowPlaceholder(context.value) ? placeholder : children
|
|
257
|
+
})
|
|
258
|
+
);
|
|
259
|
+
});
|
|
260
|
+
Value.displayName = 'SelectValue';
|
|
261
|
+
const Icon = /*#__PURE__*/forwardRef(({
|
|
262
|
+
children,
|
|
263
|
+
asChild,
|
|
264
|
+
...iconProps
|
|
265
|
+
}, forwardedRef) => {
|
|
266
|
+
const Comp = asChild ? Slot : 'span';
|
|
267
|
+
return jsx(Comp, {
|
|
268
|
+
"aria-hidden": true,
|
|
269
|
+
...iconProps,
|
|
270
|
+
ref: forwardedRef,
|
|
271
|
+
children: children || '▼'
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
Icon.displayName = 'SelectIcon';
|
|
275
|
+
const Portal = ({
|
|
276
|
+
children,
|
|
277
|
+
container
|
|
278
|
+
}) => jsx(Portal$1, {
|
|
279
|
+
container: container,
|
|
280
|
+
children: children
|
|
281
|
+
});
|
|
282
|
+
Portal.displayName = 'SelectPortal';
|
|
283
|
+
const SelectContentContext = /*#__PURE__*/createContext(null);
|
|
284
|
+
const ClosedContentContext = /*#__PURE__*/createContext(false);
|
|
285
|
+
const Content = /*#__PURE__*/forwardRef((props, forwardedRef) => {
|
|
286
|
+
const context = useSelectContext('SelectContent');
|
|
287
|
+
const [fragment, setFragment] = useState();
|
|
288
|
+
// `DocumentFragment` doesn't exist on the server.
|
|
289
|
+
useEnhancedEffect(() => {
|
|
290
|
+
setFragment(new DocumentFragment());
|
|
291
|
+
}, []);
|
|
292
|
+
if (!context.open) {
|
|
293
|
+
// While closed, the items render outside the document, so the selected text and the native options exist.
|
|
294
|
+
return fragment ? /*#__PURE__*/createPortal(jsx(ClosedContentContext.Provider, {
|
|
295
|
+
value: true,
|
|
296
|
+
children: jsx(ClosedCollectionSlot, {
|
|
297
|
+
children: props.children
|
|
298
|
+
})
|
|
299
|
+
}), fragment) : null;
|
|
300
|
+
}
|
|
301
|
+
return jsx(ContentImpl, {
|
|
302
|
+
...props,
|
|
303
|
+
ref: forwardedRef
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
Content.displayName = 'SelectContent';
|
|
307
|
+
const ClosedCollectionSlot = ({
|
|
308
|
+
children
|
|
309
|
+
}) => {
|
|
310
|
+
const collectionRef = useCollectionRef();
|
|
311
|
+
return jsx("div", {
|
|
312
|
+
ref: collectionRef,
|
|
313
|
+
children: children
|
|
314
|
+
});
|
|
315
|
+
};
|
|
316
|
+
const ContentImpl = /*#__PURE__*/forwardRef((props, forwardedRef) => {
|
|
317
|
+
const {
|
|
318
|
+
position = 'item-aligned',
|
|
319
|
+
onCloseAutoFocus,
|
|
320
|
+
onEscapeKeyDown,
|
|
321
|
+
onPointerDownOutside,
|
|
322
|
+
side,
|
|
323
|
+
sideOffset,
|
|
324
|
+
align = 'start',
|
|
325
|
+
alignOffset,
|
|
326
|
+
arrowPadding,
|
|
327
|
+
collisionBoundary,
|
|
328
|
+
collisionPadding = CONTENT_MARGIN,
|
|
329
|
+
sticky,
|
|
330
|
+
hideWhenDetached,
|
|
331
|
+
avoidCollisions,
|
|
332
|
+
asChild,
|
|
333
|
+
style,
|
|
334
|
+
children,
|
|
335
|
+
...contentProps
|
|
336
|
+
} = props;
|
|
337
|
+
const context = useSelectContext('SelectContent');
|
|
338
|
+
const [content, setContent] = useState(null);
|
|
339
|
+
const [viewport, setViewport] = useState(null);
|
|
340
|
+
const composedRefs = useComposedRefs(forwardedRef, setContent);
|
|
341
|
+
useHideOthers(content, true);
|
|
342
|
+
useStripViewportHeight(content);
|
|
343
|
+
const [selectedItem, setSelectedItem] = useState(null);
|
|
344
|
+
const [selectedItemText, setSelectedItemText] = useState(null);
|
|
345
|
+
const getItems = useCollection();
|
|
346
|
+
const [isPositioned, setIsPositioned] = useState(false);
|
|
347
|
+
const firstValidItemFoundRef = useRef(false);
|
|
348
|
+
const wrapper = useStoreState(context.store, 'popoverElement');
|
|
349
|
+
const focusFirst = useCallback(candidates => {
|
|
350
|
+
const [firstItem, ...restItems] = getItems().map(item => item.ref.current);
|
|
351
|
+
const [lastItem] = restItems.slice(-1);
|
|
352
|
+
const previouslyFocusedElement = document.activeElement;
|
|
353
|
+
for (const candidate of candidates) {
|
|
354
|
+
if (candidate === previouslyFocusedElement) {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
candidate?.scrollIntoView({
|
|
358
|
+
block: 'nearest'
|
|
359
|
+
});
|
|
360
|
+
// The viewport may have padding, so it scrolls to its edges for the first and last items.
|
|
361
|
+
if (candidate === firstItem && viewport) {
|
|
362
|
+
viewport.scrollTop = 0;
|
|
363
|
+
}
|
|
364
|
+
if (candidate === lastItem && viewport) {
|
|
365
|
+
viewport.scrollTop = viewport.scrollHeight;
|
|
366
|
+
}
|
|
367
|
+
candidate?.focus();
|
|
368
|
+
if (document.activeElement !== previouslyFocusedElement) {
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}, [getItems, viewport]);
|
|
373
|
+
const focusSelectedItem = useCallback(() => focusFirst([selectedItem, content]), [focusFirst, selectedItem, content]);
|
|
374
|
+
// Not dependent on layout, so it runs with the other effects instead of in `onPlaced`.
|
|
375
|
+
useEffect(() => {
|
|
376
|
+
if (isPositioned) {
|
|
377
|
+
focusSelectedItem();
|
|
378
|
+
}
|
|
379
|
+
}, [isPositioned, focusSelectedItem]);
|
|
380
|
+
// After opening on `pointerdown`, a `pointerup` close to it doesn't select an item, and one outside closes.
|
|
381
|
+
const {
|
|
382
|
+
onOpenChange,
|
|
383
|
+
triggerPointerDownPosRef
|
|
384
|
+
} = context;
|
|
385
|
+
useEffect(() => {
|
|
386
|
+
if (!content) {
|
|
387
|
+
return undefined;
|
|
388
|
+
}
|
|
389
|
+
let pointerMoveDelta = {
|
|
390
|
+
x: 0,
|
|
391
|
+
y: 0
|
|
392
|
+
};
|
|
393
|
+
const handlePointerMove = event => {
|
|
394
|
+
pointerMoveDelta = {
|
|
395
|
+
x: Math.abs(Math.round(event.pageX) - (triggerPointerDownPosRef.current?.x ?? 0)),
|
|
396
|
+
y: Math.abs(Math.round(event.pageY) - (triggerPointerDownPosRef.current?.y ?? 0))
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
const handlePointerUp = event => {
|
|
400
|
+
if (pointerMoveDelta.x <= 10 && pointerMoveDelta.y <= 10) {
|
|
401
|
+
event.preventDefault();
|
|
402
|
+
} else if (!content.contains(event.target)) {
|
|
403
|
+
onOpenChange(false);
|
|
404
|
+
}
|
|
405
|
+
document.removeEventListener('pointermove', handlePointerMove);
|
|
406
|
+
triggerPointerDownPosRef.current = null;
|
|
407
|
+
};
|
|
408
|
+
if (triggerPointerDownPosRef.current !== null) {
|
|
409
|
+
document.addEventListener('pointermove', handlePointerMove);
|
|
410
|
+
document.addEventListener('pointerup', handlePointerUp, {
|
|
411
|
+
capture: true,
|
|
412
|
+
once: true
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
return () => {
|
|
416
|
+
document.removeEventListener('pointermove', handlePointerMove);
|
|
417
|
+
document.removeEventListener('pointerup', handlePointerUp, {
|
|
418
|
+
capture: true
|
|
419
|
+
});
|
|
420
|
+
};
|
|
421
|
+
}, [content, onOpenChange, triggerPointerDownPosRef]);
|
|
422
|
+
useEffect(() => {
|
|
423
|
+
const close = () => onOpenChange(false);
|
|
424
|
+
window.addEventListener('blur', close);
|
|
425
|
+
window.addEventListener('resize', close);
|
|
426
|
+
return () => {
|
|
427
|
+
window.removeEventListener('blur', close);
|
|
428
|
+
window.removeEventListener('resize', close);
|
|
429
|
+
};
|
|
430
|
+
}, [onOpenChange]);
|
|
431
|
+
const [searchRef, handleTypeaheadSearch] = useTypeaheadSearch(search => {
|
|
432
|
+
const enabledItems = getItems().filter(item => !item.disabled);
|
|
433
|
+
const currentItem = enabledItems.find(item => item.ref.current === document.activeElement);
|
|
434
|
+
const nextItem = findNextItem(enabledItems, search, currentItem);
|
|
435
|
+
if (nextItem) {
|
|
436
|
+
// Focus is moved outside of the keydown handler, like in Radix.
|
|
437
|
+
setTimeout(() => nextItem.ref.current?.focus());
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
const itemRefCallback = useCallback((node, value, disabled) => {
|
|
441
|
+
const isFirstValidItem = !firstValidItemFoundRef.current && !disabled;
|
|
442
|
+
const isSelectedItem = context.value !== undefined && context.value === value;
|
|
443
|
+
if (isSelectedItem || isFirstValidItem) {
|
|
444
|
+
setSelectedItem(node);
|
|
445
|
+
if (isFirstValidItem) {
|
|
446
|
+
firstValidItemFoundRef.current = true;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}, [context.value]);
|
|
450
|
+
const handleItemLeave = useCallback(() => content?.focus(), [content]);
|
|
451
|
+
const itemTextRefCallback = useCallback((node, value, disabled) => {
|
|
452
|
+
const isFirstValidItem = !firstValidItemFoundRef.current && !disabled;
|
|
453
|
+
const isSelectedItem = context.value !== undefined && context.value === value;
|
|
454
|
+
if (isSelectedItem || isFirstValidItem) {
|
|
455
|
+
setSelectedItemText(node);
|
|
456
|
+
}
|
|
457
|
+
}, [context.value]);
|
|
458
|
+
const focusScope = useFocusScope({
|
|
459
|
+
container: content,
|
|
460
|
+
// Focus is only trapped while open.
|
|
461
|
+
trapped: context.open,
|
|
462
|
+
// The selected item gets focus once the content is positioned.
|
|
463
|
+
onMountAutoFocus: event => event.preventDefault(),
|
|
464
|
+
onUnmountAutoFocus: composeEventHandlers(onCloseAutoFocus, event => {
|
|
465
|
+
context.trigger?.focus({
|
|
466
|
+
preventScroll: true
|
|
467
|
+
});
|
|
468
|
+
event.preventDefault();
|
|
469
|
+
})
|
|
470
|
+
});
|
|
471
|
+
const dismissable = useDismissable({
|
|
472
|
+
open: context.open,
|
|
473
|
+
disableOutsidePointerEvents: true,
|
|
474
|
+
onDismiss: () => context.onOpenChange(false),
|
|
475
|
+
onEscapeKeyDown,
|
|
476
|
+
onPointerDownOutside,
|
|
477
|
+
// A `focusout` event may still happen while focus is trapped.
|
|
478
|
+
onFocusOutside: event => event.preventDefault()
|
|
479
|
+
});
|
|
480
|
+
const popper = usePopper({
|
|
481
|
+
anchor: context.trigger,
|
|
482
|
+
wrapper,
|
|
483
|
+
content,
|
|
484
|
+
side,
|
|
485
|
+
sideOffset,
|
|
486
|
+
align,
|
|
487
|
+
alignOffset,
|
|
488
|
+
arrowPadding,
|
|
489
|
+
avoidCollisions,
|
|
490
|
+
collisionBoundary,
|
|
491
|
+
collisionPadding,
|
|
492
|
+
sticky,
|
|
493
|
+
hideWhenDetached,
|
|
494
|
+
onPlaced: () => setIsPositioned(true)
|
|
495
|
+
});
|
|
496
|
+
const collectionRef = useCollectionRef();
|
|
497
|
+
const contentContextValue = useMemo(() => ({
|
|
498
|
+
content,
|
|
499
|
+
viewport,
|
|
500
|
+
onViewportChange: setViewport,
|
|
501
|
+
itemRefCallback,
|
|
502
|
+
selectedItem,
|
|
503
|
+
onItemLeave: handleItemLeave,
|
|
504
|
+
itemTextRefCallback,
|
|
505
|
+
focusSelectedItem,
|
|
506
|
+
selectedItemText,
|
|
507
|
+
position,
|
|
508
|
+
isPositioned,
|
|
509
|
+
searchRef
|
|
510
|
+
}), [content, viewport, itemRefCallback, selectedItem, handleItemLeave, itemTextRefCallback, focusSelectedItem, selectedItemText, position, isPositioned, searchRef]);
|
|
511
|
+
const asChildRender = getAsChildRender(asChild, children, contentChildren => jsxs(Fragment, {
|
|
512
|
+
children: [jsx(AllowAutoFocus, {
|
|
513
|
+
children: contentChildren
|
|
514
|
+
}), jsx(ContentCollectionRef, {
|
|
515
|
+
collectionRef: collectionRef
|
|
516
|
+
}), jsx(DismissMarker, {})]
|
|
517
|
+
}));
|
|
518
|
+
return jsx(SelectContentContext.Provider, {
|
|
519
|
+
value: contentContextValue,
|
|
520
|
+
children: jsx(PopperArrowProvider, {
|
|
521
|
+
value: popper.arrowContext,
|
|
522
|
+
children: jsx(RemoveScroll, {
|
|
523
|
+
allowPinchZoom: true,
|
|
524
|
+
as: Slot,
|
|
525
|
+
children: jsx(Popover, {
|
|
526
|
+
alwaysVisible: true,
|
|
527
|
+
autoFocusOnHide: false,
|
|
528
|
+
autoFocusOnShow: false,
|
|
529
|
+
backdrop: false,
|
|
530
|
+
"data-align": popper.contentProps['data-align'],
|
|
531
|
+
"data-side": popper.contentProps['data-side'],
|
|
532
|
+
"data-state": context.open ? 'open' : 'closed',
|
|
533
|
+
dir: context.dir,
|
|
534
|
+
getPersistentElements: getPersistentElements,
|
|
535
|
+
hideOnEscape: false,
|
|
536
|
+
hideOnInteractOutside: dismissable.hideOnInteractOutside,
|
|
537
|
+
id: context.contentId,
|
|
538
|
+
modal: true,
|
|
539
|
+
onContextMenu: preventContextMenu,
|
|
540
|
+
portal: false,
|
|
541
|
+
// Scrolling is locked with `RemoveScroll`, like in Radix.
|
|
542
|
+
preventBodyScroll: false,
|
|
543
|
+
render: asChildRender.render,
|
|
544
|
+
role: "listbox",
|
|
545
|
+
store: context.store,
|
|
546
|
+
updatePosition: noopUpdatePosition,
|
|
547
|
+
...contentProps,
|
|
548
|
+
// `wrapperProps` isn't Radix API, so a consumer's mustn't replace the positioning ones.
|
|
549
|
+
wrapperProps: popper.wrapperProps,
|
|
550
|
+
ref: composedRefs,
|
|
551
|
+
onKeyDown: composeEventHandlers(composeEventHandlers(contentProps.onKeyDown, event => {
|
|
552
|
+
const isModifierKey = event.ctrlKey || event.altKey || event.metaKey;
|
|
553
|
+
// Selects aren't navigated with the Tab key.
|
|
554
|
+
if (event.key === 'Tab') {
|
|
555
|
+
event.preventDefault();
|
|
556
|
+
}
|
|
557
|
+
if (!isModifierKey && event.key.length === 1) {
|
|
558
|
+
handleTypeaheadSearch(event.key);
|
|
559
|
+
}
|
|
560
|
+
if (['ArrowUp', 'ArrowDown', 'Home', 'End'].includes(event.key)) {
|
|
561
|
+
const items = getItems().filter(item => !item.disabled);
|
|
562
|
+
let candidateNodes = items.map(item => item.ref.current);
|
|
563
|
+
if (['ArrowUp', 'End'].includes(event.key)) {
|
|
564
|
+
candidateNodes = candidateNodes.slice().reverse();
|
|
565
|
+
}
|
|
566
|
+
if (['ArrowUp', 'ArrowDown'].includes(event.key)) {
|
|
567
|
+
const currentIndex = candidateNodes.indexOf(event.target);
|
|
568
|
+
candidateNodes = candidateNodes.slice(currentIndex + 1);
|
|
569
|
+
}
|
|
570
|
+
// Focus is moved outside of the keydown handler, like in Radix.
|
|
571
|
+
setTimeout(() => focusFirst(candidateNodes));
|
|
572
|
+
event.preventDefault();
|
|
573
|
+
}
|
|
574
|
+
}), focusScope.onKeyDown),
|
|
575
|
+
onBlurCapture: composeEventHandlers(contentProps.onBlurCapture, dismissable.contentProps.onBlurCapture),
|
|
576
|
+
onFocusCapture: composeEventHandlers(contentProps.onFocusCapture, dismissable.contentProps.onFocusCapture),
|
|
577
|
+
onPointerDownCapture: composeEventHandlers(contentProps.onPointerDownCapture, dismissable.contentProps.onPointerDownCapture),
|
|
578
|
+
style: {
|
|
579
|
+
// A flex layout places scroll buttons, and the outline is reset as the content may get focus.
|
|
580
|
+
display: 'flex',
|
|
581
|
+
flexDirection: 'column',
|
|
582
|
+
outline: 'none',
|
|
583
|
+
// floating-ui measures the border box.
|
|
584
|
+
boxSizing: 'border-box',
|
|
585
|
+
...style,
|
|
586
|
+
...getPopperCssVariables('select'),
|
|
587
|
+
...popper.contentProps.style
|
|
588
|
+
},
|
|
589
|
+
children: asChildRender.children
|
|
590
|
+
})
|
|
591
|
+
})
|
|
592
|
+
})
|
|
593
|
+
});
|
|
594
|
+
});
|
|
595
|
+
/** Makes the content element the collection container without adding an element. */
|
|
596
|
+
const ContentCollectionRef = ({
|
|
597
|
+
collectionRef
|
|
598
|
+
}) => {
|
|
599
|
+
const {
|
|
600
|
+
content
|
|
601
|
+
} = useContext(SelectContentContext) ?? {};
|
|
602
|
+
useEnhancedEffect(() => {
|
|
603
|
+
collectionRef(content ?? null);
|
|
604
|
+
}, [collectionRef, content]);
|
|
605
|
+
return null;
|
|
606
|
+
};
|
|
607
|
+
const Viewport = /*#__PURE__*/forwardRef(({
|
|
608
|
+
asChild,
|
|
609
|
+
style,
|
|
610
|
+
...viewportProps
|
|
611
|
+
}, forwardedRef) => {
|
|
612
|
+
const contentContext = useContext(SelectContentContext);
|
|
613
|
+
const composedRefs = useComposedRefs(forwardedRef, contentContext?.onViewportChange);
|
|
614
|
+
const Comp = asChild ? Slot : 'div';
|
|
615
|
+
return jsxs(Fragment, {
|
|
616
|
+
children: [jsx("style", {
|
|
617
|
+
dangerouslySetInnerHTML: {
|
|
618
|
+
__html: `[data-radix-select-viewport]{scrollbar-width:none;-ms-overflow-style:none;-webkit-overflow-scrolling:touch;}[data-radix-select-viewport]::-webkit-scrollbar{display:none}`
|
|
619
|
+
}
|
|
620
|
+
}), jsx(Comp, {
|
|
621
|
+
"data-radix-select-viewport": "",
|
|
622
|
+
role: "presentation",
|
|
623
|
+
...viewportProps,
|
|
624
|
+
ref: composedRefs,
|
|
625
|
+
style: {
|
|
626
|
+
// A relative position makes `offsetTop` of items relative to the viewport.
|
|
627
|
+
position: 'relative',
|
|
628
|
+
flex: 1,
|
|
629
|
+
overflow: 'auto',
|
|
630
|
+
...style
|
|
631
|
+
}
|
|
632
|
+
})]
|
|
633
|
+
});
|
|
634
|
+
});
|
|
635
|
+
Viewport.displayName = 'SelectViewport';
|
|
636
|
+
/* -------------------------------------------------------------------------------------------------
|
|
637
|
+
* Group, Label
|
|
638
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
639
|
+
const GroupContext = /*#__PURE__*/createContext(null);
|
|
640
|
+
const Group = /*#__PURE__*/forwardRef(({
|
|
641
|
+
asChild,
|
|
642
|
+
...groupProps
|
|
643
|
+
}, forwardedRef) => {
|
|
644
|
+
const groupId = useId();
|
|
645
|
+
const Comp = asChild ? Slot : 'div';
|
|
646
|
+
return jsx(GroupContext.Provider, {
|
|
647
|
+
value: {
|
|
648
|
+
id: groupId
|
|
649
|
+
},
|
|
650
|
+
children: jsx(Comp, {
|
|
651
|
+
"aria-labelledby": groupId,
|
|
652
|
+
role: "group",
|
|
653
|
+
...groupProps,
|
|
654
|
+
ref: forwardedRef
|
|
655
|
+
})
|
|
656
|
+
});
|
|
657
|
+
});
|
|
658
|
+
Group.displayName = 'SelectGroup';
|
|
659
|
+
const Label = /*#__PURE__*/forwardRef(({
|
|
660
|
+
asChild,
|
|
661
|
+
...labelProps
|
|
662
|
+
}, forwardedRef) => {
|
|
663
|
+
const groupContext = useContext(GroupContext);
|
|
664
|
+
const Comp = asChild ? Slot : 'div';
|
|
665
|
+
return jsx(Comp, {
|
|
666
|
+
id: groupContext?.id,
|
|
667
|
+
...labelProps,
|
|
668
|
+
ref: forwardedRef
|
|
669
|
+
});
|
|
670
|
+
});
|
|
671
|
+
Label.displayName = 'SelectLabel';
|
|
672
|
+
const SelectItemContext = /*#__PURE__*/createContext(null);
|
|
673
|
+
const Item = /*#__PURE__*/forwardRef((props, forwardedRef) => {
|
|
674
|
+
const {
|
|
675
|
+
value,
|
|
676
|
+
disabled = false,
|
|
677
|
+
textValue: textValueProp,
|
|
678
|
+
asChild,
|
|
679
|
+
...itemProps
|
|
680
|
+
} = props;
|
|
681
|
+
const context = useSelectContext('SelectItem');
|
|
682
|
+
const contentContext = useContext(SelectContentContext);
|
|
683
|
+
const isSelected = context.value === value;
|
|
684
|
+
const [textValue, setTextValue] = useState(textValueProp ?? '');
|
|
685
|
+
const [isFocused, setIsFocused] = useState(false);
|
|
686
|
+
const ref = useRef(null);
|
|
687
|
+
const itemRefCallback = useCallback(node => contentContext?.itemRefCallback(node, value, disabled), [contentContext, value, disabled]);
|
|
688
|
+
const composedRefs = useComposedRefs(forwardedRef, ref, itemRefCallback);
|
|
689
|
+
const textId = useId();
|
|
690
|
+
const collectionItemProps = useCollectionItem(ref, {
|
|
691
|
+
value,
|
|
692
|
+
disabled,
|
|
693
|
+
textValue
|
|
694
|
+
});
|
|
695
|
+
const handleSelect = () => {
|
|
696
|
+
if (!disabled) {
|
|
697
|
+
context.onValueChange(value);
|
|
698
|
+
context.onOpenChange(false);
|
|
699
|
+
}
|
|
700
|
+
};
|
|
701
|
+
if (value === '') {
|
|
702
|
+
throw new Error('A <Select.Item /> must have a value prop that is not an empty string. This is because the Select value can be set to an empty string to clear the selection and show the placeholder.');
|
|
703
|
+
}
|
|
704
|
+
const onItemTextChange = useCallback(node => {
|
|
705
|
+
setTextValue(prevTextValue => prevTextValue || (node?.textContent ?? '').trim());
|
|
706
|
+
}, []);
|
|
707
|
+
const itemContextValue = useMemo(() => ({
|
|
708
|
+
value,
|
|
709
|
+
disabled,
|
|
710
|
+
textId,
|
|
711
|
+
isSelected,
|
|
712
|
+
onItemTextChange
|
|
713
|
+
}), [value, disabled, textId, isSelected, onItemTextChange]);
|
|
714
|
+
const Comp = asChild ? Slot : 'div';
|
|
715
|
+
return jsx(SelectItemContext.Provider, {
|
|
716
|
+
value: itemContextValue,
|
|
717
|
+
children: jsx(Comp, {
|
|
718
|
+
...collectionItemProps,
|
|
719
|
+
"aria-disabled": disabled || undefined,
|
|
720
|
+
"aria-labelledby": textId,
|
|
721
|
+
"aria-selected": isSelected && isFocused,
|
|
722
|
+
"data-disabled": disabled ? '' : undefined,
|
|
723
|
+
"data-highlighted": isFocused ? '' : undefined,
|
|
724
|
+
"data-state": isSelected ? 'checked' : 'unchecked',
|
|
725
|
+
role: "option",
|
|
726
|
+
tabIndex: disabled ? undefined : -1,
|
|
727
|
+
...itemProps,
|
|
728
|
+
ref: composedRefs,
|
|
729
|
+
onBlur: composeEventHandlers(itemProps.onBlur, () => setIsFocused(false)),
|
|
730
|
+
onFocus: composeEventHandlers(itemProps.onFocus, () => setIsFocused(true)),
|
|
731
|
+
onKeyDown: composeEventHandlers(itemProps.onKeyDown, event => {
|
|
732
|
+
const isTypingAhead = contentContext?.searchRef?.current !== '';
|
|
733
|
+
if (isTypingAhead && event.key === ' ') {
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
if (SELECTION_KEYS.includes(event.key)) {
|
|
737
|
+
handleSelect();
|
|
738
|
+
}
|
|
739
|
+
// Space doesn't scroll the page.
|
|
740
|
+
if (event.key === ' ') {
|
|
741
|
+
event.preventDefault();
|
|
742
|
+
}
|
|
743
|
+
}),
|
|
744
|
+
onPointerLeave: composeEventHandlers(itemProps.onPointerLeave, event => {
|
|
745
|
+
if (event.currentTarget === document.activeElement) {
|
|
746
|
+
contentContext?.onItemLeave();
|
|
747
|
+
}
|
|
748
|
+
}),
|
|
749
|
+
onPointerMove: composeEventHandlers(itemProps.onPointerMove, event => {
|
|
750
|
+
if (disabled) {
|
|
751
|
+
contentContext?.onItemLeave();
|
|
752
|
+
} else {
|
|
753
|
+
event.currentTarget.focus({
|
|
754
|
+
preventScroll: true
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
}),
|
|
758
|
+
onPointerUp: composeEventHandlers(itemProps.onPointerUp, handleSelect)
|
|
759
|
+
})
|
|
760
|
+
});
|
|
761
|
+
});
|
|
762
|
+
Item.displayName = 'SelectItem';
|
|
763
|
+
const ItemText = /*#__PURE__*/forwardRef((props, forwardedRef) => {
|
|
764
|
+
// `className` and `style` are ignored, as this part isn't meant to be styled.
|
|
765
|
+
const {
|
|
766
|
+
className: _className,
|
|
767
|
+
style: _style,
|
|
768
|
+
asChild,
|
|
769
|
+
...itemTextProps
|
|
770
|
+
} = props;
|
|
771
|
+
const context = useSelectContext('SelectItemText');
|
|
772
|
+
const contentContext = useContext(SelectContentContext);
|
|
773
|
+
const itemContext = useContext(SelectItemContext);
|
|
774
|
+
const nativeOptionsContext = useContext(NativeOptionsContext);
|
|
775
|
+
if (!itemContext || !nativeOptionsContext) {
|
|
776
|
+
throw new Error('`SelectItemText` must be used within `SelectItem`');
|
|
777
|
+
}
|
|
778
|
+
const [itemTextNode, setItemTextNode] = useState(null);
|
|
779
|
+
const itemTextRefCallback = useCallback(node => contentContext?.itemTextRefCallback(node, itemContext.value, itemContext.disabled), [contentContext, itemContext.value, itemContext.disabled]);
|
|
780
|
+
const composedRefs = useComposedRefs(forwardedRef, setItemTextNode, itemContext.onItemTextChange, itemTextRefCallback);
|
|
781
|
+
const textContent = itemTextNode?.textContent;
|
|
782
|
+
const nativeOption = useMemo(() => jsx("option", {
|
|
783
|
+
disabled: itemContext.disabled,
|
|
784
|
+
value: itemContext.value,
|
|
785
|
+
children: textContent
|
|
786
|
+
}, itemContext.value), [itemContext.disabled, itemContext.value, textContent]);
|
|
787
|
+
const {
|
|
788
|
+
onNativeOptionAdd,
|
|
789
|
+
onNativeOptionRemove
|
|
790
|
+
} = nativeOptionsContext;
|
|
791
|
+
useEnhancedEffect(() => {
|
|
792
|
+
onNativeOptionAdd(nativeOption);
|
|
793
|
+
return () => onNativeOptionRemove(nativeOption);
|
|
794
|
+
}, [onNativeOptionAdd, onNativeOptionRemove, nativeOption]);
|
|
795
|
+
const Comp = asChild ? Slot : 'span';
|
|
796
|
+
return jsxs(Fragment, {
|
|
797
|
+
children: [jsx(Comp, {
|
|
798
|
+
id: itemContext.textId,
|
|
799
|
+
...itemTextProps,
|
|
800
|
+
ref: composedRefs
|
|
801
|
+
}), itemContext.isSelected && context.valueNode && !context.valueNodeHasChildren ? /*#__PURE__*/createPortal(itemTextProps.children, context.valueNode) : null]
|
|
802
|
+
});
|
|
803
|
+
});
|
|
804
|
+
ItemText.displayName = 'SelectItemText';
|
|
805
|
+
const ItemIndicator = /*#__PURE__*/forwardRef(({
|
|
806
|
+
asChild,
|
|
807
|
+
...itemIndicatorProps
|
|
808
|
+
}, forwardedRef) => {
|
|
809
|
+
const itemContext = useContext(SelectItemContext);
|
|
810
|
+
const Comp = asChild ? Slot : 'span';
|
|
811
|
+
return itemContext?.isSelected ? jsx(Comp, {
|
|
812
|
+
"aria-hidden": true,
|
|
813
|
+
...itemIndicatorProps,
|
|
814
|
+
ref: forwardedRef
|
|
815
|
+
}) : null;
|
|
816
|
+
});
|
|
817
|
+
ItemIndicator.displayName = 'SelectItemIndicator';
|
|
818
|
+
const Separator = /*#__PURE__*/forwardRef(({
|
|
819
|
+
asChild,
|
|
820
|
+
...separatorProps
|
|
821
|
+
}, forwardedRef) => {
|
|
822
|
+
const Comp = asChild ? Slot : 'div';
|
|
823
|
+
return jsx(Comp, {
|
|
824
|
+
"aria-hidden": true,
|
|
825
|
+
...separatorProps,
|
|
826
|
+
ref: forwardedRef
|
|
827
|
+
});
|
|
828
|
+
});
|
|
829
|
+
Separator.displayName = 'SelectSeparator';
|
|
830
|
+
const Arrow = /*#__PURE__*/forwardRef((props, forwardedRef) => {
|
|
831
|
+
const context = useSelectContext('SelectArrow');
|
|
832
|
+
const isClosedContent = useContext(ClosedContentContext);
|
|
833
|
+
return context.open && !isClosedContent ? jsx(PopperArrow, {
|
|
834
|
+
...props,
|
|
835
|
+
ref: forwardedRef
|
|
836
|
+
}) : null;
|
|
837
|
+
});
|
|
838
|
+
Arrow.displayName = 'SelectArrow';
|
|
839
|
+
function usePrevious(value) {
|
|
840
|
+
const ref = useRef({
|
|
841
|
+
value,
|
|
842
|
+
previous: value
|
|
843
|
+
});
|
|
844
|
+
if (ref.current.value !== value) {
|
|
845
|
+
ref.current.previous = ref.current.value;
|
|
846
|
+
ref.current.value = value;
|
|
847
|
+
}
|
|
848
|
+
return ref.current.previous;
|
|
849
|
+
}
|
|
850
|
+
const visuallyHiddenStyle = {
|
|
851
|
+
position: 'absolute',
|
|
852
|
+
border: 0,
|
|
853
|
+
width: 1,
|
|
854
|
+
height: 1,
|
|
855
|
+
padding: 0,
|
|
856
|
+
margin: -1,
|
|
857
|
+
overflow: 'hidden',
|
|
858
|
+
clip: 'rect(0, 0, 0, 0)',
|
|
859
|
+
whiteSpace: 'nowrap',
|
|
860
|
+
wordWrap: 'normal'
|
|
861
|
+
};
|
|
862
|
+
/**
|
|
863
|
+
* Hidden native select that keeps the value in forms (submission, autofill and `change` events).
|
|
864
|
+
* It has no `value` attribute, so programmatic changes bubble to the form's `onChange` instead of being swallowed.
|
|
865
|
+
*/
|
|
866
|
+
const BubbleSelect = /*#__PURE__*/forwardRef(({
|
|
867
|
+
value,
|
|
868
|
+
...selectProps
|
|
869
|
+
}, forwardedRef) => {
|
|
870
|
+
const ref = useRef(null);
|
|
871
|
+
const composedRefs = useComposedRefs(forwardedRef, ref);
|
|
872
|
+
const prevValue = usePrevious(value);
|
|
873
|
+
useEffect(() => {
|
|
874
|
+
const select = ref.current;
|
|
875
|
+
if (!select) {
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
const descriptor = Object.getOwnPropertyDescriptor(window.HTMLSelectElement.prototype, 'value');
|
|
879
|
+
const setValue = descriptor?.set;
|
|
880
|
+
if (prevValue !== value && setValue) {
|
|
881
|
+
const event = new Event('change', {
|
|
882
|
+
bubbles: true
|
|
883
|
+
});
|
|
884
|
+
setValue.call(select, value);
|
|
885
|
+
select.dispatchEvent(event);
|
|
886
|
+
}
|
|
887
|
+
}, [prevValue, value]);
|
|
888
|
+
return jsx("select", {
|
|
889
|
+
...selectProps,
|
|
890
|
+
ref: composedRefs,
|
|
891
|
+
defaultValue: value,
|
|
892
|
+
// A visually hidden select works with Safari autofill, unlike `display: none`.
|
|
893
|
+
style: {
|
|
894
|
+
...visuallyHiddenStyle,
|
|
895
|
+
...selectProps.style
|
|
896
|
+
}
|
|
897
|
+
});
|
|
898
|
+
});
|
|
899
|
+
BubbleSelect.displayName = 'BubbleSelect';
|
|
900
|
+
|
|
901
|
+
export { Arrow, Content, Group, Icon, Item, ItemIndicator, ItemText, Label, Portal, Root, Separator, Trigger, Value, Viewport };
|