# All files / src/main-layout MainNavigation.tsx

0% Statements 0/161
0% Branches 0/1
0% Functions 0/1
0% Lines 0/161

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162

     import { cnb } from "cnbuilder";
    import type { HTMLAttributes, ReactElement, ReactNode } from "react";
    import { lazy } from "react";
    import { useAppSize } from "../AppSizeProvider";
    import { NoSsr } from "../NoSsr";
    import type { SheetProps } from "../sheet";
    import { Sheet } from "../sheet";
    import { NullSuspense } from "../suspense";
    import { useMainLayout } from "./useMainLayout";

    const noop = (): void => {
      // do nothing
    };

    const alwaysTrue = (): boolean => true;

    const MainNavWindowSplitter = lazy(() =>
      import("./MainNavWindowSplitter").then(({ MainNavWindowSplitter }) => ({
        default: MainNavWindowSplitter,
      }))
    );

    export type MainNavigationSheetProps = Omit<
      SheetProps,
      "visible" | "onRequestClose" | "children"
    >;

    export interface MainNavigationProps extends HTMLAttributes<HTMLElement> {
      id: string;
      header?: ReactNode;
      footer?: ReactNode;
      temporarySheetProps?: MainNavigationSheetProps;
      persistentSheetProps?: MainNavigationSheetProps;
      miniChildren?: ReactNode;
    }

    export function MainNavigation(props: MainNavigationProps): ReactElement {
      const {
        id,
        header,
        footer,
        temporarySheetProps,
        persistentSheetProps,
        className,
        children,
        miniChildren,
        ...remaining
      } = props;

      const temporaryOnEntered = temporarySheetProps?.onEntered || noop;
      const temporaryOnExited = temporarySheetProps?.onExited || noop;
      const persistentOnEntered = persistentSheetProps?.onEntered || noop;
      const persistentOnExited = persistentSheetProps?.onExited || noop;
      const { isPhone } = useAppSize();

      const {
        mini,
        layout,
        variant,
        visible,
        expanded,
        collapseNavigation,
        disableTemporaryTransition,
        disablePersistentTransition,
        hideTemporaryNavigation,
      } = useMainLayout();
      const isTemporary = layout === "temporary";
      const isResizable = layout === "resizable" || layout === "resizable-mini";
      const isFullyHeight = variant === "full-height";

      return (
        <>
          <Sheet
            id={`${id}-temporary-sheet`}
            position="left"
            aria-label="Navigation"
            {...temporarySheetProps}
            className={cnb(
              "rmd-layout-sheet rmd-layout-sheet--temporary",
              temporarySheetProps?.className
            )}
            visible={visible}
            onRequestClose={hideTemporaryNavigation}
            disableTransition={
              temporarySheetProps?.disableTransition ||
              disableTemporaryTransition.current
            }
            onEntered={(appearing) => {
              temporaryOnEntered(appearing);
              disableTemporaryTransition.current = false;
            }}
            onExited={() => {
              temporaryOnExited();
              disableTemporaryTransition.current = false;
            }}
          >
            {header}
            <nav
              {...remaining}
              id={`${id}-temporary`}
              className={cnb("rmd-layout-nav", className)}
            >
              {children}
            </nav>
            {footer}
          </Sheet>
          {!isPhone && !isTemporary && (
            <Sheet
              id={`${id}-persistent-sheet`}
              position="left"
              aria-label="Navigation"
              temporary={false}
              exitedHidden
              disablePortal
              disableOverlay
              disableScrollLock
              isFocusTypeDisabled={alwaysTrue}
              {...persistentSheetProps}
              disableTransition={
                persistentSheetProps?.disableTransition ||
                disablePersistentTransition.current
              }
              onEntered={(appearing) => {
                persistentOnEntered(appearing);
                disablePersistentTransition.current = false;
              }}
              onExited={() => {
                persistentOnExited();
                disablePersistentTransition.current = false;
              }}
              visible={expanded || mini}
              onRequestClose={collapseNavigation}
              className={cnb(
                "rmd-layout-sheet rmd-layout-sheet--persistent",
                mini && "rmd-layout-sheet--mini",
                isResizable && expanded && "rmd-layout-sheet--resizable",
                !isFullyHeight && "rmd-layout-sheet--offset",
                persistentSheetProps?.className
              )}
            >
              <nav
                {...remaining}
                id={`${id}-persistent`}
                className={cnb("rmd-layout-nav", className)}
              >
                {mini && typeof miniChildren !== "undefined"
                  ? miniChildren
                  : children}
                {isResizable && (
                  <NoSsr>
                    <NullSuspense>
                      <MainNavWindowSplitter />
                    </NullSuspense>
                  </NoSsr>
                )}
              </nav>
            </Sheet>
          )}
        </>
      );
    }