Repository navigation
Expand file tree
/
Copy pathcompose-demo.js
More file actions
239 lines (219 loc) · 6.42 KB
/
Copy pathcompose-demo.js
File metadata and controls
239 lines (219 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* Gold-path composition demo.
*
* Design intent:
* 1. Plan the tree first (comment below).
* 2. One concern per Component (Header / SideNav / DetailPane).
* 3. Page render only declares assembly — no imperative DOM soup.
*
* Real apps: put each class in its own Foo.js + Foo.css file.
*/
import { Component } from '../src/engine/index.js';
import { Button, loadUi } from '../src/ui/index.js';
import { startDemoApp } from './demoApp.js';
/**
* WorkspacePage
* ├── AppHeader
* ├── SideNav
* └── DetailPane
*/
const ROUTES = [
{
id: 'overview',
label: 'Overview',
path: 'app.overview',
body: 'Page shell only composes children. Each panel owns its DOM tree.',
},
{
id: 'nav',
label: 'Side nav',
path: 'app.nav',
body: 'Selection goes through shared state + refresh — parents do not scrape child DOM.',
},
{
id: 'detail',
label: 'Detail pane',
path: 'app.detail',
body: 'Actions stay in the child that draws them (onClick on Button props).',
},
];
class AppHeader extends Component {
static styles = new URL('./compose-demo.css', import.meta.url);
constructor(deps = {}) {
super(deps);
this.store = deps.store;
}
render() {
const selected = this.store.getSelected();
this.ensureShell((ids) => ids.composeHeader, ({ header, h1, p }) => {
header(
{ id: 'composeHeader', className: 'compose-header' },
h1({ className: 'compose-header__brand', text: 'gd5 compose' }),
p({
id: 'composeHeaderMeta',
className: 'compose-header__meta',
text: selected ? `Selected · ${selected.label}` : 'Select a route',
})
);
});
const { composeHeaderMeta } = this.ids();
if (composeHeaderMeta) {
composeHeaderMeta.textContent = selected
? `Selected · ${selected.label}`
: 'Select a route';
}
}
}
class SideNav extends Component {
static styles = new URL('./compose-demo.css', import.meta.url);
constructor(deps = {}) {
super(deps);
this.store = deps.store;
}
render() {
const selectedId = this.store.selectedId;
this.ensureShell((ids) => ids.composeNav, ({ aside, h2, div, button, map }) => {
// `nav` is not in bind()'s short-tag list — use aside (or h('nav', …)).
aside(
{ id: 'composeNav', className: 'compose-nav' },
h2({ className: 'compose-nav__title', text: 'Routes' }),
div(
{ className: 'compose-nav__list', id: 'composeNavList' },
...map(
ROUTES,
(route, _i, key) =>
button({
key,
id: `composeNav-${route.id}`,
type: 'button',
className: 'compose-nav__item',
text: route.label,
onClick: () => this.store.select(route.id),
}),
(route) => route.id
)
)
);
});
for (const route of ROUTES) {
const btn = this.ids()[`composeNav-${route.id}`];
if (!btn) continue;
btn.classList.toggle('is-active', route.id === selectedId);
}
}
}
class DetailPane extends Component {
static styles = new URL('./compose-demo.css', import.meta.url);
constructor(deps = {}) {
super(deps);
this.store = deps.store;
}
render() {
const selected = this.store.getSelected();
this.ensureShell((ids) => ids.composeDetail, ({ section, h1, p, div }) => {
section(
{ id: 'composeDetail', className: 'compose-detail' },
h1({
id: 'composeDetailTitle',
className: 'compose-detail__title',
text: selected?.label || 'Nothing selected',
}),
p({
id: 'composeDetailPath',
className: 'compose-detail__path',
text: selected?.path || '—',
}),
p({
id: 'composeDetailBody',
className: 'compose-detail__body',
text: selected?.body || 'Pick a route in the side nav.',
}),
div(
{ className: 'compose-detail__actions', id: 'composeDetailActions' },
Button({
type: 'primary',
text: 'Primary action',
onClick: () => this.store.ping('primary'),
}),
Button({
type: 'default',
text: 'Secondary',
onClick: () => this.store.ping('secondary'),
})
)
);
});
const { composeDetailTitle, composeDetailPath, composeDetailBody } = this.ids();
if (composeDetailTitle) composeDetailTitle.textContent = selected?.label || 'Nothing selected';
if (composeDetailPath) composeDetailPath.textContent = selected?.path || '—';
if (composeDetailBody) {
composeDetailBody.textContent = selected?.body || 'Pick a route in the side nav.';
}
}
}
/** Tiny shared state — stand-in for a real store. */
function createComposeStore() {
let selectedId = ROUTES[0].id;
let lastAction = '';
const listeners = new Set();
const emit = () => listeners.forEach((fn) => fn());
return {
get selectedId() {
return selectedId;
},
get lastAction() {
return lastAction;
},
getSelected() {
return ROUTES.find((r) => r.id === selectedId) || null;
},
select(id) {
selectedId = id;
emit();
},
ping(which) {
lastAction = which;
emit();
},
subscribe(fn) {
listeners.add(fn);
return () => listeners.delete(fn);
},
};
}
class WorkspacePage extends Component {
static styles = new URL('./compose-demo.css', import.meta.url);
constructor(deps = {}) {
super(deps);
this.store = createComposeStore();
this._unsub = null;
}
async componentDidMount() {
await loadUi();
this._unsub = this.store.subscribe(() => this.refreshChildren());
}
componentDidUnMount() {
this._unsub?.();
this._unsub = null;
}
refreshChildren() {
for (const child of this._slotted || []) {
child.refresh?.();
}
}
render() {
// Thin page: layout + children only. No imperative host clearing.
this.ensureShell((ids) => ids.composeRoot, ({ div, h }) => {
div(
{ id: 'composeRoot', className: 'compose-workspace' },
h(AppHeader, { store: this.store }),
div(
{ className: 'compose-workspace__body' },
h(SideNav, { store: this.store }),
h(DetailPane, { store: this.store })
)
);
});
}
}
startDemoApp({ home: WorkspacePage }, undefined, { demo: 'compose' });