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

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

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
    163
    164
    165
    166
    167
    168

     import { cnb } from "cnbuilder";
    import type { HTMLAttributes, ReactElement, ReactNode } from "react";
    import { lazy, Suspense, useCallback, useMemo, useState } from "react";
    import { useAppSize } from "../AppSizeProvider";
    import { getScrollbarWidth } from "../scroll";
    import { Sheet } from "../sheet";
    import { useCSSVariables } from "../theme";
    import type { CssPosition } from "../types";
    import { useResizeObserver } from "../useResizeObserver";
    import { bem } from "../utils";
    import { useMainLayoutContext } from "./MainLayoutProvider";

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

    const styles = bem("rmd-layout-nav");

    export interface MainNavigationClassNameOptions {
      className?: string;
      mini?: boolean;
      shadow?: boolean;
      position?: CssPosition;
      appBarPosition?: CssPosition;
    }

    function mainNavigation(options: MainNavigationClassNameOptions): string {
      const { className, mini, position, appBarPosition, shadow } = options;
      const fixed = position === "fixed";

      return cnb(
        styles({
          mini,
          sticky: position !== "static",
          fixed,
          shadow,
          offset: appBarPosition === "fixed" && !fixed,
        }),
        className
      );
    }

    export interface MainNavigationProps extends HTMLAttributes<HTMLElement> {
      id: string;
      header?: ReactNode;
      footer?: ReactNode;
      resizable?: boolean;
      miniChildren?: ReactNode;
    }

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

      const {
        mini,
        layout,
        visible,
        resizable,
        appBarPosition,
        navigationPosition,
        hideTemporaryNavigation,
        disableSheetTransition,
      } = useMainLayoutContext();
      const { isPhone } = useAppSize();

      const persistent = mini || !layout.includes("temporary");
      const [scrollbarSize, setScrollbarSize] = useState(0);
      const navRef = useResizeObserver({
        // Note: the default behavior would only trigger the scrollbar size update
        // if the `navigationPosition` is set to `fixed`. Since a user might
        // manually set the height/max-height through CSS, I can't disable it when
        // `navigationPosition !== "fixed"`.
        disabled: isPhone || !mini,
        onUpdate: useCallback((entry) => {
          if (
            entry.borderBoxSize[0].inlineSize > entry.contentBoxSize[0].inlineSize
          ) {
            setScrollbarSize(getScrollbarWidth());
          } else {
            setScrollbarSize(0);
          }
        }, []),
      });
      useCSSVariables(
        useMemo(() => {
          if (scrollbarSize === 0) {
            return [];
          }

          return [
            {
              name: "--rmd-layout-mini-scrollbar",
              value: `${scrollbarSize}px`,
            },
          ];
        }, [scrollbarSize])
      );

      return (
        <>
          <Sheet
            id={`${id}-sheet`}
            visible={visible}
            position="left"
            aria-label="Navigation"
            exitedHidden
            temporary={false}
            onRequestClose={hideTemporaryNavigation}
            disablePortal
            disableTransition={disableSheetTransition.current}
            onExited={() => {
              disableSheetTransition.current = false;
            }}
            onEntered={() => {
              disableSheetTransition.current = false;
            }}
          >
            {header}
            <nav {...remaining} id={id} className={mainNavigation({ className })}>
              {children}
            </nav>
            {footer}
          </Sheet>
          {persistent && (
            <nav
              {...remaining}
              id={`${id}-persistent`}
              ref={navRef}
              hidden={!mini && isPhone}
              className={cnb(
                // "rmd-layout-nav",
                // "rmd-layout-nav--shadow",
                // !isPhone && layout === "full-height" && "rmd-layout-nav--fixed",
                // className,
                mainNavigation({
                  className,
                  // might need a var for elevation
                  shadow: true,
                  mini,
                  position: navigationPosition,
                  appBarPosition,
                })
              )}
            >
              {mini && typeof miniChildren !== "undefined"
                ? miniChildren
                : children}
              {resizable && layout !== "temporary" && (
                <Suspense fallback={null}>
                  <MainNavWindowSplitter />
                </Suspense>
              )}
            </nav>
          )}
        </>
      );
    }