# All files / src/components/ui fab.tsx

0% Statements 0/172
0% Branches 0/1
0% Functions 0/1
0% Lines 0/172

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

     'use client'

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

    /**
     * Material Design 3 Floating Action Button (FAB)
     *
     * Variants:
     * - primary: Primary container color (default)
     * - secondary: Secondary container color
     * - tertiary: Tertiary container color
     * - surface: Surface container color
     *
     * Sizes:
     * - small: 40x40 (MD3 Small FAB)
     * - default: 56x56 (MD3 FAB)
     * - large: 96x96 (MD3 Large FAB)
     */
    const fabVariants = cva(
      // Base styles
      [
        "inline-flex items-center justify-center",
        "rounded-2xl font-medium",
        "transition-all duration-200",
        "disabled:pointer-events-none disabled:opacity-[0.38]",
        "[&_svg]:pointer-events-none",
        "shrink-0 [&_svg]:shrink-0",
        "outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-offset-background",
        "relative overflow-hidden select-none",
        "shadow-lg hover:shadow-xl active:shadow-md",
        // State layer
        "before:absolute before:inset-0 before:rounded-[inherit] before:bg-current before:opacity-0 before:transition-opacity before:duration-200",
        "hover:before:opacity-[0.08] focus-visible:before:opacity-[0.12] active:before:opacity-[0.12]",
      ].join(" "),
      {
        variants: {
          variant: {
            // Primary container (default)
            primary: [
              "bg-primary text-primary-foreground",
              "focus-visible:ring-primary",
            ].join(" "),

            // Secondary container
            secondary: [
              "bg-secondary text-secondary-foreground",
              "focus-visible:ring-secondary",
            ].join(" "),

            // Tertiary container
            tertiary: [
              "bg-accent text-accent-foreground",
              "focus-visible:ring-accent",
            ].join(" "),

            // Surface container
            surface: [
              "bg-card text-primary",
              "focus-visible:ring-primary",
            ].join(" "),
          },
          size: {
            // MD3 Small FAB - 40x40
            small: "size-10 rounded-xl [&_svg]:size-5",
            // MD3 FAB - 56x56
            default: "size-14 rounded-2xl [&_svg]:size-6",
            // MD3 Large FAB - 96x96
            large: "size-24 rounded-[28px] [&_svg]:size-9",
          },
        },
        defaultVariants: {
          variant: "primary",
          size: "default",
        },
      }
    )

    interface FABProps
      extends ComponentProps<"button">,
        VariantProps<typeof fabVariants> {
      icon: React.ReactNode
    }

    const FAB = forwardRef<HTMLButtonElement, FABProps>(
      ({ className, variant, size, icon, ...props }, ref) => {
        return (
          <button
            ref={ref}
            data-slot="fab"
            className={cn(fabVariants({ variant, size }), className)}
            {...props}
          >
            {icon}
          </button>
        )
      }
    )
    FAB.displayName = "FAB"

    /**
     * Extended FAB with label
     */
    interface ExtendedFABProps
      extends ComponentProps<"button">,
        VariantProps<typeof fabVariants> {
      icon?: React.ReactNode
      label: string
    }

    const extendedFabVariants = cva(
      // Base styles
      [
        "inline-flex items-center justify-center gap-3",
        "h-14 px-4 rounded-2xl font-medium text-sm",
        "transition-all duration-200",
        "disabled:pointer-events-none disabled:opacity-[0.38]",
        "[&_svg]:pointer-events-none [&_svg]:size-6",
        "shrink-0 [&_svg]:shrink-0",
        "outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-offset-background",
        "relative overflow-hidden select-none",
        "shadow-lg hover:shadow-xl active:shadow-md",
        // State layer
        "before:absolute before:inset-0 before:rounded-[inherit] before:bg-current before:opacity-0 before:transition-opacity before:duration-200",
        "hover:before:opacity-[0.08] focus-visible:before:opacity-[0.12] active:before:opacity-[0.12]",
      ].join(" "),
      {
        variants: {
          variant: {
            primary: [
              "bg-primary text-primary-foreground",
              "focus-visible:ring-primary",
            ].join(" "),
            secondary: [
              "bg-secondary text-secondary-foreground",
              "focus-visible:ring-secondary",
            ].join(" "),
            tertiary: [
              "bg-accent text-accent-foreground",
              "focus-visible:ring-accent",
            ].join(" "),
            surface: [
              "bg-card text-primary",
              "focus-visible:ring-primary",
            ].join(" "),
          },
        },
        defaultVariants: {
          variant: "primary",
        },
      }
    )

    const ExtendedFAB = forwardRef<HTMLButtonElement, ExtendedFABProps>(
      ({ className, variant, icon, label, ...props }, ref) => {
        return (
          <button
            ref={ref}
            data-slot="extended-fab"
            className={cn(extendedFabVariants({ variant }), className)}
            {...props}
          >
            {icon}
            <span>{label}</span>
          </button>
        )
      }
    )
    ExtendedFAB.displayName = "ExtendedFAB"

    export { FAB, ExtendedFAB, fabVariants, extendedFabVariants }