# All files / src/layout MiniLayoutWrapper.tsx

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

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

     import { cnb } from "cnbuilder";
    import type { HTMLAttributes, ReactElement, ReactNode } from "react";
    import type { TreeData, TreeItemNode } from "../tree";
    import type { PropsWithRef } from "../types";
    import { bem } from "../utils";
    import { LayoutNavigation } from "./LayoutNavigation";
    import { useLayoutConfig } from "./LayoutProvider";
    import type { LayoutNavigationItem, LayoutNavigationProps } from "./types";

    const styles = bem("rmd-layout-mini-wrapper");

    /**
     * This is probably an internal only interface.
     *
     * @remarks \@since 2.8.3
     */
    export interface MiniLayoutWrapperProps<
      T extends TreeItemNode = LayoutNavigationItem
    > extends LayoutNavigationProps<T> {
      /**
       * Boolean if the current layout is one of the `mini` types.
       */
      mini: boolean;

      /**
       * A custom implementation for the main mini navigation component within the
       * `Layout`. If this is not `undefined`, it will be used instead of the
       * default implementation.
       *
       * Using this prop will make the following props do nothing for the mini nav:
       *
       * - `navProps`
       * - `navHeader`
       * - `navHeaderProps`
       * - `navHeaderTitle`
       * - `navHeaderTitleProps`
       * - `closeNav`
       * - `closeNavProps`
       * - `treeProps`
       *
       * @remarks \@since.2.7.0
       */
      miniNav?: ReactNode;

      /**
       * An optional tree to use for the mini navigation pane since the default
       * behavior of rendering mini tree items might hide content in an
       * undesirable way.
       *
       * @remarks \@since 2.7.0
       * @see {@link defaultMiniNavigationItemRenderer} for more information
       */
      miniNavItems?: TreeData<T>;

      /**
       * Boolean if the `mini` layout should be hidden. This should normally happen
       * after the non-mini layout becomes visible.
       */
      miniHidden: boolean;

      /**
       * Any additional props to provide to the wrapping `<div>`.
       */
      containerProps?: PropsWithRef<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;

      /**
       * The children to render. This should normally be the `LayoutMain` component.
       */
      children: ReactNode;
    }

    /**
     * This is probably an internal only component.
     *
     * @remarks \@since 2.8.3
     */
    export function MiniLayoutWrapper<
      T extends TreeItemNode = LayoutNavigationItem
    >(props: MiniLayoutWrapperProps<T>): ReactElement {
      const {
        mini,
        miniHidden,
        miniNav,
        miniNavItems,
        treeProps,
        header,
        headerProps,
        headerTitle,
        headerTitleProps,
        closeNav,
        closeNavProps,
        children,
        containerProps,
        ...remaining
      } = props;

      const { appBarPosition, isMiniable } = useLayoutConfig();
      if ((!mini && !isMiniable) || !treeProps || typeof miniNav !== "undefined") {
        return (
          <>
            {miniNav}
            {children}
          </>
        );
      }

      let miniTreeProps = treeProps;
      if (miniNavItems) {
        miniTreeProps = {
          ...miniTreeProps,
          navItems: miniNavItems,
        };
      }

      const miniNavigation = (
        <LayoutNavigation
          header={header}
          headerProps={headerProps}
          headerTitle={headerTitle}
          headerTitleProps={headerTitleProps}
          closeNav={closeNav}
          closeNavProps={closeNavProps}
          treeProps={miniTreeProps}
          {...remaining}
          mini
          sticky={!appBarPosition}
          hidden={miniHidden}
        />
      );

      if (appBarPosition) {
        return (
          <>
            {miniNavigation}
            {children}
          </>
        );
      }

      return (
        <div
          {...containerProps}
          className={cnb(
            !miniHidden && mini && styles(),
            containerProps?.className
          )}
        >
          {mini && miniNavigation}
          {children}
        </div>
      );
    }