|
1 |
| -# react-spring-bottom-sheet |
| 1 | +[](https://npm-stat.com/charts.html?package=react-spring-bottom-sheet) |
| 2 | +[](https://www.npmjs.com/package/react-spring-bottom-sheet) |
| 3 | +[![gzip size][gzip-badge]][unpkg-dist] |
| 4 | +[![size][size-badge]][unpkg-dist] |
| 5 | +[![module formats: cjs, es, and modern][module-formats-badge]][unpkg-dist] |
| 6 | +[](https://github.com/semantic-release/semantic-release) |
2 | 7 |
|
3 |
| - |
| 8 | + |
4 | 9 |
|
| 10 | +**react-spring-bottom-sheet** is built on top of **react-spring** and **react-use-gesture**. It busts the myth that accessibility and supporting keyboard navigation and screen readers are allegedly at odds with delightful, beautiful and highly animated UIs. Every animation and transition is implemented using CSS custom properties instead of manipulating them directly, allowing complete control over the experience from CSS alone. |
5 | 11 |
|
6 |
| -# Work in progress! |
| 12 | +# Install |
7 | 13 |
|
8 |
| -Hold off using this until `v2` is out, or you're gonna have a _bad time_! |
| 14 | +```bash |
| 15 | +npm i react-spring-bottom-sheet |
| 16 | +``` |
| 17 | + |
| 18 | +# [Demos](https://react-spring-bottom-sheet.cocody.dev/) |
| 19 | + |
| 20 | +## [Basic](https://react-spring-bottom-sheet.cocody.dev/fixtures/simple) |
| 21 | + |
| 22 | +> [View demo code](/pages/fixtures/simple.tsx#L43-L47) |
| 23 | +
|
| 24 | +MVP example, showing what you get by implementing `open`, `onDismiss` and a single **snap point** always set to `minHeight`. |
| 25 | + |
| 26 | +## [Snap points & overflow](https://react-spring-bottom-sheet.cocody.dev/fixtures/scrollable) |
| 27 | + |
| 28 | +> [View demo code](/pages/fixtures/scrollable.tsx#L85-L95) |
| 29 | +
|
| 30 | +A more elaborate example that showcases how snap points work. It also shows how it behaves if you want it to be open by default, and not closable. Notice how it responds if you resize the window, or scroll to the bottom and starts adjusting the height of the sheet without scrolling back up first. |
| 31 | + |
| 32 | +## [Sticky header & footer](https://react-spring-bottom-sheet.cocody.dev/fixtures/sticky) |
| 33 | + |
| 34 | +> [View demo code](/pages/fixtures/sticky.tsx#L40-L60) |
| 35 | +
|
| 36 | +If you provide either a `header` or `footer` prop you'll enable the special behavior seen in this example. And they're not just sticky positioned, both areas support touch gestures. |
| 37 | + |
| 38 | +## [Non-blocking overlay mode](https://react-spring-bottom-sheet.cocody.dev/fixtures/aside) |
| 39 | + |
| 40 | +> [View demo code](/pages/fixtures/aside.tsx#L41-L46) |
| 41 | +
|
| 42 | +In most cases you use a bottom sheet the same way you do with a dialog: you want it to overlay the page and block out distractions. But there are times when you want a bottom sheet but without it taking all the attention and overlaying the entire page. Providing `blocking={false}` helps this use case. By doing so you disable a couple of behaviors that are there for accessibility (focus-locking and more) that prevents a screen reader or a keyboard user from accidentally leaving the bottom sheet. |
| 43 | + |
| 44 | +# [Get started](/GET_STARTED.md) |
| 45 | + |
| 46 | +# API |
| 47 | + |
| 48 | +## props |
| 49 | + |
| 50 | +All props you provide, like `className`, `style` props or whatever else are spread onto the underlying `<animated.div>` instance, that you can style in your custom CSS using this selector: `[data-rsbs-root]`. |
| 51 | +Just note that the component is mounted in a `@reach/portal` at the bottom of `<body>`, and not in the DOM hierarchy you render it in. |
| 52 | + |
| 53 | +### open |
| 54 | + |
| 55 | +Type: `boolean` |
| 56 | + |
| 57 | +The only required prop. And it's controlled, so if you don't set this to `false` then it's not possible to close the bottom sheet. |
| 58 | + |
| 59 | +### onDismiss |
| 60 | + |
| 61 | +Type: `() => void` |
| 62 | + |
| 63 | +Called when the user do something that signal they want to dismiss the sheet: |
| 64 | + |
| 65 | +- hit the `esc` key. |
| 66 | +- tap on the backdrop. |
| 67 | +- swipes the sheet to the bottom of the viewport. |
| 68 | + |
| 69 | +### snapPoints |
| 70 | + |
| 71 | +Type: `(state) => number | number[]` |
| 72 | + |
| 73 | +This function should be pure as it's called often. You can choose to provide a single value or an array of values to customize the behavior. The `state` contains these values: |
| 74 | + |
| 75 | +- `headerHeight` – the current measured height of the `header`. |
| 76 | +- `footerHeight` – if a `footer` prop is provided then this is its height. |
| 77 | +- `height` – the current height of the sheet. |
| 78 | +- `minHeight` – the minimum height needed to avoid a scrollbar. If there's not enough height available to avoid it then this will be the same as `maxHeight`. |
| 79 | +- `maxHeight` – the maximum available height on the page, usually matches `window.innerHeight/100vh`. |
| 80 | + |
| 81 | +### defaultSnap |
| 82 | + |
| 83 | +Type: `number | (state) => number` |
| 84 | + |
| 85 | +Provide either a number, or a callback returning a number for the default position of the sheet when it opens. |
| 86 | +`state` use the same arguments as `snapPoints`, plus two more values: `snapPoints` and `lastSnap`. |
| 87 | + |
| 88 | +### header |
| 89 | + |
| 90 | +Type: `ReactNode` |
| 91 | + |
| 92 | +Supports the same value type as the `children` prop. |
| 93 | + |
| 94 | +### footer |
| 95 | + |
| 96 | +Type: `ReactNode` |
| 97 | + |
| 98 | +Supports the same value type as the `children` prop. |
| 99 | + |
| 100 | +### initialFocusRef |
| 101 | + |
| 102 | +Type: `React.Ref` |
| 103 | + |
| 104 | +A react ref to the element you want to get keyboard focus when opening. If not provided it's automatically selecting the first interactive element it finds. |
| 105 | + |
| 106 | +### blocking |
| 107 | + |
| 108 | +Type: `boolean` |
| 109 | + |
| 110 | +Enabled by default. Enables focus trapping of keyboard navigation, so you can't accidentally tab out of the bottom sheet and into the background. Also sets `aria-hidden` on the rest of the page to prevent Screen Readers from escaping as well. |
| 111 | + |
| 112 | +## ref |
| 113 | + |
| 114 | +Methods available when setting a `ref` on the sheet: |
| 115 | + |
| 116 | +```jsx |
| 117 | +export default function Example() { |
| 118 | + const sheetRef = React.useRef() |
| 119 | + return <BottomSheet open ref={sheetRef} /> |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +### snapTo |
| 124 | + |
| 125 | +Type: `(numberOrCallback: number | (state => number)) => void` |
| 126 | + |
| 127 | +Same signature as the `defaultSnap` prop, calling it will animate the sheet to the new snap point you return. You can either call it with a number, which is the height in px (it'll select the closest snap point that matches your value): `ref.current.snapTo(200)`. Or `ref.current.snapTo(({headerHeight, footerHeight, height, minHeight, maxHeight, snapPoints, lastSnap}) => Math.max(...snapPoints))`. |
9 | 128 |
|
10 | 129 | # Credits
|
11 | 130 |
|
12 | 131 | - Play icon used on frame overlays: https://fontawesome.com/icons/play-circle?style=regular
|
13 | 132 | - Phone frame used in logo: https://www.figma.com/community/file/896042888090872154/Mono-Devices-1.0
|
14 | 133 | - iPhone frame used to wrap examples: https://www.figma.com/community/file/858143367356468985/(Variants)-iOS-%26-iPadOS-14-UI-Kit-for-Figma
|
| 134 | + |
| 135 | +[gzip-badge]: http://img.badgesize.io/https://unpkg.com/react-spring-bottom-sheet/dist/index.es.js?compression=gzip&label=gzip%20size&style=flat-square |
| 136 | +[size-badge]: http://img.badgesize.io/https://unpkg.com/react-spring-bottom-sheet/dist/index.es.js?label=size&style=flat-square |
| 137 | +[unpkg-dist]: https://unpkg.com/react-spring-bottom-sheet/dist/ |
| 138 | +[module-formats-badge]: https://img.shields.io/badge/module%20formats-cjs%2C%20es%2C%20modern-green.svg?style=flat-square |
0 commit comments