# All files / src/components/ui top-app-bar.tsx

0% Statements 0/184
0% Branches 0/1
0% Functions 0/1
0% Lines 0/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
    185

     'use client'

    import { ComponentProps, forwardRef } from "react"
    import { cva, type VariantProps } from "class-variance-authority"
    import { cn } from "@/lib/utils"

    /**
     * Material Design 3 Top App Bar
     *
     * Variants:
     * - center-aligned: Logo/title centered (default)
     * - small: Standard top app bar
     * - medium: Medium with larger title
     * - large: Large with prominent title
     */

    const topAppBarVariants = cva(
      // Base styles
      [
        "w-full",
        "bg-[hsl(var(--card))]",
        "transition-all duration-200",
      ].join(" "),
      {
        variants: {
          variant: {
            "center-aligned": "h-16",
            small: "h-16",
            medium: "h-28",
            large: "h-36",
          },
          scrolled: {
            true: "shadow-md bg-[hsl(var(--card))]/95 backdrop-blur-sm",
            false: "shadow-none",
          },
        },
        defaultVariants: {
          variant: "small",
          scrolled: false,
        },
      }
    )

    interface TopAppBarProps
      extends ComponentProps<"header">,
        VariantProps<typeof topAppBarVariants> {
      navigationIcon?: React.ReactNode
      onNavigationClick?: () => void
      title?: string
      actions?: React.ReactNode
    }

    const TopAppBar = forwardRef<HTMLElement, TopAppBarProps>(
      (
        {
          className,
          variant,
          scrolled,
          navigationIcon,
          onNavigationClick,
          title,
          actions,
          children,
          ...props
        },
        ref
      ) => {
        const isCenterAligned = variant === "center-aligned"
        const isMediumOrLarge = variant === "medium" || variant === "large"

        return (
          <header
            ref={ref}
            data-slot="top-app-bar"
            className={cn(topAppBarVariants({ variant, scrolled }), className)}
            {...props}
          >
            <div
              className={cn(
                "flex items-center h-16 px-1",
                isCenterAligned && "justify-between"
              )}
            >
              {/* Leading navigation icon */}
              {navigationIcon && (
                <button
                  type="button"
                  onClick={onNavigationClick}
                  className={cn(
                    "flex items-center justify-center",
                    "size-12 rounded-full",
                    "text-foreground",
                    "hover:bg-foreground/[0.08]",
                    "focus-visible:bg-foreground/[0.12]",
                    "active:bg-foreground/[0.12]",
                    "transition-colors duration-200",
                    "outline-none"
                  )}
                >
                  {navigationIcon}
                </button>
              )}

              {/* Title - for small and center-aligned */}
              {!isMediumOrLarge && title && (
                <h1
                  className={cn(
                    "text-lg font-normal text-foreground",
                    "truncate",
                    isCenterAligned
                      ? "absolute left-1/2 -translate-x-1/2"
                      : "ml-2 flex-1"
                  )}
                >
                  {title}
                </h1>
              )}

              {/* Trailing actions */}
              {actions && (
                <div className={cn("flex items-center gap-1", !isCenterAligned && "ml-auto")}>
                  {actions}
                </div>
              )}
            </div>

            {/* Title for medium and large variants */}
            {isMediumOrLarge && title && (
              <div className="px-4 pb-4">
                <h1
                  className={cn(
                    "text-foreground font-normal",
                    variant === "medium" && "text-2xl",
                    variant === "large" && "text-3xl"
                  )}
                >
                  {title}
                </h1>
              </div>
            )}

            {children}
          </header>
        )
      }
    )
    TopAppBar.displayName = "TopAppBar"

    /**
     * Top App Bar Action Button
     */
    interface TopAppBarActionProps extends ComponentProps<"button"> {
      icon: React.ReactNode
    }

    const TopAppBarAction = forwardRef<HTMLButtonElement, TopAppBarActionProps>(
      ({ className, icon, ...props }, ref) => {
        return (
          <button
            ref={ref}
            type="button"
            data-slot="top-app-bar-action"
            className={cn(
              "flex items-center justify-center",
              "size-12 rounded-full",
              "text-foreground",
              "hover:bg-foreground/[0.08]",
              "focus-visible:bg-foreground/[0.12]",
              "active:bg-foreground/[0.12]",
              "transition-colors duration-200",
              "outline-none",
              "[&_svg]:size-6",
              className
            )}
            {...props}
          >
            {icon}
          </button>
        )
      }
    )
    TopAppBarAction.displayName = "TopAppBarAction"

    export { TopAppBar, TopAppBarAction, topAppBarVariants }