# All files / src/app-bar AppBar.tsx

100% Statements 284/284
100% Branches 11/11
100% Functions 2/2
100% Lines 284/284

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
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    2851x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    116x
    1x
     import { cnb } from "cnbuilder";
    import {
      forwardRef,
      type ElementType,
      type HTMLAttributes,
      type Ref,
    } from "react";
    import { box, type BoxOptions } from "../box/styles.js";
    import {
      cssUtils,
      type BackgroundColor,
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      type CssUtilsOptions,
    } from "../cssUtils.js";
    import { type CssPosition } from "../types.js";
    import { bem } from "../utils/bem.js";

    declare module "react" {
      interface CSSProperties {
        "--rmd-app-bar-height"?: string | number;
        "--rmd-app-bar-background-color"?: string;
        "--rmd-app-bar-color"?: string;
        "--rmd-app-bar-surface-background-color"?: string;
        "--rmd-app-bar-surface-color"?: string;
      }
    }

    const styles = bem("rmd-app-bar");

    export type AppBarPosition = "top" | "bottom";

    /**
     * - `"clear"` - the background color will be transparent
     * - `"primary"` - defaults to the current primary theme color
     * - `"secondary"` - defaults to the current secondary theme color
     * - `"surface"` - this will use the current surface background color which
     *   defaults to `#fff` for light themes, `#242424` for dark themes, and
     *   `#424242` for dark themes when the `$disable-dark-elevation` is set to
     *   `true`
     *
     * The default dark theme surface color also depends on the `$fixed-elevation`
     * value.
     */
    export type AppBarTheme = BackgroundColor | "clear";

    /**
     * - `"auto"` - the height will be determined by the height of the content
     * - `"normal"` - defaults to `3.5rem` (`$height`)
     * - `"prominent"` - defaults to `7rem` (`$prominent-height`)
     * - `"dense"` - defaults to `3rem` (`$dense-height`)
     * - `"prominent-dense"` - defaults to `6rem` (`$prominent-dense-height`)
     */
    export type AppBarHeight =
      | "auto"
      | "normal"
      | "prominent"
      | "dense"
      | "prominent-dense";

    /** @remarks \@since 6.0.0 */
    export interface AppBarClassNameOptions extends Omit<BoxOptions, "fullWidth"> {
      className?: string;

      /**
       * Set this to `"fixed"` or `"sticky"` to set `position: fixed;` or
       * `position: sticky;` to the app bar. The default position will be static and
       * inline with other content.
       *
       * @defaultValue `'static'`
       */
      position?: CssPosition;

      /**
       * The position within the page to "fix" the `AppBar` when the `fixed` prop is
       * enabled.
       *
       * @defaultValue `"top"`
       */
      pagePosition?: AppBarPosition;

      /**
       * Set this to `true` to remove the box-shadow.
       *
       * @defaultValue `false`
       */
      disableElevation?: boolean;

      /**
       * The theme to apply to the `AppBar`.
       *
       * @defaultValue `"primary"`
       * @see {@link AppBarTheme}
       */
      theme?: AppBarTheme;

      /**
       * @defaultValue `stacked ? "auto" : "normal"`
       * @see {@link AppBarHeight}
       */
      height?: AppBarHeight;

      /** @see {@link CssUtilsOptions.surfaceColor} */
      surfaceColor?: "light" | "dark";

      /**
       * Set this to `true` if the app bar's positioning and width should be
       * changed whenever the scrollbar is visible on the page. This defaults to
       * `true` when the {@link position} prop is `true` so that once dialogs and menus
       * become visible the contents in the app bar do not need to be repainted.
       *
       * @remarks \@since 6.0.0
       * @defaultValue `position === "fixed"`
       */
      scrollbarOffset?: boolean;
    }

    /**
     * Apply the `className`s for a tree component. This will be type-safe if using
     * typescript.
     *
     * @remarks \@since 6.0.0
     */
    export function appBar(options: AppBarClassNameOptions = {}): string {
      const {
        className,
        theme = "primary",
        stacked,
        height = stacked ? "auto" : "normal",
        position = "static",
        pagePosition = "top",
        scrollbarOffset = position === "fixed",
        align,
        grid,
        gridColumns,
        gridName,
        justify,
        reversed,
        surfaceColor,
        disableWrap = true,
        disablePadding,
        disableElevation,
      } = options;
      const surface = theme === "surface";
      const clear = theme === "clear";

      return cnb(
        styles({
          surface,
          [height]: height !== "normal",
          fixed: position !== "static",
          stacked,
          sticky: position === "sticky",
          [pagePosition]: position !== "static",
          elevated: position !== "static" && !disableElevation,
          "scrollbar-offset": scrollbarOffset,
          "static-scrollbar-offset": position === "static" && scrollbarOffset,
        }),
        box({
          align,
          stacked,
          fullWidth: true,
          grid,
          gridColumns,
          gridName,
          justify,
          reversed,
          disableWrap,
          disablePadding,
        }),
        cssUtils({
          backgroundColor: !surface && !clear ? theme : undefined,
          surfaceColor,
        }),
        className
      );
    }

    /** @remarks \@since 6.0.0 */
    export type CustomAppBarComponent = ElementType<
      HTMLAttributes<HTMLDivElement> & {
        ref?: Ref<HTMLDivElement>;
        className: string;
      }
    >;

    /**
     * @remarks
     * \@since 6.0.0 Renamed the `component` prop to `as` and updated the default
     * value to be a `"div"` when `fixed` is `false`.
     * \@since 6.0.0 Renamed the `fixedElevation` prop to `disableFixedElevation` to
     * match naming conventions when a feature is enabled by default.
     * \@since 6.0.0 Removed the `inheritColor` and `flexWrap` props since they are
     * no longer required.
     * \@since 6.0.0 Removed the `fixed` prop in favor of the new `position` prop
     * which enables position `fixed` or `sticky` behavior.
     * \@since 6.0.0 Added the {@link stacked} and {@link scrollbarOffset} props.
     */
    export interface AppBarProps
      extends HTMLAttributes<HTMLDivElement>,
        AppBarClassNameOptions {
      /** @defaultValue `fixed ? "header" : "div"` */
      as?: CustomAppBarComponent;
    }

    /**
     * **Server Component**
     *
     * @example
     * Simple Example
     * ```tsx
     * import { AppBar, AppBarTitle, Button } from "@react-md/core";
     * import MenuIcon from "@react-md/material-icons/MenuIcon";
     * import MoreVertIcon from "@react-md/material-icons/MoreVertIcon";
     * import type { ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   return (
     *     <AppBar>
     *       <Button aria-label="Navigation"><MenuIcon /></Button>
     *       <AppBarTitle>My Main Title</AppBarTitle>
     *       <Button aria-label="Options"><MoreVertIcon /></Button>
     *     </AppBar>
     *   );
     * }
     * ```
     *
     * @remarks
     * \@since 6.0.0 The `AppBar` was updated to use `gap` for spacing
     * instead of requiring the `AppBarNav` and `AppBarAction` components.
     */
    export const AppBar = forwardRef<HTMLDivElement, AppBarProps>(
      function AppBar(props, ref) {
        const {
          className,
          theme,
          stacked,
          height,
          align,
          grid,
          gridName,
          justify,
          reversed,
          gridColumns,
          disableWrap,
          disablePadding,
          position,
          pagePosition,
          surfaceColor,
          scrollbarOffset,
          disableElevation,
          as: Component = position ? "header" : "div",
          children,
          ...remaining
        } = props;

        return (
          <Component
            {...remaining}
            className={appBar({
              className,
              theme,
              position,
              pagePosition,
              disableElevation,
              scrollbarOffset,
              height,
              grid,
              gridName,
              gridColumns,
              align,
              stacked,
              justify,
              reversed,
              surfaceColor,
              disableWrap,
              disablePadding,
            })}
            ref={ref}
          >
            {children}
          </Component>
        );
      }
    );