@react-navigation/native#CommonNavigationAction TypeScript Examples

The following examples show how to use @react-navigation/native#CommonNavigationAction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BottomSheetRouter.tsx    From reanimated-bottom-sheet-navigator with MIT License 4 votes vote down vote up
export default function BottomSheetRouter({
  defaultSnap,
  snapPoints,
  ...rest
}: BottomSheetRouterOptions): Router<
  BottomSheetNavigationState,
  BottomSheetActionType | CommonNavigationAction
> {
  const router = (TabRouter(rest) as unknown) as Router<
    BottomSheetNavigationState,
    TabActionType | CommonNavigationAction
  >;

  return {
    ...router,

    type: 'bottom-sheet',

    getInitialState({ routeNames, routeParamList }) {
      let state = router.getInitialState({ routeNames, routeParamList });

      if (
        typeof defaultSnap === 'number' &&
        defaultSnap !== snapPoints.length - 1
      ) {
        state = snapBottomSheet(state, defaultSnap);
      }

      return {
        ...state,
        stale: false,
        type: 'bottom-sheet',
        key: `bottom-sheet-${nanoid()}`,
      };
    },

    getRehydratedState(partialState, { routeNames, routeParamList }) {
      if (partialState.stale === false) {
        return partialState;
      }

      let state = router.getRehydratedState(partialState, {
        routeNames,
        routeParamList,
      });

      const sheetState = partialState.history?.find(
        (it) => it.type === 'bottom-sheet'
      ) as { type: 'bottom-sheet'; point: number } | undefined;

      if (sheetState) {
        state = snapBottomSheet(state, sheetState.point);
      }

      return {
        ...state,
        type: 'bottom-sheet',
        key: `bottom-sheet-${nanoid()}`,
      };
    },

    getStateForRouteFocus(state, key) {
      const result = router.getStateForRouteFocus(state, key);

      if (
        typeof defaultSnap === 'number' &&
        defaultSnap !== snapPoints.length - 1
      ) {
        state = snapBottomSheet(state, defaultSnap);
      }

      return closeBottomSheet(result);
    },

    getStateForAction(state, action, options) {
      switch (action.type) {
        case 'OPEN_SHEET':
          return snapBottomSheet(state, 0);

        case 'CLOSE_SHEET':
          return closeBottomSheet(state);

        case 'SNAP_SHEET':
          if (action.payload.point === snapPoints.length - 1) {
            return closeBottomSheet(state);
          }

          return snapBottomSheet(state, action.payload.point);

        case 'GO_BACK':
          if (
            typeof defaultSnap === 'number' &&
            defaultSnap !== snapPoints.length - 1
          ) {
            if (!isBottomSheetOpen(state)) {
              return snapBottomSheet(state, 0);
            }
          } else {
            if (isBottomSheetOpen(state)) {
              return closeBottomSheet(state);
            }
          }

          return router.getStateForAction(state, action, options);

        default:
          return router.getStateForAction(state, action, options);
      }
    },

    actionCreators: BottomSheetActions,
  };
}