fix: hide lone nav items and improve primary text contrast
This commit is contained in:
1 parent
cb7a899c77
commit
b9c2365bca
4 files changed
+37
-1
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)),
|
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,6 +167,7 @@
|
|||||||
<span>{brand}</span>
|
<span>{brand}</span>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
{#if hasNavItems}
|
||||||
<nav aria-label="Primary navigation" class="sc-nav" data-open={navOpen}>
|
<nav aria-label="Primary navigation" class="sc-nav" data-open={navOpen}>
|
||||||
{#each navItems as item}
|
{#each navItems as item}
|
||||||
{@const Icon = iconFor(item)}
|
{@const Icon = iconFor(item)}
|
||||||
@@ -185,6 +189,7 @@
|
|||||||
</a>
|
</a>
|
||||||
{/each}
|
{/each}
|
||||||
</nav>
|
</nav>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<div class="sc-header-actions">
|
<div class="sc-header-actions">
|
||||||
{#if actions}{@render actions()}{/if}
|
{#if actions}{@render actions()}{/if}
|
||||||
@@ -273,6 +278,7 @@
|
|||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
{#if hasNavItems}
|
||||||
<button
|
<button
|
||||||
aria-expanded={navOpen}
|
aria-expanded={navOpen}
|
||||||
aria-label={navOpen ? 'Close navigation menu' : 'Open navigation menu'}
|
aria-label={navOpen ? 'Close navigation menu' : 'Open navigation menu'}
|
||||||
@@ -282,5 +288,6 @@
|
|||||||
>
|
>
|
||||||
{navOpen ? '×' : '☰'}
|
{navOpen ? '×' : '☰'}
|
||||||
</button>
|
</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']");
|
||||||
|
|||||||
Reference in new issue
Block a user