miral 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +67 -34
- package/dist/Assets.js +4 -0
- package/dist/Controls.js +55 -0
- package/dist/Nav.js +17 -0
- package/dist/Spinner.js +7 -0
- package/dist/Toolbar.js +139 -0
- package/dist/Viewer.js +364 -0
- package/dist/cli.js +179 -0
- package/dist/icons.js +12 -0
- package/{src → dist}/theme.css +1 -1
- package/package.json +14 -9
- package/src/Assets.tsx +0 -41
- package/src/Controls.tsx +0 -147
- package/src/Nav.tsx +0 -99
- package/src/Spinner.tsx +0 -15
- package/src/Toolbar.tsx +0 -271
- package/src/Viewer.tsx +0 -643
- package/src/cli.js +0 -206
- package/src/icons.tsx +0 -31
package/dist/Viewer.js
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { createRoot } from "react-dom/client";
|
|
4
|
+
import { Icon } from "./icons.js";
|
|
5
|
+
import "/__miral-viewer.css";
|
|
6
|
+
import { Assets } from "./Assets.js";
|
|
7
|
+
import { Controls } from "./Controls.js";
|
|
8
|
+
import { Nav } from "./Nav.js";
|
|
9
|
+
import { Spinner } from "./Spinner.js";
|
|
10
|
+
import { Toolbar } from "./Toolbar.js";
|
|
11
|
+
const modules = import.meta.glob([
|
|
12
|
+
"/**/*.mira.tsx",
|
|
13
|
+
"!/**/node_modules/**",
|
|
14
|
+
"!/**/dist/**",
|
|
15
|
+
"!/**/.git/**",
|
|
16
|
+
]);
|
|
17
|
+
const sections = import.meta.glob(["/**/*.mira.tsx", "!/**/node_modules/**", "!/**/dist/**", "!/**/.git/**"], { eager: true, query: "?miral-section", import: "default" });
|
|
18
|
+
const configs = import.meta.glob("/miral.config.ts", { eager: true });
|
|
19
|
+
const config = configs["/miral.config.ts"];
|
|
20
|
+
const { title, order = [], componentOrder = [], stylesheet, } = config?.default ?? {};
|
|
21
|
+
const catalogTitle = title?.trim();
|
|
22
|
+
document.title = catalogTitle ? `${catalogTitle} | Miral` : "Miral";
|
|
23
|
+
const assets = config?.assetFiles;
|
|
24
|
+
const assetsEntry = { path: "assets", name: "Assets", section: "" };
|
|
25
|
+
const assetsPage = {
|
|
26
|
+
Preview: () => _jsx(Assets, { files: assets ?? {} }),
|
|
27
|
+
layout: "padded",
|
|
28
|
+
};
|
|
29
|
+
function compare(a, b, preferred) {
|
|
30
|
+
const rank = (name) => preferred.includes(name) ? preferred.indexOf(name) : preferred.length;
|
|
31
|
+
return rank(a) - rank(b) || a.localeCompare(b);
|
|
32
|
+
}
|
|
33
|
+
const entries = Object.keys(modules)
|
|
34
|
+
.map((path) => {
|
|
35
|
+
const parts = path.split("/");
|
|
36
|
+
const section = sections[path] || "Elements";
|
|
37
|
+
return {
|
|
38
|
+
path,
|
|
39
|
+
section,
|
|
40
|
+
name: parts.at(-1)?.replace(/\.mira\.tsx$/, "") ?? path,
|
|
41
|
+
};
|
|
42
|
+
})
|
|
43
|
+
.sort((a, b) => compare(a.section, b.section, order) ||
|
|
44
|
+
compare(a.name, b.name, componentOrder));
|
|
45
|
+
const layouts = {
|
|
46
|
+
Mobile: [390, 844],
|
|
47
|
+
Tablet: [768, 1024],
|
|
48
|
+
Desktop: null,
|
|
49
|
+
};
|
|
50
|
+
const devices = [
|
|
51
|
+
{ name: "Mobile", label: "Smartphone", icon: "phone" },
|
|
52
|
+
{ name: "Tablet", label: "Tablet", icon: "tablet" },
|
|
53
|
+
{ name: "Desktop", label: "Desktop", icon: "desktop" },
|
|
54
|
+
];
|
|
55
|
+
function getShortcut(event) {
|
|
56
|
+
if (event.defaultPrevented ||
|
|
57
|
+
event.repeat ||
|
|
58
|
+
event.isComposing ||
|
|
59
|
+
event.ctrlKey ||
|
|
60
|
+
event.altKey ||
|
|
61
|
+
event.metaKey)
|
|
62
|
+
return;
|
|
63
|
+
if (event.key === "Escape" &&
|
|
64
|
+
!document.querySelector("dialog[open], [role=dialog][aria-modal=true]"))
|
|
65
|
+
return "escape";
|
|
66
|
+
if (!event.shiftKey ||
|
|
67
|
+
(event.target instanceof Element &&
|
|
68
|
+
event.target.closest("input, textarea, select, [contenteditable]:not([contenteditable=false])")))
|
|
69
|
+
return;
|
|
70
|
+
switch (event.key.toLowerCase()) {
|
|
71
|
+
case "c":
|
|
72
|
+
return "toolbar";
|
|
73
|
+
case "l":
|
|
74
|
+
return "layout";
|
|
75
|
+
case "t":
|
|
76
|
+
return "theme";
|
|
77
|
+
case "f":
|
|
78
|
+
return "fullscreen";
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const initialUrl = new URL(location.href);
|
|
82
|
+
const frame = initialUrl.searchParams.has("frame");
|
|
83
|
+
const emptyPage = {};
|
|
84
|
+
const pages = new Map();
|
|
85
|
+
async function importPage(path) {
|
|
86
|
+
const page = await modules[path]();
|
|
87
|
+
const PropsSchema = page.miralComponent?.PropsSchema ?? page.PropsSchema ?? page.schema;
|
|
88
|
+
const Playground = page.default ?? page.Playground;
|
|
89
|
+
const result = { ...page, PropsSchema, Playground };
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
function loadPage(path) {
|
|
93
|
+
const cached = pages.get(path);
|
|
94
|
+
if (cached)
|
|
95
|
+
return cached;
|
|
96
|
+
const result = importPage(path);
|
|
97
|
+
pages.set(path, result);
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
function link(params) {
|
|
101
|
+
const result = new URL(location.pathname, location.origin);
|
|
102
|
+
result.search = new URLSearchParams(params).toString();
|
|
103
|
+
return result.href;
|
|
104
|
+
}
|
|
105
|
+
function readValues(page, url) {
|
|
106
|
+
const defaults = page.defaults ?? {};
|
|
107
|
+
const schema = page.PropsSchema;
|
|
108
|
+
if (!schema)
|
|
109
|
+
return defaults;
|
|
110
|
+
const stored = url.searchParams.get("props");
|
|
111
|
+
if (stored) {
|
|
112
|
+
try {
|
|
113
|
+
const parsed = schema.safeParse(JSON.parse(stored));
|
|
114
|
+
if (parsed.success)
|
|
115
|
+
return parsed.data;
|
|
116
|
+
}
|
|
117
|
+
catch { }
|
|
118
|
+
}
|
|
119
|
+
const parsed = schema.safeParse(defaults);
|
|
120
|
+
const result = parsed.success ? parsed.data : defaults;
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
function animateIsolation(canvas, open) {
|
|
124
|
+
const slot = canvas.parentElement;
|
|
125
|
+
const stage = slot?.parentElement;
|
|
126
|
+
if (!slot || !stage)
|
|
127
|
+
return;
|
|
128
|
+
const current = canvas.getBoundingClientRect();
|
|
129
|
+
const normal = slot.getBoundingClientRect();
|
|
130
|
+
const visible = stage.getBoundingClientRect();
|
|
131
|
+
const edges = [
|
|
132
|
+
visible.top - normal.top,
|
|
133
|
+
normal.right - visible.right,
|
|
134
|
+
normal.bottom - visible.bottom,
|
|
135
|
+
visible.left - normal.left,
|
|
136
|
+
];
|
|
137
|
+
const clipped = "inset(" + edges.map((edge) => Math.max(0, edge) + "px").join(" ") + ")";
|
|
138
|
+
const animations = canvas.getAnimations();
|
|
139
|
+
const currentClip = !animations.length && open ? clipped : getComputedStyle(canvas).clipPath;
|
|
140
|
+
for (const animation of animations)
|
|
141
|
+
animation.cancel();
|
|
142
|
+
if (matchMedia("(prefers-reduced-motion: reduce)").matches)
|
|
143
|
+
return;
|
|
144
|
+
const bounds = (rect, clipPath) => ({
|
|
145
|
+
position: "fixed",
|
|
146
|
+
transform: "none",
|
|
147
|
+
zIndex: "1",
|
|
148
|
+
top: rect.top + "px",
|
|
149
|
+
left: rect.left + "px",
|
|
150
|
+
width: rect.width + "px",
|
|
151
|
+
height: rect.height + "px",
|
|
152
|
+
clipPath,
|
|
153
|
+
});
|
|
154
|
+
const full = new DOMRect(0, 0, document.documentElement.clientWidth, window.innerHeight);
|
|
155
|
+
canvas.animate([
|
|
156
|
+
bounds(current, currentClip === "none" ? "inset(0px)" : currentClip),
|
|
157
|
+
bounds(open ? full : normal, open ? "inset(0px)" : clipped),
|
|
158
|
+
], { duration: 200, easing: "ease-out" });
|
|
159
|
+
}
|
|
160
|
+
const FrameLoading = () => {
|
|
161
|
+
React.useLayoutEffect(() => {
|
|
162
|
+
parent.postMessage({ type: "miral-loading" }, location.origin);
|
|
163
|
+
}, []);
|
|
164
|
+
return null;
|
|
165
|
+
};
|
|
166
|
+
const Viewer = () => {
|
|
167
|
+
const [url, setUrl] = React.useState(initialUrl);
|
|
168
|
+
const entry = assets &&
|
|
169
|
+
(url.searchParams.get("component") === assetsEntry.path || !entries.length)
|
|
170
|
+
? assetsEntry
|
|
171
|
+
: (entries.find((item) => item.path === url.searchParams.get("component")) ?? entries[0]);
|
|
172
|
+
const example = url.searchParams.get("example") || "Preview";
|
|
173
|
+
let page = emptyPage;
|
|
174
|
+
if (entry === assetsEntry)
|
|
175
|
+
page = assetsPage;
|
|
176
|
+
else if (entry)
|
|
177
|
+
page = React.use(loadPage(entry.path));
|
|
178
|
+
const [values, setValues] = React.useState(() => readValues(page, url));
|
|
179
|
+
const [previousUrl, setPreviousUrl] = React.useState(url);
|
|
180
|
+
if (previousUrl !== url) {
|
|
181
|
+
setPreviousUrl(url);
|
|
182
|
+
if (["component", "example", "props"].some((key) => previousUrl.searchParams.get(key) !== url.searchParams.get(key)))
|
|
183
|
+
setValues(readValues(page, url));
|
|
184
|
+
}
|
|
185
|
+
React.useEffect(() => {
|
|
186
|
+
const handlePopState = () => React.startTransition(() => setUrl(new URL(location.href)));
|
|
187
|
+
window.addEventListener("popstate", handlePopState);
|
|
188
|
+
return () => window.removeEventListener("popstate", handlePopState);
|
|
189
|
+
}, []);
|
|
190
|
+
const handleLinkClick = (event) => {
|
|
191
|
+
if (event.defaultPrevented ||
|
|
192
|
+
event.button !== 0 ||
|
|
193
|
+
event.metaKey ||
|
|
194
|
+
event.ctrlKey ||
|
|
195
|
+
event.shiftKey ||
|
|
196
|
+
event.altKey)
|
|
197
|
+
return;
|
|
198
|
+
event.preventDefault();
|
|
199
|
+
const next = new URL(event.currentTarget.href);
|
|
200
|
+
if (next.href === location.href)
|
|
201
|
+
return;
|
|
202
|
+
history.pushState(null, "", next.href);
|
|
203
|
+
React.startTransition(() => setUrl(next));
|
|
204
|
+
};
|
|
205
|
+
const [theme, setTheme] = React.useState(url.searchParams.get("theme") === "dark" ? "dark" : "light");
|
|
206
|
+
const requestedLayout = url.searchParams.get("layout") ?? "";
|
|
207
|
+
const layout = Object.hasOwn(layouts, requestedLayout)
|
|
208
|
+
? requestedLayout
|
|
209
|
+
: "Desktop";
|
|
210
|
+
const changeLayout = (nextLayout) => {
|
|
211
|
+
const next = new URL(location.href);
|
|
212
|
+
next.searchParams.set("layout", nextLayout);
|
|
213
|
+
history.replaceState(history.state, "", next.href);
|
|
214
|
+
setUrl(next);
|
|
215
|
+
};
|
|
216
|
+
const iframe = React.useRef(null);
|
|
217
|
+
const [frameLoading, setFrameLoading] = React.useState(true);
|
|
218
|
+
const stage = React.useRef(null);
|
|
219
|
+
const [stageWidth, setStageWidth] = React.useState();
|
|
220
|
+
const hasPreview = !!entry && !frame;
|
|
221
|
+
React.useLayoutEffect(() => {
|
|
222
|
+
const element = stage.current;
|
|
223
|
+
if (!element)
|
|
224
|
+
return;
|
|
225
|
+
const measure = () => setStageWidth(element.clientWidth);
|
|
226
|
+
measure();
|
|
227
|
+
const observer = new ResizeObserver(measure);
|
|
228
|
+
observer.observe(element);
|
|
229
|
+
return () => observer.disconnect();
|
|
230
|
+
}, [hasPreview]);
|
|
231
|
+
const isolationButton = React.useRef(null);
|
|
232
|
+
const [toolbarHidden, setToolbarHidden] = React.useState(false);
|
|
233
|
+
const [isolated, setIsolated] = React.useState(!frame && initialUrl.searchParams.has("isolated"));
|
|
234
|
+
const handleThemeClick = () => setTheme((current) => (current === "light" ? "dark" : "light"));
|
|
235
|
+
const handleLayoutClick = (event) => changeLayout(event.currentTarget.value);
|
|
236
|
+
const handleIsolationClick = () => {
|
|
237
|
+
if (iframe.current)
|
|
238
|
+
animateIsolation(iframe.current, !isolated);
|
|
239
|
+
setIsolated(!isolated);
|
|
240
|
+
if (!isolated)
|
|
241
|
+
return;
|
|
242
|
+
const current = new URL(location.href);
|
|
243
|
+
current.searchParams.delete("isolated");
|
|
244
|
+
if (current.href !== location.href)
|
|
245
|
+
history.replaceState(history.state, "", current);
|
|
246
|
+
requestAnimationFrame(() => isolationButton.current?.focus({ preventScroll: true }));
|
|
247
|
+
};
|
|
248
|
+
React.useEffect(() => {
|
|
249
|
+
if (isolated)
|
|
250
|
+
iframe.current?.focus({ preventScroll: true });
|
|
251
|
+
}, [isolated]);
|
|
252
|
+
const runShortcut = React.useEffectEvent((action) => {
|
|
253
|
+
if (action === "toolbar" && !isolated)
|
|
254
|
+
setToolbarHidden((hidden) => !hidden);
|
|
255
|
+
if (action === "theme")
|
|
256
|
+
handleThemeClick();
|
|
257
|
+
if (action === "layout")
|
|
258
|
+
changeLayout(devices[(devices.findIndex((device) => device.name === layout) + 1) %
|
|
259
|
+
devices.length].name);
|
|
260
|
+
if (action === "fullscreen" || (isolated && action === "escape"))
|
|
261
|
+
handleIsolationClick();
|
|
262
|
+
});
|
|
263
|
+
React.useEffect(() => {
|
|
264
|
+
const handleKeyDown = (event) => {
|
|
265
|
+
const action = getShortcut(event);
|
|
266
|
+
if (!action)
|
|
267
|
+
return;
|
|
268
|
+
if (action !== "escape") {
|
|
269
|
+
event.preventDefault();
|
|
270
|
+
event.stopPropagation();
|
|
271
|
+
}
|
|
272
|
+
if (frame)
|
|
273
|
+
parent.postMessage({ type: "miral-shortcut", action }, location.origin);
|
|
274
|
+
else
|
|
275
|
+
runShortcut(action);
|
|
276
|
+
};
|
|
277
|
+
const handleMessage = (event) => {
|
|
278
|
+
if (!frame &&
|
|
279
|
+
event.source === iframe.current?.contentWindow &&
|
|
280
|
+
event.origin === location.origin &&
|
|
281
|
+
event.data?.type === "miral-shortcut")
|
|
282
|
+
runShortcut(event.data.action);
|
|
283
|
+
};
|
|
284
|
+
window.addEventListener("keydown", handleKeyDown, true);
|
|
285
|
+
window.addEventListener("message", handleMessage);
|
|
286
|
+
return () => {
|
|
287
|
+
window.removeEventListener("keydown", handleKeyDown, true);
|
|
288
|
+
window.removeEventListener("message", handleMessage);
|
|
289
|
+
};
|
|
290
|
+
}, []);
|
|
291
|
+
React.useEffect(() => {
|
|
292
|
+
document.documentElement.classList.toggle("dark", theme === "dark");
|
|
293
|
+
document.documentElement.classList.toggle("light", theme === "light");
|
|
294
|
+
}, [theme]);
|
|
295
|
+
React.useLayoutEffect(() => {
|
|
296
|
+
if (frame) {
|
|
297
|
+
const receive = (event) => {
|
|
298
|
+
if (event.origin !== location.origin ||
|
|
299
|
+
event.source !== parent ||
|
|
300
|
+
event.data?.type !== "miral-settings")
|
|
301
|
+
return;
|
|
302
|
+
setTheme(event.data.theme === "dark" ? "dark" : "light");
|
|
303
|
+
if (page.PropsSchema?.safeParse(event.data.values).success)
|
|
304
|
+
setValues(event.data.values);
|
|
305
|
+
};
|
|
306
|
+
window.addEventListener("message", receive);
|
|
307
|
+
parent.postMessage({ type: "miral-ready" }, location.origin);
|
|
308
|
+
return () => {
|
|
309
|
+
window.removeEventListener("message", receive);
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
}, [page]);
|
|
313
|
+
React.useEffect(() => {
|
|
314
|
+
if (frame)
|
|
315
|
+
return;
|
|
316
|
+
const sync = () => iframe.current?.contentWindow?.postMessage({ type: "miral-settings", theme, values }, location.origin);
|
|
317
|
+
const ready = (event) => {
|
|
318
|
+
if (event.origin !== location.origin ||
|
|
319
|
+
event.source !== iframe.current?.contentWindow)
|
|
320
|
+
return;
|
|
321
|
+
if (event.data?.type === "miral-ready") {
|
|
322
|
+
setFrameLoading(false);
|
|
323
|
+
sync();
|
|
324
|
+
}
|
|
325
|
+
else if (event.data?.type === "miral-loading") {
|
|
326
|
+
setFrameLoading(true);
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
window.addEventListener("message", ready);
|
|
330
|
+
sync();
|
|
331
|
+
return () => window.removeEventListener("message", ready);
|
|
332
|
+
}, [theme, values]);
|
|
333
|
+
const names = Object.keys(page).filter((key) => /^[A-Z]/.test(key) &&
|
|
334
|
+
(typeof page[key] === "function" ||
|
|
335
|
+
(page[key] !== null &&
|
|
336
|
+
typeof page[key] === "object" &&
|
|
337
|
+
"$$typeof" in page[key])) &&
|
|
338
|
+
!["Preview", "Playground", "PropsSchema"].includes(key));
|
|
339
|
+
if (page.Playground)
|
|
340
|
+
names.unshift("Playground");
|
|
341
|
+
const selected = names.includes(example) ? example : "Preview";
|
|
342
|
+
const frameUrl = entry && link({ component: entry.path, example: selected, frame: "1" });
|
|
343
|
+
React.useLayoutEffect(() => {
|
|
344
|
+
if (!frame && frameUrl)
|
|
345
|
+
setFrameLoading(true);
|
|
346
|
+
}, [frameUrl]);
|
|
347
|
+
if (frame && entry) {
|
|
348
|
+
const Component = page[selected];
|
|
349
|
+
if (!Component)
|
|
350
|
+
return _jsxs("p", { children: ["Export Preview from ", entry.name, ".mira.tsx."] });
|
|
351
|
+
return (_jsxs("main", { className: "miral:box-border miral:min-h-dvh miral:bg-paper miral:text-ink miral:data-[layout=centered]:grid miral:data-[layout=centered]:place-items-center miral:data-[layout=centered]:p-4 miral:data-[layout=padded]:p-4", "data-layout": (selected === "Playground" ? page.playgroundLayout : page.layout) ??
|
|
352
|
+
"centered", children: [!!stylesheet && (_jsx("link", { rel: "stylesheet", href: stylesheet, precedence: "preview" })), _jsx(Component, { ...(selected === "Playground" ? values : {}) })] }));
|
|
353
|
+
}
|
|
354
|
+
const size = layouts[layout];
|
|
355
|
+
const scale = size && stageWidth !== undefined ? Math.min(1, stageWidth / size[0]) : 1;
|
|
356
|
+
return (_jsxs("div", { className: "preview-shell miral:group/shell", "data-isolated": isolated || undefined, children: [!!entry && frameLoading && _jsx(Spinner, {}), _jsx(Nav, { title: title, entries: entries, assetsPath: assets && assetsEntry.path, activePath: entry?.path, names: names, selected: selected, theme: theme, layout: layout, isolated: isolated, link: link, onLinkClick: handleLinkClick }), _jsx("main", { className: "miral:flex miral:min-h-0 miral:flex-col miral:[@media(max-width:600px)]:flex-1", children: !!entry && (_jsxs(_Fragment, { children: [_jsx("div", { className: "miral:sticky miral:top-0 miral:z-1 miral:transition-opacity miral:duration-200 miral:ease-out miral:group-data-isolated/shell:opacity-0 miral:motion-reduce:transition-none", inert: isolated, children: _jsxs(Toolbar, { hidden: toolbarHidden, isolated: isolated, children: [devices.map(({ name, label, icon }) => (_jsx("button", { type: "button", value: name, "aria-label": label, "aria-pressed": layout === name, "aria-keyshortcuts": "Shift+L", title: label + " · Shift + L to cycle layouts", onClick: handleLayoutClick, children: _jsx(Icon, { name: icon }) }, name))), _jsx("button", { type: "button", role: "switch", "aria-label": "Dark preview", "aria-keyshortcuts": "Shift+T", title: "Switch theme \u00B7 Shift + T", "aria-checked": theme === "dark", onClick: handleThemeClick, children: _jsx(Icon, { name: theme === "light" ? "sun" : "moon" }) }), _jsx("button", { type: "button", ref: isolationButton, onClick: handleIsolationClick, "aria-label": "Full screen", "aria-keyshortcuts": "Shift+F", title: "Full screen \u00B7 Shift + F (Esc to exit)", children: _jsx(Icon, { name: "expand" }) })] }) }), _jsx("div", { className: "miral:flex-1 miral:overflow-auto", ref: stage, children: _jsx("div", { className: "miral:h-full miral:overflow-clip", style: size
|
|
357
|
+
? { width: size[0] * scale, height: size[1] * scale }
|
|
358
|
+
: undefined, children: _jsx("iframe", { className: "miral:block miral:origin-top-left miral:transition-none miral:max-w-none miral:border-0 miral:bg-paper miral:group-has-[[data-dragging]]/shell:pointer-events-none miral:group-data-isolated/shell:fixed miral:group-data-isolated/shell:inset-0 miral:group-data-isolated/shell:w-full miral:group-data-isolated/shell:h-dvh miral:group-data-isolated/shell:z-1", ref: iframe, style: {
|
|
359
|
+
transform: isolated || scale === 1 ? "none" : "scale(" + scale + ")",
|
|
360
|
+
}, title: entry.name, width: size?.[0] ?? "100%", height: size?.[1] ?? "100%", src: frameUrl }, frameUrl) }) }), selected === "Playground" && !!page.PropsSchema && (_jsx(Controls, { schema: page.PropsSchema, values: values, onChange: setValues }))] })) })] }));
|
|
361
|
+
};
|
|
362
|
+
const container = document.getElementById("root");
|
|
363
|
+
if (container)
|
|
364
|
+
createRoot(container).render(_jsx(React.Suspense, { fallback: frame && parent !== window ? _jsx(FrameLoading, {}) : _jsx(Spinner, {}), children: _jsx(Viewer, {}) }));
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { parseArgs } from "node:util";
|
|
6
|
+
import tailwind from "@tailwindcss/vite";
|
|
7
|
+
import react from "@vitejs/plugin-react";
|
|
8
|
+
import { build, createServer, parseSync, } from "vite";
|
|
9
|
+
const { positionals: [command, directory], values, } = parseArgs({
|
|
10
|
+
allowPositionals: true,
|
|
11
|
+
options: { port: { type: "string" }, outDir: { type: "string" } },
|
|
12
|
+
});
|
|
13
|
+
if (!["dev", "build"].includes(command)) {
|
|
14
|
+
console.error("Usage: miral dev [directory] [--port 9000] | miral build [directory] [--outDir dist]");
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
const root = path.resolve(directory ?? ".");
|
|
18
|
+
const htmlId = path.join(root, "index.html");
|
|
19
|
+
const extension = import.meta.filename.endsWith(".ts") ? "tsx" : "js";
|
|
20
|
+
const entry = `/@miral/Viewer.${extension}`;
|
|
21
|
+
const html = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Miral</title></head><body><div id="root"></div><script type="module" src="${entry}"></script></body></html>`;
|
|
22
|
+
const sources = new Map();
|
|
23
|
+
for (const name of [
|
|
24
|
+
"Viewer",
|
|
25
|
+
"icons",
|
|
26
|
+
"Assets",
|
|
27
|
+
"Nav",
|
|
28
|
+
"Spinner",
|
|
29
|
+
"Toolbar",
|
|
30
|
+
"Controls",
|
|
31
|
+
]) {
|
|
32
|
+
const filename = `${name}.${extension}`;
|
|
33
|
+
sources.set(path.join(root, "@miral", filename), `./${filename}`);
|
|
34
|
+
}
|
|
35
|
+
const sourceFiles = new Map(Array.from(sources, ([id, file]) => [
|
|
36
|
+
fileURLToPath(new URL(file, import.meta.url)),
|
|
37
|
+
id,
|
|
38
|
+
]));
|
|
39
|
+
const viewer = {
|
|
40
|
+
name: "miral-preview",
|
|
41
|
+
enforce: "pre",
|
|
42
|
+
resolveId(id, importer) {
|
|
43
|
+
if (id === "/__miral-viewer.css")
|
|
44
|
+
return fileURLToPath(new URL("./theme.css", import.meta.url));
|
|
45
|
+
if (id === htmlId || sources.has(id))
|
|
46
|
+
return id;
|
|
47
|
+
const candidate = id.startsWith("/")
|
|
48
|
+
? path.join(root, id)
|
|
49
|
+
: path.resolve(path.dirname(importer ?? htmlId), id);
|
|
50
|
+
if (sources.has(candidate))
|
|
51
|
+
return candidate;
|
|
52
|
+
},
|
|
53
|
+
transform(source, id) {
|
|
54
|
+
if (id === path.join(root, "miral.config.ts")) {
|
|
55
|
+
const { program } = parseSync(id, source);
|
|
56
|
+
const config = program.body.find((node) => node.type === "ExportDefaultDeclaration");
|
|
57
|
+
let object = config?.declaration;
|
|
58
|
+
while (object?.type === "TSAsExpression" ||
|
|
59
|
+
object?.type === "TSSatisfiesExpression")
|
|
60
|
+
object = object.expression;
|
|
61
|
+
if (object?.type !== "ObjectExpression")
|
|
62
|
+
return;
|
|
63
|
+
const property = object.properties.find((item) => {
|
|
64
|
+
if (item.type !== "Property")
|
|
65
|
+
return false;
|
|
66
|
+
const key = item.key;
|
|
67
|
+
return ((key.type === "Identifier" && key.name === "assetsDir") ||
|
|
68
|
+
(key.type === "Literal" && key.value === "assetsDir"));
|
|
69
|
+
});
|
|
70
|
+
if (property?.type !== "Property")
|
|
71
|
+
return;
|
|
72
|
+
const directory = property.value.type === "Literal" && property.value.value;
|
|
73
|
+
if (typeof directory !== "string" || !directory.trim())
|
|
74
|
+
this.error("Use a directory path string literal for assetsDir in miral.config.ts.");
|
|
75
|
+
const pattern = path.posix.join(directory, "**/*");
|
|
76
|
+
const glob = pattern.startsWith(".") || pattern.startsWith("/")
|
|
77
|
+
? pattern
|
|
78
|
+
: "./" + pattern;
|
|
79
|
+
return (source +
|
|
80
|
+
"\nexport const assetFiles = import.meta.glob(" +
|
|
81
|
+
JSON.stringify(glob) +
|
|
82
|
+
', { eager: true, query: "?url", import: "default" });');
|
|
83
|
+
}
|
|
84
|
+
if (id.endsWith(".mira.tsx")) {
|
|
85
|
+
const name = path.basename(id, ".mira.tsx") + ".tsx";
|
|
86
|
+
const pattern = "./" + name.replace(/[\\*?{}[\]()!+@]/g, "\\$&");
|
|
87
|
+
const component = JSON.stringify("./" + name);
|
|
88
|
+
return (source +
|
|
89
|
+
"\nexport const miralComponent = import.meta.glob(" +
|
|
90
|
+
JSON.stringify(pattern) +
|
|
91
|
+
", { eager: true })[" +
|
|
92
|
+
component +
|
|
93
|
+
"];");
|
|
94
|
+
}
|
|
95
|
+
if (!id.endsWith(".mira.tsx?miral-section"))
|
|
96
|
+
return;
|
|
97
|
+
const { program } = parseSync(id.split("?")[0], source);
|
|
98
|
+
for (const statement of program.body) {
|
|
99
|
+
if (statement.type !== "ExportNamedDeclaration" ||
|
|
100
|
+
statement.declaration?.type !== "VariableDeclaration")
|
|
101
|
+
continue;
|
|
102
|
+
const field = statement.declaration.declarations.find((item) => item.id.type === "Identifier" && item.id.name === "section");
|
|
103
|
+
if (!field)
|
|
104
|
+
continue;
|
|
105
|
+
if (field.init?.type !== "Literal" ||
|
|
106
|
+
typeof field.init.value !== "string")
|
|
107
|
+
this.error("Use a string literal for section in " + id.split("?")[0]);
|
|
108
|
+
return "export default " + JSON.stringify(field.init.value.trim());
|
|
109
|
+
}
|
|
110
|
+
return 'export default ""';
|
|
111
|
+
},
|
|
112
|
+
async load(id) {
|
|
113
|
+
if (id === htmlId)
|
|
114
|
+
return html;
|
|
115
|
+
const file = sources.get(id);
|
|
116
|
+
if (file) {
|
|
117
|
+
const result = await readFile(new URL(file, import.meta.url), "utf8");
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
handleHotUpdate({ file, server, modules }) {
|
|
122
|
+
const id = sourceFiles.get(file);
|
|
123
|
+
if (!id)
|
|
124
|
+
return;
|
|
125
|
+
const module = server.moduleGraph.getModuleById(id);
|
|
126
|
+
if (module)
|
|
127
|
+
return [...modules, module];
|
|
128
|
+
},
|
|
129
|
+
configureServer(server) {
|
|
130
|
+
server.watcher.add([...sourceFiles.keys()]);
|
|
131
|
+
server.middlewares.use(async (request, response, next) => {
|
|
132
|
+
const url = new URL(request.url ?? "/", "http://localhost");
|
|
133
|
+
if (!["/", "/index.html"].includes(url.pathname))
|
|
134
|
+
return next();
|
|
135
|
+
try {
|
|
136
|
+
response.setHeader("Content-Type", "text/html");
|
|
137
|
+
response.end(await server.transformIndexHtml(url.pathname, html));
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
next(error);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
const config = {
|
|
146
|
+
root,
|
|
147
|
+
base: "./",
|
|
148
|
+
configFile: false,
|
|
149
|
+
plugins: [viewer, react(), tailwind({ optimize: false })],
|
|
150
|
+
optimizeDeps: {
|
|
151
|
+
entries: ["**/*.mira.tsx", "!**/dist/**"],
|
|
152
|
+
include: [
|
|
153
|
+
"react",
|
|
154
|
+
"react-dom/client",
|
|
155
|
+
"react/jsx-runtime",
|
|
156
|
+
"react/jsx-dev-runtime",
|
|
157
|
+
],
|
|
158
|
+
},
|
|
159
|
+
resolve: { dedupe: ["react", "react-dom"] },
|
|
160
|
+
};
|
|
161
|
+
if (command === "build") {
|
|
162
|
+
await build({
|
|
163
|
+
...config,
|
|
164
|
+
build: {
|
|
165
|
+
cssTarget: "esnext",
|
|
166
|
+
outDir: path.resolve(values.outDir ?? "dist"),
|
|
167
|
+
emptyOutDir: false,
|
|
168
|
+
rolldownOptions: { input: htmlId },
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
const server = await createServer({
|
|
174
|
+
...config,
|
|
175
|
+
server: { port: Number(values.port ?? 9000), strictPort: true },
|
|
176
|
+
});
|
|
177
|
+
await server.listen();
|
|
178
|
+
server.printUrls();
|
|
179
|
+
}
|
package/dist/icons.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
const paths = {
|
|
3
|
+
phone: "M8 2.5h8a2 2 0 0 1 2 2v15a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2v-15a2 2 0 0 1 2-2Z M10.5 18.5h3",
|
|
4
|
+
tablet: "M5.5 2.5h13a2 2 0 0 1 2 2v15a2 2 0 0 1-2 2h-13a2 2 0 0 1-2-2v-15a2 2 0 0 1 2-2Z M12 5.5h.01",
|
|
5
|
+
desktop: "M2 3h20v14H2z M12 17v4 M7 21h10",
|
|
6
|
+
sun: "M16 12a4 4 0 1 1-8 0 4 4 0 0 1 8 0z M12 2v2m0 16v2M2 12h2m16 0h2M5 5l1.5 1.5m11 11L19 19M5 19l1.5-1.5m11-11L19 5",
|
|
7
|
+
moon: "M21 14a9 9 0 0 1-11-11 9.2 9.2 0 1 0 11 11z",
|
|
8
|
+
expand: "M4 9V4h5m6 0h5v5m0 6v5h-5m-6 0H4v-5",
|
|
9
|
+
grip: "M9 6h.1M15 6h.1M9 12h.1M15 12h.1M9 18h.1M15 18h.1",
|
|
10
|
+
close: "M6 6l12 12M18 6L6 18",
|
|
11
|
+
};
|
|
12
|
+
export const Icon = ({ name, className }) => (_jsx("svg", { className: className, "aria-hidden": "true", width: "1em", height: "1em", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: name === "grip" ? 3 : 2, strokeLinecap: "round", strokeLinejoin: "round", children: _jsx("path", { d: paths[name] }) }));
|
package/{src → dist}/theme.css
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
@layer theme, components, utilities;
|
|
2
2
|
@import "tailwindcss/theme.css" layer(theme) prefix(miral);
|
|
3
3
|
@import "tailwindcss/utilities.css" layer(utilities) prefix(miral) source(none);
|
|
4
|
-
@source "./*.tsx";
|
|
4
|
+
@source "./*.{js,tsx}";
|
|
5
5
|
|
|
6
6
|
@theme inline {
|
|
7
7
|
--color-paper: var(--paper);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "miral",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Like Storybook, but much simpler.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -8,16 +8,23 @@
|
|
|
8
8
|
"node": ">=24.0.0"
|
|
9
9
|
},
|
|
10
10
|
"bin": {
|
|
11
|
-
"miral": "
|
|
11
|
+
"miral": "dist/cli.js"
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
|
-
"
|
|
14
|
+
"dist/*.js",
|
|
15
|
+
"dist/theme.css"
|
|
15
16
|
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"lint": "oxlint --fix && biome check --write --unsafe && tsc --noEmit",
|
|
19
|
+
"build": "tsc && cp src/theme.css dist/",
|
|
20
|
+
"prepack": "pnpm run build"
|
|
21
|
+
},
|
|
16
22
|
"dependencies": {
|
|
17
23
|
"@tailwindcss/vite": "^4.3.5",
|
|
18
24
|
"@vitejs/plugin-react": "^6.3.0",
|
|
25
|
+
"nano-ssg": "0.0.8",
|
|
19
26
|
"tailwindcss": "^4.3.5",
|
|
20
|
-
"vite": "^8.
|
|
27
|
+
"vite": "^8.4.0"
|
|
21
28
|
},
|
|
22
29
|
"peerDependencies": {
|
|
23
30
|
"react": "^19.3.1",
|
|
@@ -25,14 +32,12 @@
|
|
|
25
32
|
},
|
|
26
33
|
"devDependencies": {
|
|
27
34
|
"@biomejs/biome": "^2.6.9",
|
|
28
|
-
"@types/
|
|
35
|
+
"@types/node": "^24.19.4",
|
|
36
|
+
"@types/react": "^19.3.3",
|
|
29
37
|
"@types/react-dom": "^19.3.0",
|
|
30
38
|
"oxlint": "^1.95.0",
|
|
31
|
-
"oxlint-better": "^1.0.
|
|
39
|
+
"oxlint-better": "^1.0.8",
|
|
32
40
|
"typescript": "~7.0.2",
|
|
33
41
|
"zod": "^4.6.7"
|
|
34
|
-
},
|
|
35
|
-
"scripts": {
|
|
36
|
-
"lint": "oxlint --fix && biome check --write --unsafe && tsc --noEmit"
|
|
37
42
|
}
|
|
38
43
|
}
|
package/src/Assets.tsx
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
type AssetsProps = {
|
|
2
|
-
className?: string;
|
|
3
|
-
files: Record<string, string>;
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
export const Assets = ({ className, files }: AssetsProps) => (
|
|
7
|
-
<section className={className}>
|
|
8
|
-
<h1>Assets</h1>
|
|
9
|
-
{!Object.keys(files).length && <p>No assets found.</p>}
|
|
10
|
-
<div className="miral:grid miral:grid-cols-[repeat(auto-fill,minmax(min(100%,12rem),1fr))] miral:gap-4">
|
|
11
|
-
{Object.entries(files)
|
|
12
|
-
.sort(([a], [b]) => a.localeCompare(b))
|
|
13
|
-
.map(([path, url]) => (
|
|
14
|
-
<figure
|
|
15
|
-
key={path}
|
|
16
|
-
className="miral:grid miral:content-start miral:gap-1.5 miral:m-0"
|
|
17
|
-
>
|
|
18
|
-
<figcaption className="miral:text-left miral:wrap-anywhere">
|
|
19
|
-
<a href={url} download={path.split("/").at(-1)}>
|
|
20
|
-
{path.split("/").at(-1)}
|
|
21
|
-
</a>
|
|
22
|
-
</figcaption>
|
|
23
|
-
<div className="miral:grid miral:place-items-center miral:min-h-32 miral:p-4 miral:border miral:border-solid miral:border-edge miral:rounded-lg">
|
|
24
|
-
{/\.(avif|bmp|gif|ico|jpe?g|png|svg|webp)$/i.test(path) ? (
|
|
25
|
-
<img
|
|
26
|
-
className="miral:w-full miral:h-32 miral:object-contain"
|
|
27
|
-
src={url}
|
|
28
|
-
alt=""
|
|
29
|
-
loading="lazy"
|
|
30
|
-
/>
|
|
31
|
-
) : (
|
|
32
|
-
<a href={url} download={path.split("/").at(-1)}>
|
|
33
|
-
Download
|
|
34
|
-
</a>
|
|
35
|
-
)}
|
|
36
|
-
</div>
|
|
37
|
-
</figure>
|
|
38
|
-
))}
|
|
39
|
-
</div>
|
|
40
|
-
</section>
|
|
41
|
-
);
|