fix(webui): unify settings switch styles

This commit is contained in:
Xubin Ren committed 2026-09-19 17:59:17 +08:00
1 parent af43292f6f
commit 70bf433018
7 files changed
+105 -53

No files matched your search

@@ -31,6 +31,7 @@ import { Input } from "@/components/ui/input";
import { SegmentedControl } from "@/components/ui/segmented-control";
import { Sheet, SheetContent, SheetDescription, SheetTitle } from "@/components/ui/sheet";
import { SkillsMarketplace } from "@/components/settings/SkillsMarketplace";
import { ToggleButton } from "@/components/settings/ToggleButton";
import { deleteSkill, fetchSkillDetail, updateSkillEnabled } from "@/lib/api";
import { notifySkillsChanged } from "@/lib/skill-events";
import type { SkillDetail, SkillSummary } from "@/lib/types";
@@ -447,15 +448,13 @@ function SkillDetailSheet({
</div>
) : (
<div className="mt-6 space-y-5">
<div className="flex min-h-16 items-start justify-between gap-3 border-y border-border/45 px-1 py-3.5">
<div className="flex min-h-16 items-center justify-between gap-3 border-y border-border/45 px-1 py-3.5">
<p className="text-[13px] font-medium text-foreground">
{t("settings.skills.enabledControl", { defaultValue: "Use this skill" })}
</p>
<button
type="button"
role="switch"
aria-checked={enabled}
aria-label={
<ToggleButton
checked={enabled}
label={
enabled
? t("settings.skills.disableSkill", {
name: activeSkill.name,
@@ -467,26 +466,8 @@ function SkillDetailSheet({
})
}
disabled={actionBusy}
onClick={() => void toggleEnabled()}
className={cn(
"relative -mr-1 h-10 w-14 shrink-0 rounded-full",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
"disabled:cursor-wait disabled:opacity-60",
)}
>
<span
className={cn(
"absolute left-1.5 top-2 h-6 w-11 rounded-full transition-colors",
enabled ? "bg-foreground" : "bg-muted-foreground/30",
)}
/>
<span
className={cn(
"absolute left-2 top-2.5 h-5 w-5 rounded-full bg-background shadow-sm transition-transform",
enabled ? "translate-x-5" : "translate-x-0",
)}
/>
</button>
onChange={() => void toggleEnabled()}
/>
</div>
{actionError ? (
@@ -27,21 +27,21 @@ export function ToggleButton({
if (!disabled) onChange(!checked);
}}
className={cn(
"relative inline-flex h-[22px] w-[38px] shrink-0 items-center rounded-full p-[2px]",
"transition-colors duration-200 ease-out focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
"relative inline-flex h-5 w-9 shrink-0 items-center rounded-full p-0.5",
"after:absolute after:left-1/2 after:top-1/2 after:h-11 after:w-11 after:-translate-x-1/2 after:-translate-y-1/2 after:content-['']",
"transition-colors duration-200 ease-out motion-reduce:transition-none focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
checked
? "bg-[#2997FF]"
: "bg-muted-foreground/20 hover:bg-muted-foreground/25",
? "bg-foreground"
: "bg-muted-foreground/25 hover:bg-muted-foreground/30",
disabled && "cursor-default opacity-60",
disabled && checked && "hover:bg-[#2997FF]",
disabled && !checked && "hover:bg-muted-foreground/20",
disabled && !checked && "hover:bg-muted-foreground/25",
)}
>
<span
aria-hidden
className={cn(
"h-[18px] w-[18px] rounded-full bg-background",
"transition-transform duration-200 ease-out",
"h-4 w-4 rounded-full bg-background shadow-sm",
"transition-transform duration-200 ease-out motion-reduce:transition-none",
checked ? "translate-x-[16px]" : "translate-x-0",
)}
/>
@@ -1,4 +1,5 @@
import { ProviderIcon } from "@/components/settings/models/ProviderSettings";
import { ToggleButton } from "@/components/settings/ToggleButton";
import { useAutoSave } from "@/components/settings/shared/useAutoSave";
import { useEffect, useId, useRef, useState, type Dispatch, type SetStateAction } from "react";
import { DisclosureContent } from "@/components/ui/disclosure";
@@ -794,36 +795,22 @@ export function ModelsSettings({
) : (
<span className="h-4 w-4 shrink-0" aria-hidden />
)}
<button
type="button"
role="switch"
aria-checked={ordered}
aria-label={
<ToggleButton
checked={ordered}
label={
ordered
? tx("settings.models.removeFromOrder", "Disable preset")
: tx("settings.models.addToOrder", "Enable preset")
}
disabled={callOrderBusy || (ordered && callOrder.length <= 1)}
onClick={() => {
onChange={() => {
if (ordered) {
removeCallOrderItem(orderIndex);
} else if (preset) {
onChangeCallOrder([...callOrder, preset.name]);
}
}}
className={cn(
"relative inline-flex h-5 w-9 shrink-0 items-center rounded-full transition-colors outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-40",
ordered ? "bg-foreground" : "bg-muted-foreground/25",
)}
>
<span
className={cn(
"h-4 w-4 rounded-full bg-background shadow-sm transition-transform",
ordered ? "translate-x-[18px]" : "translate-x-0.5",
)}
aria-hidden
/>
</button>
/>
</div>
);
return (
+2
View File
@@ -1204,6 +1204,7 @@ describe("App layout", () => {
expect(screen.getByText(/Use GitHub CLI/)).toBeInTheDocument();
const enabledSwitch = screen.getByRole("switch", { name: "Disable github" });
expect(enabledSwitch).toHaveAttribute("aria-checked", "true");
expect(enabledSwitch).toHaveClass("h-5", "w-9", "bg-foreground");
fireEvent.click(enabledSwitch);
await waitFor(() => {
expect(screen.getByRole("switch", { name: "Enable github" })).toHaveAttribute(
@@ -1211,6 +1212,7 @@ describe("App layout", () => {
"false",
);
});
expect(screen.getByRole("switch", { name: "Enable github" })).toHaveClass("h-5", "w-9", "bg-muted-foreground/25");
});
it("deletes a custom skill from its detail sheet", async () => {
@@ -36,6 +36,8 @@ describe("Settings capabilities", () => {
renderSettingsView({ initialSection: "capabilities", initialSettings: payload });
const memory = screen.getByRole("switch", { name: "Memory consolidation" });
const web = screen.getByRole("switch", { name: "Web access" });
expect(memory).toHaveClass("h-5", "w-9", "bg-foreground");
expect(web).toHaveClass("h-5", "w-9", "bg-foreground");
const webStyle = web.className;
fireEvent.click(memory);
await waitFor(() => expect(memory).toBeDisabled());
@@ -44,6 +46,7 @@ describe("Settings capabilities", () => {
expect(web.className).toBe(webStyle);
fireEvent.click(web);
expect(web).not.toBeChecked();
expect(web).toHaveClass("bg-muted-foreground/25");
await act(async () => finishMemory(memorySaved));
await waitFor(() => expect(requestMutationMock).toHaveBeenLastCalledWith(
"settings.runtime_config.update", { values: { "tools.web.enable": false } }, 20_000,
+11
View File
@@ -502,6 +502,7 @@ describe("Settings models", () => {
const enableSwitch = within(codexRow).getByRole("switch", { name: "Enable preset" });
expect(enableSwitch).not.toBeChecked();
expect(enableSwitch).toHaveClass("h-5", "w-9", "bg-muted-foreground/25");
fireEvent.click(enableSwitch);
await waitFor(() => {
@@ -516,6 +517,16 @@ describe("Settings models", () => {
expect(
within(enabledCodexRow).getByRole("switch", { name: "Disable preset" }),
).toBeChecked();
expect(within(enabledCodexRow).getByRole("switch")).toHaveClass("h-5", "w-9", "bg-foreground");
});
it("does not allow disabling the last preset", () => {
renderSettingsView({ initialSection: "models", initialSettings: settingsPayload() });
const toggle = screen.getByRole("switch", { name: "Disable preset" });
expect(toggle).toBeChecked();
expect(toggle).toBeDisabled();
fireEvent.click(toggle);
expect(requestMutationMock).not.toHaveBeenCalled();
});
it("appends a new model preset to the call order immediately", async () => {
+68
View File
@@ -0,0 +1,68 @@
import { fireEvent, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import { ToggleButton } from "@/components/settings/ToggleButton";
describe("ToggleButton", () => {
it("uses shared theme colors and compact dimensions in both states", () => {
const onChange = vi.fn();
const { rerender } = render(
<ToggleButton checked label="Feature" onChange={onChange} />,
);
const toggle = screen.getByRole("switch", { name: "Feature" });
expect(toggle).toBeChecked();
expect(toggle).toHaveClass("h-5", "w-9", "bg-foreground", "after:h-11", "after:w-11");
expect(toggle.firstElementChild).toHaveClass("h-4", "w-4", "bg-background", "translate-x-[16px]");
fireEvent.click(toggle);
expect(onChange).toHaveBeenLastCalledWith(false);
rerender(<ToggleButton checked={false} label="Feature" onChange={onChange} />);
expect(toggle).not.toBeChecked();
expect(toggle).toHaveClass("h-5", "w-9", "bg-muted-foreground/25");
expect(toggle).not.toHaveClass("bg-foreground");
expect(toggle.firstElementChild).toHaveClass("translate-x-0");
fireEvent.click(toggle);
expect(onChange).toHaveBeenLastCalledWith(true);
});
it.each([false, true])("does not toggle while disabled (checked=%s)", (checked) => {
const onChange = vi.fn();
render(<ToggleButton checked={checked} disabled label="Feature" onChange={onChange} />);
const toggle = screen.getByRole("switch", { name: "Feature" });
expect(toggle).toBeDisabled();
expect(toggle).toHaveClass(checked ? "bg-foreground" : "bg-muted-foreground/25", "opacity-60");
fireEvent.click(toggle);
expect(onChange).not.toHaveBeenCalled();
});
it("keeps accessible labeling, keyboard activation, and button semantics", async () => {
const user = userEvent.setup();
const onChange = vi.fn();
const onSubmit = vi.fn((event) => event.preventDefault());
render(
<form onSubmit={onSubmit}>
<p id="toggle-help">Enable this feature.</p>
<ToggleButton
id="feature-toggle"
checked={false}
label="Feature"
ariaLabel="Enable feature"
aria-describedby="toggle-help"
aria-invalid
onChange={onChange}
/>
</form>,
);
const toggle = screen.getByRole("switch", { name: "Enable feature" });
expect(toggle).toHaveAttribute("id", "feature-toggle");
expect(toggle).toHaveAccessibleDescription("Enable this feature.");
expect(toggle).toHaveAttribute("aria-invalid", "true");
await user.tab();
expect(toggle).toHaveFocus();
await user.keyboard(" ");
await user.keyboard("{Enter}");
expect(onChange.mock.calls).toEqual([[true], [true]]);
expect(onSubmit).not.toHaveBeenCalled();
});
});