// How wide a picture is drawn, from the room its host gives the tree. The // host gives `Tree` its `area`: how wide its main area is, as an ``'s // sizes says it (`min(1200px, 100vw - 245px)`, or with media conditions, // `(min-width: 601px) 600px, 100vw`), the slot the tree's top is drawn in. // A part that draws its children narrower says so to the pictures under it // (`setSlot`): a grid's tiles take the slot divided among its columns. A // picture with sizes takes the slot it is drawn in (`slotOf`), and the app // preloads the hero at the tree's own (`heroPreload`), so the browser // fetches the size that fills it. The sizes a grid's `min` and a `Split`'s // sides name are here too, the CSS and its sizes drawn from the same. import { getContext, setContext } from 'svelte'; const SLOT = Symbol('slot'); /** How wide a picture the full width of a tree is where its host gives `Tree` no `area`: the screen. */ export const SCREEN_WIDE = '100vw'; /** The widths `sm`, `md` and `lg` name: a grid's narrowest tile, a `Split`'s sides. */ export const SIZES = { sm: '6rem', md: '10rem', lg: '15rem' } as const; /** The width a size token names, or `fallback` where it names none. */ export const sizeOf = (token: unknown, fallback: string): string => (typeof token === 'string' && token in SIZES ? SIZES[token as keyof typeof SIZES] : fallback); type Slot = () => string; /** Says to the pictures under the caller how wide the room they are drawn in is: a getter, read as it changes. */ export const setSlot = (slot: Slot): void => void setContext(SLOT, slot); /** The slot the caller is drawn in: the screen outside a tree. Called as a component starts. */ export const slotOf = (): Slot => getContext(SLOT) ?? (() => SCREEN_WIDE); /** Each length of a sizes list (`(min-width: 601px) 600px, 100vw`) made another by `f`, its media conditions kept. */ function eachLength(sizes: string, f: (length: string) => string): string { const entries: string[] = []; let depth = 0; let from = 0; for (let i = 0; i <= sizes.length; i++) { const c = sizes[i]; if (c === '(') depth++; else if (c === ')') depth--; else if ((c === ',' && depth === 0) || i === sizes.length) { entries.push(sizes.slice(from, i).trim()); from = i + 1; } } return entries .map((entry) => { // The length is the last word outside parentheses; what is before it, a media condition. let d = 0; let at = 0; for (let i = 0; i < entry.length; i++) { if (entry[i] === '(') d++; else if (entry[i] === ')') d--; else if (entry[i] === ' ' && d === 0) at = i + 1; } return entry.slice(0, at) + f(entry.slice(at)); }) .join(', '); } /** * How wide a grid's tile in `slot` is at most: as many across as fit at * `min` (each under twice it and a gap), at most `columns` (each the slot * shared among them, less their gaps), never wider than the slot. */ export function tileSlot(slot: string, min: string, gap: string, columns?: number): string { const most = `2 * ${min} + ${gap}`; return eachLength(slot, (w) => (columns ? `min(${w}, max(${most}, (${w} - ${columns - 1} * ${gap}) / ${columns}))` : `min(${w}, ${most})`)); } /** A picture's sizes in `slot`: a lazy one's lets the browser take its drawn width, where it can (`auto`). */ export const sizesIn = (slot: string, lazy: boolean): string => (lazy ? `auto, ${slot}` : slot);