react-modal-sheet 2.2.1 → 3.1.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/README.md CHANGED
@@ -17,8 +17,7 @@
17
17
  | ![](media/1.gif) | ![](media/2.gif) | ![](media/3.gif) | ![](media/4.gif) |
18
18
  | :--------------: | :--------------: | :--------------: | :--------------: |
19
19
 
20
-
21
- ## Installation
20
+ ## 📦 Installation
22
21
 
23
22
  ```sh
24
23
  npm install react-modal-sheet
@@ -38,10 +37,10 @@ The gestures and animations are handled by the excellent [Framer Motion](https:/
38
37
  npm install framer-motion
39
38
  ```
40
39
 
41
- ## Usage
40
+ ## 💻 Usage
42
41
 
43
42
  ```jsx
44
- import Sheet from 'react-modal-sheet';
43
+ import { Sheet } from 'react-modal-sheet';
45
44
  import { useState } from 'react';
46
45
 
47
46
  function Example() {
@@ -69,36 +68,36 @@ Since the final bottom sheet is composed from smaller building blocks (`Containe
69
68
 
70
69
  Also, by constructing the sheet from smaller pieces makes it easier to apply any necessary accessibility related properties to the right components without requiring the main sheet component to be aware of them. You can read more about accessibility in the [Accessibility](#Accessibility) section.
71
70
 
72
- ## Props
71
+ ## 🎛️ Props
73
72
 
74
- | Name | Required | Default | Description |
75
- | ---------------------- | -------- | ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
76
- | `children` | yes | | Use `Sheet.Container/Content/Header/Backdrop` to compose your bottom sheet. |
77
- | `isOpen` | yes | | Boolean that indicates whether the sheet is open or not. |
78
- | `onClose` | yes | | Callback fn that is called when the sheet is closed by the user. |
79
- | `disableDrag` | no | false | Disable drag for the whole sheet. |
73
+ | Name | Required | Default | Description |
74
+ | ---------------------- | -------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
75
+ | `children` | yes | | Use `Sheet.Container/Content/Header/Backdrop` to compose your bottom sheet. |
76
+ | `isOpen` | yes | | Boolean that indicates whether the sheet is open or not. |
77
+ | `onClose` | yes | | Callback fn that is called when the sheet is closed by the user. |
78
+ | `disableDrag` | no | false | Disable drag for the whole sheet. |
80
79
  | `disableScrollLocking` | no | false | Disable scroll locking for the `body` element while sheet is open. Can be useful if you face issues with input elements and the iOS virtual keyboard. See related [issue](https://github.com/Temzasse/react-modal-sheet/issues/135). |
81
- | `detent` | no | `'full-height'` | The [detent](https://developer.apple.com/design/human-interface-guidelines/components/presentation/sheets#ios-ipados) in which the sheet should be in when opened. Available values: `'full-height'` or `'content-height'`. |
82
- | `onOpenStart` | no | | Callback fn that is called when the sheet opening animation starts. |
83
- | `onOpenEnd` | no | | Callback fn that is called when the sheet opening animation is completed. |
84
- | `onCloseStart` | no | | Callback fn that is called when the sheet closing animation starts. |
85
- | `onCloseEnd` | no | | Callback fn that is called when the sheet closing animation is completed. |
86
- | `onSnap` | no | | Callback fn that is called with the current snap point index when the sheet snaps to a new snap point. Requires `snapPoints` prop. |
87
- | `snapPoints` | no | | Eg. `[-50, 0.5, 100, 0]` - where positive values are pixels from the bottom of the screen and negative from the top. Values between 0-1 represent percentages, eg. `0.5` means 50% of window height from the bottom of the sceen. |
88
- | `initialSnap` | no | 0 | Initial snap point when sheet is opened (index from `snapPoints`). |
89
- | `rootId` | no | | The id of the element where the main app is mounted, eg. "root". Enables iOS modal effect. |
90
- | `tweenConfig` | no | `{ ease: 'easeOut', duration: 0.2 }` | Overrides the config for the sheet [tween](https://www.framer.com/motion/transition/#tween) transition when the sheet is opened, closed, or snapped to a point. |
91
- | `mountPoint` | no | `document.body` | HTML element that should be used as the mount point for the sheet. |
92
- | `prefersReducedMotion` | no | false | Skip sheet animations (sheet instantly snaps to desired location). |
93
-
94
- ## Methods and properties
80
+ | `detent` | no | `'full-height'` | The [detent](https://developer.apple.com/design/human-interface-guidelines/components/presentation/sheets#ios-ipados) in which the sheet should be in when opened. Available values: `'full-height'` or `'content-height'`. |
81
+ | `onOpenStart` | no | | Callback fn that is called when the sheet opening animation starts. |
82
+ | `onOpenEnd` | no | | Callback fn that is called when the sheet opening animation is completed. |
83
+ | `onCloseStart` | no | | Callback fn that is called when the sheet closing animation starts. |
84
+ | `onCloseEnd` | no | | Callback fn that is called when the sheet closing animation is completed. |
85
+ | `onSnap` | no | | Callback fn that is called with the current snap point index when the sheet snaps to a new snap point. Requires `snapPoints` prop. |
86
+ | `snapPoints` | no | | Eg. `[-50, 0.5, 100, 0]` - where positive values are pixels from the bottom of the screen and negative from the top. Values between 0-1 represent percentages, eg. `0.5` means 50% of window height from the bottom of the sceen. |
87
+ | `initialSnap` | no | 0 | Initial snap point when sheet is opened (index from `snapPoints`). |
88
+ | `rootId` | no | | The id of the element where the main app is mounted, eg. "root". Enables iOS modal effect. |
89
+ | `tweenConfig` | no | `{ ease: 'easeOut', duration: 0.2 }` | Overrides the config for the sheet [tween](https://www.framer.com/motion/transition/#tween) transition when the sheet is opened, closed, or snapped to a point. |
90
+ | `mountPoint` | no | `document.body` | HTML element that should be used as the mount point for the sheet. |
91
+ | `prefersReducedMotion` | no | false | Skip sheet animations (sheet instantly snaps to desired location). |
92
+
93
+ ## ⚙️ Methods and properties
95
94
 
96
95
  ### `snapTo(index)`
97
96
 
98
97
  Imperative method that can be accessed via a ref for snapping to a snap point in given index.
99
98
 
100
99
  ```tsx
101
- import Sheet, { SheetRef } from 'react-modal-sheet';
100
+ import { Sheet, SheetRef } from 'react-modal-sheet';
102
101
  import { useState, useRef } from 'react';
103
102
 
104
103
  function Example() {
@@ -117,7 +116,7 @@ function Example() {
117
116
  onClose={() => setOpen(false)}
118
117
  snapPoints={[600, 400, 100, 0]}
119
118
  initialSnap={1}
120
- onSnap={snapIndex =>
119
+ onSnap={(snapIndex) =>
121
120
  console.log('> Current snap point index:', snapIndex)
122
121
  }
123
122
  >
@@ -144,7 +143,7 @@ Similarly to the `snapTo` method the `y` value can be accessed via a ref.
144
143
  The `y` value can be useful for certain situtation eg. when you want to combine snap points with scrollable sheet content and ensure that the content stays properly scrollable in any snap point. Below you can see a simplified example of this situation and for a more detailed example take a look at the [ScrollableSnapPoints](example/components/ScrollableSnapPoints.tsx) component in the example app.
145
144
 
146
145
  ```tsx
147
- import Sheet, { SheetRef } from 'react-modal-sheet';
146
+ import { Sheet, SheetRef } from 'react-modal-sheet';
148
147
  import { useState, useRef } from 'react';
149
148
 
150
149
  function Example() {
@@ -214,7 +213,7 @@ It is possible to use snap points with `detent="content-height"` **but** the sna
214
213
 
215
214
  > ℹ️ If you are wondering where the term `detent` comes from it's from Apple's [Human Interface Guidelines](https://developer.apple.com/design/human-interface-guidelines/components/presentation/sheets#ios-ipados).
216
215
 
217
- ## Compound Components
216
+ ## 🧩 Compound Components
218
217
 
219
218
  ### `Sheet`
220
219
 
@@ -268,13 +267,31 @@ Sheet scroller can be used to make the whole sheet content or parts of it scroll
268
267
 
269
268
  Sheet backdrop is a translucent overlay that helps to separate the sheet from it's background. By default the backdrop doesn't have any interaction attached to it but if you, for example, want to close the sheet when the backdrop is clicked you can provide tap handler to it which will change the rendered element from `div` to `button`.
270
269
 
271
- ⚠️ **Note:** as the element is a motion component you need to use [`onTap`](https://www.framer.com/motion/gestures/#tap) instead of `onClick` if you want to add a click handler to it.
270
+ **⚠️ Note:** as the element is a motion component you need to use [`onTap`](https://www.framer.com/motion/gestures/#tap) instead of `onClick` if you want to add a click handler to it.
272
271
 
273
272
  > 🖥 Rendered element: `motion.div` or `motion.button`.
274
273
 
275
- ## Advanced usage
274
+ ## ✨ Advanced usage
275
+
276
+ ### ⌨️ Virtual keyboard avoidance
277
+
278
+ A common problem with React Modal Sheet is that since the sheet is rendered as a fixed positioned element it doesn't move when the virtual keyboard is opened which can lead to the keyboard covering the input elements.
279
+
280
+ Handling the virtual keyboard on mobile devices can be quite tricky. It doesn't help that the Web doesn't yet have widely supported APIs for working with the virtual keyboard.
281
+
282
+ **⚠️ Note: This library does not provide any official built-in solution for keyboard avoidance!**
283
+
284
+ This is because it is not possible for the library to know how your sheet content is structured and how the sheet should behave when the keyboard is opened.
285
+
286
+ However, there are some strategies that you can use to handle the virtual keyboard:
276
287
 
277
- ### Scrolling on touch devices
288
+ 1. Design your sheet content in a way that the input elements are positioned at the top of the sheet so that they are not covered by the keyboard. This is the easiest solution and if you look at popular mobile apps you can see that they often have input elements at the top of the screen exactly for this reason.
289
+
290
+ 2. Listen to changes in the `visualViewport`, see [Visual Viewport API](https://developer.mozilla.org/en-US/docs/Web/API/Visual_Viewport_API), and change styles like padding on the sheet content to move the input up when the keyboard is opened. See the `AvoidKeyboard` component [code](example/components/AvoidKeyboard.tsx) and [demo](https://temzasse.github.io/react-modal-sheet/#/avoid-keyboard) in the example app for a simple example of this. The example is based on a custom `useAnimatedVirtualKeyboard` hook which you can copy to your project at your own risk. **Note that at least on iOS there is a noticeable delay when the keyboard is opened and when the `resize` event is fired for the `visualViewport`**.
291
+
292
+ 3. Use the experimental [VirtualKeyboard API](https://developer.mozilla.org/en-US/docs/Web/API/VirtualKeyboard_API) to detect when the virtual keyboard is opened and adjust the sheet content accordingly. **Note that this API is not yet widely supported in browsers, but it will most likely produce the best result.**
293
+
294
+ ### 📱 Scrolling on touch devices
278
295
 
279
296
  Scrolling and dragging are **the same** gesture on touch devices which can create problems when you want to have scrollable content inside the sheet. React Modal Sheet provides a `Sheet.Scroller` component that is able to automatically disable and enable dragging inside the `Sheet.Content` component based on the scroll state. There are three modes for the `Sheet.Scroller` reflected in the `draggableAt` prop:
280
297
 
@@ -302,14 +319,13 @@ function ScrollableExample() {
302
319
  }
303
320
  ```
304
321
 
305
- ### iOS Modal View effect
322
+ ### 🪟 iOS Modal View effect
306
323
 
307
324
  In addition to the `Sheet.Backdrop` it's possible to apply a scaling effect to the main app element to highlight the modality of the bottom sheet. This effect mimics the [iOS Modal View](https://developer.apple.com/design/human-interface-guidelines/ios/app-architecture/modality/) presentation style to bring more focus to the sheet and add some delight to the user experience.
308
325
 
309
326
  | ![](media/3.gif) | ![](media/4.gif) |
310
327
  | :--------------: | :--------------: |
311
328
 
312
-
313
329
  To enable this effect you can provide the id of the root element where your application is mounted:
314
330
 
315
331
  ```jsx
@@ -320,7 +336,7 @@ function Example() {
320
336
 
321
337
  > ⚠️ **Limitations**: Since the effect is applied to the root element it will NOT work as desired if the HTML body element is scrolled down at all. One way to avoid this is to use something like `height: 100vh;` and `overflow: auto;` on the root element to make it fill the whole screen and be scrollable instead of the body element.
322
338
 
323
- ## Customization
339
+ ## 🎨 Customization
324
340
 
325
341
  The default styles for the `Sheet` component somewhat follows the styles of the previously mentioned iOS Modal View. However, if these default styles are not to your liking it's easy to make changes to them: you can provide a custom header or you can overwrite any style with CSS via the exposed class names.
326
342
 
@@ -371,8 +387,8 @@ You can add your own styles or override the default sheet styles via the exposed
371
387
  #### CSS-in-JS
372
388
 
373
389
  ```jsx
374
- import Sheet from 'react-modal-sheet';
375
- import styled from 'styled-components';
390
+ import { Sheet } from 'react-modal-sheet';
391
+ import { styled } from 'styled-components';
376
392
  import { useState } from 'react';
377
393
 
378
394
  const CustomSheet = styled(Sheet)`
@@ -412,7 +428,7 @@ function Example() {
412
428
  }
413
429
  ```
414
430
 
415
- ## Accessibility
431
+ ## ♿️ Accessibility
416
432
 
417
433
  By default, react-modal-sheet doesn't include any built-in accessibility properties in order to not make any assumptions and to support a wide range of use cases. Additionally, not including 3rd party libraries for features like focus trapping or screen reader support makes it possible to utilize any accessibility libraries that your project may already use, eg. [React Aria](https://react-spectrum.adobe.com/react-aria/getting-started.html). This also helps to reduce JS bloat by not including similar libraries multiple times in your app bundle.
418
434
 
@@ -421,7 +437,7 @@ The example below utilizes React Aria to achieve an accessible modal-like bottom
421
437
  > ℹ️ The example was built by following the React Aria's [useDialog](https://react-spectrum.adobe.com/react-aria/useDialog.html) documentation.
422
438
 
423
439
  ```jsx
424
- import Sheet from 'react-modal-sheet';
440
+ import { Sheet } from 'react-modal-sheet';
425
441
  import { useRef } from 'react';
426
442
  import { useOverlayTriggerState } from 'react-stately';
427
443
 
@@ -434,7 +450,7 @@ import {
434
450
  useDialog,
435
451
  } from 'react-aria';
436
452
 
437
- const A11yExample = () => {
453
+ function A11yExample() {
438
454
  const sheetState = useOverlayTriggerState({});
439
455
  const openButtonRef = useRef(null);
440
456
  const openButton = useButton({ onPress: sheetState.open }, openButtonRef);
@@ -454,9 +470,9 @@ const A11yExample = () => {
454
470
  </Sheet>
455
471
  </div>
456
472
  );
457
- };
473
+ }
458
474
 
459
- const SheetComp = ({ sheetState }) => {
475
+ function SheetComp({ sheetState }) {
460
476
  const containerRef = useRef(null);
461
477
  const dialog = useDialog({}, containerRef);
462
478
  const overlay = useOverlay(
@@ -493,11 +509,11 @@ const SheetComp = ({ sheetState }) => {
493
509
  <Sheet.Backdrop />
494
510
  </>
495
511
  );
496
- };
512
+ }
497
513
  ```
498
514
 
499
515
  If you want to see a more real-world-like implementation you can take a look at the [Slack example](example/components/slack-message/index.tsx) and try out the related [demo](https://temzasse.github.io/react-modal-sheet/#/slack-message) (optimized for mobile).
500
516
 
501
- ### Building a reusable sheet
517
+ ### 🔩 Building a reusable sheet
502
518
 
503
519
  In your projects it might make sense to build a reusable bottom sheet that has all the accessibility features included and can then be easily used in various places in the project. Take a look at the [A11ySheet](example/components/a11y/A11ySheet.tsx) example to get some insight on how to build such a component. By incorporating all the accessibility features inside your own reusable component you don't need to repeat them every time you want to use a bottom sheet in your app.
@@ -0,0 +1,64 @@
1
+ import { motion, EasingDefinition, MotionProps, MotionValue } from 'framer-motion';
2
+ import { ForwardRefExoticComponent, RefAttributes, ReactNode, ComponentPropsWithoutRef, HTMLAttributes } from 'react';
3
+
4
+ interface SheetEvents {
5
+ onOpenStart?: () => void;
6
+ onOpenEnd?: () => void;
7
+ onCloseStart?: () => void;
8
+ onCloseEnd?: () => void;
9
+ onSnap?: (index: number) => void;
10
+ }
11
+ type SheetDetent = 'full-height' | 'content-height';
12
+ type CommonProps = MotionProps & {
13
+ className?: string;
14
+ };
15
+ interface SheetTweenConfig {
16
+ ease: EasingDefinition;
17
+ duration: number;
18
+ }
19
+ type SheetProps = {
20
+ isOpen: boolean;
21
+ children: ReactNode;
22
+ onClose: () => void;
23
+ rootId?: string;
24
+ mountPoint?: Element;
25
+ snapPoints?: number[];
26
+ detent?: SheetDetent;
27
+ initialSnap?: number;
28
+ tweenConfig?: SheetTweenConfig;
29
+ disableDrag?: boolean;
30
+ disableScrollLocking?: boolean;
31
+ prefersReducedMotion?: boolean;
32
+ } & SheetEvents & ComponentPropsWithoutRef<typeof motion.div>;
33
+ type SheetContainerProps = Omit<CommonProps, 'initial' | 'animate' | 'exit' | 'onAnimationComplete'> & {
34
+ children: ReactNode;
35
+ };
36
+ type SheetDraggableProps = Omit<CommonProps, 'drag' | 'dragElastic' | 'dragConstraints' | 'dragMomentum' | 'onDrag' | 'onDragStart' | 'onDragEnd'> & {
37
+ children?: ReactNode;
38
+ disableDrag?: boolean;
39
+ };
40
+ type SheetBackdropProps = Omit<CommonProps, 'initial' | 'animate' | 'exit'>;
41
+ type SheetScrollerProps = HTMLAttributes<HTMLDivElement> & {
42
+ draggableAt?: 'top' | 'bottom' | 'both';
43
+ };
44
+ type ContainerComponent = ForwardRefExoticComponent<SheetContainerProps & RefAttributes<any>>;
45
+ type DraggableComponent = ForwardRefExoticComponent<SheetDraggableProps & RefAttributes<any>>;
46
+ type BackdropComponent = ForwardRefExoticComponent<SheetBackdropProps & RefAttributes<any>>;
47
+ type SheetComponent = ForwardRefExoticComponent<SheetProps & RefAttributes<any>>;
48
+ type ScrollerComponent = ForwardRefExoticComponent<SheetScrollerProps & RefAttributes<any>>;
49
+ interface SheetCompoundComponent {
50
+ Container: ContainerComponent;
51
+ Header: DraggableComponent;
52
+ Content: DraggableComponent;
53
+ Backdrop: BackdropComponent;
54
+ Scroller: ScrollerComponent;
55
+ }
56
+ type SheetCompound = SheetComponent & SheetCompoundComponent;
57
+
58
+ interface SheetRef {
59
+ y: MotionValue<number>;
60
+ snapTo: (index: number) => void;
61
+ }
62
+ declare const Sheet: SheetCompound;
63
+
64
+ export { Sheet, type SheetRef };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,64 @@
1
- import { MotionValue } from 'framer-motion';
2
- import { SheetCompound } from './types';
3
- export declare type SheetRef = {
4
- y: MotionValue<number>;
5
- snapTo: (index: number) => void;
6
- };
7
- declare const _default: SheetCompound;
8
- export default _default;
1
+ import { motion, EasingDefinition, MotionProps, MotionValue } from 'framer-motion';
2
+ import { ForwardRefExoticComponent, RefAttributes, ReactNode, ComponentPropsWithoutRef, HTMLAttributes } from 'react';
3
+
4
+ interface SheetEvents {
5
+ onOpenStart?: () => void;
6
+ onOpenEnd?: () => void;
7
+ onCloseStart?: () => void;
8
+ onCloseEnd?: () => void;
9
+ onSnap?: (index: number) => void;
10
+ }
11
+ type SheetDetent = 'full-height' | 'content-height';
12
+ type CommonProps = MotionProps & {
13
+ className?: string;
14
+ };
15
+ interface SheetTweenConfig {
16
+ ease: EasingDefinition;
17
+ duration: number;
18
+ }
19
+ type SheetProps = {
20
+ isOpen: boolean;
21
+ children: ReactNode;
22
+ onClose: () => void;
23
+ rootId?: string;
24
+ mountPoint?: Element;
25
+ snapPoints?: number[];
26
+ detent?: SheetDetent;
27
+ initialSnap?: number;
28
+ tweenConfig?: SheetTweenConfig;
29
+ disableDrag?: boolean;
30
+ disableScrollLocking?: boolean;
31
+ prefersReducedMotion?: boolean;
32
+ } & SheetEvents & ComponentPropsWithoutRef<typeof motion.div>;
33
+ type SheetContainerProps = Omit<CommonProps, 'initial' | 'animate' | 'exit' | 'onAnimationComplete'> & {
34
+ children: ReactNode;
35
+ };
36
+ type SheetDraggableProps = Omit<CommonProps, 'drag' | 'dragElastic' | 'dragConstraints' | 'dragMomentum' | 'onDrag' | 'onDragStart' | 'onDragEnd'> & {
37
+ children?: ReactNode;
38
+ disableDrag?: boolean;
39
+ };
40
+ type SheetBackdropProps = Omit<CommonProps, 'initial' | 'animate' | 'exit'>;
41
+ type SheetScrollerProps = HTMLAttributes<HTMLDivElement> & {
42
+ draggableAt?: 'top' | 'bottom' | 'both';
43
+ };
44
+ type ContainerComponent = ForwardRefExoticComponent<SheetContainerProps & RefAttributes<any>>;
45
+ type DraggableComponent = ForwardRefExoticComponent<SheetDraggableProps & RefAttributes<any>>;
46
+ type BackdropComponent = ForwardRefExoticComponent<SheetBackdropProps & RefAttributes<any>>;
47
+ type SheetComponent = ForwardRefExoticComponent<SheetProps & RefAttributes<any>>;
48
+ type ScrollerComponent = ForwardRefExoticComponent<SheetScrollerProps & RefAttributes<any>>;
49
+ interface SheetCompoundComponent {
50
+ Container: ContainerComponent;
51
+ Header: DraggableComponent;
52
+ Content: DraggableComponent;
53
+ Backdrop: BackdropComponent;
54
+ Scroller: ScrollerComponent;
55
+ }
56
+ type SheetCompound = SheetComponent & SheetCompoundComponent;
57
+
58
+ interface SheetRef {
59
+ y: MotionValue<number>;
60
+ snapTo: (index: number) => void;
61
+ }
62
+ declare const Sheet: SheetCompound;
63
+
64
+ export { Sheet, type SheetRef };