Uecko_ERP/packages/shadcn-ui/src/components/toggle-group.tsx

84 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-05-05 17:11:44 +00:00
"use client"
import * as React from "react"
import * as ToggleGroupPrimitive from "@radix-ui/react-toggle-group"
import { type VariantProps } from "class-variance-authority"
import { cn } from "@repo/shadcn-ui/lib/utils"
import { toggleVariants } from "@repo/shadcn-ui/components/toggle"
const ToggleGroupContext = React.createContext<
2025-10-21 09:30:45 +00:00
VariantProps<typeof toggleVariants> & {
spacing?: number
}
2025-05-05 17:11:44 +00:00
>({
size: "default",
variant: "default",
2025-10-21 09:30:45 +00:00
spacing: 0,
2025-05-05 17:11:44 +00:00
})
function ToggleGroup({
className,
variant,
size,
2025-10-21 09:30:45 +00:00
spacing = 0,
2025-05-05 17:11:44 +00:00
children,
...props
}: React.ComponentProps<typeof ToggleGroupPrimitive.Root> &
2025-10-21 09:30:45 +00:00
VariantProps<typeof toggleVariants> & {
spacing?: number
}) {
2025-05-05 17:11:44 +00:00
return (
<ToggleGroupPrimitive.Root
data-slot="toggle-group"
data-variant={variant}
data-size={size}
2025-10-21 09:30:45 +00:00
data-spacing={spacing}
style={{ "--gap": spacing } as React.CSSProperties}
2025-05-05 17:11:44 +00:00
className={cn(
2025-10-21 09:30:45 +00:00
"group/toggle-group flex w-fit items-center gap-[--spacing(var(--gap))] rounded-md data-[spacing=default]:data-[variant=outline]:shadow-xs",
2025-05-05 17:11:44 +00:00
className
)}
{...props}
>
2025-10-21 09:30:45 +00:00
<ToggleGroupContext.Provider value={{ variant, size, spacing }}>
2025-05-05 17:11:44 +00:00
{children}
</ToggleGroupContext.Provider>
</ToggleGroupPrimitive.Root>
)
}
function ToggleGroupItem({
className,
children,
variant,
size,
...props
}: React.ComponentProps<typeof ToggleGroupPrimitive.Item> &
VariantProps<typeof toggleVariants>) {
const context = React.useContext(ToggleGroupContext)
return (
<ToggleGroupPrimitive.Item
data-slot="toggle-group-item"
data-variant={context.variant || variant}
data-size={context.size || size}
2025-10-21 09:30:45 +00:00
data-spacing={context.spacing}
2025-05-05 17:11:44 +00:00
className={cn(
toggleVariants({
variant: context.variant || variant,
size: context.size || size,
}),
2025-10-21 09:30:45 +00:00
"w-auto min-w-0 shrink-0 px-3 focus:z-10 focus-visible:z-10",
"data-[spacing=0]:rounded-none data-[spacing=0]:shadow-none data-[spacing=0]:first:rounded-l-md data-[spacing=0]:last:rounded-r-md data-[spacing=0]:data-[variant=outline]:border-l-0 data-[spacing=0]:data-[variant=outline]:first:border-l",
2025-05-05 17:11:44 +00:00
className
)}
{...props}
>
{children}
</ToggleGroupPrimitive.Item>
)
}
export { ToggleGroup, ToggleGroupItem }