miral 0.0.4 → 0.0.6

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 CHANGED
@@ -1,55 +1,103 @@
1
1
  # Miral
2
2
 
3
- Like storybook, but opinionated, much simpler, and without interaction testing.
4
- Built on `vite`, `react`, and `tailwind`.
3
+ Miral is a lightweight UI kit explorer for React and Preact.
4
+ It’s opinionated, TypeScript-first, and ESM-first.
5
+ Built on Vite, React, Preact, and Tailwind CSS.
6
+
7
+ TLDR: Like storybook, but simpler.
8
+
9
+ ## Why Miral
10
+
11
+ Working on a component should be simpler than setting up the tools around it.
12
+ Miral grew out of frustration with Storybook’s complex toolchain, layers of configuration, tree of aging dependencies,
13
+ endless stream of warnings, bloated UI full of features rarely used, and addons to learn and configure.
14
+ As the ecosystem moved on, keeping Storybook working with newer TypeScript versions and evolving ESM support took more
15
+ effort than it should have.
16
+ Miral takes a simpler approach. TypeScript first, ESM first, and one opinionated setup built on Vite, React or Preact,
17
+ Zod, and Tailwind CSS.
18
+ Modern stack, minimal setup. Spend your time building components instead of maintaining configuration files and chasing
19
+ outdated dependencies.
5
20
 
6
21
  ## Usage
7
22
 
8
23
  ```sh
9
- npx miral preview
10
- npx miral build
24
+ npx miral dev --port 9000
25
+ npx miral build && npx -y fotone --port 9000 ./dist
11
26
  ```
12
27
 
13
- Open `http://localhost:9000` for development. Serve the generated `dist/` directory with any static HTTP server. Asset
14
- URLs are relative, so the catalog can also run under a subdirectory. Use `--port` for a different development port or
15
- `--outDir` for a different build directory. Both commands accept an optional directory to scan.
28
+ Open `http://localhost:9000` for development. Serve the generated `dist/` directory with any static HTTP server.
29
+ Asset URLs are relative, so the catalog can also run under a subdirectory.
30
+ Install `react` and `react-dom` for React catalogs, or `preact` for Preact catalogs.
16
31
 
17
- ## Sidebar
32
+ ## Add components
18
33
 
19
- Place an optional `miral.config.ts` in the directory scanned by `preview` or `build`.
34
+ Miral reads the named export `PropsSchema` from the matching component file. Give the schema any local name and
35
+ export it under that name with `as PropsSchema`.
20
36
 
21
- ```ts
22
- export default {
23
- title: "My UI kit",
24
- assetsDir: "./assets",
25
- order: ["Controls", "Components"],
26
- componentOrder: ["Select", "Input"],
37
+ ```tsx
38
+ // Button.tsx
39
+ import { z } from "zod";
40
+
41
+ export const PropsSchema = z.object({
42
+ label: z.string().default("Save"),
43
+ disabled: z.boolean().default(false),
44
+ });
45
+ export type Props = z.input<typeof PropsSchema>;
46
+
47
+ export const Button = (props: Props) => {
48
+ const { label, disabled } = PropsSchema.parse(props);
49
+ return <button type="button" disabled={disabled}>{label}</button>;
27
50
  };
28
51
  ```
29
52
 
30
- `title` sets the optional H1 at the top of the sidebar and the browser tab title (`My UI kit | Miral`). The tab shows `Miral` when the title is empty or omitted. `order` lists sections. `componentOrder` orders components within each section. Unlisted names follow in alphabetical order. Omit either list to use alphabetical order throughout that level.
53
+ ```tsx
54
+ // Button.mira.tsx
55
+ import { Button } from "./Button";
31
56
 
32
- Use an optional string literal `assetsDir` to add an Assets story at the end of the sidebar. Set the path relative to `miral.config.ts`. Each file in that directory and its subdirectories gets a card with its filename. Image cards also show a preview.
57
+ export const section = "Controls";
58
+ export default Button;
59
+ export const Preview = () => <Button/>;
60
+ export const Disabled = () => <Button disabled/>;
61
+ ```
33
62
 
34
- ## Preview files
63
+ - The default export opens in `Playground` with the schema defaults. Use the generated controls to edit the props.
64
+ - `Preview` opens when you select **Button**. Other capitalized component exports, such as `Disabled`, appear as
65
+ examples.
66
+ - `section` must be a string literal. Omit it to use `Elements`.
35
67
 
36
- Place `*.mira.tsx` files anywhere in the project. The filename names the component. Declare a section with a string literal, such as `export const section = "Metrics"`. Files without a section appear under `Elements`.
68
+ ## CLI reference
37
69
 
38
- Without `miral.config.ts`, sections and their entries sort alphabetically. If no file declares a section, all entries appear in one alphabetical `Elements` list.
70
+ ```sh
71
+ miral dev [directory] [--port <port>] [--framework react|preact]
72
+ miral build [directory] [--outDir <path>] [--framework react|preact]
73
+ ```
39
74
 
40
- ```tsx
41
- import * as z from "zod/mini";
75
+ | Argument | Command | Behavior |
76
+ | --- | --- | --- |
77
+ | `directory` | Both | Directory to scan. Defaults to the current directory. |
78
+ | `--framework` | Both | Overrides the config for this run. Defaults to `react`. |
79
+ | `--port` | `dev` | Development server port. Defaults to `9000`. |
80
+ | `--outDir` | `build` | Output directory relative to the working directory. Defaults to `dist`. |
42
81
 
43
- export const schema = z.object({ value: z.number() });
44
- export const defaults = { value: 24 };
45
- export const Playground = ({ value }: z.infer<typeof schema>) => <output>{value}</output>;
46
- export const Preview = () => <Playground {...defaults} />;
47
- export const Empty = () => <Playground value={0} />;
48
- ```
82
+ ## Configuration reference
49
83
 
50
- `Preview` opens when the component is selected. Export `Playground` and a Zod Mini object named `schema` for editable
51
- props. Supply `defaults` for required editable fields. String, number, boolean, and string enum fields generate
52
- controls. Use a wrapper component to provide callbacks, children, form providers, or sample data. Other capitalized
53
- component exports appear below Playground. Import project styles in preview files when needed.
84
+ Place an optional `miral.config.ts` in the directory Miral scans. All options are optional.
85
+
86
+ ```ts
87
+ export default {
88
+ framework: "preact",
89
+ title: "My UI kit",
90
+ assetsDir: "./assets",
91
+ order: ["Controls", "Components"],
92
+ componentOrder: ["Select", "Input"],
93
+ };
94
+ ```
54
95
 
55
- To style the preview iframe, import a project stylesheet with Vite’s `?url` suffix in `miral.config.ts` and export its URL as the `stylesheet` option. Miral supplies its own styles and icons for the catalog controls.
96
+ | Option | Type | Behavior |
97
+ |------------------|----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
98
+ | `framework` | String literal | Selects `"react"` (default) or `"preact"`. Restart the development server after changing it. |
99
+ | `title` | `string` | Sets the sidebar heading and browser tab title, for example `My UI kit \| Miral`. If empty or omitted, the sidebar has no heading and the tab shows `Miral`. |
100
+ | `assetsDir` | String literal | Path relative to `miral.config.ts`. Adds an Assets entry at the end of the sidebar. Files in this directory and its subdirectories appear by filename, with previews for images. |
101
+ | `order` | `string[]` | Lists sidebar sections in display order. Remaining sections follow alphabetically. |
102
+ | `componentOrder` | `string[]` | Lists components in display order within each section. Remaining components follow alphabetically. |
103
+ | `stylesheet` | `string` | Sets the preview iframe stylesheet. Import the CSS file with Vite’s `?url` suffix and pass the URL here. |
package/dist/Assets.js ADDED
@@ -0,0 +1,4 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ export const Assets = ({ className, files }) => (_jsxs("section", { className: className, children: [_jsx("h1", { children: "Assets" }), !Object.keys(files).length && _jsx("p", { children: "No assets found." }), _jsx("div", { className: "miral:grid miral:grid-cols-[repeat(auto-fill,minmax(min(100%,12rem),1fr))] miral:gap-4", children: Object.entries(files)
3
+ .sort(([a], [b]) => a.localeCompare(b))
4
+ .map(([path, url]) => (_jsxs("figure", { className: "miral:grid miral:content-start miral:gap-1.5 miral:m-0", children: [_jsx("figcaption", { className: "miral:text-left miral:wrap-anywhere", children: _jsx("a", { href: url, download: path.split("/").at(-1), children: path.split("/").at(-1) }) }), _jsx("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", children: /\.(avif|bmp|gif|ico|jpe?g|png|svg|webp)$/i.test(path) ? (_jsx("img", { className: "miral:w-full miral:h-32 miral:object-contain", src: url, alt: "", loading: "lazy" })) : (_jsx("a", { href: url, download: path.split("/").at(-1), children: "Download" })) })] }, path))) })] }));
@@ -0,0 +1,55 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ export const Controls = ({ schema, values, onChange }) => {
3
+ return (_jsxs("section", { className: "miral:border-t miral:border-edge miral:text-sm/[1.5] miral:font-normal miral:group-data-isolated/shell:hidden", "aria-labelledby": "preview-props-heading", children: [_jsx("h2", { id: "preview-props-heading", className: "miral:m-0 miral:px-4 miral:py-2 miral:text-[length:inherit] miral:font-semibold", children: "Props" }), _jsx("form", { className: "miral:max-h-[30dvh] miral:overflow-auto miral:px-4 miral:pb-2", onSubmit: preventSubmit, children: Object.entries(schema.shape).map(([name, field]) => {
4
+ let inner = field._zod.def;
5
+ let optional = false;
6
+ while ((inner.type === "optional" || inner.type === "default") &&
7
+ "innerType" in inner) {
8
+ optional ||= inner.type === "optional";
9
+ inner = inner.innerType._zod.def;
10
+ }
11
+ let options = [];
12
+ if ("entries" in inner)
13
+ options = Object.values(inner.entries);
14
+ else if ("values" in inner)
15
+ options = inner.values;
16
+ const value = values[name];
17
+ const handleChange = (event) => {
18
+ const text = event.currentTarget.value;
19
+ const next = { ...values };
20
+ if (event.currentTarget instanceof HTMLInputElement &&
21
+ event.currentTarget.type === "checkbox")
22
+ next[name] = event.currentTarget.checked;
23
+ else if (inner.type === "boolean")
24
+ next[name] = text === "true";
25
+ else if (inner.type === "number")
26
+ next[name] = Number(text);
27
+ else
28
+ next[name] = text;
29
+ if (text === "" && optional)
30
+ delete next[name];
31
+ const parsed = schema.safeParse(next);
32
+ if (parsed.success)
33
+ onChange(parsed.data);
34
+ };
35
+ if (inner.type === "boolean" && optional) {
36
+ return (_jsxs("div", { className: "preview-prop-row", role: "radiogroup", "aria-label": name, children: [_jsx("span", { "aria-hidden": "true", children: name }), _jsx("div", { className: "miral:flex miral:w-fit miral:border miral:border-solid miral:border-edge miral:rounded-sm", children: [
37
+ ["", "Default"],
38
+ ["true", "On"],
39
+ ["false", "Off"],
40
+ ].map(([choice, label]) => (_jsxs("label", { className: "miral:relative miral:py-[0.2rem] miral:px-2 miral:cursor-pointer miral:has-checked:text-paper miral:has-checked:bg-ink miral:focus-within:outline-2 miral:focus-within:outline-solid miral:focus-within:outline-ink miral:focus-within:outline-offset-2", children: [_jsx("input", { className: "miral:absolute miral:inset-0 miral:m-0 miral:opacity-0 miral:cursor-pointer", type: "radio", name: name, value: choice, checked: String(value ?? "") === choice, onChange: handleChange }), label] }, choice))) })] }, name));
41
+ }
42
+ if (inner.type === "boolean") {
43
+ return (_jsxs("label", { className: "preview-prop-row", children: [name, _jsx("input", { className: "miral:justify-self-start miral:accent-ink", type: "checkbox", checked: value === true, onChange: handleChange })] }, name));
44
+ }
45
+ if (options.length) {
46
+ return (_jsxs("label", { className: "preview-prop-row", children: [name, _jsxs("select", { value: String(value ?? ""), onChange: handleChange, children: [optional && _jsx("option", { value: "", children: "Default" }), options.map((item) => (_jsx("option", { children: item }, item)))] })] }, name));
47
+ }
48
+ if (inner.type !== "string" && inner.type !== "number")
49
+ return null;
50
+ return (_jsxs("label", { className: "preview-prop-row", children: [name, _jsx("input", { type: inner.type === "number" ? "number" : "text", value: String(value ?? ""), onChange: handleChange })] }, name));
51
+ }) })] }));
52
+ };
53
+ function preventSubmit(event) {
54
+ event.preventDefault();
55
+ }
package/dist/Nav.js ADDED
@@ -0,0 +1,17 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ export const Nav = ({ title, className, entries, activePath, assetsPath, names, selected, theme, layout, isolated, link, onLinkClick, }) => (_jsxs("nav", { className: [
3
+ "miral:overflow-auto miral:wrap-anywhere miral:py-[1.375rem] miral:px-3 miral:border-e miral:border-divider miral:[font-family:Inter,system-ui,sans-serif] miral:transition-opacity miral:duration-200 miral:ease-out miral:group-data-isolated/shell:opacity-0 miral:motion-reduce:transition-none miral:[@media(max-width:600px)]:max-h-[25dvh]",
4
+ className,
5
+ ]
6
+ .filter(Boolean)
7
+ .join(" "), inert: isolated, "aria-label": "Component catalog", children: [!!title && (_jsx("h1", { className: "miral:mx-2 miral:mt-0 miral:mb-[1.625rem] miral:text-2xl/[1.1] miral:font-bold miral:tracking-[-0.045em]", children: title })), Array.from(new Set(entries.map((item) => item.section))).map((section) => (_jsxs("section", { className: "miral:[section+&]:mt-[1.375rem]", children: [_jsx("h2", { className: "miral:mx-2 miral:mt-0 miral:mb-1.5 miral:text-muted miral:text-[0.71875rem]/[1.4] miral:font-[650] miral:tracking-[0.075em] miral:uppercase", children: section }), entries
8
+ .filter((item) => item.section === section)
9
+ .map((item) => (_jsxs("div", { children: [_jsx("a", { "aria-current": item.path === activePath && selected === "Preview"
10
+ ? "page"
11
+ : undefined, href: link({ component: item.path, theme, layout }), onClick: onLinkClick, children: item.name }), item.path === activePath &&
12
+ names.map((name) => (_jsx("a", { className: "miral:ms-2 miral:ps-3.5 miral:rounded-l-none", onClick: onLinkClick, "aria-current": selected === name ? "page" : undefined, href: link({
13
+ component: item.path,
14
+ example: name,
15
+ theme,
16
+ layout,
17
+ }), children: name.replace(/([a-z])([A-Z])/g, "$1 $2") }, name)))] }, item.path)))] }, section))), !!assetsPath && (_jsx("a", { className: "miral:mt-4", href: link({ component: assetsPath, theme, layout }), "aria-current": activePath === assetsPath ? "page" : undefined, onClick: onLinkClick, children: "Assets" }))] }));
@@ -0,0 +1,7 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ export const Spinner = ({ className }) => (_jsx("span", { className: [
3
+ "miral:fixed miral:inset-0 miral:z-2 miral:m-auto miral:inline-block miral:box-border miral:size-[1em] miral:pointer-events-none miral:rounded-full miral:border-2 miral:border-solid miral:border-current miral:border-r-transparent miral:text-ink miral:motion-safe:animate-spin miral:motion-safe:[animation-duration:700ms]",
4
+ className,
5
+ ]
6
+ .filter(Boolean)
7
+ .join(" "), "data-miral-spinner": true, role: "status", "aria-label": "Loading" }));
@@ -0,0 +1,139 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import React from "react";
3
+ import { Icon } from "./icons.js";
4
+ const storageKey = "miral.preview:" + location.pathname + ":toolbar";
5
+ const guideKey = storageKey + ":guide-seen";
6
+ function read(key) {
7
+ try {
8
+ const value = localStorage.getItem(key);
9
+ return value;
10
+ }
11
+ catch {
12
+ return null;
13
+ }
14
+ }
15
+ function save(key, value) {
16
+ try {
17
+ localStorage.setItem(key, value);
18
+ }
19
+ catch {
20
+ /* Storage can be disabled. */
21
+ }
22
+ }
23
+ function readPosition() {
24
+ try {
25
+ const value = JSON.parse(read(storageKey) ?? "null");
26
+ if (value && Number.isFinite(value.x) && Number.isFinite(value.y)) {
27
+ const result = { x: value.x, y: value.y };
28
+ return result;
29
+ }
30
+ }
31
+ catch {
32
+ /* Ignore invalid saved coordinates. */
33
+ }
34
+ return null;
35
+ }
36
+ function constrain(position, element) {
37
+ const x = Math.max(0, Math.min(position.x, document.documentElement.clientWidth - element.offsetWidth - 4));
38
+ const y = Math.max(0, Math.min(position.y, window.innerHeight - element.offsetHeight - 4));
39
+ return { x, y };
40
+ }
41
+ const shortcuts = [
42
+ ["Cycle layout", "Shift + L"],
43
+ ["Switch theme", "Shift + T"],
44
+ ["Toggle full screen", "Shift + F"],
45
+ ["Exit full screen", "Esc"],
46
+ ["Show or hide toolbar", "Shift + C"],
47
+ ];
48
+ export const Toolbar = ({ children, hidden, isolated, className, }) => {
49
+ const dock = React.useRef(null);
50
+ const drag = React.useRef(null);
51
+ const [position, setPosition] = React.useState(readPosition);
52
+ const [guide, setGuide] = React.useState(() => read(guideKey) !== "1");
53
+ const [dragging, setDragging] = React.useState(false);
54
+ React.useEffect(() => {
55
+ if (guide && !hidden && !isolated)
56
+ save(guideKey, "1");
57
+ }, [guide, hidden, isolated]);
58
+ React.useLayoutEffect(() => {
59
+ const element = dock.current;
60
+ if (!element)
61
+ return;
62
+ const fit = () => setPosition((current) => {
63
+ if (!current)
64
+ return current;
65
+ const next = constrain(current, element);
66
+ if (next.x === current.x && next.y === current.y)
67
+ return current;
68
+ return next;
69
+ });
70
+ fit();
71
+ const observer = new ResizeObserver(fit);
72
+ observer.observe(element);
73
+ window.addEventListener("resize", fit);
74
+ return () => {
75
+ observer.disconnect();
76
+ window.removeEventListener("resize", fit);
77
+ };
78
+ }, []);
79
+ const handlePointerDown = (event) => {
80
+ if (event.button !== 0 || !event.isPrimary || !dock.current)
81
+ return;
82
+ const rect = dock.current.getBoundingClientRect();
83
+ drag.current = {
84
+ x: event.clientX - rect.left,
85
+ y: event.clientY - rect.top,
86
+ };
87
+ event.currentTarget.setPointerCapture(event.pointerId);
88
+ setDragging(true);
89
+ };
90
+ const handlePointerMove = (event) => {
91
+ if (!drag.current ||
92
+ !dock.current ||
93
+ !event.currentTarget.hasPointerCapture(event.pointerId))
94
+ return;
95
+ const next = constrain({ x: event.clientX - drag.current.x, y: event.clientY - drag.current.y }, dock.current);
96
+ setPosition(next);
97
+ };
98
+ const handlePointerUp = (event) => {
99
+ if (!drag.current ||
100
+ !dock.current ||
101
+ !event.currentTarget.hasPointerCapture(event.pointerId))
102
+ return;
103
+ const next = constrain({ x: event.clientX - drag.current.x, y: event.clientY - drag.current.y }, dock.current);
104
+ setPosition(next);
105
+ save(storageKey, JSON.stringify(next));
106
+ };
107
+ const handleLostPointerCapture = () => {
108
+ drag.current = null;
109
+ setDragging(false);
110
+ };
111
+ const handleGripKeyDown = (event) => {
112
+ if (!dock.current ||
113
+ !["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key))
114
+ return;
115
+ event.preventDefault();
116
+ const rect = dock.current.getBoundingClientRect();
117
+ const next = constrain({
118
+ x: rect.left +
119
+ 16 *
120
+ (Number(event.key === "ArrowRight") -
121
+ Number(event.key === "ArrowLeft")),
122
+ y: rect.top +
123
+ 16 *
124
+ (Number(event.key === "ArrowDown") -
125
+ Number(event.key === "ArrowUp")),
126
+ }, dock.current);
127
+ setPosition(next);
128
+ save(storageKey, JSON.stringify(next));
129
+ };
130
+ const handleDismissClick = () => setGuide(false);
131
+ return (_jsxs("div", { ref: dock, className: [
132
+ "miral:group/dock miral:fixed miral:top-3 miral:right-3 miral:w-max miral:max-w-screen miral:text-ink miral:text-sm/[1.5] miral:font-normal miral:transition-[opacity,transform,translate,scale] miral:duration-[160ms] miral:ease-out miral:data-hidden:opacity-0 miral:data-hidden:-translate-y-1 miral:data-hidden:scale-[0.98] miral:data-hidden:pointer-events-none miral:motion-reduce:transition-none",
133
+ className,
134
+ ]
135
+ .filter(Boolean)
136
+ .join(" "), "data-hidden": hidden || undefined, "data-dragging": dragging || undefined, inert: hidden, style: position
137
+ ? { left: position.x, top: position.y, right: "auto" }
138
+ : undefined, children: [_jsxs("div", { className: "preview-toolbar miral:w-max miral:ms-auto miral:flex miral:items-center miral:p-[0.2rem] miral:border miral:border-solid miral:border-edge miral:rounded-[0.65rem] miral:bg-paper miral:text-ink", role: "group", "aria-label": "Preview settings", children: [_jsx("button", { type: "button", className: "miral:cursor-grab miral:touch-none miral:select-none miral:group-data-dragging/dock:cursor-grabbing", title: "Move toolbar \u00B7 Drag or use arrow keys", "aria-label": "Move toolbar", onKeyDown: handleGripKeyDown, onPointerDown: handlePointerDown, onPointerMove: handlePointerMove, onPointerUp: handlePointerUp, onLostPointerCapture: handleLostPointerCapture, children: _jsx(Icon, { name: "grip" }) }), children] }), guide && (_jsxs("aside", { className: "miral:mt-[0.35rem] miral:p-[0.65rem] miral:border miral:border-solid miral:border-edge miral:rounded-[0.65rem] miral:bg-paper miral:max-h-[calc(100dvh-3.5rem)] miral:overflow-auto", "aria-label": "Preview shortcuts", children: [_jsxs("div", { className: "miral:flex miral:gap-[0.35rem] miral:justify-between", children: [_jsx("strong", { className: "miral:font-[650]", children: "Preview shortcuts" }), _jsx("button", { type: "button", className: "miral:cursor-pointer", "aria-label": "Dismiss shortcut guide", onClick: handleDismissClick, children: _jsx(Icon, { name: "close" }) })] }), _jsx("p", { className: "miral:m-0 miral:mt-1", children: "Drag the handle to move the toolbar." }), _jsx("dl", { className: "miral:m-0 miral:mt-2 miral:flex miral:flex-col miral:gap-[0.35rem] miral:justify-between", children: shortcuts.map(([label, keys]) => (_jsxs("div", { className: "miral:flex miral:gap-[0.35rem] miral:justify-between", children: [_jsx("dt", { children: label }), _jsx("dd", { className: "miral:m-0", children: _jsx("kbd", { className: "miral:[font:inherit]", children: keys }) })] }, keys))) })] }))] }));
139
+ };