Command palette

@yasinsensoy/native

React Native · Components

Navigator

The app's navigation: stack (pages sliding in from the right, modals from the bottom), tabs, nested stacks, swipe back, the Android back button, deep links.

45 exports

Import

import {
  Header,
  TabBar,
  Navigator,
  HEADER_HEIGHT,
  useNavigation,
  useRoute,
  useIsFocused,
  useFocusEffect,
  useNavigationState,
  usePreventRemove,
  createRoutes,
  createNavigationRef,
  NAVIGATION_TEXTS,
} from "@yasinsensoy/native";

TabBar

component

The default tab bar: icon, label and badge. Leaves the safe area's bottom inset.

TabBar({ tabs, onPress, hideOnKeyboard, }: TabBarProps & { hideOnKeyboard?: boolean; }): JSX.Element | null

Height of the header bar (excluding the safe area, before scaling).

HEADER_HEIGHT: 

Navigation object: navigate, push, back, setOptions… When called inside a screen it is bound to that screen (setParams, setOptions, isFocused). Names and params are checked against the routes registered with Register.

useNavigation(): Navigation<ParamsOf<N>>

Examples

const navigation = useNavigation();
<Button onPress={() => navigation.push("product", { id: "42" })}>Open</Button>

useRoute

function

The screen's route: name and params. The params also arrive as the screen component's params prop.

useRoute(): RouteInfo<ParamsOf<N>>

useIsFocused

function

Whether the screen is focused (the top page and the selected tab).

useIsFocused(): boolean

Runs when the screen gains focus and cleans up when it loses focus (data refresh, listeners, timers). You can wrap the function in useCallback; it re-runs when it changes.

useFocusEffect(effect: () => undefined | (() => void)): void

Examples

useFocusEffect(useCallback(() => {
  const id = setInterval(refresh, 5000);
  return () => clearInterval(id);
}, []));

The whole navigation state; re-renders when it changes. With a selector, only when its result changes.

useNavigationState(selector?: (state: NavigatorState) => T): T

Prevents the screen from closing (back button, swipe back, back, returning to another route); for unsaved changes. When blocked onPrevent is called; event.proceed() applies the operation anyway.

usePreventRemove(prevent: boolean, onPrevent: (event: PreventRemoveEvent) => void): void

Examples

usePreventRemove(dirty, async ({ proceed }) => {
  if (await confirm({ title: "Changes will be lost", confirmText: "Leave" })) proceed();
});

createRoutes

function

Defines the route map (an identity function for type inference). Names must be unique, nested ones included.

createRoutes(routes: R): R

Examples

export const routes = createRoutes({
  main: {
    tabs: {
      feed: { screen: Feed, title: "Feed", icon: "home" },
      profile: { screen: Profile, title: "Profile", icon: "user" },
    },
  },
  product: { screen: Product, path: "product/:id", title: ({ params }) => params.name },
  settings: { screen: Settings, presentation: "modal", title: "Settings" },
});

A reference for navigating from outside the Navigator: <Navigator navigationRef={ref} />. Calls made before it is ready are ignored.

createNavigationRef(): NavigationRef

RouteInfo

interface

Route info of a screen.

NameTypeDescription
keystringUnique key.
namestringRoute name.
paramsPParams.

Deep link settings.

NameTypeDescription
prefixesstring[]The app's link prefixes: "myapp://", "https://site.com".
enabled?booleanfalse: links are not listened to.
onUnhandled?(url: string) => voidA link with no match (e.g. open it in the browser).

Enter animation of a new screen in the stack.

type StackAnimation = /** Slides in from the right, the one below shifts left slightly and dims (default). */ "slide" /** Slides up from the bottom; the one below stays in place (modal). */ | "slideUp" /** Appears sliding up from slightly below. */ | "fadeUp" /** Appears in place. */ | "fade" /** Appears growing slightly. */ | "scale" /** No animation. */ | "none"

Presentation of a screen.

type Presentation = /** A regular page. */ "card" /** A full-screen page from the bottom; closes on swipe down, with a close button in the header. */ | "modal" /** The screen below stays visible (screens with a transparent background, e.g. your own window). */ | "transparentModal"

Tab transition animation.

type TabAnimation = "fade" | "shift" | "none"

Behavior of the back button in tabs.

type TabBackBehavior = /** Returns to previous tabs in order (default). */ "history" /** Returns to the first tab, then exits. */ | "initialRoute" /** Does not change tabs. */ | "none"

Info passed to option functions.

NameTypeDescription
paramsPParams of the screen.

A value, or a function producing a value from the params.

type Dynamic = T | ((context: OptionsContext<P>) => T)

Info passed to elements drawn in the header bar.

NameTypeDescription
tintColorstringForeground color.
canGoBackbooleanWhether it can go back.

TabIconProps

interface

Info given to the tab icon.

NameTypeDescription
focusedbooleanWhether the tab is selected.
colorstringSelected/unselected color.
sizenumberSuggested size.

RouteOptions

interface

Options of a screen (or a container).

NameTypeDescription
title?Dynamic<string, P>Title (header bar and tab label).
headerShown?Dynamic<boolean, P>Whether the header bar is shown. Defaults to true for screens and false for containers (tabs/stack).
headerTitleAlign?"center" | "left"Title alignment. Defaults to "center" (the same on both platforms).
headerTransparent?booleanThe header bar sits transparent over the content (content starts below it).
headerTitle?(props: HeaderItemProps & { title: string; }) => ReactNodeDrawn in place of the title.
headerLeft?(props: HeaderItemProps) => ReactNodeDrawn on the left (in place of the back button).
headerRight?(props: HeaderItemProps) => ReactNodeDrawn on the right (action buttons).
header?(props: HeaderProps) => ReactNodeDrawn in place of the whole header bar.
presentation?PresentationPresentation. Defaults to "card".
animation?StackAnimationEnter animation; defaults by presentation (card → slide, modal → slideUp).
animationDuration?numberAnimation duration (ms).
gestureEnabled?booleanSwipe back (left to right on a card, top to bottom on a modal). Defaults to true.
fullScreenGestureEnabled?booleanThe swipe can start anywhere on the screen (not only from the left edge).
backgroundColor?stringBackground color of the screen; defaults to the theme's background color.
icon?IconProp | ((props: TabIconProps) => ReactNode)Tab icon: an icon name/data or a render function.
badge?Dynamic<number | string | boolean | undefined, P>Tab badge: a number, text or true (a dot).
tabLabel?stringTab label (title if omitted).

HeaderProps

interface

Info passed to the header bar.

NameTypeDescription
titlestringThe resolved title.
optionsResolvedOptionsThe resolved options.
canGoBackbooleanWhether the back button is shown.
onBack() => voidBack (or close in a modal).

ScreenProps

interface

Props passed to screen components.

NameTypeDescription
paramsPParams of the screen.

ScreenDef

interface

A screen definition.

NameTypeDescription
screenComponentType<ScreenProps<P>> | ComponentType<Record<string, never>>Screen component; the params type is inferred from the component's params prop.
path?stringDeep link path: "product/:id", an optional segment ":tab?". The query string (?ref=mail) is added to the params.
initialParams?Partial<P>Used when no params are given.
title?Dynamic<string, P>Title (header bar and tab label).
headerShown?Dynamic<boolean, P>Whether the header bar is shown. Defaults to true for screens and false for containers (tabs/stack).
headerTitleAlign?"center" | "left"Title alignment. Defaults to "center" (the same on both platforms).
headerTransparent?booleanThe header bar sits transparent over the content (content starts below it).
headerTitle?(props: HeaderItemProps & { title: string; }) => ReactNodeDrawn in place of the title.
headerLeft?(props: HeaderItemProps) => ReactNodeDrawn on the left (in place of the back button).
headerRight?(props: HeaderItemProps) => ReactNodeDrawn on the right (action buttons).
header?(props: HeaderProps) => ReactNodeDrawn in place of the whole header bar.
presentation?PresentationPresentation. Defaults to "card".
animation?StackAnimationEnter animation; defaults by presentation (card → slide, modal → slideUp).
animationDuration?numberAnimation duration (ms).
gestureEnabled?booleanSwipe back (left to right on a card, top to bottom on a modal). Defaults to true.
fullScreenGestureEnabled?booleanThe swipe can start anywhere on the screen (not only from the left edge).
backgroundColor?stringBackground color of the screen; defaults to the theme's background color.
icon?IconProp | ((props: TabIconProps) => ReactNode)Tab icon: an icon name/data or a render function.
badge?Dynamic<number | string | boolean | undefined, P>Tab badge: a number, text or true (a dot).
tabLabel?stringTab label (title if omitted).

TabsDef

interface

A tabbed container definition.

NameTypeDescription
tabsRouteMapTabs (in order). Each can be a screen or a container.
initialRoute?stringTab opened first; defaults to the first tab.
tabAnimation?TabAnimationTab transition animation. Defaults to "fade".
backBehavior?TabBackBehaviorBack button behavior. Defaults to "history".
lazy?booleanWhether tab screens are created on first open (default true) or all up front.
hideTabBarOnKeyboard?booleanHide the tab bar while the keyboard is open. Defaults to true.
tabBar?(props: TabBarProps) => ReactNodeDrawn in place of the tab bar.
title?Dynamic<string, P>Title (header bar and tab label).
headerShown?Dynamic<boolean, P>Whether the header bar is shown. Defaults to true for screens and false for containers (tabs/stack).
headerTitleAlign?"center" | "left"Title alignment. Defaults to "center" (the same on both platforms).
headerTransparent?booleanThe header bar sits transparent over the content (content starts below it).
headerTitle?(props: HeaderItemProps & { title: string; }) => ReactNodeDrawn in place of the title.
headerLeft?(props: HeaderItemProps) => ReactNodeDrawn on the left (in place of the back button).
headerRight?(props: HeaderItemProps) => ReactNodeDrawn on the right (action buttons).
header?(props: HeaderProps) => ReactNodeDrawn in place of the whole header bar.
presentation?PresentationPresentation. Defaults to "card".
animation?StackAnimationEnter animation; defaults by presentation (card → slide, modal → slideUp).
animationDuration?numberAnimation duration (ms).
gestureEnabled?booleanSwipe back (left to right on a card, top to bottom on a modal). Defaults to true.
fullScreenGestureEnabled?booleanThe swipe can start anywhere on the screen (not only from the left edge).
backgroundColor?stringBackground color of the screen; defaults to the theme's background color.
icon?IconProp | ((props: TabIconProps) => ReactNode)Tab icon: an icon name/data or a render function.
badge?Dynamic<number | string | boolean | undefined, P>Tab badge: a number, text or true (a dot).
tabLabel?stringTab label (title if omitted).

StackDef

interface

A nested stack definition (e.g. a tab's own pages).

NameTypeDescription
stackRouteMapScreens of the stack.
initialRoute?stringFirst screen; defaults to the first definition.
title?Dynamic<string, P>Title (header bar and tab label).
headerShown?Dynamic<boolean, P>Whether the header bar is shown. Defaults to true for screens and false for containers (tabs/stack).
headerTitleAlign?"center" | "left"Title alignment. Defaults to "center" (the same on both platforms).
headerTransparent?booleanThe header bar sits transparent over the content (content starts below it).
headerTitle?(props: HeaderItemProps & { title: string; }) => ReactNodeDrawn in place of the title.
headerLeft?(props: HeaderItemProps) => ReactNodeDrawn on the left (in place of the back button).
headerRight?(props: HeaderItemProps) => ReactNodeDrawn on the right (action buttons).
header?(props: HeaderProps) => ReactNodeDrawn in place of the whole header bar.
presentation?PresentationPresentation. Defaults to "card".
animation?StackAnimationEnter animation; defaults by presentation (card → slide, modal → slideUp).
animationDuration?numberAnimation duration (ms).
gestureEnabled?booleanSwipe back (left to right on a card, top to bottom on a modal). Defaults to true.
fullScreenGestureEnabled?booleanThe swipe can start anywhere on the screen (not only from the left edge).
backgroundColor?stringBackground color of the screen; defaults to the theme's background color.
icon?IconProp | ((props: TabIconProps) => ReactNode)Tab icon: an icon name/data or a render function.
badge?Dynamic<number | string | boolean | undefined, P>Tab badge: a number, text or true (a dot).
tabLabel?stringTab label (title if omitted).

Any route definition.

type RouteDef = ScreenDef | TabsDef | StackDef
NameTypeDescription
screenComponentType<ScreenProps<P>> | ComponentType<Record<string, never>>Screen component; the params type is inferred from the component's params prop.(ScreenDef)
path?stringDeep link path: "product/:id", an optional segment ":tab?". The query string (?ref=mail) is added to the params.(ScreenDef)
initialParams?Partial<P>Used when no params are given.(ScreenDef)
title?Dynamic<string, P>Title (header bar and tab label).
headerShown?Dynamic<boolean, P>Whether the header bar is shown. Defaults to true for screens and false for containers (tabs/stack).
headerTitleAlign?"center" | "left"Title alignment. Defaults to "center" (the same on both platforms).
headerTransparent?booleanThe header bar sits transparent over the content (content starts below it).
headerTitle?(props: HeaderItemProps & { title: string; }) => ReactNodeDrawn in place of the title.
headerLeft?(props: HeaderItemProps) => ReactNodeDrawn on the left (in place of the back button).
headerRight?(props: HeaderItemProps) => ReactNodeDrawn on the right (action buttons).
header?(props: HeaderProps) => ReactNodeDrawn in place of the whole header bar.
presentation?PresentationPresentation. Defaults to "card".
animation?StackAnimationEnter animation; defaults by presentation (card → slide, modal → slideUp).
animationDuration?numberAnimation duration (ms).
gestureEnabled?booleanSwipe back (left to right on a card, top to bottom on a modal). Defaults to true.
fullScreenGestureEnabled?booleanThe swipe can start anywhere on the screen (not only from the left edge).
backgroundColor?stringBackground color of the screen; defaults to the theme's background color.
icon?IconProp | ((props: TabIconProps) => ReactNode)Tab icon: an icon name/data or a render function.
badge?Dynamic<number | string | boolean | undefined, P>Tab badge: a number, text or true (a dot).
tabLabel?stringTab label (title if omitted).
tabsRouteMapTabs (in order). Each can be a screen or a container.(TabsDef)
initialRoute?stringTab opened first; defaults to the first tab.(TabsDef | StackDef)
tabAnimation?TabAnimationTab transition animation. Defaults to "fade".(TabsDef)
backBehavior?TabBackBehaviorBack button behavior. Defaults to "history".(TabsDef)
lazy?booleanWhether tab screens are created on first open (default true) or all up front.(TabsDef)
hideTabBarOnKeyboard?booleanHide the tab bar while the keyboard is open. Defaults to true.(TabsDef)
tabBar?(props: TabBarProps) => ReactNodeDrawn in place of the tab bar.(TabsDef)
stackRouteMapScreens of the stack.(StackDef)

Name → route definition.

type RouteMap = Record<string, RouteDef>

TabBarProps

interface

Info passed to the tab bar.

NameTypeDescription
tabs{ /** Route name. */ name: string; /** Label. */ label: string; /** Options. */ options: ResolvedOptions; /** Whether it is selected. */ focused: boolean; }[]Tabs.
onPress(name: string) => voidSwitches to the tab (returns to the start of its stack if already selected).

All names in the route map (nested ones included) → param types.

type ParamList = { [E in Entries<R> as E[0]]: E[1]; }

Register

interface

Extended to declare the app's routes; useNavigation() checks names and params against it.

Examples

declare module "@yasinsensoy/native/navigation" {
  interface Register { routes: typeof routes }
}

Param map of the registered routes.

type AppParamList = RegisteredRoutes extends RouteMap ? RouteMap extends RegisteredRoutes ? Record<string, any> : ParamList<RegisteredRoutes> : Record<string, any>

Registered route names.

type RouteName = keyof AppParamList & string

Params of a route.

type ParamsOf = AppParamList[N]

A required argument if the params are required, otherwise optional.

type ParamArgs = undefined extends ParamsOf<N> ? [params?: ParamsOf<N>] : [params: ParamsOf<N>]

RouteState

interface

State of a route.

NameTypeDescription
keystringUnique key.
namestringRoute name.
params?objectParams.
state?NavigatorStateInner state of container routes.

Info passed to the usePreventRemove callback.

NameTypeDescription
proceed() => voidApplies the blocked operation anyway (e.g. when "Leave" is confirmed).