### Run Example App Dev Server Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Navigate to the example directory and start the development server for the example app. This should be done in a separate terminal from the library watcher. ```sh cd example npm run dev ``` -------------------------------- ### Run Example App with Host Binding Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md To test the example app on your phone, run the example app with `npm run dev:host`. Then, open your computer's local IP address in your phone's browser. Ensure your phone is on the same network as your computer. ```sh npm run dev:host ``` -------------------------------- ### Install react-modal-sheet Source: https://context7.com/temzasse/react-modal-sheet/llms.txt Install the react-modal-sheet and motion libraries using npm. ```bash npm install react-modal-sheet motion ``` -------------------------------- ### Install react-modal-sheet Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Install the react-modal-sheet package using npm. ```sh npm install react-modal-sheet ``` -------------------------------- ### Install yalc Globally Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Install yalc globally to manage local package dependencies. This is a prerequisite for the local development setup. ```sh npm i yalc -g ``` -------------------------------- ### Install Dependencies and Configure Yalc Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Install project dependencies, build the library, and configure yalc for local linking. Run these commands in the project root. ```sh npm install npm run build npm run link ``` -------------------------------- ### Start Library Watcher Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Start a watcher process to continuously build the library as you make changes. This command should be run in the project root. ```sh npm run dev ``` -------------------------------- ### Install Motion Peer Dependency Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Install the Motion library, a peer dependency for gestures and animations, using npm. ```sh npm install motion ``` -------------------------------- ### Imperative snapTo Method Example Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Demonstrates how to use the imperative `snapTo` method on a `SheetRef` to programmatically control the sheet's position to a specified snap point index. Requires initializing the sheet with `snapPoints` and a `ref`. ```tsx import { Sheet, type SheetRef } from 'react-modal-sheet'; import { useState, useRef } from 'react'; const snapPoints = [0, 0.5, 1]; function SnapExample() { const [isOpen, setOpen] = useState(false); const sheetRef = useRef(null); const snapTo = (i: number) => sheetRef.current?.snapTo(i); return ( <> {/* Opens to 50% since initial index is 1 * /} setOpen(false)} initialSnap={1} snapPoints={snapPoints} onSnap={(snapIndex) => console.log('Current snap point index:', snapIndex) } > ); } ``` -------------------------------- ### Implement Custom Scroller with Utility Hooks Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Create a custom scrollable area by disabling default scrolling and using `useScrollPosition` and `useVirtualKeyboard` hooks. This example demonstrates managing scroll position and keyboard inset for a custom scroller. ```tsx import { Sheet, useScrollPosition, useVirtualKeyboard, } from 'react-modal-sheet'; function CustomScrollerExample() { const [isOpen, setOpen] = useState(false); const { scrollRef, scrollPosition } = useScrollPosition({ isEnabled: isOpen, }); const { keyboardHeight } = useVirtualKeyboard({ isEnabled: isOpen, }); /** * If you use `var(--keyboard-inset-height)` CSS variable you can just call * `useVirtualKeyboard()` without destructuring anything from it: * * useVirtualKeyboard({ isEnabled: isOpen }); */ return ( // Disable default keyboard avoidance setOpen(false)}>
Some content here...
Long content here...
More content here...
); } ``` -------------------------------- ### Accessible Modal Sheet with React Aria Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md This example demonstrates integrating react-modal-sheet with React Aria hooks (useOverlay, useModal, useDialog, useButton) to create an accessible modal bottom sheet. It includes focus management and screen reader support. Ensure React Aria is installed and configured in your project. ```tsx import { Sheet } from 'react-modal-sheet'; import { useRef } from 'react'; import { useOverlayTriggerState } from 'react-stately'; import { useOverlay, useModal, OverlayProvider, FocusScope, useButton, useDialog, } from 'react-aria'; function A11yExample() { const sheetState = useOverlayTriggerState({}); const openButtonRef = useRef(null); const openButton = useButton({ onPress: sheetState.open }, openButtonRef); return (
); } function SheetComp({ sheetState }) { const containerRef = useRef(null); const dialog = useDialog({}, containerRef); const overlay = useOverlay( { onClose: sheetState.close, isOpen: true, isDismissable: true }, containerRef ); const closeButtonRef = useRef(null); const closeButton = useButton( { onPress: sheetState.close, 'aria-label': 'Close sheet' }, closeButtonRef ); useModal(); // In real world usage this would be a separate React component const customHeader = (
Some title for sheet
); return ( <> {customHeader} {/*...*/} ); } ``` -------------------------------- ### Custom Sheet Header Example Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Create a custom header for the sheet by providing children to Sheet.Header. Includes a Sheet.DragIndicator for drag functionality. ```tsx function CustomHeaderExample() { return (

Custom Header

Sheet content goes here...

); } ``` -------------------------------- ### Ensure Content Reachability with Padding Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md This example demonstrates how to add bottom padding to sheet content based on its vertical position. This ensures that content is always reachable, especially when the sheet is not fully open. It uses the `y` motion value from the sheet's context. ```tsx import { Sheet, type SheetRef } from 'react-modal-sheet'; import { useTransform } from 'motion/react'; import { useState, useRef } from 'react'; const snapPoints = [0, 0.5, 1]; function PaddingExample() { const [isOpen, setOpen] = useState(false); const sheetRef = useRef(null); // Add padding bottom based on how far the sheet is from being fully open const paddingBottom = useTransform(() => { return sheetRef.current?.y.get() ?? 0; }); return ( <> setOpen(false)} snapPoints={snapPoints} initialSnap={1} > This content will always be reachable even when the sheet is not fully open. ); } ``` -------------------------------- ### Conditionally Disable Scroll with Snap Points Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Use the `disableScroll` prop with a function to control scrolling based on the current snap point. This example allows scrolling only at the top snap point. ```tsx const snapPoints = [0, 0.5, 1]; function ConditionalScrollExample() { return ( state.currentSnap !== 2} >
Long content here...
); } ``` -------------------------------- ### Sheet with Smart Scroll and Drag Behavior Source: https://context7.com/temzasse/react-modal-sheet/llms.txt Configures Sheet.Content to conditionally disable scrolling or dragging based on the sheet's state. This example allows scrolling only when fully expanded and dragging only when scrolled to the top. ```tsx import { Sheet } from 'react-modal-sheet'; const snapPoints = [0, 170, 0.5, 1]; function SmartScrollSheet({ isOpen, onClose }: { isOpen: boolean; onClose: () => void }) { const lastSnapIndex = snapPoints.length - 1; return ( state.currentSnap !== lastSnapIndex} // Allow dragging only when scrolled to top disableDrag={(state) => state.scrollPosition !== 'top'} // Add bottom padding equal to the current Y offset so all items are reachable scrollStyle={{ paddingBottom: 80 }}> {Array.from({ length: 30 }, (_, i) => (
Item {i + 1}
))}
); } ``` -------------------------------- ### Access Sheet Methods via Ref Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Use `useRef` to get a reference to the Sheet component. This allows imperative access to methods like `snapTo` or properties like `y`. Avoid reading the ref during render due to React's rules. ```tsx import { Sheet, type SheetRef } from 'react-modal-sheet'; import { useRef } from 'react'; function RefExample() { const sheetRef = useRef(null); // Then use eg. `sheetRef.current.snapTo()` or `sheetRef.current.y`, etc. return ( {/* Your content here */} ); } ``` -------------------------------- ### onCloseStart Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Callback function invoked when the sheet's closing animation begins. ```APIDOC ## onCloseStart ### Description Callback function that is called when the sheet closing animation starts. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### modalEffectThreshold Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md A threshold value (0-1) determining when the iOS modal effect activates during sheet dragging. 0 means the effect starts immediately, 1 means it starts when the sheet is fully visible. ```APIDOC ## modalEffectThreshold ### Description Threshold value between 0-1 which determines when the iOS modal effect will start while dragging the sheet - `0` corresponding to the start of the drag (0% has been dragged into view) and `1` corresponding to the end of the drag (100% of the sheet is visible). ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json { "modalEffectThreshold": 0 } ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Basic Usage of Sheet Component Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Demonstrates the basic usage of the Sheet component, including state management for open/close and its compound components like Container, Header, Content, and Backdrop. ```tsx import { Sheet } from 'react-modal-sheet'; import { useState } from 'react'; function Example() { const [isOpen, setOpen] = useState(false); return ( <> setOpen(false)}> {/* Your sheet content goes here */} ); } ``` -------------------------------- ### Conditionally Disable Drag Based on Scroll Position Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Control when dragging is enabled using the `disableDrag` prop, which can be a function evaluating the scroll state. This example disables drag when the content is not scrolled to the top. ```tsx function DynamicDragExample() { return ( state.scrollPosition !== 'top'} >
Long content here...
); } ``` -------------------------------- ### Basic Sheet Component Usage Source: https://context7.com/temzasse/react-modal-sheet/llms.txt Demonstrates the basic usage of the Sheet component with various configuration options. Ensure Sheet.Container, Sheet.Header, and Sheet.Content are rendered within Sheet. ```tsx import { Sheet, type SheetRef } from 'react-modal-sheet'; import { useState, useRef } from 'react'; const snapPoints = [0, 0.5, 1]; // 0 = closed, 0.5 = 50 %, 1 = fully open function BasicSheet() { const [isOpen, setOpen] = useState(false); const sheetRef = useRef(null); return ( <> setOpen(false)} // Snap points in ascending order; 0 closes the sheet snapPoints={snapPoints} initialSnap={1} // open to 50 % detent="default" // 'default' | 'content' | 'full' avoidKeyboard // shift sheet up when keyboard opens (default true) disableDismiss={false} // prevent drag-to-close disableDrag={false} // lock all drag gestures disableScrollLocking={false} dragVelocityThreshold={1200} // px/s needed to flick-close dragCloseThreshold={0.6} // fraction of sheet that must be off-screen modalEffectRootId="root" // enables iOS-style scale on app root tweenConfig={{ ease: 'easeOut', duration: 0.2 }} prefersReducedMotion={false} onOpenStart={() => console.log('opening…')} onOpenEnd={() => console.log('open')} onCloseStart={() => console.log('closing…')} onCloseEnd={() => console.log('closed')} onSnap={(index) => console.log('snapped to', index)} >

Sheet content

setOpen(false)} />
); } ``` -------------------------------- ### Accessing Sheet Methods and Properties via Ref Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Demonstrates how to use `useRef` to gain imperative access to sheet methods and properties. ```APIDOC ## Accessing Sheet Methods and Properties via Ref This example shows how to use `useRef` to gain imperative access to sheet methods and properties. ```tsx import { Sheet, type SheetRef } from 'react-modal-sheet'; import { useRef } from 'react'; function RefExample() { const sheetRef = useRef(null); // Then use eg. `sheetRef.current.snapTo()` or `sheetRef.current.y`, etc. return ( {/* Your content here */} ); } ``` > [!IMPORTANT] > [React's rules about refs](https://react.dev/reference/react/useRef#caveats) apply here so don't read the sheet ref during render. If you need to access sheet methods or properties during render, use the `Sheet.useContext` hook instead. ``` -------------------------------- ### Enable Virtual Keyboard Avoidance Source: https://github.com/temzasse/react-modal-sheet/blob/main/README.md Use the `avoidKeyboard` prop to enable built-in virtual keyboard avoidance. This is enabled by default and automatically adds bottom padding to prevent the keyboard from covering input elements. ```tsx function KeyboardExample() { return (