From fdeb95a61baa45a903821925de52281a0e953138 Mon Sep 17 00:00:00 2001 From: iris Date: Sat, 28 Nov 2026 02:33:18 +0100 Subject: [PATCH] shared: buttons answer hover, press and keyboard focus .ui-button had a pointer cursor and nothing else: no hover, no pressed state and no focus style of its own. Each variant gets a hover fill, a press nudge and a purple focus ring, with the transition off under reduced motion. --- .../packages/shared/src/button/Button.css | 38 +++++++++++++++++++ .../swarm-ui/test/button-states.test.mjs | 36 ++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 frontend/packages/swarm-ui/test/button-states.test.mjs diff --git a/frontend/packages/shared/src/button/Button.css b/frontend/packages/shared/src/button/Button.css index fe30b8f11..8aaa27a9a 100644 --- a/frontend/packages/shared/src/button/Button.css +++ b/frontend/packages/shared/src/button/Button.css @@ -31,6 +31,44 @@ } .ui-button { position: relative; + transition: + background-color 0.12s ease, + border-color 0.12s ease, + filter 0.12s ease; +} +/* Hover, press and keyboard focus: a button answers the pointer. The primary + fill brightens (on a light palette that raises the label's contrast; on a dark + one it stays above 6:1). */ +.ui-button-default:hover:not(:disabled) { + border-color: var(--purple); + background: color-mix(in srgb, var(--purple) 8%, var(--bg-elev)); +} +.ui-button-primary:hover:not(:disabled) { + filter: brightness(1.12); +} +.ui-button-danger:hover:not(:disabled) { + background: color-mix(in srgb, var(--red) 24%, var(--bg-elev)); +} +.ui-button:active:not(:disabled) { + transform: translateY(1px); +} +.ui-button:focus-visible { + outline: 2px solid var(--purple); + outline-offset: 2px; +} +@media (prefers-reduced-motion: reduce) { + .ui-button { + transition: none; + } +} +:root[data-motion="reduce"] .ui-button { + transition: none; +} +:root[data-motion="allow"] .ui-button { + transition: + background-color 0.12s ease, + border-color 0.12s ease, + filter 0.12s ease; } /* Busy: the label keeps its space, invisible, and the spinner sits over it. `opacity`, not `visibility`: the label stays in the accessibility tree, so the diff --git a/frontend/packages/swarm-ui/test/button-states.test.mjs b/frontend/packages/swarm-ui/test/button-states.test.mjs new file mode 100644 index 000000000..d550fca4e --- /dev/null +++ b/frontend/packages/swarm-ui/test/button-states.test.mjs @@ -0,0 +1,36 @@ +// A button answers the pointer and the keyboard. Run by `npm test`. The shared +// stylesheet must give every variant a hover, a pressed and a keyboard focus +// state, and switch its transition off under reduced motion. +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { test } from "node:test"; + +const css = readFileSync( + new URL("../../shared/src/button/Button.css", import.meta.url), + "utf8", +); + +for (const variant of ["default", "primary", "danger"]) { + test(`the ${variant} button has a hover state`, () => { + assert.match( + css, + new RegExp(`\\.ui-button-${variant}:hover:not\\(:disabled\\)`), + ); + }); +} + +test("a button has a pressed and a keyboard focus state", () => { + assert.match(css, /\.ui-button:active:not\(:disabled\)/); + assert.match(css, /\.ui-button:focus-visible\s*\{[^}]*outline:/); +}); + +test("the transition is off under reduced motion", () => { + assert.match( + css, + /prefers-reduced-motion: reduce\)\s*\{\s*\.ui-button\s*\{\s*transition:\s*none/, + ); + assert.match( + css, + /:root\[data-motion="reduce"\] \.ui-button\s*\{\s*transition:\s*none/, + ); +});