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

88 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-05-05 17:11:44 +00:00
import * as React from "react"
2026-04-12 10:07:54 +00:00
import { Toggle as TogglePrimitive } from "@base-ui/react/toggle"
import { ToggleGroup as ToggleGroupPrimitive } from "@base-ui/react/toggle-group"
2026-05-04 18:33:24 +00:00
import type { VariantProps } from "class-variance-authority"
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
2026-04-12 10:07:54 +00:00
}: ToggleGroupPrimitive.Props &
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 (
2026-04-12 10:07:54 +00:00
<ToggleGroupPrimitive
2025-05-05 17:11:44 +00:00
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-04-12 10:07:54 +00:00
"group/toggle-group flex w-fit flex-row items-center gap-[--spacing(var(--gap))] rounded-md data-[spacing=0]:data-[variant=outline]:shadow-xs 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>
2026-04-12 10:07:54 +00:00
</ToggleGroupPrimitive>
2025-05-05 17:11:44 +00:00
)
}
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
2026-04-12 10:07:54 +00:00
}: TogglePrimitive.Props & VariantProps<typeof toggleVariants>) {
2025-05-05 17:11:44 +00:00
const context = React.useContext(ToggleGroupContext)
return (
2026-04-12 10:07:54 +00:00
<TogglePrimitive
2025-05-05 17:11:44 +00:00
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-04-12 10:07:54 +00:00
"shrink-0 group-data-[spacing=0]/toggle-group:rounded-none group-data-[spacing=0]/toggle-group:px-2 group-data-[spacing=0]/toggle-group:shadow-none focus:z-10 focus-visible:z-10 group-data-[spacing=0]/toggle-group:has-data-[icon=inline-end]:pr-1.5 group-data-[spacing=0]/toggle-group:has-data-[icon=inline-start]:pl-1.5 group-data-horizontal/toggle-group:data-[spacing=0]:first:rounded-l-md group-data-vertical/toggle-group:data-[spacing=0]:first:rounded-t-md group-data-horizontal/toggle-group:data-[spacing=0]:last:rounded-r-md group-data-vertical/toggle-group:data-[spacing=0]:last:rounded-b-md data-[state=on]:bg-muted 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}
2026-04-12 10:07:54 +00:00
</TogglePrimitive>
2025-05-05 17:11:44 +00:00
)
}
export { ToggleGroup, ToggleGroupItem }