Skip to content

About

Stackable bottom sheets for React Native: one provider, one hook, card-stack animation, snap points, drag to dismiss, scroll hand-off.

Topics

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

rn-sheet-stack

npm

Stackable bottom sheets for React Native. One provider, one hook. Sheets stack like cards: each new sheet slides up and the one beneath scales back and dims. Snap points, drag to dismiss, scroll hand-off, keyboard lift, Android back button, reduced motion. Everything animates on the UI thread with Reanimated and Gesture Handler.

rn-sheet-stack demo

Demo recorded on the iOS Simulator from the example app, shown at 2x speed. Full video: docs/demo.mp4.

Install

npm install @asaddevworld/sheet-stack react-native-reanimated react-native-worklets react-native-gesture-handler react-native-safe-area-context

With Expo:

npx expo install @asaddevworld/sheet-stack react-native-reanimated react-native-worklets react-native-gesture-handler react-native-safe-area-context

Requires React Native 0.76 or newer, Reanimated 4, and Gesture Handler 3. Follow their installation guides once (Babel plugin for Worklets, GestureHandlerRootView at the root).

Setup

Wrap the app once:

import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { SheetProvider } from '@asaddevworld/sheet-stack';

export default function App() {
  return (
    <GestureHandlerRootView style={{ flex: 1 }}>
      <SafeAreaProvider>
        <SheetProvider>
          <Root />
        </SheetProvider>
      </SafeAreaProvider>
    </GestureHandlerRootView>
  );
}

Usage

import { useSheets, SheetScrollView } from '@asaddevworld/sheet-stack';

function PickCity() {
  const sheets = useSheets();

  return (
    <Button
      title="Choose"
      onPress={() =>
        sheets.push({
          snapPoints: ['45%', '92%'],
          render: ({ dismiss }) => (
            <SheetScrollView>
              {cities.map((city) => (
                <Row key={city} label={city} onPress={dismiss} />
              ))}
            </SheetScrollView>
          ),
        })
      }
    />
  );
}

Stack a confirmation on top of it from anywhere, including from inside the first sheet:

sheets.push({
  render: ({ dismiss }) => (
    <Confirm onYes={() => sheets.popAll()} onNo={dismiss} />
  ),
});

API

useSheets()

member type description
push(options) (SheetOptions) => SheetId Open a sheet on top of the stack.
pop() () => void Close the top sheet.
dismiss(id) (SheetId) => void Close a specific sheet.
popAll() () => void Close every sheet.
update(id, patch) (SheetId, Partial<SheetOptions>) => void Change the options of an open sheet, for example make it dismissible after a form is valid.
stack SheetId[] Ids of the open sheets, bottom to top. Reactive.

SheetOptions

option type default description
render (ctx) => ReactNode required Sheet content. ctx has id, index, isTop, dismiss().
snapPoints (number | \${n}%` | 'auto')[]` ['auto'] Heights the sheet can rest at. 'auto' fits the content.
initialSnap number 0 Index into snapPoints to open at.
dismissible boolean true Drag down, backdrop tap and Android back close the sheet.
backdropOpacity number 0.4 Dim behind the top sheet.
handle boolean true Show the grab handle.
keyboardBehavior 'padding' | 'none' 'padding' Lift the sheet with the keyboard.
onDismiss () => void Called after the close animation.
onSnap (index) => void Called when the sheet settles at a snap point.
style ViewStyle Sheet container style (background, radius).
stackScale number 0.94 Scale of the sheet beneath when a new one opens.
stackOffset number 12 Pixels the sheet beneath moves up when a new one opens.

useSheet()

Inside sheet content, returns the same { id, index, isTop, dismiss } as the render context. Handy for components nested deep in the sheet.

SheetScrollView

A ScrollView for sheet content. When the list is scrolled to the top and the user drags down, the drag goes to the sheet, so it collapses to the lower snap point or dismisses. Any other time it scrolls normally. Pass any ScrollView props.

Behaviour notes

  • Release position and velocity pick the nearest snap point. A fast downward fling or a release well below the lowest snap point dismisses, when dismissible.
  • Pulling above the top snap point rubber-bands. A non-dismissible sheet rubber-bands at its lowest point instead of closing.
  • Up to three sheets are drawn in the stack; deeper ones fade out and come back as the top ones close.
  • The hardware back button on Android closes the top sheet only.
  • When the OS reduce-motion setting is on, springs become short timed animations.
  • The top sheet is announced as modal to screen readers, and the backdrop is a "Close sheet" button.

Example app

yarn
yarn example ios

The example has four demos: Basics (snap points and scroll hand-off), Stack (three sheets deep), Form (keyboard lift, not dismissible until submitted), and Playground (tune scale, offset and backdrop live).

Credits

Inspired by the stacked sheet demos by Enzo Mangano at reactiive.io. Implemented from scratch for this package.

License

MIT © Asad Ali

About

Stackable bottom sheets for React Native: one provider, one hook, card-stack animation, snap points, drag to dismiss, scroll hand-off.

Topics

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages