fix: hide lone nav items and improve primary text contrast

This commit is contained in:
Gabriel20xx committed 2026-10-26 14:12:39 +00:00
1 parent cb7a899c77
commit b9c2365bca
4 files changed
+67 -31

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@gabriel20xx/web-ui": patch
---
`Header` no longer renders the primary navigation tab, or the mobile menu toggle, when `navItems` has only one entry. A lone item is just the app's home page, so projects like OmniLogger and OutdoorGames no longer show a single "Dashboard" tab in the header.
@@ -130,6 +130,9 @@
Boolean(username && (onProfile || (showSettings && onSettings) || onAbout || aboutHref || onLogout)), Boolean(username && (onProfile || (showSettings && onSettings) || onAbout || aboutHref || onLogout)),
); );
// A lone nav item is just the app's home page, so it isn't worth a header tab (or a menu toggle).
const hasNavItems = $derived(navItems.length > 1);
function closeMenus(): void { function closeMenus(): void {
navOpen = false; navOpen = false;
profileOpen = false; profileOpen = false;
@@ -164,27 +167,29 @@
<span>{brand}</span> <span>{brand}</span>
</a> </a>
<nav aria-label="Primary navigation" class="sc-nav" data-open={navOpen}> {#if hasNavItems}
{#each navItems as item} <nav aria-label="Primary navigation" class="sc-nav" data-open={navOpen}>
{@const Icon = iconFor(item)} {#each navItems as item}
<a {@const Icon = iconFor(item)}
aria-current={isActive(item) ? 'page' : undefined} <a
class="sc-nav-link" aria-current={isActive(item) ? 'page' : undefined}
data-active={isActive(item)} class="sc-nav-link"
href={item.href} data-active={isActive(item)}
onclick={(event) => { href={item.href}
if (onNavigate) { onclick={(event) => {
event.preventDefault(); if (onNavigate) {
onNavigate(item); event.preventDefault();
} onNavigate(item);
closeMenus(); }
}} closeMenus();
> }}
<Icon aria-hidden="true" class="sc-nav-icon" size={16} strokeWidth={2} /> >
{item.label} <Icon aria-hidden="true" class="sc-nav-icon" size={16} strokeWidth={2} />
</a> {item.label}
{/each} </a>
</nav> {/each}
</nav>
{/if}
<div class="sc-header-actions"> <div class="sc-header-actions">
{#if actions}{@render actions()}{/if} {#if actions}{@render actions()}{/if}
@@ -273,14 +278,16 @@
</div> </div>
{/if} {/if}
<button {#if hasNavItems}
aria-expanded={navOpen} <button
aria-label={navOpen ? 'Close navigation menu' : 'Open navigation menu'} aria-expanded={navOpen}
class="sc-icon-button sc-mobile-menu-button" aria-label={navOpen ? 'Close navigation menu' : 'Open navigation menu'}
type="button" class="sc-icon-button sc-mobile-menu-button"
onclick={() => (navOpen = !navOpen)} type="button"
> onclick={() => (navOpen = !navOpen)}
{navOpen ? '×' : '☰'} >
</button> {navOpen ? '×' : '☰'}
</button>
{/if}
</div> </div>
</header> </header>
@@ -74,6 +74,21 @@ function mixHex(hex: string, towards: number, weight: number): string {
return `#${channels.map((c) => Math.round(c + (towards - c) * weight).toString(16).padStart(2, '0')).join('')}` return `#${channels.map((c) => Math.round(c + (towards - c) * weight).toString(16).padStart(2, '0')).join('')}`
} }
// WCAG relative luminance of a #rrggbb color, from 0 (black) to 1 (white).
function luminance(hex: string): number {
const [red, green, blue] = [1, 3, 5].map((start) => {
const channel = parseInt(hex.slice(start, start + 2), 16) / 255
return channel <= 0.03928 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4
})
return 0.2126 * red + 0.7152 * green + 0.0722 * blue
}
// WCAG contrast ratio between two #rrggbb colors, from 1 (no contrast) to 21 (black on white).
function contrastRatio(a: string, b: string): number {
const [lighter, darker] = [luminance(a), luminance(b)].sort((x, y) => y - x)
return (lighter + 0.05) / (darker + 0.05)
}
// The color is written to a stylesheet rather than inline on <html>. Shared and app CSS re-declare these // The color is written to a stylesheet rather than inline on <html>. Shared and app CSS re-declare these
// tokens on every [data-theme] element (the AppShell root, for one), and that declaration hides the value // tokens on every [data-theme] element (the AppShell root, for one), and that declaration hides the value
// <html> would pass down, so the choice never reached the app's UI. Targeting [data-theme] elements with // <html> would pass down, so the choice never reached the app's UI. Targeting [data-theme] elements with
@@ -83,7 +98,9 @@ function primaryDeclarations(color: PaletteColor, dark: boolean): string {
// Hover is lighter on dark and darker on light, as in the shared theme. It is a hex value, not color-mix(), // Hover is lighter on dark and darker on light, as in the shared theme. It is a hex value, not color-mix(),
// because consumers such as the terminal read these tokens as plain colors. // because consumers such as the terminal read these tokens as plain colors.
const hover = dark ? mixHex(primary, 255, 0.15) : mixHex(primary, 0, 0.15) const hover = dark ? mixHex(primary, 255, 0.15) : mixHex(primary, 0, 0.15)
const foreground = dark ? color.darkFg : '#ffffff' // Text on the primary is white or the palette's dark text, whichever reads better. Mid-tone light colors
// are too light for white text, so the choice follows the color rather than the theme.
const foreground = contrastRatio(primary, '#ffffff') >= contrastRatio(primary, color.darkFg) ? '#ffffff' : color.darkFg
return [ return [
...primaryVars.map((name) => `${name}: ${primary} !important;`), ...primaryVars.map((name) => `${name}: ${primary} !important;`),
`--sc-primary-hover: ${hover} !important;`, `--sc-primary-hover: ${hover} !important;`,
@@ -54,6 +54,13 @@ describe('primary color', () => {
expect(styleText()).not.toContain('#db3e24'); expect(styleText()).not.toContain('#db3e24');
}); });
it('uses dark text on light colors where white would be hard to read', () => {
applyPrimaryColor('app', 'Honey');
const [light] = styleText().split("[data-theme='dark']");
expect(light).toContain('--sc-primary-fg: #2a2b0d !important;');
expect(light).not.toContain('--sc-primary-fg: #ffffff !important;');
});
it('makes the hover lighter on dark and darker on light', () => { it('makes the hover lighter on dark and darker on light', () => {
applyPrimaryColor('app', 'Clover'); applyPrimaryColor('app', 'Clover');
const [light, dark] = styleText().split("[data-theme='dark']"); const [light, dark] = styleText().split("[data-theme='dark']");