fix: hide lone nav items and improve primary text contrast
This commit is contained in:
1 parent
cb7a899c77
commit
b9c2365bca
4 files changed
+67
-31
No files matched your search
@@ -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)),
|
||||
);
|
||||
|
||||
// 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 {
|
||||
navOpen = false;
|
||||
profileOpen = false;
|
||||
@@ -164,27 +167,29 @@
|
||||
<span>{brand}</span>
|
||||
</a>
|
||||
|
||||
<nav aria-label="Primary navigation" class="sc-nav" data-open={navOpen}>
|
||||
{#each navItems as item}
|
||||
{@const Icon = iconFor(item)}
|
||||
<a
|
||||
aria-current={isActive(item) ? 'page' : undefined}
|
||||
class="sc-nav-link"
|
||||
data-active={isActive(item)}
|
||||
href={item.href}
|
||||
onclick={(event) => {
|
||||
if (onNavigate) {
|
||||
event.preventDefault();
|
||||
onNavigate(item);
|
||||
}
|
||||
closeMenus();
|
||||
}}
|
||||
>
|
||||
<Icon aria-hidden="true" class="sc-nav-icon" size={16} strokeWidth={2} />
|
||||
{item.label}
|
||||
</a>
|
||||
{/each}
|
||||
</nav>
|
||||
{#if hasNavItems}
|
||||
<nav aria-label="Primary navigation" class="sc-nav" data-open={navOpen}>
|
||||
{#each navItems as item}
|
||||
{@const Icon = iconFor(item)}
|
||||
<a
|
||||
aria-current={isActive(item) ? 'page' : undefined}
|
||||
class="sc-nav-link"
|
||||
data-active={isActive(item)}
|
||||
href={item.href}
|
||||
onclick={(event) => {
|
||||
if (onNavigate) {
|
||||
event.preventDefault();
|
||||
onNavigate(item);
|
||||
}
|
||||
closeMenus();
|
||||
}}
|
||||
>
|
||||
<Icon aria-hidden="true" class="sc-nav-icon" size={16} strokeWidth={2} />
|
||||
{item.label}
|
||||
</a>
|
||||
{/each}
|
||||
</nav>
|
||||
{/if}
|
||||
|
||||
<div class="sc-header-actions">
|
||||
{#if actions}{@render actions()}{/if}
|
||||
@@ -273,14 +278,16 @@
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<button
|
||||
aria-expanded={navOpen}
|
||||
aria-label={navOpen ? 'Close navigation menu' : 'Open navigation menu'}
|
||||
class="sc-icon-button sc-mobile-menu-button"
|
||||
type="button"
|
||||
onclick={() => (navOpen = !navOpen)}
|
||||
>
|
||||
{navOpen ? '×' : '☰'}
|
||||
</button>
|
||||
{#if hasNavItems}
|
||||
<button
|
||||
aria-expanded={navOpen}
|
||||
aria-label={navOpen ? 'Close navigation menu' : 'Open navigation menu'}
|
||||
class="sc-icon-button sc-mobile-menu-button"
|
||||
type="button"
|
||||
onclick={() => (navOpen = !navOpen)}
|
||||
>
|
||||
{navOpen ? '×' : '☰'}
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
</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('')}`
|
||||
}
|
||||
|
||||
// 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
|
||||
// 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
|
||||
@@ -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(),
|
||||
// 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 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 [
|
||||
...primaryVars.map((name) => `${name}: ${primary} !important;`),
|
||||
`--sc-primary-hover: ${hover} !important;`,
|
||||
|
||||
@@ -54,6 +54,13 @@ describe('primary color', () => {
|
||||
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', () => {
|
||||
applyPrimaryColor('app', 'Clover');
|
||||
const [light, dark] = styleText().split("[data-theme='dark']");
|
||||
|
||||
Reference in new issue
Block a user