UIPackage React Native
Search
Menu

Drawer Navigation

Expo Router drawer with an accessible menu, two destinations, and an explicit close action.

Install

This example uses Expo Router. 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 drawer-navigation

Source files

Expand a file to read the source the installer copies into your app.

app/navigation/drawer/_layout.tsx
import { useCanGoBack } from 'expo-router'
import {
  Drawer,
  DrawerContentScrollView,
  DrawerItem,
  DrawerItemList,
  useDrawerProgress,
  type DrawerContentComponentProps,
} from 'expo-router/drawer'
import { Menu } from 'lucide-react-native'
import { useRef, type ComponentRef, type RefObject } from 'react'
import { AccessibilityInfo, Platform, Text } from 'react-native'
import { GestureHandlerRootView } from 'react-native-gesture-handler'
import { runOnJS, useAnimatedReaction, useSharedValue } from 'react-native-reanimated'
import { Button } from '@/components/ui/button'
import { useThemeColors } from '@/lib/theme'

type MenuButton = ComponentRef<typeof Button>

function DrawerMenu({
  menuButtons,
  ...props
}: DrawerContentComponentProps & { menuButtons: RefObject<Record<string, MenuButton | null>> }) {
  const colors = useThemeColors()
  const heading = useRef<ComponentRef<typeof Text>>(null)
  const progress = useDrawerProgress()
  const wasOpen = useSharedValue(false)
  const { navigation } = props

  async function moveFocus(open: boolean) {
    if (!(await AccessibilityInfo.isScreenReaderEnabled())) return
    const { routes, index } = navigation.getState()
    const target = open ? heading.current : menuButtons.current[routes[index].name]
    if (!target) return
    if (Platform.OS === 'web') target.focus()
    else AccessibilityInfo.sendAccessibilityEvent(target, 'focus')
  }

  // The drawer exposes its accessibility elements only near the end of the animation.
  useAnimatedReaction(
    () => progress.value,
    (value) => {
      if (value >= 0.99 && !wasOpen.value) {
        wasOpen.value = true
        runOnJS(moveFocus)(true)
      } else if (value <= 0.01 && wasOpen.value) {
        wasOpen.value = false
        runOnJS(moveFocus)(false)
      }
    },
  )

  return (
    <DrawerContentScrollView {...props}>
      <Text
        ref={heading}
        {...(Platform.OS === 'web' && { tabIndex: -1 })}
        accessible
        accessibilityRole="header"
        className="px-4 py-3 font-semibold text-foreground"
      >
        Navigation menu
      </Text>
      <DrawerItemList {...props} />
      <DrawerItem label="Close menu" inactiveTintColor={colors.foreground} onPress={navigation.closeDrawer} />
    </DrawerContentScrollView>
  )
}

export default function DrawerLayout() {
  const canGoBack = useCanGoBack()
  const colors = useThemeColors()
  const menuButtons = useRef<Record<string, MenuButton | null>>({})

  return (
    <GestureHandlerRootView style={{ flex: 1 }}>
      <Drawer
        backBehavior="initialRoute"
        drawerContent={(props) => <DrawerMenu {...props} menuButtons={menuButtons} />}
        screenOptions={({ navigation, route }) => ({
          headerStyle: { backgroundColor: colors.background },
          headerTintColor: colors.foreground,
          headerShadowVisible: false,
          headerLeft: () => (
            <Button
              ref={(button) => {
                menuButtons.current[route.name] = button
              }}
              accessibilityLabel="Show navigation menu"
              variant="ghost"
              size="icon"
              onPress={navigation.openDrawer}
            >
              <Menu size={24} color={colors.foreground} aria-hidden />
            </Button>
          ),
          headerRight: () => {
            const parent = navigation.getParent()
            return canGoBack && parent ? (
              <Button variant="ghost" size="sm" onPress={parent.goBack}>
                Done
              </Button>
            ) : null
          },
          sceneStyle: { backgroundColor: colors.background },
          drawerStyle: { backgroundColor: colors.background },
          drawerActiveBackgroundColor: colors.primary,
          drawerActiveTintColor: colors.primaryForeground,
          drawerInactiveTintColor: colors.foreground,
        })}
      >
        <Drawer.Screen name="index" options={{ title: 'Inbox' }} />
        <Drawer.Screen name="archive" options={{ title: 'Archive' }} />
      </Drawer>
    </GestureHandlerRootView>
  )
}
app/navigation/drawer/index.tsx
import { Text } from '@/components/ui/text'
import { NavigationExampleScreen } from '@/lib/navigation-example-screen'

export default function InboxScreen() {
  return (
    <NavigationExampleScreen>
      <Text size="lg" accessibilityRole="header">
        Recent messages
      </Text>
      <Text variant="muted">Open the menu in the header to visit Archive.</Text>
    </NavigationExampleScreen>
  )
}
app/navigation/drawer/archive.tsx
import { Text } from '@/components/ui/text'
import { NavigationExampleScreen } from '@/lib/navigation-example-screen'

export default function ArchiveScreen() {
  return (
    <NavigationExampleScreen>
      <Text size="lg" accessibilityRole="header">
        Archived messages
      </Text>
      <Text variant="muted">Open the menu to return to Inbox.</Text>
    </NavigationExampleScreen>
  )
}

Dependencies

Registry items

npm packages

  • expo-router@^58.0.0
  • lucide-react-native
  • react-native-gesture-handler
  • react-native-reanimated
Browse navigation examples