Skip to content
← Public packages

@kentcdodds/kody-connect-talk

Kent C. Dodds's Cloudflare Connect 2026 talk: why your agents' stuff needs a common house, and how Kody builds one on Cloudflare.

FollowForkOpen app

Files

timing.mjs

83 lines · 3.3 KB · JavaScript
// How the deck turns steps into time.
//
// A slide is a list of steps: the entrance, then one per click. Inside a step,
// animation times are offsets from the moment the step starts. Nobody writes a
// stop time: the build measures how long each step's animations really run
// (counting staggered elements in the slide's own markup) and places the stop
// just after the last one finishes. So a stop can never land mid-animation.
import { parseHTML } from 'linkedom'

/** Default durations, shared with page.js so measuring and playing agree. */
export const DEFAULTS = {
	in: 0.8, // H.in: rise out of a blur
	fade: 0.5, // H.fade
	dim: 0.5, // H.dim
	tween: 0.5, // H.to / H.fromTo without a duration (GSAP's default)
	typePerChar: 0.035, // H.type
	typeMax: 1.2,
}

/** Breathing room after a step's last animation before its stop. */
export const SETTLE = 0.05
/** Gap between the last stop and the start of the slide's exit. */
export const EXIT_GAP = 0.1
/** How long each kind of exit takes. */
export const EXIT_LENGTH = { out: 0.5, wipe: 0.8, none: 0 }
/** The incoming color wipe that some slides enter under. */
export const WIPE_OUT_LENGTH = 0.9

/**
 * Measure each step of a slide: run its step functions against a recorder that
 * mirrors page.js's H, counting stagger targets in the slide's markup.
 */
export function measureSteps(slide, sceneHtml) {
	const { document } = parseHTML(`<!doctype html><html><body>${sceneHtml}</body></html>`)
	// the document holds just this slide, so document-wide matches are the slide's
	const count = (sel) => {
		const list = Array.isArray(sel) ? sel : [sel]
		let n = 0
		for (const one of list) n += document.querySelectorAll(one).length
		if (!n) throw new Error(`${slide.id}: "${list.join(', ')}" matches nothing in the slide`)
		return n
	}
	return slide.steps.map((step, i) => {
		let end = 0
		const span = (sel, at, duration, stagger = 0, repeat = 0) => {
			const n = count(sel)
			end = Math.max(end, at + duration * (1 + repeat) + stagger * (n - 1))
		}
		const recorder = {
			in: (sel, at, o = {}) => span(sel, at, o.d ?? DEFAULTS.in, o.stagger),
			fade: (sel, at, o = {}) => span(sel, at, o.d ?? DEFAULTS.fade, o.stagger),
			dim: (sel, at, o = {}) => span(sel, at, o.d ?? DEFAULTS.dim),
			type: (sel, at, chars, o = {}) =>
				span(sel, at, o.d ?? Math.min(DEFAULTS.typeMax, chars * DEFAULTS.typePerChar)),
			to: (sel, at, v = {}) => span(sel, at, v.duration ?? DEFAULTS.tween, v.stagger, v.repeat),
			fromTo: (sel, at, _from, v = {}) => span(sel, at, v.duration ?? DEFAULTS.tween, v.stagger, v.repeat),
		}
		step(recorder, slide.id)
		if (i === 0 && slide.enter === 'wipe') end = Math.max(end, WIPE_OUT_LENGTH)
		return end
	})
}

/**
 * Lay a slide out in local time: where each step starts, where each stop is,
 * and how long the slide runs (on a 1/8s grid, which is exact in binary so
 * starts never drift when added up).
 */
export function layoutSlide(slide, lengths) {
	const starts = []
	const stops = []
	let at = 0
	for (const length of lengths) {
		starts.push(at)
		at = at + length + SETTLE
		stops.push(at)
	}
	const exit = slide.exit ?? 'out'
	const exitLength = EXIT_LENGTH[exit]
	const lastStop = stops.at(-1)
	const dur = Math.ceil((lastStop + EXIT_GAP + exitLength + (exit === 'none' ? 0.3 : 0)) * 8) / 8
	return { starts, stops, dur, exitAt: dur - exitLength }
}