@navikt/ds-react 6.7.0 → 6.8.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/chat/Chat.d.ts +2 -1
- package/cjs/chat/Chat.js +2 -1
- package/cjs/chat/Chat.js.map +1 -1
- package/cjs/collapsible/parts/Collapsible.Trigger.d.ts +4 -0
- package/cjs/collapsible/parts/Collapsible.Trigger.js +1 -1
- package/cjs/collapsible/parts/Collapsible.Trigger.js.map +1 -1
- package/cjs/date/datepicker/parts/DropdownCaption.js +1 -1
- package/cjs/date/datepicker/parts/DropdownCaption.js.map +1 -1
- package/cjs/date/monthpicker/MonthCaption.js +1 -1
- package/cjs/date/utils/labels.d.ts +2 -2
- package/cjs/form/ReadOnlyIcon.d.ts +2 -2
- package/cjs/form/combobox/Combobox.js +7 -22
- package/cjs/form/combobox/Combobox.js.map +1 -1
- package/cjs/form/combobox/ComboboxProvider.js +2 -2
- package/cjs/form/combobox/ComboboxProvider.js.map +1 -1
- package/cjs/form/combobox/ComboboxWrapper.d.ts +1 -2
- package/cjs/form/combobox/ComboboxWrapper.js +4 -2
- package/cjs/form/combobox/ComboboxWrapper.js.map +1 -1
- package/cjs/form/combobox/FilteredOptions/FilteredOptions.js +4 -4
- package/cjs/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
- package/cjs/form/combobox/FilteredOptions/filteredOptionsContext.d.ts +4 -4
- package/cjs/form/combobox/FilteredOptions/filteredOptionsContext.js +13 -15
- package/cjs/form/combobox/FilteredOptions/filteredOptionsContext.js.map +1 -1
- package/cjs/form/combobox/Input/{inputContext.d.ts → Input.context.d.ts} +7 -5
- package/cjs/form/combobox/Input/{inputContext.js → Input.context.js} +22 -22
- package/cjs/form/combobox/Input/Input.context.js.map +1 -0
- package/cjs/form/combobox/Input/Input.js +2 -2
- package/cjs/form/combobox/Input/Input.js.map +1 -1
- package/cjs/form/combobox/Input/InputController.d.ts +3 -0
- package/cjs/form/combobox/Input/InputController.js +70 -0
- package/cjs/form/combobox/Input/InputController.js.map +1 -0
- package/cjs/form/combobox/{ToggleListButton.js → Input/ToggleListButton.js} +1 -1
- package/cjs/form/combobox/Input/ToggleListButton.js.map +1 -0
- package/cjs/form/combobox/SelectedOptions/SelectedOptions.js +2 -2
- package/cjs/form/combobox/SelectedOptions/SelectedOptions.js.map +1 -1
- package/cjs/form/combobox/SelectedOptions/selectedOptionsContext.d.ts +4 -4
- package/cjs/form/combobox/SelectedOptions/selectedOptionsContext.js +7 -13
- package/cjs/form/combobox/SelectedOptions/selectedOptionsContext.js.map +1 -1
- package/cjs/form/combobox/customOptionsContext.d.ts +4 -4
- package/cjs/form/combobox/customOptionsContext.js +10 -13
- package/cjs/form/combobox/customOptionsContext.js.map +1 -1
- package/cjs/form/combobox/types.d.ts +1 -1
- package/cjs/form/file-upload/FileUpload.context.d.ts +1 -1
- package/cjs/form/file-upload/FileUpload.d.ts +1 -1
- package/cjs/form/file-upload/FileUpload.js.map +1 -1
- package/cjs/form/file-upload/parts/dropzone/Dropzone.js +1 -1
- package/cjs/form/file-upload/parts/dropzone/Dropzone.js.map +1 -1
- package/cjs/form/file-upload/parts/dropzone/dropzone.types.d.ts +1 -1
- package/cjs/form/file-upload/parts/item/Item.d.ts +1 -1
- package/cjs/form/file-upload/parts/item/Item.js +1 -1
- package/cjs/form/file-upload/parts/item/Item.js.map +1 -1
- package/cjs/form/form-progress/FormProgress.d.ts +66 -0
- package/cjs/form/form-progress/FormProgress.js +85 -0
- package/cjs/form/form-progress/FormProgress.js.map +1 -0
- package/cjs/form/form-progress/index.d.ts +1 -0
- package/cjs/form/form-progress/index.js +11 -0
- package/cjs/form/form-progress/index.js.map +1 -0
- package/cjs/help-text/HelpTextIcon.d.ts +1 -1
- package/cjs/index.d.ts +1 -0
- package/cjs/index.js +3 -1
- package/cjs/index.js.map +1 -1
- package/cjs/overlay/dismiss/DismissableLayer.d.ts +70 -0
- package/cjs/overlay/dismiss/DismissableLayer.js +253 -0
- package/cjs/overlay/dismiss/DismissableLayer.js.map +1 -0
- package/cjs/overlay/dismiss/util/dispatchCustomEvent.d.ts +50 -0
- package/cjs/overlay/dismiss/util/dispatchCustomEvent.js +65 -0
- package/cjs/overlay/dismiss/util/dispatchCustomEvent.js.map +1 -0
- package/cjs/overlay/dismiss/util/useEscapeKeydown.d.ts +1 -0
- package/cjs/overlay/dismiss/util/useEscapeKeydown.js +19 -0
- package/cjs/overlay/dismiss/util/useEscapeKeydown.js.map +1 -0
- package/cjs/overlay/dismiss/util/useFocusOutside.d.ts +8 -0
- package/cjs/overlay/dismiss/util/useFocusOutside.js +42 -0
- package/cjs/overlay/dismiss/util/useFocusOutside.js.map +1 -0
- package/cjs/overlay/dismiss/util/usePointerDownOutside.d.ts +10 -0
- package/cjs/overlay/dismiss/util/usePointerDownOutside.js +84 -0
- package/cjs/overlay/dismiss/util/usePointerDownOutside.js.map +1 -0
- package/cjs/overlays/floating/Floating.d.ts +53 -0
- package/cjs/overlays/floating/Floating.js +215 -0
- package/cjs/overlays/floating/Floating.js.map +1 -0
- package/cjs/overlays/floating/Floating.utils.d.ts +18 -0
- package/cjs/overlays/floating/Floating.utils.js +52 -0
- package/cjs/overlays/floating/Floating.utils.js.map +1 -0
- package/cjs/popover/Popover.js +13 -28
- package/cjs/popover/Popover.js.map +1 -1
- package/cjs/progress-bar/ProgressBar.d.ts +20 -8
- package/cjs/progress-bar/ProgressBar.js +19 -9
- package/cjs/progress-bar/ProgressBar.js.map +1 -1
- package/cjs/stepper/Step.js +1 -1
- package/cjs/stepper/Step.js.map +1 -1
- package/cjs/stepper/Stepper.d.ts +1 -1
- package/cjs/stepper/Stepper.js +1 -1
- package/cjs/tabs/Tabs.context.d.ts +7 -3
- package/cjs/tabs/Tabs.context.js +1 -0
- package/cjs/tabs/Tabs.context.js.map +1 -1
- package/cjs/timeline/AxisLabels.d.ts +1 -1
- package/cjs/toggle-group/ToggleGroup.context.d.ts +7 -3
- package/cjs/toggle-group/ToggleGroup.context.js +1 -0
- package/cjs/toggle-group/ToggleGroup.context.js.map +1 -1
- package/cjs/util/hooks/descendants/useDescendant.d.ts +2 -2
- package/cjs/util/hooks/descendants/useDescendant.js +49 -52
- package/cjs/util/hooks/descendants/useDescendant.js.map +1 -1
- package/cjs/util/i18n/get.js.map +1 -0
- package/cjs/util/i18n/i18n.context.js.map +1 -0
- package/cjs/util/i18n/i18n.types.js.map +1 -0
- package/{esm/form/file-upload → cjs/util}/i18n/locales/nb.d.ts +5 -0
- package/cjs/{form/file-upload → util}/i18n/locales/nb.js +5 -0
- package/cjs/util/i18n/locales/nb.js.map +1 -0
- package/cjs/util/i18n/merge.js.map +1 -0
- package/cjs/util/types/AsChild.d.ts +14 -0
- package/cjs/util/types/AsChild.js +3 -0
- package/cjs/util/types/AsChild.js.map +1 -0
- package/esm/chat/Chat.d.ts +2 -1
- package/esm/chat/Chat.js +1 -0
- package/esm/chat/Chat.js.map +1 -1
- package/esm/collapsible/parts/Collapsible.Trigger.d.ts +4 -0
- package/esm/collapsible/parts/Collapsible.Trigger.js +1 -1
- package/esm/collapsible/parts/Collapsible.Trigger.js.map +1 -1
- package/esm/date/datepicker/parts/DropdownCaption.js +1 -1
- package/esm/date/datepicker/parts/DropdownCaption.js.map +1 -1
- package/esm/date/monthpicker/MonthCaption.js +1 -1
- package/esm/date/utils/labels.d.ts +2 -2
- package/esm/form/ReadOnlyIcon.d.ts +2 -2
- package/esm/form/combobox/Combobox.js +8 -23
- package/esm/form/combobox/Combobox.js.map +1 -1
- package/esm/form/combobox/ComboboxProvider.js +1 -1
- package/esm/form/combobox/ComboboxProvider.js.map +1 -1
- package/esm/form/combobox/ComboboxWrapper.d.ts +1 -2
- package/esm/form/combobox/ComboboxWrapper.js +4 -2
- package/esm/form/combobox/ComboboxWrapper.js.map +1 -1
- package/esm/form/combobox/FilteredOptions/FilteredOptions.js +3 -3
- package/esm/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
- package/esm/form/combobox/FilteredOptions/filteredOptionsContext.d.ts +4 -4
- package/esm/form/combobox/FilteredOptions/filteredOptionsContext.js +15 -16
- package/esm/form/combobox/FilteredOptions/filteredOptionsContext.js.map +1 -1
- package/esm/form/combobox/Input/{inputContext.d.ts → Input.context.d.ts} +7 -5
- package/esm/form/combobox/Input/{inputContext.js → Input.context.js} +22 -21
- package/esm/form/combobox/Input/Input.context.js.map +1 -0
- package/esm/form/combobox/Input/Input.js +1 -1
- package/esm/form/combobox/Input/Input.js.map +1 -1
- package/esm/form/combobox/Input/InputController.d.ts +3 -0
- package/esm/form/combobox/Input/InputController.js +41 -0
- package/esm/form/combobox/Input/InputController.js.map +1 -0
- package/esm/form/combobox/{ToggleListButton.js → Input/ToggleListButton.js} +1 -1
- package/esm/form/combobox/Input/ToggleListButton.js.map +1 -0
- package/esm/form/combobox/SelectedOptions/SelectedOptions.js +1 -1
- package/esm/form/combobox/SelectedOptions/SelectedOptions.js.map +1 -1
- package/esm/form/combobox/SelectedOptions/selectedOptionsContext.d.ts +4 -4
- package/esm/form/combobox/SelectedOptions/selectedOptionsContext.js +9 -14
- package/esm/form/combobox/SelectedOptions/selectedOptionsContext.js.map +1 -1
- package/esm/form/combobox/customOptionsContext.d.ts +4 -4
- package/esm/form/combobox/customOptionsContext.js +10 -12
- package/esm/form/combobox/customOptionsContext.js.map +1 -1
- package/esm/form/combobox/types.d.ts +1 -1
- package/esm/form/file-upload/FileUpload.context.d.ts +1 -1
- package/esm/form/file-upload/FileUpload.d.ts +1 -1
- package/esm/form/file-upload/FileUpload.js.map +1 -1
- package/esm/form/file-upload/parts/dropzone/Dropzone.js +1 -1
- package/esm/form/file-upload/parts/dropzone/Dropzone.js.map +1 -1
- package/esm/form/file-upload/parts/dropzone/dropzone.types.d.ts +1 -1
- package/esm/form/file-upload/parts/item/Item.d.ts +1 -1
- package/esm/form/file-upload/parts/item/Item.js +1 -1
- package/esm/form/file-upload/parts/item/Item.js.map +1 -1
- package/esm/form/form-progress/FormProgress.d.ts +66 -0
- package/esm/form/form-progress/FormProgress.js +56 -0
- package/esm/form/form-progress/FormProgress.js.map +1 -0
- package/esm/form/form-progress/index.d.ts +1 -0
- package/esm/form/form-progress/index.js +3 -0
- package/esm/form/form-progress/index.js.map +1 -0
- package/esm/help-text/HelpTextIcon.d.ts +1 -1
- package/esm/index.d.ts +1 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/overlay/dismiss/DismissableLayer.d.ts +70 -0
- package/esm/overlay/dismiss/DismissableLayer.js +226 -0
- package/esm/overlay/dismiss/DismissableLayer.js.map +1 -0
- package/esm/overlay/dismiss/util/dispatchCustomEvent.d.ts +50 -0
- package/esm/overlay/dismiss/util/dispatchCustomEvent.js +58 -0
- package/esm/overlay/dismiss/util/dispatchCustomEvent.js.map +1 -0
- package/esm/overlay/dismiss/util/useEscapeKeydown.d.ts +1 -0
- package/esm/overlay/dismiss/util/useEscapeKeydown.js +15 -0
- package/esm/overlay/dismiss/util/useEscapeKeydown.js.map +1 -0
- package/esm/overlay/dismiss/util/useFocusOutside.d.ts +8 -0
- package/esm/overlay/dismiss/util/useFocusOutside.js +38 -0
- package/esm/overlay/dismiss/util/useFocusOutside.js.map +1 -0
- package/esm/overlay/dismiss/util/usePointerDownOutside.d.ts +10 -0
- package/esm/overlay/dismiss/util/usePointerDownOutside.js +80 -0
- package/esm/overlay/dismiss/util/usePointerDownOutside.js.map +1 -0
- package/esm/overlays/floating/Floating.d.ts +53 -0
- package/esm/overlays/floating/Floating.js +188 -0
- package/esm/overlays/floating/Floating.js.map +1 -0
- package/esm/overlays/floating/Floating.utils.d.ts +18 -0
- package/esm/overlays/floating/Floating.utils.js +48 -0
- package/esm/overlays/floating/Floating.utils.js.map +1 -0
- package/esm/popover/Popover.js +16 -31
- package/esm/popover/Popover.js.map +1 -1
- package/esm/progress-bar/ProgressBar.d.ts +20 -8
- package/esm/progress-bar/ProgressBar.js +20 -10
- package/esm/progress-bar/ProgressBar.js.map +1 -1
- package/esm/stepper/Step.js +1 -1
- package/esm/stepper/Step.js.map +1 -1
- package/esm/stepper/Stepper.d.ts +1 -1
- package/esm/stepper/Stepper.js +1 -1
- package/esm/tabs/Tabs.context.d.ts +7 -3
- package/esm/tabs/Tabs.context.js +1 -0
- package/esm/tabs/Tabs.context.js.map +1 -1
- package/esm/timeline/AxisLabels.d.ts +1 -1
- package/esm/toggle-group/ToggleGroup.context.d.ts +7 -3
- package/esm/toggle-group/ToggleGroup.context.js +1 -0
- package/esm/toggle-group/ToggleGroup.context.js.map +1 -1
- package/esm/util/hooks/descendants/useDescendant.d.ts +2 -2
- package/esm/util/hooks/descendants/useDescendant.js +49 -52
- package/esm/util/hooks/descendants/useDescendant.js.map +1 -1
- package/esm/util/i18n/get.js.map +1 -0
- package/esm/util/i18n/i18n.context.js.map +1 -0
- package/esm/util/i18n/i18n.types.js.map +1 -0
- package/{cjs/form/file-upload → esm/util}/i18n/locales/nb.d.ts +5 -0
- package/esm/{form/file-upload → util}/i18n/locales/nb.js +5 -0
- package/esm/util/i18n/locales/nb.js.map +1 -0
- package/esm/util/i18n/merge.js.map +1 -0
- package/esm/util/types/AsChild.d.ts +14 -0
- package/esm/util/types/AsChild.js +2 -0
- package/esm/util/types/AsChild.js.map +1 -0
- package/package.json +16 -5
- package/src/chat/Chat.tsx +2 -1
- package/src/collapsible/parts/Collapsible.Trigger.tsx +5 -1
- package/src/date/datepicker/parts/DropdownCaption.tsx +5 -1
- package/src/date/monthpicker/MonthCaption.tsx +1 -1
- package/src/form/combobox/Combobox.tsx +6 -76
- package/src/form/combobox/ComboboxProvider.tsx +1 -1
- package/src/form/combobox/ComboboxWrapper.tsx +4 -3
- package/src/form/combobox/FilteredOptions/FilteredOptions.tsx +3 -3
- package/src/form/combobox/FilteredOptions/filteredOptionsContext.tsx +19 -29
- package/src/form/combobox/Input/{inputContext.tsx → Input.context.tsx} +30 -33
- package/src/form/combobox/Input/Input.tsx +1 -1
- package/src/form/combobox/Input/InputController.tsx +102 -0
- package/src/form/combobox/{ToggleListButton.tsx → Input/ToggleListButton.tsx} +1 -1
- package/src/form/combobox/SelectedOptions/SelectedOptions.tsx +1 -1
- package/src/form/combobox/SelectedOptions/selectedOptionsContext.tsx +12 -26
- package/src/form/combobox/{combobox-utils.test.ts → __tests__/combobox-utils.test.ts} +1 -1
- package/src/form/combobox/{combobox.test.tsx → __tests__/combobox.test.tsx} +2 -3
- package/src/form/combobox/customOptionsContext.tsx +14 -18
- package/src/form/combobox/types.ts +3 -1
- package/src/form/file-upload/FileUpload.context.tsx +1 -1
- package/src/form/file-upload/FileUpload.tsx +1 -1
- package/src/form/file-upload/parts/dropzone/Dropzone.tsx +1 -1
- package/src/form/file-upload/parts/dropzone/dropzone.types.ts +1 -1
- package/src/form/file-upload/parts/item/Item.tsx +2 -2
- package/src/form/form-progress/FormProgress.tsx +152 -0
- package/src/form/form-progress/index.ts +7 -0
- package/src/index.ts +1 -0
- package/src/overlay/README.md +5 -0
- package/src/overlay/dismiss/DismissableLayer.tsx +368 -0
- package/src/overlay/dismiss/util/dispatchCustomEvent.ts +77 -0
- package/src/overlay/dismiss/util/useEscapeKeydown.ts +21 -0
- package/src/overlay/dismiss/util/useFocusOutside.ts +52 -0
- package/src/overlay/dismiss/util/usePointerDownOutside.ts +95 -0
- package/src/overlays/floating/Floating.tsx +399 -0
- package/src/overlays/floating/Floating.utils.ts +63 -0
- package/src/popover/Popover.tsx +38 -70
- package/src/progress-bar/ProgressBar.tsx +45 -20
- package/src/stepper/Step.tsx +1 -1
- package/src/stepper/Stepper.tsx +1 -1
- package/src/tabs/Tabs.context.ts +2 -0
- package/src/toggle-group/ToggleGroup.context.ts +1 -0
- package/src/util/hooks/descendants/useDescendant.tsx +55 -68
- package/src/{form/file-upload → util}/i18n/locales/nb.ts +5 -0
- package/src/util/types/AsChild.ts +15 -0
- package/cjs/form/combobox/ClearButton.d.ts +0 -7
- package/cjs/form/combobox/ClearButton.js +0 -28
- package/cjs/form/combobox/ClearButton.js.map +0 -1
- package/cjs/form/combobox/FilteredOptions/CheckIcon.d.ts +0 -3
- package/cjs/form/combobox/FilteredOptions/CheckIcon.js +0 -12
- package/cjs/form/combobox/FilteredOptions/CheckIcon.js.map +0 -1
- package/cjs/form/combobox/Input/inputContext.js.map +0 -1
- package/cjs/form/combobox/ToggleListButton.js.map +0 -1
- package/cjs/form/file-upload/i18n/get.js.map +0 -1
- package/cjs/form/file-upload/i18n/i18n.context.js.map +0 -1
- package/cjs/form/file-upload/i18n/i18n.types.js.map +0 -1
- package/cjs/form/file-upload/i18n/locales/nb.js.map +0 -1
- package/cjs/form/file-upload/i18n/merge.js.map +0 -1
- package/esm/form/combobox/ClearButton.d.ts +0 -7
- package/esm/form/combobox/ClearButton.js +0 -21
- package/esm/form/combobox/ClearButton.js.map +0 -1
- package/esm/form/combobox/FilteredOptions/CheckIcon.d.ts +0 -3
- package/esm/form/combobox/FilteredOptions/CheckIcon.js +0 -7
- package/esm/form/combobox/FilteredOptions/CheckIcon.js.map +0 -1
- package/esm/form/combobox/Input/inputContext.js.map +0 -1
- package/esm/form/combobox/ToggleListButton.js.map +0 -1
- package/esm/form/file-upload/i18n/get.js.map +0 -1
- package/esm/form/file-upload/i18n/i18n.context.js.map +0 -1
- package/esm/form/file-upload/i18n/i18n.types.js.map +0 -1
- package/esm/form/file-upload/i18n/locales/nb.js.map +0 -1
- package/esm/form/file-upload/i18n/merge.js.map +0 -1
- package/src/form/combobox/ClearButton.tsx +0 -29
- package/src/form/combobox/FilteredOptions/CheckIcon.tsx +0 -23
- /package/cjs/form/combobox/{ToggleListButton.d.ts → Input/ToggleListButton.d.ts} +0 -0
- /package/cjs/{form/file-upload → util}/i18n/get.d.ts +0 -0
- /package/cjs/{form/file-upload → util}/i18n/get.js +0 -0
- /package/cjs/{form/file-upload → util}/i18n/i18n.context.d.ts +0 -0
- /package/cjs/{form/file-upload → util}/i18n/i18n.context.js +0 -0
- /package/cjs/{form/file-upload → util}/i18n/i18n.types.d.ts +0 -0
- /package/cjs/{form/file-upload → util}/i18n/i18n.types.js +0 -0
- /package/cjs/{form/file-upload → util}/i18n/merge.d.ts +0 -0
- /package/cjs/{form/file-upload → util}/i18n/merge.js +0 -0
- /package/esm/form/combobox/{ToggleListButton.d.ts → Input/ToggleListButton.d.ts} +0 -0
- /package/esm/{form/file-upload → util}/i18n/get.d.ts +0 -0
- /package/esm/{form/file-upload → util}/i18n/get.js +0 -0
- /package/esm/{form/file-upload → util}/i18n/i18n.context.d.ts +0 -0
- /package/esm/{form/file-upload → util}/i18n/i18n.context.js +0 -0
- /package/esm/{form/file-upload → util}/i18n/i18n.types.d.ts +0 -0
- /package/esm/{form/file-upload → util}/i18n/i18n.types.js +0 -0
- /package/esm/{form/file-upload → util}/i18n/merge.d.ts +0 -0
- /package/esm/{form/file-upload → util}/i18n/merge.js +0 -0
- /package/src/{form/file-upload → util}/i18n/get.ts +0 -0
- /package/src/{form/file-upload → util}/i18n/i18n.context.test.tsx +0 -0
- /package/src/{form/file-upload → util}/i18n/i18n.context.ts +0 -0
- /package/src/{form/file-upload → util}/i18n/i18n.types.ts +0 -0
- /package/src/{form/file-upload → util}/i18n/merge.ts +0 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
import { useCallbackRef } from "../../../util/hooks";
|
|
3
|
+
import {
|
|
4
|
+
CUSTOM_EVENTS,
|
|
5
|
+
CustomPointerDownEvent,
|
|
6
|
+
dispatchCustomEvent,
|
|
7
|
+
} from "./dispatchCustomEvent";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Listens for `pointerdown` outside a react subtree. We use `pointerdown` rather than `pointerup`
|
|
11
|
+
* to mimic layer dismissing behaviour present in OS.
|
|
12
|
+
* Returns props to pass to the node we want to check for outside events.
|
|
13
|
+
* By checking `isPointerInsideReactTreeRef` we can determine if the event happened outside the subtree of the node, saving some element-comparisons.
|
|
14
|
+
*/
|
|
15
|
+
export function usePointerDownOutside(
|
|
16
|
+
callback?: (event: CustomPointerDownEvent) => void,
|
|
17
|
+
ownerDocument: Document = globalThis?.document,
|
|
18
|
+
) {
|
|
19
|
+
const handlePointerDownOutside = useCallbackRef(callback) as EventListener;
|
|
20
|
+
const isPointerInsideReactTreeRef = useRef(false);
|
|
21
|
+
const handleClickRef = useRef<typeof handlePointerDownOutside>(() => {});
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
const handlePointerDown = (event: PointerEvent) => {
|
|
25
|
+
/**
|
|
26
|
+
* The `DismisableLayer`-API is based on the ability to stop events from propagating and in the end calling `onDismiss`
|
|
27
|
+
* if `usePointerDownOutside` runs `event.preventDefault()`.
|
|
28
|
+
*
|
|
29
|
+
* Altrough `pointerdown` is already a cancelable event,
|
|
30
|
+
* to to make sure the batching of events works corretly with `focusIn` in `useFocusOutside`,
|
|
31
|
+
* we still use a custom event like in `useFocusOutside`.
|
|
32
|
+
*
|
|
33
|
+
* Since pointer-events are `discrete` events in React: https://github.com/facebook/react/blob/a8a4742f1c54493df00da648a3f9d26e3db9c8b5/packages/react-dom/src/events/ReactDOMEventListener.js#L318
|
|
34
|
+
* we need to to use flushSync to ensure that the event is dispatched before the next event is raised.
|
|
35
|
+
*/
|
|
36
|
+
function dispatchPointerEvent() {
|
|
37
|
+
dispatchCustomEvent(
|
|
38
|
+
CUSTOM_EVENTS.POINTER_DOWN_OUTSIDE,
|
|
39
|
+
handlePointerDownOutside,
|
|
40
|
+
{ originalEvent: event },
|
|
41
|
+
{ discrete: true },
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (event.target && !isPointerInsideReactTreeRef.current) {
|
|
46
|
+
/**
|
|
47
|
+
* On touch devices, we delay reactivating pointer-events to account for the browser's delay in executing events after touch ends.
|
|
48
|
+
* This also handles cancellations when no click event is raised due to scrolling or long-pressing.
|
|
49
|
+
* We continuously remove the previous listener as we can't be sure it was raised and cleaned up.
|
|
50
|
+
*/
|
|
51
|
+
if (event.pointerType === "touch") {
|
|
52
|
+
ownerDocument.removeEventListener("click", handleClickRef.current);
|
|
53
|
+
handleClickRef.current = dispatchPointerEvent;
|
|
54
|
+
ownerDocument.addEventListener("click", handleClickRef.current, {
|
|
55
|
+
once: true,
|
|
56
|
+
});
|
|
57
|
+
} else {
|
|
58
|
+
dispatchPointerEvent();
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
// We need to remove the event listener in case the outside click has been canceled.
|
|
62
|
+
ownerDocument.removeEventListener("click", handleClickRef.current);
|
|
63
|
+
}
|
|
64
|
+
isPointerInsideReactTreeRef.current = false;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* If this hook executes in a component that mounts via a `pointerdown` event, the event
|
|
69
|
+
* would bubble up to the document and trigger a `pointerDownOutside` event. We avoid
|
|
70
|
+
* this by delaying the event listener registration on the document.
|
|
71
|
+
* This is not React specific, but rather how the DOM works, ie:
|
|
72
|
+
* ```
|
|
73
|
+
* button.addEventListener('pointerdown', () => {
|
|
74
|
+
* console.log('I will log');
|
|
75
|
+
* document.addEventListener('pointerdown', () => {
|
|
76
|
+
* console.log('I will also log');
|
|
77
|
+
* })
|
|
78
|
+
* });
|
|
79
|
+
*/
|
|
80
|
+
const timerId = window.setTimeout(() => {
|
|
81
|
+
ownerDocument.addEventListener("pointerdown", handlePointerDown);
|
|
82
|
+
}, 0);
|
|
83
|
+
|
|
84
|
+
return () => {
|
|
85
|
+
window.clearTimeout(timerId);
|
|
86
|
+
ownerDocument.removeEventListener("pointerdown", handlePointerDown);
|
|
87
|
+
ownerDocument.removeEventListener("click", handleClickRef.current);
|
|
88
|
+
};
|
|
89
|
+
}, [ownerDocument, handlePointerDownOutside]);
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
// ensures we check React component tree (not just DOM tree)
|
|
93
|
+
onPointerDownCapture: () => (isPointerInsideReactTreeRef.current = true),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Placement,
|
|
3
|
+
autoUpdate,
|
|
4
|
+
flip,
|
|
5
|
+
arrow as floatingArrow,
|
|
6
|
+
hide,
|
|
7
|
+
limitShift,
|
|
8
|
+
offset,
|
|
9
|
+
shift,
|
|
10
|
+
size,
|
|
11
|
+
useFloating,
|
|
12
|
+
} from "@floating-ui/react-dom";
|
|
13
|
+
import React, {
|
|
14
|
+
HTMLAttributes,
|
|
15
|
+
forwardRef,
|
|
16
|
+
useEffect,
|
|
17
|
+
useRef,
|
|
18
|
+
useState,
|
|
19
|
+
} from "react";
|
|
20
|
+
import { Slot } from "../../util/Slot";
|
|
21
|
+
import { createContext } from "../../util/create-context";
|
|
22
|
+
import {
|
|
23
|
+
useCallbackRef,
|
|
24
|
+
useClientLayoutEffect,
|
|
25
|
+
useMergeRefs,
|
|
26
|
+
} from "../../util/hooks";
|
|
27
|
+
import { AsChildProps } from "../../util/types";
|
|
28
|
+
import {
|
|
29
|
+
type Align,
|
|
30
|
+
type Measurable,
|
|
31
|
+
type Side,
|
|
32
|
+
getSideAndAlignFromPlacement,
|
|
33
|
+
transformOrigin,
|
|
34
|
+
} from "./Floating.utils";
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Floating Root
|
|
38
|
+
*/
|
|
39
|
+
type FloatingContextValue = {
|
|
40
|
+
anchor: Measurable | null;
|
|
41
|
+
onAnchorChange: (anchor: Measurable | null) => void;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const [FloatingProvider, useFloatingContext] =
|
|
45
|
+
createContext<FloatingContextValue>({
|
|
46
|
+
name: "FloatingContext",
|
|
47
|
+
hookName: "useFloating",
|
|
48
|
+
providerName: "FloatingProvider",
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
interface FloatingProps {
|
|
52
|
+
children: React.ReactNode;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface FloatingComponent extends React.FC<FloatingProps> {
|
|
56
|
+
Anchor: typeof FloatingAnchor;
|
|
57
|
+
Content: typeof FloatingContent;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const Floating: FloatingComponent = ({ children }: FloatingProps) => {
|
|
61
|
+
const [anchor, setAnchor] = useState<Measurable | null>(null);
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<FloatingProvider anchor={anchor} onAnchorChange={setAnchor}>
|
|
65
|
+
{children}
|
|
66
|
+
</FloatingProvider>
|
|
67
|
+
);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Floating Anchor
|
|
72
|
+
*/
|
|
73
|
+
type FloatingAnchorProps = HTMLAttributes<HTMLDivElement> &
|
|
74
|
+
AsChildProps & {
|
|
75
|
+
virtualRef?: React.RefObject<Measurable>;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* `FloatingAnchor` provides an anchor for a Floating instance.
|
|
80
|
+
* Allows anchoring to non-DOM nodes like a cursor position when used with `virtualRef`.
|
|
81
|
+
*/
|
|
82
|
+
const FloatingAnchor = forwardRef<HTMLDivElement, FloatingAnchorProps>(
|
|
83
|
+
({ virtualRef, asChild, ...rest }: FloatingAnchorProps, forwardedRef) => {
|
|
84
|
+
const context = useFloatingContext();
|
|
85
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
86
|
+
|
|
87
|
+
const mergedRef = useMergeRefs(forwardedRef, ref);
|
|
88
|
+
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
// Allows anchoring the floating to non-DOM nodes like a cursor position.
|
|
91
|
+
// We replace `anchorRef` with a virtual ref in such cases.
|
|
92
|
+
context.onAnchorChange(virtualRef?.current || ref.current);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const Comp = asChild ? Slot : "div";
|
|
96
|
+
|
|
97
|
+
return virtualRef ? null : <Comp ref={mergedRef} {...rest} />;
|
|
98
|
+
},
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Floating Arrow
|
|
103
|
+
*/
|
|
104
|
+
const OPPOSITE_SIDE: Record<Side, Side> = {
|
|
105
|
+
top: "bottom",
|
|
106
|
+
right: "left",
|
|
107
|
+
bottom: "top",
|
|
108
|
+
left: "right",
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
interface FloatingArrowProps {
|
|
112
|
+
className?: string;
|
|
113
|
+
width?: number;
|
|
114
|
+
height?: number;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const FloatingArrow = ({ width, height, className }: FloatingArrowProps) => {
|
|
118
|
+
const context = useFloatingContentContext();
|
|
119
|
+
|
|
120
|
+
const side = OPPOSITE_SIDE[context.placedSide];
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<span
|
|
124
|
+
ref={context.onArrowChange}
|
|
125
|
+
style={{
|
|
126
|
+
position: "absolute",
|
|
127
|
+
left: context.arrowX,
|
|
128
|
+
top: context.arrowY,
|
|
129
|
+
[side]: 0,
|
|
130
|
+
transformOrigin: {
|
|
131
|
+
top: "",
|
|
132
|
+
right: "0 0",
|
|
133
|
+
bottom: "center 0",
|
|
134
|
+
left: "100% 0",
|
|
135
|
+
}[context.placedSide],
|
|
136
|
+
transform: {
|
|
137
|
+
top: "translateY(100%)",
|
|
138
|
+
right: "translateY(50%) rotate(90deg) translateX(-50%)",
|
|
139
|
+
bottom: `rotate(180deg)`,
|
|
140
|
+
left: "translateY(50%) rotate(-90deg) translateX(50%)",
|
|
141
|
+
}[context.placedSide],
|
|
142
|
+
visibility: context.hideArrow ? "hidden" : undefined,
|
|
143
|
+
}}
|
|
144
|
+
aria-hidden
|
|
145
|
+
>
|
|
146
|
+
<svg
|
|
147
|
+
className={className}
|
|
148
|
+
width={width}
|
|
149
|
+
height={height}
|
|
150
|
+
viewBox="0 0 30 10"
|
|
151
|
+
preserveAspectRatio="none"
|
|
152
|
+
style={{ display: "block" }}
|
|
153
|
+
>
|
|
154
|
+
<polygon points="0,0 30,0 15,10" />
|
|
155
|
+
</svg>
|
|
156
|
+
</span>
|
|
157
|
+
);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Floating Content
|
|
162
|
+
*/
|
|
163
|
+
type FloatingContentContextValue = {
|
|
164
|
+
placedSide: Side;
|
|
165
|
+
onArrowChange: (arrow: HTMLSpanElement | null) => void;
|
|
166
|
+
arrowX?: number;
|
|
167
|
+
arrowY?: number;
|
|
168
|
+
hideArrow: boolean;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const [FloatingContentProvider, useFloatingContentContext] =
|
|
172
|
+
createContext<FloatingContentContextValue>({
|
|
173
|
+
name: "FloatingContentContext",
|
|
174
|
+
hookName: "useFloatingContentContext",
|
|
175
|
+
providerName: "FloatingContentProvider",
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
type Boundary = Element | null;
|
|
179
|
+
|
|
180
|
+
interface FloatingContentProps extends HTMLAttributes<HTMLDivElement> {
|
|
181
|
+
side?: Side;
|
|
182
|
+
sideOffset?: number;
|
|
183
|
+
align?: Align;
|
|
184
|
+
alignOffset?: number;
|
|
185
|
+
avoidCollisions?: boolean;
|
|
186
|
+
collisionBoundary?: Boundary | Boundary[];
|
|
187
|
+
collisionPadding?: number | Partial<Record<Side, number>>;
|
|
188
|
+
hideWhenDetached?: boolean;
|
|
189
|
+
updatePositionStrategy?: "optimized" | "always";
|
|
190
|
+
onPlaced?: () => void;
|
|
191
|
+
arrow?: {
|
|
192
|
+
className?: string;
|
|
193
|
+
padding?: number;
|
|
194
|
+
width: number;
|
|
195
|
+
height: number;
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const FloatingContent = forwardRef<HTMLDivElement, FloatingContentProps>(
|
|
200
|
+
(
|
|
201
|
+
{
|
|
202
|
+
children,
|
|
203
|
+
side = "bottom",
|
|
204
|
+
sideOffset = 0,
|
|
205
|
+
align = "center",
|
|
206
|
+
alignOffset = 0,
|
|
207
|
+
avoidCollisions = true,
|
|
208
|
+
collisionBoundary = [],
|
|
209
|
+
collisionPadding: collisionPaddingProp = 0,
|
|
210
|
+
hideWhenDetached = false,
|
|
211
|
+
updatePositionStrategy = "optimized",
|
|
212
|
+
onPlaced,
|
|
213
|
+
arrow: _arrow,
|
|
214
|
+
...contentProps
|
|
215
|
+
}: FloatingContentProps,
|
|
216
|
+
forwardedRef,
|
|
217
|
+
) => {
|
|
218
|
+
const context = useFloatingContext();
|
|
219
|
+
|
|
220
|
+
const [content, setContent] = useState<HTMLDivElement | null>(null);
|
|
221
|
+
const mergeRefs = useMergeRefs(forwardedRef, setContent);
|
|
222
|
+
|
|
223
|
+
const arrowDefaults = {
|
|
224
|
+
padding: 5,
|
|
225
|
+
width: 0,
|
|
226
|
+
height: 0,
|
|
227
|
+
..._arrow,
|
|
228
|
+
};
|
|
229
|
+
const [arrow, setArrow] = useState<HTMLSpanElement | null>(null);
|
|
230
|
+
const arrowWidth = arrowDefaults.width;
|
|
231
|
+
const arrowHeight = arrowDefaults.height;
|
|
232
|
+
|
|
233
|
+
const desiredPlacement = (side +
|
|
234
|
+
(align !== "center" ? "-" + align : "")) as Placement;
|
|
235
|
+
|
|
236
|
+
const collisionPadding =
|
|
237
|
+
typeof collisionPaddingProp === "number"
|
|
238
|
+
? collisionPaddingProp
|
|
239
|
+
: { top: 0, right: 0, bottom: 0, left: 0, ...collisionPaddingProp };
|
|
240
|
+
|
|
241
|
+
const boundary = Array.isArray(collisionBoundary)
|
|
242
|
+
? collisionBoundary
|
|
243
|
+
: [collisionBoundary];
|
|
244
|
+
|
|
245
|
+
const hasExplicitBoundaries = boundary.length > 0;
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* .filter(x => x !== null) does not narrow the type of the array enough.
|
|
249
|
+
*/
|
|
250
|
+
function isNotNull<T>(value: T | null): value is T {
|
|
251
|
+
return value !== null;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const detectOverflowOptions = {
|
|
255
|
+
padding: collisionPadding,
|
|
256
|
+
boundary: boundary.filter(isNotNull),
|
|
257
|
+
// with `strategy: 'fixed'`, this is the only way to get it to respect boundaries
|
|
258
|
+
altBoundary: hasExplicitBoundaries,
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const { refs, floatingStyles, placement, isPositioned, middlewareData } =
|
|
262
|
+
useFloating({
|
|
263
|
+
// default to `fixed` strategy so users don't have to pick and we also avoid focus scroll issues
|
|
264
|
+
strategy: "fixed",
|
|
265
|
+
placement: desiredPlacement,
|
|
266
|
+
whileElementsMounted: (...args) => {
|
|
267
|
+
const cleanup = autoUpdate(...args, {
|
|
268
|
+
animationFrame: updatePositionStrategy === "always",
|
|
269
|
+
});
|
|
270
|
+
return cleanup;
|
|
271
|
+
},
|
|
272
|
+
elements: {
|
|
273
|
+
reference: context.anchor,
|
|
274
|
+
},
|
|
275
|
+
middleware: [
|
|
276
|
+
offset({
|
|
277
|
+
mainAxis: sideOffset + arrowHeight,
|
|
278
|
+
alignmentAxis: alignOffset,
|
|
279
|
+
}),
|
|
280
|
+
avoidCollisions &&
|
|
281
|
+
shift({
|
|
282
|
+
mainAxis: true,
|
|
283
|
+
crossAxis: false,
|
|
284
|
+
limiter: limitShift(),
|
|
285
|
+
...detectOverflowOptions,
|
|
286
|
+
}),
|
|
287
|
+
avoidCollisions && flip({ ...detectOverflowOptions }),
|
|
288
|
+
size({
|
|
289
|
+
...detectOverflowOptions,
|
|
290
|
+
apply: ({ elements, rects, availableWidth, availableHeight }) => {
|
|
291
|
+
const { width: anchorWidth, height: anchorHeight } =
|
|
292
|
+
rects.reference;
|
|
293
|
+
const contentStyle = elements.floating.style;
|
|
294
|
+
/**
|
|
295
|
+
* Allows styling and animations based on the available space.
|
|
296
|
+
*/
|
|
297
|
+
contentStyle.setProperty(
|
|
298
|
+
"--ac-floating-available-width",
|
|
299
|
+
`${availableWidth}px`,
|
|
300
|
+
);
|
|
301
|
+
contentStyle.setProperty(
|
|
302
|
+
"--ac-floating-available-height",
|
|
303
|
+
`${availableHeight}px`,
|
|
304
|
+
);
|
|
305
|
+
contentStyle.setProperty(
|
|
306
|
+
"--ac-floating-anchor-width",
|
|
307
|
+
`${anchorWidth}px`,
|
|
308
|
+
);
|
|
309
|
+
contentStyle.setProperty(
|
|
310
|
+
"--ac-floating-anchor-height",
|
|
311
|
+
`${anchorHeight}px`,
|
|
312
|
+
);
|
|
313
|
+
},
|
|
314
|
+
}),
|
|
315
|
+
arrow &&
|
|
316
|
+
floatingArrow({ element: arrow, padding: arrowDefaults.padding }),
|
|
317
|
+
transformOrigin({ arrowWidth, arrowHeight }),
|
|
318
|
+
hideWhenDetached &&
|
|
319
|
+
hide({ strategy: "referenceHidden", ...detectOverflowOptions }),
|
|
320
|
+
],
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
const [placedSide, placedAlign] = getSideAndAlignFromPlacement(placement);
|
|
324
|
+
|
|
325
|
+
const handlePlaced = useCallbackRef(onPlaced);
|
|
326
|
+
|
|
327
|
+
useClientLayoutEffect(() => {
|
|
328
|
+
isPositioned && handlePlaced?.();
|
|
329
|
+
}, [isPositioned, handlePlaced]);
|
|
330
|
+
|
|
331
|
+
const arrowX = middlewareData.arrow?.x;
|
|
332
|
+
const arrowY = middlewareData.arrow?.y;
|
|
333
|
+
const cannotCenterArrow = middlewareData.arrow?.centerOffset !== 0;
|
|
334
|
+
|
|
335
|
+
const [contentZIndex, setContentZIndex] = useState<string>();
|
|
336
|
+
useClientLayoutEffect(() => {
|
|
337
|
+
if (content) setContentZIndex(window.getComputedStyle(content).zIndex);
|
|
338
|
+
}, [content]);
|
|
339
|
+
|
|
340
|
+
return (
|
|
341
|
+
<div
|
|
342
|
+
ref={refs.setFloating}
|
|
343
|
+
data-aksel-floating-content-wrapper=""
|
|
344
|
+
style={{
|
|
345
|
+
...floatingStyles,
|
|
346
|
+
transform: isPositioned
|
|
347
|
+
? floatingStyles.transform
|
|
348
|
+
: "translate(0, -200%)", // keep off the page when measuring
|
|
349
|
+
minWidth: "max-content",
|
|
350
|
+
zIndex: contentZIndex,
|
|
351
|
+
["--ac-floating-transform-origin" as any]: [
|
|
352
|
+
middlewareData.transformOrigin?.x,
|
|
353
|
+
middlewareData.transformOrigin?.y,
|
|
354
|
+
].join(" "),
|
|
355
|
+
}}
|
|
356
|
+
// Floating UI uses the `dir` attribute on the reference/floating node for logical alignment.
|
|
357
|
+
// This attribute is necessary for both portalled and inline calculations.
|
|
358
|
+
dir="ltr"
|
|
359
|
+
>
|
|
360
|
+
<FloatingContentProvider
|
|
361
|
+
placedSide={placedSide}
|
|
362
|
+
onArrowChange={setArrow}
|
|
363
|
+
arrowX={arrowX}
|
|
364
|
+
arrowY={arrowY}
|
|
365
|
+
hideArrow={cannotCenterArrow}
|
|
366
|
+
>
|
|
367
|
+
<div
|
|
368
|
+
ref={mergeRefs}
|
|
369
|
+
data-side={placedSide}
|
|
370
|
+
data-align={placedAlign}
|
|
371
|
+
{...contentProps}
|
|
372
|
+
style={{
|
|
373
|
+
...contentProps.style,
|
|
374
|
+
// if the FloatingContent hasn't been placed yet (not all measurements done)
|
|
375
|
+
// we prevent animations so that users's animation don't kick in too early referring wrong sides
|
|
376
|
+
animation: !isPositioned ? "none" : undefined,
|
|
377
|
+
// hide the content if using the hide middleware and should be hidden
|
|
378
|
+
opacity: middlewareData.hide?.referenceHidden ? 0 : undefined,
|
|
379
|
+
}}
|
|
380
|
+
>
|
|
381
|
+
{children}
|
|
382
|
+
{_arrow?.height && _arrow?.width && (
|
|
383
|
+
<FloatingArrow
|
|
384
|
+
width={_arrow.width}
|
|
385
|
+
height={_arrow.height}
|
|
386
|
+
className={_arrow.className}
|
|
387
|
+
/>
|
|
388
|
+
)}
|
|
389
|
+
</div>
|
|
390
|
+
</FloatingContentProvider>
|
|
391
|
+
</div>
|
|
392
|
+
);
|
|
393
|
+
},
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
Floating.Anchor = FloatingAnchor;
|
|
397
|
+
Floating.Content = FloatingContent;
|
|
398
|
+
|
|
399
|
+
export { Floating };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Middleware, Placement } from "@floating-ui/react-dom";
|
|
2
|
+
|
|
3
|
+
const SIDE_OPTIONS = ["top", "right", "bottom", "left"] as const;
|
|
4
|
+
const ALIGN_OPTIONS = ["start", "center", "end"] as const;
|
|
5
|
+
|
|
6
|
+
type Side = (typeof SIDE_OPTIONS)[number];
|
|
7
|
+
type Align = (typeof ALIGN_OPTIONS)[number];
|
|
8
|
+
type Measurable = { getBoundingClientRect(): DOMRect };
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* `transformOrigin` is a custom middleware for floating-ui that calculates the transform origin of the floating-element.
|
|
12
|
+
*/
|
|
13
|
+
function transformOrigin(options: {
|
|
14
|
+
arrowWidth: number;
|
|
15
|
+
arrowHeight: number;
|
|
16
|
+
}): Middleware {
|
|
17
|
+
return {
|
|
18
|
+
name: "transformOrigin",
|
|
19
|
+
options,
|
|
20
|
+
fn(data) {
|
|
21
|
+
const { placement, rects, middlewareData } = data;
|
|
22
|
+
|
|
23
|
+
const cannotCenterArrow = middlewareData.arrow?.centerOffset !== 0;
|
|
24
|
+
const isArrowHidden = cannotCenterArrow;
|
|
25
|
+
const arrowWidth = isArrowHidden ? 0 : options.arrowWidth;
|
|
26
|
+
const arrowHeight = isArrowHidden ? 0 : options.arrowHeight;
|
|
27
|
+
|
|
28
|
+
const [placedSide, placedAlign] = getSideAndAlignFromPlacement(placement);
|
|
29
|
+
const noArrowAlign = { start: "0%", center: "50%", end: "100%" }[
|
|
30
|
+
placedAlign
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
const arrowXCenter = (middlewareData.arrow?.x ?? 0) + arrowWidth / 2;
|
|
34
|
+
const arrowYCenter = (middlewareData.arrow?.y ?? 0) + arrowHeight / 2;
|
|
35
|
+
|
|
36
|
+
let x = "";
|
|
37
|
+
let y = "";
|
|
38
|
+
|
|
39
|
+
if (placedSide === "bottom") {
|
|
40
|
+
x = isArrowHidden ? noArrowAlign : `${arrowXCenter}px`;
|
|
41
|
+
y = `${-arrowHeight}px`;
|
|
42
|
+
} else if (placedSide === "top") {
|
|
43
|
+
x = isArrowHidden ? noArrowAlign : `${arrowXCenter}px`;
|
|
44
|
+
y = `${rects.floating.height + arrowHeight}px`;
|
|
45
|
+
} else if (placedSide === "right") {
|
|
46
|
+
x = `${-arrowHeight}px`;
|
|
47
|
+
y = isArrowHidden ? noArrowAlign : `${arrowYCenter}px`;
|
|
48
|
+
} else if (placedSide === "left") {
|
|
49
|
+
x = `${rects.floating.width + arrowHeight}px`;
|
|
50
|
+
y = isArrowHidden ? noArrowAlign : `${arrowYCenter}px`;
|
|
51
|
+
}
|
|
52
|
+
return { data: { x, y } };
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function getSideAndAlignFromPlacement(placement: Placement) {
|
|
58
|
+
const [side, align = "center"] = placement.split("-");
|
|
59
|
+
return [side as Side, align as Align] as const;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { getSideAndAlignFromPlacement, transformOrigin };
|
|
63
|
+
export type { Side, Align, Measurable };
|