@senseihq/emaki-react 0.1.0 → 0.1.2
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 +3 -3
- package/dist/components.d.ts +15 -3
- package/dist/components.js +35 -27
- package/dist/hooks.d.ts +3 -1
- package/dist/hooks.js +10 -4
- package/package.json +2 -2
- package/src/components.tsx +64 -20
- package/src/hooks.ts +11 -5
package/README.md
CHANGED
|
@@ -21,12 +21,12 @@ Call `setupMotion({ source, site, logos })` once on the client first (`emaki ini
|
|
|
21
21
|
| Export | What |
|
|
22
22
|
|---|---|
|
|
23
23
|
| `MotionScene` | `film`, `scene`, `layout` (`figure`), `locale`, `mode` (`auto`), `autoplay` |
|
|
24
|
-
| `MotionShow` | `film`, `locale`, `layout`, `mode` (`auto`), `controls`, `slide`, `loop`, `onReady(show)
|
|
24
|
+
| `MotionShow` | `film`, `locale`, `layout`, `mode` (`auto`), `poster` (cover frame first; `title`, `length` on it), `cover` (an image URL shown with the play button; the show is created on the first click, sound on), `fullscreenHref`, `controls`, `slide`, `loop`, `onReady(show)`; the controls show a progress tick per scene |
|
|
25
25
|
| `ScenePlayer` | `player`, `locale`, `mode` (`auto`), `controls` |
|
|
26
|
-
| `ShowPage` | `film`, `layout`, `locale`, `mode` (capture: `light`), `capture` |
|
|
26
|
+
| `ShowPage` | `film`, `layout`, `locale`, `mode` (capture: `light`), `src` (`json`/`js`, forces the scene source), `capture` |
|
|
27
27
|
| `useScenePlayer(player)` | re-renders on every change of a `ScenePlayerState` (useSyncExternalStore) |
|
|
28
28
|
| `usePlayerFor(ref)` | a player you own, pointed at `{ film, scene, layout }` |
|
|
29
|
-
| `useShowState(show)` | `{ index, playing, sound, layout }` of a player controller |
|
|
29
|
+
| `useShowState(show)` | `{ index, local, playing, sound, layout, started }` of a player controller (the playhead followed once a frame) |
|
|
30
30
|
|
|
31
31
|
`mode` is `light`, `dark` or `auto`. Auto follows the page's `data-mode` (as rokkit's `themable` sets it), else the
|
|
32
32
|
OS, and the scenes redraw when it changes. A film draws dark only if it has a dark look (see @senseihq/emaki).
|
package/dist/components.d.ts
CHANGED
|
@@ -30,20 +30,32 @@ export interface MotionShowProps {
|
|
|
30
30
|
controls?: boolean;
|
|
31
31
|
slide?: number;
|
|
32
32
|
loop?: boolean;
|
|
33
|
+
/** Open on the film's cover frame with a play button; the first click plays it from the top with sound on. */
|
|
34
|
+
poster?: boolean;
|
|
35
|
+
/** An image (URL) to show with the play button; the show is created on the first click (then plays, sound on). */
|
|
36
|
+
cover?: string;
|
|
37
|
+
/** The film's name and running time, for the play button's label and chip. */
|
|
38
|
+
title?: string;
|
|
39
|
+
length?: string;
|
|
40
|
+
/** Where the full-screen link goes (e.g. /show/<film>); no link when empty. */
|
|
41
|
+
fullscreenHref?: string;
|
|
33
42
|
/** The controller once ready (next/prev/play/pause/setSound/setLayout/seek/on), for custom chrome. */
|
|
34
43
|
onReady?: (show: Show) => void;
|
|
35
44
|
className?: string;
|
|
36
45
|
style?: CSSProperties;
|
|
37
46
|
}
|
|
38
|
-
/** The story player: a film's scenes as slides, fitted to its box. Keys:
|
|
39
|
-
|
|
47
|
+
/** The story player: a film's scenes as slides, fitted to its box. Keys: ←/→ scene, Space play/pause, M sound,
|
|
48
|
+
* O orientation, R replay, Home from the top, F fullscreen. `poster` opens on the cover frame; `cover` paints an
|
|
49
|
+
* image and creates nothing until the first click. */
|
|
50
|
+
export declare function MotionShow({ film, locale, layout, mode, controls, slide, loop, poster, cover, title, length, fullscreenHref, onReady, className, style }: MotionShowProps): import("react").JSX.Element;
|
|
40
51
|
/** mode: capture renders light unless asked; the interactive player follows the page (auto). */
|
|
41
52
|
export interface ShowPageProps {
|
|
42
53
|
film: string;
|
|
43
54
|
layout?: Layout;
|
|
44
55
|
locale?: string;
|
|
45
56
|
mode?: Mode;
|
|
57
|
+
src?: 'json' | 'js';
|
|
46
58
|
capture?: boolean;
|
|
47
59
|
}
|
|
48
60
|
/** The /show/<film> page the video pipeline drives: with `capture`, the stage at 1:1, no chrome, window.__show. */
|
|
49
|
-
export declare function ShowPage({ film, layout, locale, mode, capture }: ShowPageProps): import("react").JSX.Element;
|
|
61
|
+
export declare function ShowPage({ film, layout, locale, mode, src, capture }: ShowPageProps): import("react").JSX.Element;
|
package/dist/components.js
CHANGED
|
@@ -2,7 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
2
2
|
// React components over @senseihq/emaki. All are client components (they need the DOM); in Next.js import them
|
|
3
3
|
// from a file marked 'use client'. Call setupMotion({ source, site, logos }) once on the client first.
|
|
4
4
|
import { useEffect, useRef, useState } from 'react';
|
|
5
|
-
import { attachScene, createShow, installCapture } from '@senseihq/emaki';
|
|
5
|
+
import { attachScene, createShow, installCapture, showKey, ticks } from '@senseihq/emaki';
|
|
6
6
|
import { usePlayerFor, useScenePlayer, useShowState, useSize, useWidth } from './hooks.js';
|
|
7
7
|
/** Mounts a player's scene into a div and keeps it fitted (shared by MotionScene and ScenePlayer). */
|
|
8
8
|
function useAttached(player, locale, mode, autoplay) {
|
|
@@ -32,56 +32,64 @@ export function ScenePlayer({ player, locale = 'en', mode = 'auto', controls = f
|
|
|
32
32
|
const msg = { position: 'absolute', inset: 0, display: 'grid', placeItems: 'center', margin: 0, font: '14px system-ui' };
|
|
33
33
|
return (_jsxs("div", { ref: ref, className: className, style: { position: 'relative', width: '100%' }, children: [_jsx("div", { ref: host }), p.status === 'error' && _jsx("p", { role: "alert", style: { ...msg, color: '#b42318' }, children: p.error }), p.status === 'idle' && _jsx("p", { style: { ...msg, opacity: 0.7 }, children: "Loading the scene\u2026" }), controls && p.status === 'ready' && (_jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: 12, marginTop: 10, font: '12px ui-monospace, monospace' }, children: [_jsx("button", { onClick: () => p.toggle(), "aria-label": p.playing ? 'Pause' : 'Play', children: p.playing ? '❚❚' : '▶' }), _jsx("input", { type: "range", "aria-label": "Seek", min: 0, max: p.dur, step: 0.05, value: p.t, onChange: (e) => p.seek(Number(e.target.value)), style: { flex: 1 } }), _jsxs("span", { children: [p.t.toFixed(1), " / ", p.dur.toFixed(1)] })] }))] }));
|
|
34
34
|
}
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
const btn = { minWidth: 34, height: 34, borderRadius: 17, border: '1px solid currentColor', background: 'none', color: 'inherit', cursor: 'pointer', padding: '0 10px' };
|
|
36
|
+
const tickTrack = 'linear-gradient(color-mix(in srgb, currentColor 18%, transparent), color-mix(in srgb, currentColor 18%, transparent)) center / 100% 4px no-repeat';
|
|
37
|
+
/** The play button over a poster or a cover image: the label and the chip name the film and its length. */
|
|
38
|
+
function PlayButton({ title, length, cover, onClick }) {
|
|
39
|
+
return (_jsxs("button", { onClick: onClick, "aria-label": `Play the film${title ? `: ${title}` : ''}`, style: { position: 'absolute', inset: 0, border: 0, padding: 0, background: 'none', cursor: 'pointer', color: 'inherit' }, children: [cover && _jsx("img", { src: cover, alt: "", style: { position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover' } }), _jsx("span", { "aria-hidden": "true", style: { position: 'absolute', right: 18, bottom: 14, width: 64, height: 64, borderRadius: '50%', background: 'color-mix(in srgb, currentColor 85%, transparent)', display: 'grid', placeItems: 'center' }, children: _jsx("svg", { viewBox: "0 0 24 24", style: { width: 28, height: 28, fill: 'Canvas', marginLeft: 3 }, children: _jsx("path", { d: "M8 5.5v13l11-6.5z" }) }) }), _jsxs("span", { style: { position: 'absolute', left: 16, bottom: 14, padding: '6px 12px', borderRadius: 999, font: '11px/1 system-ui', letterSpacing: '.1em', textTransform: 'uppercase', background: 'color-mix(in srgb, Canvas 88%, transparent)', border: '1px solid color-mix(in srgb, currentColor 20%, transparent)' }, children: ["Play the film", length ? ` · ${length}` : ''] })] }));
|
|
40
|
+
}
|
|
41
|
+
/** The story player: a film's scenes as slides, fitted to its box. Keys: ←/→ scene, Space play/pause, M sound,
|
|
42
|
+
* O orientation, R replay, Home from the top, F fullscreen. `poster` opens on the cover frame; `cover` paints an
|
|
43
|
+
* image and creates nothing until the first click. */
|
|
44
|
+
export function MotionShow({ film, locale = 'en', layout = 'landscape', mode = 'auto', controls = true, slide = 1, loop = false, poster = false, cover = '', title = '', length = '', fullscreenHref = '', onReady, className, style }) {
|
|
37
45
|
const stage = useRef(null);
|
|
38
46
|
const [fitRef, size] = useSize();
|
|
39
47
|
const [show, setShow] = useState(null);
|
|
48
|
+
// With a cover nothing is created until the reader clicks; that click then starts the show (sound on). The poster
|
|
49
|
+
// shows only until the reader first plays: a language or mode change after that restarts without it.
|
|
50
|
+
const [waiting, setWaiting] = useState(() => !!cover);
|
|
51
|
+
const autoplay = useRef(false), begun = useRef(false);
|
|
40
52
|
const st = useShowState(show);
|
|
53
|
+
useEffect(() => { if (st?.started && show)
|
|
54
|
+
begun.current = true; }, [st?.started, show]);
|
|
41
55
|
useEffect(() => {
|
|
56
|
+
if (waiting)
|
|
57
|
+
return;
|
|
42
58
|
let alive = true, made = null;
|
|
43
|
-
|
|
59
|
+
const play = autoplay.current;
|
|
60
|
+
autoplay.current = false;
|
|
61
|
+
createShow(stage.current, film, { locale, layout, mode, slide, loop, poster: (poster || play) && !begun.current }).then(async (s) => {
|
|
44
62
|
if (!alive)
|
|
45
63
|
return s.destroy();
|
|
46
64
|
made = s;
|
|
47
65
|
setShow(s);
|
|
48
66
|
onReady?.(s);
|
|
67
|
+
if (play)
|
|
68
|
+
await s.start();
|
|
49
69
|
});
|
|
50
70
|
return () => { alive = false; made?.destroy(); setShow(null); };
|
|
51
|
-
}, [film, locale, mode]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
71
|
+
}, [film, locale, mode, waiting]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
52
72
|
useEffect(() => { if (show && show.state.layout !== layout)
|
|
53
73
|
show.setLayout(layout); }, [show, layout]);
|
|
54
74
|
useEffect(() => { show?.fit(size.w, size.h); }, [show, size, st?.layout]);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
show.prev();
|
|
63
|
-
else if (k === ' ') {
|
|
64
|
-
show.toggle();
|
|
65
|
-
e.preventDefault();
|
|
66
|
-
}
|
|
67
|
-
else if (k === 'm')
|
|
68
|
-
show.setSound(!show.state.sound);
|
|
69
|
-
else if (k === 'o')
|
|
70
|
-
show.setLayout(show.state.layout === 'landscape' ? 'portrait' : 'landscape');
|
|
71
|
-
};
|
|
72
|
-
const btn = { minWidth: 34, height: 34, borderRadius: 17, border: '1px solid currentColor', background: 'none', color: 'inherit', cursor: 'pointer', padding: '0 10px' };
|
|
73
|
-
return (_jsxs("div", { tabIndex: 0, role: "application", "aria-label": "Story player", onKeyDown: onKeyDown, className: className, style: { position: 'relative', width: '100%', height: '100%', display: 'flex', flexDirection: 'column', outline: 'none', ...style }, children: [_jsx("div", { ref: fitRef, style: { position: 'relative', flex: 1, minHeight: 0, display: 'grid', placeItems: 'center', overflow: 'hidden' }, children: _jsx("div", { ref: stage, style: { position: 'relative' } }) }), controls && show && st && (_jsxs("div", { style: { display: 'flex', gap: 8, alignItems: 'center', justifyContent: 'center', padding: 8, font: '13px system-ui' }, children: [_jsx("button", { style: btn, onClick: () => show.prev(), "aria-label": "Previous scene", children: "\u2039" }), _jsx("button", { style: btn, onClick: () => show.toggle(), "aria-label": st.playing ? 'Pause' : 'Play', children: st.playing ? '❚❚' : '▶' }), _jsx("button", { style: btn, onClick: () => show.next(), "aria-label": "Next scene", children: "\u203A" }), _jsxs("span", { style: { opacity: 0.7 }, children: [st.index + 1, " / ", show.timeline.scenes.length] }), _jsx("button", { style: btn, onClick: () => show.setSound(!st.sound), "aria-pressed": st.sound, children: st.sound ? 'Sound on' : 'Sound off' })] }))] }));
|
|
75
|
+
// Every key (←/→, Space, M, O, R, Home, F) from the core, so all players answer the same way.
|
|
76
|
+
const onKeyDown = (e) => { if (show && showKey(show, e.nativeEvent, e.currentTarget))
|
|
77
|
+
e.preventDefault(); };
|
|
78
|
+
const open = () => { autoplay.current = true; setWaiting(false); };
|
|
79
|
+
return (_jsxs("div", { tabIndex: 0, role: "application", "aria-label": "Story player", onKeyDown: onKeyDown, className: className, style: { position: 'relative', width: '100%', height: '100%', display: 'flex', flexDirection: 'column', outline: 'none', ...style }, children: [_jsx("div", { ref: fitRef, style: { position: 'relative', flex: 1, minHeight: 0, display: 'grid', placeItems: 'center', overflow: 'hidden' }, children: _jsx("div", { ref: stage, style: { position: 'relative' } }) }), waiting ? _jsx(PlayButton, { title: title, length: length, cover: cover, onClick: open })
|
|
80
|
+
: show && st && !st.started ? _jsx(PlayButton, { title: title, length: length, onClick: () => show.start() })
|
|
81
|
+
: controls && show && st && (_jsxs("div", { style: { display: 'flex', gap: 8, alignItems: 'center', justifyContent: 'center', padding: 8, font: '13px system-ui' }, children: [_jsx("button", { style: btn, onClick: () => show.prev(), "aria-label": "Previous scene", children: "\u2039" }), _jsx("button", { style: btn, onClick: () => show.toggle(), "aria-label": st.playing ? 'Pause' : 'Play', children: st.playing ? '❚❚' : '▶' }), _jsx("button", { style: btn, onClick: () => show.next(), "aria-label": "Next scene", children: "\u203A" }), _jsx("div", { style: { flex: 1, display: 'flex', gap: 3, maxWidth: 360 }, children: ticks(show).map((tk, i) => (_jsx("button", { onClick: () => show.go(i), "aria-label": tk.name, style: { flex: tk.dur, minWidth: 0, height: 18, padding: '7px 0', border: 0, borderRadius: 0, background: tickTrack, cursor: 'pointer', color: 'inherit' }, children: _jsx("span", { style: { display: 'block', height: 4, borderRadius: 2, background: 'currentColor', width: `${tk.fill * 100}%` } }) }, tk.id))) }), _jsxs("span", { style: { opacity: 0.7, fontVariantNumeric: 'tabular-nums' }, children: [st.index + 1, " / ", show.timeline.scenes.length] }), _jsx("button", { style: btn, onClick: () => show.setSound(!st.sound), "aria-pressed": st.sound, children: st.sound ? 'Sound on' : 'Sound off' }), fullscreenHref && _jsx("a", { href: fullscreenHref, "aria-label": "Open full screen", title: "Full screen", style: { ...btn, display: 'grid', placeItems: 'center', textDecoration: 'none', padding: 0 }, children: "\u2922" })] }))] }));
|
|
74
82
|
}
|
|
75
83
|
/** The /show/<film> page the video pipeline drives: with `capture`, the stage at 1:1, no chrome, window.__show. */
|
|
76
|
-
export function ShowPage({ film, layout = 'landscape', locale = 'en', mode, capture = false }) {
|
|
84
|
+
export function ShowPage({ film, layout = 'landscape', locale = 'en', mode, src, capture = false }) {
|
|
77
85
|
const host = useRef(null);
|
|
78
86
|
useEffect(() => {
|
|
79
87
|
if (!capture)
|
|
80
88
|
return;
|
|
81
89
|
let s = null;
|
|
82
|
-
installCapture(host.current, film, layout, locale, mode ?? 'light').then((x) => { s = x; });
|
|
90
|
+
installCapture(host.current, film, layout, locale, { mode: mode ?? 'light', src }).then((x) => { s = x; });
|
|
83
91
|
return () => s?.destroy();
|
|
84
|
-
}, [capture, film, layout, locale, mode]);
|
|
92
|
+
}, [capture, film, layout, locale, mode, src]);
|
|
85
93
|
if (capture)
|
|
86
94
|
return _jsx("div", { ref: host });
|
|
87
95
|
return _jsx("div", { style: { position: 'fixed', inset: 0 }, children: _jsx(MotionShow, { film: film, layout: layout, locale: locale, mode: mode ?? 'auto' }) });
|
package/dist/hooks.d.ts
CHANGED
|
@@ -10,8 +10,10 @@ export interface ShowState {
|
|
|
10
10
|
playing: boolean;
|
|
11
11
|
sound: boolean;
|
|
12
12
|
layout: Layout;
|
|
13
|
+
started: boolean;
|
|
13
14
|
}
|
|
14
|
-
/** State of a story player controller (from <MotionShow onReady>): re-renders on scene, play, sound, layout changes
|
|
15
|
+
/** State of a story player controller (from <MotionShow onReady>): re-renders on scene, play, sound, layout changes,
|
|
16
|
+
* and follows the playhead (read once a frame; it moves without notifications). `started` is false on a poster. */
|
|
15
17
|
export declare function useShowState(show: Show | null): ShowState | null;
|
|
16
18
|
/** Width of an element, tracked with a ResizeObserver. */
|
|
17
19
|
export declare function useWidth<T extends HTMLElement>(): readonly [import("react").RefObject<T | null>, number];
|
package/dist/hooks.js
CHANGED
|
@@ -14,17 +14,23 @@ export function usePlayerFor(ref) {
|
|
|
14
14
|
player.load(ref); }, [key]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
15
15
|
return useScenePlayer(player);
|
|
16
16
|
}
|
|
17
|
-
/** State of a story player controller (from <MotionShow onReady>): re-renders on scene, play, sound, layout changes
|
|
17
|
+
/** State of a story player controller (from <MotionShow onReady>): re-renders on scene, play, sound, layout changes,
|
|
18
|
+
* and follows the playhead (read once a frame; it moves without notifications). `started` is false on a poster. */
|
|
18
19
|
export function useShowState(show) {
|
|
19
20
|
const [st, setSt] = useState(show ? show.state : null);
|
|
20
|
-
const last = useRef(show);
|
|
21
21
|
useEffect(() => {
|
|
22
|
-
last.current = show;
|
|
23
22
|
if (!show)
|
|
24
23
|
return setSt(null);
|
|
25
24
|
setSt(show.state);
|
|
26
25
|
const off = show.on((s) => setSt({ ...s }));
|
|
27
|
-
|
|
26
|
+
let raf = 0;
|
|
27
|
+
const follow = () => {
|
|
28
|
+
const local = show.state.local;
|
|
29
|
+
setSt((s) => (s && s.local !== local ? { ...s, local } : s));
|
|
30
|
+
raf = requestAnimationFrame(follow);
|
|
31
|
+
};
|
|
32
|
+
raf = requestAnimationFrame(follow);
|
|
33
|
+
return () => { off(); cancelAnimationFrame(raf); };
|
|
28
34
|
}, [show]);
|
|
29
35
|
return st;
|
|
30
36
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@senseihq/emaki-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "React components and hooks for motion stories: article figures, the story player, a controlled scene player and the video capture page.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"react": ">=18"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@senseihq/emaki": "0.1.
|
|
34
|
+
"@senseihq/emaki": "0.1.2"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/react": "^19.1.0",
|
package/src/components.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// React components over @senseihq/emaki. All are client components (they need the DOM); in Next.js import them
|
|
2
2
|
// from a file marked 'use client'. Call setupMotion({ source, site, logos }) once on the client first.
|
|
3
3
|
import { useEffect, useRef, useState, type CSSProperties } from 'react';
|
|
4
|
-
import { attachScene, createShow, installCapture, type AttachedScene, type Layout, type Mode, type ScenePlayerState } from '@senseihq/emaki';
|
|
4
|
+
import { attachScene, createShow, installCapture, showKey, ticks, type AttachedScene, type Layout, type Mode, type ScenePlayerState } from '@senseihq/emaki';
|
|
5
5
|
import { usePlayerFor, useScenePlayer, useShowState, useSize, useWidth, type Show } from './hooks.js';
|
|
6
6
|
|
|
7
7
|
/** Mounts a player's scene into a div and keeps it fitted (shared by MotionScene and ScenePlayer). */
|
|
@@ -53,49 +53,93 @@ export function ScenePlayer({ player, locale = 'en', mode = 'auto', controls = f
|
|
|
53
53
|
|
|
54
54
|
export interface MotionShowProps {
|
|
55
55
|
film: string; locale?: string; layout?: Layout; mode?: Mode; controls?: boolean; slide?: number; loop?: boolean;
|
|
56
|
+
/** Open on the film's cover frame with a play button; the first click plays it from the top with sound on. */
|
|
57
|
+
poster?: boolean;
|
|
58
|
+
/** An image (URL) to show with the play button; the show is created on the first click (then plays, sound on). */
|
|
59
|
+
cover?: string;
|
|
60
|
+
/** The film's name and running time, for the play button's label and chip. */
|
|
61
|
+
title?: string; length?: string;
|
|
62
|
+
/** Where the full-screen link goes (e.g. /show/<film>); no link when empty. */
|
|
63
|
+
fullscreenHref?: string;
|
|
56
64
|
/** The controller once ready (next/prev/play/pause/setSound/setLayout/seek/on), for custom chrome. */
|
|
57
65
|
onReady?: (show: Show) => void;
|
|
58
66
|
className?: string; style?: CSSProperties;
|
|
59
67
|
}
|
|
60
68
|
|
|
61
|
-
|
|
62
|
-
|
|
69
|
+
const btn: CSSProperties = { minWidth: 34, height: 34, borderRadius: 17, border: '1px solid currentColor', background: 'none', color: 'inherit', cursor: 'pointer', padding: '0 10px' };
|
|
70
|
+
const tickTrack = 'linear-gradient(color-mix(in srgb, currentColor 18%, transparent), color-mix(in srgb, currentColor 18%, transparent)) center / 100% 4px no-repeat';
|
|
71
|
+
|
|
72
|
+
/** The play button over a poster or a cover image: the label and the chip name the film and its length. */
|
|
73
|
+
function PlayButton({ title, length, cover, onClick }: { title: string; length: string; cover?: string; onClick: () => void }) {
|
|
74
|
+
return (
|
|
75
|
+
<button onClick={onClick} aria-label={`Play the film${title ? `: ${title}` : ''}`}
|
|
76
|
+
style={{ position: 'absolute', inset: 0, border: 0, padding: 0, background: 'none', cursor: 'pointer', color: 'inherit' }}>
|
|
77
|
+
{cover && <img src={cover} alt="" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover' }} />}
|
|
78
|
+
<span aria-hidden="true" style={{ position: 'absolute', right: 18, bottom: 14, width: 64, height: 64, borderRadius: '50%', background: 'color-mix(in srgb, currentColor 85%, transparent)', display: 'grid', placeItems: 'center' }}>
|
|
79
|
+
<svg viewBox="0 0 24 24" style={{ width: 28, height: 28, fill: 'Canvas', marginLeft: 3 }}><path d="M8 5.5v13l11-6.5z" /></svg>
|
|
80
|
+
</span>
|
|
81
|
+
<span style={{ position: 'absolute', left: 16, bottom: 14, padding: '6px 12px', borderRadius: 999, font: '11px/1 system-ui', letterSpacing: '.1em', textTransform: 'uppercase', background: 'color-mix(in srgb, Canvas 88%, transparent)', border: '1px solid color-mix(in srgb, currentColor 20%, transparent)' }}>
|
|
82
|
+
Play the film{length ? ` · ${length}` : ''}
|
|
83
|
+
</span>
|
|
84
|
+
</button>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The story player: a film's scenes as slides, fitted to its box. Keys: ←/→ scene, Space play/pause, M sound,
|
|
89
|
+
* O orientation, R replay, Home from the top, F fullscreen. `poster` opens on the cover frame; `cover` paints an
|
|
90
|
+
* image and creates nothing until the first click. */
|
|
91
|
+
export function MotionShow({ film, locale = 'en', layout = 'landscape', mode = 'auto', controls = true, slide = 1, loop = false, poster = false, cover = '', title = '', length = '', fullscreenHref = '', onReady, className, style }: MotionShowProps) {
|
|
63
92
|
const stage = useRef<HTMLDivElement>(null);
|
|
64
93
|
const [fitRef, size] = useSize<HTMLDivElement>();
|
|
65
94
|
const [show, setShow] = useState<Show | null>(null);
|
|
95
|
+
// With a cover nothing is created until the reader clicks; that click then starts the show (sound on). The poster
|
|
96
|
+
// shows only until the reader first plays: a language or mode change after that restarts without it.
|
|
97
|
+
const [waiting, setWaiting] = useState(() => !!cover);
|
|
98
|
+
const autoplay = useRef(false), begun = useRef(false);
|
|
66
99
|
const st = useShowState(show);
|
|
100
|
+
useEffect(() => { if (st?.started && show) begun.current = true; }, [st?.started, show]);
|
|
67
101
|
useEffect(() => {
|
|
102
|
+
if (waiting) return;
|
|
68
103
|
let alive = true, made: Show | null = null;
|
|
69
|
-
|
|
104
|
+
const play = autoplay.current;
|
|
105
|
+
autoplay.current = false;
|
|
106
|
+
createShow(stage.current!, film, { locale, layout, mode, slide, loop, poster: (poster || play) && !begun.current }).then(async (s) => {
|
|
70
107
|
if (!alive) return s.destroy();
|
|
71
108
|
made = s; setShow(s); onReady?.(s);
|
|
109
|
+
if (play) await s.start();
|
|
72
110
|
});
|
|
73
111
|
return () => { alive = false; made?.destroy(); setShow(null); };
|
|
74
|
-
}, [film, locale, mode]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
112
|
+
}, [film, locale, mode, waiting]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
75
113
|
useEffect(() => { if (show && show.state.layout !== layout) show.setLayout(layout); }, [show, layout]);
|
|
76
114
|
useEffect(() => { show?.fit(size.w, size.h); }, [show, size, st?.layout]);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (k === 'arrowright') show.next(); else if (k === 'arrowleft') show.prev();
|
|
81
|
-
else if (k === ' ') { show.toggle(); e.preventDefault(); }
|
|
82
|
-
else if (k === 'm') show.setSound(!show.state.sound);
|
|
83
|
-
else if (k === 'o') show.setLayout(show.state.layout === 'landscape' ? 'portrait' : 'landscape');
|
|
84
|
-
};
|
|
85
|
-
const btn: CSSProperties = { minWidth: 34, height: 34, borderRadius: 17, border: '1px solid currentColor', background: 'none', color: 'inherit', cursor: 'pointer', padding: '0 10px' };
|
|
115
|
+
// Every key (←/→, Space, M, O, R, Home, F) from the core, so all players answer the same way.
|
|
116
|
+
const onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => { if (show && showKey(show, e.nativeEvent, e.currentTarget)) e.preventDefault(); };
|
|
117
|
+
const open = () => { autoplay.current = true; setWaiting(false); };
|
|
86
118
|
return (
|
|
87
119
|
<div tabIndex={0} role="application" aria-label="Story player" onKeyDown={onKeyDown} className={className}
|
|
88
120
|
style={{ position: 'relative', width: '100%', height: '100%', display: 'flex', flexDirection: 'column', outline: 'none', ...style }}>
|
|
89
121
|
<div ref={fitRef} style={{ position: 'relative', flex: 1, minHeight: 0, display: 'grid', placeItems: 'center', overflow: 'hidden' }}>
|
|
90
122
|
<div ref={stage} style={{ position: 'relative' }} />
|
|
91
123
|
</div>
|
|
92
|
-
{
|
|
124
|
+
{waiting ? <PlayButton title={title} length={length} cover={cover} onClick={open} />
|
|
125
|
+
: show && st && !st.started ? <PlayButton title={title} length={length} onClick={() => show.start()} />
|
|
126
|
+
: controls && show && st && (
|
|
93
127
|
<div style={{ display: 'flex', gap: 8, alignItems: 'center', justifyContent: 'center', padding: 8, font: '13px system-ui' }}>
|
|
94
128
|
<button style={btn} onClick={() => show.prev()} aria-label="Previous scene">‹</button>
|
|
95
129
|
<button style={btn} onClick={() => show.toggle()} aria-label={st.playing ? 'Pause' : 'Play'}>{st.playing ? '❚❚' : '▶'}</button>
|
|
96
130
|
<button style={btn} onClick={() => show.next()} aria-label="Next scene">›</button>
|
|
97
|
-
<
|
|
131
|
+
<div style={{ flex: 1, display: 'flex', gap: 3, maxWidth: 360 }}>
|
|
132
|
+
{ticks(show).map((tk, i) => (
|
|
133
|
+
<button key={tk.id} onClick={() => show.go(i)} aria-label={tk.name}
|
|
134
|
+
style={{ flex: tk.dur, minWidth: 0, height: 18, padding: '7px 0', border: 0, borderRadius: 0, background: tickTrack, cursor: 'pointer', color: 'inherit' }}>
|
|
135
|
+
<span style={{ display: 'block', height: 4, borderRadius: 2, background: 'currentColor', width: `${tk.fill * 100}%` }} />
|
|
136
|
+
</button>
|
|
137
|
+
))}
|
|
138
|
+
</div>
|
|
139
|
+
<span style={{ opacity: 0.7, fontVariantNumeric: 'tabular-nums' }}>{st.index + 1} / {show.timeline.scenes.length}</span>
|
|
98
140
|
<button style={btn} onClick={() => show.setSound(!st.sound)} aria-pressed={st.sound}>{st.sound ? 'Sound on' : 'Sound off'}</button>
|
|
141
|
+
{fullscreenHref && <a href={fullscreenHref} aria-label="Open full screen" title="Full screen"
|
|
142
|
+
style={{ ...btn, display: 'grid', placeItems: 'center', textDecoration: 'none', padding: 0 }}>⤢</a>}
|
|
99
143
|
</div>
|
|
100
144
|
)}
|
|
101
145
|
</div>
|
|
@@ -103,17 +147,17 @@ export function MotionShow({ film, locale = 'en', layout = 'landscape', mode = '
|
|
|
103
147
|
}
|
|
104
148
|
|
|
105
149
|
/** mode: capture renders light unless asked; the interactive player follows the page (auto). */
|
|
106
|
-
export interface ShowPageProps { film: string; layout?: Layout; locale?: string; mode?: Mode; capture?: boolean }
|
|
150
|
+
export interface ShowPageProps { film: string; layout?: Layout; locale?: string; mode?: Mode; src?: 'json' | 'js'; capture?: boolean }
|
|
107
151
|
|
|
108
152
|
/** The /show/<film> page the video pipeline drives: with `capture`, the stage at 1:1, no chrome, window.__show. */
|
|
109
|
-
export function ShowPage({ film, layout = 'landscape', locale = 'en', mode, capture = false }: ShowPageProps) {
|
|
153
|
+
export function ShowPage({ film, layout = 'landscape', locale = 'en', mode, src, capture = false }: ShowPageProps) {
|
|
110
154
|
const host = useRef<HTMLDivElement>(null);
|
|
111
155
|
useEffect(() => {
|
|
112
156
|
if (!capture) return;
|
|
113
157
|
let s: Show | null = null;
|
|
114
|
-
installCapture(host.current!, film, layout, locale, mode ?? 'light').then((x) => { s = x; });
|
|
158
|
+
installCapture(host.current!, film, layout, locale, { mode: mode ?? 'light', src }).then((x) => { s = x; });
|
|
115
159
|
return () => s?.destroy();
|
|
116
|
-
}, [capture, film, layout, locale, mode]);
|
|
160
|
+
}, [capture, film, layout, locale, mode, src]);
|
|
117
161
|
if (capture) return <div ref={host} />;
|
|
118
162
|
return <div style={{ position: 'fixed', inset: 0 }}><MotionShow film={film} layout={layout} locale={locale} mode={mode ?? 'auto'} /></div>;
|
|
119
163
|
}
|
package/src/hooks.ts
CHANGED
|
@@ -21,18 +21,24 @@ export function usePlayerFor(ref: SceneRef | null) {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export type Show = Awaited<ReturnType<typeof createShow>>;
|
|
24
|
-
export interface ShowState { index: number; local: number; playing: boolean; sound: boolean; layout: Layout }
|
|
24
|
+
export interface ShowState { index: number; local: number; playing: boolean; sound: boolean; layout: Layout; started: boolean }
|
|
25
25
|
|
|
26
|
-
/** State of a story player controller (from <MotionShow onReady>): re-renders on scene, play, sound, layout changes
|
|
26
|
+
/** State of a story player controller (from <MotionShow onReady>): re-renders on scene, play, sound, layout changes,
|
|
27
|
+
* and follows the playhead (read once a frame; it moves without notifications). `started` is false on a poster. */
|
|
27
28
|
export function useShowState(show: Show | null): ShowState | null {
|
|
28
29
|
const [st, setSt] = useState<ShowState | null>(show ? show.state : null);
|
|
29
|
-
const last = useRef(show);
|
|
30
30
|
useEffect(() => {
|
|
31
|
-
last.current = show;
|
|
32
31
|
if (!show) return setSt(null);
|
|
33
32
|
setSt(show.state);
|
|
34
33
|
const off = show.on((s) => setSt({ ...s }));
|
|
35
|
-
|
|
34
|
+
let raf = 0;
|
|
35
|
+
const follow = () => {
|
|
36
|
+
const local = show.state.local;
|
|
37
|
+
setSt((s) => (s && s.local !== local ? { ...s, local } : s));
|
|
38
|
+
raf = requestAnimationFrame(follow);
|
|
39
|
+
};
|
|
40
|
+
raf = requestAnimationFrame(follow);
|
|
41
|
+
return () => { off(); cancelAnimationFrame(raf); };
|
|
36
42
|
}, [show]);
|
|
37
43
|
return st;
|
|
38
44
|
}
|