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

切换按钮组

FreeRegistry
components/linyi/toggle-group.tsx
@base-ui/reactclass-variance-authority
"use client";
 
import * as React from "react";
import { Toggle as BaseToggle } from "@base-ui/react/toggle";
import { ToggleGroup as BaseToggleGroup } from "@base-ui/react/toggle-group";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/cn";
 
type ToggleVariant = "surface" | "ghost" | "outline";
type ToggleSize = "sm" | "md" | "lg";
 
interface ToggleGroupContextValue {
variant: ToggleVariant;
size: ToggleSize;
}
 
const ToggleGroupContext = React.createContext<ToggleGroupContextValue>({
variant: "surface",
size: "md",
});
 
export const toggleGroupVariants = cva(
"inline-flex items-center select-none",
{
variants: {
variant: {
surface: "rounded-control border border-line bg-well p-0.5 gap-0.5",
outline: "rounded-control border border-line-strong bg-surface p-0.5 gap-0.5",
ghost: "gap-1 bg-transparent",
},
orientation: {
horizontal: "flex-row",
vertical: "flex-col items-stretch",
},
},
defaultVariants: {
variant: "surface",
orientation: "horizontal",
},
}
);
 
export const toggleVariants = cva(
[
"inline-flex items-center justify-center gap-1.5 rounded-[7px] font-medium cursor-pointer select-none outline-none",
"transition-[background-color,color,border-color,box-shadow,transform] duration-150 ease-[cubic-bezier(0.16,1,0.3,1)] motion-reduce:transition-none",
"active:not([data-disabled]):scale-[0.97]",
"focus-visible:ring-2 focus-visible:ring-accent/35 focus-visible:ring-offset-1",
"data-[disabled]:opacity-40 data-[disabled]:pointer-events-none",
].join(" "),
{
variants: {
variant: {
surface: [
"text-fg-secondary hover:text-fg hover:bg-surface/60",
"data-[pressed]:bg-surface data-[pressed]:text-fg data-[pressed]:shadow-[var(--tab-pill-shadow)]",
].join(" "),
outline: [
"border border-transparent text-fg-secondary hover:text-fg hover:bg-well/70",
"data-[pressed]:border-accent/40 data-[pressed]:bg-accent-soft data-[pressed]:text-accent",
].join(" "),
ghost: [
"text-fg-secondary hover:text-fg hover:bg-well/80",
"data-[pressed]:bg-inverse data-[pressed]:text-fg-inverse",
].join(" "),
},
size: {
sm: "h-7 min-w-7 px-2 text-[11px]",
md: "h-8 min-w-8 px-2.5 text-xs",
lg: "h-9 min-w-9 px-3 text-xs",
},
},
defaultVariants: {
variant: "surface",
size: "md",
},
}
);
 
export interface ToggleProps
extends Omit<React.ComponentPropsWithRef<typeof BaseToggle>, "className">,
VariantProps<typeof toggleVariants> {
className?: string;
}
 
export function Toggle({
className,
variant = "surface",
size = "md",
...props
}: ToggleProps) {
return (
<BaseToggle
className={cn(toggleVariants({ variant, size }), className)}
{...props}
/>
);
}
 
export interface ToggleGroupProps<Value extends string = string>
extends Omit<BaseToggleGroup.Props<Value>, "className">,
Omit<VariantProps<typeof toggleGroupVariants>, "orientation"> {
className?: string;
size?: ToggleSize;
ref?: React.Ref<HTMLDivElement>;
}
 
export function ToggleGroup<Value extends string = string>({
className,
variant = "surface",
size = "md",
orientation = "horizontal",
...props
}: ToggleGroupProps<Value>) {
const resolvedVariant = variant ?? "surface";
const resolvedSize = size ?? "md";
 
return (
<ToggleGroupContext.Provider
value={{ variant: resolvedVariant, size: resolvedSize }}
>
<BaseToggleGroup<Value>
orientation={orientation}
className={cn(
toggleGroupVariants({ variant: resolvedVariant, orientation }),
className
)}
{...props}
/>
</ToggleGroupContext.Provider>
);
}
 
export interface ToggleGroupItemProps
extends Omit<React.ComponentPropsWithRef<typeof BaseToggle>, "className">,
Partial<VariantProps<typeof toggleVariants>> {
className?: string;
}
 
export function ToggleGroupItem({
className,
variant,
size,
...props
}: ToggleGroupItemProps) {
const ctx = React.useContext(ToggleGroupContext);
return (
<BaseToggle
className={cn(
toggleVariants({
variant: variant ?? ctx.variant,
size: size ?? ctx.size,
}),
className
)}
{...props}
/>
);
}