Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

refactor(web): Clean up `Toggle` (#16588)

Ben Bachemedc741157ade
diff --git a/web/.storybook/main.ts b/web/.storybook/main.ts
index 83dc99e67b..dae1eb256b 100644
--- a/web/.storybook/main.ts
+++ b/web/.storybook/main.ts
@@ -22,6 +22,7 @@ const DESIGN_COMPONENT_STORIES = [
"SearchInput/SearchInput",
"Spinner/Spinner",
"Switch/Switch",
+ "Toggle/Toggle",
"Table/columns/createBadgeTableColumn",
"Table/columns/createDateTableColumn",
"Table/columns/createDurationTableColumn",
diff --git a/web/src/components/design-system/Toggle/Toggle.stories.tsx b/web/src/components/design-system/Toggle/Toggle.stories.tsx
new file mode 100644
index 0000000000..63530a258b
--- /dev/null
+++ b/web/src/components/design-system/Toggle/Toggle.stories.tsx
@@ -0,0 +1,40 @@
+import { expect, fn, userEvent, within } from "storybook/test";
+
+import preview from "../../../../.storybook/preview";
+import { Toggle } from "./Toggle";
+
+const meta = preview.meta({
+ component: Toggle,
+});
+
+export const Default = meta.story({
+ args: {
+ children: "Comparison",
+ pressed: false,
+ },
+});
+
+export const Pressed = meta.story({
+ args: {
+ children: "Baseline",
+ pressed: true,
+ },
+});
+
+export const TestHandlesClick = meta.story({
+ name: "(Test) Handles Click",
+ args: {
+ children: "Toggle",
+ onClick: fn(),
+ pressed: false,
+ },
+ play: async ({ args, canvasElement }) => {
+ const canvas = within(canvasElement);
+ const toggle = canvas.getByRole("button", { name: "Toggle" });
+
+ await expect(toggle).toHaveAttribute("aria-pressed", "false");
+ await userEvent.click(toggle);
+ await expect(toggle).toHaveAttribute("aria-pressed", "false");
+ await expect(args.onClick).toHaveBeenCalledOnce();
+ },
+});
diff --git a/web/src/components/design-system/Toggle/Toggle.tsx b/web/src/components/design-system/Toggle/Toggle.tsx
new file mode 100644
index 0000000000..528f73e30b
--- /dev/null
+++ b/web/src/components/design-system/Toggle/Toggle.tsx
@@ -0,0 +1,18 @@
+"use client";
+
+import * as React from "react";
+import * as TogglePrimitive from "@radix-ui/react-toggle";
+
+type ToggleProps = Pick<
+ React.ComponentPropsWithoutRef<typeof TogglePrimitive.Root>,
+ "children" | "onClick" | "onMouseEnter" | "onMouseLeave" | "pressed"
+>;
+
+export function Toggle(props: ToggleProps) {
+ return (
+ <TogglePrimitive.Root
+ className="text-muted-foreground/50 ring-offset-background hover:bg-background hover:text-primary-accent focus-visible:ring-ring data-[state=on]:text-primary-accent inline-flex h-8 items-center justify-center rounded-md bg-transparent p-1 text-xs font-bold transition-colors focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-hidden"
+ {...props}
+ />
+ );
+}
diff --git a/web/src/components/ui/toggle-group.tsx b/web/src/components/ui/toggle-group.tsx
index 8143b875a7..e5c353a124 100644
--- a/web/src/components/ui/toggle-group.tsx
+++ b/web/src/components/ui/toggle-group.tsx
@@ -3,12 +3,35 @@
import * as React from "react";
import * as ToggleGroupPrimitive from "@radix-ui/react-toggle-group";
-import { type VariantProps } from "class-variance-authority";
+import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/src/utils/tailwind";
-import { toggleVariants } from "@/src/components/ui/toggle";
+
+const toggleGroupItemVariants = cva(
+ "inline-flex items-center justify-center rounded-md font-bold ring-offset-background transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
+ {
+ variants: {
+ variant: {
+ default:
+ "bg-transparent text-foreground hover:bg-muted hover:text-muted-foreground data-[state=on]:bg-primary data-[state=on]:text-primary-foreground",
+ outline:
+ "border border-input bg-transparent text-foreground hover:bg-accent hover:text-accent-foreground data-[state=on]:bg-primary data-[state=on]:text-primary-foreground",
+ },
+ size: {
+ default: "h-8 px-3 text-sm",
+ xs: "h-6 px-1.5 text-sm",
+ sm: "h-9 px-2.5 text-sm",
+ lg: "h-11 px-5 text-sm",
+ },
+ },
+ defaultVariants: {
+ variant: "default",
+ size: "default",
+ },
+ },
+);
const ToggleGroupContext = React.createContext<
- VariantProps<typeof toggleVariants>
+ VariantProps<typeof toggleGroupItemVariants>
>({
size: "default",
variant: "default",
@@ -17,7 +40,7 @@ const ToggleGroupContext = React.createContext<
const ToggleGroup = React.forwardRef<
React.ComponentRef<typeof ToggleGroupPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof ToggleGroupPrimitive.Root> &
- VariantProps<typeof toggleVariants>
+ VariantProps<typeof toggleGroupItemVariants>
>(({ className, variant, size, children, ...props }, ref) => (
<ToggleGroupPrimitive.Root
ref={ref}
@@ -35,7 +58,7 @@ ToggleGroup.displayName = ToggleGroupPrimitive.Root.displayName;
const ToggleGroupItem = React.forwardRef<
React.ComponentRef<typeof ToggleGroupPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof ToggleGroupPrimitive.Item> &
- VariantProps<typeof toggleVariants>
+ VariantProps<typeof toggleGroupItemVariants>
>(({ className, children, variant, size, ...props }, ref) => {
const context = React.useContext(ToggleGroupContext);
@@ -43,7 +66,7 @@ const ToggleGroupItem = React.forwardRef<
<ToggleGroupPrimitive.Item
ref={ref}
className={cn(
- toggleVariants({
+ toggleGroupItemVariants({
variant: context.variant || variant,
size: context.size || size,
}),
diff --git a/web/src/components/ui/toggle.tsx b/web/src/components/ui/toggle.tsx
deleted file mode 100644
index 2963a40b16..0000000000
--- a/web/src/components/ui/toggle.tsx
+++ /dev/null
@@ -1,47 +0,0 @@
-/* eslint-disable @repo/no-style-props */
-"use client";
-
-import * as React from "react";
-import * as TogglePrimitive from "@radix-ui/react-toggle";
-import { cva, type VariantProps } from "class-variance-authority";
-
-import { cn } from "@/src/utils/tailwind";
-
-const toggleVariants = cva(
- "inline-flex items-center justify-center rounded-md text-sm font-bold ring-offset-background transition-colors hover:bg-muted hover:text-muted-foreground focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=on]:bg-primary data-[state=on]:text-primary-foreground",
- {
- variants: {
- variant: {
- default: "bg-transparent",
- outline:
- "border border-input bg-transparent hover:bg-accent hover:text-accent-foreground",
- },
- size: {
- default: "h-8 px-3",
- xs: "h-6 px-1.5",
- sm: "h-9 px-2.5",
- lg: "h-11 px-5",
- },
- },
- defaultVariants: {
- variant: "default",
- size: "default",
- },
- },
-);
-
-const Toggle = React.forwardRef<
- React.ComponentRef<typeof TogglePrimitive.Root>,
- React.ComponentPropsWithoutRef<typeof TogglePrimitive.Root> &
- VariantProps<typeof toggleVariants>
->(({ className, variant, size, ...props }, ref) => (
- <TogglePrimitive.Root
- ref={ref}
- className={cn(toggleVariants({ variant, size, className }))}
- {...props}
- />
-));
-
-Toggle.displayName = TogglePrimitive.Root.displayName;
-
-export { Toggle, toggleVariants };
diff --git a/web/src/features/datasets/components/DatasetRunAggregateColumnHelpers.tsx b/web/src/features/datasets/components/DatasetRunAggregateColumnHelpers.tsx
index 046a073efb..84162783e0 100644
--- a/web/src/features/datasets/components/DatasetRunAggregateColumnHelpers.tsx
+++ b/web/src/features/datasets/components/DatasetRunAggregateColumnHelpers.tsx
@@ -9,9 +9,8 @@ import { type Row } from "@tanstack/react-table";
import React, { useEffect, useRef, useState } from "react";
import { useDebounce } from "@/src/hooks/useDebounce";
import { type ScoreColumn } from "@/src/features/scores/types";
-import { Toggle } from "@/src/components/ui/toggle";
+import { Toggle } from "@/src/components/design-system/Toggle/Toggle";
import { useRouter } from "next/router";
-import { cn } from "@/src/utils/tailwind";
function DatasetAggregateCellWithBaselineDetection({
value,
@@ -97,10 +96,7 @@ function BaselineToggle({ runId }: { runId: string }) {
return (
<Toggle
- className={cn(
- "text-muted-foreground/50 hover:bg-background hover:text-primary-accent p-1 data-[state=on]:bg-transparent data-[state=on]:text-current",
- isBaseline && "text-primary-accent",
- )}
+ pressed={isBaseline}
onClick={handleClick}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}