Skip to content

Virtual keyboard avoidance

React Modal Sheet includes built-in virtual keyboard avoidance that works automatically on mobile devices. When the avoidKeyboard prop is enabled (which is the default), the sheet will automatically add bottom padding to avoid the virtual keyboard covering input elements.

function KeyboardExample() {
return (
<Sheet avoidKeyboard>
<Sheet.Container>
<Sheet.Header />
<Sheet.Content>
<input placeholder="Your input here" />
<textarea placeholder="Your message here" />
{/* More form elements */}
</Sheet.Content>
</Sheet.Container>
<Sheet.Backdrop />
</Sheet>
);
}

Virtual keyboard avoidance consists of several key features:

  • Uses the env(keyboard-inset-height) from Virtual Keyboard API and fallbacks to Visual Viewport API and custom keyboard-inset-height CSS environment variable.
  • Works automatically on mobile devices with virtual keyboard support.
  • Applies dynamic padding to keep content visible when the keyboard opens.
  • Scrolls the focused input into view if it’s covered by the keyboard.
  • Disables drag gestures while the keyboard is open to prevent glitches.
  • When using snap points, the sheet will be automatically moved up to the last snap point to ensure that all content is reachable (even when the content is scrollable) within the sheet when the keyboard is open.

If the built-in keyboard avoidance doesn’t work for your use case, you can disable it with avoidKeyboard={false} and manage the keyboard state manually with the help of the useVirtualKeyboard hook.

import { Sheet, useVirtualKeyboard } from 'react-modal-sheet';
function ManualKeyboardExample() {
const [isOpen, setOpen] = useState(false);
// This hook manages the `--keyboard-inset-height` CSS variable for you
useVirtualKeyboard({ isEnabled: isOpen });
return (
// Disable default keyboard avoidance
<Sheet avoidKeyboard={false} isOpen={isOpen} onClose={() => setOpen(false)}>
<Sheet.Container>
<Sheet.Header />
<Sheet.Content
// Apply keyboard height as padding bottom to keep content above the keyboard
style={{ paddingBottom: 'var(--keyboard-inset-height)' }}
>
{/* Your content here */}
</Sheet.Content>
</Sheet.Container>
<Sheet.Backdrop />
</Sheet>
);
}

For a more detailed example of using useVirtualKeyboard see the Creating custom scrollers guide.