# All files / src/main-layout useMainLayout.ts

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

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

     import { createContext, useContext } from "react";
    import type { NonNullMutableRef, UseStateInitializer } from "../types";

    /**
     * Due to the limited viewport, phones will always render as `"temporary"` or
     * `"temporary-mini"`.
     *
     * - `"temporary"`
     *   - show navigation in a sheet
     * - `"temporary-mini"`
     *   - show full navigation in a sheet
     *   - a mini variant of the navigation is always rendered
     * - `"toggleable"`
     *   -
     *
     * @remarks \@since 6.0.0
     */
    export type MainLayoutType =
      | "temporary"
      | "temporary-mini"
      | "toggleable"
      | "toggleable-mini"
      | "persistent"
      | "resizable"
      | "resizable-mini";

    /**
     * - `"full-height"`
     *   - the navigation panel will span the entire viewport height
     *   - the app bar's size will be updated so it is not covered by the
     *     navigation panel
     * - `"clipped"`
     *   - the navigation panel will be updated so it's height stops below the app
     *     bar
     * - `"static-app-bar"`
     *   - same behavior as `"clipped"`, but the main app bar will be updated so
     *     that scrolling the page will also "hide" the app bar by removing
     *     `position: fixed`.
     *
     * @remarks \@since 6.0.0
     */
    export type MainLayoutVariant = "full-height" | "clipped" | "static-app-bar";

    /**
     * @remarks \@since 6.0.0
     */
    export interface MainLayoutState {
      /**
       * This will be `true` when the temporary layout type is visible.
       */
      visible: boolean;

      /**
       * This will be `true` when the toggleable or resizable layout types are
       * expanded.
       */
      expanded: boolean;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface MainLayoutConfiguration {
      /**
       * @see {@link MainLayoutType}
       * @defaultValue `"temporary"`
       */
      layout?: MainLayoutType;

      /**
       * @see {@link MainLayoutVariant}
       * @defaultValue `"full-height"`
       */
      variant?: MainLayoutVariant;

      /**
       * If you want to store the expanded state in local storage, set this to some
       * value.
       *
       * @defaultValue `""`
       */
      expandedKey?: string;

      /**
       * @see {@link MainLayoutState.visible}
       * @defaultValue `false`
       */
      defaultVisible?: UseStateInitializer<boolean>;

      /**
       * @see {@link MainLayoutState.expanded}
       * @defaultValue `false`
       */
      defaultExpanded?: UseStateInitializer<boolean>;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface MainLayoutContext extends MainLayoutState {
      mini: boolean;
      navId: string;
      mainId: string;
      variant: MainLayoutVariant;
      layout: MainLayoutType;
      currentLayout: MainLayoutType;

      /**
       * When the layout is `temporary` (or a phone), this will toggle the
       * visibility of the navigation panel. Otherwise this will toggle the expanded
       * state for the navigation panel.
       */
      toggleNavigation(): void;
      expandNavigation(): void;
      collapseNavigation(): void;
      showTemporaryNavigation(): void;
      hideTemporaryNavigation(): void;

      /**
       * This is used so that the sheet does not animate when:
       *
       * - the current layout changes due to resizing the browser
       * - the layout changes due to rehydrating with the client and local storage
       */
      disableTemporaryTransition: NonNullMutableRef<boolean>;
      disablePersistentTransition: NonNullMutableRef<boolean>;
    }

    const context = createContext<MainLayoutContext | undefined>(undefined);

    /**
     * @remarks \@since 6.0.0
     */
    export const { Provider: MainLayoutProvider } = context;

    /**
     * @remarks \@since 6.0.0
     */
    export function useMainLayout(): MainLayoutContext {
      const value = useContext(context);
      if (!value) {
        throw new Error("The `MainLayoutProvider` is not a parent component.");
      }

      return value;
    }