@krate/motion 0.0.0-bootstrap.0 → 0.5.0-rc.17
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 +201 -0
- package/NOTICE +36 -0
- package/README.md +56 -1
- package/package.json +64 -6
- package/src/components.tsx +59 -0
- package/src/context.ts +9 -0
- package/src/engine/animate.ts +340 -0
- package/src/engine/animation.ts +852 -0
- package/src/engine/drag.ts +584 -0
- package/src/engine/easing.ts +108 -0
- package/src/engine/factories.ts +192 -0
- package/src/engine/fallback.ts +44 -0
- package/src/engine/frameloop.ts +179 -0
- package/src/engine/generators.ts +342 -0
- package/src/engine/gesture-features.ts +136 -0
- package/src/engine/gestures.ts +229 -0
- package/src/engine/index.ts +22 -0
- package/src/engine/krate.ts +402 -0
- package/src/engine/layout.ts +365 -0
- package/src/engine/motion-value.ts +234 -0
- package/src/engine/node.ts +988 -0
- package/src/engine/render.ts +295 -0
- package/src/engine/reorder.ts +65 -0
- package/src/engine/runtime.ts +74 -0
- package/src/engine/scope.ts +83 -0
- package/src/engine/scroll.ts +272 -0
- package/src/engine/signals.ts +108 -0
- package/src/engine/utils.ts +122 -0
- package/src/engine/values.ts +508 -0
- package/src/hooks.ts +191 -0
- package/src/index.ts +143 -0
- package/src/island/index.ts +696 -0
- package/src/motion-types.ts +25 -0
- package/src/reorder.tsx +112 -0
- package/src/types.ts +179 -0
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
// Building and writing styles and SVG attributes from motion values:
|
|
2
|
+
// individual transforms are combined in a fixed order, numbers get their
|
|
3
|
+
// default unit, CSS variables are set with setProperty. Ported from
|
|
4
|
+
// framer-motion and motion-dom (MIT, Copyright (c) 2018 Framer B.V.), see
|
|
5
|
+
// NOTICE.
|
|
6
|
+
|
|
7
|
+
import { getDefaultValueType, getValueAsType, numberValueTypes, px } from './values.js';
|
|
8
|
+
|
|
9
|
+
export const transformPropOrder = [
|
|
10
|
+
'transformPerspective',
|
|
11
|
+
'x',
|
|
12
|
+
'y',
|
|
13
|
+
'z',
|
|
14
|
+
'translateX',
|
|
15
|
+
'translateY',
|
|
16
|
+
'translateZ',
|
|
17
|
+
'scale',
|
|
18
|
+
'scaleX',
|
|
19
|
+
'scaleY',
|
|
20
|
+
'rotate',
|
|
21
|
+
'rotateX',
|
|
22
|
+
'rotateY',
|
|
23
|
+
'rotateZ',
|
|
24
|
+
'skew',
|
|
25
|
+
'skewX',
|
|
26
|
+
'skewY',
|
|
27
|
+
];
|
|
28
|
+
export const transformProps: Set<string> = new Set(transformPropOrder);
|
|
29
|
+
export const positionalKeys: Set<string> = new Set(['width', 'height', 'top', 'left', 'right', 'bottom', ...transformPropOrder]);
|
|
30
|
+
|
|
31
|
+
export const isCSSVar = (name: string): boolean => name.startsWith('--');
|
|
32
|
+
|
|
33
|
+
export const camelToDash = (s: string): string => s.replace(/([a-z])([A-Z])/gu, '$1-$2').toLowerCase();
|
|
34
|
+
|
|
35
|
+
export interface RenderState {
|
|
36
|
+
style: Record<string, any>;
|
|
37
|
+
transform: Record<string, any>;
|
|
38
|
+
transformOrigin: Record<string, any>;
|
|
39
|
+
vars: Record<string, any>;
|
|
40
|
+
attrs: Record<string, any>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const createRenderState = (): RenderState => ({ style: {}, transform: {}, transformOrigin: {}, vars: {}, attrs: {} });
|
|
44
|
+
|
|
45
|
+
export type TransformTemplate = (transform: Record<string, any>, generated: string) => string;
|
|
46
|
+
|
|
47
|
+
const translateAlias: Record<string, string> = { x: 'translateX', y: 'translateY', z: 'translateZ', transformPerspective: 'perspective' };
|
|
48
|
+
|
|
49
|
+
/** Builds a CSS transform from individual transform values, in Motion's order. */
|
|
50
|
+
export function buildTransform(latest: Record<string, any>, transform: Record<string, any>, template?: TransformTemplate): string {
|
|
51
|
+
let out = '';
|
|
52
|
+
let isDefault = true;
|
|
53
|
+
for (const key of transformPropOrder) {
|
|
54
|
+
const value = latest[key];
|
|
55
|
+
if (value === undefined) continue;
|
|
56
|
+
const valueIsDefault = typeof value === 'number' ? value === (key.startsWith('scale') ? 1 : 0) : parseFloat(value) === 0;
|
|
57
|
+
if (!valueIsDefault || template) {
|
|
58
|
+
const asType = getValueAsType(value, numberValueTypes[key]);
|
|
59
|
+
if (!valueIsDefault) {
|
|
60
|
+
isDefault = false;
|
|
61
|
+
out += `${translateAlias[key] || key}(${asType}) `;
|
|
62
|
+
}
|
|
63
|
+
if (template) transform[key] = asType;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
out = out.trim();
|
|
67
|
+
if (template) out = template(transform, isDefault ? '' : out);
|
|
68
|
+
else if (isDefault) out = 'none';
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function buildHTMLStyles(state: RenderState, latest: Record<string, any>, template?: TransformTemplate): void {
|
|
73
|
+
const { style, vars, transformOrigin } = state;
|
|
74
|
+
let hasTransform = false;
|
|
75
|
+
let hasOrigin = false;
|
|
76
|
+
for (const key in latest) {
|
|
77
|
+
const value = latest[key];
|
|
78
|
+
if (transformProps.has(key)) {
|
|
79
|
+
hasTransform = true;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (isCSSVar(key)) {
|
|
83
|
+
vars[key] = value;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const asType = getValueAsType(value, numberValueTypes[key]);
|
|
87
|
+
if (key.startsWith('origin')) {
|
|
88
|
+
hasOrigin = true;
|
|
89
|
+
transformOrigin[key] = asType;
|
|
90
|
+
} else {
|
|
91
|
+
style[key] = asType;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (!latest.transform) {
|
|
95
|
+
if (hasTransform || template) style.transform = buildTransform(latest, state.transform, template);
|
|
96
|
+
else if (style.transform) style.transform = 'none';
|
|
97
|
+
}
|
|
98
|
+
if (hasOrigin) {
|
|
99
|
+
const { originX = '50%', originY = '50%', originZ = 0 } = transformOrigin;
|
|
100
|
+
style.transformOrigin = `${originX} ${originY} ${originZ}`;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Attributes always written in camel case. */
|
|
105
|
+
export const camelCaseAttributes: Set<string> = new Set([
|
|
106
|
+
'baseFrequency',
|
|
107
|
+
'diffuseConstant',
|
|
108
|
+
'kernelMatrix',
|
|
109
|
+
'kernelUnitLength',
|
|
110
|
+
'keySplines',
|
|
111
|
+
'keyTimes',
|
|
112
|
+
'limitingConeAngle',
|
|
113
|
+
'markerHeight',
|
|
114
|
+
'markerWidth',
|
|
115
|
+
'numOctaves',
|
|
116
|
+
'targetX',
|
|
117
|
+
'targetY',
|
|
118
|
+
'surfaceScale',
|
|
119
|
+
'specularConstant',
|
|
120
|
+
'specularExponent',
|
|
121
|
+
'stdDeviation',
|
|
122
|
+
'tableValues',
|
|
123
|
+
'viewBox',
|
|
124
|
+
'gradientTransform',
|
|
125
|
+
'pathLength',
|
|
126
|
+
'startOffset',
|
|
127
|
+
'textLength',
|
|
128
|
+
'lengthAdjust',
|
|
129
|
+
]);
|
|
130
|
+
|
|
131
|
+
/** pathLength/pathSpacing/pathOffset (0-1 of the path) as stroke-dasharray/offset over a pathLength of 1. */
|
|
132
|
+
export function buildSVGPath(attrs: Record<string, any>, length: number, spacing = 1, offset = 0, dashCase = true): void {
|
|
133
|
+
attrs.pathLength = 1;
|
|
134
|
+
attrs[dashCase ? 'stroke-dashoffset' : 'strokeDashoffset'] = px.transform!(-offset);
|
|
135
|
+
attrs[dashCase ? 'stroke-dasharray' : 'strokeDasharray'] = `${px.transform!(length)} ${px.transform!(spacing)}`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function buildSVGAttrs(
|
|
139
|
+
state: RenderState,
|
|
140
|
+
{ attrX, attrY, attrScale, pathLength, pathSpacing = 1, pathOffset = 0, ...latest }: Record<string, any>,
|
|
141
|
+
isSVGTag: boolean,
|
|
142
|
+
template?: TransformTemplate,
|
|
143
|
+
styleProp?: Record<string, any>,
|
|
144
|
+
): void {
|
|
145
|
+
buildHTMLStyles(state, latest, template);
|
|
146
|
+
if (isSVGTag) {
|
|
147
|
+
if (state.style.viewBox) state.attrs.viewBox = state.style.viewBox;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
state.attrs = state.style;
|
|
151
|
+
state.style = {};
|
|
152
|
+
const { attrs, style } = state;
|
|
153
|
+
if (attrs.transform) {
|
|
154
|
+
style.transform = attrs.transform;
|
|
155
|
+
delete attrs.transform;
|
|
156
|
+
}
|
|
157
|
+
if (style.transform || attrs.transformOrigin) {
|
|
158
|
+
style.transformOrigin = attrs.transformOrigin ?? '50% 50%';
|
|
159
|
+
delete attrs.transformOrigin;
|
|
160
|
+
}
|
|
161
|
+
if (style.transform) {
|
|
162
|
+
style.transformBox = styleProp?.transformBox ?? 'fill-box';
|
|
163
|
+
delete attrs.transformBox;
|
|
164
|
+
}
|
|
165
|
+
if (attrX !== undefined) attrs.x = attrX;
|
|
166
|
+
if (attrY !== undefined) attrs.y = attrY;
|
|
167
|
+
if (attrScale !== undefined) attrs.scale = attrScale;
|
|
168
|
+
if (pathLength !== undefined) buildSVGPath(attrs, pathLength, pathSpacing, pathOffset, false);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function setStyle(el: HTMLElement | SVGElement, name: string, value: any): void {
|
|
172
|
+
if (isCSSVar(name)) el.style.setProperty(name, value);
|
|
173
|
+
else (el.style as any)[name] = value;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function renderHTML(el: HTMLElement | SVGElement, state: RenderState): void {
|
|
177
|
+
const s = el.style as any;
|
|
178
|
+
for (const key in state.style) s[key] = state.style[key];
|
|
179
|
+
for (const key in state.vars) s.setProperty(key, state.vars[key]);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function renderSVG(el: SVGElement, state: RenderState): void {
|
|
183
|
+
renderHTML(el, state);
|
|
184
|
+
for (const key in state.attrs) {
|
|
185
|
+
const v = state.attrs[key];
|
|
186
|
+
el.setAttribute(!camelCaseAttributes.has(key) ? camelToDash(key) : key, v);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export const lowercaseSVGElements = [
|
|
191
|
+
'animate',
|
|
192
|
+
'circle',
|
|
193
|
+
'defs',
|
|
194
|
+
'desc',
|
|
195
|
+
'ellipse',
|
|
196
|
+
'g',
|
|
197
|
+
'image',
|
|
198
|
+
'line',
|
|
199
|
+
'filter',
|
|
200
|
+
'marker',
|
|
201
|
+
'mask',
|
|
202
|
+
'metadata',
|
|
203
|
+
'path',
|
|
204
|
+
'pattern',
|
|
205
|
+
'polygon',
|
|
206
|
+
'polyline',
|
|
207
|
+
'rect',
|
|
208
|
+
'stop',
|
|
209
|
+
'switch',
|
|
210
|
+
'symbol',
|
|
211
|
+
'svg',
|
|
212
|
+
'text',
|
|
213
|
+
'tspan',
|
|
214
|
+
'use',
|
|
215
|
+
'view',
|
|
216
|
+
];
|
|
217
|
+
|
|
218
|
+
/** Whether a tag name renders an SVG element (and gets attributes instead of styles). */
|
|
219
|
+
export function isSVGComponent(tag: unknown): boolean {
|
|
220
|
+
if (typeof tag !== 'string' || tag.includes('-')) return false;
|
|
221
|
+
return lowercaseSVGElements.includes(tag) || /[A-Z]/u.test(tag);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const radToDeg = (rad: number): number => (rad * 180) / Math.PI;
|
|
225
|
+
const rebaseAngle = (a: number): number => {
|
|
226
|
+
a %= 360;
|
|
227
|
+
return a < 0 ? a + 360 : a;
|
|
228
|
+
};
|
|
229
|
+
const rotate2d = (v: number[]): number => rebaseAngle(radToDeg(Math.atan2(v[1], v[0])));
|
|
230
|
+
const matrix2d: Record<string, number | ((v: number[]) => number)> = {
|
|
231
|
+
x: 4,
|
|
232
|
+
y: 5,
|
|
233
|
+
translateX: 4,
|
|
234
|
+
translateY: 5,
|
|
235
|
+
scaleX: 0,
|
|
236
|
+
scaleY: 3,
|
|
237
|
+
scale: (v) => (Math.abs(v[0]) + Math.abs(v[3])) / 2,
|
|
238
|
+
rotate: rotate2d,
|
|
239
|
+
rotateZ: rotate2d,
|
|
240
|
+
skewX: (v) => radToDeg(Math.atan(v[1])),
|
|
241
|
+
skewY: (v) => radToDeg(Math.atan(v[2])),
|
|
242
|
+
skew: (v) => (Math.abs(v[1]) + Math.abs(v[2])) / 2,
|
|
243
|
+
};
|
|
244
|
+
const scaleX3 = (v: number[]): number => Math.sqrt(v[0] * v[0] + v[1] * v[1]);
|
|
245
|
+
const scaleY3 = (v: number[]): number => Math.sqrt(v[4] * v[4] + v[5] * v[5]);
|
|
246
|
+
const matrix3d: Record<string, number | ((v: number[]) => number)> = {
|
|
247
|
+
x: 12,
|
|
248
|
+
y: 13,
|
|
249
|
+
z: 14,
|
|
250
|
+
translateX: 12,
|
|
251
|
+
translateY: 13,
|
|
252
|
+
translateZ: 14,
|
|
253
|
+
scaleX: scaleX3,
|
|
254
|
+
scaleY: scaleY3,
|
|
255
|
+
scale: (v) => (scaleX3(v) + scaleY3(v)) / 2,
|
|
256
|
+
rotateX: (v) => rebaseAngle(radToDeg(Math.atan2(v[6], v[5]))),
|
|
257
|
+
rotateY: (v) => rebaseAngle(radToDeg(Math.atan2(-v[2], v[0]))),
|
|
258
|
+
rotateZ: rotate2d,
|
|
259
|
+
rotate: rotate2d,
|
|
260
|
+
skewX: (v) => radToDeg(Math.atan(v[4])),
|
|
261
|
+
skewY: (v) => radToDeg(Math.atan(v[1])),
|
|
262
|
+
skew: (v) => (Math.abs(v[1]) + Math.abs(v[4])) / 2,
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
export const defaultTransformValue = (name: string): number => (name.includes('scale') ? 1 : 0);
|
|
266
|
+
|
|
267
|
+
/** Reads one transform value (x, scale, rotate, ...) out of a computed `matrix()`/`matrix3d()`. */
|
|
268
|
+
export function parseValueFromTransform(transform: string | undefined, name: string): number {
|
|
269
|
+
if (!transform || transform === 'none') return defaultTransformValue(name);
|
|
270
|
+
const m3 = transform.match(/^matrix3d\(([-\d.e\s,]+)\)$/u);
|
|
271
|
+
const parsers = m3 ? matrix3d : matrix2d;
|
|
272
|
+
const match = m3 || transform.match(/^matrix\(([-\d.e\s,]+)\)$/u);
|
|
273
|
+
if (!match) return defaultTransformValue(name);
|
|
274
|
+
const parser = parsers[name];
|
|
275
|
+
const values = match[1].split(',').map((s) => parseFloat(s.trim()));
|
|
276
|
+
if (parser === undefined) return defaultTransformValue(name);
|
|
277
|
+
return typeof parser === 'function' ? parser(values) : values[parser];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** The current value of `key` on an element, read from its computed style or attributes. */
|
|
281
|
+
export function readDOMValue(el: Element, key: string, isSVG: boolean): any {
|
|
282
|
+
if (transformProps.has(key)) {
|
|
283
|
+
if (isSVG) {
|
|
284
|
+
const t = getDefaultValueType(key);
|
|
285
|
+
return t ? t.default || 0 : 0;
|
|
286
|
+
}
|
|
287
|
+
return parseValueFromTransform(getComputedStyle(el).transform, key);
|
|
288
|
+
}
|
|
289
|
+
if (isSVG && !(key in (el as HTMLElement).style)) {
|
|
290
|
+
return el.getAttribute(!camelCaseAttributes.has(key) ? camelToDash(key) : key);
|
|
291
|
+
}
|
|
292
|
+
const cs = getComputedStyle(el);
|
|
293
|
+
const v = (isCSSVar(key) ? cs.getPropertyValue(key) : (cs as any)[key]) || 0;
|
|
294
|
+
return typeof v === 'string' ? v.trim() : v;
|
|
295
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Reorder.Group / Reorder.Item: items are dragged on one axis; when an item
|
|
2
|
+
// passes the middle of its neighbour the group calls onReorder with the new
|
|
3
|
+
// order, and layout animations move the others. Ported from framer-motion
|
|
4
|
+
// (MIT, Copyright (c) 2018 Framer B.V.), see NOTICE.
|
|
5
|
+
|
|
6
|
+
import type { Box } from './drag.js';
|
|
7
|
+
import { mixNumber, moveItem } from './utils.js';
|
|
8
|
+
|
|
9
|
+
export interface ItemData<T> {
|
|
10
|
+
value: T;
|
|
11
|
+
layout: { min: number; max: number };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ReorderContext<T> {
|
|
15
|
+
axis: 'x' | 'y';
|
|
16
|
+
registerItem(value: T, layout: Box): void;
|
|
17
|
+
updateOrder(value: T, offset: number, velocity: number): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function checkReorder<T>(order: ItemData<T>[], value: T, offset: number, velocity: number): ItemData<T>[] {
|
|
21
|
+
if (!velocity) return order;
|
|
22
|
+
const index = order.findIndex((item) => item.value === value);
|
|
23
|
+
if (index === -1) return order;
|
|
24
|
+
const step = velocity > 0 ? 1 : -1;
|
|
25
|
+
const next = order[index + step];
|
|
26
|
+
if (!next) return order;
|
|
27
|
+
const item = order[index];
|
|
28
|
+
const nextCenter = mixNumber(next.layout.min, next.layout.max, 0.5);
|
|
29
|
+
if ((step === 1 && item.layout.max + offset > nextCenter) || (step === -1 && item.layout.min + offset < nextCenter)) {
|
|
30
|
+
return moveItem(order, index, index + step);
|
|
31
|
+
}
|
|
32
|
+
return order;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The context a Reorder.Group gives its items. `getProps` returns its live props. */
|
|
36
|
+
export function createReorderGroup<T>(getProps: () => { axis?: 'x' | 'y'; values: T[]; onReorder: (values: T[]) => void }): ReorderContext<T> {
|
|
37
|
+
const order: ItemData<T>[] = [];
|
|
38
|
+
let reordered = false;
|
|
39
|
+
const ctx: ReorderContext<T> = {
|
|
40
|
+
get axis() {
|
|
41
|
+
return getProps().axis || 'y';
|
|
42
|
+
},
|
|
43
|
+
registerItem(value, layout) {
|
|
44
|
+
const axis = ctx.axis;
|
|
45
|
+
const idx = order.findIndex((e) => value === e.value);
|
|
46
|
+
if (idx !== -1) order[idx].layout = layout[axis];
|
|
47
|
+
else order.push({ value, layout: layout[axis] });
|
|
48
|
+
const values = getProps().values;
|
|
49
|
+
order.sort((a, b) => values.indexOf(a.value) - values.indexOf(b.value));
|
|
50
|
+
},
|
|
51
|
+
updateOrder(value, offset, velocity) {
|
|
52
|
+
if (reordered) return;
|
|
53
|
+
const next = checkReorder(order, value, offset, velocity);
|
|
54
|
+
if (order !== next) {
|
|
55
|
+
reordered = true;
|
|
56
|
+
const { values, onReorder } = getProps();
|
|
57
|
+
onReorder(next.map((e) => e.value).filter((v) => values.includes(v)));
|
|
58
|
+
queueMicrotask(() => {
|
|
59
|
+
reordered = false;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
return ctx;
|
|
65
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// The engine is bundled for the browser like an npm dependency, so it uses
|
|
2
|
+
// the page's Krate runtime from the global object instead of importing a
|
|
3
|
+
// second copy (as @krate/konva's engine does).
|
|
4
|
+
|
|
5
|
+
export interface KrateRuntime {
|
|
6
|
+
createSignal<T>(initial: T): [() => T, (next: T | ((prev: T) => T)) => void];
|
|
7
|
+
createEffect(fn: () => void): () => void;
|
|
8
|
+
createRoot<T>(fn: (dispose: () => void) => T): T;
|
|
9
|
+
onCleanup(fn: () => void): void;
|
|
10
|
+
untrack<T>(fn: () => T): T;
|
|
11
|
+
krateSetRef?(ref: unknown, value: unknown): () => void;
|
|
12
|
+
krateSetExitHook?(hook: ExitHook | null): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** What the runtime asks before it removes an element: keep it for an exit animation? */
|
|
16
|
+
export interface ExitHook {
|
|
17
|
+
/** Returns true to keep `el` in the document; `done` removes it later. */
|
|
18
|
+
exit(el: Element, done: () => void): boolean;
|
|
19
|
+
/** `el` is shown again (a conditional switched back) before its exit finished. */
|
|
20
|
+
cancel?(el: Element): void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Every runtime function is read as a member of globalThis written out in
|
|
24
|
+
// full (globalThis.createEffect), so the production build, which keeps only
|
|
25
|
+
// the runtime exports a site's code reads, sees each one.
|
|
26
|
+
|
|
27
|
+
/** The page runtime, or null outside a Krate page (tests install it). */
|
|
28
|
+
export function rt(): KrateRuntime | null {
|
|
29
|
+
return typeof (globalThis as any).createEffect === 'function' ? ((globalThis as any) as KrateRuntime) : null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function untrack<T>(fn: () => T): T {
|
|
33
|
+
return typeof (globalThis as any).untrack === 'function' ? (globalThis as any).untrack(fn) : fn();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Runs `fn` now and again whenever a signal it reads changes; returns the dispose. */
|
|
37
|
+
export function effect(fn: () => void): () => void {
|
|
38
|
+
if (typeof (globalThis as any).createEffect !== 'function' || typeof (globalThis as any).createRoot !== 'function') {
|
|
39
|
+
fn();
|
|
40
|
+
return () => {};
|
|
41
|
+
}
|
|
42
|
+
return (globalThis as any).createRoot((dispose: () => void) => {
|
|
43
|
+
(globalThis as any).createEffect(fn);
|
|
44
|
+
return dispose;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Attaches `value` to a ref (object, callback or Krate variable setter); returns the detach. */
|
|
49
|
+
export function setRef(ref: unknown, value: unknown): () => void {
|
|
50
|
+
if (ref == null) return () => {};
|
|
51
|
+
if (typeof (globalThis as any).krateSetRef === 'function') return (globalThis as any).krateSetRef(ref, value);
|
|
52
|
+
if (typeof ref === 'function') {
|
|
53
|
+
const cleanup = (ref as (v: unknown) => unknown)(value);
|
|
54
|
+
return () => {
|
|
55
|
+
if (typeof cleanup === 'function') cleanup();
|
|
56
|
+
else (ref as (v: unknown) => unknown)(null);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
(ref as { current: unknown }).current = value;
|
|
60
|
+
return () => {
|
|
61
|
+
if ((ref as { current: unknown }).current === value) (ref as { current: unknown }).current = null;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let installed: ExitHook | null = null;
|
|
66
|
+
|
|
67
|
+
/** Installs the exit hook in the page runtime once (older runtimes remove at once). */
|
|
68
|
+
export function installExitHook(hook: ExitHook): void {
|
|
69
|
+
if (installed) return;
|
|
70
|
+
if (typeof (globalThis as any).krateSetExitHook === 'function') {
|
|
71
|
+
installed = hook;
|
|
72
|
+
(globalThis as any).krateSetExitHook(hook);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Scopes: what AnimatePresence, MotionConfig, LayoutGroup and LazyMotion give
|
|
2
|
+
// the motion elements under them. A scope is created once per provider
|
|
3
|
+
// instance (compiled code creates it at the top of the component that writes
|
|
4
|
+
// the provider, so elements in the same JSX get it directly; elements in
|
|
5
|
+
// other components get it through the provider's context).
|
|
6
|
+
|
|
7
|
+
export type ScopeKind = 'presence' | 'config' | 'group' | 'lazy';
|
|
8
|
+
|
|
9
|
+
export interface Scope {
|
|
10
|
+
kind: ScopeKind;
|
|
11
|
+
/** The provider's current props. */
|
|
12
|
+
get: () => Record<string, any>;
|
|
13
|
+
parent: Scope | null;
|
|
14
|
+
/** Mount batch the scope was created in (AnimatePresence `initial={false}`). */
|
|
15
|
+
batch: number;
|
|
16
|
+
/** Per-scope state (presence bookkeeping, layout group registry). */
|
|
17
|
+
state: Record<string, any>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Counts mount batches; batch 0 is the page's hydration. */
|
|
21
|
+
export const batches = { current: 0, hydrated: false };
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Normalizes what a context read gives (a scope, a `{ s }` context value, or
|
|
25
|
+
* a getter of either, as compiled hydration code passes it) to a scope.
|
|
26
|
+
*/
|
|
27
|
+
export function resolveScope(v: unknown): Scope | null {
|
|
28
|
+
for (let i = 0; i < 3 && typeof v === 'function'; i++) v = (v as () => unknown)();
|
|
29
|
+
if (!v || typeof v !== 'object') return null;
|
|
30
|
+
if ((v as { kind?: unknown }).kind && typeof (v as Scope).get === 'function') return v as Scope;
|
|
31
|
+
const s = (v as { s?: unknown }).s;
|
|
32
|
+
return s ? resolveScope(s) : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Creates the scope of a provider (`kind`, live props `get`) under `parent`. */
|
|
36
|
+
export function createScope(kind: ScopeKind, get: () => Record<string, any>, parent?: unknown): Scope {
|
|
37
|
+
let resolvedParent: Scope | null | undefined;
|
|
38
|
+
const scope: Scope = {
|
|
39
|
+
kind,
|
|
40
|
+
get,
|
|
41
|
+
get parent() {
|
|
42
|
+
if (resolvedParent === undefined) resolvedParent = resolveScope(parent);
|
|
43
|
+
return resolvedParent;
|
|
44
|
+
},
|
|
45
|
+
batch: batches.current,
|
|
46
|
+
state: {},
|
|
47
|
+
} as Scope;
|
|
48
|
+
return scope;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The nearest scope of `kind` at or above `scope`. */
|
|
52
|
+
export function findScope(scope: Scope | null, kind: ScopeKind): Scope | null {
|
|
53
|
+
for (let s = scope; s; s = s.parent) if (s.kind === kind) return s;
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** MotionConfig values merged from every MotionConfig above. */
|
|
58
|
+
export function configFromScope(scope: Scope | null): Record<string, any> {
|
|
59
|
+
const chain: Scope[] = [];
|
|
60
|
+
for (let s = scope; s; s = s.parent) if (s.kind === 'config') chain.unshift(s);
|
|
61
|
+
const out: Record<string, any> = {};
|
|
62
|
+
for (const s of chain) {
|
|
63
|
+
const p = s.get() || {};
|
|
64
|
+
for (const key of ['transition', 'reducedMotion', 'transformPagePoint', 'isStatic', 'nonce']) {
|
|
65
|
+
if (p[key] === undefined) continue;
|
|
66
|
+
if (key === 'transition' && out.transition && p.transition && p.transition.inherit) out.transition = { ...out.transition, ...p.transition };
|
|
67
|
+
else out[key] = p[key];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The LayoutGroup id prefix for layoutId (nested groups join with "-"). */
|
|
74
|
+
export function layoutGroupId(scope: Scope | null): string | undefined {
|
|
75
|
+
const ids: string[] = [];
|
|
76
|
+
for (let s = scope; s; s = s.parent) {
|
|
77
|
+
if (s.kind !== 'group') continue;
|
|
78
|
+
const { id, inherit = true } = s.get() || {};
|
|
79
|
+
if (id) ids.unshift(id);
|
|
80
|
+
if (inherit === false || inherit === 'id') break;
|
|
81
|
+
}
|
|
82
|
+
return ids.length ? ids.join('-') : undefined;
|
|
83
|
+
}
|