@navikt/ds-react 6.13.0 → 6.15.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/cjs/accordion/AccordionContext.d.ts +0 -1
- package/cjs/collapsible/Collapsible.context.d.ts +0 -1
- package/cjs/date/context/useDateInputContext.d.ts +0 -1
- package/cjs/date/datepicker/parts/HeadRow.d.ts +0 -1
- package/cjs/date/datepicker/parts/HeadRow.js +2 -3
- package/cjs/date/datepicker/parts/HeadRow.js.map +1 -1
- package/cjs/date/datepicker/parts/Row.d.ts +0 -1
- package/cjs/date/datepicker/parts/TableHead.d.ts +0 -1
- package/cjs/date/datepicker/parts/WeekNumber.d.ts +0 -1
- package/cjs/date/datepicker/types.d.ts +0 -1
- package/cjs/date/monthpicker/types.d.ts +0 -1
- package/cjs/date/utils/check-dates.js +2 -2
- package/cjs/date/utils/check-dates.js.map +1 -1
- package/cjs/date/utils/get-initial-year.js +1 -2
- package/cjs/date/utils/get-initial-year.js.map +1 -1
- package/cjs/date/utils/get-month-weeks.js +2 -3
- package/cjs/date/utils/get-month-weeks.js.map +1 -1
- package/cjs/date/utils/is-match.js +2 -3
- package/cjs/date/utils/is-match.js.map +1 -1
- package/cjs/dropdown/Menu/index.d.ts +1 -1
- package/cjs/dropdown/context.d.ts +0 -1
- package/cjs/expansion-card/context.d.ts +0 -1
- package/cjs/form/checkbox/useCheckbox.d.ts +3 -3
- package/cjs/form/combobox/Combobox.d.ts +1 -1
- package/cjs/form/combobox/Combobox.js.map +1 -1
- package/cjs/form/combobox/ComboboxProvider.js +3 -1
- package/cjs/form/combobox/ComboboxProvider.js.map +1 -1
- package/cjs/form/combobox/FilteredOptions/AddNewOption.d.ts +3 -0
- package/cjs/form/combobox/FilteredOptions/AddNewOption.js +41 -0
- package/cjs/form/combobox/FilteredOptions/AddNewOption.js.map +1 -0
- package/cjs/form/combobox/FilteredOptions/FilteredOptions.js +13 -57
- package/cjs/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
- package/cjs/form/combobox/FilteredOptions/FilteredOptionsItem.d.ts +6 -0
- package/cjs/form/combobox/FilteredOptions/FilteredOptionsItem.js +43 -0
- package/cjs/form/combobox/FilteredOptions/FilteredOptionsItem.js.map +1 -0
- package/cjs/form/combobox/FilteredOptions/LoadingMessage.d.ts +3 -0
- package/cjs/form/combobox/FilteredOptions/LoadingMessage.js +16 -0
- package/cjs/form/combobox/FilteredOptions/LoadingMessage.js.map +1 -0
- package/cjs/form/combobox/FilteredOptions/MaxSelectedMessage.d.ts +3 -0
- package/cjs/form/combobox/FilteredOptions/MaxSelectedMessage.js +20 -0
- package/cjs/form/combobox/FilteredOptions/MaxSelectedMessage.js.map +1 -0
- package/cjs/form/combobox/FilteredOptions/NoSearchHitsMessage.d.ts +3 -0
- package/cjs/form/combobox/FilteredOptions/NoSearchHitsMessage.js +14 -0
- package/cjs/form/combobox/FilteredOptions/NoSearchHitsMessage.js.map +1 -0
- package/cjs/form/combobox/FilteredOptions/filtered-options-util.d.ts +1 -0
- package/cjs/form/combobox/FilteredOptions/filtered-options-util.js +6 -1
- package/cjs/form/combobox/FilteredOptions/filtered-options-util.js.map +1 -1
- package/cjs/form/combobox/FilteredOptions/filteredOptionsContext.js +5 -5
- package/cjs/form/combobox/FilteredOptions/filteredOptionsContext.js.map +1 -1
- package/cjs/form/combobox/Input/Input.context.d.ts +20 -7
- package/cjs/form/combobox/Input/Input.context.js +6 -12
- package/cjs/form/combobox/Input/Input.context.js.map +1 -1
- package/cjs/form/combobox/Input/Input.d.ts +2 -1
- package/cjs/form/combobox/Input/Input.js +45 -20
- package/cjs/form/combobox/Input/Input.js.map +1 -1
- package/cjs/form/combobox/Input/InputController.d.ts +1 -1
- package/cjs/form/combobox/Input/InputController.js +1 -1
- package/cjs/form/combobox/Input/InputController.js.map +1 -1
- package/cjs/form/combobox/types.d.ts +8 -4
- package/cjs/form/fieldset/context.d.ts +0 -1
- package/cjs/form/fieldset/useFieldset.d.ts +1 -1
- package/cjs/form/file-upload/FileUpload.context.d.ts +0 -1
- package/cjs/form/file-upload/parts/dropzone/dropzone.types.d.ts +0 -1
- package/cjs/form/file-upload/parts/item/utils/format-file-size.js +1 -2
- package/cjs/form/file-upload/parts/item/utils/format-file-size.js.map +1 -1
- package/cjs/form/file-upload/useFileUpload.d.ts +1 -2
- package/cjs/form/file-upload/utils/is-accepted-file-type.js +1 -2
- package/cjs/form/file-upload/utils/is-accepted-file-type.js.map +1 -1
- package/cjs/form/file-upload/utils/is-accepted-size.js +1 -2
- package/cjs/form/file-upload/utils/is-accepted-size.js.map +1 -1
- package/cjs/form/radio/useRadio.d.ts +3 -3
- package/cjs/form/search/context.d.ts +0 -1
- package/cjs/layout/base/PrimitiveAsChildProps.d.ts +0 -1
- package/cjs/layout/grid/HGrid.js +4 -1
- package/cjs/layout/grid/HGrid.js.map +1 -1
- package/cjs/layout/stack/Stack.js +7 -2
- package/cjs/layout/stack/Stack.js.map +1 -1
- package/cjs/layout/utilities/css.js +2 -3
- package/cjs/layout/utilities/css.js.map +1 -1
- package/cjs/list/context.d.ts +0 -1
- package/cjs/list/types.d.ts +0 -1
- package/cjs/modal/ModalUtils.js +3 -3
- package/cjs/modal/ModalUtils.js.map +1 -1
- package/cjs/modal/types.d.ts +0 -1
- package/cjs/overlays/dismissablelayer/DismissableLayer.d.ts +1 -1
- package/cjs/overlays/dismissablelayer/util/dispatchCustomEvent.js +2 -2
- package/cjs/overlays/dismissablelayer/util/dispatchCustomEvent.js.map +1 -1
- package/cjs/overlays/dismissablelayer/util/useEscapeKeydown.js +1 -2
- package/cjs/overlays/dismissablelayer/util/useEscapeKeydown.js.map +1 -1
- package/cjs/overlays/dismissablelayer/util/useFocusOutside.js +1 -2
- package/cjs/overlays/dismissablelayer/util/useFocusOutside.js.map +1 -1
- package/cjs/overlays/dismissablelayer/util/usePointerDownOutside.js +1 -2
- package/cjs/overlays/dismissablelayer/util/usePointerDownOutside.js.map +1 -1
- package/cjs/overlays/floating/Floating.utils.js +2 -3
- package/cjs/overlays/floating/Floating.utils.js.map +1 -1
- package/cjs/overlays/floating-menu/Menu.d.ts +106 -0
- package/cjs/overlays/floating-menu/Menu.js +593 -0
- package/cjs/overlays/floating-menu/Menu.js.map +1 -0
- package/cjs/overlays/floating-menu/parts/FocusScope.d.ts +22 -0
- package/cjs/overlays/floating-menu/parts/FocusScope.js +89 -0
- package/cjs/overlays/floating-menu/parts/FocusScope.js.map +1 -0
- package/cjs/overlays/floating-menu/parts/RovingFocus.d.ts +9 -0
- package/cjs/overlays/floating-menu/parts/RovingFocus.js +112 -0
- package/cjs/overlays/floating-menu/parts/RovingFocus.js.map +1 -0
- package/cjs/overlays/floating-menu/parts/SlottedDivElement.d.ts +7 -0
- package/cjs/overlays/floating-menu/parts/SlottedDivElement.js +46 -0
- package/cjs/overlays/floating-menu/parts/SlottedDivElement.js.map +1 -0
- package/cjs/slot/merge-props.js +1 -2
- package/cjs/slot/merge-props.js.map +1 -1
- package/cjs/stepper/context.d.ts +0 -1
- package/cjs/table/context.d.ts +0 -1
- package/cjs/tabs/Tabs.context.d.ts +1 -2
- package/cjs/tabs/parts/tab/useTab.d.ts +1 -2
- package/cjs/tabs/parts/tab/useTab.js +1 -2
- package/cjs/tabs/parts/tab/useTab.js.map +1 -1
- package/cjs/tabs/parts/tablist/useScrollButtons.d.ts +0 -1
- package/cjs/tabs/parts/tablist/useScrollButtons.js +1 -2
- package/cjs/tabs/parts/tablist/useScrollButtons.js.map +1 -1
- package/cjs/tabs/parts/tablist/useTabList.js +3 -3
- package/cjs/tabs/parts/tablist/useTabList.js.map +1 -1
- package/cjs/tabs/parts/tabpanel/useTabPanel.js +1 -2
- package/cjs/tabs/parts/tabpanel/useTabPanel.js.map +1 -1
- package/cjs/tabs/useTabs.d.ts +0 -1
- package/cjs/tabs/useTabs.js +1 -2
- package/cjs/tabs/useTabs.js.map +1 -1
- package/cjs/timeline/hooks/usePeriodContext.d.ts +0 -1
- package/cjs/timeline/hooks/useRowContext.d.ts +0 -1
- package/cjs/timeline/hooks/useTimelineContext.d.ts +0 -1
- package/cjs/timeline/period/types.d.ts +0 -1
- package/cjs/timeline/zoom/index.d.ts +1 -1
- package/cjs/toggle-group/ToggleGroup.context.d.ts +1 -2
- package/cjs/toggle-group/parts/useToggleItem.d.ts +1 -2
- package/cjs/toggle-group/parts/useToggleItem.js +3 -3
- package/cjs/toggle-group/parts/useToggleItem.js.map +1 -1
- package/cjs/toggle-group/useToggleGroup.d.ts +0 -1
- package/cjs/toggle-group/useToggleGroup.js +1 -2
- package/cjs/toggle-group/useToggleGroup.js.map +1 -1
- package/cjs/util/composeEventHandlers.d.ts +1 -2
- package/cjs/util/composeEventHandlers.js +1 -2
- package/cjs/util/composeEventHandlers.js.map +1 -1
- package/cjs/util/copy.js +1 -1
- package/cjs/util/copy.js.map +1 -1
- package/cjs/util/create-context.js +1 -2
- package/cjs/util/create-context.js.map +1 -1
- package/cjs/util/debounce.js +1 -1
- package/cjs/util/debounce.js.map +1 -1
- package/cjs/util/hooks/descendants/useDescendant.d.ts +1 -1
- package/cjs/util/hooks/descendants/useDescendant.js +1 -2
- package/cjs/util/hooks/descendants/useDescendant.js.map +1 -1
- package/cjs/util/hooks/descendants/utils.js +4 -4
- package/cjs/util/hooks/descendants/utils.js.map +1 -1
- package/cjs/util/hooks/useCallbackRef.d.ts +0 -1
- package/cjs/util/hooks/useCallbackRef.js +1 -2
- package/cjs/util/hooks/useCallbackRef.js.map +1 -1
- package/cjs/util/hooks/useControllableState.js +1 -2
- package/cjs/util/hooks/useControllableState.js.map +1 -1
- package/cjs/util/hooks/useId.js +1 -2
- package/cjs/util/hooks/useId.js.map +1 -1
- package/cjs/util/hooks/useMergeRefs.d.ts +1 -1
- package/cjs/util/hooks/useMergeRefs.js +2 -3
- package/cjs/util/hooks/useMergeRefs.js.map +1 -1
- package/cjs/util/i18n/get.js +1 -2
- package/cjs/util/i18n/get.js.map +1 -1
- package/cjs/util/i18n/i18n.context.d.ts +0 -1
- package/cjs/util/i18n/i18n.context.js +2 -2
- package/cjs/util/i18n/i18n.context.js.map +1 -1
- package/cjs/util/i18n/merge.js +1 -2
- package/cjs/util/i18n/merge.js.map +1 -1
- package/cjs/util/omit.js +1 -2
- package/cjs/util/omit.js.map +1 -1
- package/cjs/util/types/AsChild.d.ts +0 -1
- package/cjs/util/types/AsChildProps.d.ts +0 -1
- package/cjs/util/virtualfocus/Context.d.ts +43 -0
- package/cjs/util/virtualfocus/Context.js +9 -0
- package/cjs/util/virtualfocus/Context.js.map +1 -0
- package/cjs/util/virtualfocus/SlottedDivElement.d.ts +7 -0
- package/cjs/util/virtualfocus/SlottedDivElement.js +46 -0
- package/cjs/util/virtualfocus/SlottedDivElement.js.map +1 -0
- package/cjs/util/virtualfocus/VirtualFocus.d.ts +62 -0
- package/cjs/util/virtualfocus/VirtualFocus.js +90 -0
- package/cjs/util/virtualfocus/VirtualFocus.js.map +1 -0
- package/cjs/util/virtualfocus/parts/VirtualFocusAnchor.d.ts +33 -0
- package/cjs/util/virtualfocus/parts/VirtualFocusAnchor.js +80 -0
- package/cjs/util/virtualfocus/parts/VirtualFocusAnchor.js.map +1 -0
- package/cjs/util/virtualfocus/parts/VirtualFocusContent.d.ts +4 -0
- package/cjs/util/virtualfocus/parts/VirtualFocusContent.js +45 -0
- package/cjs/util/virtualfocus/parts/VirtualFocusContent.js.map +1 -0
- package/cjs/util/virtualfocus/parts/VirtualFocusItem.d.ts +18 -0
- package/cjs/util/virtualfocus/parts/VirtualFocusItem.js +64 -0
- package/cjs/util/virtualfocus/parts/VirtualFocusItem.js.map +1 -0
- package/esm/accordion/AccordionContext.d.ts +0 -1
- package/esm/collapsible/Collapsible.context.d.ts +0 -1
- package/esm/date/context/useDateInputContext.d.ts +0 -1
- package/esm/date/datepicker/parts/HeadRow.d.ts +0 -1
- package/esm/date/datepicker/parts/Row.d.ts +0 -1
- package/esm/date/datepicker/parts/TableHead.d.ts +0 -1
- package/esm/date/datepicker/parts/WeekNumber.d.ts +0 -1
- package/esm/date/datepicker/types.d.ts +0 -1
- package/esm/date/monthpicker/types.d.ts +0 -1
- package/esm/dropdown/Menu/index.d.ts +1 -1
- package/esm/dropdown/context.d.ts +0 -1
- package/esm/expansion-card/context.d.ts +0 -1
- package/esm/form/checkbox/useCheckbox.d.ts +3 -3
- package/esm/form/combobox/Combobox.d.ts +1 -1
- package/esm/form/combobox/Combobox.js.map +1 -1
- package/esm/form/combobox/ComboboxProvider.js +3 -1
- package/esm/form/combobox/ComboboxProvider.js.map +1 -1
- package/esm/form/combobox/FilteredOptions/AddNewOption.d.ts +3 -0
- package/esm/form/combobox/FilteredOptions/AddNewOption.js +36 -0
- package/esm/form/combobox/FilteredOptions/AddNewOption.js.map +1 -0
- package/esm/form/combobox/FilteredOptions/FilteredOptions.js +13 -57
- package/esm/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
- package/esm/form/combobox/FilteredOptions/FilteredOptionsItem.d.ts +6 -0
- package/esm/form/combobox/FilteredOptions/FilteredOptionsItem.js +38 -0
- package/esm/form/combobox/FilteredOptions/FilteredOptionsItem.js.map +1 -0
- package/esm/form/combobox/FilteredOptions/LoadingMessage.d.ts +3 -0
- package/esm/form/combobox/FilteredOptions/LoadingMessage.js +11 -0
- package/esm/form/combobox/FilteredOptions/LoadingMessage.js.map +1 -0
- package/esm/form/combobox/FilteredOptions/MaxSelectedMessage.d.ts +3 -0
- package/esm/form/combobox/FilteredOptions/MaxSelectedMessage.js +15 -0
- package/esm/form/combobox/FilteredOptions/MaxSelectedMessage.js.map +1 -0
- package/esm/form/combobox/FilteredOptions/NoSearchHitsMessage.d.ts +3 -0
- package/esm/form/combobox/FilteredOptions/NoSearchHitsMessage.js +9 -0
- package/esm/form/combobox/FilteredOptions/NoSearchHitsMessage.js.map +1 -0
- package/esm/form/combobox/FilteredOptions/filtered-options-util.d.ts +1 -0
- package/esm/form/combobox/FilteredOptions/filtered-options-util.js +6 -1
- package/esm/form/combobox/FilteredOptions/filtered-options-util.js.map +1 -1
- package/esm/form/combobox/FilteredOptions/filteredOptionsContext.js +5 -5
- package/esm/form/combobox/FilteredOptions/filteredOptionsContext.js.map +1 -1
- package/esm/form/combobox/Input/Input.context.d.ts +20 -7
- package/esm/form/combobox/Input/Input.context.js +7 -13
- package/esm/form/combobox/Input/Input.context.js.map +1 -1
- package/esm/form/combobox/Input/Input.d.ts +2 -1
- package/esm/form/combobox/Input/Input.js +46 -21
- package/esm/form/combobox/Input/Input.js.map +1 -1
- package/esm/form/combobox/Input/InputController.d.ts +1 -1
- package/esm/form/combobox/Input/InputController.js +1 -1
- package/esm/form/combobox/Input/InputController.js.map +1 -1
- package/esm/form/combobox/types.d.ts +8 -4
- package/esm/form/fieldset/context.d.ts +0 -1
- package/esm/form/fieldset/useFieldset.d.ts +1 -1
- package/esm/form/file-upload/FileUpload.context.d.ts +0 -1
- package/esm/form/file-upload/parts/dropzone/dropzone.types.d.ts +0 -1
- package/esm/form/file-upload/useFileUpload.d.ts +1 -2
- package/esm/form/radio/useRadio.d.ts +3 -3
- package/esm/form/search/context.d.ts +0 -1
- package/esm/layout/base/PrimitiveAsChildProps.d.ts +0 -1
- package/esm/layout/grid/HGrid.js +4 -1
- package/esm/layout/grid/HGrid.js.map +1 -1
- package/esm/layout/stack/Stack.js +7 -2
- package/esm/layout/stack/Stack.js.map +1 -1
- package/esm/list/context.d.ts +0 -1
- package/esm/list/types.d.ts +0 -1
- package/esm/modal/types.d.ts +0 -1
- package/esm/overlays/dismissablelayer/DismissableLayer.d.ts +1 -1
- package/esm/overlays/floating-menu/Menu.d.ts +106 -0
- package/esm/overlays/floating-menu/Menu.js +551 -0
- package/esm/overlays/floating-menu/Menu.js.map +1 -0
- package/esm/overlays/floating-menu/parts/FocusScope.d.ts +22 -0
- package/esm/overlays/floating-menu/parts/FocusScope.js +63 -0
- package/esm/overlays/floating-menu/parts/FocusScope.js.map +1 -0
- package/esm/overlays/floating-menu/parts/RovingFocus.d.ts +9 -0
- package/esm/overlays/floating-menu/parts/RovingFocus.js +86 -0
- package/esm/overlays/floating-menu/parts/RovingFocus.js.map +1 -0
- package/esm/overlays/floating-menu/parts/SlottedDivElement.d.ts +7 -0
- package/esm/overlays/floating-menu/parts/SlottedDivElement.js +20 -0
- package/esm/overlays/floating-menu/parts/SlottedDivElement.js.map +1 -0
- package/esm/stepper/context.d.ts +0 -1
- package/esm/table/context.d.ts +0 -1
- package/esm/tabs/Tabs.context.d.ts +1 -2
- package/esm/tabs/parts/tab/useTab.d.ts +1 -2
- package/esm/tabs/parts/tablist/useScrollButtons.d.ts +0 -1
- package/esm/tabs/parts/tablist/useTabList.js +2 -1
- package/esm/tabs/parts/tablist/useTabList.js.map +1 -1
- package/esm/tabs/useTabs.d.ts +0 -1
- package/esm/timeline/hooks/usePeriodContext.d.ts +0 -1
- package/esm/timeline/hooks/useRowContext.d.ts +0 -1
- package/esm/timeline/hooks/useTimelineContext.d.ts +0 -1
- package/esm/timeline/period/types.d.ts +0 -1
- package/esm/timeline/zoom/index.d.ts +1 -1
- package/esm/toggle-group/ToggleGroup.context.d.ts +1 -2
- package/esm/toggle-group/parts/useToggleItem.d.ts +1 -2
- package/esm/toggle-group/parts/useToggleItem.js +2 -1
- package/esm/toggle-group/parts/useToggleItem.js.map +1 -1
- package/esm/toggle-group/useToggleGroup.d.ts +0 -1
- package/esm/util/composeEventHandlers.d.ts +1 -2
- package/esm/util/hooks/descendants/useDescendant.d.ts +1 -1
- package/esm/util/hooks/useCallbackRef.d.ts +0 -1
- package/esm/util/hooks/useMergeRefs.d.ts +1 -1
- package/esm/util/i18n/i18n.context.d.ts +0 -1
- package/esm/util/types/AsChild.d.ts +0 -1
- package/esm/util/types/AsChildProps.d.ts +0 -1
- package/esm/util/virtualfocus/Context.d.ts +43 -0
- package/esm/util/virtualfocus/Context.js +5 -0
- package/esm/util/virtualfocus/Context.js.map +1 -0
- package/esm/util/virtualfocus/SlottedDivElement.d.ts +7 -0
- package/esm/util/virtualfocus/SlottedDivElement.js +20 -0
- package/esm/util/virtualfocus/SlottedDivElement.js.map +1 -0
- package/esm/util/virtualfocus/VirtualFocus.d.ts +62 -0
- package/esm/util/virtualfocus/VirtualFocus.js +63 -0
- package/esm/util/virtualfocus/VirtualFocus.js.map +1 -0
- package/esm/util/virtualfocus/parts/VirtualFocusAnchor.d.ts +33 -0
- package/esm/util/virtualfocus/parts/VirtualFocusAnchor.js +54 -0
- package/esm/util/virtualfocus/parts/VirtualFocusAnchor.js.map +1 -0
- package/esm/util/virtualfocus/parts/VirtualFocusContent.d.ts +4 -0
- package/esm/util/virtualfocus/parts/VirtualFocusContent.js +19 -0
- package/esm/util/virtualfocus/parts/VirtualFocusContent.js.map +1 -0
- package/esm/util/virtualfocus/parts/VirtualFocusItem.d.ts +18 -0
- package/esm/util/virtualfocus/parts/VirtualFocusItem.js +38 -0
- package/esm/util/virtualfocus/parts/VirtualFocusItem.js.map +1 -0
- package/package.json +3 -3
- package/src/form/combobox/Combobox.tsx +4 -1
- package/src/form/combobox/ComboboxProvider.tsx +3 -0
- package/src/form/combobox/FilteredOptions/AddNewOption.tsx +63 -0
- package/src/form/combobox/FilteredOptions/FilteredOptions.tsx +11 -121
- package/src/form/combobox/FilteredOptions/FilteredOptionsItem.tsx +73 -0
- package/src/form/combobox/FilteredOptions/LoadingMessage.tsx +20 -0
- package/src/form/combobox/FilteredOptions/MaxSelectedMessage.tsx +27 -0
- package/src/form/combobox/FilteredOptions/NoSearchHitsMessage.tsx +19 -0
- package/src/form/combobox/FilteredOptions/filtered-options-util.ts +9 -1
- package/src/form/combobox/FilteredOptions/filteredOptionsContext.tsx +8 -5
- package/src/form/combobox/Input/Input.context.tsx +27 -25
- package/src/form/combobox/Input/Input.tsx +60 -29
- package/src/form/combobox/Input/InputController.tsx +2 -0
- package/src/form/combobox/__tests__/combobox.test.tsx +174 -66
- package/src/form/combobox/types.ts +11 -7
- package/src/layout/grid/HGrid.tsx +4 -1
- package/src/layout/stack/Stack.tsx +6 -2
- package/src/overlays/floating-menu/Menu.tsx +1177 -0
- package/src/overlays/floating-menu/parts/FocusScope.tsx +84 -0
- package/src/overlays/floating-menu/parts/RovingFocus.tsx +121 -0
- package/src/overlays/floating-menu/parts/SlottedDivElement.tsx +17 -0
- package/src/tabs/parts/tablist/useTabList.ts +4 -1
- package/src/toggle-group/parts/useToggleItem.ts +4 -1
- package/src/util/composeEventHandlers.ts +1 -1
- package/src/util/virtualfocus/Context.tsx +27 -0
- package/src/util/virtualfocus/SlottedDivElement.tsx +17 -0
- package/src/util/virtualfocus/VirtualFocus.tsx +89 -0
- package/src/util/virtualfocus/parts/VirtualFocusAnchor.tsx +102 -0
- package/src/util/virtualfocus/parts/VirtualFocusContent.tsx +17 -0
- package/src/util/virtualfocus/parts/VirtualFocusItem.tsx +60 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { forwardRef, useEffect, useState } from "react";
|
|
3
|
+
import { Slot } from "../../../slot/Slot";
|
|
4
|
+
import { useCallbackRef, useMergeRefs } from "../../../util/hooks";
|
|
5
|
+
|
|
6
|
+
const AUTOFOCUS_ON_MOUNT = "focusScope.autoFocusOnMount";
|
|
7
|
+
const AUTOFOCUS_ON_UNMOUNT = "focusScope.autoFocusOnUnmount";
|
|
8
|
+
const EVENT_OPTIONS = { bubbles: false, cancelable: true };
|
|
9
|
+
|
|
10
|
+
interface FocusScopeProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
11
|
+
/**
|
|
12
|
+
* Event handler called on mount, unless the component already has focus. Used for auto-focusing.
|
|
13
|
+
* Can be prevented.
|
|
14
|
+
*/
|
|
15
|
+
onMountHandler?: (event: Event) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Event handler called on unmount. Used for auto-focusing.
|
|
18
|
+
* Can be prevented.
|
|
19
|
+
*/
|
|
20
|
+
onUnmountHandler?: (event: Event) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* FocusScope manages focus on mount and unmount of container.
|
|
25
|
+
* This is used to better handle autofocus of elements when mounted and unmounted.
|
|
26
|
+
* Example usage:
|
|
27
|
+
* - Focus first item in a list when mounted
|
|
28
|
+
* - Focus a button when unmounted
|
|
29
|
+
*/
|
|
30
|
+
const FocusScope = forwardRef<HTMLDivElement, FocusScopeProps>(
|
|
31
|
+
(
|
|
32
|
+
{
|
|
33
|
+
onMountHandler: onMountHandlerCallback,
|
|
34
|
+
onUnmountHandler: onUnmountHandlerCallback,
|
|
35
|
+
...rest
|
|
36
|
+
},
|
|
37
|
+
ref,
|
|
38
|
+
) => {
|
|
39
|
+
const [container, setContainer] = useState<HTMLElement | null>(null);
|
|
40
|
+
const onMountHandler = useCallbackRef(onMountHandlerCallback);
|
|
41
|
+
const onUnmountHandler = useCallbackRef(onUnmountHandlerCallback);
|
|
42
|
+
|
|
43
|
+
const composedRefs = useMergeRefs(ref, setContainer);
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
if (!container) return;
|
|
47
|
+
|
|
48
|
+
const ownerDocument = container.ownerDocument ?? globalThis?.document;
|
|
49
|
+
const hasFocus = container.contains(ownerDocument.activeElement);
|
|
50
|
+
|
|
51
|
+
if (!hasFocus) {
|
|
52
|
+
const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
|
|
53
|
+
container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountHandler);
|
|
54
|
+
container.dispatchEvent(mountEvent);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return () => {
|
|
58
|
+
container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountHandler);
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* https://github.com/facebook/react/issues/17894
|
|
62
|
+
* As usual when dealing with focus and useEffect,
|
|
63
|
+
* we need to defer the focus to the next event-loop
|
|
64
|
+
* setTimeout makes sure the code is ran after the next render-cycle
|
|
65
|
+
*/
|
|
66
|
+
setTimeout(() => {
|
|
67
|
+
const unmountEvent = new CustomEvent(
|
|
68
|
+
AUTOFOCUS_ON_UNMOUNT,
|
|
69
|
+
EVENT_OPTIONS,
|
|
70
|
+
);
|
|
71
|
+
container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountHandler);
|
|
72
|
+
container.dispatchEvent(unmountEvent);
|
|
73
|
+
|
|
74
|
+
// we need to remove the listener after we `dispatchEvent`
|
|
75
|
+
container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountHandler);
|
|
76
|
+
}, 0);
|
|
77
|
+
};
|
|
78
|
+
}, [container, onMountHandler, onUnmountHandler]);
|
|
79
|
+
|
|
80
|
+
return <Slot tabIndex={-1} {...rest} ref={composedRefs} />;
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
export { FocusScope, type FocusScopeProps };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import React, { forwardRef, useCallback, useEffect, useRef } from "react";
|
|
2
|
+
import { Slot } from "../../../slot/Slot";
|
|
3
|
+
import { composeEventHandlers } from "../../../util/composeEventHandlers";
|
|
4
|
+
import { useCallbackRef, useMergeRefs } from "../../../util/hooks";
|
|
5
|
+
import { DescendantsManager } from "../../../util/hooks/descendants/descendant";
|
|
6
|
+
|
|
7
|
+
interface RovingFocusProps
|
|
8
|
+
extends Omit<React.HTMLAttributes<HTMLDivElement>, "tabIndex"> {
|
|
9
|
+
asChild?: boolean;
|
|
10
|
+
descendants: DescendantsManager<HTMLDivElement, object>;
|
|
11
|
+
onEntryFocus?: (event: Event) => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const ENTRY_FOCUS = "rovingFocusGroup.onEntryFocus";
|
|
15
|
+
const EVENT_OPTIONS = { bubbles: false, cancelable: true };
|
|
16
|
+
|
|
17
|
+
const RovingFocus = forwardRef<HTMLDivElement, RovingFocusProps>(
|
|
18
|
+
(
|
|
19
|
+
{
|
|
20
|
+
children,
|
|
21
|
+
asChild,
|
|
22
|
+
descendants,
|
|
23
|
+
onKeyDown,
|
|
24
|
+
onEntryFocus,
|
|
25
|
+
onMouseDown,
|
|
26
|
+
onFocus,
|
|
27
|
+
...rest
|
|
28
|
+
}: RovingFocusProps,
|
|
29
|
+
ref,
|
|
30
|
+
) => {
|
|
31
|
+
const _ref = React.useRef<HTMLDivElement>(null);
|
|
32
|
+
const composedRefs = useMergeRefs(ref, _ref);
|
|
33
|
+
|
|
34
|
+
const handleEntryFocus = useCallbackRef(onEntryFocus);
|
|
35
|
+
const isMouseFocusRef = useRef(false);
|
|
36
|
+
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
const node = _ref.current;
|
|
39
|
+
if (node) {
|
|
40
|
+
node.addEventListener(ENTRY_FOCUS, handleEntryFocus);
|
|
41
|
+
return () => node.removeEventListener(ENTRY_FOCUS, handleEntryFocus);
|
|
42
|
+
}
|
|
43
|
+
}, [handleEntryFocus]);
|
|
44
|
+
|
|
45
|
+
const handleKeyDown = useCallback(
|
|
46
|
+
(event: React.KeyboardEvent) => {
|
|
47
|
+
const loop = false;
|
|
48
|
+
|
|
49
|
+
const ownerDocument =
|
|
50
|
+
_ref?.current?.ownerDocument ?? globalThis?.document;
|
|
51
|
+
|
|
52
|
+
const idx = descendants
|
|
53
|
+
.values()
|
|
54
|
+
.findIndex((x) => x.node.isSameNode(ownerDocument.activeElement));
|
|
55
|
+
|
|
56
|
+
const nextItem = () => {
|
|
57
|
+
const next = descendants.nextEnabled(idx, loop);
|
|
58
|
+
next && next.node?.focus();
|
|
59
|
+
};
|
|
60
|
+
const prevItem = () => {
|
|
61
|
+
const prev = descendants.prevEnabled(idx, loop);
|
|
62
|
+
prev && prev.node?.focus();
|
|
63
|
+
};
|
|
64
|
+
const firstItem = () => {
|
|
65
|
+
const first = descendants.firstEnabled();
|
|
66
|
+
first && first.node?.focus();
|
|
67
|
+
};
|
|
68
|
+
const lastItem = () => {
|
|
69
|
+
const last = descendants.lastEnabled();
|
|
70
|
+
last && last.node?.focus();
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const keyMap: Record<string, React.KeyboardEventHandler> = {
|
|
74
|
+
ArrowUp: prevItem,
|
|
75
|
+
ArrowDown: nextItem,
|
|
76
|
+
Home: firstItem,
|
|
77
|
+
End: lastItem,
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const action = keyMap[event.key];
|
|
81
|
+
|
|
82
|
+
if (action) {
|
|
83
|
+
event.preventDefault();
|
|
84
|
+
action(event);
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
[descendants],
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const Comp = asChild ? Slot : "div";
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<Comp
|
|
94
|
+
ref={composedRefs}
|
|
95
|
+
{...rest}
|
|
96
|
+
tabIndex={descendants.enabledCount() === 0 ? -1 : 0}
|
|
97
|
+
style={{ outline: "none", ...rest.style }}
|
|
98
|
+
onKeyDown={composeEventHandlers(onKeyDown, handleKeyDown)}
|
|
99
|
+
onMouseDown={composeEventHandlers(onMouseDown, () => {
|
|
100
|
+
isMouseFocusRef.current = true;
|
|
101
|
+
})}
|
|
102
|
+
onFocus={composeEventHandlers(onFocus, (event) => {
|
|
103
|
+
if (event.target === event.currentTarget) {
|
|
104
|
+
const entryFocusEvent = new CustomEvent(ENTRY_FOCUS, EVENT_OPTIONS);
|
|
105
|
+
event.currentTarget.dispatchEvent(entryFocusEvent);
|
|
106
|
+
|
|
107
|
+
if (!entryFocusEvent.defaultPrevented) {
|
|
108
|
+
descendants.firstEnabled()?.node.focus({ preventScroll: true });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
isMouseFocusRef.current = false;
|
|
113
|
+
})}
|
|
114
|
+
>
|
|
115
|
+
{children}
|
|
116
|
+
</Comp>
|
|
117
|
+
);
|
|
118
|
+
},
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
export { RovingFocus, type RovingFocusProps };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React, { forwardRef } from "react";
|
|
2
|
+
import { Slot } from "../../../slot/Slot";
|
|
3
|
+
|
|
4
|
+
interface SlottedDivProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
5
|
+
asChild?: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const SlottedDivElement = forwardRef<HTMLDivElement, SlottedDivProps>(
|
|
9
|
+
({ asChild, ...rest }, forwardedRef) => {
|
|
10
|
+
const Comp = asChild ? Slot : "div";
|
|
11
|
+
return <Comp {...rest} ref={forwardedRef} />;
|
|
12
|
+
},
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
type SlottedDivElementRef = React.ElementRef<typeof SlottedDivElement>;
|
|
16
|
+
|
|
17
|
+
export { SlottedDivElement, type SlottedDivElementRef, type SlottedDivProps };
|
|
@@ -48,9 +48,12 @@ export function useTabList() {
|
|
|
48
48
|
End: lastTab,
|
|
49
49
|
};
|
|
50
50
|
|
|
51
|
+
const hasModifiers =
|
|
52
|
+
event.shiftKey || event.ctrlKey || event.altKey || event.metaKey;
|
|
53
|
+
|
|
51
54
|
const action = keyMap[event.key];
|
|
52
55
|
|
|
53
|
-
if (action) {
|
|
56
|
+
if (action && !hasModifiers) {
|
|
54
57
|
event.preventDefault();
|
|
55
58
|
action(event);
|
|
56
59
|
} else if (event.key === "Tab") {
|
|
@@ -77,9 +77,12 @@ export function useToggleItem<P extends UseToggleItemProps>(
|
|
|
77
77
|
End: lastTab,
|
|
78
78
|
};
|
|
79
79
|
|
|
80
|
+
const hasModifiers =
|
|
81
|
+
event.shiftKey || event.ctrlKey || event.altKey || event.metaKey;
|
|
82
|
+
|
|
80
83
|
const action = keyMap[event.key];
|
|
81
84
|
|
|
82
|
-
if (action) {
|
|
85
|
+
if (action && !hasModifiers) {
|
|
83
86
|
event.preventDefault();
|
|
84
87
|
action(event);
|
|
85
88
|
} else if (event.key === "Tab") {
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Utility to consistently call original eventhandler, often from props and internal eventhandler
|
|
5
5
|
* @internal
|
|
6
6
|
*/
|
|
7
|
-
export function composeEventHandlers<T extends React.SyntheticEvent>(
|
|
7
|
+
export function composeEventHandlers<T extends React.SyntheticEvent | Event>(
|
|
8
8
|
originalEventHandler?: (event: T) => void,
|
|
9
9
|
ourEventHandler?: (event: T) => void,
|
|
10
10
|
{ checkForDefaultPrevented = true } = {},
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Dispatch, SetStateAction } from "react";
|
|
2
|
+
import { createContext } from "../create-context";
|
|
3
|
+
import { createDescendantContext } from "../hooks/descendants/useDescendant";
|
|
4
|
+
import { SlottedDivElementRef } from "./SlottedDivElement";
|
|
5
|
+
|
|
6
|
+
export const [
|
|
7
|
+
VirtualFocusDescendantsProvider,
|
|
8
|
+
useVirtualFocusDescendantsContext,
|
|
9
|
+
useVirtualFocusDescendantInitializer,
|
|
10
|
+
useVirtualFocusDescendant,
|
|
11
|
+
] = createDescendantContext<
|
|
12
|
+
SlottedDivElementRef,
|
|
13
|
+
{
|
|
14
|
+
handleOnSelect: () => void;
|
|
15
|
+
handleOnActive: () => void;
|
|
16
|
+
}
|
|
17
|
+
>();
|
|
18
|
+
|
|
19
|
+
export const [
|
|
20
|
+
VirtualFocusInternalContextProvider,
|
|
21
|
+
useVirtualFocusInternalContext,
|
|
22
|
+
] = createContext<{
|
|
23
|
+
virtualFocusIdx: number;
|
|
24
|
+
setVirtualFocusIdx: Dispatch<SetStateAction<number>>;
|
|
25
|
+
loop: boolean;
|
|
26
|
+
uniqueId: string;
|
|
27
|
+
}>();
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React, { forwardRef } from "react";
|
|
2
|
+
import { Slot } from "../../slot/Slot";
|
|
3
|
+
|
|
4
|
+
interface SlottedDivProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
5
|
+
asChild?: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const SlottedDivElement = forwardRef<HTMLDivElement, SlottedDivProps>(
|
|
9
|
+
({ asChild, ...rest }, forwardedRef) => {
|
|
10
|
+
const Comp = asChild ? Slot : "div";
|
|
11
|
+
return <Comp {...rest} ref={forwardedRef} />;
|
|
12
|
+
},
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
type SlottedDivElementRef = React.ElementRef<typeof SlottedDivElement>;
|
|
16
|
+
|
|
17
|
+
export { SlottedDivElement, type SlottedDivElementRef, type SlottedDivProps };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
import { useId } from "../../util/hooks";
|
|
3
|
+
import {
|
|
4
|
+
VirtualFocusDescendantsProvider,
|
|
5
|
+
VirtualFocusInternalContextProvider,
|
|
6
|
+
useVirtualFocusDescendantInitializer,
|
|
7
|
+
} from "./Context";
|
|
8
|
+
import { VirtualFocusAnchor } from "./parts/VirtualFocusAnchor";
|
|
9
|
+
import { VirtualFocusContent } from "./parts/VirtualFocusContent";
|
|
10
|
+
import { VirtualFocusItem } from "./parts/VirtualFocusItem";
|
|
11
|
+
|
|
12
|
+
type VirtualFocusProps = {
|
|
13
|
+
children: React.ReactNode;
|
|
14
|
+
/**
|
|
15
|
+
* Whether to cause focus to loop around when it hits the first or last element
|
|
16
|
+
* @default false
|
|
17
|
+
**/
|
|
18
|
+
loop?: boolean;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A component that manages a virtual focus using the 'up' and 'down'
|
|
23
|
+
* arrow keys as well as selection with 'Return'.
|
|
24
|
+
*
|
|
25
|
+
* @see [📝 Documentation](https://aksel.nav.no/komponenter/core/virtualfocus)
|
|
26
|
+
* @see 🏷️ {@link AccordionProps}
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```jsx
|
|
30
|
+
* <VirtualFocus>
|
|
31
|
+
* <VirtualFocus.Anchor
|
|
32
|
+
* role="combobox"
|
|
33
|
+
* onSelect={() => {
|
|
34
|
+
* console.log("you selected the anchor");
|
|
35
|
+
* }}
|
|
36
|
+
* onActive={() => {
|
|
37
|
+
* console.log("the anchor is now virtually focused");
|
|
38
|
+
* }}
|
|
39
|
+
* >
|
|
40
|
+
* <input type="text" />
|
|
41
|
+
* </VirtualFocus.Anchor>
|
|
42
|
+
* <VirtualFocus.Content>
|
|
43
|
+
* <VirtualFocus.Item
|
|
44
|
+
* onSelect={() => {
|
|
45
|
+
* console.log("you selected the item");
|
|
46
|
+
* }}
|
|
47
|
+
* onActive={() => {
|
|
48
|
+
* console.log("the item is now virtually focused");
|
|
49
|
+
* }}
|
|
50
|
+
* >
|
|
51
|
+
* <p>item 1</p>
|
|
52
|
+
* </VirtualFocus.Item>
|
|
53
|
+
* <VirtualFocus.Item
|
|
54
|
+
* onSelect={() => {
|
|
55
|
+
* console.log("you selected the item");
|
|
56
|
+
* }}
|
|
57
|
+
* onActive={() => {
|
|
58
|
+
* console.log("the item is now virtually focused");
|
|
59
|
+
* }}
|
|
60
|
+
* >
|
|
61
|
+
* <p>item 2</p>
|
|
62
|
+
* </VirtualFocus.Item>
|
|
63
|
+
* </VirtualFocus.Content>
|
|
64
|
+
* </VirtualFocus>
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export const VirtualFocus = ({ children, loop = false }: VirtualFocusProps) => {
|
|
68
|
+
const descendants = useVirtualFocusDescendantInitializer();
|
|
69
|
+
const [virtualFocusIdx, setVirtualFocusIdx] = useState(0);
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<VirtualFocusInternalContextProvider
|
|
73
|
+
virtualFocusIdx={virtualFocusIdx}
|
|
74
|
+
setVirtualFocusIdx={setVirtualFocusIdx}
|
|
75
|
+
loop={loop}
|
|
76
|
+
uniqueId={useId()}
|
|
77
|
+
>
|
|
78
|
+
<VirtualFocusDescendantsProvider value={descendants}>
|
|
79
|
+
{children}
|
|
80
|
+
</VirtualFocusDescendantsProvider>
|
|
81
|
+
</VirtualFocusInternalContextProvider>
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
VirtualFocus.Anchor = VirtualFocusAnchor;
|
|
86
|
+
VirtualFocus.Item = VirtualFocusItem;
|
|
87
|
+
VirtualFocus.Content = VirtualFocusContent;
|
|
88
|
+
|
|
89
|
+
export default VirtualFocus;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import React, { forwardRef } from "react";
|
|
2
|
+
import { Slot } from "../../../slot/Slot";
|
|
3
|
+
import { composeEventHandlers } from "../../composeEventHandlers";
|
|
4
|
+
import { useMergeRefs } from "../../hooks";
|
|
5
|
+
import {
|
|
6
|
+
useVirtualFocusDescendant,
|
|
7
|
+
useVirtualFocusInternalContext,
|
|
8
|
+
} from "../Context";
|
|
9
|
+
|
|
10
|
+
export interface VirtualFocusAnchorProps
|
|
11
|
+
extends Omit<React.HTMLAttributes<HTMLDivElement>, "id"> {
|
|
12
|
+
/**
|
|
13
|
+
* The role of the container. This is a limited subset of roles that
|
|
14
|
+
* require manual focus management.
|
|
15
|
+
*
|
|
16
|
+
* Children that are to get focus inside this container element shall be
|
|
17
|
+
* pointed to by `aria-activedescendant`.
|
|
18
|
+
**/
|
|
19
|
+
role:
|
|
20
|
+
| "combobox"
|
|
21
|
+
| "grid"
|
|
22
|
+
| "listbox"
|
|
23
|
+
| "menu"
|
|
24
|
+
| "menubar"
|
|
25
|
+
| "radiogroup"
|
|
26
|
+
| "tree"
|
|
27
|
+
| "treegrid"
|
|
28
|
+
| "tablist";
|
|
29
|
+
/**
|
|
30
|
+
* The function that is run when the focused element
|
|
31
|
+
* is to be selected (eg. do an actual search, change route... etc)
|
|
32
|
+
*/
|
|
33
|
+
onSelect: () => void;
|
|
34
|
+
/**
|
|
35
|
+
* The function that is run when the element gets
|
|
36
|
+
* virtual focus set to it.
|
|
37
|
+
*/
|
|
38
|
+
onActive: () => void;
|
|
39
|
+
children: React.ReactElement;
|
|
40
|
+
/**
|
|
41
|
+
* Set this to `0` if you want the Anchor itself
|
|
42
|
+
* to be focusable. Since this Anchor is hoisted & merged with
|
|
43
|
+
* its first child, you most likely want to keep this as `0`.
|
|
44
|
+
* @default 0
|
|
45
|
+
*/
|
|
46
|
+
tabIndex?: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Must have a single child that is an input element.
|
|
51
|
+
*/
|
|
52
|
+
export const VirtualFocusAnchor = forwardRef<
|
|
53
|
+
HTMLInputElement,
|
|
54
|
+
VirtualFocusAnchorProps
|
|
55
|
+
>(({ onSelect, onActive, children, ...rest }, ref) => {
|
|
56
|
+
const { virtualFocusIdx, setVirtualFocusIdx, loop, uniqueId } =
|
|
57
|
+
useVirtualFocusInternalContext();
|
|
58
|
+
|
|
59
|
+
const { register, descendants, index } = useVirtualFocusDescendant({
|
|
60
|
+
handleOnActive: () => {
|
|
61
|
+
setVirtualFocusIdx(0);
|
|
62
|
+
onActive();
|
|
63
|
+
},
|
|
64
|
+
handleOnSelect: onSelect,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const mergedRefs = useMergeRefs(ref, register);
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<Slot
|
|
71
|
+
ref={mergedRefs}
|
|
72
|
+
tabIndex={0}
|
|
73
|
+
{...rest}
|
|
74
|
+
id={`virtualfocus-${uniqueId}-${index}`}
|
|
75
|
+
aria-owns={`virtualfocus-${uniqueId}-content`}
|
|
76
|
+
aria-controls={`virtualfocus-${uniqueId}-content`}
|
|
77
|
+
aria-activedescendant={`virtualfocus-${uniqueId}-${virtualFocusIdx}`}
|
|
78
|
+
onKeyDown={composeEventHandlers(rest.onKeyDown, (event) => {
|
|
79
|
+
if (event.key === "ArrowDown") {
|
|
80
|
+
event.preventDefault();
|
|
81
|
+
const to_focus_descendant = descendants.next(virtualFocusIdx, loop);
|
|
82
|
+
if (to_focus_descendant) {
|
|
83
|
+
to_focus_descendant.handleOnActive();
|
|
84
|
+
}
|
|
85
|
+
} else if (event.key === "ArrowUp") {
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
const to_focus_descendant = descendants.prev(virtualFocusIdx, loop);
|
|
88
|
+
if (to_focus_descendant) {
|
|
89
|
+
to_focus_descendant.handleOnActive();
|
|
90
|
+
}
|
|
91
|
+
} else if (event.key === "Enter") {
|
|
92
|
+
const curr = descendants.item(virtualFocusIdx);
|
|
93
|
+
if (curr?.handleOnSelect) {
|
|
94
|
+
curr.handleOnSelect();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
})}
|
|
98
|
+
>
|
|
99
|
+
{children}
|
|
100
|
+
</Slot>
|
|
101
|
+
);
|
|
102
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React, { forwardRef } from "react";
|
|
2
|
+
import { useVirtualFocusInternalContext } from "../Context";
|
|
3
|
+
|
|
4
|
+
export interface VirtualFocusContentProps
|
|
5
|
+
extends Omit<React.HTMLAttributes<HTMLDivElement>, "id"> {}
|
|
6
|
+
|
|
7
|
+
export const VirtualFocusContent = forwardRef<
|
|
8
|
+
HTMLDivElement,
|
|
9
|
+
VirtualFocusContentProps
|
|
10
|
+
>(({ children, ...rest }, ref) => {
|
|
11
|
+
const { uniqueId } = useVirtualFocusInternalContext();
|
|
12
|
+
return (
|
|
13
|
+
<div ref={ref} {...rest} id={`virtualfocus-${uniqueId}-content`}>
|
|
14
|
+
{children}
|
|
15
|
+
</div>
|
|
16
|
+
);
|
|
17
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import React, { forwardRef } from "react";
|
|
2
|
+
import { Slot } from "../../../slot/Slot";
|
|
3
|
+
import { composeEventHandlers } from "../../composeEventHandlers";
|
|
4
|
+
import { useMergeRefs } from "../../hooks";
|
|
5
|
+
import {
|
|
6
|
+
useVirtualFocusDescendant,
|
|
7
|
+
useVirtualFocusInternalContext,
|
|
8
|
+
} from "../Context";
|
|
9
|
+
|
|
10
|
+
export interface VirtualFocusItemProps
|
|
11
|
+
extends Omit<React.HTMLAttributes<HTMLDivElement>, "id" | "tabIndex"> {
|
|
12
|
+
/**
|
|
13
|
+
* The function that is run when the element is focused
|
|
14
|
+
* (virtually, not actual focus, eg. set a border around an item)
|
|
15
|
+
*/
|
|
16
|
+
onActive: () => void;
|
|
17
|
+
/**
|
|
18
|
+
* The function that is run when the focused element
|
|
19
|
+
* is to be selected (eg. do an actual search, change route... etc)
|
|
20
|
+
*/
|
|
21
|
+
onSelect: () => void;
|
|
22
|
+
children: React.ReactNode;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Contains an item you want to be iterable via virtual focus.
|
|
27
|
+
*/
|
|
28
|
+
export const VirtualFocusItem = forwardRef<HTMLElement, VirtualFocusItemProps>(
|
|
29
|
+
({ children, onActive, onSelect, ...rest }, ref) => {
|
|
30
|
+
const { virtualFocusIdx, setVirtualFocusIdx, uniqueId } =
|
|
31
|
+
useVirtualFocusInternalContext();
|
|
32
|
+
const { register, index } = useVirtualFocusDescendant({
|
|
33
|
+
handleOnActive: () => {
|
|
34
|
+
setVirtualFocusIdx(index);
|
|
35
|
+
onActive();
|
|
36
|
+
},
|
|
37
|
+
handleOnSelect: onSelect,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const mergedRefs = useMergeRefs(ref, register);
|
|
41
|
+
return (
|
|
42
|
+
<Slot
|
|
43
|
+
ref={mergedRefs}
|
|
44
|
+
{...rest}
|
|
45
|
+
id={`virtualfocus-${uniqueId}-${index}`}
|
|
46
|
+
data-aksel-virtualfocus={virtualFocusIdx === index}
|
|
47
|
+
tabIndex={-1}
|
|
48
|
+
onClick={composeEventHandlers(rest.onClick, () => {
|
|
49
|
+
onSelect();
|
|
50
|
+
})}
|
|
51
|
+
onMouseMove={composeEventHandlers(rest.onMouseMove, () => {
|
|
52
|
+
setVirtualFocusIdx(index);
|
|
53
|
+
onActive();
|
|
54
|
+
})}
|
|
55
|
+
>
|
|
56
|
+
{children}
|
|
57
|
+
</Slot>
|
|
58
|
+
);
|
|
59
|
+
},
|
|
60
|
+
);
|