Open History to see the last commit
1"use client";
2
3import type { Toggle as TogglePrimitive } from "@base-ui/react/toggle";
4import { ToggleGroup as ToggleGroupPrimitive } from "@base-ui/react/toggle-group";
5import type { VariantProps } from "class-variance-authority";
6import * as React from "react";
7
8import { cn } from "~/lib/utils";
9import { Separator } from "~/components/ui/separator";
10import { Toggle as ToggleComponent, type toggleVariants } from "~/components/ui/toggle";
11
12const ToggleGroupContext = React.createContext<VariantProps<typeof toggleVariants>>({
13 size: "default",
14 variant: "default",
15});
16
17function ToggleGroup({
18 className,
19 variant = "default",
20 size = "default",
21 orientation = "horizontal",
22 children,
23 ...props
24}: ToggleGroupPrimitive.Props & VariantProps<typeof toggleVariants>) {
25 const value = React.useMemo(() => ({ size, variant }), [size, variant]);
26 return (
27 <ToggleGroupPrimitive
28 className={cn(
29 "flex w-fit *:focus-visible:z-10 dark:*:[[data-slot=separator]:has(+[data-slot=toggle]:hover)]:before:bg-input/64 dark:*:[[data-slot=separator]:has(+[data-slot=toggle][data-pressed])]:before:bg-input dark:*:[[data-slot=toggle]:hover+[data-slot=separator]]:before:bg-input/64 dark:*:[[data-slot=toggle][data-pressed]+[data-slot=separator]]:before:bg-input",
30 orientation === "horizontal"
31 ? "*:pointer-coarse:after:min-w-auto"
32 : "*:pointer-coarse:after:min-h-auto",
33 variant === "default"
34 ? "gap-0.5"
35 : orientation === "horizontal"
36 ? "*:not-first:not-data-[slot=separator]:before:-start-[0.5px] *:not-last:not-data-[slot=separator]:before:-end-[0.5px] *:not-first:rounded-s-none *:not-last:rounded-e-none *:not-first:border-s-0 *:not-last:border-e-0 *:not-first:before:rounded-s-none *:not-last:before:rounded-e-none"
37 : "*:not-first:not-data-[slot=separator]:before:-top-[0.5px] *:not-last:not-data-[slot=separator]:before:-bottom-[0.5px] flex-col *:not-first:rounded-t-none *:not-last:rounded-b-none *:not-first:border-t-0 *:not-last:border-b-0 *:not-first:before:rounded-t-none *:not-last:before:rounded-b-none *:data-[slot=toggle]:not-last:before:hidden dark:*:last:before:hidden dark:*:first:before:block",
38 className,
39 )}
40 data-size={size}
41 data-slot="toggle-group"
42 data-variant={variant}
43 orientation={orientation}
44 {...props}
45 >
46 <ToggleGroupContext value={value}>{children}</ToggleGroupContext>
47 </ToggleGroupPrimitive>
48 );
49}
50
51function Toggle({
52 className,
53 children,
54 variant,
55 size,
56 ...props
57}: TogglePrimitive.Props & VariantProps<typeof toggleVariants>) {
58 const context = React.use(ToggleGroupContext);
59
60 const resolvedVariant = variant ?? context.variant;
61 const resolvedSize = size ?? context.size;
62
63 return (
64 <ToggleComponent
65 className={className}
66 data-size={resolvedSize}
67 data-variant={resolvedVariant}
68 size={resolvedSize}
69 variant={resolvedVariant}
70 {...props}
71 >
72 {children}
73 </ToggleComponent>
74 );
75}
76
77function ToggleGroupSeparator({
78 className,
79 orientation = "vertical",
80 ...props
81}: {
82 className?: string;
83} & React.ComponentProps<typeof Separator>) {
84 return (
85 <Separator
86 className={cn(
87 "pointer-events-none relative bg-input before:absolute before:inset-0 dark:before:bg-input/32",
88 className,
89 )}
90 orientation={orientation}
91 {...props}
92 />
93 );
94}
95
96export { ToggleGroup, Toggle, Toggle as ToggleGroupItem, ToggleGroupSeparator };