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 { Toggle as ToggleComponent, type toggleVariants } from "~/components/ui/toggle";
10
11const ToggleGroupContext = React.createContext<VariantProps<typeof toggleVariants>>({
12 size: "default",
13 variant: "default",
14});
15
16function ToggleGroup({
17 className,
18 variant = "segmented",
19 size: sizeProp,
20 orientation = "horizontal",
21 children,
22 ...props
23}: ToggleGroupPrimitive.Props & VariantProps<typeof toggleVariants>) {
24 const size =
25 sizeProp === undefined ? (variant === "segmented" ? "segmented" : "default") : sizeProp;
26 const value = { size, variant };
27 return (
28 <ToggleGroupPrimitive
29 className={cn(
30 "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",
31 orientation === "horizontal"
32 ? "*:pointer-coarse:after:min-w-auto"
33 : "*:pointer-coarse:after:min-h-auto",
34 variant === "segmented"
35 ? "gap-0.5 rounded-lg bg-input/40 p-0.5"
36 : variant === "default"
37 ? "gap-0.5"
38 : orientation === "horizontal"
39 ? "*: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"
40 : "*: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",
41 className,
42 )}
43 data-size={size}
44 data-slot="toggle-group"
45 data-variant={variant}
46 orientation={orientation}
47 {...props}
48 >
49 <ToggleGroupContext value={value}>{children}</ToggleGroupContext>
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.use(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
80export { ToggleGroup, Toggle, Toggle as ToggleGroupItem };