# All files / src/components/ui toggle-group.tsx

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

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

     "use client"

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

    interface ToggleGroupContextType {
      size?: "default" | "sm" | "lg"
      variant?: "default" | "outline"
      value?: string | string[]
      onValueChange?: (value: string | string[]) => void
      type?: "single" | "multiple"
    }

    const ToggleGroupContext = createContext<ToggleGroupContextType>({
      size: "default",
      variant: "default",
      type: "single",
    })

    interface ToggleGroupProps extends ComponentProps<"div"> {
      variant?: "default" | "outline"
      size?: "default" | "sm" | "lg"
      value?: string | string[]
      onValueChange?: (value: string | string[]) => void
      type?: "single" | "multiple"
    }

    function ToggleGroup({
      className,
      variant = "default",
      size = "default",
      value: controlledValue,
      onValueChange,
      type = "single",
      children,
      ...props
    }: ToggleGroupProps) {
      const [uncontrolledValue, setUncontrolledValue] = useState<string | string[]>(
        type === "single" ? "" : []
      )
      const actualValue = controlledValue !== undefined ? controlledValue : uncontrolledValue

      const handleValueChange = (newValue: string | string[]) => {
        setUncontrolledValue(newValue)
        onValueChange?.(newValue)
      }

      return (
        <div
          data-slot="toggle-group"
          data-variant={variant}
          data-size={size}
          className={cn(
            "group/toggle-group inline-flex items-center rounded-md",
            variant === "outline" && "shadow-sm",
            className
          )}
          {...props}
        >
          <ToggleGroupContext.Provider
            value={{
              variant,
              size,
              value: actualValue,
              onValueChange: handleValueChange,
              type,
            }}
          >
            {children}
          </ToggleGroupContext.Provider>
        </div>
      )
    }

    interface ToggleGroupItemProps extends ComponentProps<"button"> {
      value: string
    }

    function ToggleGroupItem({
      className,
      value,
      onClick,
      ...props
    }: ToggleGroupItemProps) {
      const context = useContext(ToggleGroupContext)
      const isPressed =
        context.type === "single"
          ? context.value === value
          : Array.isArray(context.value) && context.value.includes(value)

      const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
        if (context.type === "single") {
          context.onValueChange?.(value)
        } else {
          const newValue = Array.isArray(context.value) ? [...context.value] : []
          if (newValue.includes(value)) {
            newValue.splice(newValue.indexOf(value), 1)
          } else {
            newValue.push(value)
          }
          context.onValueChange?.(newValue)
        }
        onClick?.(e)
      }

      const sizeClasses = {
        default: "h-9 px-2 min-w-9",
        sm: "h-8 px-1.5 min-w-8",
        lg: "h-10 px-2.5 min-w-10",
      }[context.size || "default"]

      const variantClasses = {
        default: "bg-transparent hover:bg-gray-200 dark:hover:bg-gray-700",
        outline:
          "border border-gray-300 dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-800",
      }[context.variant || "default"]

      return (
        <button
          data-slot="toggle-group-item"
          data-variant={context.variant}
          data-size={context.size}
          className={cn(
            "inline-flex items-center justify-center rounded-none text-sm font-medium",
            "disabled:pointer-events-none disabled:opacity-50",
            "focus-visible:ring-2 focus-visible:ring-offset-2 focus:z-10 focus-visible:z-10",
            "transition-colors",
            "first:rounded-l-md last:rounded-r-md",
            context.variant === "outline" && "border-l-0 first:border-l",
            variantClasses,
            isPressed && "bg-blue-600 dark:bg-blue-500 text-white",
            sizeClasses,
            className
          )}
          onClick={handleClick}
          aria-pressed={isPressed}
          {...props}
        />
      )
    }

    export { ToggleGroup, ToggleGroupItem }