Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.0 KB
Raw Blame
import { Stack } from 'expo-router'
import { Children, Fragment, isValidElement, type ReactElement, type ReactNode } from 'react'
import { useNativeHeaderCleanup } from '@/ui/useNativeHeaderCleanup'
import { nativeIcons } from './icons/nativeIcons'
import { useOverlayDismiss } from './overlayDismiss'
import type { ScreenChromeProps } from './ScreenChrome'
import { useColors } from './theme'

function toolbarItems(children: ReactNode): ReactNode[] {
    return Children.toArray(children).flatMap((child) =>
        isValidElement<{ children?: ReactNode }>(child) && child.type === Fragment
            ? toolbarItems(child.props.children)
            : [child]
    )
}

export function NativeScreenChrome({ onBack, nativeActions }: ScreenChromeProps): ReactElement {
    useNativeHeaderCleanup()
    const colors = useColors()
    const dismiss = useOverlayDismiss()
    return (
        <>
            <Stack.Screen
                options={{
                    headerShown: true,
                    gestureEnabled: !dismiss,
                    headerTransparent: true,
                    headerStyle: { backgroundColor: colors.background },
                    headerLargeStyle: { backgroundColor: colors.background },
                    headerShadowVisible: false,
                    scrollEdgeEffects: { top: 'hidden' },
                    headerTitle: '',
                    headerTintColor: colors.ink,
                    headerBackVisible: false,
                }}
            />
            <Stack.Toolbar placement="left">
                <Stack.Toolbar.Button
                    iconRenderingMode="template"
                    icon={dismiss ? nativeIcons.close : nativeIcons.back}
                    accessibilityLabel={dismiss ? 'Close' : 'Back'}
                    onPress={dismiss ?? onBack}
                />
            </Stack.Toolbar>
            <Stack.Toolbar placement="right">{dismiss ? null : toolbarItems(nativeActions)}</Stack.Toolbar>
        </>
    )
}