Skip to content

Commit 9956b8f

Browse files
committed
docs(examples): demo shared-iframe soft navigation in the vite + next hubs
Add a 'Tabbed Tool' to both minimal hub examples: a single SPA (Overview / Components / Timeline / Settings) mounted as a subTabs anchor. Each SPA ships the ~40-line devframe:frame-nav postMessage shim (announces a tab manifest, answers navigate, reports navigated); the hub's client host attaches the frame-nav adapter, materializing one client-only dock per tab that all share one iframe. To satisfy the viewer contract, both shells now keep one iframe alive per frameId (shown/hidden instead of re-src'd), drive selection through docks.switchEntry so the adapter hears entry:activated, set state.domElements.iframe + emit dom:iframe:mounted on mount, and re-render when the adapter reconciles member docks. Verified end-to-end in a browser: opening the anchor reveals its tabs, selecting a tab soft-navigates the shared iframe with no reload, and navigating inside the frame moves the hub's active dock (bidirectional).
1 parent 0056365 commit 9956b8f

12 files changed

Lines changed: 682 additions & 83 deletions

File tree

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Next Tabbed Tool</title>
7+
<style>
8+
:root {
9+
color-scheme: light dark;
10+
font-family: system-ui, sans-serif;
11+
--fg: #1c2024;
12+
--muted: #6b7280;
13+
--line: #e4e6ea;
14+
--accent: #3a6a45;
15+
--panel: #f6f7f8;
16+
}
17+
@media (prefers-color-scheme: dark) {
18+
:root { --fg: #e6e8ea; --muted: #9aa2ad; --line: #2a2d31; --accent: #7fb98a; --panel: #17191c; }
19+
body { background: #0f1113; }
20+
}
21+
* { box-sizing: border-box; }
22+
body { margin: 0; color: var(--fg); }
23+
header { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
24+
header .brand { font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 6px; }
25+
header .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
26+
nav { display: flex; gap: 4px; margin-left: 8px; }
27+
nav button {
28+
appearance: none; border: 1px solid transparent; background: transparent; color: var(--muted);
29+
font: inherit; font-size: 13px; padding: 4px 10px; border-radius: 7px; cursor: pointer;
30+
}
31+
nav button:hover { color: var(--fg); background: var(--panel); }
32+
nav button[aria-current="true"] { color: var(--fg); background: var(--panel); border-color: var(--line); }
33+
main { padding: 24px; }
34+
h1 { margin: 0 0 6px; font-size: 18px; }
35+
p { max-width: 60ch; line-height: 1.6; color: var(--muted); }
36+
code { font-family: ui-monospace, monospace; background: rgba(127, 127, 127, 0.16); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
37+
.view { display: none; }
38+
.view[data-active="true"] { display: block; }
39+
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; margin-top: 16px; }
40+
.card { border: 1px solid var(--line); border-radius: 10px; padding: 14px; background: var(--panel); }
41+
.card b { display: block; font-size: 13px; }
42+
.card span { font-size: 12px; color: var(--muted); }
43+
ol { padding-left: 18px; }
44+
ol li { margin: 6px 0; }
45+
.tag { display: inline-block; font-size: 11px; font-family: ui-monospace, monospace; color: var(--accent); }
46+
</style>
47+
</head>
48+
<body>
49+
<header>
50+
<span class="brand"><span class="dot"></span>Next Tabbed Tool</span>
51+
<nav id="tabs"></nav>
52+
</header>
53+
<main>
54+
<section class="view" data-view="overview">
55+
<h1>Overview</h1>
56+
<p>
57+
One iframe, many hub docks. This SPA hosts its own internal views
58+
(<span class="tag">Overview · Components · Timeline · Settings</span>)
59+
and exposes each as a first-class hub dock that shares
60+
<em>this single iframe</em>. Switching docks soft-navigates here — no
61+
reload — via the <code>devframe:frame-nav</code> postMessage protocol.
62+
</p>
63+
<div class="grid">
64+
<div class="card"><b>Shared frame</b><span>frameId · next-tabbed-tool</span></div>
65+
<div class="card"><b>Transport</b><span>window.postMessage</span></div>
66+
<div class="card"><b>Docks</b><span>client-only, from manifest</span></div>
67+
<div class="card"><b>Nav</b><span>bidirectional</span></div>
68+
</div>
69+
</section>
70+
71+
<section class="view" data-view="components">
72+
<h1>Components</h1>
73+
<p>A stand-in view. Selecting the <b>Components</b> dock in the hub posts
74+
<code>navigate</code> to this frame, which routes here client-side.</p>
75+
<div class="grid">
76+
<div class="card"><b>&lt;Button /&gt;</b><span>12 usages</span></div>
77+
<div class="card"><b>&lt;Dialog /&gt;</b><span>4 usages</span></div>
78+
<div class="card"><b>&lt;Tabs /&gt;</b><span>7 usages</span></div>
79+
</div>
80+
</section>
81+
82+
<section class="view" data-view="timeline">
83+
<h1>Timeline</h1>
84+
<p>Navigate <em>inside</em> this frame (the tab bar above) and watch the
85+
hub's active dock follow — the frame posts <code>navigated</code> back.</p>
86+
<ol>
87+
<li>10:02 — build started</li>
88+
<li>10:02 — 214 modules transformed</li>
89+
<li>10:03 — page reload</li>
90+
</ol>
91+
</section>
92+
93+
<section class="view" data-view="settings">
94+
<h1>Settings</h1>
95+
<p>The manifest each tab reports (<code>title</code>, <code>icon</code>,
96+
<code>navTarget</code>) becomes a client-only dock in the hub — never
97+
written to the <code>devframe:docks</code> shared state.</p>
98+
<div class="grid">
99+
<div class="card"><b>Theme</b><span>follows OS</span></div>
100+
<div class="card"><b>Telemetry</b><span>off</span></div>
101+
</div>
102+
</section>
103+
</main>
104+
105+
<script>
106+
// ── The tabbed tool's own tiny router ────────────────────────────────
107+
const TABS = [
108+
{ id: 'overview', title: 'Overview', icon: 'ph:house-duotone', navTarget: { path: '/overview' } },
109+
{ id: 'components', title: 'Components', icon: 'ph:puzzle-piece-duotone', navTarget: { path: '/components' } },
110+
{ id: 'timeline', title: 'Timeline', icon: 'ph:clock-duotone', navTarget: { path: '/timeline' } },
111+
{ id: 'settings', title: 'Settings', icon: 'ph:gear-duotone', navTarget: { path: '/settings' } },
112+
]
113+
const pathToTab = path => (path || '/overview').replace(/^#?\/?/, '') || 'overview'
114+
let current = pathToTab(location.hash.slice(1))
115+
if (!TABS.some(t => t.id === current)) current = 'overview'
116+
117+
const navEl = document.getElementById('tabs')
118+
navEl.innerHTML = TABS.map(t => `<button data-tab="${t.id}">${t.title}</button>`).join('')
119+
120+
function render() {
121+
for (const el of document.querySelectorAll('.view'))
122+
el.dataset.active = String(el.dataset.view === current)
123+
for (const b of navEl.querySelectorAll('button'))
124+
b.setAttribute('aria-current', String(b.dataset.tab === current))
125+
}
126+
127+
// ── The postMessage nav shim (devframe:frame-nav protocol) ───────────
128+
const CHANNEL = 'devframe:frame-nav'
129+
const VERSION = 1
130+
const FRAME_ID = 'next-tabbed-tool'
131+
// Best-effort parent origin so the first (unsolicited) `ready` can be sent
132+
// even if the host's `hello` is missed during iframe boot.
133+
let hostOrigin
134+
= (location.ancestorOrigins && location.ancestorOrigins[0])
135+
|| (document.referrer ? new URL(document.referrer).origin : '*')
136+
137+
function post(message) {
138+
parent.postMessage({ channel: CHANNEL, v: VERSION, frameId: FRAME_ID, from: 'frame', ...message }, hostOrigin)
139+
}
140+
const manifest = () => TABS.map(t => ({ id: t.id, title: t.title, icon: t.icon, navTarget: t.navTarget }))
141+
142+
function setView(id, fromHost) {
143+
if (!TABS.some(t => t.id === id)) return
144+
current = id
145+
location.hash = `/${id}`
146+
render()
147+
// Report internal navigation so the host highlights the matching dock.
148+
// When the host itself asked us to navigate, its idempotent guard makes
149+
// this echo a no-op.
150+
if (!fromHost) post({ type: 'navigated', tabId: id })
151+
}
152+
153+
addEventListener('message', (e) => {
154+
const d = e.data
155+
if (!d || d.channel !== CHANNEL || d.v !== VERSION || d.from !== 'host') return
156+
if (d.frameId && d.frameId !== FRAME_ID) return
157+
hostOrigin = e.origin || hostOrigin
158+
if (d.type === 'hello')
159+
post({ type: 'ready', tabs: manifest(), current })
160+
else if (d.type === 'navigate')
161+
setView(pathToTab(d.navTarget && d.navTarget.path), true)
162+
})
163+
164+
// The user clicking a tab inside this frame drives the loop the other way.
165+
navEl.addEventListener('click', (e) => {
166+
const b = e.target.closest('button[data-tab]')
167+
if (b) setView(b.dataset.tab, false)
168+
})
169+
170+
render()
171+
// Announce ourselves on load (the host also sends `hello` — either wins).
172+
post({ type: 'ready', tabs: manifest(), current })
173+
</script>
174+
</body>
175+
</html>

‎examples/minimal-next-devframe-hub/src/client/app/icons.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ const ICON_CLASS: Record<string, string> = {
1313
'ph:rocket-duotone': 'i-ph-rocket-duotone',
1414
'ph:wrench-duotone': 'i-ph-wrench-duotone',
1515
'ph:plug-duotone': 'i-ph-plug-duotone',
16+
'ph:layout-duotone': 'i-ph-layout-duotone',
17+
'ph:note-pencil-duotone': 'i-ph-note-pencil-duotone',
18+
'ph:squares-four-duotone': 'i-ph-squares-four-duotone',
19+
'ph:house-duotone': 'i-ph-house-duotone',
20+
'ph:puzzle-piece-duotone': 'i-ph-puzzle-piece-duotone',
21+
'ph:clock-duotone': 'i-ph-clock-duotone',
22+
'ph:gear-duotone': 'i-ph-gear-duotone',
1623
}
1724

1825
/**

0 commit comments

Comments
 (0)