From 3f4e1ca0982b0840ac0ac97c633be2e053e9eaf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 22 Oct 2026 13:20:54 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20feat:=20appearance=20colours=20a?= =?UTF-8?q?re=20a=20few=20presets=20plus=20a=20hue=20stripe,=20in=20light?= =?UTF-8?q?=20and=20dark=20tabs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/forge-web/assets/nav.js | 101 +++++++ crates/forge-web/src/i18n/misc.rs | 28 +- .../src/pages/settings_appearance.rs | 246 ++++++++++++++---- crates/forge-web/tests/ui_prefs_e2e.rs | 39 +-- style/main.scss | 145 ++++++++--- 5 files changed, 453 insertions(+), 106 deletions(-) diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index 99e6140..869814a 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -502,6 +502,8 @@ var i = pair.indexOf('='); if (i < 0) { return; } var name = pair.slice(0, i), value = pair.slice(i + 1); + // The custom colour sheet's href, or empty when no colour is a custom hue. + if (name === 'custom_css') { setCustomCss(value); return; } if (name.indexOf('data-') !== 0) { return; } if (value) { root.setAttribute(name, value); } else { root.removeAttribute(name); } }); @@ -738,6 +740,52 @@ // The attributes match what the shell renders: theme `auto` has no data-theme, `dark` is dark, else light. var PREVIEW_ATTR = { accent_light: 'data-accent-light', accent_dark: 'data-accent-dark', base_light: 'data-base-light', base_dark: 'data-base-dark', sidebar_side: 'data-sidebar' }; + // The custom stylesheet sets the colours of the hue choices (`hue-N`). Its link follows the four + // colour attributes on : it is added when one of them is a hue, its href is kept in step, + // and it is removed when none is. + // An empty href removes the link. The server sends the href in the live payload (`custom_css`). + function setCustomCss(href) { + var link = document.head.querySelector('link[rel="stylesheet"][href^="/theme/custom.css"]'); + if (!href) { + if (link) { link.remove(); } + return; + } + if (!link) { + link = document.createElement('link'); + link.rel = 'stylesheet'; + document.head.appendChild(link); + } + if (link.getAttribute('href') !== href) { link.setAttribute('href', href); } + } + + // Builds the custom sheet's href from the four colour attributes on while a drag or a + // preset pick is shown before the save returns. The server builds the same href on the next load. + function syncCustomCss() { + var slots = ['data-accent-light', 'data-accent-dark', 'data-base-light', 'data-base-dark']; + var vals = slots.map(function (name) { return root.getAttribute(name) || ''; }); + var custom = vals.some(function (v) { return /^hue-\d+$/.test(v); }); + if (!custom) { setCustomCss(''); return; } + setCustomCss('/theme/custom.css?al=' + encodeURIComponent(vals[0]) + '&ad=' + encodeURIComponent(vals[1]) + + '&bl=' + encodeURIComponent(vals[2]) + '&bd=' + encodeURIComponent(vals[3])); + } + + // A drag moves the stylesheet at most once a frame, however many input events it sends. + var previewFrame = 0; + function previewLater() { + if (previewFrame) { return; } + previewFrame = requestAnimationFrame(function () { previewFrame = 0; syncCustomCss(); }); + } + + // Moves a row's stripe knob to a hue, and makes the row's Custom choice that hue (the value a save posts). + // The hue is also set on the row as `--hue`, which the stripe's knob and the Custom swatch read. + function setKnob(row, hue) { + var range = row.querySelector('input[data-hue-stripe]'); + var custom = row.querySelector('input[data-hue-custom]'); + if (range) { range.value = String(hue); } + row.style.setProperty('--hue', String(hue)); + if (custom) { custom.value = 'hue-' + hue; } + } + document.addEventListener('change', function (e) { var el = e.target; if (!el || !el.form || !el.form.hasAttribute('data-appearance')) { return; } @@ -746,11 +794,63 @@ else { root.setAttribute('data-theme', el.value === 'dark' ? 'dark' : 'light'); } return; } + if (el.type === 'radio' && el.hasAttribute('data-colour-tab')) { + var group = el.closest('[data-colour-group]'); + if (group) { group.setAttribute('data-tab', el.value); } + return; + } if (el.type === 'radio' && Object.prototype.hasOwnProperty.call(PREVIEW_ATTR, el.name)) { root.setAttribute(PREVIEW_ATTR[el.name], el.value); + // A preset moves its row's knob to the preset's hue, without choosing Custom. + var row = el.closest('[data-colour-row]'); + var hue = el.getAttribute('data-hue'); + if (row && hue) { setKnob(row, hue); } + syncCustomCss(); + } + }, false); + + // Dragging a stripe selects its Custom choice and shows the hue on while the drag lasts. + // The save waits for the release (the change event), which the autosubmit handler sends. + document.addEventListener('input', function (e) { + var el = e.target; + if (!el || el.type !== 'range' || !el.hasAttribute('data-hue-stripe') || !el.form || !el.form.hasAttribute('data-appearance')) { return; } + var row = el.closest('[data-colour-row]'); + var custom = row ? row.querySelector('input[data-hue-custom]') : null; + if (!custom) { return; } + setKnob(row, el.value); + custom.checked = true; + if (Object.prototype.hasOwnProperty.call(PREVIEW_ATTR, custom.name)) { + root.setAttribute(PREVIEW_ATTR[custom.name], custom.value); } + previewLater(); }, false); + // The theme the viewer sees: `dark` or `light` on , else the system's setting. + function effectiveDark() { + var t = root.getAttribute('data-theme'); + if (t === 'dark') { return true; } + if (t === 'light') { return false; } + return !!(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches); + } + + // The colour panel opens on the theme the viewer sees. A tab the viewer picked is kept across a + // swap, because the swap restores it as a changed field. Each panel is set up once; after that its + // shown panel follows its tab radios. Called on every page change (see `enhance`). + function enhanceColours() { + document.querySelectorAll('[data-colour-group]').forEach(function (group) { + var tabs = group.querySelectorAll('input[data-colour-tab]'); + if (!group.__colourTab) { + group.__colourTab = true; + var moved = Array.prototype.some.call(tabs, function (r) { return r.checked !== r.defaultChecked; }); + if (!moved) { + Array.prototype.forEach.call(tabs, function (r) { r.checked = r.value === (effectiveDark() ? 'dark' : 'light'); }); + } + } + var on = group.querySelector('input[data-colour-tab]:checked'); + if (on) { group.setAttribute('data-tab', on.value); } + }); + } + // A filtered view asked for in place: the URL is replaced (no history entry per change), fields // and caret come through the swap, and the scroll stays put. // Live unread count. The bell is re-rendered by every swap, so it is looked up on each update. One @@ -1894,6 +1994,7 @@ // ---- toolbar and upload ---- function enhance() { + enhanceColours(); document.querySelectorAll('textarea').forEach(function (ta) { if (ta.__md || !eligible(ta)) { return; } ta.__md = true; diff --git a/crates/forge-web/src/i18n/misc.rs b/crates/forge-web/src/i18n/misc.rs index 2b22653..57ecb84 100644 --- a/crates/forge-web/src/i18n/misc.rs +++ b/crates/forge-web/src/i18n/misc.rs @@ -25,10 +25,11 @@ pub enum K { SwitchToMatchSystem, ChooseTheme, ChooseLanguage, - AccentLabel, ChooseAccent, - LightColours, - DarkColours, + ColoursLabel, + AccentRowLabel, + CustomLabel, + HueLabel, BackgroundLabel, ChooseBase, // Appearance: which side the shell sidebar sits on. @@ -175,10 +176,11 @@ const ALL: &[K] = &[ K::SwitchToMatchSystem, K::ChooseTheme, K::ChooseLanguage, - K::AccentLabel, K::ChooseAccent, - K::LightColours, - K::DarkColours, + K::ColoursLabel, + K::AccentRowLabel, + K::CustomLabel, + K::HueLabel, K::BackgroundLabel, K::ChooseBase, K::SidebarLabel, @@ -330,14 +332,16 @@ pub fn t(lang: Lang, key: K) -> &'static str { (ChooseTheme, Tr) => "Listeden bir tema seçin.", (ChooseLanguage, En) => "Choose a language from the list.", (ChooseLanguage, Tr) => "Listeden bir dil seçin.", - (AccentLabel, En) => "Accent colour", - (AccentLabel, Tr) => "Vurgu rengi", (ChooseAccent, En) => "Choose an accent colour from the list.", (ChooseAccent, Tr) => "Listeden bir vurgu rengi seçin.", - (LightColours, En) => "Light theme", - (LightColours, Tr) => "Açık tema", - (DarkColours, En) => "Dark theme", - (DarkColours, Tr) => "Koyu tema", + (ColoursLabel, En) => "Colours", + (ColoursLabel, Tr) => "Renkler", + (AccentRowLabel, En) => "Accent", + (AccentRowLabel, Tr) => "Vurgu", + (CustomLabel, En) => "Custom", + (CustomLabel, Tr) => "Özel", + (HueLabel, En) => "Hue", + (HueLabel, Tr) => "Ton", (BackgroundLabel, En) => "Background", (BackgroundLabel, Tr) => "Arka plan", (ChooseBase, En) => "Choose a background from the list.", diff --git a/crates/forge-web/src/pages/settings_appearance.rs b/crates/forge-web/src/pages/settings_appearance.rs index 3fa8f04..5aedeb6 100644 --- a/crates/forge-web/src/pages/settings_appearance.rs +++ b/crates/forge-web/src/pages/settings_appearance.rs @@ -15,7 +15,7 @@ use topcoat::router::error::{SeeOther, see_other}; use topcoat::router::{page, route}; use topcoat::view::{View, component, view}; -use crate::accent; +use crate::accent::{self, hue_of, preset_accent_hue, preset_base_hue}; use crate::audit::{self, Actor, Target}; use crate::auth::{require_csrf, require_user}; use crate::i18n::accents::{accent_name, base_name}; @@ -34,6 +34,9 @@ const THEME_CODES: &[&str] = &["auto", "light", "dark"]; /// Sidebar sides, in display order. Must match `store::ui_prefs::SIDEBAR_SIDES`. const SIDEBAR_CODES: &[&str] = &["left", "right"]; +/// The colour tabs, in display order: the light theme's colours, then the dark theme's. +const TAB_CODES: &[&str] = &["light", "dark"]; + /// Reads a field that is present as a string, even an empty one. The plain `Option` /// reading would turn an empty value into `None`, which is the same as a missing field. fn present_string<'de, D: Deserializer<'de>>( @@ -69,17 +72,29 @@ struct Choice { value: &'static str, label: &'static str, selected: bool, + /// The hue a preset moves the stripe knob to, as text; empty when the preset has none. + hue: String, +} + +/// Where a hue stripe's knob sits. `custom` says the stored choice is that hue, rather than a +/// preset whose hue the knob was moved to. +struct Stripe { + hue: u16, + custom: bool, } -/// The colour choices of one theme: its background and its accent colour. `dark` picks the -/// field names and the swatch attributes that belong to the dark theme. +/// The colour choices of one theme: a background and an accent, each a row of presets and a hue +/// stripe. `dark` picks the field names and the swatch attributes that belong to the dark theme. struct ColourGroup { dark: bool, - heading: &'static str, background: &'static str, accent: &'static str, + custom: &'static str, + hue: &'static str, bases: Vec, accents: Vec, + base_stripe: Stripe, + accent_stripe: Stripe, } struct AppearancePage { @@ -87,6 +102,10 @@ struct AppearancePage { csrf: String, theme: Vec, language: Vec, + colours: &'static str, + /// The tab that opens first: the light or the dark colours. + tab: &'static str, + tabs: Vec, light: ColourGroup, dark: ColourGroup, sidebar: Vec, @@ -134,52 +153,103 @@ fn choices( value, label: label(value), selected: value == current, + hue: String::new(), }) .collect() } -/// One theme's colour choices, with the stored (resolved) background and accent selected. +/// The label of a colour tab: the light or the dark theme. +fn tab_label(lang: Lang, code: &str) -> &'static str { + if code == "dark" { + g::t(lang, K::Dark) + } else { + g::t(lang, K::Light) + } +} + +/// Fills in the hue each preset moves the stripe knob to. +fn with_hues(mut list: Vec, hue: fn(&str) -> Option) -> Vec { + for choice in &mut list { + choice.hue = hue(choice.value).map(|h| h.to_string()).unwrap_or_default(); + } + list +} + +/// The id a row shows as selected. A custom hue stays as stored; a preset is resolved, so an id +/// that is no longer offered shows the default, as the page renders it on ``. +fn shown<'a>(stored: &'a str, resolved: &'static str) -> &'a str { + if hue_of(stored).is_some() { + stored + } else { + resolved + } +} + +/// The stripe of one row. A stored custom hue puts the knob on it; a preset puts the knob on the +/// preset's hue, and nothing is selected as custom. +fn stripe(stored: &str, preset: Option) -> Stripe { + match hue_of(stored) { + Some(hue) => Stripe { hue, custom: true }, + None => Stripe { + hue: preset.unwrap_or(0), + custom: false, + }, + } +} + +/// One theme's colour choices, with the stored background and accent selected. fn colour_group(lang: Lang, dark: bool, base: &str, accent_id: &str) -> ColourGroup { let base_codes: Vec<&'static str> = accent::base_ids().collect(); let accent_codes: Vec<&'static str> = accent::ids().collect(); + let base_shown = shown(base, accent::resolve_base(base).id); + let accent_shown = shown(accent_id, accent::resolve(accent_id).id); ColourGroup { dark, - heading: g::t( - lang, - if dark { - K::DarkColours - } else { - K::LightColours - }, - ), background: g::t(lang, K::BackgroundLabel), - accent: g::t(lang, K::AccentLabel), - bases: choices(&base_codes, base, |id| base_name(lang, id)), - accents: choices(&accent_codes, accent_id, |id| accent_name(lang, id)), + accent: g::t(lang, K::AccentRowLabel), + custom: g::t(lang, K::CustomLabel), + hue: g::t(lang, K::HueLabel), + bases: with_hues( + choices(&base_codes, base_shown, |id| base_name(lang, id)), + preset_base_hue, + ), + accents: with_hues( + choices(&accent_codes, accent_shown, |id| accent_name(lang, id)), + preset_accent_hue, + ), + base_stripe: stripe(base, preset_base_hue(base_shown)), + accent_stripe: stripe(accent_id, preset_accent_hue(accent_shown)), } } +/// One theme's panel: a Background row and an Accent row. Each row is its presets as round +/// radios, then a hue stripe whose Custom radio carries the `hue-N` value. #[component] -async fn colour_group_fields(group: ColourGroup) -> Result { +async fn colour_panel(group: ColourGroup) -> Result { let ColourGroup { dark, - heading, background, accent, + custom, + hue, bases, accents, + base_stripe, + accent_stripe, } = group; let base_field = if dark { "base_dark" } else { "base_light" }; let accent_field = if dark { "accent_dark" } else { "accent_light" }; + let panel = if dark { "dark" } else { "light" }; + let base_custom = format!("hue-{}", base_stripe.hue); + let accent_custom = format!("hue-{}", accent_stripe.hue); Ok(view! { -
- (heading) -
+
+
(background)
for choice in bases.into_iter() { }
+
+ + +
-
+
(accent)
for choice in accents.into_iter() { }
+
+ + +
-
+
}) } @@ -217,6 +303,9 @@ async fn appearance_body(page: AppearancePage) -> Result { csrf, theme, language, + colours, + tab, + tabs, light, dark, sidebar, @@ -258,10 +347,19 @@ async fn appearance_body(page: AppearancePage) -> Result {
-
- colour_group_fields(group: light) - colour_group_fields(group: dark) -
+
+ (colours) +
+ for choice in tabs.into_iter() { + + } +
+ colour_panel(group: light) + colour_panel(group: dark) +
}) @@ -318,6 +416,13 @@ pub fn sidebar_stored(stored: &str) -> &'static str { /// stored id is no longer offered, which is what the page renders on `` too. fn page_for(viewer: &(User, Session), lang: Lang) -> AppearancePage { let user = &viewer.0; + // The tab that opens first is the dark colours for a dark theme. The page script moves it to + // the one the viewer sees when the theme follows the system. + let tab = if user.theme == "dark" { + "dark" + } else { + "light" + }; AppearancePage { lang, csrf: viewer.1.csrf_token.clone(), @@ -325,18 +430,11 @@ fn page_for(viewer: &(User, Session), lang: Lang) -> AppearancePage { language: choices(LANGUAGE_CODES, &user.language, |code| { language_label(lang, code) }), - light: colour_group( - lang, - false, - accent::resolve_base(&user.base_light).id, - accent::resolve(&user.accent).id, - ), - dark: colour_group( - lang, - true, - accent::resolve_base(&user.base_dark).id, - accent::resolve(&user.accent_dark).id, - ), + colours: g::t(lang, K::ColoursLabel), + tab, + tabs: choices(TAB_CODES, tab, |code| tab_label(lang, code)), + light: colour_group(lang, false, &user.base_light, &user.accent), + dark: colour_group(lang, true, &user.base_dark, &user.accent_dark), sidebar: choices(SIDEBAR_CODES, sidebar_stored(&user.sidebar_side), |code| { sidebar_label(lang, code) }), @@ -426,18 +524,40 @@ mod tests { #[test] fn the_newer_colours_pass_and_their_wrong_cases_do_not() { for id in [ - "teal", "moss", "indigo", "amber", "clay", "berry", "ember", "crimson", "coral", - "gold", "lime", "mint", "cyan", "sky", "violet", "fuchsia", + "sage", "spark", "ocean", "plum", "rose", "gold", "hue-0", "hue-359", ] { assert_eq!(accent_choice(Lang::En, Some(id), "sage").unwrap(), id); assert!(accent_choice(Lang::Tr, Some(&id.to_uppercase()), "sage").is_err()); } - for id in ["dune", "lilac", "dusk", "blush", "graphite", "pine"] { + for id in ["fern", "spark", "graphite", "dusk", "ink", "hue-120"] { assert_eq!(base_choice(Lang::Tr, Some(id), "fern").unwrap(), id); assert!(base_choice(Lang::Tr, Some(&id.to_uppercase()), "fern").is_err()); } - assert!(accent_choice(Lang::En, Some("terracotta"), "sage").is_err()); - assert!(base_choice(Lang::En, Some("slate-blue"), "fern").is_err()); + // Removed presets, and hue values that are out of range or not canonical, are refused. + for id in [ + "teal", + "moss", + "ember", + "terracotta", + "hue-360", + "hue-07", + "hue-", + "hue-+5", + "teal", + ] { + assert!(accent_choice(Lang::En, Some(id), "sage").is_err(), "{id}"); + } + for id in [ + "dune", + "lilac", + "blush", + "pine", + "slate-blue", + "hue-360", + "hue-07", + ] { + assert!(base_choice(Lang::En, Some(id), "fern").is_err(), "{id}"); + } } #[test] @@ -491,6 +611,40 @@ mod tests { assert_eq!(group.bases[0].value, crate::accent_palette::BASE_DEFAULT); } + #[test] + fn a_stored_hue_checks_custom_and_sets_the_knob() { + let group = colour_group(Lang::En, false, "hue-200", "hue-40"); + assert!(group.base_stripe.custom); + assert_eq!(group.base_stripe.hue, 200); + assert!(group.accent_stripe.custom); + assert_eq!(group.accent_stripe.hue, 40); + assert!(group.bases.iter().all(|c| !c.selected)); + assert!(group.accents.iter().all(|c| !c.selected)); + } + + #[test] + fn a_stored_preset_checks_the_preset_and_moves_the_knob_to_its_hue() { + let group = colour_group(Lang::En, true, "ink", "plum"); + let plum_hue = preset_accent_hue("plum").unwrap(); + let ink_hue = preset_base_hue("ink").unwrap(); + assert!(!group.accent_stripe.custom); + assert_eq!(group.accent_stripe.hue, plum_hue); + assert!(!group.base_stripe.custom); + assert_eq!(group.base_stripe.hue, ink_hue); + let plum = group.accents.iter().find(|c| c.value == "plum").unwrap(); + assert!(plum.selected); + assert_eq!(plum.hue, plum_hue.to_string()); + } + + #[test] + fn a_stored_hue_is_only_read_in_range() { + assert_eq!(hue_of("hue-359"), Some(359)); + assert_eq!(hue_of("hue-360"), None); + assert_eq!(hue_of("hue-"), None); + assert_eq!(hue_of("hue-+5"), None); + assert_eq!(hue_of("sage"), None); + } + #[test] fn labels_follow_the_page_language() { assert_eq!(theme_label(Lang::En, "dark"), "Dark"); diff --git a/crates/forge-web/tests/ui_prefs_e2e.rs b/crates/forge-web/tests/ui_prefs_e2e.rs index 8c7d256..b322865 100644 --- a/crates/forge-web/tests/ui_prefs_e2e.rs +++ b/crates/forge-web/tests/ui_prefs_e2e.rs @@ -309,7 +309,10 @@ async fn the_form_has_no_save_button_and_every_field_autosubmits() { scraper::Selector::parse("form[data-appearance] input, form[data-appearance] select") .expect("selector"); for field in doc.select(&fields) { - if field.value().attr("type") == Some("hidden") { + // The hidden fields never save, and the colour tab switch only shows a panel. + if field.value().attr("type") == Some("hidden") + || field.value().attr("data-colour-tab").is_some() + { continue; } assert!( @@ -342,7 +345,7 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() { ("accent_light", "ocean"), ("accent_dark", "plum"), ("base_light", "ink"), - ("base_dark", "mist"), + ("base_dark", "dusk"), ], Some(&session), ) @@ -350,7 +353,7 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() { assert_eq!(saved.status, 303, "save: {}", saved.body); let page = env.http.get("/settings/appearance", Some(&session)).await; - let chosen = ["ocean", "plum", "ink", "mist"].map(str::to_owned); + let chosen = ["ocean", "plum", "ink", "dusk"].map(str::to_owned); assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); assert_eq!(html_colours(&page.body), chosen.map(Some)); @@ -363,14 +366,14 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() { ("csrf", &csrf), ("theme", "light"), ("language", "en"), - ("base_dark", "sand"), + ("base_dark", "spark"), ], Some(&session), ) .await; assert_eq!(saved.status, 303, "save: {}", saved.body); let page = env.http.get("/settings/appearance", Some(&session)).await; - let chosen = ["ocean", "plum", "ink", "sand"].map(str::to_owned); + let chosen = ["ocean", "plum", "ink", "spark"].map(str::to_owned); assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); assert_eq!(html_colours(&page.body), chosen.map(Some)); @@ -396,7 +399,7 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() { assert_ne!(res.status, 303, "bad {field} {bad:?} must not save"); } let page = env.http.get("/settings/appearance", Some(&session)).await; - let chosen = ["ocean", "plum", "ink", "sand"].map(str::to_owned); + let chosen = ["ocean", "plum", "ink", "spark"].map(str::to_owned); assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); assert_eq!(html_colours(&page.body), chosen.map(Some)); @@ -409,9 +412,9 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() { ("csrf", &csrf), ("theme", "light"), ("language", "tr"), - ("accent_light", "teal"), - ("accent_dark", "berry"), - ("base_light", "dune"), + ("accent_light", "hue-200"), + ("accent_dark", "gold"), + ("base_light", "spark"), ("base_dark", "dusk"), ], Some(&session), @@ -419,7 +422,7 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() { .await; assert_eq!(saved.status, 303, "save: {}", saved.body); let page = env.http.get("/settings/appearance", Some(&session)).await; - let chosen = ["teal", "berry", "dune", "dusk"].map(str::to_owned); + let chosen = ["hue-200", "gold", "spark", "dusk"].map(str::to_owned); assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); assert_eq!(html_colours(&page.body), chosen.map(Some)); @@ -432,9 +435,9 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() { ("csrf", &csrf), ("theme", "light"), ("language", "en"), - ("accent_light", "crimson"), - ("accent_dark", "fuchsia"), - ("base_light", "pine"), + ("accent_light", "sage"), + ("accent_dark", "ocean"), + ("base_light", "ink"), ("base_dark", "graphite"), ], Some(&session), @@ -442,7 +445,7 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() { .await; assert_eq!(saved.status, 303, "save: {}", saved.body); let page = env.http.get("/settings/appearance", Some(&session)).await; - let chosen = ["crimson", "fuchsia", "pine", "graphite"].map(str::to_owned); + let chosen = ["sage", "ocean", "ink", "graphite"].map(str::to_owned); assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); assert_eq!(html_colours(&page.body), chosen.map(Some)); } @@ -463,16 +466,16 @@ async fn a_save_without_colour_fields_keeps_the_stored_colours() { ("theme", "light"), ("language", "en"), ("accent_light", "rose"), - ("accent_dark", "slate"), - ("base_light", "sand"), - ("base_dark", "stone"), + ("accent_dark", "gold"), + ("base_light", "fern"), + ("base_dark", "spark"), ], Some(&session), ) .await; assert_eq!(saved.status, 303, "save: {}", saved.body); let before = env.http.get("/settings/appearance", Some(&session)).await; - let stored = ["rose", "slate", "sand", "stone"].map(str::to_owned); + let stored = ["rose", "gold", "fern", "spark"].map(str::to_owned); assert_eq!(html_colours(&before.body), stored.clone().map(Some)); let saved = env diff --git a/style/main.scss b/style/main.scss index 27ccf2c..e9aea62 100644 --- a/style/main.scss +++ b/style/main.scss @@ -2094,8 +2094,8 @@ code { // Colour pickers (appearance settings). Each choice is a native radio, so the keyboard and screen // readers work as usual; the swatch beside it shows the colour. The swatch colours come from the -// generated _accents.scss, so nothing here names a colour. The two themes sit side by side, and -// stack on narrow screens, so the whole page fits a laptop window without a long scroll. +// generated _accents.scss, so nothing here names a colour. One theme's panel shows at a time +// (Light or Dark tabs), so the page fits a laptop window without a long scroll. .appearance { display: grid; gap: 16px; @@ -2178,13 +2178,114 @@ code { } } -// One theme card per row, so each card is wide enough for 11 labelled colour columns. The light -// card and the dark card are the same shape, so they still line up. -.appearance-columns { +// One card for both themes, with Light and Dark tabs. nav.js sets data-tab on the card; the panel +// that is not shown is hidden, but it stays in the form, so a save still posts all four colours. +.colour-group[data-tab="light"] > .colour-panel[data-colour-panel="dark"], +.colour-group[data-tab="dark"] > .colour-panel[data-colour-panel="light"] { + display: none; +} + +.colour-panel { display: grid; - grid-template-columns: minmax(0, 1fr); - gap: 16px; + gap: 18px; + min-width: 0; +} + +// The hue stripe: a range input whose track is a rainbow, with a round knob. The background stripe +// is a pale, low-saturation band so it reads as a tint. nav.js sets --hue on the row: the knob and the +// Custom swatch take their colour from it. +[data-colour-row] { + --hue: 0; +} + +.hue-row { + display: flex; + align-items: center; + gap: 12px; min-width: 0; + + // The Custom choice takes the width of one preset, so the stripe lines up with the presets' row. + > .colour-choice { + flex: none; + width: 4.5rem; + } +} + +.hue-stripe { + --track: linear-gradient(to right, hsl(0 85% 55%), hsl(60 90% 52%), hsl(120 65% 45%), hsl(180 70% 42%), hsl(240 80% 58%), hsl(300 75% 55%), hsl(360 85% 55%)); + -webkit-appearance: none; + appearance: none; + flex: 1 1 auto; + width: 100%; + min-width: 0; + height: 22px; + margin: 0; + padding: 0; + border: 0; + background: transparent; + cursor: pointer; + + &.hue-stripe-base { + --track: linear-gradient(to right, hsl(0 45% 84%), hsl(60 45% 84%), hsl(120 35% 81%), hsl(180 35% 81%), hsl(240 40% 85%), hsl(300 35% 83%), hsl(360 45% 84%)); + } + + &::-webkit-slider-runnable-track { + height: 14px; + border-radius: var(--radius-full); + background: var(--track); + } + + &::-moz-range-track { + height: 14px; + border-radius: var(--radius-full); + background: var(--track); + } + + &::-webkit-slider-thumb { + -webkit-appearance: none; + appearance: none; + box-sizing: border-box; + width: 22px; + height: 22px; + margin-top: -4px; + border-radius: 50%; + border: 3px solid var(--surface); + background: hsl(var(--hue) 80% 50%); + box-shadow: 0 0 0 1px var(--border-strong); + } + + &::-moz-range-thumb { + box-sizing: border-box; + width: 22px; + height: 22px; + border-radius: 50%; + border: 3px solid var(--surface); + background: hsl(var(--hue) 80% 50%); + box-shadow: 0 0 0 1px var(--border-strong); + } + + &.hue-stripe-base::-webkit-slider-thumb { + background: hsl(var(--hue) 40% 62%); + } + + &.hue-stripe-base::-moz-range-thumb { + background: hsl(var(--hue) 40% 62%); + } + + &:focus-visible { + outline: 2px solid var(--focus); + outline-offset: 3px; + } +} + +// The Custom swatch shows the hue the stripe is on. The accent one is saturated, the background +// one pale, like the presets beside it. +.hue-swatch { + background: hsl(var(--hue) 75% 50%); +} + +.base-swatch.hue-swatch { + background: hsl(var(--hue) 40% 82%); } .colour-group { @@ -2222,30 +2323,13 @@ code { } } -// The picker is a grid with equal columns, so rows are exact and the Background and Accent rows -// share column lines. The card is the container: wide cards get 11 columns (the 11 backgrounds -// are one row, the 22 accents two), medium cards 6, and phone cards 4. -.colour-group { - container-type: inline-size; -} - +// The presets are a wrapping row of fixed-width columns, so a phone wraps them and a wide card +// keeps them in one line. .colour-choices { - display: grid; - grid-template-columns: repeat(4, minmax(0, 1fr)); - gap: 12px 4px; -} - -@container (min-width: 30rem) { - .colour-choices { - grid-template-columns: repeat(6, minmax(0, 1fr)); - } -} - -@container (min-width: 34rem) { - .colour-choices { - grid-template-columns: repeat(11, minmax(0, 1fr)); - gap: 12px 2px; - } + display: flex; + flex-wrap: wrap; + gap: 12px 8px; + min-width: 0; } .colour-choice { @@ -2253,6 +2337,7 @@ code { flex-direction: column; align-items: center; gap: 4px; + width: 4.5rem; min-width: 0; position: relative; cursor: pointer;