{
  "$schema": "../../schema.json",
  "version": "1.0.0",
  "license": "MIT",
  "id": "toggle-flip",
  "type": "block",
  "title": "Toggle flip",
  "summary": "A cursor flips a switch and the whole interface changes theme in a circle that spreads out from the switch.",
  "tags": [
    "block",
    "ui",
    "toggle",
    "theme",
    "dark-mode",
    "reveal"
  ],
  "params": [
    {
      "name": "background",
      "type": "string",
      "default": "",
      "description": "a background id from the registry; empty uses the brand's first",
      "group": "advanced",
      "control": "select"
    },
    {
      "name": "title",
      "type": "string",
      "default": "Appearance",
      "description": "title",
      "group": "main",
      "control": "text"
    },
    {
      "name": "toggle",
      "type": "string",
      "default": "Light mode",
      "description": "toggle",
      "group": "main",
      "control": "text"
    },
    {
      "name": "format",
      "type": "enum",
      "default": "1920x1080",
      "options": [
        "1920x1080",
        "1080x1920",
        "1080x1080"
      ],
      "description": "output size; the layout recomposes for each",
      "group": "main",
      "control": "segmented"
    }
  ],
  "install": {
    "kind": "scene"
  },
  "files": [
    {
      "path": "pieces/toggle-flip/scene.html",
      "content": "<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>toggle-flip</title>\n<!--\n  Component · toggle flip (5.2 s). A cursor flips a switch: the knob slides over on a spring (squashing as it goes),\n  and the whole interface answers, a circle of the new theme spreading out from the switch until everything has\n  turned. The camera is close on the switch for the click and pulls back to watch the change sweep the screen.\n  Reach 0.4–1.1 s · Click 1.2 s · Sweep 1.35–2.5 s · Hold to 5.2 s.\n  Vertical: a phone-shaped settings screen without sidebar, larger type; square: a squarer window. The pull-back stops where the whole screen fits.\n-->\n<style>\n  #stage { overflow: hidden; }\n  .abs { position: absolute; box-sizing: border-box; }\n  #comp { position: absolute; box-sizing: border-box; }\n  .theme { position: absolute; inset: 0; overflow: hidden; display: grid; grid-template-columns: 22% 1fr; }\n  .side { display: flex; flex-direction: column; gap: 1.1em; padding: 2.2em 1.6em; }\n  .main { display: flex; flex-direction: column; gap: 1.2em; padding: 2.2em 2.6em; }\n  .row { display: flex; align-items: center; justify-content: space-between; padding: 1.1em 1.3em; border-radius: 0.8em; }\n  .sw { position: relative; width: 3.6em; height: 2em; border-radius: 999px; flex: none; }\n  .kn { position: absolute; top: 0.25em; left: 0.25em; width: 1.5em; height: 1.5em; border-radius: 999px; }\n  i { display: block; height: 0.6em; border-radius: 999px; }\n  #cursor { position: absolute; left: 0; top: 0; width: 0; height: 0; z-index: 20; }\n  #cursor svg { position: absolute; left: 0; top: 0; }\n</style>\n</head>\n<body>\n<main id=\"stage\">\n  <div id=\"bg\" class=\"abs\" style=\"inset:0\"></div>\n  <div id=\"world\"><div id=\"comp\"></div></div>\n</main>\n<script type=\"module\">\nimport { scene, progress, ease, spring } from 'paper-motion';\nimport { camera } from 'paper-motion/kit';\nimport { loadBrand } from 'paper-motion/brand';\n\nconst CONFIG = {\n  background: null,                      // a shader-set id, or null for the brand's first entry\n  title: 'Appearance',\n  toggle: 'Light mode',\n  rows: [['Motion blur', true], ['Sound from /brand', true]],\n  format: [1920, 1080],\n};\n\nconst B = await loadBrand();\nconst [W, H] = CONFIG.format, SHORT = Math.min(W, H), D = 5.2;\nconst s = scene({ width: W, height: H, fps: 30, duration: D, background: B.color('ground') });\nconst bgId = CONFIG.background ?? B.set.entries.find((e) => e.shader && !e.image)?.id;\nif (bgId) B.shader(s, '#bg', bgId);\nconst $ = (id) => document.getElementById(id);\n$('bg').style.opacity = 0.3;\n\nconst DIST = SHORT * 0.62, K = 1 + DIST / (SHORT * 1.7);\nconst cam = camera($('stage'), $('world'), { perspective: SHORT * 1.7 });\nconst ground = B.color('ground'), muted = B.color('muted'), figure = B.color('figure'), accent = B.color('accent', '#E9E4D8');\nconst tall = H > W, square = W === H;\nconst comp = $('comp'), cw = (tall ? W * 0.86 : square ? W * 0.84 : W * 0.66) * K, ch = cw * (tall ? 1.45 : square ? 0.78 : 0.58), FS = tall ? 0.032 : square ? 0.019 : 0.0135, R = (B.tokens?.shape?.radius ?? 12) * (SHORT / 1080) * K;\nObject.assign(comp.style, { width: `${cw}px`, height: `${ch}px`, borderRadius: `${R}px`, boxShadow: `0 ${SHORT * 0.04}px ${SHORT * 0.12}px rgba(0,0,0,0.5)`, fontSize: `${cw * FS}px`, fontFamily: B.font('text') });\n// the two themes, the same layout in each; the light one is revealed by a growing circle\nconst themes = { dark: { bg: B.surface(0.18), side: ground, row: ground, ink: figure, sub: muted, line: muted, on: accent, off: '#333', kn: ground }, light: { bg: accent, side: '#ddd7c9', row: '#f3efe6', ink: '#111', sub: '#6b6b6b', line: '#555', on: '#111', off: '#bbb', kn: accent } };\nconst build = (k, lightOn) => {\n  const c = themes[k], el = document.createElement('div'); el.className = 'theme';\n  const line = (w, o = 0.45) => `<i style=\"width:${w}%;background:${c.line};opacity:${o}\"></i>`;\n  const sw = (on, main) => `<div class=\"sw${main ? ' main-sw' : ''}\" style=\"background:${on ? c.on : c.off}\"><div class=\"kn\" style=\"background:${on ? c.kn : '#fff'};transform:translateX(${on ? 1.6 : 0}em)\"></div></div>`;\n  const row = (label, on, main) => `<div class=\"row\" style=\"background:${c.row};border:1px solid ${c.line}22\"><div><div style=\"color:${c.ink};font-weight:500;font-size:1.1em\">${label}</div>${main ? `<div style=\"color:${c.sub};margin-top:0.3em\">Switch the whole interface</div>` : ''}</div>${sw(on, main)}</div>`;\n  if (tall) el.style.gridTemplateColumns = '1fr';\n  el.innerHTML = `<div class=\"side\" style=\"background:${c.side}${tall ? ';display:none' : ''}\">${line(70, 0.9)}${line(85)}${line(60)}${line(75)}</div><div class=\"main\" style=\"background:${c.bg}\"><div style=\"font:400 2.1em ${B.font('display')};color:${c.ink}\">${CONFIG.title}</div>${row(CONFIG.toggle, lightOn, true)}${CONFIG.rows.map(([l, on]) => row(l, on)).join('')}<div style=\"flex:1;border-radius:0.8em;background:${c.row};border:1px solid ${c.line}22;padding:1.3em;display:flex;flex-direction:column;gap:0.8em\">${line(30, 0.8)}${line(80)}${line(66)}</div></div>`;\n  el.style.borderRadius = `${R}px`;\n  comp.append(el); return el;\n};\nconst dark = build('dark', false), light = build('light', true);\nconst cursor = document.createElement('div'); cursor.id = 'cursor'; const cs = cw * (tall || square ? FS * 2.6 : 0.035);\ncursor.innerHTML = `<svg width=\"${cs}\" height=\"${cs * 1.4}\" viewBox=\"0 0 20 28\"><path d=\"M2 2 L18 14 L10 15.5 L14.5 25 L11 26.5 L6.5 17 L2 22 Z\" fill=\"${figure}\" stroke=\"${ground}\" stroke-width=\"1.5\" stroke-linejoin=\"round\"/></svg>`;\ncomp.append(cursor);\ncam.place(comp, { x: 0, y: 0, z: 0 });\nawait document.fonts.ready;\n\n// the switch, in the comp's own pixels\nconst sw = dark.querySelector('.main-sw'), knob = sw.querySelector('.kn');\nconst off = (el) => { let x = 0, y = 0, e = el; while (e && e !== comp) { x += e.offsetLeft; y += e.offsetTop; e = e.offsetParent; } return [x, y]; };\nconst [sx, sy] = off(sw), sw0 = sw.offsetWidth, sh0 = sw.offsetHeight;\nconst kx = sx + sw0 / 2, ky = sy + sh0 / 2;\nconst em = cw * FS, far = Math.hypot(Math.max(kx, cw - kx), Math.max(ky, ch - ky));\n// the pull-back: as far as the original, or farther if the whole screen would not fit the frame\nconst P = SHORT * 1.7, zBack = Math.max(DIST - SHORT * 0.42, P * cw / (W * 0.82) - P, P * ch / (H * 0.86) - P);\nconst T = { reach: [0.4, 1.1], click: 1.2, sweep: [1.35, 2.5] };\ns.update((t) => {\n  // the knob: slides on a spring, stretching while it travels\n  const k = t >= T.click ? spring(t - T.click, { stiffness: 260, damping: 15 }) : 0;\n  const vel = t >= T.click ? Math.max(0, Math.sin(Math.min(1, (t - T.click) / 0.25) * Math.PI)) : 0;\n  knob.style.transform = `translateX(${1.6 * k}em) scaleX(${1 + 0.35 * vel})`;\n  knob.style.transformOrigin = k < 0.5 ? '0 50%' : '100% 50%';\n  sw.style.background = k > 0.5 ? accent : '#333';\n  // the sweep: the light theme in a circle growing out of the switch\n  const r = progress(t, ...T.sweep, ease.inOutCubic) * far * 1.05;\n  light.style.clipPath = `circle(${r}px at ${kx}px ${ky}px)`;\n  light.style.visibility = t >= T.sweep[0] ? '' : 'hidden';\n  // the cursor: in to the switch, a press, then out of the way\n  const p = progress(t, ...T.reach, ease.inOutCubic), out = progress(t, T.click + 0.4, T.click + 1.2, ease.inOutCubic);\n  const x = cw * 0.85 + (kx + em * 0.5 - cw * 0.85) * p + cw * 0.06 * out, y = ch * 1.05 + (ky - ch * 1.05) * p + ch * 0.12 * out;\n  cursor.style.transform = `translate(${x}px, ${y}px) scale(${t >= T.click && t < T.click + 0.16 ? 0.86 : 1})`;\n  cursor.style.opacity = String(progress(t, 0.3, 0.5));\n  cursor.querySelector('path').setAttribute('fill', r > Math.hypot(x - kx, y - ky) ? '#111' : figure);\n  cursor.querySelector('path').setAttribute('stroke', r > Math.hypot(x - kx, y - ky) ? accent : ground);\n  // the camera: close on the switch for the click, then back to watch the change spread\n  const arrive = progress(t, 0, 0.8, ease.outCubic), close = progress(t, 0.3, 1.1, ease.inOutCubic), back = progress(t, T.sweep[0] - 0.05, T.sweep[1] + 0.8, ease.inOutCubic);\n  const focus = close * (1 - back);\n  cam.at({ x: (kx - cw / 2) * focus, y: (ky - ch / 2) * focus, z: DIST + SHORT * 0.3 * (1 - arrive) - SHORT * 0.95 * focus + (zBack - DIST) * back, yaw: -8 * (1 - arrive) - 4 * focus + 3 * back, pitch: 3 * (1 - arrive) + 2 * focus, roll: 0 });\n  comp.style.opacity = String(progress(t, 0, 0.3));\n});\nB.cue(s, 'text', T.click); B.cue(s, 'transition', T.sweep[0]); B.cue(s, 'land', T.sweep[1]);\nB.sound(s);\nawait s.start();\n</script>\n</body>\n</html>\n"
    },
    {
      "path": "pieces/toggle-flip/brief.md",
      "content": "# Toggle flip\n\nBlock: toggle-flip (5.2 s), from paper-motion.app. The look, fonts and sound come from `brand/`.\n\nA cursor flips a switch and the whole interface changes theme in a circle that spreads out from the switch.\n\nA block is one ready-made moment. Edit `CONFIG` (its words, numbers and format: 16:9, 9:16 or 1:1, the layout recomposes for each); check `npx paper-motion still` at 1.1 and 1.7999999999999998 s; render.\n"
    }
  ],
  "requires": {
    "engine": ">=0.2.6"
  },
  "usage": "npx paper-motion add toggle-flip --name my-toggle-flip\n# edit CONFIG in pieces/my-toggle-flip/scene.html, then:\nnpx paper-motion render pieces/my-toggle-flip/scene.html --motion-blur 5",
  "notes": [
    "Wired to the brand folder: needs brand/tokens.json (a palette and a font pair work) and, for the field, a shader-set entry.",
    "Recomposes for 16:9, 9:16 and 1:1: set `format` and the layout and camera follow."
  ],
  "related": [
    "form-fill",
    "iris"
  ],
  "preview": {
    "video": "previews/toggle-flip.webm",
    "poster": "previews/toggle-flip.jpg",
    "at": 2.4,
    "values": {}
  },
  "mood": [
    "playful",
    "minimal"
  ],
  "duration": 5.2,
  "formats": [
    "16:9",
    "9:16",
    "1:1"
  ],
  "beats": [
    {
      "name": "Reach",
      "from": 0.4,
      "to": 1.1,
      "what": "reach"
    },
    {
      "name": "Click",
      "from": 1.2,
      "to": 1.7999999999999998,
      "what": "click"
    },
    {
      "name": "Sweep",
      "from": 1.35,
      "to": 2.5,
      "what": "sweep"
    },
    {
      "name": "Hold",
      "from": 2.5,
      "to": 5.2,
      "what": "hold"
    }
  ],
  "when": [
    "a setting that changes everything",
    "light and dark mode"
  ],
  "needs": [
    {
      "kind": "toggle",
      "fill": {
        "title": "$title",
        "toggle": "$toggle",
        "rows": "$rows"
      }
    }
  ]
}