medkit/src/kit/content-tabs.tsx

81 lines
2.7 KiB
TypeScript

import { type LucideIcon } from "lucide-react"
import { cn } from "../lib/utils"
export interface ContentTabItem<T extends string = string> {
readonly id: T
readonly label: string
readonly icon?: LucideIcon
/** Счётчик справа от подписи. */
readonly count?: number
readonly disabled?: boolean
}
/**
* Контентные вкладки внутри страницы/карточки.
* `line` — подчёркивание (разделы карточки), `pill` — заливка (переключатели в блоках).
*/
export function ContentTabs<T extends string>({
tabs,
value,
onValueChange,
variant = "line",
className,
}: {
tabs: readonly ContentTabItem<T>[]
value: T
onValueChange: (value: T) => void
variant?: "line" | "pill"
className?: string
}) {
return (
<div
role="tablist"
className={cn(
"scrollbar-none flex shrink-0 items-center gap-1 overflow-x-auto",
variant === "line" && "border-b px-3",
variant === "pill" && "w-fit rounded-xl bg-muted/70 p-1",
className,
)}
>
{tabs.map((tab) => {
const active = tab.id === value
return (
<button
key={tab.id}
type="button"
role="tab"
aria-selected={active}
disabled={tab.disabled}
onClick={() => onValueChange(tab.id)}
className={cn(
"relative flex shrink-0 items-center gap-2 text-sm font-medium whitespace-nowrap transition-colors outline-none focus-visible:ring-2 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-40",
variant === "line" && "h-10 px-3",
variant === "line" && (active ? "text-foreground" : "text-muted-foreground hover:text-foreground"),
variant === "pill" && "h-8 rounded-lg px-3",
variant === "pill" &&
(active ? "bg-card text-foreground shadow-xs" : "text-muted-foreground hover:text-foreground"),
)}
>
{tab.icon && <tab.icon className={cn("size-4", active && "text-primary")} />}
{tab.label}
{tab.count !== undefined && (
<span
className={cn(
"rounded-md px-1.5 text-[11px] leading-5 font-semibold tabular",
active ? "bg-primary/10 text-primary" : "bg-muted text-muted-foreground",
)}
>
{tab.count}
</span>
)}
{variant === "line" && active && (
<span className="absolute inset-x-2 -bottom-px h-0.5 rounded-full bg-primary" />
)}
</button>
)
})}
</div>
)
}