# All files / src/components/ui bottom-navigation.tsx

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

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

     'use client'

    import { ComponentProps, createContext, forwardRef, useContext } from "react"
    import { cn } from "@/lib/utils"

    /**
     * Material Design 3 Bottom Navigation Bar
     *
     * Android-style bottom navigation with:
     * - 3-5 destinations
     * - Active indicator pill
     * - Icon + label layout
     * - Badge support
     */

    interface BottomNavigationContextValue {
      activeValue?: string
      onValueChange?: (value: string) => void
    }

    const BottomNavigationContext = createContext<BottomNavigationContextValue>({})

    interface BottomNavigationProps extends ComponentProps<"nav"> {
      value?: string
      onValueChange?: (value: string) => void
    }

    const BottomNavigation = forwardRef<HTMLElement, BottomNavigationProps>(
      ({ className, value, onValueChange, children, ...props }, ref) => {
        return (
          <BottomNavigationContext.Provider value={{ activeValue: value, onValueChange }}>
            <nav
              ref={ref}
              data-slot="bottom-navigation"
              className={cn(
                // Container styles
                "fixed bottom-0 left-0 right-0 z-50",
                "h-20 px-2",
                "bg-[hsl(var(--card))]",
                "border-t border-border/50",
                "shadow-[0_-2px_10px_rgba(0,0,0,0.1)]",
                // Flex layout
                "flex items-center justify-around",
                // Safe area for notched phones
                "pb-safe",
                className
              )}
              {...props}
            >
              {children}
            </nav>
          </BottomNavigationContext.Provider>
        )
      }
    )
    BottomNavigation.displayName = "BottomNavigation"

    interface BottomNavigationItemProps extends ComponentProps<"button"> {
      value: string
      icon: React.ReactNode
      activeIcon?: React.ReactNode
      label: string
      badge?: number | boolean
    }

    const BottomNavigationItem = forwardRef<HTMLButtonElement, BottomNavigationItemProps>(
      ({ className, value, icon, activeIcon, label, badge, ...props }, ref) => {
        const context = useContext(BottomNavigationContext)
        const isActive = context.activeValue === value

        const handleClick = () => {
          context.onValueChange?.(value)
        }

        return (
          <button
            ref={ref}
            data-slot="bottom-navigation-item"
            data-active={isActive}
            className={cn(
              // Container
              "relative flex flex-col items-center justify-center",
              "min-w-[64px] h-full px-3 py-2",
              "bg-transparent border-0 cursor-pointer",
              "transition-all duration-200",
              "outline-none",
              // Focus state
              "focus-visible:outline-none",
              className
            )}
            onClick={handleClick}
            {...props}
          >
            {/* Active indicator pill */}
            <span
              className={cn(
                "absolute top-2 w-16 h-8 rounded-full",
                "bg-primary/20",
                "transform transition-all duration-200",
                isActive ? "scale-100 opacity-100" : "scale-75 opacity-0"
              )}
            />

            {/* Icon container */}
            <span
              className={cn(
                "relative z-10 flex items-center justify-center",
                "w-6 h-6 mb-1",
                "[&_svg]:size-6",
                "transition-colors duration-200",
                isActive ? "text-primary" : "text-muted-foreground"
              )}
            >
              {isActive && activeIcon ? activeIcon : icon}

              {/* Badge */}
              {badge !== undefined && badge !== false && (
                <span
                  className={cn(
                    "absolute -top-1 -right-2",
                    "flex items-center justify-center",
                    "min-w-[16px] h-4 px-1",
                    "bg-destructive text-destructive-foreground",
                    "text-[10px] font-medium",
                    "rounded-full"
                  )}
                >
                  {typeof badge === "number" ? (badge > 99 ? "99+" : badge) : ""}
                </span>
              )}
            </span>

            {/* Label */}
            <span
              className={cn(
                "relative z-10 text-xs font-medium",
                "transition-colors duration-200",
                isActive ? "text-primary" : "text-muted-foreground"
              )}
            >
              {label}
            </span>
          </button>
        )
      }
    )
    BottomNavigationItem.displayName = "BottomNavigationItem"

    export { BottomNavigation, BottomNavigationItem }