# All files / layout useTemporaryLayout.ts

100% Statements 168/168
100% Branches 3/3
100% Functions 2/2
100% Lines 168/168

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
    1691x
    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
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    13x
    13x
    13x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    72x
    1x
    1x
    72x
    72x
    72x
     "use client";
    import type { PropsWithChildren } from "react";
    import { useEffect } from "react";
    import type { AppBarProps } from "../app-bar/AppBar.js";
    import type { ButtonProps } from "../button/Button.js";
    import { useIcon } from "../icon/IconProvider.js";
    import type { SheetProps } from "../sheet/Sheet.js";
    import type { CssPosition, UseStateInitializer } from "../types.js";
    import { useToggle } from "../useToggle.js";
    import type { MainProps } from "./Main.js";

    export interface TemporaryLayoutOptions {
      /**
       * This is used to automatically hide the temporary navigation whenever the
       * route changes. Set this to `null` if you want to handle closing yourself.
       */
      pathname: string | null;

      /** @defaultValue `"fixed"` */
      appBarPosition?: CssPosition;

      /** @defaultValue `false` */
      defaultVisible?: UseStateInitializer<boolean>;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export type ProvidedLayoutNavToggleProps = PropsWithChildren<
      Pick<Required<ButtonProps>, "aria-label" | "buttonType" | "onClick">
    >;

    /**
     * @remarks \@since 6.0.0
     */
    export type ProvidedTemporaryLayoutNavProps = Pick<
      Required<SheetProps>,
      "aria-label" | "visible" | "onRequestClose"
    >;

    /**
     * @remarks \@since 6.0.0
     */
    export type ProvidedTemporaryLayoutMainProps = Required<
      Pick<MainProps, "appBarOffset">
    >;

    /**
     * @remarks \@since 6.0.0
     */
    export type ProvidedTemporaryLayoutAppBarProps = Required<
      Pick<AppBarProps, "position">
    >;

    /**
     * @remarks \@since 6.0.0
     */
    export interface TemporaryLayoutImplementation {
      visible: boolean;
      showTemporaryNav(): void;
      hideTemporaryNav(): void;
      appBarProps: ProvidedTemporaryLayoutAppBarProps;
      mainProps: ProvidedTemporaryLayoutMainProps;
      navToggleProps: ProvidedLayoutNavToggleProps;
      temporaryNavProps: ProvidedTemporaryLayoutNavProps;
    }

    /**
     * @example
     * ```tsx
     * import {
     *   AppBar,
     *   AppBarTitle,
     *   Button,
     *   Main,
     *   Sheet,
     *   useTemporaryLayout,
     * } from "@react-md/core";
     * import type { ReactElement, ReactNode } from "react";
     *
     * import { CustomNavigation } from "./CustomNavigation.jsx";
     *
     * 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 {
     *     mainProps,
     *     navToggleProps,
     *     temporaryNavProps,
     *   } = useTemporaryLayout({ pathname });
     *
     *   return (
     *     <>
     *       <AppBar position="fixed">
     *         <Button {...navToggleProps} />
     *         <AppBarTitle>Hello, world!</AppBarTitle>
     *       </AppBar>
     *       <Main {...mainProps}>{children}</Main>
     *       <Sheet {...temporaryNavProps}>
     *         <CustomNavigation />
     *       </Sheet>
     *     </>
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export function useTemporaryLayout(
      options: TemporaryLayoutOptions
    ): TemporaryLayoutImplementation {
      const {
        pathname,
        defaultVisible = false,
        appBarPosition = "fixed",
      } = options;

      const menuIcon = useIcon("menu");
      const {
        toggled: visible,
        enable: showTemporaryNav,
        disable: hideTemporaryNav,
      } = useToggle(defaultVisible);

      // hide the temporary nav whenever the route changes
      useEffect(() => {
        if (pathname !== null) {
          hideTemporaryNav();
        }
      }, [hideTemporaryNav, pathname]);

      return {
        visible,
        showTemporaryNav,
        hideTemporaryNav,
        appBarProps: {
          position: appBarPosition,
        },
        mainProps: {
          appBarOffset: appBarPosition !== "static",
        },
        temporaryNavProps: {
          "aria-label": "Navigation",
          visible,
          onRequestClose: hideTemporaryNav,
        },
        navToggleProps: {
          "aria-label": "Navigation",
          buttonType: "icon",
          children: menuIcon,
          onClick() {
            showTemporaryNav();
          },
        },
      };
    }