· 3 min read
Projects from data and a contrast checker
Added src/lib/projects/: a typed list of projects (projects.ts) and one folder per tool. Each project's tool is a dynamic import(), so its code is its own…
What we did
- Added
src/lib/projects/: a typed list of projects (projects.ts) and one folder per tool. Each project'stoolis a dynamicimport(), so its code is its own chunk and only loads on its page. /projectsis a card grid from that list, and the landing's "Featured projects" use it too./projects/[tool]gets its pages fromentries()and its component from a universal+page.tsload (a component can't come from a server load).- The first tool: a contrast checker. It scores every text token on every surface, in both themes, against WCAG. The pair you pick goes in the URL (
?text=brand&surface=surface-2), so a pair can be shared. tokens.cssdeclares the tokens again on any[data-theme]element, not only on:root.- The layout's
onNavigateskips the view transition for shallow navigations.
What I learned
- The tool reads the colors in the browser instead of parsing
tokens.cssat build time. A hidden probe getscolor: var(--token),getComputedStyle()returnsoklch(…), and a 1×1 canvas turns that into sRGB: paint the pixel, read it back withgetImageData(). The browser does all the resolving, and the tool will check future themes that only exist in the browser. The cost: no ratios without JS. - A custom property is computed where it's declared. On
:root,--text-1: light-dark(…)is already one color, and children inherit that color, not the formula.color-schemeon a child can't change it. Declaring the tokens again on the child makes it compute them with its own scheme. (Measured in Chrome.) url.searchParamscan't be read while prerendering: there's no visitor yet.onMountonly runs in the browser, so the tool reads them there.- SvelteKit 3 deprecates
replaceState.goto(url, { shallow: true, replace: true })changes the address bar without a navigation, andreplacekeeps it to one history entry. - Shallow navigations still run
onNavigate. Without a guard, every pick in a select would have faded the page out and in. - The URL updates in the selects'
onchange, not in an$effect: it reacts to what I did, not to every state change. page.urlis typed read-only, so I copy it withnew URL(page.url.href)before setting params.- WCAG ratios are cut, not rounded: 4.497 fails AA and mustn't show as 4.50.
Problems & fixes
- First version: both themes showed the same ratios. The probe had
color-scheme: dark, butgetPropertyValue('--text-1')on:rootwas alreadyoklch(21% .02 250). Fixed with[data-theme]intokens.css. - At 320px the tables were 11px too wide (
<body>is a grid and grew to fit). Tighter cell padding fixed it. - Lighthouse gave
/projects98 forheading-order(anh1straight to the cards'h3s). A "Tools"h2brought it to 100. - The tool found something: in the light theme,
--code-constantand--code-typeare only "large" (4.34, 4.39) on--surface-3. Code sits on--surface-2, where they pass.
Links
- MDN:
light-dark() - MDN:
getImageData() - WCAG: Contrast (minimum)
- SvelteKit: shallow routing
- SvelteKit: prerender and
url.searchParams - Svelte: when not to use
$effect
Next
- The first Awesome list, with search and tag filters saved in the URL.