Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

feat(theme): give switches their own theme variables (MUL-9094) (#10414)

Jiayuan Zhanga60a0b1c1b6d
diff --git a/docs/theming.md b/docs/theming.md
index 20a4696bb3..49f47ab369 100644
--- a/docs/theming.md
+++ b/docs/theming.md
@@ -415,7 +415,7 @@ and dark differ, both defaults are given.
| `--card-foreground` | `oklch(0.141 0.005 285.823)` / `oklch(0.985 0 0)` | Text on a card. |
| `--popover` | `var(--surface-raised)` | Shadcn alias; follows `surface-raised` unless set. |
| `--popover-foreground` | `oklch(0.141 0.005 285.823)` / `oklch(0.985 0 0)` | Text in menus and popovers. |
-| `--primary` | `oklch(0.21 0.006 285.885)` / `oklch(0.92 0.004 286.32)` | Primary buttons and strong marks; also used as text. |
+| `--primary` | `oklch(0.21 0.006 285.885)` / `oklch(0.92 0.004 286.32)` | Primary buttons and strong marks; also used as text. Fills switches that are on unless `switch-checked-background` is set. |
| `--primary-foreground` | `oklch(0.985 0 0)` / `oklch(0.21 0.006 285.885)` | Text on primary. |
| `--secondary` | `oklch(0.967 0.001 286.375)` / `oklch(0.274 0.006 286.033)` | Secondary buttons. |
| `--secondary-foreground` | `oklch(0.21 0.006 285.885)` / `oklch(0.985 0 0)` | Text on secondary. |
@@ -425,7 +425,7 @@ and dark differ, both defaults are given.
| `--accent` | `oklch(0.967 0.001 286.375)` / `oklch(0.274 0.006 286.033)` | Hover fill in menus and lists. |
| `--accent-foreground` | `oklch(0.21 0.006 285.885)` / `oklch(0.985 0 0)` | Text on accent. |
| `--border` | `oklch(0.945 0.003 286.32)` / `oklch(1 0 0 / 6%)` | Dividers and table borders. |
-| `--input` | `oklch(0.92 0.004 286.32)` / `oklch(1 0 0 / 15%)` | Input borders, and pressed display chips unless `display-chip-pressed-background` is set. |
+| `--input` | `oklch(0.92 0.004 286.32)` / `oklch(1 0 0 / 15%)` | Input borders. Also fills pressed display chips and switches that are off, unless `display-chip-pressed-background` and `switch-unchecked-background` are set. |
| `--ring` | `oklch(0.705 0.015 286.067)` / `oklch(0.552 0.016 285.938)` | Focus ring. |
| `--sidebar` | `oklch(0.964435 0.001327 286.375)` / `oklch(0.21 0.006 285.885)` | Sidebar background (shadcn sidebar). |
| `--sidebar-foreground` | `oklch(0.141 0.005 285.823)` / `oklch(0.985 0 0)` | Sidebar text. |
@@ -544,6 +544,11 @@ and dark differ, both defaults are given.
| `--menu-item-hover-color` | `var(--accent-foreground)` | Text of the highlighted menu item. |
| `--display-chip-pressed-background` | `var(--input)` | Fill of a pressed on/off chip in the Display and sidebar settings popovers. |
| `--display-chip-pressed-color` | `var(--foreground)` | Text of a pressed display chip. |
+| `--switch-checked-background` | `var(--primary)` | Track of a switch that is on. |
+| `--switch-unchecked-background` | `var(--input)` / `color-mix(in oklab, var(--input) 80%, transparent)` | Track of a switch that is off. |
+| `--switch-checked-thumb` | `var(--background)` / `var(--primary-foreground)` | Thumb of a switch that is on. |
+| `--switch-unchecked-thumb` | `var(--background)` / `var(--foreground)` | Thumb of a switch that is off. |
+| `--switch-border-color` | `transparent` | Outline of a switch, on or off. |
| `--popover-radius` | `var(--radius)` | Popovers. |
| `--floating-border-color` | `color-mix(in oklab, var(--surface-border), var(--foreground) 6%)` | Hairline around layers that float over the page without a backdrop: menus, popovers, select lists and floating panels. |
| `--dialog-radius` | `calc(var(--radius) * 1.5)` | Dialogs and alert dialogs. |
diff --git a/packages/core/appearance/built-in-themes.ts b/packages/core/appearance/built-in-themes.ts
index 334d5cabcb..65dbf72748 100644
--- a/packages/core/appearance/built-in-themes.ts
+++ b/packages/core/appearance/built-in-themes.ts
@@ -77,6 +77,12 @@ const POP_SHARED: ThemeTokens = {
"menu-item-hover-color": "var(--foreground)",
"display-chip-pressed-background": "var(--surface-selected)",
"display-chip-pressed-color": "var(--foreground)",
+ // --primary and --input are the same ink, so switches get their own fills.
+ "switch-checked-background": "var(--button-primary)",
+ "switch-unchecked-background": "var(--surface)",
+ "switch-checked-thumb": "var(--button-primary-foreground)",
+ "switch-unchecked-thumb": "var(--surface-border)",
+ "switch-border-color": "var(--surface-border)",
"label-radius": "0.25rem",
"board-column-radius": "0.25rem",
"board-card-radius": "0.25rem",
@@ -151,13 +157,6 @@ const POP_CSS = [
"/* Code blocks sit on the same hard shadow as cards. */",
`:root[data-theme-option-offset-shadow] [data-mc="content"] pre { box-shadow: 3px 3px 0 var(--surface-border); }`,
"",
- "/* Switches: --primary and --input are the same ink in both modes, so on and off get their own fills. */",
- '[data-slot="switch"] { border-color: var(--surface-border); }',
- '[data-slot="switch"][data-unchecked] { background-color: var(--surface); }',
- '[data-slot="switch"][data-checked] { background-color: var(--button-primary); }',
- '[data-slot="switch-thumb"] { background-color: var(--surface-border); }',
- '[data-slot="switch"][data-checked] [data-slot="switch-thumb"] { background-color: var(--button-primary-foreground); }',
- "",
"/* Thick signal underlines, and an ink outline on the current sidebar item. */",
'[data-mc="content"] a { text-decoration-thickness: 3px; }',
'[data-mc="nav-item"][data-active="true"] { box-shadow: inset 0 0 0 1px var(--surface-border); }',
diff --git a/packages/core/appearance/theme-variables.ts b/packages/core/appearance/theme-variables.ts
index 1b6235370b..44f1403daa 100644
--- a/packages/core/appearance/theme-variables.ts
+++ b/packages/core/appearance/theme-variables.ts
@@ -90,7 +90,7 @@ const COLORS: Entry[] = [
["card-foreground", "Text on a card."],
["popover", "Shadcn alias; follows `surface-raised` unless set."],
["popover-foreground", "Text in menus and popovers."],
- ["primary", "Primary buttons and strong marks; also used as text."],
+ ["primary", "Primary buttons and strong marks; also used as text. Fills switches that are on unless `switch-checked-background` is set."],
["primary-foreground", "Text on primary."],
["secondary", "Secondary buttons."],
["secondary-foreground", "Text on secondary."],
@@ -100,7 +100,7 @@ const COLORS: Entry[] = [
["accent", "Hover fill in menus and lists."],
["accent-foreground", "Text on accent."],
["border", "Dividers and table borders."],
- ["input", "Input borders, and pressed display chips unless `display-chip-pressed-background` is set."],
+ ["input", "Input borders. Also fills pressed display chips and switches that are off, unless `display-chip-pressed-background` and `switch-unchecked-background` are set."],
["ring", "Focus ring."],
["sidebar", "Sidebar background (shadcn sidebar)."],
["sidebar-foreground", "Sidebar text."],
@@ -211,6 +211,11 @@ const CONTROLS: Entry[] = [
["menu-item-hover-color", "Text of the highlighted menu item.", "var(--accent-foreground)"],
["display-chip-pressed-background", "Fill of a pressed on/off chip in the Display and sidebar settings popovers.", "var(--input)"],
["display-chip-pressed-color", "Text of a pressed display chip.", "var(--foreground)"],
+ ["switch-checked-background", "Track of a switch that is on.", "var(--primary)"],
+ ["switch-unchecked-background", "Track of a switch that is off.", "var(--input)", "color-mix(in oklab, var(--input) 80%, transparent)"],
+ ["switch-checked-thumb", "Thumb of a switch that is on.", "var(--background)", "var(--primary-foreground)"],
+ ["switch-unchecked-thumb", "Thumb of a switch that is off.", "var(--background)", "var(--foreground)"],
+ ["switch-border-color", "Outline of a switch, on or off.", "transparent"],
["popover-radius", "Popovers.", "var(--radius)"],
["floating-border-color", "Hairline around layers that float over the page without a backdrop: menus, popovers, select lists and floating panels.", "color-mix(in oklab, var(--surface-border), var(--foreground) 6%)"],
["dialog-radius", "Dialogs and alert dialogs.", "calc(var(--radius) * 1.5)"],
diff --git a/packages/ui/components/ui/switch.tsx b/packages/ui/components/ui/switch.tsx
index 75c5d42647..3cbd632e78 100644
--- a/packages/ui/components/ui/switch.tsx
+++ b/packages/ui/components/ui/switch.tsx
@@ -16,14 +16,14 @@ function Switch({
data-slot="switch"
data-size={size}
className={cn(
- "peer group/switch relative inline-flex shrink-0 items-center rounded-full border border-transparent transition-all outline-none after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 data-checked:bg-primary data-unchecked:bg-input dark:data-unchecked:bg-input/80 data-disabled:cursor-not-allowed data-disabled:opacity-50",
+ "peer group/switch relative inline-flex shrink-0 items-center rounded-full border border-switch-border transition-all outline-none after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 data-checked:bg-switch-checked data-unchecked:bg-switch-unchecked dark:data-unchecked:bg-switch-unchecked-dark data-disabled:cursor-not-allowed data-disabled:opacity-50",
className
)}
{...props}
>
<SwitchPrimitive.Thumb
data-slot="switch-thumb"
- className="pointer-events-none block rounded-full bg-background ring-0 transition-transform group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-checked:translate-x-[calc(100%-2px)] dark:data-checked:bg-primary-foreground group-data-[size=default]/switch:data-unchecked:translate-x-0 group-data-[size=sm]/switch:data-unchecked:translate-x-0 dark:data-unchecked:bg-foreground"
+ className="pointer-events-none block rounded-full ring-0 transition-transform group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-checked:translate-x-[calc(100%-2px)] data-checked:bg-switch-checked-thumb dark:data-checked:bg-switch-checked-thumb-dark group-data-[size=default]/switch:data-unchecked:translate-x-0 group-data-[size=sm]/switch:data-unchecked:translate-x-0 data-unchecked:bg-switch-unchecked-thumb dark:data-unchecked:bg-switch-unchecked-thumb-dark"
/>
</SwitchPrimitive.Root>
)
diff --git a/packages/ui/styles/theme-variables.css b/packages/ui/styles/theme-variables.css
index 2756ffd8ef..f86937362a 100644
--- a/packages/ui/styles/theme-variables.css
+++ b/packages/ui/styles/theme-variables.css
@@ -34,6 +34,16 @@
--color-tooltip-foreground: var(--tooltip-foreground, var(--foreground));
--color-display-chip-pressed: var(--display-chip-pressed-background, var(--input));
--color-display-chip-pressed-foreground: var(--display-chip-pressed-color, var(--foreground));
+ /* Switches fall back to different tokens per mode, so the dark-mode
+ defaults are aliases of their own that read the same public names. */
+ --color-switch-checked: var(--switch-checked-background, var(--primary));
+ --color-switch-unchecked: var(--switch-unchecked-background, var(--input));
+ --color-switch-unchecked-dark: var(--switch-unchecked-background, color-mix(in oklab, var(--input) 80%, transparent));
+ --color-switch-checked-thumb: var(--switch-checked-thumb, var(--background));
+ --color-switch-checked-thumb-dark: var(--switch-checked-thumb, var(--primary-foreground));
+ --color-switch-unchecked-thumb: var(--switch-unchecked-thumb, var(--background));
+ --color-switch-unchecked-thumb-dark: var(--switch-unchecked-thumb, var(--foreground));
+ --color-switch-border: var(--switch-border-color, transparent);
--color-nav-item: var(--nav-item-color, var(--muted-foreground));
--color-nav-item-hover: var(--nav-item-hover-background, var(--surface-hover));
--color-nav-item-hover-foreground: var(--nav-item-hover-color, var(--foreground));
diff --git a/scripts/theme-preview.test.mjs b/scripts/theme-preview.test.mjs
index 349394e076..678a9fee08 100644
--- a/scripts/theme-preview.test.mjs
+++ b/scripts/theme-preview.test.mjs
@@ -137,9 +137,26 @@ const EXPECTED = [
classes: ["ring-floating-border", "shadow-[var(--floating-shadow)]"],
source: "packages/views/issues/components/issue-peek.tsx",
},
+ {
+ what: "the switches",
+ find: (els) => els.filter((el) => el.attrs["data-slot"] === "switch"),
+ classes: ["border-switch-border", "data-checked:bg-switch-checked", "data-unchecked:bg-switch-unchecked", "dark:data-unchecked:bg-switch-unchecked-dark"],
+ source: "packages/ui/components/ui/switch.tsx",
+ },
+ {
+ what: "the switch thumbs",
+ find: (els) => els.filter((el) => el.attrs["data-slot"] === "switch-thumb"),
+ classes: [
+ "data-checked:bg-switch-checked-thumb",
+ "dark:data-checked:bg-switch-checked-thumb-dark",
+ "data-unchecked:bg-switch-unchecked-thumb",
+ "dark:data-unchecked:bg-switch-unchecked-thumb-dark",
+ ],
+ source: "packages/ui/components/ui/switch.tsx",
+ },
{
what: "the small buttons",
- find: (els) => els.filter((el) => ["sm", "icon-sm", "icon-xs"].includes(el.attrs["data-size"])),
+ find: (els) => els.filter((el) => "data-variant" in el.attrs && ["sm", "icon-sm", "icon-xs"].includes(el.attrs["data-size"])),
classes: ["rounded-button-sm"],
source: "packages/ui/components/ui/button.tsx",
},
@@ -223,6 +240,21 @@ test("status and priority glyphs use the colour of their status and priority", (
}
});
+test("switches are drawn on and off, with thumbs in the same state", () => {
+ const elements = parseFixture();
+ const switches = elements.filter((el) => el.attrs["data-slot"] === "switch");
+ for (const state of ["data-checked", "data-unchecked"]) {
+ assert.ok(switches.some((el) => state in el.attrs), `fixture.html draws no switch with ${state}`);
+ }
+ for (const el of switches) {
+ const state = "data-checked" in el.attrs ? "data-checked" : "data-unchecked";
+ assert.equal(el.attrs["aria-checked"], String(state === "data-checked"), `a switch with ${state} has the wrong aria-checked`);
+ const thumbs = descendants(elements, el).filter((child) => child.attrs["data-slot"] === "switch-thumb");
+ assert.equal(thumbs.length, 1, "each switch has one thumb");
+ assert.ok(state in thumbs[0].attrs, `the thumb of a switch with ${state} lacks it`);
+ }
+});
+
test("every component alias is drawn or listed as not drawn", () => {
const elements = parseFixture();
const { radius, color } = componentAliases();
diff --git a/scripts/theme-preview/fixture.html b/scripts/theme-preview/fixture.html
index ce99680f12..2cefaf0a84 100644
--- a/scripts/theme-preview/fixture.html
+++ b/scripts/theme-preview/fixture.html
@@ -7,9 +7,9 @@
Each element copies the structure and the Tailwind aliases of the component
it stands for (app-rail.tsx, space-panel.tsx, panel-nav.tsx, list-view.tsx,
list-row.tsx, label-chip.tsx, issue-peek.tsx, comment-card.tsx,
- comment-input.tsx). States a component sets in script are written out:
- `aria-current`, `data-active`, `data-peeked`, the selected row's
- background. scripts/theme-preview.test.mjs checks the aliases against
+ comment-input.tsx, switch.tsx). States a component sets in script are
+ written out: `aria-current`, `data-active`, `data-peeked`, `data-checked`
+ and `data-unchecked`, the selected row's background. scripts/theme-preview.test.mjs checks the aliases against
packages/ui/styles/theme-variables.css. Class names here are only for the
fixture; themes must not rely on them.
-->
@@ -149,7 +149,6 @@
<div data-mc="content" class="rich-text-editor mt-5 text-body">
<p>New workspaces should see a short checklist on the dashboard: connect a repository, create an agent and assign it a first issue.</p>
<ul>
- <li><p>Hide it once every step is done.</p></li>
<li><p>Keep the copy in <code>checklist.json</code>.</p></li>
</ul>
<blockquote><p>Measure how many teams finish it in a week.</p></blockquote>
@@ -158,7 +157,10 @@
<h2 class="text-title-sm font-semibold">Activity</h2>
<div class="mt-4 flex flex-col gap-3 px-4 pb-3">
<div class="flex items-center text-label text-muted-foreground"><div class="mr-2 flex w-4 shrink-0 justify-center"><span data-slot="avatar" class="inline-flex size-4 shrink-0 items-center justify-center overflow-hidden rounded-full bg-muted text-[9px] font-medium text-muted-foreground">A</span></div><div class="flex min-w-0 flex-1 items-center gap-1"><span class="shrink-0 font-medium">Ada</span><span class="truncate">created this issue</span><span class="ml-auto shrink-0">3h ago</span></div></div>
- <div class="flex items-center text-label text-muted-foreground"><div class="mr-2 flex w-4 shrink-0 justify-center"><span data-slot="avatar" class="inline-flex size-4 shrink-0 items-center justify-center overflow-hidden rounded-full bg-muted text-[9px] font-medium text-muted-foreground">L</span></div><div class="flex min-w-0 flex-1 items-center gap-1"><span class="shrink-0 font-medium">Lambda</span><span class="truncate">changed the status to In Progress</span><span class="ml-auto shrink-0">2h ago</span></div></div>
+ </div>
+ <div class="mb-2 flex flex-col">
+ <div class="flex min-h-8 items-center gap-2 px-2 text-label"><span class="min-w-0 flex-1 truncate">Wake Lambda when the PR merges</span><span role="switch" aria-checked="true" data-slot="switch" data-size="sm" data-checked="" class="peer group/switch relative inline-flex shrink-0 items-center rounded-full border border-switch-border transition-all outline-none data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] data-checked:bg-switch-checked data-unchecked:bg-switch-unchecked dark:data-unchecked:bg-switch-unchecked-dark"><span data-slot="switch-thumb" data-checked="" class="pointer-events-none block rounded-full ring-0 transition-transform group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-checked:translate-x-[calc(100%-2px)] data-checked:bg-switch-checked-thumb dark:data-checked:bg-switch-checked-thumb-dark group-data-[size=default]/switch:data-unchecked:translate-x-0 group-data-[size=sm]/switch:data-unchecked:translate-x-0 data-unchecked:bg-switch-unchecked-thumb dark:data-unchecked:bg-switch-unchecked-thumb-dark"></span></span></div>
+ <div class="flex min-h-8 items-center gap-2 px-2 text-label"><span class="min-w-0 flex-1 truncate">Remind Ada tomorrow at 9:00</span><span role="switch" aria-checked="false" data-slot="switch" data-size="sm" data-unchecked="" class="peer group/switch relative inline-flex shrink-0 items-center rounded-full border border-switch-border transition-all outline-none data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] data-checked:bg-switch-checked data-unchecked:bg-switch-unchecked dark:data-unchecked:bg-switch-unchecked-dark"><span data-slot="switch-thumb" data-unchecked="" class="pointer-events-none block rounded-full ring-0 transition-transform group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-checked:translate-x-[calc(100%-2px)] data-checked:bg-switch-checked-thumb dark:data-checked:bg-switch-checked-thumb-dark group-data-[size=default]/switch:data-unchecked:translate-x-0 group-data-[size=sm]/switch:data-unchecked:translate-x-0 data-unchecked:bg-switch-unchecked-thumb dark:data-unchecked:bg-switch-unchecked-thumb-dark"></span></span></div>
</div>
<div data-slot="card" data-size="default" data-mc="comment" data-author-type="agent" class="flex flex-col overflow-clip rounded-card border border-surface-border bg-surface text-body text-surface-foreground shadow-[var(--surface-shadow)]">
<div class="mb-1 bg-card px-4 pt-3 pb-1">