@basic-ui/core 0.0.50 → 0.0.52
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/README.md +3 -3
- package/build/cjs/index.js +106 -92
- package/build/cjs/index.js.map +1 -1
- package/build/esm/Accordion/Accordion.d.ts +9 -9
- package/build/esm/Accordion/Accordion.js.map +1 -1
- package/build/esm/Accordion/AccordionBody.d.ts +6 -6
- package/build/esm/Accordion/AccordionBody.js.map +1 -1
- package/build/esm/Accordion/AccordionHeader.d.ts +7 -7
- package/build/esm/Accordion/AccordionHeader.js.map +1 -1
- package/build/esm/Accordion/AccordionItem.d.ts +9 -9
- package/build/esm/Accordion/AccordionItem.js.map +1 -1
- package/build/esm/Accordion/context.d.ts +19 -19
- package/build/esm/Accordion/context.js.map +1 -1
- package/build/esm/Accordion/index.d.ts +4 -4
- package/build/esm/Accordion/index.js.map +1 -1
- package/build/esm/Accordion/scopeQuery.d.ts +2 -2
- package/build/esm/Accordion/scopeQuery.js.map +1 -1
- package/build/esm/CheckBox/CheckBox.d.ts +7 -7
- package/build/esm/CheckBox/CheckBox.js.map +1 -1
- package/build/esm/CheckBox/index.d.ts +1 -1
- package/build/esm/CheckBox/index.js.map +1 -1
- package/build/esm/ComboBox/Combobox.d.ts +18 -18
- package/build/esm/ComboBox/Combobox.js.map +1 -1
- package/build/esm/ComboBox/ComboboxButton.d.ts +9 -9
- package/build/esm/ComboBox/ComboboxButton.js.map +1 -1
- package/build/esm/ComboBox/ComboboxInput.d.ts +17 -17
- package/build/esm/ComboBox/ComboboxInput.js.map +1 -1
- package/build/esm/ComboBox/ComboboxLabel.d.ts +7 -7
- package/build/esm/ComboBox/ComboboxLabel.js.map +1 -1
- package/build/esm/ComboBox/ComboboxList.d.ts +8 -8
- package/build/esm/ComboBox/ComboboxList.js.map +1 -1
- package/build/esm/ComboBox/ComboboxOption.d.ts +11 -11
- package/build/esm/ComboBox/ComboboxOption.js.map +1 -1
- package/build/esm/ComboBox/ComboboxPopover.d.ts +9 -9
- package/build/esm/ComboBox/ComboboxPopover.js.map +1 -1
- package/build/esm/ComboBox/cities.d.ts +5 -5
- package/build/esm/ComboBox/cities.js.map +1 -1
- package/build/esm/ComboBox/context.d.ts +30 -30
- package/build/esm/ComboBox/context.js.map +1 -1
- package/build/esm/ComboBox/hooks.d.ts +37 -37
- package/build/esm/ComboBox/hooks.js.map +1 -1
- package/build/esm/ComboBox/index.d.ts +8 -8
- package/build/esm/ComboBox/index.js.map +1 -1
- package/build/esm/ComboBox/makeHash.d.ts +1 -1
- package/build/esm/ComboBox/makeHash.js.map +1 -1
- package/build/esm/ComboBox/scopeQuery.d.ts +1 -1
- package/build/esm/ComboBox/scopeQuery.js.map +1 -1
- package/build/esm/FocusLock/FocusLock.d.ts +9 -9
- package/build/esm/FocusLock/FocusLock.js.map +1 -1
- package/build/esm/FocusLock/index.d.ts +1 -1
- package/build/esm/FocusLock/index.js.map +1 -1
- package/build/esm/FocusLock/tabUtils.d.ts +3 -3
- package/build/esm/FocusLock/tabUtils.js.map +1 -1
- package/build/esm/FocusLock/useFocusLock.d.ts +7 -7
- package/build/esm/FocusLock/useFocusLock.js.map +1 -1
- package/build/esm/List/List.d.ts +7 -7
- package/build/esm/List/List.js.map +1 -1
- package/build/esm/List/ListItem.d.ts +7 -7
- package/build/esm/List/ListItem.js.map +1 -1
- package/build/esm/List/context.d.ts +4 -4
- package/build/esm/List/context.js.map +1 -1
- package/build/esm/List/index.d.ts +2 -2
- package/build/esm/List/index.js.map +1 -1
- package/build/esm/Menu/ContextMenuTrigger.d.ts +11 -11
- package/build/esm/Menu/ContextMenuTrigger.js.map +1 -1
- package/build/esm/Menu/Menu.d.ts +10 -10
- package/build/esm/Menu/Menu.js.map +1 -1
- package/build/esm/Menu/MenuButton.d.ts +11 -11
- package/build/esm/Menu/MenuButton.js.map +1 -1
- package/build/esm/Menu/MenuItem.d.ts +8 -8
- package/build/esm/Menu/MenuItem.js +1 -0
- package/build/esm/Menu/MenuItem.js.map +1 -1
- package/build/esm/Menu/MenuList.d.ts +7 -7
- package/build/esm/Menu/MenuList.js +17 -4
- package/build/esm/Menu/MenuList.js.map +1 -1
- package/build/esm/Menu/MenuPopover.d.ts +8 -8
- package/build/esm/Menu/MenuPopover.js.map +1 -1
- package/build/esm/Menu/context.d.ts +25 -25
- package/build/esm/Menu/context.js.map +1 -1
- package/build/esm/Menu/fixtures/countryList.d.ts +1 -1
- package/build/esm/Menu/fixtures/countryList.js.map +1 -1
- package/build/esm/Menu/index.d.ts +6 -6
- package/build/esm/Menu/index.js.map +1 -1
- package/build/esm/Menu/scope.d.ts +1 -1
- package/build/esm/Menu/scope.js.map +1 -1
- package/build/esm/Modal/Modal.d.ts +9 -9
- package/build/esm/Modal/Modal.js.map +1 -1
- package/build/esm/Modal/ModalBackdrop.d.ts +10 -10
- package/build/esm/Modal/ModalBackdrop.js.map +1 -1
- package/build/esm/Modal/index.d.ts +2 -2
- package/build/esm/Modal/index.js.map +1 -1
- package/build/esm/Popper/Popper.d.ts +35 -35
- package/build/esm/Popper/Popper.js.map +1 -1
- package/build/esm/Popper/PopperArrow.d.ts +6 -6
- package/build/esm/Popper/PopperArrow.js.map +1 -1
- package/build/esm/Popper/context.d.ts +6 -6
- package/build/esm/Popper/context.js.map +1 -1
- package/build/esm/Popper/index.d.ts +3 -3
- package/build/esm/Popper/index.js.map +1 -1
- package/build/esm/Portal/Portal.d.ts +6 -6
- package/build/esm/Portal/Portal.js.map +1 -1
- package/build/esm/Portal/index.d.ts +1 -1
- package/build/esm/Portal/index.js.map +1 -1
- package/build/esm/RadioButton/RadioButton.d.ts +10 -10
- package/build/esm/RadioButton/RadioButton.js.map +1 -1
- package/build/esm/RadioButton/RadioGroup.d.ts +12 -12
- package/build/esm/RadioButton/RadioGroup.js.map +1 -1
- package/build/esm/RadioButton/context.d.ts +9 -9
- package/build/esm/RadioButton/context.js.map +1 -1
- package/build/esm/RadioButton/index.d.ts +2 -2
- package/build/esm/RadioButton/index.js.map +1 -1
- package/build/esm/SkipNav/SkipNav.d.ts +7 -7
- package/build/esm/SkipNav/SkipNav.js.map +1 -1
- package/build/esm/SkipNav/index.d.ts +1 -1
- package/build/esm/SkipNav/index.js.map +1 -1
- package/build/esm/Slider/Slider.d.ts +197 -197
- package/build/esm/Slider/Slider.js +82 -82
- package/build/esm/Slider/Slider.js.map +1 -1
- package/build/esm/Slider/index.d.ts +1 -1
- package/build/esm/Slider/index.js.map +1 -1
- package/build/esm/Spinner/Spinner.d.ts +12 -12
- package/build/esm/Spinner/Spinner.js.map +1 -1
- package/build/esm/Spinner/SpinnerButton.d.ts +8 -8
- package/build/esm/Spinner/SpinnerButton.js.map +1 -1
- package/build/esm/Spinner/context.d.ts +12 -12
- package/build/esm/Spinner/context.js.map +1 -1
- package/build/esm/Spinner/index.d.ts +2 -2
- package/build/esm/Spinner/index.js.map +1 -1
- package/build/esm/Tabs/Tab.d.ts +7 -7
- package/build/esm/Tabs/Tab.js.map +1 -1
- package/build/esm/Tabs/TabList.d.ts +9 -9
- package/build/esm/Tabs/TabList.js.map +1 -1
- package/build/esm/Tabs/TabPanel.d.ts +8 -8
- package/build/esm/Tabs/TabPanel.js.map +1 -1
- package/build/esm/Tabs/TabPanels.d.ts +8 -8
- package/build/esm/Tabs/TabPanels.js.map +1 -1
- package/build/esm/Tabs/Tabs.d.ts +10 -10
- package/build/esm/Tabs/Tabs.js.map +1 -1
- package/build/esm/Tabs/context.d.ts +17 -17
- package/build/esm/Tabs/context.js.map +1 -1
- package/build/esm/Tabs/index.d.ts +5 -5
- package/build/esm/Tabs/index.js.map +1 -1
- package/build/esm/Tabs/scopeQuery.d.ts +1 -1
- package/build/esm/Tabs/scopeQuery.js.map +1 -1
- package/build/esm/Tooltip/Tooltip.d.ts +10 -10
- package/build/esm/Tooltip/Tooltip.js.map +1 -1
- package/build/esm/Tooltip/index.d.ts +1 -1
- package/build/esm/Tooltip/index.js.map +1 -1
- package/build/esm/Tooltip/stateMachine.d.ts +28 -28
- package/build/esm/Tooltip/stateMachine.js.map +1 -1
- package/build/esm/Tooltip/useTooltip.d.ts +10 -10
- package/build/esm/Tooltip/useTooltip.js.map +1 -1
- package/build/esm/hooks/index.d.ts +13 -13
- package/build/esm/hooks/index.js.map +1 -1
- package/build/esm/hooks/useAutoFocus.d.ts +2 -2
- package/build/esm/hooks/useAutoFocus.js.map +1 -1
- package/build/esm/hooks/useChildrenCounter.d.ts +7 -7
- package/build/esm/hooks/useChildrenCounter.js.map +1 -1
- package/build/esm/hooks/useControlledState.d.ts +3 -3
- package/build/esm/hooks/useControlledState.js +1 -1
- package/build/esm/hooks/useControlledState.js.map +1 -1
- package/build/esm/hooks/useFocusReturn.d.ts +2 -2
- package/build/esm/hooks/useFocusReturn.js.map +1 -1
- package/build/esm/hooks/useFocusState.d.ts +11 -11
- package/build/esm/hooks/useFocusState.js.map +1 -1
- package/build/esm/hooks/useGestureHandlers.d.ts +52 -52
- package/build/esm/hooks/useGestureHandlers.js.map +1 -1
- package/build/esm/hooks/useMeasure.d.ts +7 -7
- package/build/esm/hooks/useMeasure.js.map +1 -1
- package/build/esm/hooks/useOnClickOutside.d.ts +2 -2
- package/build/esm/hooks/useOnClickOutside.js.map +1 -1
- package/build/esm/hooks/useOnKeyDown.d.ts +1 -1
- package/build/esm/hooks/useOnKeyDown.js.map +1 -1
- package/build/esm/hooks/useReducerMachine.d.ts +24 -24
- package/build/esm/hooks/useReducerMachine.js.map +1 -1
- package/build/esm/hooks/useRemoveBodyScroll.d.ts +2 -2
- package/build/esm/hooks/useRemoveBodyScroll.js.map +1 -1
- package/build/esm/hooks/useScope.d.ts +11 -11
- package/build/esm/hooks/useScope.js.map +1 -1
- package/build/esm/hooks/useThrottle.d.ts +1 -1
- package/build/esm/hooks/useThrottle.js.map +1 -1
- package/build/esm/index.d.ts +15 -15
- package/build/esm/index.js.map +1 -1
- package/build/esm/utils/assign-ref.d.ts +3 -3
- package/build/esm/utils/assign-ref.js.map +1 -1
- package/build/esm/utils/can-use-dom.d.ts +1 -1
- package/build/esm/utils/can-use-dom.js.map +1 -1
- package/build/esm/utils/clamp.d.ts +1 -1
- package/build/esm/utils/clamp.js.map +1 -1
- package/build/esm/utils/context.d.ts +7 -7
- package/build/esm/utils/context.js.map +1 -1
- package/build/esm/utils/create-subscription.d.ts +4 -4
- package/build/esm/utils/create-subscription.js.map +1 -1
- package/build/esm/utils/get-circular-index.d.ts +1 -1
- package/build/esm/utils/get-circular-index.js.map +1 -1
- package/build/esm/utils/index.d.ts +10 -10
- package/build/esm/utils/index.js.map +1 -1
- package/build/esm/utils/is-right-click.d.ts +6 -6
- package/build/esm/utils/is-right-click.js +4 -4
- package/build/esm/utils/is-right-click.js.map +1 -1
- package/build/esm/utils/owner-document.d.ts +7 -7
- package/build/esm/utils/owner-document.js +5 -5
- package/build/esm/utils/owner-document.js.map +1 -1
- package/build/esm/utils/polymorphic.d.ts +39 -39
- package/build/esm/utils/polymorphic.js.map +1 -1
- package/build/esm/utils/rubber-band-clamp.d.ts +2 -2
- package/build/esm/utils/rubber-band-clamp.js.map +1 -1
- package/build/esm/utils/use-stable-callback.d.ts +16 -16
- package/build/esm/utils/use-stable-callback.js +16 -16
- package/build/esm/utils/use-stable-callback.js.map +1 -1
- package/build/esm/utils/wrap-event.d.ts +3 -3
- package/build/esm/utils/wrap-event.js.map +1 -1
- package/build/tsconfig-build.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/Accordion/Accordion.story.tsx +74 -74
- package/src/Accordion/Accordion.tsx +59 -59
- package/src/Accordion/AccordionBody.tsx +52 -52
- package/src/Accordion/AccordionHeader.tsx +167 -167
- package/src/Accordion/AccordionItem.tsx +50 -50
- package/src/Accordion/context.ts +37 -37
- package/src/Accordion/index.ts +4 -4
- package/src/Accordion/scopeQuery.ts +7 -7
- package/src/Accordion/styles.css +21 -21
- package/src/CheckBox/CheckBox.tsx +41 -41
- package/src/CheckBox/index.ts +1 -1
- package/src/ComboBox/ComboBox.story.tsx +120 -120
- package/src/ComboBox/Combobox.tsx +148 -148
- package/src/ComboBox/ComboboxButton.tsx +61 -61
- package/src/ComboBox/ComboboxInput.tsx +187 -187
- package/src/ComboBox/ComboboxLabel.tsx +33 -33
- package/src/ComboBox/ComboboxList.tsx +47 -47
- package/src/ComboBox/ComboboxOption.tsx +111 -111
- package/src/ComboBox/ComboboxPopover.tsx +64 -64
- package/src/ComboBox/cities.ts +23194 -23194
- package/src/ComboBox/context.ts +35 -35
- package/src/ComboBox/hooks.tsx +451 -451
- package/src/ComboBox/index.ts +8 -8
- package/src/ComboBox/makeHash.ts +19 -19
- package/src/ComboBox/scopeQuery.ts +6 -6
- package/src/ComboBox/styles.css +32 -32
- package/src/FocusLock/FocusLock.tsx +66 -66
- package/src/FocusLock/index.ts +1 -1
- package/src/FocusLock/tabUtils.ts +40 -40
- package/src/FocusLock/useFocusLock.ts +56 -56
- package/src/List/List.story.tsx +18 -18
- package/src/List/List.tsx +17 -17
- package/src/List/ListItem.tsx +23 -23
- package/src/List/context.ts +19 -19
- package/src/List/index.ts +2 -2
- package/src/Menu/ContextMenu.story.tsx +73 -73
- package/src/Menu/ContextMenuTrigger.tsx +76 -76
- package/src/Menu/Menu.story.tsx +160 -160
- package/src/Menu/Menu.tsx +83 -83
- package/src/Menu/MenuButton.tsx +83 -83
- package/src/Menu/MenuComplex.story.tsx +58 -58
- package/src/Menu/MenuItem.tsx +88 -87
- package/src/Menu/MenuList.tsx +254 -243
- package/src/Menu/MenuPopover.tsx +35 -35
- package/src/Menu/context.ts +44 -44
- package/src/Menu/fixtures/countryList.ts +198 -198
- package/src/Menu/index.ts +6 -6
- package/src/Menu/scope.ts +7 -7
- package/src/Menu/styles.css +42 -42
- package/src/Modal/Modal.story.tsx +258 -258
- package/src/Modal/Modal.tsx +48 -48
- package/src/Modal/ModalBackdrop.tsx +78 -78
- package/src/Modal/NavDrawer.story.tsx +158 -158
- package/src/Modal/index.ts +2 -2
- package/src/Modal/styles.css +46 -46
- package/src/Popper/Popper.story.tsx +263 -263
- package/src/Popper/Popper.tsx +154 -154
- package/src/Popper/PopperArrow.tsx +35 -35
- package/src/Popper/context.ts +10 -10
- package/src/Popper/index.ts +3 -3
- package/src/Popper/styles.css +60 -60
- package/src/Portal/Portal.tsx +20 -20
- package/src/Portal/index.ts +1 -1
- package/src/RadioButton/RadioButton.story.tsx +77 -77
- package/src/RadioButton/RadioButton.tsx +55 -55
- package/src/RadioButton/RadioGroup.tsx +60 -60
- package/src/RadioButton/context.ts +17 -17
- package/src/RadioButton/index.ts +2 -2
- package/src/SkipNav/SkipNav.tsx +16 -16
- package/src/SkipNav/index.tsx +1 -1
- package/src/Slider/Slider.story.tsx +45 -45
- package/src/Slider/Slider.tsx +1120 -1120
- package/src/Slider/index.ts +1 -1
- package/src/Slider/styles.css +131 -131
- package/src/Spinner/Spinner.story.tsx +31 -31
- package/src/Spinner/Spinner.tsx +117 -117
- package/src/Spinner/SpinnerButton.tsx +54 -54
- package/src/Spinner/context.ts +20 -20
- package/src/Spinner/index.ts +2 -2
- package/src/Spinner/styles.css +23 -23
- package/src/Tabs/Tab.story.tsx +80 -80
- package/src/Tabs/Tab.tsx +136 -136
- package/src/Tabs/TabList.tsx +71 -71
- package/src/Tabs/TabPanel.tsx +53 -53
- package/src/Tabs/TabPanels.tsx +30 -30
- package/src/Tabs/Tabs.tsx +46 -46
- package/src/Tabs/context.ts +30 -30
- package/src/Tabs/index.tsx +5 -5
- package/src/Tabs/scopeQuery.ts +6 -6
- package/src/Tooltip/Tooltip.story.tsx +61 -61
- package/src/Tooltip/Tooltip.tsx +50 -50
- package/src/Tooltip/index.ts +1 -1
- package/src/Tooltip/stateMachine.ts +192 -192
- package/src/Tooltip/styles.css +17 -17
- package/src/Tooltip/useTooltip.ts +136 -136
- package/src/hooks/index.ts +13 -13
- package/src/hooks/useAutoFocus.ts +22 -22
- package/src/hooks/useChildrenCounter.ts +51 -51
- package/src/hooks/useControlledState.ts +3 -3
- package/src/hooks/useFocusReturn.ts +43 -43
- package/src/hooks/useFocusState.ts +30 -30
- package/src/hooks/useGestureHandlers.ts +286 -286
- package/src/hooks/useMeasure.ts +33 -33
- package/src/hooks/useOnClickOutside.ts +32 -32
- package/src/hooks/useOnKeyDown.ts +19 -19
- package/src/hooks/useReducerMachine.ts +60 -60
- package/src/hooks/useRemoveBodyScroll.ts +39 -39
- package/src/hooks/useScope.ts +52 -52
- package/src/hooks/useThrottle.ts +19 -19
- package/src/index.ts +20 -20
- package/src/utils/assign-ref.ts +27 -27
- package/src/utils/can-use-dom.ts +7 -7
- package/src/utils/clamp.ts +3 -3
- package/src/utils/context.tsx +48 -48
- package/src/utils/create-subscription.ts +16 -16
- package/src/utils/get-circular-index.ts +7 -7
- package/src/utils/index.ts +10 -10
- package/src/utils/is-right-click.ts +14 -14
- package/src/utils/owner-document.ts +13 -13
- package/src/utils/polymorphic.ts +78 -78
- package/src/utils/rubber-band-clamp.ts +25 -25
- package/src/utils/use-stable-callback.ts +58 -58
- package/src/utils/wrap-event.ts +22 -22
|
@@ -1,286 +1,286 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
MutableRefObject,
|
|
3
|
-
MouseEvent as ReactMouseEvent,
|
|
4
|
-
TouchEvent as ReactTouchEvent,
|
|
5
|
-
} from 'react';
|
|
6
|
-
import { useRef } from 'react';
|
|
7
|
-
|
|
8
|
-
import { getOwnerDocument } from '../utils';
|
|
9
|
-
|
|
10
|
-
export interface GestureHandlersState {
|
|
11
|
-
target: null | EventTarget;
|
|
12
|
-
x: number;
|
|
13
|
-
xDelta: number;
|
|
14
|
-
xDeltaPercent: number;
|
|
15
|
-
xInitial: number;
|
|
16
|
-
xPrev: number;
|
|
17
|
-
xVelocity: number;
|
|
18
|
-
y: number;
|
|
19
|
-
yDelta: number;
|
|
20
|
-
yDeltaPercent: number;
|
|
21
|
-
yInitial: number;
|
|
22
|
-
yPrev: number;
|
|
23
|
-
yVelocity: number;
|
|
24
|
-
startTime: number;
|
|
25
|
-
down: boolean;
|
|
26
|
-
scrollLocked: boolean;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
type SetStateFunc<S> = (
|
|
30
|
-
state: (prevState: Readonly<S>) => S,
|
|
31
|
-
callback?: () => void
|
|
32
|
-
) => void;
|
|
33
|
-
|
|
34
|
-
export interface GestureHandlersReturn {
|
|
35
|
-
onMouseDown: (e: ReactMouseEvent<HTMLElement>) => void;
|
|
36
|
-
onTouchStart: (e: ReactTouchEvent<HTMLElement>) => void;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface GestureHandlerOptions {
|
|
40
|
-
ensureTargetIsContainer?: boolean;
|
|
41
|
-
minTouchDelta?: number;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export const initialGestureHandlersState: GestureHandlersState = {
|
|
45
|
-
target: null,
|
|
46
|
-
x: 0,
|
|
47
|
-
xDelta: 0,
|
|
48
|
-
xDeltaPercent: 0,
|
|
49
|
-
xInitial: 0,
|
|
50
|
-
xPrev: 0,
|
|
51
|
-
xVelocity: 0,
|
|
52
|
-
y: 0,
|
|
53
|
-
yDelta: 0,
|
|
54
|
-
yDeltaPercent: 0,
|
|
55
|
-
yInitial: 0,
|
|
56
|
-
yPrev: 0,
|
|
57
|
-
yVelocity: 0,
|
|
58
|
-
startTime: 0,
|
|
59
|
-
down: false,
|
|
60
|
-
scrollLocked: false,
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const FRAMERATE_CONST = 1000 / 60; // 60 fps
|
|
64
|
-
const VELOCITY_DEPR_FACTOR = FRAMERATE_CONST * 2;
|
|
65
|
-
|
|
66
|
-
export function gestureHandlers(
|
|
67
|
-
set: SetStateFunc<GestureHandlersState>,
|
|
68
|
-
containerRef?: MutableRefObject<HTMLElement | null>,
|
|
69
|
-
options: GestureHandlerOptions = {}
|
|
70
|
-
): GestureHandlersReturn {
|
|
71
|
-
const { ensureTargetIsContainer = false, minTouchDelta = 0 } = options;
|
|
72
|
-
|
|
73
|
-
// Common handlers
|
|
74
|
-
const handleUp = () => {
|
|
75
|
-
set((state: GestureHandlersState) => {
|
|
76
|
-
const deltaTime = Date.now() - state.startTime;
|
|
77
|
-
const xDelta = state.x - state.xInitial;
|
|
78
|
-
const yDelta = state.y - state.yInitial;
|
|
79
|
-
const xVelocity = calcVelocity(xDelta, deltaTime, state.xVelocity);
|
|
80
|
-
const yVelocity = calcVelocity(yDelta, deltaTime, state.yVelocity);
|
|
81
|
-
const newState: GestureHandlersState = {
|
|
82
|
-
...state,
|
|
83
|
-
xVelocity,
|
|
84
|
-
yVelocity,
|
|
85
|
-
target: null,
|
|
86
|
-
down: false,
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
return newState;
|
|
90
|
-
});
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const handleDown = (e: MouseEvent) => {
|
|
94
|
-
const { target, pageX, pageY } = e;
|
|
95
|
-
|
|
96
|
-
set((state: GestureHandlersState) => {
|
|
97
|
-
const newState = {
|
|
98
|
-
...state,
|
|
99
|
-
target,
|
|
100
|
-
x: pageX,
|
|
101
|
-
xDelta: 0,
|
|
102
|
-
xDeltaPercent: 0,
|
|
103
|
-
xVelocity: 0,
|
|
104
|
-
xInitial: pageX,
|
|
105
|
-
xPrev: pageX,
|
|
106
|
-
y: pageY,
|
|
107
|
-
yDelta: 0,
|
|
108
|
-
yDeltaPercent: 0,
|
|
109
|
-
yVelocity: 0,
|
|
110
|
-
yInitial: pageY,
|
|
111
|
-
yPrev: pageY,
|
|
112
|
-
startTime: Date.now(),
|
|
113
|
-
down: true,
|
|
114
|
-
scrollLocked: false,
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
return newState;
|
|
118
|
-
});
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
function calcVelocity(
|
|
122
|
-
deltaSpace: number,
|
|
123
|
-
deltaTime: number,
|
|
124
|
-
prevVelocity: number
|
|
125
|
-
) {
|
|
126
|
-
if (deltaTime < 1) {
|
|
127
|
-
deltaTime = 1;
|
|
128
|
-
}
|
|
129
|
-
const speed = deltaSpace / deltaTime;
|
|
130
|
-
const depr = 0.5 + Math.min(deltaTime / VELOCITY_DEPR_FACTOR, 0.5);
|
|
131
|
-
return speed * depr + prevVelocity * (1 - depr);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function handleMove(e: MouseEvent) {
|
|
135
|
-
const { pageX, pageY } = e;
|
|
136
|
-
if (e.cancelable) {
|
|
137
|
-
// prevent drag & drop behaviour from browser
|
|
138
|
-
e.preventDefault && e.preventDefault();
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
set((state: GestureHandlersState) => {
|
|
142
|
-
function getDeltaSum(
|
|
143
|
-
currentPos: number,
|
|
144
|
-
initialPos: number,
|
|
145
|
-
prevPos: number
|
|
146
|
-
): number {
|
|
147
|
-
if (
|
|
148
|
-
state.scrollLocked ||
|
|
149
|
-
Math.abs(currentPos - initialPos) >= minTouchDelta
|
|
150
|
-
) {
|
|
151
|
-
state.scrollLocked = true;
|
|
152
|
-
return currentPos - prevPos;
|
|
153
|
-
}
|
|
154
|
-
return 0;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const target =
|
|
158
|
-
(containerRef && containerRef.current) || (e as any).target;
|
|
159
|
-
|
|
160
|
-
const deltaTime = Date.now() - state.startTime;
|
|
161
|
-
|
|
162
|
-
const width = target ? target.offsetWidth : NaN;
|
|
163
|
-
const xDelta = state.xDelta + getDeltaSum(pageX, state.xInitial, state.x);
|
|
164
|
-
const xDeltaPercent = (xDelta * 100) / width;
|
|
165
|
-
const xVelocity = calcVelocity(xDelta, deltaTime, state.xVelocity);
|
|
166
|
-
|
|
167
|
-
const height = target ? target.offsetHeight : NaN;
|
|
168
|
-
const yDelta = state.yDelta + getDeltaSum(pageY, state.yInitial, state.y);
|
|
169
|
-
const yDeltaPercent = (yDelta * 100) / height;
|
|
170
|
-
const yVelocity = calcVelocity(yDelta, deltaTime, state.yVelocity);
|
|
171
|
-
|
|
172
|
-
const newState = {
|
|
173
|
-
...state,
|
|
174
|
-
xDelta,
|
|
175
|
-
xDeltaPercent,
|
|
176
|
-
x: pageX,
|
|
177
|
-
xPrev: state.x,
|
|
178
|
-
xVelocity,
|
|
179
|
-
yDelta,
|
|
180
|
-
yDeltaPercent,
|
|
181
|
-
y: pageY,
|
|
182
|
-
yPrev: state.y,
|
|
183
|
-
yVelocity,
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
return newState;
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// Touch handlers
|
|
191
|
-
|
|
192
|
-
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
193
|
-
function handleTouchMove(e: TouchEvent) {
|
|
194
|
-
if (e.cancelable) {
|
|
195
|
-
// prevent drag & drop behaviour from browser
|
|
196
|
-
e.preventDefault();
|
|
197
|
-
}
|
|
198
|
-
handleMove(e.touches.item(0) as any);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function handleTouchStart(e: ReactTouchEvent<HTMLElement>) {
|
|
202
|
-
// making sure we're not dragging the element when more than one finger press the screen
|
|
203
|
-
const { touches } = e;
|
|
204
|
-
if (touches.length > 1) {
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (
|
|
209
|
-
ensureTargetIsContainer &&
|
|
210
|
-
containerRef &&
|
|
211
|
-
e.target !== containerRef.current
|
|
212
|
-
) {
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
217
|
-
const ownerDocument = getOwnerDocument(e.currentTarget)!;
|
|
218
|
-
const ownerWindow = ownerDocument.defaultView || window;
|
|
219
|
-
ownerWindow.addEventListener('touchmove', handleTouchMove, {
|
|
220
|
-
passive: false,
|
|
221
|
-
});
|
|
222
|
-
ownerWindow.addEventListener('touchend', handleTouchEnd);
|
|
223
|
-
ownerWindow.addEventListener('touchcancel', handleTouchEnd);
|
|
224
|
-
handleDown(e.touches.item(0) as any);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function handleTouchEnd(this: Window) {
|
|
228
|
-
this.removeEventListener('touchmove', handleTouchMove);
|
|
229
|
-
this.removeEventListener('touchend', handleTouchEnd);
|
|
230
|
-
this.removeEventListener('touchcancel', handleTouchEnd);
|
|
231
|
-
handleUp();
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// Mouse handlers
|
|
235
|
-
function handleMouseDown(e: ReactMouseEvent<HTMLElement>) {
|
|
236
|
-
if (
|
|
237
|
-
ensureTargetIsContainer &&
|
|
238
|
-
containerRef &&
|
|
239
|
-
e.target !== containerRef.current
|
|
240
|
-
) {
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
245
|
-
const ownerDocument = getOwnerDocument(e.currentTarget)!;
|
|
246
|
-
const ownerWindow = ownerDocument.defaultView || window;
|
|
247
|
-
if (e.button === 0) {
|
|
248
|
-
ownerWindow.addEventListener('mousemove', handleMove);
|
|
249
|
-
ownerWindow.addEventListener('mouseup', handleMouseUp);
|
|
250
|
-
handleDown(e as any);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
function handleMouseUp(this: Window) {
|
|
255
|
-
this.removeEventListener('mousemove', handleMove);
|
|
256
|
-
this.removeEventListener('mouseup', handleMouseUp);
|
|
257
|
-
handleUp();
|
|
258
|
-
}
|
|
259
|
-
/* eslint-enable @typescript-eslint/no-use-before-define */
|
|
260
|
-
|
|
261
|
-
return {
|
|
262
|
-
onMouseDown: handleMouseDown,
|
|
263
|
-
onTouchStart: handleTouchStart,
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export const useGestureHandlers = (
|
|
268
|
-
containerRef: MutableRefObject<HTMLElement | null> | undefined,
|
|
269
|
-
onGesture: (e: GestureHandlersState) => void,
|
|
270
|
-
options: GestureHandlerOptions = {}
|
|
271
|
-
) => {
|
|
272
|
-
const state = useRef({ ...initialGestureHandlersState });
|
|
273
|
-
|
|
274
|
-
const set = (
|
|
275
|
-
cb: (prevState: GestureHandlersState) => GestureHandlersState
|
|
276
|
-
) => {
|
|
277
|
-
state.current = cb(state.current);
|
|
278
|
-
onGesture && onGesture(state.current);
|
|
279
|
-
|
|
280
|
-
return state.current;
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
const handlers = gestureHandlers(set, containerRef, options);
|
|
284
|
-
|
|
285
|
-
return { state: state.current, handlers };
|
|
286
|
-
};
|
|
1
|
+
import type {
|
|
2
|
+
MutableRefObject,
|
|
3
|
+
MouseEvent as ReactMouseEvent,
|
|
4
|
+
TouchEvent as ReactTouchEvent,
|
|
5
|
+
} from 'react';
|
|
6
|
+
import { useRef } from 'react';
|
|
7
|
+
|
|
8
|
+
import { getOwnerDocument } from '../utils';
|
|
9
|
+
|
|
10
|
+
export interface GestureHandlersState {
|
|
11
|
+
target: null | EventTarget;
|
|
12
|
+
x: number;
|
|
13
|
+
xDelta: number;
|
|
14
|
+
xDeltaPercent: number;
|
|
15
|
+
xInitial: number;
|
|
16
|
+
xPrev: number;
|
|
17
|
+
xVelocity: number;
|
|
18
|
+
y: number;
|
|
19
|
+
yDelta: number;
|
|
20
|
+
yDeltaPercent: number;
|
|
21
|
+
yInitial: number;
|
|
22
|
+
yPrev: number;
|
|
23
|
+
yVelocity: number;
|
|
24
|
+
startTime: number;
|
|
25
|
+
down: boolean;
|
|
26
|
+
scrollLocked: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type SetStateFunc<S> = (
|
|
30
|
+
state: (prevState: Readonly<S>) => S,
|
|
31
|
+
callback?: () => void
|
|
32
|
+
) => void;
|
|
33
|
+
|
|
34
|
+
export interface GestureHandlersReturn {
|
|
35
|
+
onMouseDown: (e: ReactMouseEvent<HTMLElement>) => void;
|
|
36
|
+
onTouchStart: (e: ReactTouchEvent<HTMLElement>) => void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface GestureHandlerOptions {
|
|
40
|
+
ensureTargetIsContainer?: boolean;
|
|
41
|
+
minTouchDelta?: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const initialGestureHandlersState: GestureHandlersState = {
|
|
45
|
+
target: null,
|
|
46
|
+
x: 0,
|
|
47
|
+
xDelta: 0,
|
|
48
|
+
xDeltaPercent: 0,
|
|
49
|
+
xInitial: 0,
|
|
50
|
+
xPrev: 0,
|
|
51
|
+
xVelocity: 0,
|
|
52
|
+
y: 0,
|
|
53
|
+
yDelta: 0,
|
|
54
|
+
yDeltaPercent: 0,
|
|
55
|
+
yInitial: 0,
|
|
56
|
+
yPrev: 0,
|
|
57
|
+
yVelocity: 0,
|
|
58
|
+
startTime: 0,
|
|
59
|
+
down: false,
|
|
60
|
+
scrollLocked: false,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const FRAMERATE_CONST = 1000 / 60; // 60 fps
|
|
64
|
+
const VELOCITY_DEPR_FACTOR = FRAMERATE_CONST * 2;
|
|
65
|
+
|
|
66
|
+
export function gestureHandlers(
|
|
67
|
+
set: SetStateFunc<GestureHandlersState>,
|
|
68
|
+
containerRef?: MutableRefObject<HTMLElement | null>,
|
|
69
|
+
options: GestureHandlerOptions = {}
|
|
70
|
+
): GestureHandlersReturn {
|
|
71
|
+
const { ensureTargetIsContainer = false, minTouchDelta = 0 } = options;
|
|
72
|
+
|
|
73
|
+
// Common handlers
|
|
74
|
+
const handleUp = () => {
|
|
75
|
+
set((state: GestureHandlersState) => {
|
|
76
|
+
const deltaTime = Date.now() - state.startTime;
|
|
77
|
+
const xDelta = state.x - state.xInitial;
|
|
78
|
+
const yDelta = state.y - state.yInitial;
|
|
79
|
+
const xVelocity = calcVelocity(xDelta, deltaTime, state.xVelocity);
|
|
80
|
+
const yVelocity = calcVelocity(yDelta, deltaTime, state.yVelocity);
|
|
81
|
+
const newState: GestureHandlersState = {
|
|
82
|
+
...state,
|
|
83
|
+
xVelocity,
|
|
84
|
+
yVelocity,
|
|
85
|
+
target: null,
|
|
86
|
+
down: false,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return newState;
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const handleDown = (e: MouseEvent) => {
|
|
94
|
+
const { target, pageX, pageY } = e;
|
|
95
|
+
|
|
96
|
+
set((state: GestureHandlersState) => {
|
|
97
|
+
const newState = {
|
|
98
|
+
...state,
|
|
99
|
+
target,
|
|
100
|
+
x: pageX,
|
|
101
|
+
xDelta: 0,
|
|
102
|
+
xDeltaPercent: 0,
|
|
103
|
+
xVelocity: 0,
|
|
104
|
+
xInitial: pageX,
|
|
105
|
+
xPrev: pageX,
|
|
106
|
+
y: pageY,
|
|
107
|
+
yDelta: 0,
|
|
108
|
+
yDeltaPercent: 0,
|
|
109
|
+
yVelocity: 0,
|
|
110
|
+
yInitial: pageY,
|
|
111
|
+
yPrev: pageY,
|
|
112
|
+
startTime: Date.now(),
|
|
113
|
+
down: true,
|
|
114
|
+
scrollLocked: false,
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
return newState;
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
function calcVelocity(
|
|
122
|
+
deltaSpace: number,
|
|
123
|
+
deltaTime: number,
|
|
124
|
+
prevVelocity: number
|
|
125
|
+
) {
|
|
126
|
+
if (deltaTime < 1) {
|
|
127
|
+
deltaTime = 1;
|
|
128
|
+
}
|
|
129
|
+
const speed = deltaSpace / deltaTime;
|
|
130
|
+
const depr = 0.5 + Math.min(deltaTime / VELOCITY_DEPR_FACTOR, 0.5);
|
|
131
|
+
return speed * depr + prevVelocity * (1 - depr);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function handleMove(e: MouseEvent) {
|
|
135
|
+
const { pageX, pageY } = e;
|
|
136
|
+
if (e.cancelable) {
|
|
137
|
+
// prevent drag & drop behaviour from browser
|
|
138
|
+
e.preventDefault && e.preventDefault();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
set((state: GestureHandlersState) => {
|
|
142
|
+
function getDeltaSum(
|
|
143
|
+
currentPos: number,
|
|
144
|
+
initialPos: number,
|
|
145
|
+
prevPos: number
|
|
146
|
+
): number {
|
|
147
|
+
if (
|
|
148
|
+
state.scrollLocked ||
|
|
149
|
+
Math.abs(currentPos - initialPos) >= minTouchDelta
|
|
150
|
+
) {
|
|
151
|
+
state.scrollLocked = true;
|
|
152
|
+
return currentPos - prevPos;
|
|
153
|
+
}
|
|
154
|
+
return 0;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const target =
|
|
158
|
+
(containerRef && containerRef.current) || (e as any).target;
|
|
159
|
+
|
|
160
|
+
const deltaTime = Date.now() - state.startTime;
|
|
161
|
+
|
|
162
|
+
const width = target ? target.offsetWidth : NaN;
|
|
163
|
+
const xDelta = state.xDelta + getDeltaSum(pageX, state.xInitial, state.x);
|
|
164
|
+
const xDeltaPercent = (xDelta * 100) / width;
|
|
165
|
+
const xVelocity = calcVelocity(xDelta, deltaTime, state.xVelocity);
|
|
166
|
+
|
|
167
|
+
const height = target ? target.offsetHeight : NaN;
|
|
168
|
+
const yDelta = state.yDelta + getDeltaSum(pageY, state.yInitial, state.y);
|
|
169
|
+
const yDeltaPercent = (yDelta * 100) / height;
|
|
170
|
+
const yVelocity = calcVelocity(yDelta, deltaTime, state.yVelocity);
|
|
171
|
+
|
|
172
|
+
const newState = {
|
|
173
|
+
...state,
|
|
174
|
+
xDelta,
|
|
175
|
+
xDeltaPercent,
|
|
176
|
+
x: pageX,
|
|
177
|
+
xPrev: state.x,
|
|
178
|
+
xVelocity,
|
|
179
|
+
yDelta,
|
|
180
|
+
yDeltaPercent,
|
|
181
|
+
y: pageY,
|
|
182
|
+
yPrev: state.y,
|
|
183
|
+
yVelocity,
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
return newState;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Touch handlers
|
|
191
|
+
|
|
192
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
193
|
+
function handleTouchMove(e: TouchEvent) {
|
|
194
|
+
if (e.cancelable) {
|
|
195
|
+
// prevent drag & drop behaviour from browser
|
|
196
|
+
e.preventDefault();
|
|
197
|
+
}
|
|
198
|
+
handleMove(e.touches.item(0) as any);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function handleTouchStart(e: ReactTouchEvent<HTMLElement>) {
|
|
202
|
+
// making sure we're not dragging the element when more than one finger press the screen
|
|
203
|
+
const { touches } = e;
|
|
204
|
+
if (touches.length > 1) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (
|
|
209
|
+
ensureTargetIsContainer &&
|
|
210
|
+
containerRef &&
|
|
211
|
+
e.target !== containerRef.current
|
|
212
|
+
) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
217
|
+
const ownerDocument = getOwnerDocument(e.currentTarget)!;
|
|
218
|
+
const ownerWindow = ownerDocument.defaultView || window;
|
|
219
|
+
ownerWindow.addEventListener('touchmove', handleTouchMove, {
|
|
220
|
+
passive: false,
|
|
221
|
+
});
|
|
222
|
+
ownerWindow.addEventListener('touchend', handleTouchEnd);
|
|
223
|
+
ownerWindow.addEventListener('touchcancel', handleTouchEnd);
|
|
224
|
+
handleDown(e.touches.item(0) as any);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function handleTouchEnd(this: Window) {
|
|
228
|
+
this.removeEventListener('touchmove', handleTouchMove);
|
|
229
|
+
this.removeEventListener('touchend', handleTouchEnd);
|
|
230
|
+
this.removeEventListener('touchcancel', handleTouchEnd);
|
|
231
|
+
handleUp();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Mouse handlers
|
|
235
|
+
function handleMouseDown(e: ReactMouseEvent<HTMLElement>) {
|
|
236
|
+
if (
|
|
237
|
+
ensureTargetIsContainer &&
|
|
238
|
+
containerRef &&
|
|
239
|
+
e.target !== containerRef.current
|
|
240
|
+
) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
245
|
+
const ownerDocument = getOwnerDocument(e.currentTarget)!;
|
|
246
|
+
const ownerWindow = ownerDocument.defaultView || window;
|
|
247
|
+
if (e.button === 0) {
|
|
248
|
+
ownerWindow.addEventListener('mousemove', handleMove);
|
|
249
|
+
ownerWindow.addEventListener('mouseup', handleMouseUp);
|
|
250
|
+
handleDown(e as any);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function handleMouseUp(this: Window) {
|
|
255
|
+
this.removeEventListener('mousemove', handleMove);
|
|
256
|
+
this.removeEventListener('mouseup', handleMouseUp);
|
|
257
|
+
handleUp();
|
|
258
|
+
}
|
|
259
|
+
/* eslint-enable @typescript-eslint/no-use-before-define */
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
onMouseDown: handleMouseDown,
|
|
263
|
+
onTouchStart: handleTouchStart,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export const useGestureHandlers = (
|
|
268
|
+
containerRef: MutableRefObject<HTMLElement | null> | undefined,
|
|
269
|
+
onGesture: (e: GestureHandlersState) => void,
|
|
270
|
+
options: GestureHandlerOptions = {}
|
|
271
|
+
) => {
|
|
272
|
+
const state = useRef({ ...initialGestureHandlersState });
|
|
273
|
+
|
|
274
|
+
const set = (
|
|
275
|
+
cb: (prevState: GestureHandlersState) => GestureHandlersState
|
|
276
|
+
) => {
|
|
277
|
+
state.current = cb(state.current);
|
|
278
|
+
onGesture && onGesture(state.current);
|
|
279
|
+
|
|
280
|
+
return state.current;
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const handlers = gestureHandlers(set, containerRef, options);
|
|
284
|
+
|
|
285
|
+
return { state: state.current, handlers };
|
|
286
|
+
};
|
package/src/hooks/useMeasure.ts
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import type { MutableRefObject } from 'react';
|
|
2
|
-
import { useRef, useState, useEffect } from 'react';
|
|
3
|
-
|
|
4
|
-
export function useMeasure(ref: MutableRefObject<HTMLElement | null>) {
|
|
5
|
-
const ro = useRef<ResizeObserver | null>(null);
|
|
6
|
-
const [bounds, setBounds] = useState({
|
|
7
|
-
left: 0,
|
|
8
|
-
top: 0,
|
|
9
|
-
width: 0,
|
|
10
|
-
height: 0,
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
useEffect(() => {
|
|
14
|
-
if (ro.current === null) {
|
|
15
|
-
ro.current = new ResizeObserver((entries: any) => {
|
|
16
|
-
const entry = entries[0];
|
|
17
|
-
setBounds(entry.contentRect);
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (ref.current) {
|
|
22
|
-
ro.current.observe(ref.current);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return () => {
|
|
26
|
-
if (ro.current) {
|
|
27
|
-
ro.current.disconnect();
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
}, [ref]);
|
|
31
|
-
|
|
32
|
-
return bounds;
|
|
33
|
-
}
|
|
1
|
+
import type { MutableRefObject } from 'react';
|
|
2
|
+
import { useRef, useState, useEffect } from 'react';
|
|
3
|
+
|
|
4
|
+
export function useMeasure(ref: MutableRefObject<HTMLElement | null>) {
|
|
5
|
+
const ro = useRef<ResizeObserver | null>(null);
|
|
6
|
+
const [bounds, setBounds] = useState({
|
|
7
|
+
left: 0,
|
|
8
|
+
top: 0,
|
|
9
|
+
width: 0,
|
|
10
|
+
height: 0,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (ro.current === null) {
|
|
15
|
+
ro.current = new ResizeObserver((entries: any) => {
|
|
16
|
+
const entry = entries[0];
|
|
17
|
+
setBounds(entry.contentRect);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (ref.current) {
|
|
22
|
+
ro.current.observe(ref.current);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return () => {
|
|
26
|
+
if (ro.current) {
|
|
27
|
+
ro.current.disconnect();
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}, [ref]);
|
|
31
|
+
|
|
32
|
+
return bounds;
|
|
33
|
+
}
|