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