Teardown

API Reference

Complete API documentation for @teardown/navigation.

Hooks

useTypedNavigation

Returns a type-safe navigation object.

function useTypedNavigation(): TypedNavigation;

TypedNavigation Interface

interface TypedNavigation {
  navigate<T extends RegisteredRoutePath>(
    ...args: ParamsFor<T> extends undefined ? [path: T] : [path: T, params: ParamsFor<T>]
  ): void;

  push<T extends RegisteredRoutePath>(
    ...args: ParamsFor<T> extends undefined ? [path: T] : [path: T, params: ParamsFor<T>]
  ): void;

  replace<T extends RegisteredRoutePath>(
    ...args: ParamsFor<T> extends undefined ? [path: T] : [path: T, params: ParamsFor<T>]
  ): void;

  goBack(): void;
  canGoBack(): boolean;
  reset<T extends RegisteredRoutePath>(state: NavigationState<T>): void;
  setParams<T extends RegisteredRoutePath>(params: Partial<ParamsFor<T>>): void;
  getParent(): TypedNavigation | undefined;
  isFocused(): boolean;
}

useTypedParams

Returns the current route's parameters.

function useTypedParams<T extends RegisteredRoutePath>(): ParamsFor<T>;

Example:

const { userId } = useTypedParams<"/users/:userId">();

useTypedRoute

Returns the full route object.

function useTypedRoute<T extends RegisteredRoutePath>(): TypedRoute<T>;

interface TypedRoute<T> {
  name: string;
  params: ParamsFor<T>;
  key: string;
}

Primitives

defineScreen

Defines a screen component with configuration.

function defineScreen<TParams = unknown>(config: ScreenConfig<TParams>): ScreenDefinition<TParams>;

ScreenConfig

interface ScreenConfig<TParams = unknown> {
  component: ComponentType;
  options?: ScreenOptions | ((props: { route: { params?: TParams }; navigation: unknown }) => ScreenOptions);
  params?: TParams;
  listeners?: {
    focus?: () => void;
    blur?: () => void;
    beforeRemove?: (e: { preventDefault: () => void }) => void;
  };
}

ScreenOptions

interface ScreenOptions {
  title?: string;
  headerShown?: boolean;
  headerTitle?: string | (() => React.ReactNode);
  headerLeft?: () => React.ReactNode;
  headerRight?: () => React.ReactNode;
  headerStyle?: object;
  headerTitleStyle?: object;
  headerBackTitle?: string;
  headerBackTitleVisible?: boolean;
  presentation?: "card" | "modal" | "transparentModal" | "containedModal" | "fullScreenModal" | "formSheet";
  animation?: "default" | "fade" | "fade_from_bottom" | "flip" | "simple_push" | "slide_from_bottom" | "slide_from_right" | "slide_from_left" | "none";
  gestureEnabled?: boolean;
  gestureDirection?: "horizontal" | "vertical";
  animationDuration?: number;
  // Tab options
  tabBarLabel?: string;
  tabBarIcon?: (props: { focused: boolean; color: string; size: number }) => React.ReactNode;
  tabBarBadge?: string | number;
  // Drawer options
  drawerLabel?: string;
  drawerIcon?: (props: { focused: boolean; color: string; size: number }) => React.ReactNode;
}

defineLayout

Defines a navigator layout.

function defineLayout<T extends LayoutConfig>(config: T): LayoutDefinition<T>;

LayoutConfig Types

type LayoutConfig = StackLayoutConfig | TabsLayoutConfig | DrawerLayoutConfig;

interface StackLayoutConfig {
  type: "stack";
  initialRouteName?: string;
  screenOptions?: StackScreenOptions;
}

interface TabsLayoutConfig {
  type: "tabs";
  initialRouteName?: string;
  screenOptions?: TabScreenOptions;
  tabBar?: ComponentType;
  screens?: Record<string, PerScreenOptions>;
}

interface DrawerLayoutConfig {
  type: "drawer";
  initialRouteName?: string;
  screenOptions?: DrawerScreenOptions;
  drawerContent?: ComponentType;
  screens?: Record<string, PerScreenOptions>;
}

createParamSchema

Creates a parameter validation schema using Zod.

function createParamSchema<T extends ParamSchemaShape>(shape: T): ParamSchema<T>;

Example:

import { z } from "zod";

const params = createParamSchema({
  userId: z.string().uuid(),
  tab: z.enum(["profile", "settings"]).optional(),
});

Router

createTeardownRouter

Creates a router component from a generated route tree.

function createTeardownRouter<T extends NavigatorNode | FlatRouteTree>(
  options: TeardownRouterOptions<T>
): ComponentType;

TeardownRouterOptions

interface TeardownRouterOptions<T> {
  routeTree: T;
  initialRouteName?: string;
  linking?: LinkingOptions<Record<string, unknown>>;
  theme?: Theme;
  notFoundComponent?: ComponentType;
  navigationContainerProps?: Omit<NavigationContainerProps, "children">;
}

Example:

import { createTeardownRouter } from "@teardown/navigation";
import { routeTree } from "../.teardown/routeTree.generated";
import { linking } from "../.teardown/linking.generated";

export const Router = createTeardownRouter({
  routeTree,
  linking,
  initialRouteName: "index",
});

Utilities

buildUrl

Builds a URL from a route path and parameters.

function buildUrl<T extends string>(path: T, params?: Record<string, string | number>): string;

Example:

buildUrl("/users/:userId", { userId: "123" }); // "/users/123"
buildUrl("/search/:query?", { query: "react" }); // "/search/react"
buildUrl("/search/:query?"); // "/search"

matchPath

Matches a URL against a route pattern.

function matchPath(pattern: string, path: string): MatchResult | null;

interface MatchResult {
  params: Record<string, string>;
  path: string;
}

parsePath

Parses a URL path into segments.

function parsePath(path: string): ParsedPath;

interface ParsedPath {
  segments: string[];
  params: Record<string, string>;
}

pathToScreenName

Converts a URL path to a React Navigation screen name.

function pathToScreenName(path: string): string;

Example:

pathToScreenName("/users/:userId"); // "users/[userId]"
pathToScreenName("/"); // "index"

Type Utilities

ParamsFor

Extracts parameter types for a route.

type ParamsFor<T extends RegisteredRoutePath> = RegisteredRouteParams[T];

RegisteredRoutePath

Union of all registered route paths.

type RegisteredRoutePath = keyof RegisteredRouteParams;

InferParams

Infers parameter types from a route path string.

type InferParams<T extends string> = /* inferred param object type */;

Example:

type Params = InferParams<"/users/:userId/posts/:postId">;
// { userId: string; postId: string }

Metro Plugin

withTeardownNavigation

Wraps a Metro configuration with navigation support.

function withTeardownNavigation(
  config: MetroConfig,
  options?: TeardownNavigationOptions
): MetroConfig;

TeardownNavigationOptions

interface TeardownNavigationOptions {
  routesDir?: string;      // Default: "./src/_routes"
  generatedDir?: string;   // Default: "./.teardown"
  prefixes?: string[];     // Default: []
  verbose?: boolean;       // Default: false
  autoTemplate?: boolean;  // Default: true
  usePolling?: boolean;    // Default: false
}

Generator

Class for generating route type files.

class Generator {
  constructor(config: GeneratorConfig);
  run(event: GeneratorEvent): Promise<void>;
}

startRouteWatcher

Starts watching for route file changes.

function startRouteWatcher(options: WatcherOptions): void;

stopRouteWatcher

Stops the route file watcher.

function stopRouteWatcher(): void;

Generated Files

The Metro plugin generates these files in .teardown/:

FileExportDescription
routes.generated.tsRouteParamsRoute parameter interface
routeTree.generated.tsrouteTreeNavigator tree structure
linking.generated.tslinkingDeep linking configuration
register.d.ts-Type augmentation for hooks
manifest.json-Route metadata