# All files / src/layout LayoutChildren.tsx

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

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
    204
    205
    206
    207
    208
    209
    210
    211
    212

     import type { HTMLAttributes, ReactElement, ReactNode } from "react";
    import { useEffect, useState } from "react";
    import type { SkipToMainContentProps } from "../link";
    import { SkipToMainContent } from "../link";
    import type { TreeData, TreeItemNode } from "../tree";
    import type { PropsWithRef } from "../types";
    import { LayoutAppBar } from "./LayoutAppBar";
    import type { LayoutMainProps } from "./LayoutMain";
    import { LayoutMain } from "./LayoutMain";
    import { LayoutNavigation } from "./LayoutNavigation";
    import { useLayoutConfig } from "./LayoutProvider";
    import { MiniLayoutWrapper } from "./MiniLayoutWrapper";
    import type {
      FlattenedLayoutComponentConfiguration,
      LayoutNavigationItem,
    } from "./types";
    import { isMiniLayout } from "./utils";

    /**
     * This used to just be the `LayoutProps` but was split up to help with mini
     * layouts.
     *
     * @remarks \@since 2.7.0
     */
    export interface LayoutChildrenProps<
      T extends TreeItemNode = LayoutNavigationItem
    > extends FlattenedLayoutComponentConfiguration<T> {
      /**
       * The base id to use for everything within the layout component. The `id`
       * will be applied to:
       *
       * - the `LayoutAppBar` as `${id}-header`
       * - the `AppBarTitle` as `${id}-title`
       * - the `LayoutNavToggle` as `${id}-nav-toggle`
       * - the `LayoutMain` element as `${id}-main`
       */
      id?: string;

      /**
       * Boolean if the main app bar should appear after the navigation component.
       * It is generally recommended to enable this prop if the navigation component
       * as a focusable element in the header since it will have a better tab focus
       * order.
       */
      navAfterAppBar?: boolean;

      /**
       * Any optional props to provide to the `<main>` element of the page.
       */
      mainProps?: PropsWithRef<LayoutMainProps, HTMLDivElement>;

      /**
       * Any additional props to provide to the `<SkipToMainLink />` link that is
       * automatically rendered in the layout.
       */
      skipProps?: Omit<SkipToMainContentProps, "mainId">;

      /**
       * 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>;

      /**
       * This prop allows you to provide additional props to the `<div>` surrounding
       * the `LayoutMain` and mini `LayoutNavigation` components.
       *
       * Note: This additional `<div>` will only be rendered if:
       * - at least one of the provided layout types are `mini`
       * - the layout is not using a fixed app bar
       * - the `miniNav` prop has not been defined
       * - `treeProps` have been provided
       *
       * @remarks
       * \@since 2.8.3
       * \@since 2.9.1 This will render if any provided layout type is `mini`.
       */
      miniWrapperProps?: PropsWithRef<
        HTMLAttributes<HTMLDivElement>,
        HTMLDivElement
      >;

      /**
       * The children to display within the layout. This is pretty much required
       * since you'll have an empty app otherwise, but it's left as optional just
       * for prototyping purposes.
       */
      children?: ReactNode;
    }

    /**
     * The only purpose of this component is to render the children and different
     * parts of the `Layout` depending on the current layout that is active. Since
     * the `Layout` component defines the provider itself, this has to be a child
     * component to get the resolved `layout` type.
     *
     * @remarks \@since 2.7.0
     * @internal
     */
    export function LayoutChildren<T extends TreeItemNode = LayoutNavigationItem>({
      id = "layout",
      appBar: propAppBar,
      appBarProps,
      customTitle,
      title,
      titleProps,
      navToggle,
      navToggleProps,
      navAfterAppBar = false,
      nav: propNav,
      miniNav,
      miniNavItems,
      miniWrapperProps,
      navHeader,
      navHeaderProps,
      navHeaderTitle,
      navHeaderTitleProps,
      closeNav,
      closeNavProps,
      treeProps,
      navProps,
      skipProps,
      mainProps,
      children,
    }: LayoutChildrenProps<T>): ReactElement {
      const mainId = mainProps?.id || `${id}-main`;
      const { layout, visible, appBarPosition } = useLayoutConfig();
      const mini = isMiniLayout(layout);
      const [miniHidden, setMiniHidden] = useState(visible);
      // when the layout changes, the hidden state for the mini drawer must also be
      // updated
      useEffect(() => {
        setMiniHidden(visible);
        // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [layout]);

      let appBar = propAppBar;
      if (typeof appBar === "undefined") {
        appBar = (
          <LayoutAppBar
            {...appBarProps}
            customTitle={customTitle}
            title={title}
            titleProps={titleProps}
            navToggle={navToggle}
            navToggleProps={navToggleProps}
          />
        );
      }

      let nav = propNav;
      if (typeof nav === "undefined") {
        nav = (
          <LayoutNavigation
            header={navHeader}
            headerProps={navHeaderProps}
            headerTitle={navHeaderTitle}
            headerTitleProps={navHeaderTitleProps}
            closeNav={closeNav}
            closeNavProps={closeNavProps}
            treeProps={treeProps}
            {...navProps}
            onEntered={(appearing) => {
              navProps?.onEntered?.(appearing);
              setMiniHidden(true);
            }}
            onExit={() => {
              navProps?.onExit?.();
              setMiniHidden(false);
            }}
          />
        );
      }

      return (
        <>
          <SkipToMainContent {...skipProps} mainId={mainId} />
          {navAfterAppBar && appBar}
          {nav}
          {!navAfterAppBar && appBar}
          <MiniLayoutWrapper
            mini={mini}
            miniNav={miniNav}
            miniHidden={miniHidden}
            containerProps={miniWrapperProps}
            miniNavItems={miniNavItems}
            treeProps={treeProps}
            header={navHeader}
            headerProps={navHeaderProps}
            headerTitle={navHeaderTitle}
            headerTitleProps={navHeaderTitleProps}
            closeNav={closeNav}
            closeNavProps={closeNavProps}
          >
            <LayoutMain
              headerOffset={!!appBarPosition}
              mini={mini}
              miniHidden={miniHidden}
              {...mainProps}
              id={mainId}
            >
              {children}
            </LayoutMain>
          </MiniLayoutWrapper>
        </>
      );
    }