# All files AppBar.tsx

100% Statements 233/233
100% Branches 8/8
100% Functions 2/2
100% Lines 233/233

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
    2341x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    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
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    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
    1x
     import { cnb } from "cnbuilder";
    import { forwardRef, type ElementType, type HTMLAttributes, type Ref } from "react";
    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 = "clear" | "primary" | "secondary" | "surface";

    /**
     * - `"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 {
      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;

      /**
       * Set this to `true` to update the `AppBar` to have `flex-direction: column`.
       * This is useful when nesting `AppBar`s.
       *
       * @remarks \@since 6.0.0
       * @defaultValue `false`
       */
      stacked?: boolean;

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

      /**
       * 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,
        height = "normal",
        theme = "primary",
        stacked = false,
        position = "static",
        pagePosition = "top",
        scrollbarOffset = position === "fixed",
        disableElevation = false,
      } = options;

      return cnb(
        styles({
          [theme]: theme !== "clear",
          [height]: height !== "normal",
          fixed: position !== "static",
          sticky: position === "sticky",
          stacked,
          [pagePosition]: position !== "static",
          elevated: position !== "static" && !disableElevation,
          "scrollbar-offset": scrollbarOffset,
          "static-scrollbar-offset": position === "static" && scrollbarOffset,
        }),
        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 = stacked ? "auto" : "normal",
          position,
          pagePosition,
          scrollbarOffset,
          disableElevation,
          as: Component = position ? "header" : "div",
          children,
          ...remaining
        } = props;

        return (
          <Component
            {...remaining}
            className={appBar({
              theme,
              position,
              pagePosition,
              disableElevation,
              stacked,
              height,
              className,
              scrollbarOffset,
            })}
            ref={ref}
          >
            {children}
          </Component>
        );
      }
    );