@simoneggert/react-modal-sheet 5.4.3
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/LICENSE +21 -0
- package/README.md +994 -0
- package/dist/index.d.mts +188 -0
- package/dist/index.d.ts +188 -0
- package/dist/index.js +1511 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1502 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +87 -0
- package/src/SheetBackdrop.tsx +46 -0
- package/src/SheetContainer.tsx +53 -0
- package/src/SheetContent.tsx +117 -0
- package/src/SheetDragIndicator.tsx +52 -0
- package/src/SheetHeader.tsx +52 -0
- package/src/constants.ts +19 -0
- package/src/context.tsx +12 -0
- package/src/debug.ts +38 -0
- package/src/hooks/use-dimensions.ts +30 -0
- package/src/hooks/use-drag-constraints.ts +14 -0
- package/src/hooks/use-isomorphic-layout-effect.ts +5 -0
- package/src/hooks/use-keyboard-avoidance.ts +75 -0
- package/src/hooks/use-modal-effect.ts +161 -0
- package/src/hooks/use-prevent-scroll.ts +357 -0
- package/src/hooks/use-resize-observer.ts +31 -0
- package/src/hooks/use-safe-area-insets.ts +40 -0
- package/src/hooks/use-scroll-position.ts +142 -0
- package/src/hooks/use-sheet-state.ts +63 -0
- package/src/hooks/use-stable-callback.ts +18 -0
- package/src/hooks/use-virtual-keyboard.ts +269 -0
- package/src/index.tsx +41 -0
- package/src/sheet.tsx +414 -0
- package/src/snap.ts +242 -0
- package/src/styles.ts +110 -0
- package/src/types.tsx +154 -0
- package/src/utils.ts +116 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { EasingDefinition, motion, MotionProps, MotionValue } from 'motion/react';
|
|
2
|
+
import { ForwardRefExoticComponent, ReactNode, ComponentPropsWithoutRef, RefAttributes, FunctionComponent, HTMLAttributes, RefObject } from 'react';
|
|
3
|
+
|
|
4
|
+
type SheetDetent = 'default' | 'full' | 'content';
|
|
5
|
+
type CommonProps = {
|
|
6
|
+
className?: string;
|
|
7
|
+
unstyled?: boolean;
|
|
8
|
+
};
|
|
9
|
+
type MotionDivProps = ComponentPropsWithoutRef<typeof motion.div>;
|
|
10
|
+
type MotionCommonProps = Omit<MotionDivProps, 'initial' | 'animate' | 'exit'>;
|
|
11
|
+
interface SheetTweenConfig {
|
|
12
|
+
ease: EasingDefinition;
|
|
13
|
+
duration: number;
|
|
14
|
+
}
|
|
15
|
+
type SheetProps = {
|
|
16
|
+
unstyled?: boolean;
|
|
17
|
+
avoidKeyboard?: boolean;
|
|
18
|
+
children: ReactNode;
|
|
19
|
+
detent?: SheetDetent;
|
|
20
|
+
disableDismiss?: boolean;
|
|
21
|
+
disableDrag?: boolean;
|
|
22
|
+
disableScrollLocking?: boolean;
|
|
23
|
+
dragCloseThreshold?: number;
|
|
24
|
+
dragVelocityThreshold?: number;
|
|
25
|
+
initialSnap?: number;
|
|
26
|
+
isOpen: boolean;
|
|
27
|
+
modalEffectRootId?: string;
|
|
28
|
+
modalEffectThreshold?: number;
|
|
29
|
+
mountPoint?: Element;
|
|
30
|
+
prefersReducedMotion?: boolean;
|
|
31
|
+
snapPoints?: number[];
|
|
32
|
+
tweenConfig?: SheetTweenConfig;
|
|
33
|
+
onClose: () => void;
|
|
34
|
+
onCloseEnd?: () => void;
|
|
35
|
+
onCloseStart?: () => void;
|
|
36
|
+
onOpenEnd?: () => void;
|
|
37
|
+
onOpenStart?: () => void;
|
|
38
|
+
onSnap?: (index: number) => void;
|
|
39
|
+
} & MotionCommonProps;
|
|
40
|
+
type SheetContainerProps = MotionCommonProps & CommonProps & {
|
|
41
|
+
children: ReactNode;
|
|
42
|
+
};
|
|
43
|
+
type SheetHeaderProps = MotionCommonProps & CommonProps & {
|
|
44
|
+
children?: ReactNode;
|
|
45
|
+
disableDrag?: boolean;
|
|
46
|
+
};
|
|
47
|
+
type SheetContentProps = MotionCommonProps & CommonProps & {
|
|
48
|
+
disableDrag?: boolean | ((args: SheetStateInfo) => boolean);
|
|
49
|
+
disableScroll?: boolean | ((args: SheetStateInfo) => boolean);
|
|
50
|
+
scrollRef?: RefObject<HTMLDivElement | null>;
|
|
51
|
+
scrollClassName?: string;
|
|
52
|
+
scrollStyle?: MotionProps['style'];
|
|
53
|
+
};
|
|
54
|
+
type SheetBackdropProps = MotionDivProps & CommonProps;
|
|
55
|
+
type SheetDragIndicatorProps = HTMLAttributes<HTMLDivElement> & CommonProps;
|
|
56
|
+
type SheetStateInfo = {
|
|
57
|
+
scrollPosition?: 'top' | 'bottom' | 'middle';
|
|
58
|
+
currentSnap?: number;
|
|
59
|
+
};
|
|
60
|
+
type SheetSnapPoint = {
|
|
61
|
+
snapIndex: number;
|
|
62
|
+
snapValue: number;
|
|
63
|
+
snapValueY: number;
|
|
64
|
+
};
|
|
65
|
+
type SheetComponent = ForwardRefExoticComponent<SheetProps & RefAttributes<any>>;
|
|
66
|
+
type ContainerComponent = ForwardRefExoticComponent<SheetContainerProps & RefAttributes<any>>;
|
|
67
|
+
type HeaderComponent = ForwardRefExoticComponent<SheetHeaderProps & RefAttributes<any>>;
|
|
68
|
+
type BackdropComponent = ForwardRefExoticComponent<SheetBackdropProps & RefAttributes<any>>;
|
|
69
|
+
type ContentComponent = ForwardRefExoticComponent<SheetContentProps & RefAttributes<any>>;
|
|
70
|
+
interface SheetCompoundComponent {
|
|
71
|
+
Container: ContainerComponent;
|
|
72
|
+
Header: HeaderComponent;
|
|
73
|
+
DragIndicator: FunctionComponent<SheetDragIndicatorProps>;
|
|
74
|
+
Content: ContentComponent;
|
|
75
|
+
Backdrop: BackdropComponent;
|
|
76
|
+
}
|
|
77
|
+
type SheetCompound = SheetComponent & SheetCompoundComponent;
|
|
78
|
+
|
|
79
|
+
type UseScrollPositionOptions = {
|
|
80
|
+
/**
|
|
81
|
+
* Debounce delay in ms for scroll event handling.
|
|
82
|
+
* @default 32
|
|
83
|
+
*/
|
|
84
|
+
debounceDelay?: number;
|
|
85
|
+
/**
|
|
86
|
+
* Enable or disable the hook entirely.
|
|
87
|
+
* @default true
|
|
88
|
+
*/
|
|
89
|
+
isEnabled?: boolean;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Hook to track the scroll position of an element.
|
|
93
|
+
*
|
|
94
|
+
* The scroll position can be 'top', 'bottom', 'middle', or undefined if the content is not scrollable.
|
|
95
|
+
* The hook provides a `scrollRef` callback to assign to the scrollable element.
|
|
96
|
+
*
|
|
97
|
+
* Note that the scroll position is only updated when the user stops scrolling
|
|
98
|
+
* for a short moment (debounced). You can set `debounceDelay` to `0` to disable debouncing entirely.
|
|
99
|
+
*
|
|
100
|
+
* @param options Configuration options for the hook.
|
|
101
|
+
* @returns An object containing the `scrollRef` callback and the current `scrollPosition`.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```tsx
|
|
105
|
+
* import { useScrollPosition } from 'react-modal-sheet';
|
|
106
|
+
*
|
|
107
|
+
* function MyComponent() {
|
|
108
|
+
* const { scrollRef, scrollPosition } = useScrollPosition();
|
|
109
|
+
*
|
|
110
|
+
* return (
|
|
111
|
+
* <div>
|
|
112
|
+
* <p>Scroll position: {scrollPosition}</p>
|
|
113
|
+
* <div ref={scrollRef} style={{ overflowY: 'auto', maxHeight: '300px' }}>
|
|
114
|
+
* ...long content...
|
|
115
|
+
* </div>
|
|
116
|
+
* </div>
|
|
117
|
+
* );
|
|
118
|
+
* }
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
declare function useScrollPosition(options?: UseScrollPositionOptions): {
|
|
122
|
+
scrollRef: (element: HTMLElement | null) => void;
|
|
123
|
+
scrollPosition: "top" | "bottom" | "middle" | undefined;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
type UseVirtualKeyboardOptions = {
|
|
127
|
+
/**
|
|
128
|
+
* Ref to the container element to apply `keyboard-inset-height` CSS variable updates.
|
|
129
|
+
* @default document.documentElement
|
|
130
|
+
*/
|
|
131
|
+
containerRef?: RefObject<HTMLElement | null>;
|
|
132
|
+
/**
|
|
133
|
+
* Enable or disable the hook entirely.
|
|
134
|
+
* @default true
|
|
135
|
+
*/
|
|
136
|
+
isEnabled?: boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Minimum pixel height difference to consider the keyboard visible.
|
|
139
|
+
* @default 100
|
|
140
|
+
*/
|
|
141
|
+
visualViewportThreshold?: number;
|
|
142
|
+
/**
|
|
143
|
+
* Delay in ms for debouncing viewport changes.
|
|
144
|
+
* @default 100
|
|
145
|
+
*/
|
|
146
|
+
debounceDelay?: number;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* A hook that detects virtual keyboard visibility and height.
|
|
150
|
+
* It listens to focus events and visual viewport changes to determine
|
|
151
|
+
* if a text input is focused and the keyboard is likely visible.
|
|
152
|
+
*
|
|
153
|
+
* It also sets the `--keyboard-inset-height` CSS variable on the specified container
|
|
154
|
+
* (or `:root` by default) to allow for easy styling adjustments when the keyboard is open.
|
|
155
|
+
*
|
|
156
|
+
* @param options Configuration options for the hook.
|
|
157
|
+
* @returns An object containing `isKeyboardOpen` and `keyboardHeight`.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```tsx
|
|
161
|
+
* import { useVirtualKeyboard } from 'react-modal-sheet';
|
|
162
|
+
*
|
|
163
|
+
* function MyComponent() {
|
|
164
|
+
* const { isKeyboardOpen, keyboardHeight } = useVirtualKeyboard();
|
|
165
|
+
*
|
|
166
|
+
* return (
|
|
167
|
+
* <div>
|
|
168
|
+
* <p>Keyboard is {isKeyboardOpen ? 'open' : 'closed'}</p>
|
|
169
|
+
* <p>Keyboard height: {keyboardHeight}px</p>
|
|
170
|
+
* </div>
|
|
171
|
+
* );
|
|
172
|
+
* }
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
declare function useVirtualKeyboard(options?: UseVirtualKeyboardOptions): {
|
|
176
|
+
keyboardHeight: number;
|
|
177
|
+
isKeyboardOpen: boolean;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
interface SheetRef {
|
|
181
|
+
y: MotionValue<number>;
|
|
182
|
+
yInverted: MotionValue<number>;
|
|
183
|
+
height: number;
|
|
184
|
+
snapTo: (index: number) => Promise<void>;
|
|
185
|
+
}
|
|
186
|
+
declare const Sheet: SheetCompound;
|
|
187
|
+
|
|
188
|
+
export { Sheet, type SheetBackdropProps, type SheetContainerProps, type SheetContentProps, type SheetDetent, type SheetDragIndicatorProps, type SheetHeaderProps, type SheetProps, type SheetRef, type SheetSnapPoint, type SheetStateInfo, type SheetTweenConfig, useScrollPosition, useVirtualKeyboard };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { EasingDefinition, motion, MotionProps, MotionValue } from 'motion/react';
|
|
2
|
+
import { ForwardRefExoticComponent, ReactNode, ComponentPropsWithoutRef, RefAttributes, FunctionComponent, HTMLAttributes, RefObject } from 'react';
|
|
3
|
+
|
|
4
|
+
type SheetDetent = 'default' | 'full' | 'content';
|
|
5
|
+
type CommonProps = {
|
|
6
|
+
className?: string;
|
|
7
|
+
unstyled?: boolean;
|
|
8
|
+
};
|
|
9
|
+
type MotionDivProps = ComponentPropsWithoutRef<typeof motion.div>;
|
|
10
|
+
type MotionCommonProps = Omit<MotionDivProps, 'initial' | 'animate' | 'exit'>;
|
|
11
|
+
interface SheetTweenConfig {
|
|
12
|
+
ease: EasingDefinition;
|
|
13
|
+
duration: number;
|
|
14
|
+
}
|
|
15
|
+
type SheetProps = {
|
|
16
|
+
unstyled?: boolean;
|
|
17
|
+
avoidKeyboard?: boolean;
|
|
18
|
+
children: ReactNode;
|
|
19
|
+
detent?: SheetDetent;
|
|
20
|
+
disableDismiss?: boolean;
|
|
21
|
+
disableDrag?: boolean;
|
|
22
|
+
disableScrollLocking?: boolean;
|
|
23
|
+
dragCloseThreshold?: number;
|
|
24
|
+
dragVelocityThreshold?: number;
|
|
25
|
+
initialSnap?: number;
|
|
26
|
+
isOpen: boolean;
|
|
27
|
+
modalEffectRootId?: string;
|
|
28
|
+
modalEffectThreshold?: number;
|
|
29
|
+
mountPoint?: Element;
|
|
30
|
+
prefersReducedMotion?: boolean;
|
|
31
|
+
snapPoints?: number[];
|
|
32
|
+
tweenConfig?: SheetTweenConfig;
|
|
33
|
+
onClose: () => void;
|
|
34
|
+
onCloseEnd?: () => void;
|
|
35
|
+
onCloseStart?: () => void;
|
|
36
|
+
onOpenEnd?: () => void;
|
|
37
|
+
onOpenStart?: () => void;
|
|
38
|
+
onSnap?: (index: number) => void;
|
|
39
|
+
} & MotionCommonProps;
|
|
40
|
+
type SheetContainerProps = MotionCommonProps & CommonProps & {
|
|
41
|
+
children: ReactNode;
|
|
42
|
+
};
|
|
43
|
+
type SheetHeaderProps = MotionCommonProps & CommonProps & {
|
|
44
|
+
children?: ReactNode;
|
|
45
|
+
disableDrag?: boolean;
|
|
46
|
+
};
|
|
47
|
+
type SheetContentProps = MotionCommonProps & CommonProps & {
|
|
48
|
+
disableDrag?: boolean | ((args: SheetStateInfo) => boolean);
|
|
49
|
+
disableScroll?: boolean | ((args: SheetStateInfo) => boolean);
|
|
50
|
+
scrollRef?: RefObject<HTMLDivElement | null>;
|
|
51
|
+
scrollClassName?: string;
|
|
52
|
+
scrollStyle?: MotionProps['style'];
|
|
53
|
+
};
|
|
54
|
+
type SheetBackdropProps = MotionDivProps & CommonProps;
|
|
55
|
+
type SheetDragIndicatorProps = HTMLAttributes<HTMLDivElement> & CommonProps;
|
|
56
|
+
type SheetStateInfo = {
|
|
57
|
+
scrollPosition?: 'top' | 'bottom' | 'middle';
|
|
58
|
+
currentSnap?: number;
|
|
59
|
+
};
|
|
60
|
+
type SheetSnapPoint = {
|
|
61
|
+
snapIndex: number;
|
|
62
|
+
snapValue: number;
|
|
63
|
+
snapValueY: number;
|
|
64
|
+
};
|
|
65
|
+
type SheetComponent = ForwardRefExoticComponent<SheetProps & RefAttributes<any>>;
|
|
66
|
+
type ContainerComponent = ForwardRefExoticComponent<SheetContainerProps & RefAttributes<any>>;
|
|
67
|
+
type HeaderComponent = ForwardRefExoticComponent<SheetHeaderProps & RefAttributes<any>>;
|
|
68
|
+
type BackdropComponent = ForwardRefExoticComponent<SheetBackdropProps & RefAttributes<any>>;
|
|
69
|
+
type ContentComponent = ForwardRefExoticComponent<SheetContentProps & RefAttributes<any>>;
|
|
70
|
+
interface SheetCompoundComponent {
|
|
71
|
+
Container: ContainerComponent;
|
|
72
|
+
Header: HeaderComponent;
|
|
73
|
+
DragIndicator: FunctionComponent<SheetDragIndicatorProps>;
|
|
74
|
+
Content: ContentComponent;
|
|
75
|
+
Backdrop: BackdropComponent;
|
|
76
|
+
}
|
|
77
|
+
type SheetCompound = SheetComponent & SheetCompoundComponent;
|
|
78
|
+
|
|
79
|
+
type UseScrollPositionOptions = {
|
|
80
|
+
/**
|
|
81
|
+
* Debounce delay in ms for scroll event handling.
|
|
82
|
+
* @default 32
|
|
83
|
+
*/
|
|
84
|
+
debounceDelay?: number;
|
|
85
|
+
/**
|
|
86
|
+
* Enable or disable the hook entirely.
|
|
87
|
+
* @default true
|
|
88
|
+
*/
|
|
89
|
+
isEnabled?: boolean;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Hook to track the scroll position of an element.
|
|
93
|
+
*
|
|
94
|
+
* The scroll position can be 'top', 'bottom', 'middle', or undefined if the content is not scrollable.
|
|
95
|
+
* The hook provides a `scrollRef` callback to assign to the scrollable element.
|
|
96
|
+
*
|
|
97
|
+
* Note that the scroll position is only updated when the user stops scrolling
|
|
98
|
+
* for a short moment (debounced). You can set `debounceDelay` to `0` to disable debouncing entirely.
|
|
99
|
+
*
|
|
100
|
+
* @param options Configuration options for the hook.
|
|
101
|
+
* @returns An object containing the `scrollRef` callback and the current `scrollPosition`.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```tsx
|
|
105
|
+
* import { useScrollPosition } from 'react-modal-sheet';
|
|
106
|
+
*
|
|
107
|
+
* function MyComponent() {
|
|
108
|
+
* const { scrollRef, scrollPosition } = useScrollPosition();
|
|
109
|
+
*
|
|
110
|
+
* return (
|
|
111
|
+
* <div>
|
|
112
|
+
* <p>Scroll position: {scrollPosition}</p>
|
|
113
|
+
* <div ref={scrollRef} style={{ overflowY: 'auto', maxHeight: '300px' }}>
|
|
114
|
+
* ...long content...
|
|
115
|
+
* </div>
|
|
116
|
+
* </div>
|
|
117
|
+
* );
|
|
118
|
+
* }
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
declare function useScrollPosition(options?: UseScrollPositionOptions): {
|
|
122
|
+
scrollRef: (element: HTMLElement | null) => void;
|
|
123
|
+
scrollPosition: "top" | "bottom" | "middle" | undefined;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
type UseVirtualKeyboardOptions = {
|
|
127
|
+
/**
|
|
128
|
+
* Ref to the container element to apply `keyboard-inset-height` CSS variable updates.
|
|
129
|
+
* @default document.documentElement
|
|
130
|
+
*/
|
|
131
|
+
containerRef?: RefObject<HTMLElement | null>;
|
|
132
|
+
/**
|
|
133
|
+
* Enable or disable the hook entirely.
|
|
134
|
+
* @default true
|
|
135
|
+
*/
|
|
136
|
+
isEnabled?: boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Minimum pixel height difference to consider the keyboard visible.
|
|
139
|
+
* @default 100
|
|
140
|
+
*/
|
|
141
|
+
visualViewportThreshold?: number;
|
|
142
|
+
/**
|
|
143
|
+
* Delay in ms for debouncing viewport changes.
|
|
144
|
+
* @default 100
|
|
145
|
+
*/
|
|
146
|
+
debounceDelay?: number;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* A hook that detects virtual keyboard visibility and height.
|
|
150
|
+
* It listens to focus events and visual viewport changes to determine
|
|
151
|
+
* if a text input is focused and the keyboard is likely visible.
|
|
152
|
+
*
|
|
153
|
+
* It also sets the `--keyboard-inset-height` CSS variable on the specified container
|
|
154
|
+
* (or `:root` by default) to allow for easy styling adjustments when the keyboard is open.
|
|
155
|
+
*
|
|
156
|
+
* @param options Configuration options for the hook.
|
|
157
|
+
* @returns An object containing `isKeyboardOpen` and `keyboardHeight`.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```tsx
|
|
161
|
+
* import { useVirtualKeyboard } from 'react-modal-sheet';
|
|
162
|
+
*
|
|
163
|
+
* function MyComponent() {
|
|
164
|
+
* const { isKeyboardOpen, keyboardHeight } = useVirtualKeyboard();
|
|
165
|
+
*
|
|
166
|
+
* return (
|
|
167
|
+
* <div>
|
|
168
|
+
* <p>Keyboard is {isKeyboardOpen ? 'open' : 'closed'}</p>
|
|
169
|
+
* <p>Keyboard height: {keyboardHeight}px</p>
|
|
170
|
+
* </div>
|
|
171
|
+
* );
|
|
172
|
+
* }
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
declare function useVirtualKeyboard(options?: UseVirtualKeyboardOptions): {
|
|
176
|
+
keyboardHeight: number;
|
|
177
|
+
isKeyboardOpen: boolean;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
interface SheetRef {
|
|
181
|
+
y: MotionValue<number>;
|
|
182
|
+
yInverted: MotionValue<number>;
|
|
183
|
+
height: number;
|
|
184
|
+
snapTo: (index: number) => Promise<void>;
|
|
185
|
+
}
|
|
186
|
+
declare const Sheet: SheetCompound;
|
|
187
|
+
|
|
188
|
+
export { Sheet, type SheetBackdropProps, type SheetContainerProps, type SheetContentProps, type SheetDetent, type SheetDragIndicatorProps, type SheetHeaderProps, type SheetProps, type SheetRef, type SheetSnapPoint, type SheetStateInfo, type SheetTweenConfig, useScrollPosition, useVirtualKeyboard };
|