@motionhub/runtime 0.3.0 → 0.4.0

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
@@ -18,6 +18,7 @@ You do not use this package on its own: an engine binding provides it.
18
18
  | `<Caption opacity>` | One spoken line, drawn from the `caption` tokens (font, size, weight, color, background, radius, padding-y, padding-x, bottom, max-width, shadow, rule-left or rule-top) |
19
19
  | `<Sequence from duration>`, `interpolate(t, input, output, {easing})`, `Easing` | As Remotion's, in seconds: a span whose children count time from its start, a value mapped through a curve, and the curves (`Easing.bezier` takes a cubicBezier token). For composing components, as a reel does |
20
20
  | `<MotionRoot tokens>` | A full-frame box carrying the tokens as CSS custom properties |
21
+ | `<MotionScope tokens root>` | Its children drawn in another motion, on the engine's clock: `useTokens()` under it gives that motion's tokens (a `tokens.json`, or tokens already resolved), set as custom properties on a full-frame root (`root={false}` leaves it out). A film in several motions wraps each scene, or each part of a frame, in one |
21
22
  | `resolveTokens(dtcg)`, `tokenVariables(tokens)` | `tokens.json` (W3C design tokens) as values, and those values as custom properties: `--color-ink`, `--font-display`, `--duration-light`, `--easing-light`, `--caption-size` |
22
23
 
23
24
  A component colours with `var(--color-<name>)` and sets type with `var(--font-<name>)`, reads its time from `useTime()`, never from a clock, a timer or `Math.random`, and takes what changes (words, a progress from 0 to 1, a video's `src`) as props. So the same moment draws the same picture every time.
package/index.d.ts CHANGED
@@ -36,6 +36,8 @@ export declare const useVideoConfig: () => {width: number; height: number; fps:
36
36
  export declare const useTokens: () => Tokens;
37
37
  export declare const Video: FC<VideoProps>;
38
38
  export declare const MotionRoot: FC<{tokens?: Tokens; style?: CSSProperties; children?: ReactNode}>;
39
+ /** Its children drawn in another motion: its tokens (a tokens.json, or resolved), keeping the engine's time; one per scene or part of a frame */
40
+ export declare const MotionScope: FC<{tokens: unknown; root?: boolean; children?: ReactNode}>;
39
41
  export declare const Caption: FC<{children?: ReactNode; opacity?: number; style?: CSSProperties}>;
40
42
 
41
43
  export type EasingFn = (x: number) => number;
package/index.js CHANGED
@@ -4,7 +4,7 @@
4
4
  // the motion's tokens follow the W3C design tokens format, and their CSS custom properties follow Style Dictionary's
5
5
  // naming (--color-ink, --font-display). An engine binding (@motionhub/remotion, @motionhub/hyperframes, or an engine's own) supplies
6
6
  // the runtime through <MotionRuntimeProvider>; this package holds no engine.
7
- import {createContext, createElement, useContext} from 'react';
7
+ import {createContext, createElement, useContext, useMemo} from 'react';
8
8
 
9
9
  // ---- seeded randomness: the same seed gives the same stream on every machine and in every engine ----
10
10
  /** mulberry32 */
@@ -31,7 +31,7 @@ const hash = (key) => {
31
31
  export const seededRandom = (seed) => prng(typeof seed === 'number' ? seed : hash(String(seed)));
32
32
 
33
33
  export {resolveTokens, tokenVariables} from './tokens.js';
34
- import {tokenVariables} from './tokens.js';
34
+ import {resolveTokens, tokenVariables} from './tokens.js';
35
35
 
36
36
  // ---- the runtime an engine provides ----
37
37
  const Runtime = createContext(null);
@@ -115,6 +115,23 @@ export const MotionRoot = ({tokens, style, children}) => createElement('div', {
115
115
  style: {position: 'absolute', inset: 0, overflow: 'hidden', ...tokenVariables(tokens), ...style},
116
116
  }, children);
117
117
 
118
+ /** Whether `tokens` are a tokens.json (W3C design tokens, with `$value`s) rather than tokens already resolved */
119
+ const isDtcg = (tokens) => {
120
+ const walk = (node) => node && typeof node === 'object' && ('$value' in node || Object.values(node).some(walk));
121
+ return walk(tokens);
122
+ };
123
+
124
+ /**
125
+ * Its children drawn in another motion: every component under it reads `tokens` (that motion's tokens.json, or tokens already resolved) from
126
+ * useTokens(), and they are set as custom properties on a full-frame root (`root={false}` leaves it out), while time, frame and size stay the
127
+ * engine's. So one film uses several motions (SPEC.md §13): a <MotionScope> for each scene, or for each part of a frame, in its motion.
128
+ */
129
+ export const MotionScope = ({tokens, root = true, children}) => {
130
+ const parent = runtime();
131
+ const resolved = useMemo(() => (isDtcg(tokens) ? resolveTokens(tokens) : tokens ?? {}), [tokens]);
132
+ return createElement(Runtime.Provider, {value: {...parent, tokens: resolved}}, root ? createElement(MotionRoot, {tokens: resolved}, children) : children);
133
+ };
134
+
118
135
  /**
119
136
  * The motion's caption: one spoken line on the frame, drawn from the caption group of its tokens: font, size, weight, color, background, radius,
120
137
  * padding-y, padding-x, bottom, max-width, shadow (a text shadow), and a rule: rule-left (a bar down its left side) or rule-top (a short bar above it).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@motionhub/runtime",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "What a motion's React components draw with (time, frame, tokens, seeded randomness, video, caption), in any engine that provides it.",
5
5
  "license": "MIT",
6
6
  "repository": {