@markgrafhq/starlight-markgraf 0.0.42

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mark Eibes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # @markgrafhq/starlight-markgraf
2
+
3
+ Astro/Starlight integration for embedding [markgraf](https://github.com/markgrafhq) animations.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @markgrafhq/starlight-markgraf @markgrafhq/markgraf-react @astrojs/react react react-dom
9
+ ```
10
+
11
+ ## Configure
12
+
13
+ ```js
14
+ // astro.config.mjs
15
+ import { defineConfig } from "astro/config";
16
+ import react from "@astrojs/react";
17
+ import starlight from "@astrojs/starlight";
18
+ import markgraf from "@markgrafhq/starlight-markgraf";
19
+
20
+ export default defineConfig({
21
+ integrations: [react(), starlight({ title: "Docs" }), markgraf()],
22
+ });
23
+ ```
24
+
25
+ Import the player CSS in a global stylesheet:
26
+
27
+ ```css
28
+ @import "@markgrafhq/starlight-markgraf/css";
29
+ ```
30
+
31
+ ## Use
32
+
33
+ Fenced code blocks with the `markgraf` language render as live players:
34
+
35
+ ````markdown
36
+ ```markgraf
37
+ seed 1
38
+ scene v1 {
39
+ + client: Client
40
+ + api: API
41
+ + client -> api
42
+ client ~> api: GET /user/42
43
+ }
44
+ ```
45
+ ````
46
+
47
+ Or use the component directly in MDX/Astro:
48
+
49
+ ```astro
50
+ ---
51
+ import Markgraf from "@markgrafhq/starlight-markgraf/Markgraf.astro";
52
+ ---
53
+
54
+ <Markgraf src={`seed 1\nscene v1 { + a: A }`} />
55
+ ```
56
+
57
+ Players include the floating play, scrub, time, and speed controls by default.
58
+ The Astro component accepts `controls={false}` when a page should hide them.
59
+
60
+ Custom controls belong in a React island so the control function stays in the
61
+ browser rather than crossing Astro's serialization boundary:
62
+
63
+ ```tsx
64
+ import { StarlightMarkgrafPlayer } from "@markgrafhq/starlight-markgraf";
65
+
66
+ export const Diagram = ({ src }: { src: string }) => (
67
+ <StarlightMarkgrafPlayer
68
+ src={src}
69
+ controls={(api) => <MyDiagramControls api={api} />}
70
+ />
71
+ );
72
+ ```
@@ -0,0 +1,16 @@
1
+ import { type ReactNode } from "react";
2
+ import { type MarkgrafApi } from "@markgrafhq/markgraf-react";
3
+ type Renderer = "canvas" | "svg";
4
+ export type MarkgrafControls = boolean | ((api: MarkgrafApi<Element>) => ReactNode);
5
+ export interface MarkgrafPlayerProps {
6
+ src: string;
7
+ renderer?: Renderer;
8
+ className?: string;
9
+ /**
10
+ * Shows the transport bar by default. Pass `false` to hide it, or a render
11
+ * function to replace it with controls driven by the same player API.
12
+ */
13
+ controls?: MarkgrafControls;
14
+ }
15
+ export declare const StarlightMarkgrafPlayer: ({ src, renderer, className, controls, }: MarkgrafPlayerProps) => import("react").JSX.Element;
16
+ export {};
@@ -0,0 +1,61 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useEffect, useState } from "react";
3
+ import { useMarkgraf } from "@markgrafhq/markgraf-react";
4
+ const playIcon = (_jsx("svg", { viewBox: "0 0 15 15", "aria-hidden": "true", focusable: "false", children: _jsx("path", { d: "M3.24 2.05a.5.5 0 0 1 .52.02l8.5 5a.5.5 0 0 1 0 .86l-8.5 5A.5.5 0 0 1 3 12.5v-10a.5.5 0 0 1 .24-.45Z" }) }));
5
+ const pauseIcon = (_jsx("svg", { viewBox: "0 0 15 15", "aria-hidden": "true", focusable: "false", children: _jsx("path", { d: "M3.5 2.5a.5.5 0 0 1 .5-.5h2a.5.5 0 0 1 .5.5v10a.5.5 0 0 1-.5.5H4a.5.5 0 0 1-.5-.5v-10Zm5 0A.5.5 0 0 1 9 2h2a.5.5 0 0 1 .5.5v10a.5.5 0 0 1-.5.5H9a.5.5 0 0 1-.5-.5v-10Z" }) }));
6
+ const speedIcon = (_jsx("svg", { className: "markgraf-doc-speed-icon", viewBox: "0 0 15 15", "aria-hidden": "true", focusable: "false", children: _jsx("path", { d: "M3.14 5.64a.5.5 0 0 1 .71 0L7.5 9.29l3.65-3.65a.5.5 0 0 1 .7.71l-4 4a.5.5 0 0 1-.7 0l-4-4a.5.5 0 0 1 0-.71Z" }) }));
7
+ const useDocumentTheme = () => {
8
+ const [theme, setTheme] = useState("light");
9
+ useEffect(() => {
10
+ const root = document.documentElement;
11
+ const update = () => setTheme(root.dataset.theme === "dark" ? "dark" : "light");
12
+ update();
13
+ const observer = new MutationObserver(update);
14
+ observer.observe(root, {
15
+ attributes: true,
16
+ attributeFilter: ["data-theme"],
17
+ });
18
+ return () => observer.disconnect();
19
+ }, []);
20
+ return theme;
21
+ };
22
+ const formatTime = (seconds) => {
23
+ const safe = Number.isFinite(seconds) ? Math.max(0, seconds) : 0;
24
+ const minutes = Math.floor(safe / 60);
25
+ const remainder = safe - minutes * 60;
26
+ return `${minutes}:${remainder.toFixed(1).padStart(4, "0")}`;
27
+ };
28
+ const DefaultControls = ({ api, speed, setSpeed, }) => {
29
+ const duration = Number.isFinite(api.duration)
30
+ ? Math.max(0, api.duration)
31
+ : 0;
32
+ const time = Number.isFinite(api.time)
33
+ ? Math.max(0, Math.min(api.time, duration))
34
+ : 0;
35
+ return (_jsxs("div", { className: "markgraf-doc-controls", "aria-label": "Diagram playback controls", children: [_jsx("button", { className: "markgraf-doc-play", type: "button", "aria-label": api.playing ? "Pause diagram" : "Play diagram", "aria-pressed": api.playing, disabled: !api.ready, onClick: api.toggle, children: api.playing ? pauseIcon : playIcon }), _jsx("input", { className: "markgraf-doc-scrub", type: "range", "aria-label": "Diagram time", min: "0", max: Math.max(duration, 0.001), step: "0.01", value: time, disabled: !api.ready, onChange: (event) => api.seek(event.currentTarget.valueAsNumber) }), _jsxs("output", { className: "markgraf-doc-time", "aria-live": "off", children: [formatTime(time), " / ", formatTime(duration)] }), _jsxs("div", { className: "markgraf-doc-speed-control", children: [_jsxs("select", { className: "markgraf-doc-speed", "aria-label": "Playback speed", value: speed, disabled: !api.ready, onChange: (event) => setSpeed(Number(event.currentTarget.value)), children: [_jsx("option", { value: "0.5", children: "0.5\u00D7" }), _jsx("option", { value: "1", children: "1\u00D7" }), _jsx("option", { value: "1.5", children: "1.5\u00D7" }), _jsx("option", { value: "2", children: "2\u00D7" })] }), speedIcon] })] }));
36
+ };
37
+ const Controls = ({ api, controls, }) => {
38
+ const [speed, setSpeed] = useState(1);
39
+ useEffect(() => {
40
+ if (controls === true && api.ready)
41
+ api.setSpeed(speed);
42
+ }, [api.ready, controls, speed]);
43
+ if (typeof controls === "function")
44
+ return controls(api);
45
+ if (!controls)
46
+ return null;
47
+ return _jsx(DefaultControls, { api: api, speed: speed, setSpeed: setSpeed });
48
+ };
49
+ const CanvasPlayer = ({ src, theme, className, controls, }) => {
50
+ const api = useMarkgraf(src, { renderer: "canvas", theme });
51
+ return (_jsxs("div", { className: `not-content markgraf-doc-player ${className ?? ""}`, "data-markgraf-theme": theme, children: [_jsx("canvas", { className: "markgraf-player markgraf-doc-stage", ref: api.elementRef, onClick: api.toggle, "aria-label": "Animated Markgraf diagram" }), _jsx(Controls, { api: api, controls: controls })] }));
52
+ };
53
+ const SvgPlayer = ({ src, theme, className, controls, }) => {
54
+ const api = useMarkgraf(src, { renderer: "svg", theme });
55
+ return (_jsxs("div", { className: `not-content markgraf-doc-player ${className ?? ""}`, "data-markgraf-theme": theme, children: [_jsx("svg", { className: "markgraf-player markgraf-doc-stage", ref: api.elementRef, onClick: api.toggle, "aria-label": "Animated Markgraf diagram" }), _jsx(Controls, { api: api, controls: controls })] }));
56
+ };
57
+ export const StarlightMarkgrafPlayer = ({ src, renderer = "canvas", className, controls = true, }) => {
58
+ const theme = useDocumentTheme();
59
+ const props = { src, renderer, className, controls, theme };
60
+ return renderer === "svg" ? (_jsx(SvgPlayer, { ...props })) : (_jsx(CanvasPlayer, { ...props }));
61
+ };
@@ -0,0 +1,5 @@
1
+ import type { AstroIntegration } from "astro";
2
+ import remarkMarkgraf from "./remark-markgraf.mjs";
3
+ export { remarkMarkgraf };
4
+ export { StarlightMarkgrafPlayer, type MarkgrafControls, type MarkgrafPlayerProps, } from "./MarkgrafPlayer.js";
5
+ export default function markgrafIntegration(): AstroIntegration;
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ import remarkMarkgraf from "./remark-markgraf.mjs";
2
+ export { remarkMarkgraf };
3
+ export { StarlightMarkgrafPlayer, } from "./MarkgrafPlayer.js";
4
+ export default function markgrafIntegration() {
5
+ return {
6
+ name: "markgraf-starlight",
7
+ hooks: {
8
+ "astro:config:setup": ({ updateConfig }) => {
9
+ updateConfig({
10
+ markdown: {
11
+ remarkPlugins: [remarkMarkgraf],
12
+ },
13
+ });
14
+ },
15
+ },
16
+ };
17
+ }
@@ -0,0 +1,154 @@
1
+ @import "@markgrafhq/markgraf-react/css";
2
+
3
+ .markgraf-doc-player {
4
+ position: relative;
5
+ display: flex;
6
+ flex-direction: column;
7
+ width: 100%;
8
+ min-width: 0;
9
+ min-height: 11.25rem;
10
+ aspect-ratio: 16 / 9;
11
+ overflow: hidden;
12
+ border: 1px solid color-mix(in srgb, currentColor 14%, transparent);
13
+ border-radius: 0.875rem;
14
+ background: #ffffff;
15
+ box-shadow: 0 1rem 3rem rgb(0 0 0 / 10%);
16
+ }
17
+
18
+ .markgraf-doc-player[data-markgraf-theme="dark"] {
19
+ background: #101827;
20
+ }
21
+
22
+ .markgraf-doc-stage {
23
+ display: block;
24
+ width: 100%;
25
+ height: 100%;
26
+ min-height: 11.25rem;
27
+ cursor: pointer;
28
+ }
29
+
30
+ .markgraf-doc-controls {
31
+ position: absolute;
32
+ z-index: 2;
33
+ right: 50%;
34
+ bottom: 0.75rem;
35
+ display: grid;
36
+ grid-template-columns: 1.75rem minmax(7rem, 1fr) auto auto;
37
+ align-items: center;
38
+ gap: 0.5rem;
39
+ width: min(35rem, calc(100% - 2rem));
40
+ padding: 0.375rem 0.5rem;
41
+ border: 1px solid rgb(255 255 255 / 14%);
42
+ border-radius: 999px;
43
+ color: #ffffff;
44
+ background: rgb(8 13 22 / 58%);
45
+ box-shadow: 0 0.875rem 2.75rem rgb(0 0 0 / 28%);
46
+ opacity: 0.78;
47
+ transform: translateX(50%);
48
+ transition:
49
+ opacity 140ms ease,
50
+ background 140ms ease;
51
+ backdrop-filter: blur(18px);
52
+ }
53
+
54
+ .markgraf-doc-player:hover .markgraf-doc-controls,
55
+ .markgraf-doc-controls:focus-within {
56
+ background: rgb(8 13 22 / 78%);
57
+ opacity: 0.98;
58
+ }
59
+
60
+ .markgraf-doc-play {
61
+ display: grid;
62
+ width: 1.75rem;
63
+ height: 1.75rem;
64
+ padding: 0;
65
+ border: 0;
66
+ border-radius: 999px;
67
+ place-items: center;
68
+ color: inherit;
69
+ background: rgb(255 255 255 / 10%);
70
+ cursor: pointer;
71
+ }
72
+
73
+ .markgraf-doc-play:hover,
74
+ .markgraf-doc-play:focus-visible {
75
+ background: rgb(255 255 255 / 20%);
76
+ }
77
+
78
+ .markgraf-doc-play svg {
79
+ width: 0.875rem;
80
+ height: 0.875rem;
81
+ }
82
+
83
+ .markgraf-doc-scrub {
84
+ min-width: 0;
85
+ height: 0.875rem;
86
+ margin: 0;
87
+ accent-color: #ffffff;
88
+ cursor: pointer;
89
+ }
90
+
91
+ .markgraf-doc-time {
92
+ color: rgb(255 255 255 / 76%);
93
+ font-family: var(
94
+ --sl-font-mono,
95
+ var(--ifm-font-family-monospace, ui-monospace, monospace)
96
+ );
97
+ font-size: 0.625rem;
98
+ line-height: 1;
99
+ font-variant-numeric: tabular-nums;
100
+ white-space: nowrap;
101
+ }
102
+
103
+ .markgraf-doc-speed-control {
104
+ position: relative;
105
+ display: flex;
106
+ align-items: center;
107
+ }
108
+
109
+ .markgraf-doc-speed {
110
+ appearance: none;
111
+ width: 100%;
112
+ padding: 0.1875rem 1.375rem 0.1875rem 0.5rem;
113
+ border: 0;
114
+ border-radius: 999px;
115
+ color: #ffffff;
116
+ background: rgb(255 255 255 / 10%);
117
+ font-size: 0.625rem;
118
+ cursor: pointer;
119
+ }
120
+
121
+ .markgraf-doc-speed-icon {
122
+ position: absolute;
123
+ right: 0.4375rem;
124
+ width: 0.75rem;
125
+ height: 0.75rem;
126
+ color: rgb(255 255 255 / 72%);
127
+ pointer-events: none;
128
+ }
129
+
130
+ .markgraf-doc-speed option {
131
+ color: #111827;
132
+ background: #ffffff;
133
+ }
134
+
135
+ .markgraf-doc-controls :disabled {
136
+ cursor: wait;
137
+ opacity: 0.5;
138
+ }
139
+
140
+ @media (max-width: 32rem) {
141
+ .markgraf-doc-controls {
142
+ grid-template-columns: 1.75rem minmax(5rem, 1fr) auto;
143
+ }
144
+
145
+ .markgraf-doc-time {
146
+ display: none;
147
+ }
148
+ }
149
+
150
+ @media (prefers-reduced-motion: reduce) {
151
+ .markgraf-doc-controls {
152
+ transition: none;
153
+ }
154
+ }
@@ -0,0 +1,51 @@
1
+ import { visit } from "unist-util-visit";
2
+
3
+ const importNode = {
4
+ type: "mdxjsEsm",
5
+ value: 'import Markgraf from "@markgrafhq/starlight-markgraf/Markgraf.astro";',
6
+ data: {
7
+ estree: {
8
+ type: "Program",
9
+ sourceType: "module",
10
+ body: [
11
+ {
12
+ type: "ImportDeclaration",
13
+ specifiers: [
14
+ {
15
+ type: "ImportDefaultSpecifier",
16
+ local: { type: "Identifier", name: "Markgraf" },
17
+ },
18
+ ],
19
+ source: {
20
+ type: "Literal",
21
+ value: "@markgrafhq/starlight-markgraf/Markgraf.astro",
22
+ raw: '"@markgrafhq/starlight-markgraf/Markgraf.astro"',
23
+ },
24
+ },
25
+ ],
26
+ },
27
+ },
28
+ };
29
+
30
+ export default function remarkMarkgraf() {
31
+ return (tree) => {
32
+ let needsImport = false;
33
+
34
+ visit(tree, "code", (node, index, parent) => {
35
+ if (node.lang !== "markgraf" || !parent || index == null) return;
36
+
37
+ needsImport = true;
38
+
39
+ parent.children[index] = {
40
+ type: "mdxJsxFlowElement",
41
+ name: "Markgraf",
42
+ attributes: [
43
+ { type: "mdxJsxAttribute", name: "src", value: node.value },
44
+ ],
45
+ children: [],
46
+ };
47
+ });
48
+
49
+ if (needsImport) tree.children.unshift(importNode);
50
+ };
51
+ }
@@ -0,0 +1,51 @@
1
+ import { visit } from "unist-util-visit";
2
+
3
+ const importNode = {
4
+ type: "mdxjsEsm",
5
+ value: 'import Markgraf from "@markgrafhq/starlight-markgraf/Markgraf.astro";',
6
+ data: {
7
+ estree: {
8
+ type: "Program",
9
+ sourceType: "module",
10
+ body: [
11
+ {
12
+ type: "ImportDeclaration",
13
+ specifiers: [
14
+ {
15
+ type: "ImportDefaultSpecifier",
16
+ local: { type: "Identifier", name: "Markgraf" },
17
+ },
18
+ ],
19
+ source: {
20
+ type: "Literal",
21
+ value: "@markgrafhq/starlight-markgraf/Markgraf.astro",
22
+ raw: '"@markgrafhq/starlight-markgraf/Markgraf.astro"',
23
+ },
24
+ },
25
+ ],
26
+ },
27
+ },
28
+ };
29
+
30
+ export default function remarkMarkgraf() {
31
+ return (tree) => {
32
+ let needsImport = false;
33
+
34
+ visit(tree, "code", (node, index, parent) => {
35
+ if (node.lang !== "markgraf" || !parent || index == null) return;
36
+
37
+ needsImport = true;
38
+
39
+ parent.children[index] = {
40
+ type: "mdxJsxFlowElement",
41
+ name: "Markgraf",
42
+ attributes: [
43
+ { type: "mdxJsxAttribute", name: "src", value: node.value },
44
+ ],
45
+ children: [],
46
+ };
47
+ });
48
+
49
+ if (needsImport) tree.children.unshift(importNode);
50
+ };
51
+ }
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@markgrafhq/starlight-markgraf",
3
+ "version": "0.0.42",
4
+ "description": "Starlight/Astro integration for embedding markgraf animations. Transforms ```markgraf fenced code blocks into a live player and exposes a <Markgraf src=\"…\" /> component.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ },
14
+ "./Markgraf.astro": "./src/Markgraf.astro",
15
+ "./remark": {
16
+ "default": "./dist/remark-markgraf.js"
17
+ },
18
+ "./css": "./dist/markgraf-react.css"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "src/Markgraf.astro",
23
+ "src/MarkgrafPlayer.tsx",
24
+ "src/player.css",
25
+ "README.md",
26
+ "LICENSE"
27
+ ],
28
+ "sideEffects": [
29
+ "./dist/markgraf-react.css"
30
+ ],
31
+ "scripts": {
32
+ "build": "tsc -p tsconfig.json && cp src/remark-markgraf.mjs dist/remark-markgraf.mjs && cp src/remark-markgraf.mjs dist/remark-markgraf.js && cp src/player.css dist/markgraf-react.css"
33
+ },
34
+ "peerDependencies": {
35
+ "astro": ">=4",
36
+ "@astrojs/react": ">=3",
37
+ "react": ">=18",
38
+ "react-dom": ">=18"
39
+ },
40
+ "dependencies": {
41
+ "@markgrafhq/markgraf-react": "0.0.42",
42
+ "unist-util-visit": "^5"
43
+ },
44
+ "devDependencies": {
45
+ "@types/react": "^19.0.0",
46
+ "typescript": "^5"
47
+ },
48
+ "keywords": [
49
+ "markgraf",
50
+ "starlight",
51
+ "astro",
52
+ "astro-integration",
53
+ "remark",
54
+ "animation",
55
+ "diagram"
56
+ ],
57
+ "license": "MIT",
58
+ "author": "Mark Eibes <mark.eibes@gmail.com>",
59
+ "homepage": "https://github.com/markgrafhq/starlight-markgraf#readme",
60
+ "bugs": "https://github.com/markgrafhq/starlight-markgraf/issues",
61
+ "repository": {
62
+ "type": "git",
63
+ "url": "git+https://github.com/markgrafhq/starlight-markgraf.git"
64
+ }
65
+ }
@@ -0,0 +1,21 @@
1
+ ---
2
+ import { StarlightMarkgrafPlayer } from "./MarkgrafPlayer";
3
+ import "./player.css";
4
+
5
+ interface Props {
6
+ src: string;
7
+ renderer?: "canvas" | "svg";
8
+ class?: string;
9
+ controls?: boolean;
10
+ }
11
+
12
+ const { src, renderer = "canvas", class: className, controls = true } = Astro.props;
13
+ ---
14
+
15
+ <StarlightMarkgrafPlayer
16
+ client:visible
17
+ src={src}
18
+ renderer={renderer}
19
+ className={className}
20
+ controls={controls}
21
+ />
@@ -0,0 +1,217 @@
1
+ import { type ReactNode, useEffect, useState } from "react";
2
+ import { useMarkgraf, type MarkgrafApi } from "@markgrafhq/markgraf-react";
3
+
4
+ type Renderer = "canvas" | "svg";
5
+ type Theme = "light" | "dark";
6
+
7
+ export type MarkgrafControls =
8
+ | boolean
9
+ | ((api: MarkgrafApi<Element>) => ReactNode);
10
+ const playIcon = (
11
+ <svg viewBox="0 0 15 15" aria-hidden="true" focusable="false">
12
+ <path d="M3.24 2.05a.5.5 0 0 1 .52.02l8.5 5a.5.5 0 0 1 0 .86l-8.5 5A.5.5 0 0 1 3 12.5v-10a.5.5 0 0 1 .24-.45Z" />
13
+ </svg>
14
+ );
15
+
16
+ const pauseIcon = (
17
+ <svg viewBox="0 0 15 15" aria-hidden="true" focusable="false">
18
+ <path d="M3.5 2.5a.5.5 0 0 1 .5-.5h2a.5.5 0 0 1 .5.5v10a.5.5 0 0 1-.5.5H4a.5.5 0 0 1-.5-.5v-10Zm5 0A.5.5 0 0 1 9 2h2a.5.5 0 0 1 .5.5v10a.5.5 0 0 1-.5.5H9a.5.5 0 0 1-.5-.5v-10Z" />
19
+ </svg>
20
+ );
21
+
22
+ const speedIcon = (
23
+ <svg
24
+ className="markgraf-doc-speed-icon"
25
+ viewBox="0 0 15 15"
26
+ aria-hidden="true"
27
+ focusable="false"
28
+ >
29
+ <path d="M3.14 5.64a.5.5 0 0 1 .71 0L7.5 9.29l3.65-3.65a.5.5 0 0 1 .7.71l-4 4a.5.5 0 0 1-.7 0l-4-4a.5.5 0 0 1 0-.71Z" />
30
+ </svg>
31
+ );
32
+
33
+ export interface MarkgrafPlayerProps {
34
+ src: string;
35
+ renderer?: Renderer;
36
+ className?: string;
37
+ /**
38
+ * Shows the transport bar by default. Pass `false` to hide it, or a render
39
+ * function to replace it with controls driven by the same player API.
40
+ */
41
+ controls?: MarkgrafControls;
42
+ }
43
+
44
+ const useDocumentTheme = (): Theme => {
45
+ const [theme, setTheme] = useState<Theme>("light");
46
+
47
+ useEffect(() => {
48
+ const root = document.documentElement;
49
+ const update = () =>
50
+ setTheme(root.dataset.theme === "dark" ? "dark" : "light");
51
+ update();
52
+
53
+ const observer = new MutationObserver(update);
54
+ observer.observe(root, {
55
+ attributes: true,
56
+ attributeFilter: ["data-theme"],
57
+ });
58
+ return () => observer.disconnect();
59
+ }, []);
60
+
61
+ return theme;
62
+ };
63
+
64
+ const formatTime = (seconds: number): string => {
65
+ const safe = Number.isFinite(seconds) ? Math.max(0, seconds) : 0;
66
+ const minutes = Math.floor(safe / 60);
67
+ const remainder = safe - minutes * 60;
68
+ return `${minutes}:${remainder.toFixed(1).padStart(4, "0")}`;
69
+ };
70
+
71
+ const DefaultControls = ({
72
+ api,
73
+ speed,
74
+ setSpeed,
75
+ }: {
76
+ api: MarkgrafApi<Element>;
77
+ speed: number;
78
+ setSpeed: (speed: number) => void;
79
+ }) => {
80
+ const duration = Number.isFinite(api.duration)
81
+ ? Math.max(0, api.duration)
82
+ : 0;
83
+ const time = Number.isFinite(api.time)
84
+ ? Math.max(0, Math.min(api.time, duration))
85
+ : 0;
86
+
87
+ return (
88
+ <div
89
+ className="markgraf-doc-controls"
90
+ aria-label="Diagram playback controls"
91
+ >
92
+ <button
93
+ className="markgraf-doc-play"
94
+ type="button"
95
+ aria-label={api.playing ? "Pause diagram" : "Play diagram"}
96
+ aria-pressed={api.playing}
97
+ disabled={!api.ready}
98
+ onClick={api.toggle}
99
+ >
100
+ {api.playing ? pauseIcon : playIcon}
101
+ </button>
102
+
103
+ <input
104
+ className="markgraf-doc-scrub"
105
+ type="range"
106
+ aria-label="Diagram time"
107
+ min="0"
108
+ max={Math.max(duration, 0.001)}
109
+ step="0.01"
110
+ value={time}
111
+ disabled={!api.ready}
112
+ onChange={(event) => api.seek(event.currentTarget.valueAsNumber)}
113
+ />
114
+
115
+ <output className="markgraf-doc-time" aria-live="off">
116
+ {formatTime(time)} / {formatTime(duration)}
117
+ </output>
118
+
119
+ <div className="markgraf-doc-speed-control">
120
+ <select
121
+ className="markgraf-doc-speed"
122
+ aria-label="Playback speed"
123
+ value={speed}
124
+ disabled={!api.ready}
125
+ onChange={(event) => setSpeed(Number(event.currentTarget.value))}
126
+ >
127
+ <option value="0.5">0.5×</option>
128
+ <option value="1">1×</option>
129
+ <option value="1.5">1.5×</option>
130
+ <option value="2">2×</option>
131
+ </select>
132
+ {speedIcon}
133
+ </div>
134
+ </div>
135
+ );
136
+ };
137
+
138
+ const Controls = ({
139
+ api,
140
+ controls,
141
+ }: {
142
+ api: MarkgrafApi<Element>;
143
+ controls: MarkgrafControls;
144
+ }) => {
145
+ const [speed, setSpeed] = useState(1);
146
+
147
+ useEffect(() => {
148
+ if (controls === true && api.ready) api.setSpeed(speed);
149
+ }, [api.ready, controls, speed]);
150
+
151
+ if (typeof controls === "function") return controls(api);
152
+ if (!controls) return null;
153
+ return <DefaultControls api={api} speed={speed} setSpeed={setSpeed} />;
154
+ };
155
+
156
+ const CanvasPlayer = ({
157
+ src,
158
+ theme,
159
+ className,
160
+ controls,
161
+ }: MarkgrafPlayerProps & { theme: Theme; controls: MarkgrafControls }) => {
162
+ const api = useMarkgraf(src, { renderer: "canvas", theme });
163
+
164
+ return (
165
+ <div
166
+ className={`not-content markgraf-doc-player ${className ?? ""}`}
167
+ data-markgraf-theme={theme}
168
+ >
169
+ <canvas
170
+ className="markgraf-player markgraf-doc-stage"
171
+ ref={api.elementRef}
172
+ onClick={api.toggle}
173
+ aria-label="Animated Markgraf diagram"
174
+ />
175
+ <Controls api={api} controls={controls} />
176
+ </div>
177
+ );
178
+ };
179
+
180
+ const SvgPlayer = ({
181
+ src,
182
+ theme,
183
+ className,
184
+ controls,
185
+ }: MarkgrafPlayerProps & { theme: Theme; controls: MarkgrafControls }) => {
186
+ const api = useMarkgraf(src, { renderer: "svg", theme });
187
+
188
+ return (
189
+ <div
190
+ className={`not-content markgraf-doc-player ${className ?? ""}`}
191
+ data-markgraf-theme={theme}
192
+ >
193
+ <svg
194
+ className="markgraf-player markgraf-doc-stage"
195
+ ref={api.elementRef}
196
+ onClick={api.toggle}
197
+ aria-label="Animated Markgraf diagram"
198
+ />
199
+ <Controls api={api} controls={controls} />
200
+ </div>
201
+ );
202
+ };
203
+
204
+ export const StarlightMarkgrafPlayer = ({
205
+ src,
206
+ renderer = "canvas",
207
+ className,
208
+ controls = true,
209
+ }: MarkgrafPlayerProps) => {
210
+ const theme = useDocumentTheme();
211
+ const props = { src, renderer, className, controls, theme };
212
+ return renderer === "svg" ? (
213
+ <SvgPlayer {...props} />
214
+ ) : (
215
+ <CanvasPlayer {...props} />
216
+ );
217
+ };
package/src/player.css ADDED
@@ -0,0 +1,154 @@
1
+ @import "@markgrafhq/markgraf-react/css";
2
+
3
+ .markgraf-doc-player {
4
+ position: relative;
5
+ display: flex;
6
+ flex-direction: column;
7
+ width: 100%;
8
+ min-width: 0;
9
+ min-height: 11.25rem;
10
+ aspect-ratio: 16 / 9;
11
+ overflow: hidden;
12
+ border: 1px solid color-mix(in srgb, currentColor 14%, transparent);
13
+ border-radius: 0.875rem;
14
+ background: #ffffff;
15
+ box-shadow: 0 1rem 3rem rgb(0 0 0 / 10%);
16
+ }
17
+
18
+ .markgraf-doc-player[data-markgraf-theme="dark"] {
19
+ background: #101827;
20
+ }
21
+
22
+ .markgraf-doc-stage {
23
+ display: block;
24
+ width: 100%;
25
+ height: 100%;
26
+ min-height: 11.25rem;
27
+ cursor: pointer;
28
+ }
29
+
30
+ .markgraf-doc-controls {
31
+ position: absolute;
32
+ z-index: 2;
33
+ right: 50%;
34
+ bottom: 0.75rem;
35
+ display: grid;
36
+ grid-template-columns: 1.75rem minmax(7rem, 1fr) auto auto;
37
+ align-items: center;
38
+ gap: 0.5rem;
39
+ width: min(35rem, calc(100% - 2rem));
40
+ padding: 0.375rem 0.5rem;
41
+ border: 1px solid rgb(255 255 255 / 14%);
42
+ border-radius: 999px;
43
+ color: #ffffff;
44
+ background: rgb(8 13 22 / 58%);
45
+ box-shadow: 0 0.875rem 2.75rem rgb(0 0 0 / 28%);
46
+ opacity: 0.78;
47
+ transform: translateX(50%);
48
+ transition:
49
+ opacity 140ms ease,
50
+ background 140ms ease;
51
+ backdrop-filter: blur(18px);
52
+ }
53
+
54
+ .markgraf-doc-player:hover .markgraf-doc-controls,
55
+ .markgraf-doc-controls:focus-within {
56
+ background: rgb(8 13 22 / 78%);
57
+ opacity: 0.98;
58
+ }
59
+
60
+ .markgraf-doc-play {
61
+ display: grid;
62
+ width: 1.75rem;
63
+ height: 1.75rem;
64
+ padding: 0;
65
+ border: 0;
66
+ border-radius: 999px;
67
+ place-items: center;
68
+ color: inherit;
69
+ background: rgb(255 255 255 / 10%);
70
+ cursor: pointer;
71
+ }
72
+
73
+ .markgraf-doc-play:hover,
74
+ .markgraf-doc-play:focus-visible {
75
+ background: rgb(255 255 255 / 20%);
76
+ }
77
+
78
+ .markgraf-doc-play svg {
79
+ width: 0.875rem;
80
+ height: 0.875rem;
81
+ }
82
+
83
+ .markgraf-doc-scrub {
84
+ min-width: 0;
85
+ height: 0.875rem;
86
+ margin: 0;
87
+ accent-color: #ffffff;
88
+ cursor: pointer;
89
+ }
90
+
91
+ .markgraf-doc-time {
92
+ color: rgb(255 255 255 / 76%);
93
+ font-family: var(
94
+ --sl-font-mono,
95
+ var(--ifm-font-family-monospace, ui-monospace, monospace)
96
+ );
97
+ font-size: 0.625rem;
98
+ line-height: 1;
99
+ font-variant-numeric: tabular-nums;
100
+ white-space: nowrap;
101
+ }
102
+
103
+ .markgraf-doc-speed-control {
104
+ position: relative;
105
+ display: flex;
106
+ align-items: center;
107
+ }
108
+
109
+ .markgraf-doc-speed {
110
+ appearance: none;
111
+ width: 100%;
112
+ padding: 0.1875rem 1.375rem 0.1875rem 0.5rem;
113
+ border: 0;
114
+ border-radius: 999px;
115
+ color: #ffffff;
116
+ background: rgb(255 255 255 / 10%);
117
+ font-size: 0.625rem;
118
+ cursor: pointer;
119
+ }
120
+
121
+ .markgraf-doc-speed-icon {
122
+ position: absolute;
123
+ right: 0.4375rem;
124
+ width: 0.75rem;
125
+ height: 0.75rem;
126
+ color: rgb(255 255 255 / 72%);
127
+ pointer-events: none;
128
+ }
129
+
130
+ .markgraf-doc-speed option {
131
+ color: #111827;
132
+ background: #ffffff;
133
+ }
134
+
135
+ .markgraf-doc-controls :disabled {
136
+ cursor: wait;
137
+ opacity: 0.5;
138
+ }
139
+
140
+ @media (max-width: 32rem) {
141
+ .markgraf-doc-controls {
142
+ grid-template-columns: 1.75rem minmax(5rem, 1fr) auto;
143
+ }
144
+
145
+ .markgraf-doc-time {
146
+ display: none;
147
+ }
148
+ }
149
+
150
+ @media (prefers-reduced-motion: reduce) {
151
+ .markgraf-doc-controls {
152
+ transition: none;
153
+ }
154
+ }