# All files / src AppSizeProvider.tsx

97.86% Statements 229/234
92.3% Branches 12/13
100% Functions 2/2
97.86% Lines 229/234

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
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    2351x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    823x
    823x
    4x
    4x
    818x
    818x
    818x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    198x
    198x
    198x
    198x
    198x
    198x
    198x
    198x
    198x
    198x
    198x
    4x
    4x
    192x
    192x
    192x
    192x
    192x
    192x
    192x
    192x
    192x
    192x
    192x
    192x
    192x
    192x
    192x
    198x
    198x
    198x
    198x
    198x
    198x
    198x
    133x
    133x
    133x
    133x
    133x
    133x
    133x
    198x
    198x
    198x
    198x
    198x

    192x
    192x
    192x
     "use client";
    import type { ReactElement, ReactNode } from "react";
    import { createContext, useContext, useMemo } from "react";

    import { useSsr } from "./SsrProvider.js";
    import { useMediaQuery } from "./useMediaQuery.js";
    import { useOrientation } from "./useOrientation.js";

    export type QuerySize = number | string;

    /**
     * The current size for your application. This should work both server side and
     * client side, but you will have much better results client side.
     */
    export interface AppSize {
      /**
       * Boolean if currently matching a phone by comparing the max width of the
       * device.
       */
      isPhone: boolean;

      /**
       * Boolean if currently matching a tablet by comparing the max width of the
       * device.
       */
      isTablet: boolean;

      /**
       * Boolean if currently matching a desktop screen by comparing the max width
       * of the device.
       */
      isDesktop: boolean;

      /**
       * Boolean if currently matching a large desktop screen by comparing the max
       * width of the device.
       */
      isLargeDesktop: boolean;

      /**
       * Boolean if the app is considered to be in landscape mode. This will just
       * verify that the window width is greater than the window height.
       *
       * NOTE: This might not be super accurate on Android devices since the soft
       * keyboard will change the dimensions of the viewport when it appears. It is
       * recommended to use the `useOrientation` hook as well if you'd like to get
       * the current orientation type.
       */
      isLandscape: boolean;
    }

    export const DEFAULT_APP_SIZE: AppSize = {
      isPhone: false,
      isTablet: false,
      isDesktop: true,
      isLargeDesktop: false,
      isLandscape: true,
    };

    /** @internal */
    interface AppSizeContext extends AppSize {
      __root: boolean;
    }

    const context = createContext<AppSizeContext>({
      __root: false,
      isPhone: false,
      isTablet: false,
      isDesktop: true,
      isLargeDesktop: false,
      isLandscape: true,
    });
    context.displayName = "AppSize";
    const { Provider } = context;

    /**
     * **Note**: The app size object returned by this hook will **not** be equal
     * between renders.
     *
     * @example
     * Simple Example
     * ```tsx
     * import { useAppSize } from "@react-md/core";
     *
     * function Example() {
     *   const { isPhone, isTablet, isDesktop, isLargeDesktop, isLandscape } =
     *     useAppSize()
     *
     *   // do something based on the app size
     *   return null;
     * }
     * ```
     *
     * @returns the current app size.
     * @throws "The `AppSizeProvider` has not been mounted."
     */
    export function useAppSize(): Readonly<AppSize> {
      const { __root, ...appSize } = useContext(context);
      if (!__root) {
        throw new Error("The `AppSizeProvider` has not been mounted.");
      }

      return appSize;
    }

    export const DEFAULT_DESKTOP_MIN_WIDTH: QuerySize = `${1025 / 16}em`;
    export const DEFAULT_TABLET_MIN_WIDTH: QuerySize = `${768 / 16}em`;
    export const DEFAULT_TABLET_MAX_WIDTH: QuerySize = `${1024 / 16}em`;
    export const DEFAULT_PHONE_MAX_WIDTH: QuerySize = `${767 / 16}em`;
    export const DEFAULT_DESKTOP_LARGE_MIN_WIDTH: QuerySize = `${1280 / 16}em`;
    export const DEFAULT_APP_SIZE_QUERIES: Readonly<AppSizeQueries> = {
      phoneMaxWidth: DEFAULT_PHONE_MAX_WIDTH,
      tabletMinWidth: DEFAULT_TABLET_MIN_WIDTH,
      tabletMaxWidth: DEFAULT_TABLET_MAX_WIDTH,
      desktopMinWidth: DEFAULT_DESKTOP_MIN_WIDTH,
      desktopLargeMinWidth: DEFAULT_DESKTOP_LARGE_MIN_WIDTH,
    };

    export interface AppSizeQueries {
      /**
       * The max width to use for phones. This one is a max width unlike the others
       * since everything from 0 to this value will be considered a phone.
       *
       * @defaultValue `47.9375em`
       * @see {@link DEFAULT_PHONE_MAX_WIDTH}
       */
      phoneMaxWidth?: QuerySize;

      /**
       * The min width for a tablet device.
       *
       * @defaultValue `48em`
       * @see {@link DEFAULT_TABLET_MIN_WIDTH}
       */
      tabletMinWidth?: QuerySize;

      /**
       * The max width for a tablet device. This should normally be `1px` less than
       * the `desktopMinWidth`, but it can be any value if needed. The tablet has a
       * range of min to max so that you can have a bit more control.
       *
       * @defaultValue `64em`
       * @see {@link DEFAULT_TABLET_MAX_WIDTH}
       */
      tabletMaxWidth?: QuerySize;

      /**
       * The min width for a desktop screen.
       *
       * @defaultValue `64.0625em`
       * @see {@link DEFAULT_DESKTOP_MIN_WIDTH}
       */
      desktopMinWidth?: QuerySize;

      /**
       * The min width for a large (1440p, 4k, etc) desktop screen.
       *
       * @defaultValue `80em`
       * @see {@link DEFAULT_DESKTOP_LARGE_MIN_WIDTH}
       */
      desktopLargeMinWidth?: QuerySize;
    }

    export interface AppSizeProviderProps extends AppSizeQueries {
      /**
       * @defaultValue `DEFAULT_APP_SIZE`
       * @see {@link DEFAULT_APP_SIZE}
       */
      ssrSize?: Readonly<AppSize>;
      children: ReactNode;
    }

    /**
     * **Client Component**
     *
     * This component should be mounted near the root of your app to determine the
     * current app size based on different media queries.
     */
    export function AppSizeProvider(props: AppSizeProviderProps): ReactElement {
      const {
        ssrSize = DEFAULT_APP_SIZE,
        phoneMaxWidth = DEFAULT_PHONE_MAX_WIDTH,
        tabletMinWidth = DEFAULT_TABLET_MIN_WIDTH,
        tabletMaxWidth = DEFAULT_TABLET_MAX_WIDTH,
        desktopMinWidth = DEFAULT_DESKTOP_MIN_WIDTH,
        desktopLargeMinWidth = DEFAULT_DESKTOP_LARGE_MIN_WIDTH,
        children,
      } = props;
      const { __root } = useContext(context);
      if (__root) {
        throw new Error("The `AppSizeProvider` cannot be mounted multiple times.");
      }

      const ssr = useSsr();
      const matchesDesktop = useMediaQuery(
        `screen and (min-width: ${desktopMinWidth})`
      );
      const matchesLargeDesktop = useMediaQuery(
        `screen and (min-width: ${desktopLargeMinWidth})`
      );
      const matchesTablet = useMediaQuery(
        `screen and (min-width: ${tabletMinWidth}) and (max-width: ${tabletMaxWidth})`
      );
      const matchesPhone = useMediaQuery(
        `screen and (max-width: ${phoneMaxWidth})`
      );
      const isDesktop = matchesDesktop;
      const isTablet = !matchesDesktop && matchesTablet;
      const isPhone = !isTablet && !isDesktop && matchesPhone;
      const isLandscape = useOrientation().includes("landscape");
      const isLargeDesktop = matchesLargeDesktop;

      const appSize = useMemo<AppSizeContext>(
        () => ({
          __root: true,
          isPhone,
          isTablet,
          isDesktop,
          isLargeDesktop,
          isLandscape,
        }),
        [isDesktop, isLandscape, isLargeDesktop, isPhone, isTablet]
      );

      let value = appSize;
      if (ssr || typeof window === "undefined") {
        value = {
          __root: true,
          ...ssrSize,
        };
      }

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