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

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

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

     import type { HTMLAttributes, ReactElement, ReactNode } from "react";
    import type { SkipToMainContentProps } from "../link";
    import { SkipToMainContent } from "../link";
    import type { PropsWithRef } from "../types";
    import { useEnsuredId } from "../useEnsuredId";
    import { Main } from "./Main";
    import type { MainLayoutAppBarProps } from "./MainLayoutAppBar";
    import { MainLayoutAppBar } from "./MainLayoutAppBar";
    import type { MainLayoutTitleProps } from "./MainLayoutTitle";
    import type { MainNavigationProps } from "./MainNavigation";
    import { MainNavigation } from "./MainNavigation";
    import type { ConfigurableMainLayoutNavigationButtonProps } from "./MainLayoutNavigationButton";
    import type { MainLayoutConfiguration } from "./useMainLayout";
    import { MainLayoutProvider } from "./useMainLayout";
    import { useMainLayoutProvider } from "./useMainLayoutProvider";

    export interface MainLayoutProps extends MainLayoutConfiguration {
      children: ReactNode;

      navProps?: PropsWithRef<Partial<MainNavigationProps>, HTMLElement>;
      navHeader?: ReactNode;
      navFooter?: ReactNode;

      /**
       * This is just a convenience prop for `navProps.children`.
       *
       * @example
       * ```tsx
       * // these two are the same
       * navProps={{
       *   children: <Tree {...props} />,
       * }}
       * navChildren={<Tree {...props} />}
       * ```
       */
      navChildren?: ReactNode;

      mainProps?: PropsWithRef<HTMLAttributes<HTMLElement>, HTMLElement>;

      skipProps?: PropsWithRef<
        Omit<SkipToMainContentProps, "mainId">,
        HTMLAnchorElement
      >;

      /**
       * @see {@link appBarTitle}
       * @see {@link appBarTitleProps}
       * @see {@link appBarChildren}
       * @see {@link navigationButtonProps}
       */
      appBarProps?: PropsWithRef<MainLayoutAppBarProps, HTMLDivElement>;

      /**
       * This is just a convenience prop for `appBarProps.children`
       *
       * @example
       * ```tsx
       * // these two are the same
       * appBarProps={{
       *   children: <Button>Button</Button>,
       * }}
       * appBarChildren={<Button>Button</Button>}
       * ```
       */
      appBarChildren?: ReactNode;

      /**
       * This is just a convenience prop for `appBarProps.title`.
       *
       * @see {@link MainLayoutAppBarProps.title}
       *
       * @example
       * ```tsx
       * // these two are the same
       * appBarProps={{
       *   title: <>Some Title</>,
       * }}
       * appBarTitle={<>Some Title</>}
       * ```
       */
      appBarTitle?: ReactNode;

      /**
       * This is just a convenience prop for `appBarProps.titleProps`
       *
       * @see {@link MainLayoutTitleProps}
       *
       * @example
       * ```tsx
       * // these two are the same
       * appBarProps={{
       *   titleProps: <Button>Button</Button>,
       * }}
       * appBarTitleProps={<Button>Button</Button>}
       * ```
       */
      appBarTitleProps?: PropsWithRef<MainLayoutTitleProps, HTMLHeadingElement>;

      /**
       * This is just a convenience prop for `appBarProps.children`
       *
       * @example
       * ```tsx
       * // these two are the same
       * appBarProps={{
       *   navigationButtonProps: {
       *     "aria-label": "Custom Label",
       *   },
       * }}
       * navigationButtonProps={{
       *   "aria-label": "Custom Label",
       * }}
       * ```
       */
      navigationButtonProps?: ConfigurableMainLayoutNavigationButtonProps;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export function MainLayout(props: MainLayoutProps): ReactElement {
      const {
        children,
        appBarProps,
        appBarChildren,
        appBarTitle,
        appBarTitleProps,
        navProps,
        navHeader,
        navFooter,
        navChildren,
        skipProps,
        mainProps,
        layout = "temporary",
        variant = "full-height",
        expandedKey = "",
        defaultVisible = false,
        defaultExpanded = false,
        navigationButtonProps,
      } = props;
      const navId = useEnsuredId(navProps?.id, "nav");
      const mainId = useEnsuredId(mainProps?.id, "main");
      const context = useMainLayoutProvider({
        navId,
        mainId,
        layout,
        variant,
        expandedKey,
        defaultVisible,
        defaultExpanded,
      });

      return (
        <MainLayoutProvider value={context}>
          <SkipToMainContent {...skipProps} mainId={mainId} />
          <MainLayoutAppBar
            title={appBarTitle}
            titleProps={appBarTitleProps}
            position={variant === "static-app-bar" ? "static" : "fixed"}
            navigationButtonProps={navigationButtonProps}
            {...appBarProps}
          >
            {appBarChildren || appBarProps?.children}
          </MainLayoutAppBar>
          <MainNavigation
            header={navHeader}
            footer={navFooter}
            {...navProps}
            id={navId}
          >
            {navProps?.children || navChildren}
          </MainNavigation>
          <Main {...mainProps} id={mainId}>
            {children}
          </Main>
        </MainLayoutProvider>
      );
    }