Snackbar
Non-modal feedback with optional actions, safe-area and keyboard placement, polite announcements, and automatic dismissal paused for screen readers, touch, and backgrounding.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add snackbarSource files
Expand a file to read the source the installer copies into your app.
components/ui/snackbar/Snackbar.tsx
import * as React from 'react'
import {
AccessibilityInfo,
AppState,
KeyboardAvoidingView,
Platform,
Pressable,
ScrollView,
StyleSheet,
View,
type ViewProps,
} from 'react-native'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { X } from 'lucide-react-native'
import { Button, type ButtonProps } from '@/components/ui/button'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'
import { useThemeColors } from '@/lib/theme'
export interface SnackbarProps extends ViewProps {
open?: boolean
defaultOpen?: boolean
onOpenChange?: (open: boolean) => void
message: string
/** Milliseconds. Zero disables automatic dismissal. Screen readers also keep the message open. */
duration?: number
/** Extra space above app-owned bottom navigation. Safe areas are added automatically. */
bottomOffset?: number
closeLabel?: string
className?: string
}
const SnackbarContext = React.createContext<(() => void) | null>(null)
export const Snackbar = React.forwardRef<React.ComponentRef<typeof View>, SnackbarProps>(
(
{
open,
defaultOpen = false,
onOpenChange,
message,
duration = 5000,
bottomOffset = 0,
closeLabel = 'Dismiss message',
className,
children,
...props
},
ref,
) => {
const [internal, setInternal] = React.useState(defaultOpen)
const visible = open ?? internal
const [screenReader, setScreenReader] = React.useState(true)
const [appState, setAppState] = React.useState(AppState.currentState)
const [interacting, setInteracting] = React.useState(false)
const insets = useSafeAreaInsets()
const colors = useThemeColors()
const requested = React.useRef(visible)
requested.current = visible
const controlled = open !== undefined
const change = React.useRef(onOpenChange)
change.current = onOpenChange
const dismiss = React.useCallback(() => {
if (!requested.current) return
if (!controlled) {
requested.current = false
setInternal(false)
}
change.current?.(false)
}, [controlled])
useAnnouncement(visible ? message : undefined)
React.useEffect(() => {
setInteracting(false)
if (!visible) return
let mounted = true
void AccessibilityInfo.isScreenReaderEnabled().then((enabled) => {
if (mounted) setScreenReader(enabled)
})
setAppState(AppState.currentState)
const reader = AccessibilityInfo.addEventListener('screenReaderChanged', setScreenReader)
const app = AppState.addEventListener('change', setAppState)
return () => {
mounted = false
reader.remove()
app.remove()
}
}, [visible])
React.useEffect(() => {
if (
!visible ||
screenReader ||
interacting ||
appState !== 'active' ||
duration <= 0 ||
!Number.isFinite(duration)
)
return
const timeout = setTimeout(dismiss, duration)
return () => clearTimeout(timeout)
}, [visible, message, duration, screenReader, appState, interacting, dismiss])
if (!visible) return null
return (
<SnackbarContext.Provider value={dismiss}>
<KeyboardAvoidingView
pointerEvents="box-none"
behavior={Platform.OS === 'ios' ? 'padding' : undefined}
style={StyleSheet.absoluteFill}
>
<View
pointerEvents="box-none"
style={{
flex: 1,
justifyContent: 'flex-end',
paddingLeft: insets.left + 16,
paddingRight: insets.right + 16,
paddingBottom: insets.bottom + 16 + Math.max(0, bottomOffset),
}}
>
<View
ref={ref}
{...props}
onTouchStart={(event) => {
setInteracting(true)
props.onTouchStart?.(event)
}}
onTouchEnd={(event) => {
setInteracting(false)
props.onTouchEnd?.(event)
}}
onTouchCancel={(event) => {
setInteracting(false)
props.onTouchCancel?.(event)
}}
style={[
{ width: '100%', maxWidth: 600, maxHeight: '100%', flexShrink: 1, alignSelf: 'center' },
props.style,
]}
className={cn('bg-card border-border gap-3 rounded-xl border p-4 shadow-sm', className)}
>
<ScrollView
style={{ flexGrow: 0, flexShrink: 1 }}
contentContainerStyle={{ gap: 12 }}
keyboardShouldPersistTaps="handled"
>
<Text>{message}</Text>
<View className="flex-row flex-wrap items-center justify-end gap-[8px]">
{children}
<Button variant="ghost" size="icon" accessibilityLabel={closeLabel} onPress={dismiss}>
<X size={20} color={colors.foreground} aria-hidden />
</Button>
</View>
</ScrollView>
</View>
</View>
</KeyboardAvoidingView>
</SnackbarContext.Provider>
)
},
)
Snackbar.displayName = 'Snackbar'
export const SnackbarAction = React.forwardRef<React.ComponentRef<typeof Pressable>, ButtonProps>(
({ onPress, disabled, loading, ...props }, ref) => {
const dismiss = React.useContext(SnackbarContext)
if (!dismiss) throw new Error('SnackbarAction must be inside Snackbar')
return (
<Button
ref={ref}
variant="secondary"
{...props}
disabled={disabled}
loading={loading}
onPress={(event) => {
if (disabled || loading) return
onPress?.(event)
if (!event.isDefaultPrevented()) dismiss()
}}
/>
)
},
)
SnackbarAction.displayName = 'SnackbarAction'
components/ui/snackbar/index.ts
export { Snackbar, SnackbarAction, type SnackbarProps } from './Snackbar'
Dependencies
Registry items
npm packages
react-native-safe-area-contextlucide-react-nativereact-native-svg