Files
craft-libs/crates/theme/tests/strong_hover.rs
T
Martin BallandClaude Opus 5.5 ee01a2b421 theme: dark-mode accent buttons show a visible hover (wordcraft#7317 C7-14) (#3124)
* theme: dark-mode accent buttons show a visible hover (wordcraft#7317 C7-14)

accent.strong_hover/pressed offset the rest fill by the ramp distance from accent.solid to
accent.hover/pressed. A light dark-mode seed leaves those ramp steps 0.01 OKLCH-L apart, so the
hover was invisible; and a fill near white or black clamped. The offset now has a floor
(hover 0.06, pressed 0.10) and flips toward the text when there is no room away from it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE

* theme: explain names the shift's floor and flip

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE

* wordcraft#7317 C7-14: test that explaining the dark accent hover names its base and its lightness floor

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FypgCZhHKRuRB5wZUiw6BE

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 13:48:29 +00:00

56 lines
3.0 KiB
Rust

//! Primary / accent buttons (`accent.strong`) must show hover and press in every built-in theme,
//! in both modes, without the text on them dropping below the text minimum (wordcraft#7317 C7-14).
use craft_theme::contrast::contrast_ratio;
use craft_theme::{Appearance, Contrast, Mode, Size, TextSize, Variants, builtins, resolve};
/// Least OKLCH lightness change of hover and of press from rest. Below about 0.05 a fill change is
/// hard to see next to a neighbouring control.
const HOVER_MIN: f64 = 0.05;
const PRESSED_MIN: f64 = 0.08;
#[test]
fn strong_accent_hover_and_pressed_are_visible_and_keep_text_contrast() {
for theme in builtins() {
// Classic pins all three states to one grey: its bevel, not the fill, shows the state.
if theme.name == "Classic" {
continue;
}
let modes = match theme.variants {
Variants::Both => vec![Mode::Light, Mode::Dark],
Variants::LightOnly => vec![Mode::Light],
Variants::DarkOnly => vec![Mode::Dark],
};
for mode in modes {
let appearance =
Appearance { mode, contrast: Contrast::Standard, size: Size::Comfortable, text_size: TextSize::Small, reduced_motion: false };
let c = resolve(theme, appearance, None).tokens.color;
let (rest, hover, pressed) = (c.accent.strong, c.accent.strong_hover, c.accent.strong_pressed);
let l = |x: craft_theme::color::Rgba| x.to_oklch().l;
let who = format!("{} {mode:?}", theme.name);
assert!((l(hover) - l(rest)).abs() >= HOVER_MIN, "{who}: hover barely differs from rest");
assert!((l(pressed) - l(rest)).abs() >= PRESSED_MIN, "{who}: pressed barely differs from rest");
for (state, fill) in [("rest", rest), ("hover", hover), ("pressed", pressed)] {
let ratio = contrast_ratio(c.text.on_strong, fill);
assert!(ratio >= 4.5, "{who}: text.on_strong on {state} is {ratio:.2}");
}
}
}
}
/// A user asking why the hover fill differs from rest is told which token it starts from and how much
/// lightness it moves at least; the floor shown is the one the colour is held to.
#[test]
fn explaining_strong_hover_names_its_base_and_the_lightness_floor() {
use craft_theme::{ColorSource, ColorToken, explain};
let theme = builtins().iter().find(|t| t.name != "Classic" && t.variants != Variants::LightOnly).unwrap();
let appearance =
Appearance { mode: Mode::Dark, contrast: Contrast::Standard, size: Size::Comfortable, text_size: TextSize::Small, reduced_motion: false };
let e = explain(theme, appearance, None);
let origin = e.color(ColorToken::AccentStrongHover).unwrap();
let ColorSource::ShiftedFrom { min, .. } = origin.source else { panic!("hover is a shifted colour: {:?}", origin.source) };
let words = origin.source_words();
assert!(words.contains(ColorToken::AccentStrong.path()), "names the base token: {words}");
assert!(words.contains(&min.to_string()), "names the floor {min}: {words}");
}