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/src/Controls.tsx DELETED
@@ -1,147 +0,0 @@
1
- import type React from "react";
2
- import type * as z from "zod/mini";
3
- export type Values = Record<string, string | number | boolean>;
4
-
5
- type ControlsProps = {
6
- schema: z.ZodMiniObject;
7
- values: Values;
8
- onChange: (values: Values) => void;
9
- };
10
-
11
- export const Controls = ({ schema, values, onChange }: ControlsProps) => {
12
- return (
13
- <section
14
- className="miral:border-t miral:border-edge miral:text-sm/[1.5] miral:font-normal miral:group-data-isolated/shell:hidden"
15
- aria-labelledby="preview-props-heading"
16
- >
17
- <h2
18
- id="preview-props-heading"
19
- className="miral:m-0 miral:px-4 miral:py-2 miral:text-[length:inherit] miral:font-semibold"
20
- >
21
- Props
22
- </h2>
23
- <form
24
- className="miral:max-h-[30dvh] miral:overflow-auto miral:px-4 miral:pb-2"
25
- onSubmit={preventSubmit}
26
- >
27
- {Object.entries(schema.shape).map(([name, field]) => {
28
- const definition = field._zod.def;
29
- const optional = definition.type === "optional";
30
-
31
- const inner =
32
- optional && "innerType" in definition
33
- ? (definition.innerType as z.ZodMiniType)._zod.def
34
- : definition;
35
-
36
- let options: string[] = [];
37
-
38
- if ("entries" in inner)
39
- options = Object.values(inner.entries as Record<string, string>);
40
- else if ("values" in inner) options = inner.values as string[];
41
-
42
- const value = values[name];
43
-
44
- const handleChange = (
45
- event: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>,
46
- ) => {
47
- const text = event.currentTarget.value;
48
- const next = { ...values };
49
-
50
- if (
51
- event.currentTarget instanceof HTMLInputElement &&
52
- event.currentTarget.type === "checkbox"
53
- )
54
- next[name] = event.currentTarget.checked;
55
- else if (inner.type === "boolean") next[name] = text === "true";
56
- else if (inner.type === "number") next[name] = Number(text);
57
- else next[name] = text;
58
-
59
- if (text === "" && optional) delete next[name];
60
-
61
- if (schema.safeParse(next).success) onChange(next);
62
- };
63
-
64
- if (inner.type === "boolean" && optional) {
65
- return (
66
- <div
67
- key={name}
68
- className="preview-prop-row"
69
- role="radiogroup"
70
- aria-label={name}
71
- >
72
- <span aria-hidden="true">{name}</span>
73
- <div className="miral:flex miral:w-fit miral:border miral:border-solid miral:border-edge miral:rounded-sm">
74
- {[
75
- ["", "Default"],
76
- ["true", "On"],
77
- ["false", "Off"],
78
- ].map(([choice, label]) => (
79
- <label
80
- key={choice}
81
- 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"
82
- >
83
- <input
84
- className="miral:absolute miral:inset-0 miral:m-0 miral:opacity-0 miral:cursor-pointer"
85
- type="radio"
86
- name={name}
87
- value={choice}
88
- checked={String(value ?? "") === choice}
89
- onChange={handleChange}
90
- />
91
- {label}
92
- </label>
93
- ))}
94
- </div>
95
- </div>
96
- );
97
- }
98
-
99
- if (inner.type === "boolean") {
100
- return (
101
- <label key={name} className="preview-prop-row">
102
- {name}
103
- <input
104
- className="miral:justify-self-start miral:accent-ink"
105
- type="checkbox"
106
- checked={value === true}
107
- onChange={handleChange}
108
- />
109
- </label>
110
- );
111
- }
112
-
113
- if (options.length) {
114
- return (
115
- <label key={name} className="preview-prop-row">
116
- {name}
117
- <select value={String(value ?? "")} onChange={handleChange}>
118
- {optional && <option value="">Default</option>}
119
- {options.map((item) => (
120
- <option key={item}>{item}</option>
121
- ))}
122
- </select>
123
- </label>
124
- );
125
- }
126
-
127
- if (inner.type !== "string" && inner.type !== "number") return null;
128
-
129
- return (
130
- <label key={name} className="preview-prop-row">
131
- {name}
132
- <input
133
- type={inner.type === "number" ? "number" : "text"}
134
- value={String(value ?? "")}
135
- onChange={handleChange}
136
- />
137
- </label>
138
- );
139
- })}
140
- </form>
141
- </section>
142
- );
143
- };
144
-
145
- function preventSubmit(event: React.FormEvent) {
146
- event.preventDefault();
147
- }
package/src/Nav.tsx DELETED
@@ -1,99 +0,0 @@
1
- import type React from "react";
2
-
3
- type NavProps = {
4
- title?: string;
5
- className?: string;
6
- entries: { path: string; name: string; section: string }[];
7
- activePath?: string;
8
- assetsPath?: string;
9
- names: string[];
10
- selected: string;
11
- theme: string;
12
- layout: string;
13
- isolated: boolean;
14
- link: (params: Record<string, string>) => string;
15
- onLinkClick: React.MouseEventHandler<HTMLAnchorElement>;
16
- };
17
-
18
- export const Nav = ({
19
- title,
20
- className,
21
- entries,
22
- activePath,
23
- assetsPath,
24
- names,
25
- selected,
26
- theme,
27
- layout,
28
- isolated,
29
- link,
30
- onLinkClick,
31
- }: NavProps) => (
32
- <nav
33
- className={[
34
- "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]",
35
- className,
36
- ]
37
- .filter(Boolean)
38
- .join(" ")}
39
- inert={isolated}
40
- aria-label="Component catalog"
41
- >
42
- {!!title && (
43
- <h1 className="miral:mx-2 miral:mt-0 miral:mb-[1.625rem] miral:text-2xl/[1.1] miral:font-bold miral:tracking-[-0.045em]">
44
- {title}
45
- </h1>
46
- )}
47
- {Array.from(new Set(entries.map((item) => item.section))).map((section) => (
48
- <section key={section} className="miral:[section+&]:mt-[1.375rem]">
49
- <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">
50
- {section}
51
- </h2>
52
- {entries
53
- .filter((item) => item.section === section)
54
- .map((item) => (
55
- <div key={item.path}>
56
- <a
57
- aria-current={
58
- item.path === activePath && selected === "Preview"
59
- ? "page"
60
- : undefined
61
- }
62
- href={link({ component: item.path, theme, layout })}
63
- onClick={onLinkClick}
64
- >
65
- {item.name}
66
- </a>
67
- {item.path === activePath &&
68
- names.map((name) => (
69
- <a
70
- key={name}
71
- className="miral:ms-2 miral:ps-3.5 miral:rounded-l-none"
72
- onClick={onLinkClick}
73
- aria-current={selected === name ? "page" : undefined}
74
- href={link({
75
- component: item.path,
76
- example: name,
77
- theme,
78
- layout,
79
- })}
80
- >
81
- {name.replace(/([a-z])([A-Z])/g, "$1 $2")}
82
- </a>
83
- ))}
84
- </div>
85
- ))}
86
- </section>
87
- ))}
88
- {!!assetsPath && (
89
- <a
90
- className="miral:mt-4"
91
- href={link({ component: assetsPath, theme, layout })}
92
- aria-current={activePath === assetsPath ? "page" : undefined}
93
- onClick={onLinkClick}
94
- >
95
- Assets
96
- </a>
97
- )}
98
- </nav>
99
- );
package/src/Spinner.tsx DELETED
@@ -1,15 +0,0 @@
1
- type SpinnerProps = { className?: string };
2
-
3
- export const Spinner = ({ className }: SpinnerProps) => (
4
- <span
5
- className={[
6
- "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]",
7
- className,
8
- ]
9
- .filter(Boolean)
10
- .join(" ")}
11
- data-miral-spinner
12
- role="status"
13
- aria-label="Loading"
14
- />
15
- );
package/src/Toolbar.tsx DELETED
@@ -1,271 +0,0 @@
1
- import React from "react";
2
- import { Icon } from "./icons.tsx";
3
-
4
- type Position = { x: number; y: number };
5
-
6
- type ToolbarProps = {
7
- children: React.ReactNode;
8
- hidden: boolean;
9
- isolated: boolean;
10
- className?: string;
11
- };
12
-
13
- const storageKey = "miral.preview:" + location.pathname + ":toolbar";
14
- const guideKey = storageKey + ":guide-seen";
15
-
16
- function read(key: string) {
17
- try {
18
- const value = localStorage.getItem(key);
19
- return value;
20
- } catch {
21
- return null;
22
- }
23
- }
24
-
25
- function save(key: string, value: string) {
26
- try {
27
- localStorage.setItem(key, value);
28
- } catch {
29
- /* Storage can be disabled. */
30
- }
31
- }
32
-
33
- function readPosition(): Position | null {
34
- try {
35
- const value = JSON.parse(read(storageKey) ?? "null");
36
-
37
- if (value && Number.isFinite(value.x) && Number.isFinite(value.y))
38
- return { x: value.x, y: value.y };
39
- } catch {
40
- /* Ignore invalid saved coordinates. */
41
- }
42
-
43
- return null;
44
- }
45
-
46
- function constrain(position: Position, element: HTMLElement) {
47
- const x = Math.max(
48
- 0,
49
- Math.min(
50
- position.x,
51
- document.documentElement.clientWidth - element.offsetWidth - 4,
52
- ),
53
- );
54
-
55
- const y = Math.max(
56
- 0,
57
- Math.min(position.y, window.innerHeight - element.offsetHeight - 4),
58
- );
59
-
60
- return { x, y };
61
- }
62
-
63
- const shortcuts = [
64
- ["Cycle layout", "Shift + L"],
65
- ["Switch theme", "Shift + T"],
66
- ["Toggle full screen", "Shift + F"],
67
- ["Exit full screen", "Esc"],
68
- ["Show or hide toolbar", "Shift + C"],
69
- ];
70
-
71
- export const Toolbar = ({
72
- children,
73
- hidden,
74
- isolated,
75
- className,
76
- }: ToolbarProps) => {
77
- const dock = React.useRef<HTMLDivElement>(null);
78
- const drag = React.useRef<Position | null>(null);
79
- const [position, setPosition] = React.useState(readPosition);
80
- const [guide, setGuide] = React.useState(() => read(guideKey) !== "1");
81
- const [dragging, setDragging] = React.useState(false);
82
-
83
- React.useEffect(() => {
84
- if (guide && !hidden && !isolated) save(guideKey, "1");
85
- }, [guide, hidden, isolated]);
86
-
87
- React.useLayoutEffect(() => {
88
- const element = dock.current;
89
- if (!element) return;
90
-
91
- const fit = () =>
92
- setPosition((current) => {
93
- if (!current) return current;
94
-
95
- const next = constrain(current, element);
96
- if (next.x === current.x && next.y === current.y) return current;
97
- return next;
98
- });
99
-
100
- fit();
101
-
102
- const observer = new ResizeObserver(fit);
103
-
104
- observer.observe(element);
105
- window.addEventListener("resize", fit);
106
-
107
- return () => {
108
- observer.disconnect();
109
- window.removeEventListener("resize", fit);
110
- };
111
- }, []);
112
-
113
- const handlePointerDown = (event: React.PointerEvent<HTMLButtonElement>) => {
114
- if (event.button !== 0 || !event.isPrimary || !dock.current) return;
115
-
116
- const rect = dock.current.getBoundingClientRect();
117
-
118
- drag.current = {
119
- x: event.clientX - rect.left,
120
- y: event.clientY - rect.top,
121
- };
122
-
123
- event.currentTarget.setPointerCapture(event.pointerId);
124
- setDragging(true);
125
- };
126
-
127
- const handlePointerMove = (event: React.PointerEvent<HTMLButtonElement>) => {
128
- if (
129
- !drag.current ||
130
- !dock.current ||
131
- !event.currentTarget.hasPointerCapture(event.pointerId)
132
- )
133
- return;
134
-
135
- const next = constrain(
136
- { x: event.clientX - drag.current.x, y: event.clientY - drag.current.y },
137
- dock.current,
138
- );
139
-
140
- setPosition(next);
141
- };
142
-
143
- const handlePointerUp = (event: React.PointerEvent<HTMLButtonElement>) => {
144
- if (
145
- !drag.current ||
146
- !dock.current ||
147
- !event.currentTarget.hasPointerCapture(event.pointerId)
148
- )
149
- return;
150
-
151
- const next = constrain(
152
- { x: event.clientX - drag.current.x, y: event.clientY - drag.current.y },
153
- dock.current,
154
- );
155
-
156
- setPosition(next);
157
- save(storageKey, JSON.stringify(next));
158
- };
159
-
160
- const handleLostPointerCapture = () => {
161
- drag.current = null;
162
- setDragging(false);
163
- };
164
-
165
- const handleGripKeyDown = (event: React.KeyboardEvent<HTMLButtonElement>) => {
166
- if (
167
- !dock.current ||
168
- !["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key)
169
- )
170
- return;
171
-
172
- event.preventDefault();
173
-
174
- const rect = dock.current.getBoundingClientRect();
175
-
176
- const next = constrain(
177
- {
178
- x:
179
- rect.left +
180
- 16 *
181
- (Number(event.key === "ArrowRight") -
182
- Number(event.key === "ArrowLeft")),
183
- y:
184
- rect.top +
185
- 16 *
186
- (Number(event.key === "ArrowDown") -
187
- Number(event.key === "ArrowUp")),
188
- },
189
- dock.current,
190
- );
191
-
192
- setPosition(next);
193
- save(storageKey, JSON.stringify(next));
194
- };
195
-
196
- const handleDismissClick = () => setGuide(false);
197
- return (
198
- <div
199
- ref={dock}
200
- className={[
201
- "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",
202
- className,
203
- ]
204
- .filter(Boolean)
205
- .join(" ")}
206
- data-hidden={hidden || undefined}
207
- data-dragging={dragging || undefined}
208
- inert={hidden}
209
- style={
210
- position
211
- ? { left: position.x, top: position.y, right: "auto" }
212
- : undefined
213
- }
214
- >
215
- <div
216
- 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"
217
- role="group"
218
- aria-label="Preview settings"
219
- >
220
- <button
221
- type="button"
222
- className="miral:cursor-grab miral:touch-none miral:select-none miral:group-data-dragging/dock:cursor-grabbing"
223
- title="Move toolbar · Drag or use arrow keys"
224
- aria-label="Move toolbar"
225
- onKeyDown={handleGripKeyDown}
226
- onPointerDown={handlePointerDown}
227
- onPointerMove={handlePointerMove}
228
- onPointerUp={handlePointerUp}
229
- onLostPointerCapture={handleLostPointerCapture}
230
- >
231
- <Icon name="grip" />
232
- </button>
233
- {children}
234
- </div>
235
- {guide && (
236
- <aside
237
- 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"
238
- aria-label="Preview shortcuts"
239
- >
240
- <div className="miral:flex miral:gap-[0.35rem] miral:justify-between">
241
- <strong className="miral:font-[650]">Preview shortcuts</strong>
242
- <button
243
- type="button"
244
- className="miral:cursor-pointer"
245
- aria-label="Dismiss shortcut guide"
246
- onClick={handleDismissClick}
247
- >
248
- <Icon name="close" />
249
- </button>
250
- </div>
251
- <p className="miral:m-0 miral:mt-1">
252
- Drag the handle to move the toolbar.
253
- </p>
254
- <dl className="miral:m-0 miral:mt-2 miral:flex miral:flex-col miral:gap-[0.35rem] miral:justify-between">
255
- {shortcuts.map(([label, keys]) => (
256
- <div
257
- key={keys}
258
- className="miral:flex miral:gap-[0.35rem] miral:justify-between"
259
- >
260
- <dt>{label}</dt>
261
- <dd className="miral:m-0">
262
- <kbd className="miral:[font:inherit]">{keys}</kbd>
263
- </dd>
264
- </div>
265
- ))}
266
- </dl>
267
- </aside>
268
- )}
269
- </div>
270
- );
271
- };