|
| 1 | +import { initSqlHighlight } from "./sql-highlight.js"; |
| 2 | +import { initTabs, selectTab } from "./tabs.js"; |
| 3 | + |
| 4 | +/** Prepare the next live example before changing the visible preview and SQL. */ |
| 5 | +export function initComponentDemos(root, signal) { |
| 6 | + const panel = root.querySelector("#demo-panel"); |
| 7 | + const viewport = panel.querySelector(".demo-preview"); |
| 8 | + const source = root.querySelector("#demo-source"); |
| 9 | + const status = root.querySelector(".demo-status"); |
| 10 | + const renderSource = initSqlHighlight(source, signal); |
| 11 | + const media = matchMedia("(prefers-reduced-motion: reduce)"); |
| 12 | + const picker = root.querySelector(".component-picker"); |
| 13 | + let preview = viewport.querySelector("iframe"); |
| 14 | + let component = "table"; |
| 15 | + let sourceFiles = { main: source.textContent }; |
| 16 | + let request; |
| 17 | + let pending; |
| 18 | + let animation; |
| 19 | + let fade; |
| 20 | + let resizePreview; |
| 21 | + const previewHeight = (content) => |
| 22 | + `${Math.ceil(content.getBoundingClientRect().height) + 2}px`; |
| 23 | + |
| 24 | + function resizePanel(change, entering = false) { |
| 25 | + const before = panel.getBoundingClientRect().height; |
| 26 | + animation?.cancel(); |
| 27 | + fade?.cancel(); |
| 28 | + change(); |
| 29 | + const after = panel.getBoundingClientRect().height; |
| 30 | + if (media.matches) return; |
| 31 | + animation = panel.animate( |
| 32 | + [ |
| 33 | + { height: `${before}px`, opacity: entering ? 0 : 1 }, |
| 34 | + { height: `${after}px`, opacity: 1 }, |
| 35 | + ], |
| 36 | + { duration: 320, easing: "cubic-bezier(0.22, 1, 0.36, 1)" }, |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + function fitPreview() { |
| 41 | + resizePreview?.disconnect(); |
| 42 | + const content = preview.contentDocument?.querySelector( |
| 43 | + "#sqlpage_main_wrapper", |
| 44 | + ); |
| 45 | + if (!content) return; |
| 46 | + const fit = () => { |
| 47 | + const height = previewHeight(content); |
| 48 | + if (viewport.style.height !== height) |
| 49 | + resizePanel(() => { |
| 50 | + viewport.style.height = height; |
| 51 | + }); |
| 52 | + }; |
| 53 | + resizePreview = new ResizeObserver(fit); |
| 54 | + resizePreview.observe(content); |
| 55 | + fit(); |
| 56 | + } |
| 57 | + preview.addEventListener("load", fitPreview, { signal }); |
| 58 | + fitPreview(); |
| 59 | + |
| 60 | + root.querySelector("[data-copy-sql]").addEventListener( |
| 61 | + "click", |
| 62 | + async () => { |
| 63 | + try { |
| 64 | + await navigator.clipboard.writeText(source.textContent); |
| 65 | + status.textContent = "SQL copied."; |
| 66 | + } catch { |
| 67 | + status.textContent = "Copy unavailable. Select the SQL below."; |
| 68 | + } |
| 69 | + }, |
| 70 | + { signal }, |
| 71 | + ); |
| 72 | + initTabs( |
| 73 | + root.querySelector(".source-tabs"), |
| 74 | + "aria-selected", |
| 75 | + (button) => { |
| 76 | + source.setAttribute("aria-labelledby", button.id); |
| 77 | + status.textContent = ""; |
| 78 | + resizePanel(() => { |
| 79 | + void renderSource(sourceFiles[button.dataset.file]); |
| 80 | + }); |
| 81 | + }, |
| 82 | + signal, |
| 83 | + ); |
| 84 | + |
| 85 | + function loadPreview(frame, requestSignal) { |
| 86 | + return new Promise((resolve, reject) => { |
| 87 | + let observer; |
| 88 | + const abort = () => { |
| 89 | + observer?.disconnect(); |
| 90 | + reject(new DOMException("Demo cancelled", "AbortError")); |
| 91 | + }; |
| 92 | + requestSignal.addEventListener("abort", abort, { once: true }); |
| 93 | + frame.addEventListener( |
| 94 | + "load", |
| 95 | + async () => { |
| 96 | + const document = frame.contentDocument; |
| 97 | + if (!document?.querySelector("#sqlpage_main_wrapper")) { |
| 98 | + reject(new Error("Preview unavailable")); |
| 99 | + return; |
| 100 | + } |
| 101 | + const ready = () => { |
| 102 | + if (document.querySelector("[data-pre-init]")) return; |
| 103 | + observer?.disconnect(); |
| 104 | + requestSignal.removeEventListener("abort", abort); |
| 105 | + resolve(); |
| 106 | + }; |
| 107 | + await document.fonts.ready; |
| 108 | + if (requestSignal.aborted) return; |
| 109 | + observer = new MutationObserver(ready); |
| 110 | + observer.observe(document, { attributes: true, subtree: true }); |
| 111 | + ready(); |
| 112 | + }, |
| 113 | + { once: true, signal: requestSignal }, |
| 114 | + ); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + async function selectDemo(button) { |
| 119 | + request?.abort(); |
| 120 | + pending?.remove(); |
| 121 | + fade?.cancel(); |
| 122 | + const next = button.dataset.demo; |
| 123 | + if (next === component) { |
| 124 | + panel.removeAttribute("aria-busy"); |
| 125 | + return; |
| 126 | + } |
| 127 | + const controller = new AbortController(); |
| 128 | + request = controller; |
| 129 | + const incoming = preview.cloneNode(false); |
| 130 | + pending = incoming; |
| 131 | + incoming.title = "Loading SQLPage component demo"; |
| 132 | + incoming.setAttribute("aria-hidden", "true"); |
| 133 | + incoming.inert = true; |
| 134 | + incoming.loading = "eager"; |
| 135 | + const loaded = loadPreview(incoming, controller.signal); |
| 136 | + incoming.src = `/landing-demos/demo.sql?component=${next}`; |
| 137 | + viewport.append(incoming); |
| 138 | + panel.setAttribute("aria-busy", "true"); |
| 139 | + status.textContent = ""; |
| 140 | + try { |
| 141 | + const [files] = await Promise.all([ |
| 142 | + next === "catalog" |
| 143 | + ? null |
| 144 | + : fetch(`/landing-demos/source.sql?component=${next}`, { |
| 145 | + signal: controller.signal, |
| 146 | + }).then((response) => { |
| 147 | + if (!response.ok) throw new Error("Source unavailable"); |
| 148 | + return response.json(); |
| 149 | + }), |
| 150 | + loaded, |
| 151 | + ]); |
| 152 | + if (controller.signal.aborted) return; |
| 153 | + if (!media.matches) { |
| 154 | + fade = panel.animate([{ opacity: 1 }, { opacity: 0 }], { |
| 155 | + duration: 100, |
| 156 | + fill: "forwards", |
| 157 | + }); |
| 158 | + await fade.finished; |
| 159 | + } |
| 160 | + if (controller.signal.aborted) return; |
| 161 | + resizePreview?.disconnect(); |
| 162 | + preview.removeEventListener("load", fitPreview); |
| 163 | + const outgoing = preview; |
| 164 | + resizePanel(() => { |
| 165 | + outgoing.remove(); |
| 166 | + preview = incoming; |
| 167 | + pending = null; |
| 168 | + component = next; |
| 169 | + incoming.title = "Live SQLPage component demo"; |
| 170 | + incoming.removeAttribute("aria-hidden"); |
| 171 | + incoming.inert = false; |
| 172 | + viewport.style.height = previewHeight( |
| 173 | + incoming.contentDocument.querySelector("#sqlpage_main_wrapper"), |
| 174 | + ); |
| 175 | + panel.dataset.demo = next; |
| 176 | + panel.setAttribute("aria-labelledby", button.id); |
| 177 | + root.querySelector('[data-file="save"]').hidden = next !== "form"; |
| 178 | + root.querySelector("#demo-filename").textContent = `${next}.sql`; |
| 179 | + const link = panel.querySelector(".demo-docs"); |
| 180 | + link.href = `/component.sql?component=${next}`; |
| 181 | + link.firstChild.textContent = `Explore the ${next} component `; |
| 182 | + if (files) { |
| 183 | + sourceFiles = { main: files.source, save: files.save_source }; |
| 184 | + // Avoid an intermediate one-line loading state or a nested resize. |
| 185 | + const main = root.querySelector("#demo-filename"); |
| 186 | + selectTab(root.querySelector(".source-tabs"), main); |
| 187 | + source.setAttribute("aria-labelledby", main.id); |
| 188 | + void renderSource(sourceFiles.main); |
| 189 | + } |
| 190 | + }, true); |
| 191 | + preview.addEventListener("load", fitPreview, { signal }); |
| 192 | + fitPreview(); |
| 193 | + panel.removeAttribute("aria-busy"); |
| 194 | + } catch { |
| 195 | + if (controller.signal.aborted) return; |
| 196 | + controller.abort(); |
| 197 | + incoming.remove(); |
| 198 | + fade?.cancel(); |
| 199 | + panel.removeAttribute("aria-busy"); |
| 200 | + selectTab(picker, picker.querySelector(`[data-demo="${component}"]`)); |
| 201 | + status.textContent = "The demo could not load. Please try again."; |
| 202 | + } |
| 203 | + } |
| 204 | + initTabs( |
| 205 | + picker, |
| 206 | + "aria-selected", |
| 207 | + (button) => void selectDemo(button), |
| 208 | + signal, |
| 209 | + ); |
| 210 | + media.addEventListener( |
| 211 | + "change", |
| 212 | + () => { |
| 213 | + if (media.matches) animation?.cancel(); |
| 214 | + }, |
| 215 | + { signal }, |
| 216 | + ); |
| 217 | + return () => { |
| 218 | + request?.abort(); |
| 219 | + pending?.remove(); |
| 220 | + resizePreview?.disconnect(); |
| 221 | + animation?.cancel(); |
| 222 | + fade?.cancel(); |
| 223 | + }; |
| 224 | +} |
0 commit comments