Skip to content
TypeScript

hatch/variant functions (#13191)

Julius Marminge29931dd3a9a4
diff --git a/apps/web/src/components/CustomSnoozeDialog.tsx b/apps/web/src/components/CustomSnoozeDialog.tsx
index 729200c907b..f944d6af604 100644
--- a/apps/web/src/components/CustomSnoozeDialog.tsx
+++ b/apps/web/src/components/CustomSnoozeDialog.tsx
@@ -1,5 +1,4 @@
import { useEffect, useId, useState } from "react";
-import { Tabs } from "@base-ui/react/tabs";
import { create } from "zustand";
import {
localSnoozeDate,
@@ -14,7 +13,7 @@ import { weekStartsOn } from "../timestampFormat";
import { Popover, PopoverTrigger, PopoverPopup } from "./ui/popover";
import { Input } from "./ui/input";
import { Label } from "./ui/label";
-import { toggleVariants } from "./ui/toggle";
+import { Toggle, ToggleGroup } from "./ui/toggle-group";
import { Select, SelectTrigger, SelectValue, SelectPopup, SelectItem } from "./ui/select";
import {
NumberField,
@@ -95,34 +94,21 @@ function CustomSnoozeDialog() {
<DialogDescription>Choose when snoozed threads return to your inbox.</DialogDescription>
</DialogHeader>
<DialogPanel className="text-base sm:text-sm">
- <Tabs.Root
- value={mode}
- onValueChange={(value) => {
- if (value === "date" || value === "duration") setMode(value);
- setError(null);
- }}
- className="flex flex-col gap-4"
- >
- <Tabs.List
+ <div className="flex flex-col gap-4">
+ <ToggleGroup
aria-label="Schedule type"
- className="flex gap-0.5 rounded-lg bg-input/40 p-0.5"
+ className="w-full *:flex-1"
+ value={[mode]}
+ onValueChange={(next) => {
+ const value = next[0];
+ if (value === "date" || value === "duration") setMode(value);
+ setError(null);
+ }}
>
- {(["date", "duration"] as const).map((value) => (
- <Tabs.Tab
- key={value}
- value={value}
- data-pressed={mode === value ? "" : undefined}
- className={toggleVariants({
- variant: "segmented",
- size: "sm",
- className: "flex-1",
- })}
- >
- {value === "date" ? "Date and time" : "Duration"}
- </Tabs.Tab>
- ))}
- </Tabs.List>
- <Tabs.Panel value={mode} className="flex flex-col gap-4">
+ <Toggle value="date">Date and time</Toggle>
+ <Toggle value="duration">Duration</Toggle>
+ </ToggleGroup>
+ <div className="flex flex-col gap-4">
{mode === "date" ? (
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
<div className="flex min-w-0 flex-col gap-1.5">
@@ -226,8 +212,8 @@ function CustomSnoozeDialog() {
</Label>
</div>
)}
- </Tabs.Panel>
- </Tabs.Root>
+ </div>
+ </div>
{error && (
<p role="alert" className="text-destructive">
{error}
diff --git a/apps/web/src/components/GitActionsControl.tsx b/apps/web/src/components/GitActionsControl.tsx
index 4f461ce21cf..41ae826dce9 100644
--- a/apps/web/src/components/GitActionsControl.tsx
+++ b/apps/web/src/components/GitActionsControl.tsx
@@ -48,7 +48,7 @@ import {
} from "~/components/Icons";
import { RadioGroup } from "~/components/ui/radio-group";
import { Spinner } from "~/components/ui/spinner";
-import { toggleVariants } from "~/components/ui/toggle";
+import { Toggle, ToggleGroup } from "~/components/ui/toggle-group";
import { cn } from "~/lib/utils";
import {
buildGitActionProgressStages,
@@ -815,10 +815,10 @@ function PublishRepositoryDialog(props: PublishRepositoryDialogProps) {
>
Protocol
</span>
- <RadioGroup
- className="w-fit flex-row gap-0.5 rounded-lg bg-input/40 p-0.5"
- value={publishProtocol}
- onValueChange={(protocol) => {
+ <ToggleGroup
+ value={[publishProtocol]}
+ onValueChange={(next) => {
+ const protocol = next[0];
if (protocol === "ssh" || protocol === "https") {
setPublishProtocol(protocol);
}
@@ -826,20 +826,9 @@ function PublishRepositoryDialog(props: PublishRepositoryDialogProps) {
aria-labelledby="publish-protocol-label"
disabled={publishRepositoryAction.isPending}
>
- {(["ssh", "https"] as const).map((protocol) => (
- <RadioPrimitive.Root
- key={protocol}
- value={protocol}
- data-pressed={publishProtocol === protocol ? "" : undefined}
- className={toggleVariants({
- variant: "segmented",
- size: "segmented",
- })}
- >
- {protocol.toUpperCase()}
- </RadioPrimitive.Root>
- ))}
- </RadioGroup>
+ <Toggle value="ssh">SSH</Toggle>
+ <Toggle value="https">HTTPS</Toggle>
+ </ToggleGroup>
</div>
</div>
) : null}
diff --git a/apps/web/src/components/chat/ComposerBanner.tsx b/apps/web/src/components/chat/ComposerBanner.tsx
index bdd28ce555a..4e52dfa51b6 100644
--- a/apps/web/src/components/chat/ComposerBanner.tsx
+++ b/apps/web/src/components/chat/ComposerBanner.tsx
@@ -4,7 +4,7 @@ import { ChevronDownIcon, XIcon } from "lucide-react";
import type { ComponentProps } from "react";
import { cn } from "~/lib/utils";
-import { Button, buttonVariants } from "../ui/button";
+import { Button } from "../ui/button";
import { ScrollArea } from "../ui/scroll-area";
export type ComposerBannerVariant = "default" | "error" | "info" | "success" | "warning";
@@ -323,18 +323,18 @@ function Dot({ className, ...props }: ComponentProps<"span">) {
);
}
-function ToggleIcon({ expanded, className }: { expanded: boolean; className?: string }) {
+// Decorative: the row itself is the control, so this only matches Dismiss's box.
+function ToggleIcon({ expanded }: { expanded: boolean }) {
return (
- <span
- aria-hidden
- className={cn(
- buttonVariants({ size: "icon-xs", variant: "ghost" }),
- "pointer-events-none",
- className,
- )}
+ <Button
+ render={<span aria-hidden />}
+ size="icon-xs"
+ variant="ghost"
+ tabIndex={-1}
+ className="pointer-events-none"
>
<ChevronDownIcon className={cn("size-3.5", !expanded && "rotate-180")} />
- </span>
+ </Button>
);
}
diff --git a/apps/web/src/components/chat/ProviderModelPicker.tsx b/apps/web/src/components/chat/ProviderModelPicker.tsx
index 2d1b147fd9b..7ccf4f93ba6 100644
--- a/apps/web/src/components/chat/ProviderModelPicker.tsx
+++ b/apps/web/src/components/chat/ProviderModelPicker.tsx
@@ -5,9 +5,8 @@ import {
type ResolvedKeybindingsConfig,
} from "@t3tools/contracts";
import { memo, useEffect, useMemo, useState } from "react";
-import type { VariantProps } from "class-variance-authority";
import { Badge } from "../ui/badge";
-import { buttonVariants } from "../ui/button";
+import type { ButtonVariant } from "../ui/button";
import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import { cn } from "~/lib/utils";
@@ -49,7 +48,7 @@ export const ProviderModelPicker = memo(function ProviderModelPicker(props: {
disabled?: boolean;
terminalOpen?: boolean;
open?: boolean;
- triggerVariant?: VariantProps<typeof buttonVariants>["variant"];
+ triggerVariant?: ButtonVariant;
triggerClassName?: string;
/** Aggregate settings can show a neutral value without claiming one provider is selected. */
triggerLabel?: string;
diff --git a/apps/web/src/components/chat/TraitsPicker.tsx b/apps/web/src/components/chat/TraitsPicker.tsx
index 07ca60cedcc..c0740c73a2d 100644
--- a/apps/web/src/components/chat/TraitsPicker.tsx
+++ b/apps/web/src/components/chat/TraitsPicker.tsx
@@ -16,9 +16,8 @@ import {
normalizeModelSlug,
} from "@t3tools/shared/model";
import { memo, useCallback } from "react";
-import type { VariantProps } from "class-variance-authority";
import { GaugeIcon, ZapIcon } from "lucide-react";
-import { buttonVariants } from "../ui/button";
+import type { ButtonVariant } from "../ui/button";
import {
Menu,
MenuGroup,
@@ -277,7 +276,7 @@ export interface TraitsMenuContentProps {
modelOptions?: ProviderOptions | null | undefined;
allowPromptInjectedEffort?: boolean;
planModeEnabled: boolean;
- triggerVariant?: VariantProps<typeof buttonVariants>["variant"];
+ triggerVariant?: ButtonVariant;
triggerClassName?: string;
isComposerOwned?: boolean;
}
diff --git a/apps/web/src/components/chat/composerProviderState.tsx b/apps/web/src/components/chat/composerProviderState.tsx
index c6837700cdf..96fd01a2238 100644
--- a/apps/web/src/components/chat/composerProviderState.tsx
+++ b/apps/web/src/components/chat/composerProviderState.tsx
@@ -13,10 +13,9 @@ import {
isClaudeUltrathinkPrompt,
normalizeModelSlug,
} from "@t3tools/shared/model";
-import type { VariantProps } from "class-variance-authority";
import type { ReactNode } from "react";
-import type { buttonVariants } from "../ui/button";
+import type { ButtonVariant } from "../ui/button";
import type { DraftId } from "../../composerDraftStore";
import { getProviderModelCapabilities } from "../../providerModels";
import type { ComposerControlSize } from "./ComposerControl";
@@ -55,7 +54,7 @@ type TraitsRenderInput = {
planModeEnabled: boolean;
size?: ComposerControlSize;
hidden?: boolean;
- triggerVariant?: VariantProps<typeof buttonVariants>["variant"];
+ triggerVariant?: ButtonVariant;
triggerClassName?: string;
isComposerOwned?: boolean;
};
diff --git a/apps/web/src/components/settings/FontFamilyPicker.tsx b/apps/web/src/components/settings/FontFamilyPicker.tsx
index 1981b852c03..f9084ca027a 100644
--- a/apps/web/src/components/settings/FontFamilyPicker.tsx
+++ b/apps/web/src/components/settings/FontFamilyPicker.tsx
@@ -1,5 +1,5 @@
import { LegendList, type LegendListRef } from "@legendapp/list/react";
-import { CheckIcon, ChevronDownIcon } from "lucide-react";
+import { CheckIcon } from "lucide-react";
import { useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
import { isMonospaceFamily, queryInstalledFontFamilies } from "../../appearanceFonts";
import {
@@ -11,7 +11,7 @@ import {
ComboboxPopup,
ComboboxTrigger,
} from "../ui/combobox";
-import { selectTriggerVariants } from "../ui/select";
+import { SelectButton } from "../ui/select";
const DEFAULT_FONT_VALUE = "__default__";
@@ -204,11 +204,8 @@ export function FontFamilyPicker({
void listRef.current?.scrollIndexIntoView?.({ index: eventDetails.index, animated: false });
}}
>
- <ComboboxTrigger aria-label={ariaLabel} className={selectTriggerVariants({ size: "sm" })}>
- <span className="min-w-0 truncate">
- {selectedFamily.length === 0 ? defaultFamily : selectedFamily}
- </span>
- <ChevronDownIcon className="-me-1 size-3 opacity-50" />
+ <ComboboxTrigger aria-label={ariaLabel} render={<SelectButton size="sm" />}>
+ {selectedFamily.length === 0 ? defaultFamily : selectedFamily}
</ComboboxTrigger>
<ComboboxPopup align="end" className="flex w-72 flex-col">
<ComboboxSearchInput
diff --git a/apps/web/src/components/settings/SnapShotSettings.tsx b/apps/web/src/components/settings/SnapShotSettings.tsx
index 26f1b609704..31063f91274 100644
--- a/apps/web/src/components/settings/SnapShotSettings.tsx
+++ b/apps/web/src/components/settings/SnapShotSettings.tsx
@@ -7,11 +7,9 @@ import {
type DesktopSnapShotSetupAction,
type SnapShotShortcut,
} from "@t3tools/contracts";
-import { ChevronDownIcon, PlayIcon } from "lucide-react";
+import { PlayIcon } from "lucide-react";
import { useCallback, useEffect, useRef, useState } from "react";
-import { cn } from "~/lib/utils";
-
import { useClientSettings, useUpdateClientSettings } from "../../hooks/useSettings";
import { getDesktopSnapShotBridge } from "../../lib/desktopSnapShot";
import {
@@ -44,7 +42,7 @@ import {
import { searchableSetting } from "./settingsSearch";
import { Button } from "../ui/button";
import { Menu, MenuItem, MenuPopup, MenuRadioGroup, MenuRadioItem, MenuTrigger } from "../ui/menu";
-import { selectTriggerVariants } from "../ui/select";
+import { SelectButton } from "../ui/select";
import { Switch } from "../ui/switch";
import { toastManager } from "../ui/toast";
import { SnapShotSetupDialog } from "./SnapShotSetupDialog";
@@ -497,21 +495,19 @@ export function SnapShotSettings() {
<Menu>
<MenuTrigger
aria-label={"Snapshot sound: " + soundLabel}
- className={cn(selectTriggerVariants({ size: "sm" }), "w-auto min-w-0")}
+ render={<SelectButton size="sm" />}
+ className="w-auto min-w-0"
disabled={!captureAvailable}
>
- <span className="min-w-0 flex-1 truncate text-left">
- {soundSelection === "off" ? (
- "Off"
- ) : soundSelection === "soft-pop" ? (
- <>
- Whoosh <span className="text-muted-foreground">(Default)</span>
- </>
- ) : (
- "Click"
- )}
- </span>
- <ChevronDownIcon className="-me-1 size-3 shrink-0 opacity-50" />
+ {soundSelection === "off" ? (
+ "Off"
+ ) : soundSelection === "soft-pop" ? (
+ <>
+ Whoosh <span className="text-muted-foreground">(Default)</span>
+ </>
+ ) : (
+ "Click"
+ )}
</MenuTrigger>
<MenuPopup align="end">
<MenuRadioGroup
diff --git a/apps/web/src/components/ui/badge.tsx b/apps/web/src/components/ui/badge.tsx
index d5d7a47eb98..db688707fb9 100644
--- a/apps/web/src/components/ui/badge.tsx
+++ b/apps/web/src/components/ui/badge.tsx
@@ -60,4 +60,5 @@ function Badge({ className, variant, size, render, ...props }: BadgeProps) {
});
}
+// The *Variants function is for components/ui modules only.
export { Badge, badgeVariants };
diff --git a/apps/web/src/components/ui/button.tsx b/apps/web/src/components/ui/button.tsx
index f2775a82ffd..c9a0d0dd585 100644
--- a/apps/web/src/components/ui/button.tsx
+++ b/apps/web/src/components/ui/button.tsx
@@ -71,9 +71,12 @@ const buttonVariants = cva(
},
);
+type ButtonVariant = NonNullable<VariantProps<typeof buttonVariants>["variant"]>;
+type ButtonSize = NonNullable<VariantProps<typeof buttonVariants>["size"]>;
+
interface ButtonProps extends useRender.ComponentProps<"button"> {
- variant?: VariantProps<typeof buttonVariants>["variant"];
- size?: VariantProps<typeof buttonVariants>["size"];
+ variant?: ButtonVariant;
+ size?: ButtonSize;
}
function Button({ className, variant, size, render, ...props }: ButtonProps) {
@@ -95,7 +98,9 @@ function Button({ className, variant, size, render, ...props }: ButtonProps) {
});
}
-export { Button, buttonVariants };
+// buttonVariants is exported for other components/ui modules only; app code
+// renders a Button (with `render` for other elements) instead.
+export { Button, buttonVariants, type ButtonSize, type ButtonVariant };
const inlineButtonVariants = cva(
"inline-flex shrink-0 cursor-pointer items-center gap-0.5 whitespace-nowrap font-medium underline-offset-2 hover:underline focus-visible:outline-2 focus-visible:outline-ring disabled:pointer-events-none disabled:opacity-64",
diff --git a/apps/web/src/components/ui/select.tsx b/apps/web/src/components/ui/select.tsx
index a7ab17eb928..6fd85f510a2 100644
--- a/apps/web/src/components/ui/select.tsx
+++ b/apps/web/src/components/ui/select.tsx
@@ -1,5 +1,7 @@
"use client";
+import { mergeProps } from "@base-ui/react/merge-props";
import { Select as SelectPrimitive } from "@base-ui/react/select";
+import { useRender } from "@base-ui/react/use-render";
import { cva, type VariantProps } from "class-variance-authority";
import { ChevronDownIcon, ChevronUpIcon } from "lucide-react";
import type * as React from "react";
@@ -57,6 +59,37 @@ function SelectTrigger({
);
}
+/**
+ * The select-field look for a picker that is not a Select, such as a Menu or
+ * Combobox trigger. Render it as that trigger: `<MenuTrigger render={<SelectButton />}>`.
+ */
+function SelectButton({
+ className,
+ size = "default",
+ children,
+ render,
+ ...props
+}: useRender.ComponentProps<"button"> & Pick<VariantProps<typeof selectTriggerVariants>, "size">) {
+ const defaultProps = {
+ className: cn(selectTriggerVariants({ size }), className),
+ "data-slot": "select-trigger",
+ type: render ? undefined : ("button" as const),
+ };
+ return useRender({
+ defaultTagName: "button",
+ props: {
+ ...mergeProps<"button">(defaultProps, props),
+ children: (
+ <>
+ <span className="min-w-0 flex-1 truncate text-left">{children}</span>
+ <ChevronDownIcon aria-hidden className="-me-1 size-3 shrink-0 opacity-50" />
+ </>
+ ),
+ },
+ render,
+ });
+}
+
function SelectValue({ className, ...props }: SelectPrimitive.Value.Props) {
return (
<SelectPrimitive.Value
@@ -182,7 +215,7 @@ function SelectGroupLabel(props: SelectPrimitive.GroupLabel.Props) {
export {
Select,
SelectTrigger,
- selectTriggerVariants,
+ SelectButton,
SelectValue,
SelectPopup,
SelectPopup as SelectContent,
diff --git a/apps/web/src/components/ui/toggle.tsx b/apps/web/src/components/ui/toggle.tsx
index 17efb492ebe..844c5c1e785 100644
--- a/apps/web/src/components/ui/toggle.tsx
+++ b/apps/web/src/components/ui/toggle.tsx
@@ -52,4 +52,5 @@ function Toggle({
);
}
+// The *Variants function is for components/ui modules only.
export { Toggle, toggleVariants };
diff --git a/scripts/lint-restyle-ceiling.ts b/scripts/lint-restyle-ceiling.ts
index 0c1028d4483..80cff165214 100644
--- a/scripts/lint-restyle-ceiling.ts
+++ b/scripts/lint-restyle-ceiling.ts
@@ -8,7 +8,7 @@ import * as NodeURL from "node:url";
// components/ui exports are migrated to variants (see vite.config.ts). This gate keeps the
// count from growing: CI fails when findings exceed the ceiling. Lower the ceiling when you
// migrate a file, and delete this script when the rule becomes an error.
-export const RESTYLE_CEILING = 627;
+export const RESTYLE_CEILING = 623;
const RULE = "shadcn(no-restyle)";
diff --git a/vite.config.ts b/vite.config.ts
index 65b4a989fd2..87fc1fae13b 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -16,6 +16,20 @@ const RESTRICTED_IMPORT_PATHS = [
},
];
+/**
+ * The cva functions behind components/ui exports. They style a foreign element to look
+ * like a Button or Toggle, which bypasses the component's variants; render the component
+ * instead (`render={<Button …/>}`, or `SelectButton` for a picker trigger).
+ */
+const RESTRICTED_UI_VARIANT_PATTERNS = [
+ {
+ group: ["**/components/ui/*", "**/ui/*", "./ui/*"],
+ importNames: ["buttonVariants", "toggleVariants", "badgeVariants", "selectTriggerVariants"],
+ message:
+ "Render the components/ui export instead of borrowing its class recipe (render={<Button …/>}, SelectButton, ToggleGroup).",
+ },
+];
+
/** Lucide's pull-request glyphs, which only `pullRequestIcons.tsx` may name. */
const RESTRICTED_PULL_REQUEST_GLYPH_IMPORTS = {
name: "lucide-react",
@@ -160,6 +174,19 @@ export default defineConfig({
files: ["packages/shared/src/hostProcess.ts"],
rules: { "t3code/no-global-process-runtime": "off" },
},
+ {
+ files: ["apps/web/src/**"],
+ excludeFiles: ["apps/web/src/components/ui/**"],
+ rules: {
+ "eslint/no-restricted-imports": [
+ "error",
+ {
+ paths: [...RESTRICTED_IMPORT_PATHS, RESTRICTED_PULL_REQUEST_GLYPH_IMPORTS],
+ patterns: RESTRICTED_UI_VARIANT_PATTERNS,
+ },
+ ],
+ },
+ },
{
// The one module allowed to name lucide's pull-request glyphs; everything else picks
// from its vocabulary. The other import restrictions still apply here.