# All files / layout LayoutNav.tsx

100% Statements 203/203
50% Branches 1/2
100% Functions 1/1
100% Lines 203/203

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
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    2041x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    1x
     "use client";
    import { cnb } from "cnbuilder";
    import type { HTMLAttributes, ReactNode } from "react";
    import { forwardRef } from "react";
    import {
      DEFAULT_SHEET_CLASSNAMES,
      DEFAULT_SHEET_TIMEOUT,
    } from "../sheet/Sheet.js";
    import type { BaseSheetClassNameOptions } from "../sheet/styles.js";
    import { sheet } from "../sheet/styles.js";
    import type {
      CSSTransitionClassNames,
      TransitionActions,
      TransitionCallbacks,
      TransitionTimeout,
    } from "../transition/types.js";
    import { useCSSTransition } from "../transition/useCSSTransition.js";
    import { layoutNav } from "./layoutNavStyles.js";

    /**
     * @remarks \@since 6.0.0
     */
    export interface LayoutNavProps
      extends HTMLAttributes<HTMLDivElement>,
        BaseSheetClassNameOptions,
        TransitionCallbacks,
        TransitionActions {
      children: ReactNode;

      /**
       * The component to render as.
       *
       * @defaultValue `"nav"`
       */
      as?: "nav" | "div";

      /**
       * Set this to `true` to display the navigation.
       */
      expanded: boolean;

      /**
       * Set this to `true` to force the navigation to appear below the fixed app
       * bar.
       *
       * @defaultValue `false`
       */
      appBarOffset?: boolean;

      /** @defaultValue {@link DEFAULT_SHEET_TIMEOUT} */
      timeout?: TransitionTimeout;
      /** @defaultValue {@link DEFAULT_SHEET_CLASSNAMES} */
      classNames?: CSSTransitionClassNames;
    }

    /**
     * **Client Component**
     *
     * This component is used to render a persistent navigation on the left of the
     * page. There is some built-in functionality to support resizing with the
     * `LayoutWindowSplitter` and expandable layouts. Do not use this component for
     * temporary navigation and instead use a `Sheet`.
     *
     * @example
     * Full Height Layout
     * ```tsx
     * "use client";
     * import {
     *   LayoutAppBar,
     *   LayoutNav,
     *   Main,
     *   useHorizontalLayoutTransition,
     * } from "@react-md/core";
     * import type { ReactElement, PropsWithChildren } from "react";
     *
     * function Layout({ children }: PropsWithChildren): ReactElement {
     *   const { elementProps } = useHorizontalLayoutTransition({
     *     transitionIn: true,
     *   });
     *
     *   return (
     *     <>
     *       <LayoutAppBar {...elementProps}>
     *         <YourAppBarContent />
     *       </LayoutAppBar>
     *       <LayoutNav expanded>
     *         <YourNavigationComponent />
     *       </LayoutNav>
     *       <Main navOffset appBarOffset {...elementProps}>
     *         {children}
     *       </Main>
     *     </>
     *   );
     * }
     * ```
     *
     * @example
     * Toggleable Layout
     * ```tsx
     * "use client";
     * import {
     *   LayoutNav,
     *   Main,
     *   useHorizontalLayoutTransition,
     *   useToggle,
     * } from "@react-md/core";
     * import MenuIcon from "@react-md/material-icons/MenuIcon";
     * import { cnb } from "cnbuilder";
     * import type { ReactElement, PropsWithChildren } from "react";
     *
     * function Layout({ children }: PropsWithChildren): ReactElement {
     *   const { toggled: expanded, toggle } = useToggle();
     *   const { elementProps } = useHorizontalLayoutTransition({
     *     transitionIn: expanded,
     *   });
     *
     *   return (
     *     <>
     *       <LayoutAppBar {...elementProps}>
     *         <Button
     *           aria-label="Navigation"
     *           onClick={toggle}
     *           buttonType="icon"
     *         >
     *           <MenuIcon />
     *         </Button>
     *         <YourAppBarContent />
     *       </LayoutAppBar>
     *       <LayoutNav expanded>
     *         <YourNavigationComponent />
     *       </LayoutNav>
     *       <Main navOffset={expanded} appBarOffset {...elementProps}>
     *         {children}
     *       </Main>
     *     </>
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export const LayoutNav = forwardRef<HTMLDivElement, LayoutNavProps>(
      function LayoutNav(props, ref) {
        const {
          as: Component = "nav",
          "aria-labelledby": ariaLabelledBy,
          "aria-label": ariaLabel = Component == "nav" && !ariaLabelledBy
            ? "Navigation"
            : undefined,
          expanded,
          children,
          className,
          timeout = DEFAULT_SHEET_TIMEOUT,
          classNames = DEFAULT_SHEET_CLASSNAMES,
          appear,
          enter,
          exit,
          onEnter,
          onEntering,
          onEntered,
          onExit,
          onExited,
          onExiting,
          appBarOffset,
          ...remaining
        } = props;
        const { elementProps } = useCSSTransition({
          nodeRef: ref,
          timeout,
          className: cnb(
            layoutNav({ appBarOffset }),
            sheet({
              className,
              horizontalSize: "none",
              disableOverlay: true,
            })
          ),
          classNames,
          enter,
          exit,
          appear,
          onEnter,
          onEntering,
          onEntered,
          onExit,
          onExited,
          onExiting,
          exitedHidden: true,
          transitionIn: expanded,
        });

        return (
          <Component
            aria-label={ariaLabel}
            aria-labelledby={ariaLabelledBy}
            {...remaining}
            {...elementProps}
          >
            {children}
          </Component>
        );
      }
    );