feat(webui): dark/light switch in the navbar (ui slice 1)
This commit is contained in:
1 parent
f12021c6d7
commit
545f77dd86
6 files changed
+57
-6
No files matched your search
@@ -50,6 +50,11 @@
|
||||
const row = id => data?.sessions.find(r => key(r) === id);
|
||||
const announce = text => { $('announce').textContent = text; };
|
||||
// Colours come from tokens.css by data-palette and data-mode. System removes data-mode, so the OS setting picks light or dark.
|
||||
const darkPreference = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
function syncThemeSwitch() {
|
||||
const dark = mode === 'dark' || mode === 'dim' || (mode === 'system' && darkPreference.matches);
|
||||
$('theme-switch').setAttribute('aria-checked', String(dark));
|
||||
}
|
||||
function theme() {
|
||||
const p = Brand.palettes.find(p => p.id === palette) || Brand.palettes[0];
|
||||
palette = p.id;
|
||||
@@ -57,6 +62,7 @@
|
||||
document.documentElement.dataset.palette = palette;
|
||||
if (mode === 'system') delete document.documentElement.dataset.mode; else document.documentElement.dataset.mode = mode;
|
||||
$('palette').value = palette; $('mode').value = mode;
|
||||
syncThemeSwitch();
|
||||
try { localStorage.setItem('mosaic-console-appearance', JSON.stringify({ palette, mode })); } catch {}
|
||||
}
|
||||
$('palette').innerHTML = Brand.palettes.map(p => `<option value="${p.id}">${p.name}</option>`).join('');
|
||||
@@ -64,6 +70,9 @@
|
||||
theme();
|
||||
$('palette').onchange = e => { palette = e.target.value; theme(); };
|
||||
$('mode').onchange = e => { mode = e.target.value; theme(); };
|
||||
$('mobile-refresh').onclick = () => $('refresh').click();
|
||||
$('theme-switch').onclick = () => { $('mode').value = $('theme-switch').getAttribute('aria-checked') === 'true' ? 'light' : 'dark'; $('mode').dispatchEvent(new Event('change', { bubbles: true })); };
|
||||
darkPreference.addEventListener('change', syncThemeSwitch);
|
||||
function seenButton(r) {
|
||||
if (!['waiting', 'error'].includes(r.state) || !r.lastActivity) return '';
|
||||
return `<button type="button" class="btn" data-seen="${esc(key(r))}" data-intent="${r.seen ? 'unsee' : 'seen'}" aria-label="${r.seen ? 'Unsee' : 'Mark seen'} ${esc(r.agent)}">${r.seen ? 'Unsee' : 'Seen'}</button>`;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
<header class="cmdbar"><button type="button" id="nav-open" class="btn" aria-controls="console-nav" aria-expanded="false" aria-label="Open menu" title="Menu"><svg class="i" viewBox="0 0 24 24" aria-hidden="true"><path d="M4 6h16M4 12h16M4 18h16"/></svg></button><a href="#/" class="wordmark" aria-label="Mosaic Stack Dashboard"><svg class="mosaic-logo" viewBox="0 0 36 36" aria-hidden="true"><rect x="3" y="12" width="13" height="10" rx="2"/><rect x="18" y="3" width="13" height="12" rx="2"/><rect x="3" y="24" width="13" height="10" rx="2"/><rect x="18" y="17" width="13" height="12" rx="2"/></svg><span>Mosaic Stack</span></a>
|
||||
<button type="button" class="cmdk" id="cmdk" aria-haspopup="dialog" aria-controls="cmdk-dialog" aria-label="Search" title="Search"><svg class="i" aria-hidden="true"><use href="/icons.svg#i-search"/></svg><span>Go to a view, decision or task</span><kbd>Ctrl K</kbd></button>
|
||||
<nav id="federation" class="federation small" aria-label="Consoles" hidden></nav><p class="fresh" id="fresh"><span id="fresh-board"></span><span id="fresh-bus"></span></p>
|
||||
<div class="cmd-right"><a href="#/" id="processing-link" class="processing-link"><span aria-hidden="true">●</span> <span id="processing-short">Processing unavailable</span></a><button class="header-action" id="refresh" type="button" aria-label="Refresh" title="Refresh"><svg class="i" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 7v5h-5M4 17v-5h5M5 9a7 7 0 0 1 12-3l3 3M19 15a7 7 0 0 1-12 3l-3-3"/></svg><span>Refresh</span></button><button class="header-action" id="pause" type="button" aria-label="Pause" title="Pause" aria-pressed="false"><svg class="i" viewBox="0 0 24 24" aria-hidden="true"><path d="M9 5v14M15 5v14"/></svg><span>Pause</span></button><div class="avatar-wrap"><button type="button" id="avatar-button" aria-haspopup="menu" aria-expanded="false" aria-controls="avatar-menu"><span class="avatar-initial">L</span><span id="avatar-name">Local operator</span><span aria-hidden="true">⌄</span></button><div id="avatar-menu" class="avatar-menu" role="menu" hidden><a href="#/settings" role="menuitem">Profile settings</a><a href="#/" id="mobile-processing-link" class="mobile-menu-item" role="menuitem">Processing unavailable</a><button id="mobile-pause" class="mobile-menu-item" type="button" role="menuitem">Pause</button><label>Palette<select id="palette"></select></label><label>Appearance<select id="mode"><option value="system">system</option><option value="light">light</option><option value="dim">dim</option><option value="dark">dark</option></select></label><form id="sign-out" method="post" action="/auth/logout" hidden><button type="submit" role="menuitem">Sign out</button></form><div id="local-sign-out" role="menuitem" aria-disabled="true">Sign out <small>Not signed in (local dev)</small></div></div></div></div></header>
|
||||
<div class="cmd-right"><a href="#/" id="processing-link" class="processing-link"><span aria-hidden="true">●</span> <span id="processing-short">Processing unavailable</span></a><button class="header-action" id="refresh" type="button" aria-label="Refresh" title="Refresh"><svg class="i" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 7v5h-5M4 17v-5h5M5 9a7 7 0 0 1 12-3l3 3M19 15a7 7 0 0 1-12 3l-3-3"/></svg><span>Refresh</span></button><button class="header-action" id="pause" type="button" aria-label="Pause" title="Pause" aria-pressed="false"><svg class="i" viewBox="0 0 24 24" aria-hidden="true"><path d="M9 5v14M15 5v14"/></svg><span>Pause</span></button><button id="theme-switch" class="theme-switch" type="button" role="switch" aria-checked="false" aria-label="Dark mode" title="Dark mode"><span class="theme-track" aria-hidden="true"><svg class="theme-sun" viewBox="0 0 24 24"><circle cx="12" cy="12" r="4"/><path d="M12 2v2m0 16v2M2 12h2m16 0h2M5 5l1.5 1.5m11 11L19 19M19 5l-1.5 1.5m-11 11L5 19"/></svg><svg class="theme-moon" viewBox="0 0 24 24"><path d="M20 15a8 8 0 0 1-11-11 8 8 0 1 0 11 11Z"/></svg><span class="theme-thumb"></span></span></button><div class="avatar-wrap"><button type="button" id="avatar-button" aria-haspopup="menu" aria-expanded="false" aria-controls="avatar-menu"><span class="avatar-initial">L</span><span id="avatar-name">Local operator</span><span aria-hidden="true">⌄</span></button><div id="avatar-menu" class="avatar-menu" role="menu" hidden><a href="#/settings" role="menuitem">Profile settings</a><a href="#/" id="mobile-processing-link" class="mobile-menu-item" role="menuitem">Processing unavailable</a><button id="mobile-refresh" class="mobile-menu-item" type="button" role="menuitem">Refresh</button><button id="mobile-pause" class="mobile-menu-item" type="button" role="menuitem">Pause</button><label>Palette<select id="palette"></select></label><label>Appearance<select id="mode"><option value="system">system</option><option value="light">light</option><option value="dim">dim</option><option value="dark">dark</option></select></label><form id="sign-out" method="post" action="/auth/logout" hidden><button type="submit" role="menuitem">Sign out</button></form><div id="local-sign-out" role="menuitem" aria-disabled="true">Sign out <small>Not signed in (local dev)</small></div></div></div></div></header>
|
||||
<div id="processing-banner" class="processing-banner" role="alert" hidden></div>
|
||||
<div id="processing-mobile"></div>
|
||||
<div class="frame">
|
||||
|
||||
@@ -108,3 +108,13 @@ body.signed-out #dashboard-summary,body.signed-out #dashboard-cards,body.signed-
|
||||
.cmd-right .processing-link{display:none}
|
||||
}
|
||||
@media(min-width:760px) and (max-width:1100px){.kpi-grid{grid-template-columns:repeat(4,minmax(0,1fr));row-gap:16px}.kpi:nth-child(4){border-right:0}}
|
||||
|
||||
/* Appearance toggle shares the existing saved appearance setting. */
|
||||
.theme-switch{display:inline-flex;align-items:center;justify-content:center;width:56px;min-width:56px;height:44px;padding:0;border:0;border-radius:var(--r);background:transparent;color:var(--muted);cursor:pointer}
|
||||
.theme-switch:hover{background:var(--raised)}
|
||||
.theme-switch:focus-visible{outline:2px solid var(--action);outline-offset:2px}
|
||||
.theme-track{position:relative;display:flex;align-items:center;justify-content:space-between;width:42px;height:24px;padding:4px;border:1px solid var(--line);border-radius:999px;background:var(--raised)}
|
||||
.theme-track svg{width:14px;height:14px;fill:none;stroke:currentColor;stroke-width:1.7;stroke-linecap:round;stroke-linejoin:round}
|
||||
.theme-thumb{position:absolute;left:3px;top:3px;width:16px;height:16px;border-radius:50%;background:var(--action);transition:transform .15s ease}
|
||||
.theme-switch[aria-checked="true"] .theme-thumb{transform:translateX(18px)}
|
||||
@media(max-width:759px){.theme-switch{width:48px;min-width:48px}.cmd-right #refresh{display:none}}
|
||||
@@ -49,17 +49,19 @@ test('avatar menu supports keyboard navigation and local or SSO sign out', {time
|
||||
assert.deepEqual(await b.evaluate('(()=>{const r=document.querySelector("#sign-out button").getBoundingClientRect();return [r.width>=44,r.height>=44]})()'),[true,true]);
|
||||
});
|
||||
|
||||
test('compact header keeps search and refresh usable, with pause in the avatar menu', {timeout:90000}, async t => {
|
||||
test('compact header keeps search and theme switch usable, with refresh and pause in the avatar menu', {timeout:90000}, async t => {
|
||||
const {f,b,wait}=await w15Fixture(t);
|
||||
await b.call('Emulation.setDeviceMetricsOverride',{width:390,height:900,deviceScaleFactor:1,mobile:false});
|
||||
await b.navigate(f.base+'/#/'); await wait('document.querySelectorAll("#dashboard-kpis .kpi").length > 0');
|
||||
const state = await b.evaluate(`(()=>{const ids=['nav-open','cmdk','refresh','avatar-button'];const boxes=ids.map(id=>document.getElementById(id).getBoundingClientRect());return {tops:boxes.map(x=>x.top), widths:boxes.map(x=>x.width), overflow:document.documentElement.scrollWidth>innerWidth, pauseVisible:!!document.querySelector('#pause').getClientRects().length, zeroBadges:document.querySelectorAll('#sections .count').length, held:document.querySelector('#held').textContent}})()`);
|
||||
const state = await b.evaluate(`(()=>{const ids=['nav-open','cmdk','theme-switch','avatar-button'];const boxes=ids.map(id=>document.getElementById(id).getBoundingClientRect());return {tops:boxes.map(x=>x.top), widths:boxes.map(x=>x.width), overflow:document.documentElement.scrollWidth>innerWidth, pauseVisible:!!document.querySelector('#pause').getClientRects().length, zeroBadges:document.querySelectorAll('#sections .count').length, held:document.querySelector('#held').textContent}})()`);
|
||||
assert.ok(Math.max(...state.tops)-Math.min(...state.tops)<15, JSON.stringify(state));
|
||||
assert.ok(state.widths.every(x=>x>=44)); assert.equal(state.overflow,false); assert.equal(state.pauseVisible,false);
|
||||
assert.equal(state.zeroBadges,0); assert.doesNotMatch(state.held,/behind the sign-in proxy/);
|
||||
await b.evaluate('document.querySelector("#cmdk").click()'); assert.equal(await b.evaluate('document.querySelector("#cmdk-dialog").open'),true);
|
||||
await b.key('Escape'); await b.evaluate('document.querySelector("#avatar-button").click()');
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#mobile-refresh").getClientRects().length && !document.querySelector("#refresh").getClientRects().length'),true);
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#mobile-pause").getClientRects().length'),true);
|
||||
assert.equal(await b.evaluate('(()=>{let n=0;document.querySelector("#refresh").addEventListener("click",()=>n++,{once:true});document.querySelector("#mobile-refresh").click();return n})()'),1);
|
||||
await b.evaluate('document.querySelector("#mobile-pause").click()');
|
||||
assert.equal(await b.evaluate('document.querySelector("#pause").getAttribute("aria-pressed")'),'true');
|
||||
});
|
||||
|
||||
@@ -326,7 +326,7 @@ test('shell in a browser: System default, seeded states, 400px, focus and the Ct
|
||||
|
||||
// Ctrl+K: opens on the views and the kept tasks, filters, Enter goes, Esc restores focus.
|
||||
await go('#/inbox', 'Inbox');
|
||||
await b.evaluate('document.querySelector("#refresh").focus()');
|
||||
await b.evaluate('document.querySelector("#theme-switch").focus()');
|
||||
await b.key('k', 'KeyK', 3); await new Promise(r => setTimeout(r, 200));
|
||||
assert.equal(await b.evaluate('document.querySelector("#cmdk-dialog").open'), false, 'Ctrl+Alt+K opens nothing');
|
||||
await b.key('k', 'KeyK', 2); await wait('document.querySelector("#cmdk-dialog").open');
|
||||
@@ -336,14 +336,14 @@ test('shell in a browser: System default, seeded states, 400px, focus and the Ct
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#cmdk-list [data-href]")].map(e=>e.dataset.href)'), ['#/agents']);
|
||||
// The view render focuses its h1; wait for it so a late render can't take focus from the next step.
|
||||
await b.key('Enter'); await wait('location.hash==="#/agents" && !document.querySelector("#cmdk-dialog").open && document.activeElement===document.querySelector("#bus-view h1") && document.activeElement.textContent==="Agents"');
|
||||
await b.evaluate('document.querySelector("#refresh").focus()');
|
||||
await b.evaluate('document.querySelector("#theme-switch").focus()');
|
||||
await b.key('k', 'KeyK', 2); await wait('document.querySelector("#cmdk-dialog").open');
|
||||
await b.call('Input.insertText', { text: 'kept' });
|
||||
assert.equal(await b.evaluate('document.querySelector("#cmdk-list [aria-selected=true]").dataset.href'), '#/tasks/vikunja%3A32%2F7');
|
||||
await b.key('ArrowDown'); await b.key('ArrowUp');
|
||||
assert.equal(await b.evaluate('document.querySelector("#cmdk-input").getAttribute("aria-activedescendant")'), 'cmdk-opt-0');
|
||||
await flat(); await shot('palette-400');
|
||||
await b.key('Escape'); await wait('!document.querySelector("#cmdk-dialog").open && document.activeElement?.id==="refresh"');
|
||||
await b.key('Escape'); await wait('!document.querySelector("#cmdk-dialog").open && document.activeElement?.id==="theme-switch"');
|
||||
await b.evaluate('{const c=document.querySelector("#cmdk");c.focus();c.click()}'); await wait('document.querySelector("#cmdk-dialog").open');
|
||||
await b.call('Input.insertText', { text: 'zzzz' });
|
||||
assert.match(await text('#cmdk-list'), /No view, decision or task matches\./);
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { w15Fixture } from './w15-fixture.mjs';
|
||||
|
||||
test('navbar theme switch follows and persists appearance', {timeout:90000}, async t => {
|
||||
const {f,b,wait}=await w15Fixture(t);
|
||||
await b.call('Emulation.setEmulatedMedia',{features:[{name:'prefers-color-scheme',value:'light'}]});
|
||||
await b.navigate(f.base+'/#/'); await wait('!!document.querySelector("#mode")');
|
||||
assert.equal(await b.evaluate('document.querySelector("#theme-switch")?.getAttribute("role")'),'switch');
|
||||
await b.evaluate('document.querySelector("#mode").value="system";document.querySelector("#mode").dispatchEvent(new Event("change",{bubbles:true}))');
|
||||
assert.equal(await b.evaluate('document.querySelector("#theme-switch").getAttribute("aria-checked")'),'false');
|
||||
await b.evaluate('document.querySelector("#theme-switch").click()');
|
||||
assert.deepEqual(await b.evaluate('[document.documentElement.dataset.mode,document.querySelector("#mode").value,document.querySelector("#theme-switch").getAttribute("aria-checked")]'),['dark','dark','true']);
|
||||
await b.call('Page.reload'); await wait('document.querySelector("#theme-switch")?.getAttribute("aria-checked") === "true"');
|
||||
assert.equal(await b.evaluate('document.documentElement.dataset.mode'),'dark');
|
||||
await b.evaluate('document.querySelector("#mode").value="dim";document.querySelector("#mode").dispatchEvent(new Event("change"))');
|
||||
assert.equal(await b.evaluate('document.querySelector("#theme-switch").getAttribute("aria-checked")'),'true');
|
||||
await b.evaluate('document.querySelector("#theme-switch").click()');
|
||||
assert.deepEqual(await b.evaluate('[document.documentElement.dataset.mode,document.querySelector("#mode").value]'),['light','light']);
|
||||
await b.evaluate('document.querySelector("#mode").value="system";document.querySelector("#mode").dispatchEvent(new Event("change"))');
|
||||
assert.equal(await b.evaluate('document.querySelector("#theme-switch").getAttribute("aria-checked")'),'false');
|
||||
await b.call('Emulation.setEmulatedMedia',{features:[{name:'prefers-color-scheme',value:'dark'}]});
|
||||
await wait('document.querySelector("#theme-switch").getAttribute("aria-checked") === "true"');
|
||||
assert.equal(await b.evaluate('document.documentElement.hasAttribute("data-mode")'),false);
|
||||
for (const width of [1440,390]) {
|
||||
await b.viewport(width,900);
|
||||
const v=await b.evaluate(`(()=>{const s=document.querySelector('#theme-switch'),a=document.querySelector('#avatar-button'),r=s.getBoundingClientRect(),ar=a.getBoundingClientRect();return {role:s.getAttribute('role'),name:s.getAttribute('aria-label'),width:r.width,height:r.height,left:r.right<=ar.left,top:Math.abs(r.top-ar.top),overflow:document.documentElement.scrollWidth>innerWidth}})()`);
|
||||
assert.deepEqual([v.role,v.name,v.width>=44,v.height>=44,v.left,v.top<15,v.overflow],['switch','Dark mode',true,true,true,true,false],JSON.stringify(v));
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user