# All files / src/menu MenuConfigurationProvider.tsx

100% Statements 184/184
100% Branches 13/13
100% Functions 2/2
100% Lines 184/184

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
    1851x
    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
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1323x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    92x
    92x
    92x
    92x
    92x
    92x
    92x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
    595x
     "use client";
    import {
      createContext,
      useContext,
      useMemo,
      type ReactElement,
      type ReactNode,
    } from "react";
    import { type SheetPosition, type SheetVerticalSize } from "../sheet/styles.js";

    /** @remarks \@since 5.0.0 */
    export interface MenuOrientationProps {
      /**
       * Boolean if the menu should be rendered horizontally instead of vertically.
       * This will also update the `aria-orientation`.
       *
       * @defaultValue `false`
       */
      horizontal?: boolean;
    }

    /**
     * This allows the menu to be conditionally rendered as a `Sheet` instead of a
     * menu.
     *
     * - `false` - always render as a `Menu`
     * - `true` - always render as a `Sheet`
     * - `"phone"` - render as a sheet only when the {@link AppSize} is considered
     *   phone (`isPhone === true`).
     *
     * @defaultValue `false`
     * @remarks \@since 5.0.0
     */
    export type RenderMenuAsSheet = boolean | "phone";

    /** @remarks \@since 5.0.0 */
    export interface MenuConfiguration extends MenuOrientationProps {
      /** {@inheritDoc RenderMenuAsSheet} */
      renderAsSheet?: RenderMenuAsSheet;

      /**
       * @see {@link SheetPosition}
       * @defaultValue `"bottom"`
       */
      sheetPosition?: SheetPosition;

      /**
       * @see {@link SheetVerticalSize}
       * @defaultValue `"touch"`
       */
      sheetVerticalSize?: SheetVerticalSize;

      /**
       * Any children to render above the sheet's menu implementation. This would
       * normally be something like a `<DialogHeader>` or `AppBar`.
       *
       * @defaultValue `null`
       */
      sheetHeader?: ReactNode;

      /**
       * Any children to render below the sheet's menu implementation. This would
       * normally be something like a `<DialogFooter>`.
       *
       * @defaultValue `null`
       */
      sheetFooter?: ReactNode;
    }

    /** @remarks \@since 5.0.0 */
    export type MenuConfigurationContext = Required<MenuConfiguration>;

    /** @remarks \@since 5.0.0 */
    export const DEFAULT_MENU_CONFIGURATION: Readonly<MenuConfiguration> = {
      horizontal: false,
      renderAsSheet: false,
      sheetHeader: null,
      sheetFooter: null,
      sheetPosition: "bottom",
      sheetVerticalSize: "touch",
    };

    /**
     * @internal
     * @remarks \@since 5.0.0
     */
    const context = createContext<MenuConfigurationContext>({
      horizontal: false,
      renderAsSheet: false,
      sheetHeader: null,
      sheetFooter: null,
      sheetPosition: "bottom",
      sheetVerticalSize: "touch",
    });
    context.displayName = "MenuConfiguration";

    /**
     * @internal
     * @remarks \@since 5.0.0
     */
    const { Provider } = context;

    /**
     * This is probably just an internal only hook that allows you to get the
     * {@link MenuConfigurationContext} with optional overrides.
     *
     * @param overrides - An object of {@link MenuConfiguration} that would override
     * the inherited context values if they are not `undefined`.
     * @returns the {@link MenuConfigurationContext} with any overrides that were
     * provided.
     * @remarks \@since 5.0.0
     */
    export function useMenuConfiguration(
      overrides: MenuConfiguration = {}
    ): Readonly<MenuConfigurationContext> {
      const {
        horizontal,
        renderAsSheet,
        sheetHeader,
        sheetFooter,
        sheetPosition,
        sheetVerticalSize,
      } = overrides;
      const inherited = useContext(context);

      return {
        horizontal: horizontal ?? inherited.horizontal,
        renderAsSheet: renderAsSheet ?? inherited.renderAsSheet,
        sheetHeader:
          sheetHeader === null ? null : sheetHeader ?? inherited.sheetHeader,
        sheetFooter:
          sheetFooter === null ? null : sheetFooter ?? inherited.sheetFooter,
        sheetPosition: sheetPosition ?? inherited.sheetPosition,
        sheetVerticalSize: sheetVerticalSize ?? inherited.sheetVerticalSize,
      };
    }

    /** @remarks \@since 5.0.0 */
    export interface MenuConfigurationProviderProps extends MenuConfiguration {
      children: ReactNode;
    }

    /**
     * **Client Component**
     *
     * Note: This will always merge values with any parent
     * `MenuConfigurationProvider`s.
     *
     * @remarks \@since 5.0.0
     */
    export function MenuConfigurationProvider(
      props: MenuConfigurationProviderProps
    ): ReactElement {
      const { children, ...configuration } = props;
      const {
        horizontal,
        renderAsSheet,
        sheetHeader,
        sheetFooter,
        sheetPosition,
        sheetVerticalSize,
      } = useMenuConfiguration(configuration);

      const value = useMemo<MenuConfigurationContext>(
        () => ({
          horizontal,
          renderAsSheet,
          sheetHeader,
          sheetFooter,
          sheetPosition,
          sheetVerticalSize,
        }),
        [
          horizontal,
          renderAsSheet,
          sheetFooter,
          sheetHeader,
          sheetPosition,
          sheetVerticalSize,
        ]
      );

      return <Provider value={value}>{children}</Provider>;
    }