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 return (
26 <ToggleGroupPrimitive
27 className={cn(
28 "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",
29 orientation === "horizontal"
30 ? "*:pointer-coarse:after:min-w-auto"
31 : "*:pointer-coarse:after:min-h-auto",
32 variant === "default"
33 ? "gap-0.5"
34 : orientation === "horizontal"
35 ? "*: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"
36 : "*: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",
37 className,
38 )}
39 data-size={size}
40 data-slot="toggle-group"
41 data-variant={variant}
42 orientation={orientation}
43 {...props}
44 >
45 <ToggleGroupContext.Provider
46 value={React.useMemo(() => ({ size, variant }), [size, variant])}
47 >
48 {children}
49 </ToggleGroupContext.Provider>
50 </ToggleGroupPrimitive>
51 );
52}
53
54function Toggle({
55 className,
56 children,
57 variant,
58 size,
59 ...props
60}: TogglePrimitive.Props & VariantProps<typeof toggleVariants>) {
61 const context = React.useContext(ToggleGroupContext);
62
63 const resolvedVariant = variant ?? context.variant;
64 const resolvedSize = size ?? context.size;
65
66 return (
67 <ToggleComponent
68 className={className}
69 data-size={resolvedSize}
70 data-variant={resolvedVariant}
71 size={resolvedSize}
72 variant={resolvedVariant}
73 {...props}
74 >
75 {children}
76 </ToggleComponent>
77 );
78}
79
80function ToggleGroupSeparator({
81 className,
82 orientation = "vertical",
83 ...props
84}: {
85 className?: string;
86} & React.ComponentProps<typeof Separator>) {
87 return (
88 <Separator
89 className={cn(
90 "pointer-events-none relative bg-input before:absolute before:inset-0 dark:before:bg-input/32",
91 className,
92 )}
93 orientation={orientation}
94 {...props}
95 />
96 );
97}
98
99export { ToggleGroup, Toggle, Toggle as ToggleGroupItem, ToggleGroupSeparator };