first commit

This commit is contained in:
2026-03-10 16:18:05 +00:00
commit 11f9c069b5
31635 changed files with 3187747 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
import { nanoid } from 'nanoid/non-secure';
import type {
CommonNavigationAction,
NavigationState,
PartialState,
} from './types';
/**
* Base router object that can be used when writing custom routers.
* This provides few helper methods to handle common actions such as `RESET`.
*/
export const BaseRouter = {
getStateForAction<State extends NavigationState>(
state: State,
action: CommonNavigationAction
): State | PartialState<State> | null {
switch (action.type) {
case 'SET_PARAMS':
case 'REPLACE_PARAMS': {
const index = action.source
? state.routes.findIndex((r) => r.key === action.source)
: state.index;
if (index === -1) {
return null;
}
return {
...state,
routes: state.routes.map((r, i) =>
i === index
? {
...r,
params:
action.type === 'REPLACE_PARAMS'
? action.payload.params
: { ...r.params, ...action.payload.params },
}
: r
),
};
}
case 'RESET': {
const nextState = action.payload as State | PartialState<State>;
if (
nextState.routes.length === 0 ||
nextState.routes.some(
(route: { name: string }) => !state.routeNames.includes(route.name)
)
) {
return null;
}
if (nextState.stale === false) {
if (
state.routeNames.length !== nextState.routeNames.length ||
nextState.routeNames.some(
(name) => !state.routeNames.includes(name)
)
) {
return null;
}
return {
...nextState,
routes: nextState.routes.map((route) =>
route.key ? route : { ...route, key: `${route.name}-${nanoid()}` }
),
};
}
return nextState;
}
default:
return null;
}
},
shouldActionChangeFocus(action: CommonNavigationAction) {
return action.type === 'NAVIGATE' || action.type === 'NAVIGATE_DEPRECATED';
},
};

View File

@@ -0,0 +1,180 @@
import type { NavigationState, PartialState, Route } from './types';
type ResetState =
| PartialState<NavigationState>
| NavigationState
| (Omit<NavigationState, 'routes'> & {
routes: Omit<Route<string>, 'key'>[];
});
type GoBackAction = {
type: 'GO_BACK';
source?: string;
target?: string;
};
type NavigateAction = {
type: 'NAVIGATE';
payload: {
name: string;
params?: object;
path?: string;
merge?: boolean;
pop?: boolean;
};
source?: string;
target?: string;
};
type NavigateDeprecatedAction = {
type: 'NAVIGATE_DEPRECATED';
payload: {
name: string;
params?: object;
merge?: boolean;
};
source?: string;
target?: string;
};
type ResetAction = {
type: 'RESET';
payload: ResetState | undefined;
source?: string;
target?: string;
};
type SetParamsAction = {
type: 'SET_PARAMS';
payload: { params?: object };
source?: string;
target?: string;
};
type ReplaceParamsAction = {
type: 'REPLACE_PARAMS';
payload: { params?: object };
source?: string;
target?: string;
};
type PreloadAction = {
type: 'PRELOAD';
payload: {
name: string;
params?: object;
};
source?: string;
target?: string;
};
export type Action =
| GoBackAction
| NavigateAction
| NavigateDeprecatedAction
| ResetAction
| SetParamsAction
| ReplaceParamsAction
| PreloadAction;
export function goBack(): Action {
return { type: 'GO_BACK' };
}
export function navigate(
name: string,
params?: object,
options?: {
merge?: boolean;
pop?: boolean;
}
): Action;
export function navigate(options: {
name: string;
params?: object;
path?: string;
merge?: boolean;
pop?: boolean;
}): Action;
export function navigate(...args: any): Action {
if (typeof args[0] === 'string') {
const [name, params, options] = args;
if (typeof options === 'boolean') {
console.warn(
`Passing a boolean as the third argument to 'navigate' is deprecated. Pass '{ merge: true }' instead.`
);
}
return {
type: 'NAVIGATE',
payload: {
name,
params,
merge: typeof options === 'boolean' ? options : options?.merge,
pop: options?.pop,
},
};
} else {
const payload = args[0] || {};
if (!('name' in payload)) {
throw new Error(
'You need to specify a name when calling navigate with an object as the argument. See https://reactnavigation.org/docs/navigation-actions#navigate for usage.'
);
}
return { type: 'NAVIGATE', payload };
}
}
export function navigateDeprecated(
...args:
| [name: string]
| [name: string, params: object | undefined]
| [options: { name: string; params?: object }]
): Action {
if (typeof args[0] === 'string') {
return {
type: 'NAVIGATE_DEPRECATED',
payload: { name: args[0], params: args[1] },
};
} else {
const payload = args[0] || {};
if (!('name' in payload)) {
throw new Error(
'You need to specify a name when calling navigateDeprecated with an object as the argument. See https://reactnavigation.org/docs/navigation-actions#navigatelegacy for usage.'
);
}
return { type: 'NAVIGATE_DEPRECATED', payload };
}
}
export function reset(state: ResetState | undefined) {
return { type: 'RESET', payload: state } as const satisfies ResetAction;
}
export function setParams(params: object) {
return {
type: 'SET_PARAMS',
payload: { params },
} as const satisfies SetParamsAction;
}
export function replaceParams(params: object) {
return {
type: 'REPLACE_PARAMS',
payload: { params },
} as const satisfies ReplaceParamsAction;
}
export function preload(name: string, params?: object) {
return {
type: 'PRELOAD',
payload: { name, params },
} as const satisfies PreloadAction;
}

View File

@@ -0,0 +1,249 @@
import { nanoid } from 'nanoid/non-secure';
import {
type TabActionHelpers,
TabActions,
type TabActionType,
type TabNavigationState,
TabRouter,
type TabRouterOptions,
} from './TabRouter';
import type {
CommonNavigationAction,
ParamListBase,
PartialState,
Router,
} from './types';
export type DrawerStatus = 'open' | 'closed';
export type DrawerActionType =
| TabActionType
| {
type: 'OPEN_DRAWER' | 'CLOSE_DRAWER' | 'TOGGLE_DRAWER';
source?: string;
target?: string;
};
export type DrawerRouterOptions = TabRouterOptions & {
defaultStatus?: DrawerStatus;
};
export type DrawerNavigationState<ParamList extends ParamListBase> = Omit<
TabNavigationState<ParamList>,
'type' | 'history'
> & {
/**
* Type of the router, in this case, it's drawer.
*/
type: 'drawer';
/**
* Default status of the drawer.
*/
default: DrawerStatus;
/**
* List of previously visited route keys and drawer open status.
*/
history: (
| { type: 'route'; key: string }
| { type: 'drawer'; status: DrawerStatus }
)[];
};
export type DrawerActionHelpers<ParamList extends ParamListBase> =
TabActionHelpers<ParamList> & {
/**
* Open the drawer sidebar.
*/
openDrawer(): void;
/**
* Close the drawer sidebar.
*/
closeDrawer(): void;
/**
* Open the drawer sidebar if closed, or close if opened.
*/
toggleDrawer(): void;
};
export const DrawerActions = {
...TabActions,
openDrawer() {
return { type: 'OPEN_DRAWER' } as const satisfies DrawerActionType;
},
closeDrawer() {
return { type: 'CLOSE_DRAWER' } as const satisfies DrawerActionType;
},
toggleDrawer() {
return { type: 'TOGGLE_DRAWER' } as const satisfies DrawerActionType;
},
};
export function DrawerRouter({
defaultStatus = 'closed',
...rest
}: DrawerRouterOptions): Router<
DrawerNavigationState<ParamListBase>,
DrawerActionType | CommonNavigationAction
> {
const router = TabRouter(rest) as unknown as Router<
DrawerNavigationState<ParamListBase>,
TabActionType | CommonNavigationAction
>;
const isDrawerInHistory = (
state:
| DrawerNavigationState<ParamListBase>
| PartialState<DrawerNavigationState<ParamListBase>>
) => Boolean(state.history?.some((it) => it.type === 'drawer'));
const addDrawerToHistory = (
state: DrawerNavigationState<ParamListBase>
): DrawerNavigationState<ParamListBase> => {
if (isDrawerInHistory(state)) {
return state;
}
return {
...state,
history: [
...state.history,
{
type: 'drawer',
status: defaultStatus === 'open' ? 'closed' : 'open',
},
],
};
};
const removeDrawerFromHistory = (
state: DrawerNavigationState<ParamListBase>
): DrawerNavigationState<ParamListBase> => {
if (!isDrawerInHistory(state)) {
return state;
}
return {
...state,
history: state.history.filter((it) => it.type !== 'drawer'),
};
};
const openDrawer = (
state: DrawerNavigationState<ParamListBase>
): DrawerNavigationState<ParamListBase> => {
if (defaultStatus === 'open') {
return removeDrawerFromHistory(state);
}
return addDrawerToHistory(state);
};
const closeDrawer = (
state: DrawerNavigationState<ParamListBase>
): DrawerNavigationState<ParamListBase> => {
if (defaultStatus === 'open') {
return addDrawerToHistory(state);
}
return removeDrawerFromHistory(state);
};
return {
...router,
type: 'drawer',
getInitialState({ routeNames, routeParamList, routeGetIdList }) {
const state = router.getInitialState({
routeNames,
routeParamList,
routeGetIdList,
});
return {
...state,
default: defaultStatus,
stale: false,
type: 'drawer',
key: `drawer-${nanoid()}`,
};
},
getRehydratedState(
partialState,
{ routeNames, routeParamList, routeGetIdList }
) {
if (partialState.stale === false) {
return partialState;
}
let state = router.getRehydratedState(partialState, {
routeNames,
routeParamList,
routeGetIdList,
});
if (isDrawerInHistory(partialState)) {
// Re-sync the drawer entry in history to correct it if it was wrong
state = removeDrawerFromHistory(state);
state = addDrawerToHistory(state);
}
return {
...state,
default: defaultStatus,
type: 'drawer',
key: `drawer-${nanoid()}`,
};
},
getStateForRouteFocus(state, key) {
const result = router.getStateForRouteFocus(state, key);
return closeDrawer(result);
},
getStateForAction(state, action, options) {
switch (action.type) {
case 'OPEN_DRAWER':
return openDrawer(state);
case 'CLOSE_DRAWER':
return closeDrawer(state);
case 'TOGGLE_DRAWER':
if (isDrawerInHistory(state)) {
return removeDrawerFromHistory(state);
}
return addDrawerToHistory(state);
case 'JUMP_TO':
case 'NAVIGATE':
case 'NAVIGATE_DEPRECATED': {
const result = router.getStateForAction(state, action, options);
if (result != null && result.index !== state.index) {
return closeDrawer(result as DrawerNavigationState<ParamListBase>);
}
return result;
}
case 'GO_BACK':
if (isDrawerInHistory(state)) {
return removeDrawerFromHistory(state);
}
return router.getStateForAction(state, action, options);
default:
return router.getStateForAction(state, action, options);
}
},
actionCreators: DrawerActions,
};
}

View File

@@ -0,0 +1,733 @@
import { nanoid } from 'nanoid/non-secure';
import { BaseRouter } from './BaseRouter';
import { createParamsFromAction } from './createParamsFromAction';
import { createRouteFromAction } from './createRouteFromAction';
import type {
CommonNavigationAction,
DefaultRouterOptions,
NavigationRoute,
NavigationState,
ParamListBase,
Route,
Router,
} from './types';
export type StackActionType =
| {
type: 'REPLACE';
payload: { name: string; params?: object };
source?: string;
target?: string;
}
| {
type: 'PUSH';
payload: { name: string; params?: object };
source?: string;
target?: string;
}
| {
type: 'POP';
payload: { count: number };
source?: string;
target?: string;
}
| {
type: 'POP_TO_TOP';
source?: string;
target?: string;
}
| {
type: 'POP_TO';
payload: {
name: string;
params?: object;
merge?: boolean;
};
source?: string;
target?: string;
};
export type StackRouterOptions = DefaultRouterOptions;
export type StackNavigationState<ParamList extends ParamListBase> =
NavigationState<ParamList> & {
/**
* Type of the router, in this case, it's stack.
*/
type: 'stack';
/**
* List of routes, which are supposed to be preloaded before navigating to.
*/
preloadedRoutes: NavigationRoute<ParamList, keyof ParamList>[];
};
export type StackActionHelpers<ParamList extends ParamListBase> = {
/**
* Replace the current route with a new one.
*
* @param screen Name of the new route that will replace the current one.
* @param [params] Params object for the new route.
*/
replace<RouteName extends keyof ParamList>(
...args: RouteName extends unknown
? undefined extends ParamList[RouteName]
? [screen: RouteName, params?: ParamList[RouteName]]
: [screen: RouteName, params: ParamList[RouteName]]
: never
): void;
/**
* Push a new screen onto the stack.
*
* @param screen Name of the route to push onto the stack.
* @param [params] Params object for the route.
*/
push<RouteName extends keyof ParamList>(
...args: RouteName extends unknown
? undefined extends ParamList[RouteName]
? [screen: RouteName, params?: ParamList[RouteName]]
: [screen: RouteName, params: ParamList[RouteName]]
: never
): void;
/**
* Pop a screen from the stack.
*/
pop(count?: number): void;
/**
* Pop to the first route in the stack, dismissing all other screens.
*/
popToTop(): void;
/**
* Pop any screens to go back to the specified screen.
* If the specified screen doesn't exist, it'll be added to the stack.
*
* @param screen Name of the route to pop to.
* @param [params] Params object for the route.
* @param [options.merge] Whether to merge the params onto the route. Defaults to `false`.
*/
popTo<RouteName extends keyof ParamList>(
...args: RouteName extends unknown
? undefined extends ParamList[RouteName]
? [
screen: RouteName,
params?: ParamList[RouteName],
options?: { merge?: boolean },
]
: [
screen: RouteName,
params: ParamList[RouteName],
options?: { merge?: boolean },
]
: never
): void;
};
export const StackActions = {
replace(name: string, params?: object) {
return {
type: 'REPLACE',
payload: { name, params },
} as const satisfies StackActionType;
},
push(name: string, params?: object) {
return {
type: 'PUSH',
payload: { name, params },
} as const satisfies StackActionType;
},
pop(count: number = 1) {
return {
type: 'POP',
payload: { count },
} as const satisfies StackActionType;
},
popToTop() {
return { type: 'POP_TO_TOP' } as const satisfies StackActionType;
},
popTo(name: string, params?: object, options?: { merge?: boolean }) {
if (typeof options === 'boolean') {
console.warn(
`Passing a boolean as the third argument to 'popTo' is deprecated. Pass '{ merge: true }' instead.`
);
}
return {
type: 'POP_TO',
payload: {
name,
params,
merge: typeof options === 'boolean' ? options : options?.merge,
},
} as const satisfies StackActionType;
},
};
export function StackRouter(options: StackRouterOptions) {
const router: Router<
StackNavigationState<ParamListBase>,
CommonNavigationAction | StackActionType
> = {
...BaseRouter,
type: 'stack',
getInitialState({ routeNames, routeParamList }) {
const initialRouteName =
options.initialRouteName !== undefined &&
routeNames.includes(options.initialRouteName)
? options.initialRouteName
: routeNames[0];
return {
stale: false,
type: 'stack',
key: `stack-${nanoid()}`,
index: 0,
routeNames,
preloadedRoutes: [],
routes: [
{
key: `${initialRouteName}-${nanoid()}`,
name: initialRouteName,
params: routeParamList[initialRouteName],
},
],
};
},
getRehydratedState(partialState, { routeNames, routeParamList }) {
const state = partialState;
if (state.stale === false) {
return state;
}
const routes = state.routes
.filter((route) => routeNames.includes(route.name))
.map((route) => ({
...route,
key: route.key || `${route.name}-${nanoid()}`,
params:
routeParamList[route.name] !== undefined
? {
...routeParamList[route.name],
...route.params,
}
: route.params,
}));
const preloadedRoutes =
state.preloadedRoutes
?.filter((route) => routeNames.includes(route.name))
.map(
(route) =>
({
...route,
key: route.key || `${route.name}-${nanoid()}`,
params:
routeParamList[route.name] !== undefined
? {
...routeParamList[route.name],
...route.params,
}
: route.params,
}) as Route<string>
) ?? [];
if (routes.length === 0) {
const initialRouteName =
options.initialRouteName !== undefined
? options.initialRouteName
: routeNames[0];
routes.push({
key: `${initialRouteName}-${nanoid()}`,
name: initialRouteName,
params: routeParamList[initialRouteName],
});
}
return {
stale: false,
type: 'stack',
key: `stack-${nanoid()}`,
index: routes.length - 1,
routeNames,
routes,
preloadedRoutes,
};
},
getStateForRouteNamesChange(
state,
{ routeNames, routeParamList, routeKeyChanges }
) {
const routes = state.routes.filter(
(route) =>
routeNames.includes(route.name) &&
!routeKeyChanges.includes(route.name)
);
if (routes.length === 0) {
const initialRouteName =
options.initialRouteName !== undefined &&
routeNames.includes(options.initialRouteName)
? options.initialRouteName
: routeNames[0];
routes.push({
key: `${initialRouteName}-${nanoid()}`,
name: initialRouteName,
params: routeParamList[initialRouteName],
});
}
return {
...state,
routeNames,
routes,
index: Math.min(state.index, routes.length - 1),
};
},
getStateForRouteFocus(state, key) {
const index = state.routes.findIndex((r) => r.key === key);
if (index === -1 || index === state.index) {
return state;
}
return {
...state,
index,
routes: state.routes.slice(0, index + 1),
};
},
getStateForAction(state, action, options) {
const { routeParamList } = options;
switch (action.type) {
case 'REPLACE': {
const currentIndex =
action.target === state.key && action.source
? state.routes.findIndex((r) => r.key === action.source)
: state.index;
if (currentIndex === -1) {
return null;
}
if (!state.routeNames.includes(action.payload.name)) {
return null;
}
const getId = options.routeGetIdList[action.payload.name];
const id = getId?.({ params: action.payload.params });
// Re-use preloaded route if available
let route = state.preloadedRoutes.find(
(route) =>
route.name === action.payload.name &&
id === getId?.({ params: route.params })
);
if (!route) {
route = createRouteFromAction({ action, routeParamList });
}
return {
...state,
routes: state.routes.map((r, i) =>
i === currentIndex ? route : r
),
preloadedRoutes: state.preloadedRoutes.filter(
(r) => r.key !== route.key
),
};
}
case 'PUSH':
case 'NAVIGATE': {
if (!state.routeNames.includes(action.payload.name)) {
return null;
}
const getId = options.routeGetIdList[action.payload.name];
const id = getId?.({ params: action.payload.params });
let route: Route<string> | undefined;
if (id !== undefined) {
route = state.routes.findLast(
(route) =>
route.name === action.payload.name &&
id === getId?.({ params: route.params })
);
} else if (action.type === 'NAVIGATE') {
const currentRoute = state.routes[state.index];
// If the route matches the current one, then navigate to it
if (action.payload.name === currentRoute.name) {
route = currentRoute;
} else if (action.payload.pop) {
route = state.routes.findLast(
(route) => route.name === action.payload.name
);
}
}
if (!route) {
route = state.preloadedRoutes.find(
(route) =>
route.name === action.payload.name &&
id === getId?.({ params: route.params })
);
}
let params;
if (action.type === 'NAVIGATE' && action.payload.merge && route) {
params =
action.payload.params !== undefined ||
routeParamList[action.payload.name] !== undefined
? {
...routeParamList[action.payload.name],
...route.params,
...action.payload.params,
}
: route.params;
} else {
params = createParamsFromAction({ action, routeParamList });
}
let routes: Route<string>[];
if (route) {
if (action.type === 'NAVIGATE' && action.payload.pop) {
routes = [];
// Get all routes until the matching one
for (const r of state.routes) {
if (r.key === route.key) {
routes.push({
...route,
path:
action.payload.path !== undefined
? action.payload.path
: route.path,
params,
});
break;
}
routes.push(r);
}
} else {
routes = state.routes.filter((r) => r.key !== route.key);
routes.push({
...route,
path:
action.type === 'NAVIGATE' &&
action.payload.path !== undefined
? action.payload.path
: route.path,
params,
});
}
} else {
routes = [
...state.routes,
{
key: `${action.payload.name}-${nanoid()}`,
name: action.payload.name,
path:
action.type === 'NAVIGATE' ? action.payload.path : undefined,
params,
},
];
}
return {
...state,
index: routes.length - 1,
preloadedRoutes: state.preloadedRoutes.filter(
(route) => routes[routes.length - 1].key !== route.key
),
routes,
};
}
case 'NAVIGATE_DEPRECATED': {
if (!state.routeNames.includes(action.payload.name)) {
return null;
}
if (
state.preloadedRoutes.find(
(route) =>
route.name === action.payload.name &&
id === getId?.({ params: route.params })
)
) {
return null;
}
// If the route already exists, navigate to that
let index = -1;
const getId = options.routeGetIdList[action.payload.name];
const id = getId?.({ params: action.payload.params });
if (id !== undefined) {
index = state.routes.findIndex(
(route) =>
route.name === action.payload.name &&
id === getId?.({ params: route.params })
);
} else if (state.routes[state.index].name === action.payload.name) {
index = state.index;
} else {
index = state.routes.findLastIndex(
(route) => route.name === action.payload.name
);
}
if (index === -1) {
const routes = [
...state.routes,
createRouteFromAction({ action, routeParamList }),
];
return {
...state,
routes,
index: routes.length - 1,
};
}
const route = state.routes[index];
let params;
if (action.payload.merge) {
params =
action.payload.params !== undefined ||
routeParamList[route.name] !== undefined
? {
...routeParamList[route.name],
...route.params,
...action.payload.params,
}
: route.params;
} else {
params = createParamsFromAction({ action, routeParamList });
}
return {
...state,
index,
routes: [
...state.routes.slice(0, index),
params !== route.params
? { ...route, params }
: state.routes[index],
],
};
}
case 'POP': {
const currentIndex =
action.target === state.key && action.source
? state.routes.findIndex((r) => r.key === action.source)
: state.index;
if (currentIndex > 0) {
const count = Math.max(currentIndex - action.payload.count + 1, 1);
const routes = state.routes
.slice(0, count)
.concat(state.routes.slice(currentIndex + 1));
return {
...state,
index: routes.length - 1,
routes,
};
}
return null;
}
case 'POP_TO_TOP':
return router.getStateForAction(
state,
{
type: 'POP',
payload: { count: state.routes.length - 1 },
},
options
);
case 'POP_TO': {
const currentIndex =
action.target === state.key && action.source
? state.routes.findLastIndex((r) => r.key === action.source)
: state.index;
if (currentIndex === -1) {
return null;
}
if (!state.routeNames.includes(action.payload.name)) {
return null;
}
// If the route already exists, navigate to it
let index = -1;
const getId = options.routeGetIdList[action.payload.name];
const id = getId?.({ params: action.payload.params });
if (id !== undefined) {
index = state.routes.findIndex(
(route) =>
route.name === action.payload.name &&
id === getId?.({ params: route.params })
);
} else if (state.routes[currentIndex].name === action.payload.name) {
index = currentIndex;
} else {
for (let i = currentIndex; i >= 0; i--) {
if (state.routes[i].name === action.payload.name) {
index = i;
break;
}
}
}
// If the route doesn't exist, remove the current route and add the new one
if (index === -1) {
// Re-use preloaded route if available
let route = state.preloadedRoutes.find(
(route) =>
route.name === action.payload.name &&
id === getId?.({ params: route.params })
);
if (!route) {
route = createRouteFromAction({ action, routeParamList });
}
const routes = state.routes.slice(0, currentIndex).concat(route);
return {
...state,
index: routes.length - 1,
routes,
preloadedRoutes: state.preloadedRoutes.filter(
(r) => r.key !== route.key
),
};
}
const route = state.routes[index];
let params;
if (action.payload.merge) {
params =
action.payload.params !== undefined ||
routeParamList[route.name] !== undefined
? {
...routeParamList[route.name],
...route.params,
...action.payload.params,
}
: route.params;
} else {
params = createParamsFromAction({ action, routeParamList });
}
return {
...state,
index,
routes: [
...state.routes.slice(0, index),
params !== route.params
? { ...route, params }
: state.routes[index],
],
};
}
case 'GO_BACK':
if (state.index > 0) {
return router.getStateForAction(
state,
{
type: 'POP',
payload: { count: 1 },
target: action.target,
source: action.source,
},
options
);
}
return null;
case 'PRELOAD': {
const getId = options.routeGetIdList[action.payload.name];
const id = getId?.({ params: action.payload.params });
let route: Route<string> | undefined;
if (id !== undefined) {
route = state.routes.find(
(route) =>
route.name === action.payload.name &&
id === getId?.({ params: route.params })
);
}
if (route) {
return {
...state,
routes: state.routes.map((r) => {
if (r.key !== route?.key) {
return r;
}
return {
...r,
params: createParamsFromAction({ action, routeParamList }),
};
}),
};
} else {
return {
...state,
preloadedRoutes: state.preloadedRoutes
.filter(
(r) =>
r.name !== action.payload.name ||
id !== getId?.({ params: r.params })
)
.concat(createRouteFromAction({ action, routeParamList })),
};
}
}
default:
return BaseRouter.getStateForAction(state, action);
}
},
actionCreators: StackActions,
};
return router;
}

View File

@@ -0,0 +1,546 @@
import { nanoid } from 'nanoid/non-secure';
import { BaseRouter } from './BaseRouter';
import { createParamsFromAction } from './createParamsFromAction';
import type {
CommonNavigationAction,
DefaultRouterOptions,
NavigationState,
ParamListBase,
PartialState,
Route,
Router,
} from './types';
export type TabActionType = {
type: 'JUMP_TO';
payload: { name: string; params?: object };
source?: string;
target?: string;
};
export type BackBehavior =
| 'firstRoute'
| 'initialRoute'
| 'order'
| 'history'
| 'fullHistory'
| 'none';
export type TabRouterOptions = DefaultRouterOptions & {
/**
* Control how going back should behave
* - `firstRoute` - return to the first defined route
* - `initialRoute` - return to the route from `initialRouteName`
* - `order` - return to the route defined before the focused route
* - `history` - return to last visited route; if the same route is visited multiple times, the older entries are dropped from the history
* - `fullHistory` - return to last visited route; doesn't drop duplicate entries unlike `history` - matches behavior of web pages
* - `none` - do not handle going back
*/
backBehavior?: BackBehavior;
};
export type TabNavigationState<ParamList extends ParamListBase> = Omit<
NavigationState<ParamList>,
'history'
> & {
/**
* Type of the router, in this case, it's tab.
*/
type: 'tab';
/**
* List of previously visited route keys.
*/
history: { type: 'route'; key: string; params?: object | undefined }[];
/**
* List of routes' key, which are supposed to be preloaded before navigating to.
*/
preloadedRouteKeys: string[];
};
export type TabActionHelpers<ParamList extends ParamListBase> = {
/**
* Jump to an existing tab.
*
* @param screen Name of the route to jump to.
* @param [params] Params object for the route.
*/
jumpTo<RouteName extends keyof ParamList>(
...args: RouteName extends unknown
? undefined extends ParamList[RouteName]
? [screen: RouteName, params?: ParamList[RouteName]]
: [screen: RouteName, params: ParamList[RouteName]]
: never
): void;
};
const TYPE_ROUTE = 'route' as const;
export const TabActions = {
jumpTo(name: string, params?: object) {
return {
type: 'JUMP_TO',
payload: { name, params },
} as const satisfies TabActionType;
},
};
const getRouteHistory = (
routes: Route<string>[],
index: number,
backBehavior: BackBehavior,
initialRouteName: string | undefined
) => {
const history = [
{
type: TYPE_ROUTE,
key: routes[index].key,
},
];
let initialRouteIndex;
switch (backBehavior) {
case 'order':
for (let i = index; i > 0; i--) {
history.unshift({
type: TYPE_ROUTE,
key: routes[i - 1].key,
});
}
break;
case 'firstRoute':
if (index !== 0) {
history.unshift({
type: TYPE_ROUTE,
key: routes[0].key,
});
}
break;
case 'initialRoute':
initialRouteIndex = routes.findIndex(
(route) => route.name === initialRouteName
);
initialRouteIndex = initialRouteIndex === -1 ? 0 : initialRouteIndex;
if (index !== initialRouteIndex) {
history.unshift({
type: TYPE_ROUTE,
key: routes[initialRouteIndex].key,
});
}
break;
case 'history':
case 'fullHistory':
// The history will fill up on navigation
break;
}
return history;
};
const changeIndex = (
state: TabNavigationState<ParamListBase>,
index: number,
backBehavior: BackBehavior,
initialRouteName: string | undefined
) => {
let history = state.history;
if (backBehavior === 'history' || backBehavior === 'fullHistory') {
const currentRoute = state.routes[index];
if (backBehavior === 'history') {
// Remove the existing key from the history to de-duplicate it
history = history.filter((it) =>
it.type === 'route' ? it.key !== currentRoute.key : false
);
} else if (backBehavior === 'fullHistory') {
const lastHistoryRouteItemIndex = history.findLastIndex(
(item) => item.type === 'route'
);
if (currentRoute.key === history[lastHistoryRouteItemIndex]?.key) {
// For full-history, only remove if it matches the last route
// Useful for drawer, if current route was in history, then drawer state changed
// Then we only need to move the route to the front
history = [
...history.slice(0, lastHistoryRouteItemIndex),
...history.slice(lastHistoryRouteItemIndex + 1),
];
}
}
history = history.concat({
type: TYPE_ROUTE,
key: currentRoute.key,
params: backBehavior === 'fullHistory' ? currentRoute.params : undefined,
});
} else {
history = getRouteHistory(
state.routes,
index,
backBehavior,
initialRouteName
);
}
return {
...state,
index,
history,
};
};
export function TabRouter({
initialRouteName,
backBehavior = 'firstRoute',
}: TabRouterOptions) {
const router: Router<
TabNavigationState<ParamListBase>,
TabActionType | CommonNavigationAction
> = {
...BaseRouter,
type: 'tab',
getInitialState({ routeNames, routeParamList }) {
const index =
initialRouteName !== undefined && routeNames.includes(initialRouteName)
? routeNames.indexOf(initialRouteName)
: 0;
const routes = routeNames.map((name) => ({
name,
key: `${name}-${nanoid()}`,
params: routeParamList[name],
}));
const history = getRouteHistory(
routes,
index,
backBehavior,
initialRouteName
);
return {
stale: false,
type: 'tab',
key: `tab-${nanoid()}`,
index,
routeNames,
history,
routes,
preloadedRouteKeys: [],
};
},
getRehydratedState(partialState, { routeNames, routeParamList }) {
const state = partialState;
if (state.stale === false) {
return state;
}
const routes = routeNames.map((name) => {
const route = (
state as PartialState<TabNavigationState<ParamListBase>>
).routes.find((r) => r.name === name);
return {
...route,
name,
key:
route && route.name === name && route.key
? route.key
: `${name}-${nanoid()}`,
params:
routeParamList[name] !== undefined
? {
...routeParamList[name],
...(route ? route.params : undefined),
}
: route
? route.params
: undefined,
} as Route<string>;
});
const index = Math.min(
Math.max(routeNames.indexOf(state.routes[state?.index ?? 0]?.name), 0),
routes.length - 1
);
const routeKeys = routes.map((route) => route.key);
const history =
state.history?.filter((it) => routeKeys.includes(it.key)) ?? [];
return changeIndex(
{
stale: false,
type: 'tab',
key: `tab-${nanoid()}`,
index,
routeNames,
history,
routes,
preloadedRouteKeys:
state.preloadedRouteKeys?.filter((key) =>
routeKeys.includes(key)
) ?? [],
},
index,
backBehavior,
initialRouteName
);
},
getStateForRouteNamesChange(
state,
{ routeNames, routeParamList, routeKeyChanges }
) {
const routes = routeNames.map(
(name) =>
state.routes.find(
(r) => r.name === name && !routeKeyChanges.includes(r.name)
) || {
name,
key: `${name}-${nanoid()}`,
params: routeParamList[name],
}
);
const index = Math.max(
0,
routeNames.indexOf(state.routes[state.index].name)
);
let history = state.history.filter(
// Type will always be 'route' for tabs, but could be different in a router extending this (e.g. drawer)
(it) => it.type !== 'route' || routes.find((r) => r.key === it.key)
);
if (!history.length) {
history = getRouteHistory(
routes,
index,
backBehavior,
initialRouteName
);
}
return {
...state,
history,
routeNames,
routes,
index,
};
},
getStateForRouteFocus(state, key) {
const index = state.routes.findIndex((r) => r.key === key);
if (index === -1 || index === state.index) {
return state;
}
return changeIndex(state, index, backBehavior, initialRouteName);
},
getStateForAction(state, action, { routeParamList, routeGetIdList }) {
switch (action.type) {
case 'JUMP_TO':
case 'NAVIGATE':
case 'NAVIGATE_DEPRECATED': {
const index = state.routes.findIndex(
(route) => route.name === action.payload.name
);
if (index === -1) {
return null;
}
const updatedState = changeIndex(
{
...state,
routes: state.routes.map((route) => {
if (route.name !== action.payload.name) {
return route;
}
const getId = routeGetIdList[route.name];
const currentId = getId?.({ params: route.params });
const nextId = getId?.({ params: action.payload.params });
const key =
currentId === nextId
? route.key
: `${route.name}-${nanoid()}`;
let params;
if (
(action.type === 'NAVIGATE' ||
action.type === 'NAVIGATE_DEPRECATED') &&
action.payload.merge &&
currentId === nextId
) {
params =
action.payload.params !== undefined ||
routeParamList[route.name] !== undefined
? {
...routeParamList[route.name],
...route.params,
...action.payload.params,
}
: route.params;
} else {
params = createParamsFromAction({ action, routeParamList });
}
const path =
action.type === 'NAVIGATE' && action.payload.path != null
? action.payload.path
: route.path;
return params !== route.params || path !== route.path
? { ...route, key, path, params }
: route;
}),
},
index,
backBehavior,
initialRouteName
);
return {
...updatedState,
preloadedRouteKeys: updatedState.preloadedRouteKeys.filter(
(key) => key !== state.routes[updatedState.index].key
),
};
}
case 'SET_PARAMS':
case 'REPLACE_PARAMS': {
const nextState = BaseRouter.getStateForAction(state, action);
if (nextState !== null) {
const index = nextState.index;
if (index != null) {
const focusedRoute = nextState.routes[index];
const historyItemIndex = state.history.findLastIndex(
(item) => item.key === focusedRoute.key
);
let updatedHistory = state.history;
if (historyItemIndex !== -1) {
updatedHistory = [...state.history];
updatedHistory[historyItemIndex] = {
...updatedHistory[historyItemIndex],
params: focusedRoute.params,
};
}
return {
...nextState,
history: updatedHistory,
};
}
}
return nextState;
}
case 'GO_BACK': {
if (state.history.length === 1) {
return null;
}
const previousHistoryItem = state.history[state.history.length - 2];
const previousKey = previousHistoryItem?.key;
const index = state.routes.findLastIndex(
(route) => route.key === previousKey
);
if (index === -1) {
return null;
}
let routes = state.routes;
if (
backBehavior === 'fullHistory' &&
routes[index].params !== previousHistoryItem.params
) {
routes = [...state.routes];
routes[index] = {
...routes[index],
params: previousHistoryItem.params,
};
}
return {
...state,
routes,
preloadedRouteKeys: state.preloadedRouteKeys.filter(
(key) => key !== state.routes[index].key
),
history: state.history.slice(0, -1),
index,
};
}
case 'PRELOAD': {
const routeIndex = state.routes.findIndex(
(route) => route.name === action.payload.name
);
if (routeIndex === -1) {
return null;
}
const route = state.routes[routeIndex];
const getId = routeGetIdList[route.name];
const currentId = getId?.({ params: route.params });
const nextId = getId?.({ params: action.payload.params });
const key =
currentId === nextId ? route.key : `${route.name}-${nanoid()}`;
const params = createParamsFromAction({ action, routeParamList });
const newRoute =
params !== route.params ? { ...route, key, params } : route;
return {
...state,
preloadedRouteKeys: state.preloadedRouteKeys
.filter((key) => key !== route.key)
.concat(newRoute.key),
routes: state.routes.map((route, index) =>
index === routeIndex ? newRoute : route
),
history:
key === route.key
? state.history
: state.history.filter((record) => record.key !== route.key),
};
}
default:
return BaseRouter.getStateForAction(state, action);
}
},
actionCreators: TabActions,
};
return router;
}

View File

@@ -0,0 +1,22 @@
import type { ParamListBase } from './types';
type Options = {
action: {
payload: {
name: string;
params?: object;
};
};
routeParamList: ParamListBase;
};
export function createParamsFromAction({ action, routeParamList }: Options) {
const { name, params } = action.payload;
return routeParamList[name] !== undefined
? {
...routeParamList[name],
...params,
}
: params;
}

View File

@@ -0,0 +1,24 @@
import { nanoid } from 'nanoid/non-secure';
import { createParamsFromAction } from './createParamsFromAction';
import type { ParamListBase } from './types';
type Options = {
action: {
payload: {
name: string;
params?: object;
};
};
routeParamList: ParamListBase;
};
export function createRouteFromAction({ action, routeParamList }: Options) {
const { name } = action.payload;
return {
key: `${name}-${nanoid()}`,
name,
params: createParamsFromAction({ action, routeParamList }),
};
}

28
node_modules/@react-navigation/routers/src/index.tsx generated vendored Normal file
View File

@@ -0,0 +1,28 @@
import * as CommonActions from './CommonActions';
export { CommonActions };
export { BaseRouter } from './BaseRouter';
export type {
DrawerActionHelpers,
DrawerActionType,
DrawerNavigationState,
DrawerRouterOptions,
DrawerStatus,
} from './DrawerRouter';
export { DrawerActions, DrawerRouter } from './DrawerRouter';
export type {
StackActionHelpers,
StackActionType,
StackNavigationState,
StackRouterOptions,
} from './StackRouter';
export { StackActions, StackRouter } from './StackRouter';
export type {
TabActionHelpers,
TabActionType,
TabNavigationState,
TabRouterOptions,
} from './TabRouter';
export { TabActions, TabRouter } from './TabRouter';
export * from './types';

228
node_modules/@react-navigation/routers/src/types.tsx generated vendored Normal file
View File

@@ -0,0 +1,228 @@
import type * as CommonActions from './CommonActions';
export type CommonNavigationAction = CommonActions.Action;
export type NavigationRoute<
ParamList extends ParamListBase,
RouteName extends keyof ParamList,
> = Route<Extract<RouteName, string>, ParamList[RouteName]> & {
state?: NavigationState | PartialState<NavigationState>;
};
export type NavigationState<ParamList extends ParamListBase = ParamListBase> =
Readonly<{
/**
* Unique key for the navigation state.
*/
key: string;
/**
* Index of the currently focused route.
*/
index: number;
/**
* List of valid route names as defined in the screen components.
*/
routeNames: Extract<keyof ParamList, string>[];
/**
* Alternative entries for history.
*/
history?: unknown[];
/**
* List of rendered routes.
*/
routes: NavigationRoute<ParamList, keyof ParamList>[];
/**
* Custom type for the state, whether it's for tab, stack, drawer etc.
* During rehydration, the state will be discarded if type doesn't match with router type.
* It can also be used to detect the type of the navigator we're dealing with.
*/
type: string;
/**
* Whether the navigation state has been rehydrated.
*/
stale: false;
}>;
export type InitialState = Readonly<
Partial<Omit<NavigationState, 'stale' | 'routes'>> & {
routes: (Omit<Route<string>, 'key'> & { state?: InitialState })[];
}
>;
export type PartialRoute<R extends Route<string>> = Omit<R, 'key'> & {
key?: string;
state?: PartialState<NavigationState>;
};
export type PartialState<State extends NavigationState> = Partial<
Omit<State, 'stale' | 'routes'>
> &
Readonly<{
stale?: true;
routes: PartialRoute<Route<State['routeNames'][number]>>[];
}>;
export type Route<
RouteName extends string,
Params extends object | undefined = object | undefined,
> = Readonly<{
/**
* Unique key for the route.
*/
key: string;
/**
* User-provided name for the route.
*/
name: RouteName;
/**
* Path associated with the route.
* Usually present when the screen was opened from a deep link.
*/
path?: string;
}> &
(undefined extends Params
? Readonly<{
/**
* Params for this route
*/
params?: Readonly<Params>;
}>
: Readonly<{
/**
* Params for this route
*/
params: Readonly<Params>;
}>);
export type ParamListBase = Record<string, object | undefined>;
export type NavigationAction = Readonly<{
/**
* Type of the action (e.g. `NAVIGATE`)
*/
type: string;
/**
* Additional data for the action
*/
payload?: object;
/**
* Key of the route which dispatched this action.
*/
source?: string;
/**
* Key of the navigator which should handle this action.
*/
target?: string;
}>;
export type ActionCreators<Action extends NavigationAction> = {
[key: string]: (...args: any) => Action;
};
export type DefaultRouterOptions<RouteName extends string = string> = {
/**
* Name of the route to focus by on initial render.
* If not specified, usually the first route is used.
*/
initialRouteName?: RouteName;
};
export type RouterFactory<
State extends NavigationState,
Action extends NavigationAction,
RouterOptions extends DefaultRouterOptions,
> = (options: RouterOptions) => Router<State, Action>;
export type RouterConfigOptions = {
routeNames: string[];
routeParamList: ParamListBase;
routeGetIdList: Record<
string,
| ((options: { params?: Record<string, any> }) => string | undefined)
| undefined
>;
};
export type Router<
State extends NavigationState,
Action extends NavigationAction,
> = {
/**
* Type of the router. Should match the `type` property in state.
* If the type doesn't match, the state will be discarded during rehydration.
*/
type: State['type'];
/**
* Initialize the navigation state.
*
* @param options.routeNames List of valid route names as defined in the screen components.
* @param options.routeParamsList Object containing params for each route.
*/
getInitialState(options: RouterConfigOptions): State;
/**
* Rehydrate the full navigation state from a given partial state.
*
* @param partialState Navigation state to rehydrate from.
* @param options.routeNames List of valid route names as defined in the screen components.
* @param options.routeParamsList Object containing params for each route.
*/
getRehydratedState(
partialState: PartialState<State> | State,
options: RouterConfigOptions
): State;
/**
* Take the current state and updated list of route names, and return a new state.
*
* @param state State object to update.
* @param options.routeNames New list of route names.
* @param options.routeParamsList Object containing params for each route.
*/
getStateForRouteNamesChange(
state: State,
options: RouterConfigOptions & {
/**
* List of routes whose key has changed even if they still have the same name.
* This allows to remove screens declaratively.
*/
routeKeyChanges: string[];
}
): State;
/**
* Take the current state and key of a route, and return a new state with the route focused
*
* @param state State object to apply the action on.
* @param key Key of the route to focus.
*/
getStateForRouteFocus(state: State, key: string): State;
/**
* Take the current state and action, and return a new state.
* If the action cannot be handled, return `null`.
*
* @param state State object to apply the action on.
* @param action Action object to apply.
* @param options.routeNames List of valid route names as defined in the screen components.
* @param options.routeParamsList Object containing params for each route.
*/
getStateForAction(
state: State,
action: Action,
options: RouterConfigOptions
): State | PartialState<State> | null;
/**
* Whether the action should also change focus in parent navigator
*
* @param action Action object to check.
*/
shouldActionChangeFocus(action: NavigationAction): boolean;
/**
* Action creators for the router.
*/
actionCreators?: ActionCreators<Action>;
};