@prismora/motion 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +12 -2
- package/dist/index.d.ts +67 -0
- package/dist/index.js +125 -0
- package/package.json +36 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aravind Kumar and Prismora contributors
|
|
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
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @prismora/motion
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Prismora motion tokens: durations, easings and spring curves as CSS linear() functions.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @prismora/motion
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { easings } from '@prismora/motion';
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Documentation: [prismora.design/docs/motion](https://prismora.design/docs/motion) · Part of [Prismora](https://github.com/axxra/prismora), one seed, every theme. MIT licence.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @prismora/motion — motion tokens, physics-based CSS `linear()` easings and small helpers.
|
|
3
|
+
* All helpers respect `prefers-reduced-motion` and the `data-motion` attribute.
|
|
4
|
+
*/
|
|
5
|
+
export interface SpringOptions {
|
|
6
|
+
/** Stiffness (k). Higher = snappier. */
|
|
7
|
+
stiffness?: number;
|
|
8
|
+
/** Damping (c). Lower = bouncier. */
|
|
9
|
+
damping?: number;
|
|
10
|
+
/** Mass (m). */
|
|
11
|
+
mass?: number;
|
|
12
|
+
/** Number of sample points in the generated curve. */
|
|
13
|
+
samples?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface SpringEasing {
|
|
16
|
+
/** CSS `linear()` easing function. */
|
|
17
|
+
easing: string;
|
|
18
|
+
/** Natural settle duration in ms (use as animation/transition duration). */
|
|
19
|
+
duration: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Damped harmonic oscillator sampled into a CSS `linear()` timing function.
|
|
23
|
+
* Settles when displacement and velocity drop below threshold.
|
|
24
|
+
*/
|
|
25
|
+
export declare function spring(opts?: SpringOptions): SpringEasing;
|
|
26
|
+
export declare const durations: {
|
|
27
|
+
readonly instant: 50;
|
|
28
|
+
readonly fast: 120;
|
|
29
|
+
readonly normal: 200;
|
|
30
|
+
readonly moderate: 280;
|
|
31
|
+
readonly slow: 400;
|
|
32
|
+
readonly slower: 600;
|
|
33
|
+
};
|
|
34
|
+
export declare const easings: {
|
|
35
|
+
readonly standard: "cubic-bezier(0.2, 0, 0, 1)";
|
|
36
|
+
readonly decelerate: "cubic-bezier(0, 0, 0, 1)";
|
|
37
|
+
readonly accelerate: "cubic-bezier(0.3, 0, 1, 1)";
|
|
38
|
+
readonly emphasized: "cubic-bezier(0.3, 0, 0, 1.0)";
|
|
39
|
+
readonly spring: string;
|
|
40
|
+
readonly bounce: string;
|
|
41
|
+
readonly gentle: string;
|
|
42
|
+
};
|
|
43
|
+
/** CSS declarations for motion tokens (scaled by --pm-motion-scale: 0 | 0.5 | 1). */
|
|
44
|
+
export declare function motionDeclarations(): string[];
|
|
45
|
+
export type MotionLevel = 'none' | 'reduced' | 'full';
|
|
46
|
+
/** Effective motion level for an element (data-motion attribute wins, then OS preference). */
|
|
47
|
+
export declare function motionLevel(el?: Element): MotionLevel;
|
|
48
|
+
/**
|
|
49
|
+
* Run a DOM update inside a same-document View Transition when supported and motion is allowed.
|
|
50
|
+
* Always applies the update (final state preserved) even without support.
|
|
51
|
+
*/
|
|
52
|
+
export declare function withViewTransition(update: () => void | Promise<void>, opts?: {
|
|
53
|
+
types?: string[];
|
|
54
|
+
}): Promise<void>;
|
|
55
|
+
export type Preset = 'fade' | 'scale' | 'slide-up' | 'slide-down' | 'slide-start' | 'slide-end' | 'blur' | 'pop';
|
|
56
|
+
/**
|
|
57
|
+
* Play a preset with the Web Animations API. Reduced motion → opacity-only; none → no-op.
|
|
58
|
+
* Final state is always the element's natural style (fill: none), so content is never left hidden.
|
|
59
|
+
*/
|
|
60
|
+
export declare function animate(el: Element, preset: Preset, opts?: {
|
|
61
|
+
duration?: number;
|
|
62
|
+
delay?: number;
|
|
63
|
+
easing?: string;
|
|
64
|
+
reverse?: boolean;
|
|
65
|
+
}): Animation | null;
|
|
66
|
+
/** Stagger a preset across children. */
|
|
67
|
+
export declare function stagger(parent: Element, preset?: Preset, step?: number): Animation[];
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @prismora/motion — motion tokens, physics-based CSS `linear()` easings and small helpers.
|
|
3
|
+
* All helpers respect `prefers-reduced-motion` and the `data-motion` attribute.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Damped harmonic oscillator sampled into a CSS `linear()` timing function.
|
|
7
|
+
* Settles when displacement and velocity drop below threshold.
|
|
8
|
+
*/
|
|
9
|
+
export function spring(opts = {}) {
|
|
10
|
+
const { stiffness = 170, damping = 18, mass = 1, samples = 40 } = opts;
|
|
11
|
+
const dt = 1 / 600;
|
|
12
|
+
let x = 0;
|
|
13
|
+
let v = 0;
|
|
14
|
+
const points = [];
|
|
15
|
+
let t = 0;
|
|
16
|
+
let settledFor = 0;
|
|
17
|
+
// Integrate until settled (max 3s).
|
|
18
|
+
const all = [];
|
|
19
|
+
while (t < 3) {
|
|
20
|
+
const a = (-stiffness * (x - 1) - damping * v) / mass;
|
|
21
|
+
v += a * dt;
|
|
22
|
+
x += v * dt;
|
|
23
|
+
t += dt;
|
|
24
|
+
all.push(x);
|
|
25
|
+
if (Math.abs(x - 1) < 0.001 && Math.abs(v) < 0.01) {
|
|
26
|
+
settledFor += dt;
|
|
27
|
+
if (settledFor > 0.05)
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
else
|
|
31
|
+
settledFor = 0;
|
|
32
|
+
}
|
|
33
|
+
for (let i = 0; i < samples; i++) {
|
|
34
|
+
const idx = Math.round((i / (samples - 1)) * (all.length - 1));
|
|
35
|
+
points.push(all[idx] ?? 1);
|
|
36
|
+
}
|
|
37
|
+
points[points.length - 1] = 1;
|
|
38
|
+
const easing = `linear(${[0, ...points.slice(1)].map((p) => Math.round(p * 1000) / 1000).join(', ')})`;
|
|
39
|
+
return { easing, duration: Math.round(t * 1000) };
|
|
40
|
+
}
|
|
41
|
+
export const durations = {
|
|
42
|
+
instant: 50,
|
|
43
|
+
fast: 120,
|
|
44
|
+
normal: 200,
|
|
45
|
+
moderate: 280,
|
|
46
|
+
slow: 400,
|
|
47
|
+
slower: 600,
|
|
48
|
+
};
|
|
49
|
+
export const easings = {
|
|
50
|
+
standard: 'cubic-bezier(0.2, 0, 0, 1)',
|
|
51
|
+
decelerate: 'cubic-bezier(0, 0, 0, 1)',
|
|
52
|
+
accelerate: 'cubic-bezier(0.3, 0, 1, 1)',
|
|
53
|
+
emphasized: 'cubic-bezier(0.3, 0, 0, 1.0)',
|
|
54
|
+
spring: spring({ stiffness: 260, damping: 22 }).easing,
|
|
55
|
+
bounce: spring({ stiffness: 300, damping: 12 }).easing,
|
|
56
|
+
gentle: spring({ stiffness: 120, damping: 18 }).easing,
|
|
57
|
+
};
|
|
58
|
+
/** CSS declarations for motion tokens (scaled by --pm-motion-scale: 0 | 0.5 | 1). */
|
|
59
|
+
export function motionDeclarations() {
|
|
60
|
+
const d = [];
|
|
61
|
+
for (const [k, v] of Object.entries(durations))
|
|
62
|
+
d.push(`--pm-duration-${k}: calc(${v}ms * var(--pm-motion-scale, 1));`);
|
|
63
|
+
for (const [k, v] of Object.entries(easings))
|
|
64
|
+
d.push(`--pm-ease-${k}: ${v};`);
|
|
65
|
+
return d;
|
|
66
|
+
}
|
|
67
|
+
/** Effective motion level for an element (data-motion attribute wins, then OS preference). */
|
|
68
|
+
export function motionLevel(el = document.documentElement) {
|
|
69
|
+
const attr = el.closest('[data-motion]')?.getAttribute('data-motion');
|
|
70
|
+
if (attr === 'none' || attr === 'reduced' || attr === 'full')
|
|
71
|
+
return attr;
|
|
72
|
+
return matchMedia('(prefers-reduced-motion: reduce)').matches ? 'reduced' : 'full';
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Run a DOM update inside a same-document View Transition when supported and motion is allowed.
|
|
76
|
+
* Always applies the update (final state preserved) even without support.
|
|
77
|
+
*/
|
|
78
|
+
export async function withViewTransition(update, opts = {}) {
|
|
79
|
+
const doc = document;
|
|
80
|
+
if (!doc.startViewTransition || motionLevel() !== 'full') {
|
|
81
|
+
await update();
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
const vt = opts.types
|
|
86
|
+
? doc.startViewTransition({ update, types: opts.types })
|
|
87
|
+
: doc.startViewTransition(update);
|
|
88
|
+
await vt.finished;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
await update();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const KEYFRAMES = {
|
|
95
|
+
fade: [{ opacity: 0 }, { opacity: 1 }],
|
|
96
|
+
scale: [{ opacity: 0, scale: '0.96' }, { opacity: 1, scale: '1' }],
|
|
97
|
+
pop: [{ opacity: 0, scale: '0.85' }, { opacity: 1, scale: '1' }],
|
|
98
|
+
'slide-up': [{ opacity: 0, translate: '0 12px' }, { opacity: 1, translate: '0 0' }],
|
|
99
|
+
'slide-down': [{ opacity: 0, translate: '0 -12px' }, { opacity: 1, translate: '0 0' }],
|
|
100
|
+
'slide-start': [{ opacity: 0, translate: '-16px 0' }, { opacity: 1, translate: '0 0' }],
|
|
101
|
+
'slide-end': [{ opacity: 0, translate: '16px 0' }, { opacity: 1, translate: '0 0' }],
|
|
102
|
+
blur: [{ opacity: 0, filter: 'blur(8px)' }, { opacity: 1, filter: 'blur(0)' }],
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Play a preset with the Web Animations API. Reduced motion → opacity-only; none → no-op.
|
|
106
|
+
* Final state is always the element's natural style (fill: none), so content is never left hidden.
|
|
107
|
+
*/
|
|
108
|
+
export function animate(el, preset, opts = {}) {
|
|
109
|
+
const level = motionLevel(el);
|
|
110
|
+
if (level === 'none')
|
|
111
|
+
return null;
|
|
112
|
+
const frames = level === 'reduced' ? KEYFRAMES.fade : KEYFRAMES[preset];
|
|
113
|
+
return el.animate(opts.reverse ? [...frames].reverse() : frames, {
|
|
114
|
+
duration: opts.duration ?? durations.moderate,
|
|
115
|
+
delay: opts.delay ?? 0,
|
|
116
|
+
easing: opts.easing ?? (preset === 'pop' ? easings.spring : easings.standard),
|
|
117
|
+
fill: opts.reverse ? 'forwards' : 'none',
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/** Stagger a preset across children. */
|
|
121
|
+
export function stagger(parent, preset = 'slide-up', step = 40) {
|
|
122
|
+
return [...parent.children]
|
|
123
|
+
.map((child, i) => animate(child, preset, { delay: i * step }))
|
|
124
|
+
.filter((a) => a !== null);
|
|
125
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismora/motion",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Motion tokens, spring linear() easings, view transition and animation helpers.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"prismora",
|
|
7
|
+
"design-system",
|
|
8
|
+
"animation",
|
|
9
|
+
"easing"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://prismora.design",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/axxra/prismora/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/axxra/prismora.git",
|
|
18
|
+
"directory": "packages/motion"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Aravind Kumar",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
}
|
|
38
|
+
}
|