# All files / src/layout useResizableLayout.ts

100% Statements 150/150
100% Branches 3/3
100% Functions 1/1
100% Lines 150/150

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
    1511x
    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
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
     import { useEnsuredId } from "../useEnsuredId.js";
    import { DISPLAY_NONE_CLASS } from "../utils/isElementVisible.js";
    import { type LayoutWindowSplitterProps } from "./LayoutWindowSplitter.js";
    import {
      type ExpandableLayoutImplementation,
      type ExpandableLayoutOptions,
      type ProvidedLayoutNavProps,
    } from "./useExpandableLayout.js";
    import { useExpandableLayout } from "./useExpandableLayout.js";
    import {
      useLayoutWindowSplitter,
      type LayoutWindowSplitterOptions,
    } from "./useLayoutWindowSplitter.js";

    /**
     * @remarks \@since 6.0.0
     */
    export interface ResizableLayoutOptions
      extends ExpandableLayoutOptions,
        LayoutWindowSplitterOptions {
      /**
       * This id will be used as the `aria-controls` prop for the
       * `LayoutWindowSplitter` and should be applied to the `LayoutNav` as an `id`.
       *
       * @defaultValue `"layout-nav" + useId()`
       */
      navId?: string;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface ProvidedResizableLayoutNavProps
      extends ProvidedLayoutNavProps {
      id: string;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface ResizableLayoutImplementation
      extends ExpandableLayoutImplementation {
      expandableNavProps: ProvidedResizableLayoutNavProps;
      windowSplitterProps: LayoutWindowSplitterProps;
    }

    /**
     * @example
     * Main USage
     * ```tsx
     * import {
     *   AppBarTitle,
     *   Button,
     *   LayoutAppBar,
     *   LayoutNav,
     *   LayoutWindowSplitter,
     *   Main,
     *   Sheet,
     *   useResizableLayout,
     * } from "@react-md/core";
     * import type { ReactElement, ReactNode } from "react";
     *
     * import { CustomNavigation } from "./CustomNavigation";
     *
     * export interface LayoutProps {
     *   children: ReactNode;
     * }
     *
     * export function Layout(props: LayoutProps): ReactElement {
     *   const { children } = props;
     *
     *   // choose whichever one for your app
     *   // nextjs app dir
     *   const pathname = usePathname();
     *   // nextjs pages
     *   const { pathname } = useRouter();
     *   // react router
     *   const { pathname } = useHistory();
     *
     *   const {
     *     isPhone,
     *     appBarProps,
     *     expandableNavProps,
     *     mainProps,
     *     navToggleProps,
     *     temporaryNavProps,
     *     windowSplitterProps,
     *   } = useResizableLayout({ pathname });
     *
     *   return {
     *     <>
     *       <LayoutAppBar {...appBarProps}>
     *         <Button {...navToggleProps} />
     *         <AppBarTitle>Hello, world!</AppBarTitle>
     *       </LayoutAppBar>
     *       <LayoutNav {...expandableNavProps}>
     *         <CustomNavigation />
     *       </LayoutNav>
     *       <LayoutWindowSplitter {...windowSplitterProps} />
     *       {isPhone && (
     *         <Sheet {...temporaryNavProps}>
     *           <CustomNavigation />
     *         </Sheet>
     *       )}
     *       <Main {...mainProps}>{children}</Main>
     *     </>
     *   }
     * }
     * ```
     * @remarks \@since 6.0.0
     */
    export function useResizableLayout(
      options: ResizableLayoutOptions
    ): ResizableLayoutImplementation {
      const {
        navId: propNavId,
        pathname,
        appBarPosition = "fixed",
        fullHeightNav = false,
        defaultVisible,
        defaultExpanded,
        ...splitterOptions
      } = options;
      const navId = useEnsuredId(propNavId, "layout-nav");
      const { splitterProps, ...windowSplitter } =
        useLayoutWindowSplitter(splitterOptions);
      const expandableLayout = useExpandableLayout({
        pathname,
        appBarPosition,
        fullHeightNav,
        defaultExpanded,
        defaultVisible,
      });
      const { expandableNavProps, expanded } = expandableLayout;

      return {
        ...expandableLayout,
        ...windowSplitter,
        expandableNavProps: {
          id: navId,
          ...expandableNavProps,
        },
        windowSplitterProps: {
          ...splitterProps,
          "aria-controls": navId,
          appBarOffset: !fullHeightNav,
          className: (!expanded && DISPLAY_NONE_CLASS) || undefined,
        },
      };
    }