跳至正文
获取 Pro
基础原语U.05

标签页

FreeRegistry
components/linyi/tabs.tsx
@base-ui/reactclass-variance-authority
"use client";
 
import * as React from "react";
import { Tabs as BaseTabs } from "@base-ui/react/tabs";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/cn";
 
const TabsVariantContext = React.createContext<"pill" | "segmented" | "line">("pill");
 
export const tabsListVariants = cva("relative inline-flex items-center select-none", {
variants: {
variant: {
pill: "rounded-pill bg-well border border-line p-[3px] shadow-[inset_0_1px_2px_rgba(0,0,0,0.04)]",
segmented:
"rounded-control bg-well border border-line p-[3px] shadow-[inset_0_1px_2px_rgba(0,0,0,0.04)]",
line: "border-b border-line gap-1 bg-transparent p-0",
},
fullWidth: {
true: "w-full",
false: "",
},
},
defaultVariants: {
variant: "pill",
fullWidth: false,
},
});
 
export const tabsTriggerVariants = cva(
"relative z-10 inline-flex items-center justify-center gap-1.5 font-medium whitespace-nowrap cursor-pointer select-none transition-colors outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-1 text-fg-secondary hover:text-fg data-[active]:text-fg data-[disabled]:opacity-40 data-[disabled]:pointer-events-none",
{
variants: {
variant: {
pill: "rounded-pill",
segmented: "rounded-[7px]",
line: "rounded-t-[6px] pb-2.5",
},
size: {
sm: "h-7 px-3 text-[11px]",
md: "h-8 px-3.5 text-xs",
},
},
defaultVariants: {
variant: "pill",
size: "md",
},
}
);
 
export type TabsProps = React.ComponentPropsWithRef<typeof BaseTabs.Root>;
 
export function Tabs({ className, ...props }: TabsProps) {
return <BaseTabs.Root className={cn("w-full", className)} {...props} />;
}
 
export interface TabsListProps
extends React.ComponentPropsWithRef<typeof BaseTabs.List>,
VariantProps<typeof tabsListVariants> {}
 
export function TabsList({
className,
variant = "pill",
fullWidth = false,
children,
...props
}: TabsListProps) {
const resolvedVariant = variant ?? "pill";
 
return (
<TabsVariantContext.Provider value={resolvedVariant}>
<BaseTabs.List
className={cn(tabsListVariants({ variant: resolvedVariant, fullWidth }), className)}
{...props}
>
{children}
<BaseTabs.Indicator
aria-hidden="true"
className={cn(
// 位移走合成层 translate,仅尺寸随目标标签变化,切换时不再逐帧改写 left / top
"pointer-events-none absolute left-0 transition-[translate,width,height] duration-200 ease-[cubic-bezier(0.22,1,0.36,1)] motion-reduce:transition-none",
resolvedVariant === "line"
? "bottom-[-1px] translate-x-[var(--active-tab-left)] w-[var(--active-tab-width)] h-[2px] rounded-full bg-accent"
: cn(
"top-0 translate-x-[var(--active-tab-left)] translate-y-[var(--active-tab-top)] w-[var(--active-tab-width)] h-[var(--active-tab-height)] bg-surface border border-line shadow-[var(--tab-pill-shadow)]",
resolvedVariant === "pill" ? "rounded-pill" : "rounded-[7px]"
)
)}
/>
</BaseTabs.List>
</TabsVariantContext.Provider>
);
}
 
export interface TabsTriggerProps
extends React.ComponentPropsWithRef<typeof BaseTabs.Tab>,
Omit<VariantProps<typeof tabsTriggerVariants>, "variant"> {}
 
export function TabsTrigger({ className, size = "md", ...props }: TabsTriggerProps) {
const variant = React.useContext(TabsVariantContext);
return (
<BaseTabs.Tab
className={cn(tabsTriggerVariants({ variant, size }), "flex-1", className)}
{...props}
/>
);
}
 
export type TabsContentProps = React.ComponentPropsWithRef<typeof BaseTabs.Panel>;
 
export function TabsContent({ className, ...props }: TabsContentProps) {
return (
<BaseTabs.Panel
className={cn(
"mt-3 outline-none focus-visible:ring-2 focus-visible:ring-accent rounded-well",
className
)}
{...props}
/>
);
}