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

90 lines
3.0 KiB
TypeScript
Raw Normal View History

2025-05-05 17:11:44 +00:00
"use client"
import * as React from "react"
import { type VariantProps } from "class-variance-authority"
2026-03-17 17:49:54 +00:00
import { ToggleGroup as ToggleGroupPrimitive } from "radix-ui"
2025-05-05 17:11:44 +00:00
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
2026-03-17 17:49:54 +00:00
orientation?: "horizontal" | "vertical"
2025-10-21 09:30:45 +00:00
}
2025-05-05 17:11:44 +00:00
>({
size: "default",
variant: "default",
2025-10-21 09:30:45 +00:00
spacing: 0,
2026-03-17 17:49:54 +00:00
orientation: "horizontal",
2025-05-05 17:11:44 +00:00
})
function ToggleGroup({
className,
variant,
size,
2025-10-21 09:30:45 +00:00
spacing = 0,
2026-03-17 17:49:54 +00:00
orientation = "horizontal",
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
2026-03-17 17:49:54 +00:00
orientation?: "horizontal" | "vertical"
2025-10-21 09:30:45 +00:00
}) {
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}
2026-03-17 17:49:54 +00:00
data-orientation={orientation}
2025-10-21 09:30:45 +00:00
style={{ "--gap": spacing } as React.CSSProperties}
2025-05-05 17:11:44 +00:00
className={cn(
2026-03-17 17:49:54 +00:00
"group/toggle-group flex w-fit flex-row items-center gap-[--spacing(var(--gap))] rounded-lg data-[size=sm]:rounded-[min(var(--radius-md),10px)] data-vertical:flex-col data-vertical:items-stretch",
2025-05-05 17:11:44 +00:00
className
)}
{...props}
>
2026-03-17 17:49:54 +00:00
<ToggleGroupContext.Provider
value={{ variant, size, spacing, orientation }}
>
2025-05-05 17:11:44 +00:00
{children}
</ToggleGroupContext.Provider>
</ToggleGroupPrimitive.Root>
)
}
function ToggleGroupItem({
className,
children,
2026-03-17 17:49:54 +00:00
variant = "default",
size = "default",
2025-05-05 17:11:44 +00:00
...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(
2026-03-17 17:49:54 +00:00
"shrink-0 group-data-[spacing=0]/toggle-group:rounded-none group-data-[spacing=0]/toggle-group:px-2 focus:z-10 focus-visible:z-10 group-data-horizontal/toggle-group:data-[spacing=0]:first:rounded-l-lg group-data-vertical/toggle-group:data-[spacing=0]:first:rounded-t-lg group-data-horizontal/toggle-group:data-[spacing=0]:last:rounded-r-lg group-data-vertical/toggle-group:data-[spacing=0]:last:rounded-b-lg group-data-horizontal/toggle-group:data-[spacing=0]:data-[variant=outline]:border-l-0 group-data-vertical/toggle-group:data-[spacing=0]:data-[variant=outline]:border-t-0 group-data-horizontal/toggle-group:data-[spacing=0]:data-[variant=outline]:first:border-l group-data-vertical/toggle-group:data-[spacing=0]:data-[variant=outline]:first:border-t",
2025-05-05 17:11:44 +00:00
toggleVariants({
variant: context.variant || variant,
size: context.size || size,
}),
className
)}
{...props}
>
{children}
</ToggleGroupPrimitive.Item>
)
}
export { ToggleGroup, ToggleGroupItem }