Control · Overlays
Bottom sheet
Screen
Colour scheme
Mode
Corners
Colours
Every state
- When
- Behaviour
Save these 2 files in components/mobilens/. They need only react-native (checked with Expo SDK 58).
// Bottom sheet: a panel that slides up over the screen for a short task or a choice.
// It closes on a tap outside, on a drag down from the handle, and on Android's back button, unless the task
// must not be interrupted (dismissible={false}). It rises with the keyboard.
// Works in Expo and bare React Native on iOS, Android and web. Depends only on react-native and ./theme.
import { useEffect, useRef, useState, type ReactNode } from 'react';
import {
AccessibilityInfo, Animated, KeyboardAvoidingView, Modal, PanResponder, Platform, Pressable, StyleSheet, Text, View,
} from 'react-native';
import { colors, radius } from './theme';
const NATIVE_DRIVER = Platform.OS !== 'web';
export type BottomSheetProps = {
/** Open or closed. Closing animates out before the sheet is removed. */
visible: boolean;
/** Called when the user asks to close it: backdrop, drag down or back button. Set visible to false. */
onClose: () => void;
/** Shown at the top and announced when the sheet opens. */
title?: string;
/** The sheet's content. */
children: ReactNode;
/** false keeps it open on backdrop taps, drags and back, for example while it saves. */
dismissible?: boolean;
};
export function BottomSheet({ visible, onClose, title, children, dismissible = true }: BottomSheetProps) {
const [mounted, setMounted] = useState(visible);
const open = useRef(new Animated.Value(0)).current;
const drag = useRef(new Animated.Value(0)).current;
const still = useReduceMotion();
const close = useRef(onClose);
close.current = onClose;
const canClose = useRef(dismissible);
canClose.current = dismissible;
useEffect(() => {
if (visible) {
setMounted(true);
drag.setValue(0);
Animated.timing(open, { toValue: 1, duration: still ? 150 : 260, useNativeDriver: NATIVE_DRIVER }).start();
} else {
Animated.timing(open, { toValue: 0, duration: still ? 120 : 200, useNativeDriver: NATIVE_DRIVER }).start(({ finished }) => finished && setMounted(false));
}
}, [visible, open, drag, still]);
const pan = useRef(
PanResponder.create({
onStartShouldSetPanResponder: () => canClose.current,
onMoveShouldSetPanResponder: (_, g) => canClose.current && g.dy > 4,
onPanResponderMove: (_, g) => drag.setValue(Math.max(0, g.dy)),
onPanResponderRelease: (_, g) => {
if (g.dy > 80 || g.vy > 1) close.current();
else Animated.spring(drag, { toValue: 0, useNativeDriver: NATIVE_DRIVER, bounciness: 0 }).start();
},
}),
).current;
if (!mounted) return null;
const rise = open.interpolate({ inputRange: [0, 1], outputRange: [still ? 0 : 400, 0] });
const sheet = (
<View style={styles.layer}>
<Animated.View style={[styles.layer, { opacity: open }]}>
<Pressable style={styles.backdrop} onPress={dismissible ? onClose : undefined} accessibilityRole="button" accessibilityLabel="Close" />
</Animated.View>
<KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : undefined} style={styles.bottom} pointerEvents="box-none">
<Animated.View style={{ opacity: still ? open : 1, transform: [{ translateY: Animated.add(rise, drag) }] }}>
<SheetView title={title} handle={<View {...pan.panHandlers} style={styles.handleArea}><View style={styles.handle} /></View>}>
{children}
</SheetView>
</Animated.View>
</KeyboardAvoidingView>
</View>
);
// On a device the sheet covers everything, header and tab bar too. On the web a Modal covers the whole window,
// so there the sheet stays inside the screen that renders it: render it as the screen's last child.
if (Platform.OS === 'web') return sheet;
return (
<Modal transparent visible animationType="none" statusBarTranslucent onRequestClose={() => {
if (dismissible) onClose();
}}>
{sheet}
</Modal>
);
}
/** The panel itself, with no animation or gestures. BottomSheet draws this; use it alone to show a state. */
export function SheetView({ title, handle, children }: { title?: string; handle?: ReactNode; children: ReactNode }) {
return (
<View style={styles.sheet} accessibilityViewIsModal>
{handle ?? <View style={styles.handleArea}><View style={styles.handle} /></View>}
{title ? <Text style={styles.title} accessibilityRole="header">{title}</Text> : null}
<View style={styles.body}>{children}</View>
</View>
);
}
/** The dimmed screen behind a sheet. */
export function SheetBackdrop() {
return <View style={[styles.layer, styles.backdrop]} />;
}
function useReduceMotion() {
const [on, setOn] = useState(false);
useEffect(() => {
let live = true;
AccessibilityInfo.isReduceMotionEnabled().then((v) => live && setOn(v));
const sub = AccessibilityInfo.addEventListener('reduceMotionChanged', setOn);
return () => {
live = false;
sub.remove();
};
}, []);
return on;
}
const styles = StyleSheet.create({
layer: { position: 'absolute', top: 0, right: 0, bottom: 0, left: 0 },
backdrop: { flex: 1, backgroundColor: colors.scrim },
bottom: { position: 'absolute', left: 0, right: 0, bottom: 0 },
sheet: {
paddingHorizontal: 20,
paddingBottom: 24,
borderTopLeftRadius: radius.lg,
borderTopRightRadius: radius.lg,
backgroundColor: colors.surface,
},
handleArea: { height: 28, alignItems: 'center', justifyContent: 'center' },
handle: { width: 36, height: 5, borderRadius: 3, backgroundColor: colors.outlineVariant },
title: { fontSize: 20, fontWeight: '600', color: colors.onSurface },
body: { marginTop: 12, gap: 12 },
});
// Bottom sheet: a panel that slides up over the screen for a short task or a choice.
// It closes on a tap outside, on a drag down from the handle, and on Android's back button, unless the task
// must not be interrupted (dismissible={false}). It rises with the keyboard.
// Works in Expo and bare React Native on iOS, Android and web. Depends only on react-native and ./theme.
import { useEffect, useRef, useState, type ReactNode } from 'react';
import {
AccessibilityInfo, Animated, KeyboardAvoidingView, Modal, PanResponder, Platform, Pressable, StyleSheet, Text, View,
} from 'react-native';
import { colors, radius } from './theme';
const NATIVE_DRIVER = Platform.OS !== 'web';
export type BottomSheetProps = {
/** Open or closed. Closing animates out before the sheet is removed. */
visible: boolean;
/** Called when the user asks to close it: backdrop, drag down or back button. Set visible to false. */
onClose: () => void;
/** Shown at the top and announced when the sheet opens. */
title?: string;
/** The sheet's content. */
children: ReactNode;
/** false keeps it open on backdrop taps, drags and back, for example while it saves. */
dismissible?: boolean;
};
export function BottomSheet({ visible, onClose, title, children, dismissible = true }: BottomSheetProps) {
const [mounted, setMounted] = useState(visible);
const open = useRef(new Animated.Value(0)).current;
const drag = useRef(new Animated.Value(0)).current;
const still = useReduceMotion();
const close = useRef(onClose);
close.current = onClose;
const canClose = useRef(dismissible);
canClose.current = dismissible;
useEffect(() => {
if (visible) {
setMounted(true);
drag.setValue(0);
Animated.timing(open, { toValue: 1, duration: still ? 150 : 260, useNativeDriver: NATIVE_DRIVER }).start();
} else {
Animated.timing(open, { toValue: 0, duration: still ? 120 : 200, useNativeDriver: NATIVE_DRIVER }).start(({ finished }) => finished && setMounted(false));
}
}, [visible, open, drag, still]);
const pan = useRef(
PanResponder.create({
onStartShouldSetPanResponder: () => canClose.current,
onMoveShouldSetPanResponder: (_, g) => canClose.current && g.dy > 4,
onPanResponderMove: (_, g) => drag.setValue(Math.max(0, g.dy)),
onPanResponderRelease: (_, g) => {
if (g.dy > 80 || g.vy > 1) close.current();
else Animated.spring(drag, { toValue: 0, useNativeDriver: NATIVE_DRIVER, bounciness: 0 }).start();
},
}),
).current;
if (!mounted) return null;
const rise = open.interpolate({ inputRange: [0, 1], outputRange: [still ? 0 : 400, 0] });
const sheet = (
<View style={styles.layer}>
<Animated.View style={[styles.layer, { opacity: open }]}>
<Pressable style={styles.backdrop} onPress={dismissible ? onClose : undefined} accessibilityRole="button" accessibilityLabel="Close" />
</Animated.View>
<KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : undefined} style={styles.bottom} pointerEvents="box-none">
<Animated.View style={{ opacity: still ? open : 1, transform: [{ translateY: Animated.add(rise, drag) }] }}>
<SheetView title={title} handle={<View {...pan.panHandlers} style={styles.handleArea}><View style={styles.handle} /></View>}>
{children}
</SheetView>
</Animated.View>
</KeyboardAvoidingView>
</View>
);
// On a device the sheet covers everything, header and tab bar too. On the web a Modal covers the whole window,
// so there the sheet stays inside the screen that renders it: render it as the screen's last child.
if (Platform.OS === 'web') return sheet;
return (
<Modal transparent visible animationType="none" statusBarTranslucent onRequestClose={() => {
if (dismissible) onClose();
}}>
{sheet}
</Modal>
);
}
/** The panel itself, with no animation or gestures. BottomSheet draws this; use it alone to show a state. */
export function SheetView({ title, handle, children }: { title?: string; handle?: ReactNode; children: ReactNode }) {
return (
<View style={styles.sheet} accessibilityViewIsModal>
{handle ?? <View style={styles.handleArea}><View style={styles.handle} /></View>}
{title ? <Text style={styles.title} accessibilityRole="header">{title}</Text> : null}
<View style={styles.body}>{children}</View>
</View>
);
}
/** The dimmed screen behind a sheet. */
export function SheetBackdrop() {
return <View style={[styles.layer, styles.backdrop]} />;
}
function useReduceMotion() {
const [on, setOn] = useState(false);
useEffect(() => {
let live = true;
AccessibilityInfo.isReduceMotionEnabled().then((v) => live && setOn(v));
const sub = AccessibilityInfo.addEventListener('reduceMotionChanged', setOn);
return () => {
live = false;
sub.remove();
};
}, []);
return on;
}
const styles = StyleSheet.create({
layer: { position: 'absolute', top: 0, right: 0, bottom: 0, left: 0 },
backdrop: { flex: 1, backgroundColor: colors.scrim },
bottom: { position: 'absolute', left: 0, right: 0, bottom: 0 },
sheet: {
paddingHorizontal: 20,
paddingBottom: 24,
borderTopLeftRadius: radius.lg,
borderTopRightRadius: radius.lg,
backgroundColor: colors.surface,
},
handleArea: { height: 28, alignItems: 'center', justifyContent: 'center' },
handle: { width: 36, height: 5, borderRadius: 3, backgroundColor: colors.outlineVariant },
title: { fontSize: 20, fontWeight: '600', color: colors.onSurface },
body: { marginTop: 12, gap: 12 },
});
// Theme: Emerald, light, default corners. Shared by every Mobilens component; edit the values to match your app.
export const colors = {
background: '#fafafa',
surface: '#ffffff',
surfaceVariant: '#f4f4f5',
onBackground: '#18181b',
onSurface: '#18181b',
onSurfaceVariant: '#6b6b74',
outline: '#8a8a93',
outlineVariant: '#e4e4e7',
error: '#ba1a1a',
success: '#296b2a',
warning: '#b45309',
shadow: '#000000',
scrim: 'rgba(0,0,0,0.32)',
primary: '#006c50',
onPrimary: '#ffffff',
primaryContainer: '#82f8cb',
onPrimaryContainer: '#002116',
secondary: '#3e6374',
onSecondary: '#ffffff',
secondaryContainer: '#d8dfe2',
onSecondaryContainer: '#1c2d34',
tertiary: '#1f6862',
onTertiary: '#ffffff',
tertiaryContainer: '#d3e0df',
onTertiaryContainer: '#0e2f2c',
onError: '#ffffff',
onSuccess: '#ffffff',
onWarning: '#ffffff',
} as const;
export const radius = {
sm: 6,
md: 12,
lg: 20,
} as const;
// Theme: Emerald, light, default corners. Shared by every Mobilens component; edit the values to match your app.
export const colors = {
background: '#fafafa',
surface: '#ffffff',
surfaceVariant: '#f4f4f5',
onBackground: '#18181b',
onSurface: '#18181b',
onSurfaceVariant: '#6b6b74',
outline: '#8a8a93',
outlineVariant: '#e4e4e7',
error: '#ba1a1a',
success: '#296b2a',
warning: '#b45309',
shadow: '#000000',
scrim: 'rgba(0,0,0,0.32)',
primary: '#006c50',
onPrimary: '#ffffff',
primaryContainer: '#82f8cb',
onPrimaryContainer: '#002116',
secondary: '#3e6374',
onSecondary: '#ffffff',
secondaryContainer: '#d8dfe2',
onSecondaryContainer: '#1c2d34',
tertiary: '#1f6862',
onTertiary: '#ffffff',
tertiaryContainer: '#d3e0df',
onTertiaryContainer: '#0e2f2c',
onError: '#ffffff',
onSuccess: '#ffffff',
onWarning: '#ffffff',
} as const;
export const radius = {
sm: 6,
md: 12,
lg: 20,
} as const;
Usage
import { useState } from 'react';
import { BottomSheet } from '@/components/mobilens/BottomSheet';
import { Button } from '@/components/mobilens/Button';
export function ShareButton({ copyLink, invite }: { copyLink: () => void; invite: () => void }) {
const [open, setOpen] = useState(false);
const run = (action: () => void) => () => {
setOpen(false);
action();
};
return (
<>
<Button title="Share" variant="secondary" onPress={() => setOpen(true)} />
{/* Render it as the screen's last child. */}
<BottomSheet visible={open} onClose={() => setOpen(false)} title="Share this list">
<Button title="Copy link" variant="secondary" onPress={run(copyLink)} />
<Button title="Invite people" onPress={run(invite)} />
</BottomSheet>
</>
);
}
import { useState } from 'react';
import { BottomSheet } from '@/components/mobilens/BottomSheet';
import { Button } from '@/components/mobilens/Button';
export function ShareButton({ copyLink, invite }: { copyLink: () => void; invite: () => void }) {
const [open, setOpen] = useState(false);
const run = (action: () => void) => () => {
setOpen(false);
action();
};
return (
<>
<Button title="Share" variant="secondary" onPress={() => setOpen(true)} />
{/* Render it as the screen's last child. */}
<BottomSheet visible={open} onClose={() => setOpen(false)} title="Share this list">
<Button title="Copy link" variant="secondary" onPress={run(copyLink)} />
<Button title="Invite people" onPress={run(invite)} />
</BottomSheet>
</>
);
}
API
| Prop | Type | Default | Description |
|---|---|---|---|
| visible | boolean | – | Open or closed. Closing animates out before the sheet is removed. |
| onClose | () => void | – | Called when the user asks to close it: backdrop, drag down or back button. Set visible to false. |
| title? | string | – | Shown at the top and announced when the sheet opens. |
| children | ReactNode | – | The sheet's content. |
| dismissible? | boolean | true | false keeps it open on backdrop taps, drags and back, for example while it saves. |
Used in
Add the "Bottom sheet" control to this Expo or React Native project. Create the files below with exactly this content. Do not restyle or rewrite them.
FILES
Put them in components/mobilens/. theme.ts is shared by every Mobilens component: if it already exists, keep one copy, and skip any other file that is already there with the same content. If the project has no "@/..." import alias, change those imports to relative paths.
--- components/mobilens/theme.ts ---
// Theme: Emerald, light, default corners. Shared by every Mobilens component; edit the values to match your app.
export const colors = {
background: '#fafafa',
surface: '#ffffff',
surfaceVariant: '#f4f4f5',
onBackground: '#18181b',
onSurface: '#18181b',
onSurfaceVariant: '#6b6b74',
outline: '#8a8a93',
outlineVariant: '#e4e4e7',
error: '#ba1a1a',
success: '#296b2a',
warning: '#b45309',
shadow: '#000000',
scrim: 'rgba(0,0,0,0.32)',
primary: '#006c50',
onPrimary: '#ffffff',
primaryContainer: '#82f8cb',
onPrimaryContainer: '#002116',
secondary: '#3e6374',
onSecondary: '#ffffff',
secondaryContainer: '#d8dfe2',
onSecondaryContainer: '#1c2d34',
tertiary: '#1f6862',
onTertiary: '#ffffff',
tertiaryContainer: '#d3e0df',
onTertiaryContainer: '#0e2f2c',
onError: '#ffffff',
onSuccess: '#ffffff',
onWarning: '#ffffff',
} as const;
export const radius = {
sm: 6,
md: 12,
lg: 20,
} as const;
--- end of theme.ts ---
--- components/mobilens/BottomSheet.tsx ---
// Bottom sheet: a panel that slides up over the screen for a short task or a choice.
// It closes on a tap outside, on a drag down from the handle, and on Android's back button, unless the task
// must not be interrupted (dismissible={false}). It rises with the keyboard.
// Works in Expo and bare React Native on iOS, Android and web. Depends only on react-native and ./theme.
import { useEffect, useRef, useState, type ReactNode } from 'react';
import {
AccessibilityInfo, Animated, KeyboardAvoidingView, Modal, PanResponder, Platform, Pressable, StyleSheet, Text, View,
} from 'react-native';
import { colors, radius } from './theme';
const NATIVE_DRIVER = Platform.OS !== 'web';
export type BottomSheetProps = {
/** Open or closed. Closing animates out before the sheet is removed. */
visible: boolean;
/** Called when the user asks to close it: backdrop, drag down or back button. Set visible to false. */
onClose: () => void;
/** Shown at the top and announced when the sheet opens. */
title?: string;
/** The sheet's content. */
children: ReactNode;
/** false keeps it open on backdrop taps, drags and back, for example while it saves. */
dismissible?: boolean;
};
export function BottomSheet({ visible, onClose, title, children, dismissible = true }: BottomSheetProps) {
const [mounted, setMounted] = useState(visible);
const open = useRef(new Animated.Value(0)).current;
const drag = useRef(new Animated.Value(0)).current;
const still = useReduceMotion();
const close = useRef(onClose);
close.current = onClose;
const canClose = useRef(dismissible);
canClose.current = dismissible;
useEffect(() => {
if (visible) {
setMounted(true);
drag.setValue(0);
Animated.timing(open, { toValue: 1, duration: still ? 150 : 260, useNativeDriver: NATIVE_DRIVER }).start();
} else {
Animated.timing(open, { toValue: 0, duration: still ? 120 : 200, useNativeDriver: NATIVE_DRIVER }).start(({ finished }) => finished && setMounted(false));
}
}, [visible, open, drag, still]);
const pan = useRef(
PanResponder.create({
onStartShouldSetPanResponder: () => canClose.current,
onMoveShouldSetPanResponder: (_, g) => canClose.current && g.dy > 4,
onPanResponderMove: (_, g) => drag.setValue(Math.max(0, g.dy)),
onPanResponderRelease: (_, g) => {
if (g.dy > 80 || g.vy > 1) close.current();
else Animated.spring(drag, { toValue: 0, useNativeDriver: NATIVE_DRIVER, bounciness: 0 }).start();
},
}),
).current;
if (!mounted) return null;
const rise = open.interpolate({ inputRange: [0, 1], outputRange: [still ? 0 : 400, 0] });
const sheet = (
<View style={styles.layer}>
<Animated.View style={[styles.layer, { opacity: open }]}>
<Pressable style={styles.backdrop} onPress={dismissible ? onClose : undefined} accessibilityRole="button" accessibilityLabel="Close" />
</Animated.View>
<KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : undefined} style={styles.bottom} pointerEvents="box-none">
<Animated.View style={{ opacity: still ? open : 1, transform: [{ translateY: Animated.add(rise, drag) }] }}>
<SheetView title={title} handle={<View {...pan.panHandlers} style={styles.handleArea}><View style={styles.handle} /></View>}>
{children}
</SheetView>
</Animated.View>
</KeyboardAvoidingView>
</View>
);
// On a device the sheet covers everything, header and tab bar too. On the web a Modal covers the whole window,
// so there the sheet stays inside the screen that renders it: render it as the screen's last child.
if (Platform.OS === 'web') return sheet;
return (
<Modal transparent visible animationType="none" statusBarTranslucent onRequestClose={() => {
if (dismissible) onClose();
}}>
{sheet}
</Modal>
);
}
/** The panel itself, with no animation or gestures. BottomSheet draws this; use it alone to show a state. */
export function SheetView({ title, handle, children }: { title?: string; handle?: ReactNode; children: ReactNode }) {
return (
<View style={styles.sheet} accessibilityViewIsModal>
{handle ?? <View style={styles.handleArea}><View style={styles.handle} /></View>}
{title ? <Text style={styles.title} accessibilityRole="header">{title}</Text> : null}
<View style={styles.body}>{children}</View>
</View>
);
}
/** The dimmed screen behind a sheet. */
export function SheetBackdrop() {
return <View style={[styles.layer, styles.backdrop]} />;
}
function useReduceMotion() {
const [on, setOn] = useState(false);
useEffect(() => {
let live = true;
AccessibilityInfo.isReduceMotionEnabled().then((v) => live && setOn(v));
const sub = AccessibilityInfo.addEventListener('reduceMotionChanged', setOn);
return () => {
live = false;
sub.remove();
};
}, []);
return on;
}
const styles = StyleSheet.create({
layer: { position: 'absolute', top: 0, right: 0, bottom: 0, left: 0 },
backdrop: { flex: 1, backgroundColor: colors.scrim },
bottom: { position: 'absolute', left: 0, right: 0, bottom: 0 },
sheet: {
paddingHorizontal: 20,
paddingBottom: 24,
borderTopLeftRadius: radius.lg,
borderTopRightRadius: radius.lg,
backgroundColor: colors.surface,
},
handleArea: { height: 28, alignItems: 'center', justifyContent: 'center' },
handle: { width: 36, height: 5, borderRadius: 3, backgroundColor: colors.outlineVariant },
title: { fontSize: 20, fontWeight: '600', color: colors.onSurface },
body: { marginTop: 12, gap: 12 },
});
--- end of BottomSheet.tsx ---
HOW TO USE IT
import { useState } from 'react';
import { BottomSheet } from '@/components/mobilens/BottomSheet';
import { Button } from '@/components/mobilens/Button';
export function ShareButton({ copyLink, invite }: { copyLink: () => void; invite: () => void }) {
const [open, setOpen] = useState(false);
const run = (action: () => void) => () => {
setOpen(false);
action();
};
return (
<>
<Button title="Share" variant="secondary" onPress={() => setOpen(true)} />
{/* Render it as the screen's last child. */}
<BottomSheet visible={open} onClose={() => setOpen(false)} title="Share this list">
<Button title="Copy link" variant="secondary" onPress={run(copyLink)} />
<Button title="Invite people" onPress={run(invite)} />
</BottomSheet>
</>
);
}
STATES: all 4 must work in the screen where you use it
- Closed: when nothing has opened it, or it was closed. Closing slides it down and fades the backdrop before it is removed.
- Open: when the screen sets visible. It slides up over a dimmed screen; with Reduce Motion on it fades. Tapping the dimmed area closes it. On Android the back button closes it. Screen readers stay inside the sheet while it is open.
- Dragging: when the user drags the handle down. It follows the finger. Let go past 80 points, or flick, and it closes; otherwise it springs back.
- With keyboard: when A field in the sheet has focus. The sheet rises so the field and its button stay above the keyboard.
ACCESSIBILITY
- The sheet is announced as a modal with its title; focus stays inside until it closes.
- The dimmed area is a labelled Close button, so it can be reached without the drag.
- With Reduce Motion on, it fades instead of sliding.
WHEN YOU FINISH
List every state and rule above and say how you verified each one.