Skip to content
← Public packages

@kentcdodds/invisible-software-talk

Invisible Software: Kent C. Dodds's AI Coding Summit NYC 2026 talk deck. Agents become software's next billion users.

FollowForkOpen app

Files

demo.js

81 lines · 3.6 KB · JavaScript
// The phone demo (scripts/build-demo.mjs) embedded in slides. Each <div class="demo" ...> becomes an
// iframe whose srcdoc is the composed demo page, created when its slide comes within two slides of
// the current one. The deck's click steps drive it:
//   data-start="-1" | "2" | "scene:5"  frame shown at the slide's step 0 (a held demo step, or a
//                                      scene's first frame)
//   data-first="3"                     demo step played at the slide's step 1; step i plays first+i-1
//   data-count="3"                     how many demo steps the slide walks through
// Stepping forward on the current slide (motion on) plays the demo step with its automatic
// follow-up; anything else (going back, jumping, ?static) holds the step's settled end.
;(() => {
	const html = window.__DEMO_HTML
	const frames = [...document.querySelectorAll('.reveal .slides section .demo')]
	const target = (el, step) => {
		const first = el.dataset.first == null ? null : Number(el.dataset.first)
		const count = Number(el.dataset.count ?? 0)
		if (first == null || step === 0) return el.dataset.start ?? '-1'
		return String(first + Math.min(step, count) - 1)
	}
	const apply = (el, animate) => {
		const win = el.__iframe?.contentWindow
		if (!win?.__demoReady) return
		const section = el.closest('section')
		const step = Number(section.dataset.step ?? 0)
		const want = target(el, step)
		const prev = el.__shown
		el.__shown = want
		const ss = win.__slideshow
		if (want.startsWith('scene:')) return ss.seekScene(want.slice(6))
		const k = Number(want)
		const live = animate && document.documentElement.dataset.motion !== 'off' && window.__deck?.getCurrentSlide() === section
		const nextOne = prev === String(k - 1) || (prev?.startsWith('scene:') && k === Number(el.dataset.first))
		if (live && nextOne) ss.play(k)
		else ss.hold(k, false)
	}
	const mount = (el) => {
		if (el.__iframe) return
		const iframe = document.createElement('iframe')
		iframe.setAttribute('aria-hidden', 'true')
		iframe.setAttribute('tabindex', '-1')
		iframe.addEventListener('load', () => {
			const win = iframe.contentWindow
			const go = () => apply(el, false)
			if (win.__demoReady) go()
			else win.addEventListener('demoready', go)
		})
		// Hosted: the demo is its own content-hashed document (same origin, cached by the service worker).
		// Offline file: the whole document is inlined and loaded as srcdoc.
		if (window.__DEMO_URL) iframe.src = window.__DEMO_URL
		else iframe.srcdoc = html
		el.append(iframe)
		el.__iframe = iframe
	}
	const near = () => {
		const deck = window.__deck
		if (!deck?.isReady()) return
		const cur = deck.getIndices().h
		for (const el of frames) {
			const h = deck.getIndices(el.closest('section')).h
			if (Math.abs(h - cur) <= 2) mount(el)
		}
	}
	for (const el of frames) {
		el.closest('section').addEventListener('stepchange', () => apply(el, true))
	}
	const deck = window.__deck
	deck.on('ready', near)
	deck.on('slidechanged', (e) => {
		near()
		// Arriving or leaving: settle every frame on its slide's current step (no replay).
		for (const el of frames) if (el.closest('section') === e.currentSlide || el.closest('section') === e.previousSlide) apply(el, false)
	})
	if (deck.isReady()) near()
	// For verify and screenshots: true once every frame on the current slide is loaded and settled.
	window.__demoSettled = () => {
		const section = deck.getCurrentSlide()
		return [...section.querySelectorAll('.demo')].every((el) => {
			const win = el.__iframe?.contentWindow
			return win?.__demoReady && !win.__slideshow.state.playing && el.__shown === target(el, Number(section.dataset.step ?? 0))
		})
	}
})()