import m from '@garage44/mithril' /** * The page's movement, in three pieces a view attaches to an element: a list * whose rows glide to their new places when the order changes, a pill that * slides under the chosen button of a group, and a page change that slides * instead of cutting. None of it runs under the phone's "reduce motion". */ export const reduced = () => window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false type Rects = Map const placed = new WeakMap() function measure(list: Element): Rects { const out: Rects = new Map() for (const el of list.children) { const key = (el as HTMLElement).dataset.key if (key) out.set(key, el.getBoundingClientRect()) } return out } /** * Lifecycle for a list (rows carrying `data-key`): where each row was is * measured before a redraw, and a row that moved is played from there to its * new place (FLIP), so a session moving up the list is seen to move. */ export const flipList = { oncreate: (v: m.VnodeDOM) => placed.set(v.dom, measure(v.dom)), onbeforeupdate: (_v: m.Vnode, old: m.VnodeDOM) => { placed.set(old.dom, measure(old.dom)) return true }, onupdate: (v: m.VnodeDOM) => { const before = placed.get(v.dom) if (!before || reduced()) return for (const el of v.dom.children) { const key = (el as HTMLElement).dataset.key const was = key ? before.get(key) : undefined if (!was) continue const now = el.getBoundingClientRect() const dy = was.top - now.top if (Math.abs(dy) < 1) continue el.animate([{transform: `translateY(${dy}px)`}, {transform: 'none'}], {duration: 380, easing: 'cubic-bezier(0.2, 0.8, 0.2, 1)'}) } }, } function placePill(group: Element, animate: boolean) { const pill = group.querySelector(':scope > .seg__pill') const on = group.querySelector(':scope > [aria-pressed="true"], :scope > [aria-current="page"]') if (!pill) return if (!on) { pill.style.opacity = '0' return } // The first placing jumps; every one after it slides. pill.style.transition = animate && !reduced() ? '' : 'none' pill.style.opacity = '1' pill.style.width = `${on.offsetWidth}px` pill.style.transform = `translateX(${on.offsetLeft}px)` if (!animate) void pill.offsetWidth } /** Lifecycle for a group with a `.seg__pill` child: the pill sits under the pressed (or current) one, sliding when that changes. */ export const slidingPill = { oncreate: (v: m.VnodeDOM) => { placePill(v.dom, false) // Its buttons' widths settle once the font has loaded, and change with the window (a // phone turned, text sized up): a pill placed once was left under the gap between two. void document.fonts?.ready.then(() => placePill(v.dom, false)) const watch = new ResizeObserver(() => placePill(v.dom, false)) watch.observe(v.dom) ;(v.dom as Element & {watch?: ResizeObserver}).watch = watch }, onupdate: (v: m.VnodeDOM) => placePill(v.dom, true), onremove: (v: m.VnodeDOM) => (v.dom as Element & {watch?: ResizeObserver}).watch?.disconnect(), } /** * A change of page as a slide: forward into a session, back out of one, or * across between tabs. Only the new page moves, on transform and opacity, so * the phone composes it without repainting. (View transitions were tried and * stuttered: each change snapshotted the whole screen, a long transcript * included, and cross-faded two pages over each other.) Under reduced motion * the page simply changes. */ export function pageChange(direction: 'forward' | 'back' | 'across', change: () => void) { // The cards rise in on the first load only: rising each card again under a sliding page doubled the motion and dropped frames. document.documentElement.dataset.moved = '' change() if (reduced()) return const page = document.querySelector('.frame__page') if (!page) return const dx = direction === 'forward' ? 24 : direction === 'back' ? -24 : 0 page.animate([{opacity: 0, transform: `translateX(${dx}px)`}, {opacity: 1, transform: 'none'}], {duration: direction === 'across' ? 180 : 240, easing: 'cubic-bezier(0.2, 0.8, 0.2, 1)'}) }