@praxisjs/motion 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/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @praxisjs/motion
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - aaf7dab: Initial beta release
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [aaf7dab]
12
+ - @praxisjs/core@0.1.0
13
+ - @praxisjs/shared@0.1.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026-present Mateus Martins
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.
@@ -0,0 +1,3 @@
1
+ import { type TweenOptions } from "./tween";
2
+ export declare function Animate(options?: TweenOptions): (target: object, propertyKey: string) => void;
3
+ //# sourceMappingURL=decorators.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorators.d.ts","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,YAAY,EAAc,MAAM,SAAS,CAAC;AAE/D,wBAAgB,OAAO,CAAC,OAAO,GAAE,YAAiB,IAC/B,QAAQ,MAAM,EAAE,aAAa,MAAM,KAAG,IAAI,CAiB5D"}
@@ -0,0 +1,21 @@
1
+ import { tween } from "./tween";
2
+ export function Animate(options = {}) {
3
+ return function (target, propertyKey) {
4
+ const tweens = new WeakMap();
5
+ const original = Object.getOwnPropertyDescriptor(target, propertyKey);
6
+ Object.defineProperty(target, propertyKey, {
7
+ get() {
8
+ return tweens.get(this)?.value() ?? 0;
9
+ },
10
+ set(value) {
11
+ if (!tweens.has(this))
12
+ tweens.set(this, tween(value, value, options));
13
+ tweens.get(this)?.target.set(value);
14
+ original?.set?.call(this, value);
15
+ },
16
+ enumerable: true,
17
+ configurable: true,
18
+ });
19
+ };
20
+ }
21
+ //# sourceMappingURL=decorators.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorators.js","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAiC,MAAM,SAAS,CAAC;AAE/D,MAAM,UAAU,OAAO,CAAC,UAAwB,EAAE;IAChD,OAAO,UAAU,MAAc,EAAE,WAAmB;QAClD,MAAM,MAAM,GAAG,IAAI,OAAO,EAAiB,CAAC;QAC5C,MAAM,QAAQ,GAAG,MAAM,CAAC,wBAAwB,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;QAEtE,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE;YACzC,GAAG;gBACD,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;YACxC,CAAC;YACD,GAAG,CAAe,KAAa;gBAC7B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC;oBAAE,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;gBACtE,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;gBACpC,QAAQ,EAAE,GAAG,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YACnC,CAAC;YACD,UAAU,EAAE,IAAI;YAChB,YAAY,EAAE,IAAI;SACnB,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,12 @@
1
+ export declare const easings: {
2
+ linear: (t: number) => number;
3
+ easeIn: (t: number) => number;
4
+ easeOut: (t: number) => number;
5
+ easeInOut: (t: number) => number;
6
+ easeInCubic: (t: number) => number;
7
+ bounce: (t: number) => number;
8
+ elastic: (t: number) => number;
9
+ };
10
+ export type Easing = keyof typeof easings | ((t: number) => number);
11
+ export declare function resolveEasing(easing: Easing): (t: number) => number;
12
+ //# sourceMappingURL=easings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"easings.d.ts","sourceRoot":"","sources":["../src/easings.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO;gBACN,MAAM;gBACN,MAAM;iBACL,MAAM;mBACJ,MAAM;qBACJ,MAAM;gBACX,MAAM;iBAQL,MAAM;CAM4B,CAAC;AAElD,MAAM,MAAM,MAAM,GAAG,MAAM,OAAO,OAAO,GAAG,CAAC,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;AAEpE,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAEnE"}
@@ -0,0 +1,26 @@
1
+ export const easings = {
2
+ linear: (t) => t,
3
+ easeIn: (t) => t * t,
4
+ easeOut: (t) => 1 - (1 - t) ** 2,
5
+ easeInOut: (t) => (t < 0.5 ? 2 * t * t : 1 - (-2 * t + 2) ** 2 / 2),
6
+ easeInCubic: (t) => t * t * t,
7
+ bounce: (t) => {
8
+ const n1 = 7.5625, d1 = 2.75;
9
+ if (t < 1 / d1)
10
+ return n1 * t * t;
11
+ if (t < 2 / d1)
12
+ return n1 * (t -= 1.5 / d1) * t + 0.75;
13
+ if (t < 2.5 / d1)
14
+ return n1 * (t -= 2.25 / d1) * t + 0.9375;
15
+ return n1 * (t -= 2.625 / d1) * t + 0.984375;
16
+ },
17
+ elastic: (t) => {
18
+ if (t === 0 || t === 1)
19
+ return t;
20
+ return (-(2 ** (10 * t - 10)) * Math.sin((t * 10 - 10.75) * ((2 * Math.PI) / 3)));
21
+ },
22
+ };
23
+ export function resolveEasing(easing) {
24
+ return typeof easing === "function" ? easing : easings[easing];
25
+ }
26
+ //# sourceMappingURL=easings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"easings.js","sourceRoot":"","sources":["../src/easings.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,MAAM,EAAE,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC;IACxB,MAAM,EAAE,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC;IAC5B,OAAO,EAAE,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;IACxC,SAAS,EAAE,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC3E,WAAW,EAAE,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC;IACrC,MAAM,EAAE,CAAC,CAAS,EAAE,EAAE;QACpB,MAAM,EAAE,GAAG,MAAM,EACf,EAAE,GAAG,IAAI,CAAC;QACZ,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;YAAE,OAAO,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;QAClC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;YAAE,OAAO,EAAE,GAAG,CAAC,CAAC,IAAI,GAAG,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;QACvD,IAAI,CAAC,GAAG,GAAG,GAAG,EAAE;YAAE,OAAO,EAAE,GAAG,CAAC,CAAC,IAAI,IAAI,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC;QAC5D,OAAO,EAAE,GAAG,CAAC,CAAC,IAAI,KAAK,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC;IAC/C,CAAC;IACD,OAAO,EAAE,CAAC,CAAS,EAAE,EAAE;QACrB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;YAAE,OAAO,CAAC,CAAC;QACjC,OAAO,CACL,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CACzE,CAAC;IACJ,CAAC;CAC8C,CAAC;AAIlD,MAAM,UAAU,aAAa,CAAC,MAAc;IAC1C,OAAO,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;AACjE,CAAC"}
@@ -0,0 +1,12 @@
1
+ export { easings, resolveEasing } from "./easings";
2
+ export type { Easing } from "./easings";
3
+ export { tween } from "./tween";
4
+ export type { Tween, TweenOptions } from "./tween";
5
+ export { spring } from "./spring";
6
+ export type { SpringOptions } from "./spring";
7
+ export { useMotion } from "./use-motion";
8
+ export type { MotionKeyframes } from "./use-motion";
9
+ export { createTransition } from "./transition";
10
+ export type { TransitionOptions } from "./transition";
11
+ export { Animate } from "./decorators";
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AACnD,YAAY,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAExC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,YAAY,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEnD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,YAAY,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEtD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export { easings, resolveEasing } from "./easings";
2
+ export { tween } from "./tween";
3
+ export { spring } from "./spring";
4
+ export { useMotion } from "./use-motion";
5
+ export { createTransition } from "./transition";
6
+ export { Animate } from "./decorators";
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAGnD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAGhC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGlC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGhD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC"}
@@ -0,0 +1,12 @@
1
+ export interface SpringOptions {
2
+ stiffness?: number;
3
+ damping?: number;
4
+ mass?: number;
5
+ precision?: number;
6
+ }
7
+ export declare function spring(initial: number, options?: SpringOptions): {
8
+ value: import("@praxisjs/shared").Computed<number>;
9
+ target: import("@praxisjs/shared").Signal<number>;
10
+ stop(): void;
11
+ };
12
+ //# sourceMappingURL=spring.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spring.d.ts","sourceRoot":"","sources":["../src/spring.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,aAAa;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,wBAAgB,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,aAAkB;;;;EA2ClE"}
package/dist/spring.js ADDED
@@ -0,0 +1,38 @@
1
+ import { signal, computed, effect } from "@praxisjs/core";
2
+ export function spring(initial, options = {}) {
3
+ const { stiffness = 0.15, damping = 0.8, mass = 1, precision = 0.001, } = options;
4
+ const _value = signal(initial);
5
+ const _target = signal(initial);
6
+ let velocity = 0;
7
+ let raf;
8
+ function tick() {
9
+ const current = _value();
10
+ const target = _target();
11
+ const force = (target - current) * stiffness;
12
+ velocity = (velocity + force / mass) * damping;
13
+ const next = current + velocity;
14
+ _value.set(next);
15
+ if (Math.abs(target - next) > precision || Math.abs(velocity) > precision) {
16
+ raf = requestAnimationFrame(tick);
17
+ }
18
+ else {
19
+ _value.set(target);
20
+ velocity = 0;
21
+ }
22
+ }
23
+ effect(() => {
24
+ void _target();
25
+ if (raf)
26
+ cancelAnimationFrame(raf);
27
+ raf = requestAnimationFrame(tick);
28
+ });
29
+ return {
30
+ value: computed(() => _value()),
31
+ target: _target,
32
+ stop() {
33
+ if (raf)
34
+ cancelAnimationFrame(raf);
35
+ },
36
+ };
37
+ }
38
+ //# sourceMappingURL=spring.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spring.js","sourceRoot":"","sources":["../src/spring.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAS1D,MAAM,UAAU,MAAM,CAAC,OAAe,EAAE,UAAyB,EAAE;IACjE,MAAM,EACJ,SAAS,GAAG,IAAI,EAChB,OAAO,GAAG,GAAG,EACb,IAAI,GAAG,CAAC,EACR,SAAS,GAAG,KAAK,GAClB,GAAG,OAAO,CAAC;IAEZ,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IAC/B,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IAChC,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,IAAI,GAAuB,CAAC;IAE5B,SAAS,IAAI;QACX,MAAM,OAAO,GAAG,MAAM,EAAE,CAAC;QACzB,MAAM,MAAM,GAAG,OAAO,EAAE,CAAC;QACzB,MAAM,KAAK,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC,GAAG,SAAS,CAAC;QAC7C,QAAQ,GAAG,CAAC,QAAQ,GAAG,KAAK,GAAG,IAAI,CAAC,GAAG,OAAO,CAAC;QAE/C,MAAM,IAAI,GAAG,OAAO,GAAG,QAAQ,CAAC;QAChC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAEjB,IAAI,IAAI,CAAC,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,SAAS,IAAI,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,SAAS,EAAE,CAAC;YAC1E,GAAG,GAAG,qBAAqB,CAAC,IAAI,CAAC,CAAC;QACpC,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YACnB,QAAQ,GAAG,CAAC,CAAC;QACf,CAAC;IACH,CAAC;IAED,MAAM,CAAC,GAAG,EAAE;QACV,KAAK,OAAO,EAAE,CAAC;QACf,IAAI,GAAG;YAAE,oBAAoB,CAAC,GAAG,CAAC,CAAC;QACnC,GAAG,GAAG,qBAAqB,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,KAAK,EAAE,QAAQ,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC;QAC/B,MAAM,EAAE,OAAO;QACf,IAAI;YACF,IAAI,GAAG;gBAAE,oBAAoB,CAAC,GAAG,CAAC,CAAC;QACrC,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,11 @@
1
+ export interface TransitionOptions {
2
+ name?: string;
3
+ duration?: number;
4
+ onEnter?: (el: HTMLElement) => void;
5
+ onLeave?: (el: HTMLElement) => void;
6
+ }
7
+ export declare function createTransition(options?: TransitionOptions): {
8
+ enter(el: HTMLElement): Promise<void>;
9
+ leave(el: HTMLElement): Promise<void>;
10
+ };
11
+ //# sourceMappingURL=transition.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transition.d.ts","sourceRoot":"","sources":["../src/transition.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,iBAAiB;IAChC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,CAAC,EAAE,EAAE,WAAW,KAAK,IAAI,CAAC;IACpC,OAAO,CAAC,EAAE,CAAC,EAAE,EAAE,WAAW,KAAK,IAAI,CAAC;CACrC;AAED,wBAAgB,gBAAgB,CAAC,OAAO,GAAE,iBAAsB;cAIlD,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;cAc3B,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;EAexC"}
@@ -0,0 +1,30 @@
1
+ export function createTransition(options = {}) {
2
+ const { name = "transition", duration = 300, onEnter, onLeave } = options;
3
+ return {
4
+ enter(el) {
5
+ onEnter?.(el);
6
+ el.classList.add(`${name}-enter-from`);
7
+ requestAnimationFrame(() => {
8
+ el.classList.remove(`${name}-enter-from`);
9
+ el.classList.add(`${name}-enter-to`);
10
+ });
11
+ return new Promise((res) => setTimeout(() => {
12
+ el.classList.remove(`${name}-enter-to`);
13
+ res();
14
+ }, duration));
15
+ },
16
+ leave(el) {
17
+ onLeave?.(el);
18
+ el.classList.add(`${name}-leave-from`);
19
+ requestAnimationFrame(() => {
20
+ el.classList.remove(`${name}-leave-from`);
21
+ el.classList.add(`${name}-leave-to`);
22
+ });
23
+ return new Promise((res) => setTimeout(() => {
24
+ el.classList.remove(`${name}-leave-to`);
25
+ res();
26
+ }, duration));
27
+ },
28
+ };
29
+ }
30
+ //# sourceMappingURL=transition.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transition.js","sourceRoot":"","sources":["../src/transition.ts"],"names":[],"mappings":"AAOA,MAAM,UAAU,gBAAgB,CAAC,UAA6B,EAAE;IAC9D,MAAM,EAAE,IAAI,GAAG,YAAY,EAAE,QAAQ,GAAG,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAE1E,OAAO;QACL,KAAK,CAAC,EAAe;YACnB,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;YACd,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,IAAI,aAAa,CAAC,CAAC;YACvC,qBAAqB,CAAC,GAAG,EAAE;gBACzB,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,IAAI,aAAa,CAAC,CAAC;gBAC1C,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,IAAI,WAAW,CAAC,CAAC;YACvC,CAAC,CAAC,CAAC;YACH,OAAO,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CACzB,UAAU,CAAC,GAAG,EAAE;gBACd,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,IAAI,WAAW,CAAC,CAAC;gBACxC,GAAG,EAAE,CAAC;YACR,CAAC,EAAE,QAAQ,CAAC,CACb,CAAC;QACJ,CAAC;QACD,KAAK,CAAC,EAAe;YACnB,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;YACd,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,IAAI,aAAa,CAAC,CAAC;YACvC,qBAAqB,CAAC,GAAG,EAAE;gBACzB,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,IAAI,aAAa,CAAC,CAAC;gBAC1C,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,IAAI,WAAW,CAAC,CAAC;YACvC,CAAC,CAAC,CAAC;YACH,OAAO,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CACzB,UAAU,CAAC,GAAG,EAAE;gBACd,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,IAAI,WAAW,CAAC,CAAC;gBACxC,GAAG,EAAE,CAAC;YACR,CAAC,EAAE,QAAQ,CAAC,CACb,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { Computed, Signal } from "@praxisjs/shared";
2
+ import { type Easing } from "./easings";
3
+ export interface TweenOptions {
4
+ duration?: number;
5
+ easing?: Easing;
6
+ delay?: number;
7
+ }
8
+ export interface Tween {
9
+ value: Computed<number>;
10
+ target: Signal<number>;
11
+ playing: Computed<boolean>;
12
+ progress: Computed<number>;
13
+ stop(): void;
14
+ reset(): void;
15
+ }
16
+ export declare function tween(from: number, to: number, options?: TweenOptions): Tween;
17
+ //# sourceMappingURL=tween.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tween.d.ts","sourceRoot":"","sources":["../src/tween.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAEzD,OAAO,EAAiB,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAEvD,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,KAAK;IACpB,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IACvB,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC;IAC3B,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC3B,IAAI,IAAI,IAAI,CAAC;IACb,KAAK,IAAI,IAAI,CAAC;CACf;AAED,wBAAgB,KAAK,CACnB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,OAAO,GAAE,YAAiB,GACzB,KAAK,CAyDP"}
package/dist/tween.js ADDED
@@ -0,0 +1,57 @@
1
+ import { signal, computed, effect } from "@praxisjs/core";
2
+ import { resolveEasing } from "./easings";
3
+ export function tween(from, to, options = {}) {
4
+ const { duration = 300, easing = "easeOut", delay = 0 } = options;
5
+ const easeFn = resolveEasing(easing);
6
+ const _value = signal(from);
7
+ const _target = signal(to);
8
+ const _progress = signal(0);
9
+ const _playing = signal(false);
10
+ let raf;
11
+ let startTime;
12
+ let startValue = from;
13
+ function animate(timestamp) {
14
+ startTime ??= timestamp + delay;
15
+ const elapsed = Math.max(0, timestamp - startTime);
16
+ const t = Math.min(elapsed / duration, 1);
17
+ _progress.set(t);
18
+ _value.set(startValue + (_target() - startValue) * easeFn(t));
19
+ if (t < 1) {
20
+ raf = requestAnimationFrame(animate);
21
+ }
22
+ else {
23
+ _value.set(_target());
24
+ _playing.set(false);
25
+ }
26
+ }
27
+ function start() {
28
+ if (raf)
29
+ cancelAnimationFrame(raf);
30
+ startValue = _value();
31
+ startTime = undefined;
32
+ _playing.set(true);
33
+ raf = requestAnimationFrame(animate);
34
+ }
35
+ effect(() => {
36
+ void _target();
37
+ start();
38
+ });
39
+ return {
40
+ value: computed(() => _value()),
41
+ target: _target,
42
+ playing: computed(() => _playing()),
43
+ progress: computed(() => _progress()),
44
+ stop() {
45
+ if (raf)
46
+ cancelAnimationFrame(raf);
47
+ _playing.set(false);
48
+ },
49
+ reset() {
50
+ if (raf)
51
+ cancelAnimationFrame(raf);
52
+ _value.set(from);
53
+ _progress.set(0);
54
+ },
55
+ };
56
+ }
57
+ //# sourceMappingURL=tween.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tween.js","sourceRoot":"","sources":["../src/tween.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAG1D,OAAO,EAAE,aAAa,EAAe,MAAM,WAAW,CAAC;AAiBvD,MAAM,UAAU,KAAK,CACnB,IAAY,EACZ,EAAU,EACV,UAAwB,EAAE;IAE1B,MAAM,EAAE,QAAQ,GAAG,GAAG,EAAE,MAAM,GAAG,SAAS,EAAE,KAAK,GAAG,CAAC,EAAE,GAAG,OAAO,CAAC;IAClE,MAAM,MAAM,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC;IAErC,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAC5B,MAAM,OAAO,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;IAC3B,MAAM,SAAS,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAC5B,MAAM,QAAQ,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAE/B,IAAI,GAAuB,CAAC;IAC5B,IAAI,SAA6B,CAAC;IAClC,IAAI,UAAU,GAAG,IAAI,CAAC;IAEtB,SAAS,OAAO,CAAC,SAAiB;QAChC,SAAS,KAAK,SAAS,GAAG,KAAK,CAAC;QAChC,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,SAAS,GAAG,SAAS,CAAC,CAAC;QACnD,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,QAAQ,EAAE,CAAC,CAAC,CAAC;QAE1C,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACjB,MAAM,CAAC,GAAG,CAAC,UAAU,GAAG,CAAC,OAAO,EAAE,GAAG,UAAU,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QAE9D,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YACV,GAAG,GAAG,qBAAqB,CAAC,OAAO,CAAC,CAAC;QACvC,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;YACtB,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;IAED,SAAS,KAAK;QACZ,IAAI,GAAG;YAAE,oBAAoB,CAAC,GAAG,CAAC,CAAC;QACnC,UAAU,GAAG,MAAM,EAAE,CAAC;QACtB,SAAS,GAAG,SAAS,CAAC;QACtB,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACnB,GAAG,GAAG,qBAAqB,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IAED,MAAM,CAAC,GAAG,EAAE;QACV,KAAK,OAAO,EAAE,CAAC;QACf,KAAK,EAAE,CAAC;IACV,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,KAAK,EAAE,QAAQ,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC;QAC/B,MAAM,EAAE,OAAO;QACf,OAAO,EAAE,QAAQ,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC;QACnC,QAAQ,EAAE,QAAQ,CAAC,GAAG,EAAE,CAAC,SAAS,EAAE,CAAC;QACrC,IAAI;YACF,IAAI,GAAG;gBAAE,oBAAoB,CAAC,GAAG,CAAC,CAAC;YACnC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;QACD,KAAK;YACH,IAAI,GAAG;gBAAE,oBAAoB,CAAC,GAAG,CAAC,CAAC;YACnC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACjB,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACnB,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,20 @@
1
+ import { type Easing } from "./easings";
2
+ export interface MotionKeyframes {
3
+ opacity?: [number, number];
4
+ x?: [number, number];
5
+ y?: [number, number];
6
+ scale?: [number, number];
7
+ rotate?: [number, number];
8
+ duration?: number;
9
+ easing?: Easing;
10
+ delay?: number;
11
+ onComplete?: () => void;
12
+ }
13
+ export declare function useMotion(ref: {
14
+ current: HTMLElement | null;
15
+ }): {
16
+ animate: (keyframes: MotionKeyframes) => () => void;
17
+ enter: (kf: MotionKeyframes) => () => void;
18
+ exit: (kf: MotionKeyframes) => () => void;
19
+ };
20
+ //# sourceMappingURL=use-motion.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-motion.d.ts","sourceRoot":"","sources":["../src/use-motion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAEvD,MAAM,WAAW,eAAe;IAC9B,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3B,CAAC,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrB,CAAC,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrB,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzB,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;CACzB;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE;IAAE,OAAO,EAAE,WAAW,GAAG,IAAI,CAAA;CAAE;yBAChC,eAAe,KAAG,MAAM,IAAI;gBAgE1C,eAAe,WAhEuB,IAAI;eAiE3C,eAAe,WAjEwB,IAAI;EAyEzD"}
@@ -0,0 +1,58 @@
1
+ import { resolveEasing } from "./easings";
2
+ export function useMotion(ref) {
3
+ function animate(keyframes) {
4
+ const el = ref.current;
5
+ if (!el)
6
+ return () => undefined;
7
+ const safeEl = el;
8
+ const { opacity, x, y, scale, rotate, duration = 300, easing = "easeOut", delay = 0, onComplete, } = keyframes;
9
+ const easeFn = resolveEasing(easing);
10
+ let raf;
11
+ let startTime;
12
+ let cancelled = false;
13
+ function frame(ts) {
14
+ if (cancelled)
15
+ return;
16
+ startTime ??= ts + delay;
17
+ const elapsed = Math.max(0, ts - startTime);
18
+ const t = Math.min(elapsed / duration, 1);
19
+ const e = easeFn(t);
20
+ const transforms = [];
21
+ if (x)
22
+ transforms.push(`translateX(${String(x[0] + (x[1] - x[0]) * e)}px)`);
23
+ if (y)
24
+ transforms.push(`translateY(${String(y[0] + (y[1] - y[0]) * e)}px)`);
25
+ if (scale)
26
+ transforms.push(`scale(${String(scale[0] + (scale[1] - scale[0]) * e)})`);
27
+ if (rotate)
28
+ transforms.push(`rotate(${String(rotate[0] + (rotate[1] - rotate[0]) * e)}deg)`);
29
+ if (transforms.length)
30
+ safeEl.style.transform = transforms.join(" ");
31
+ if (opacity)
32
+ safeEl.style.opacity = String(opacity[0] + (opacity[1] - opacity[0]) * e);
33
+ if (t < 1) {
34
+ raf = requestAnimationFrame(frame);
35
+ }
36
+ else {
37
+ onComplete?.();
38
+ }
39
+ }
40
+ raf = requestAnimationFrame(frame);
41
+ return () => {
42
+ cancelled = true;
43
+ if (raf)
44
+ cancelAnimationFrame(raf);
45
+ };
46
+ }
47
+ return {
48
+ animate,
49
+ enter: (kf) => animate(kf),
50
+ exit: (kf) => animate({
51
+ ...kf,
52
+ opacity: kf.opacity ? [kf.opacity[1], kf.opacity[0]] : undefined,
53
+ x: kf.x ? [kf.x[1], kf.x[0]] : undefined,
54
+ y: kf.y ? [kf.y[1], kf.y[0]] : undefined,
55
+ }),
56
+ };
57
+ }
58
+ //# sourceMappingURL=use-motion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-motion.js","sourceRoot":"","sources":["../src/use-motion.ts"],"names":[],"mappings":"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"}
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@praxisjs/motion",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "types": "./dist/index.d.ts"
11
+ }
12
+ },
13
+ "devDependencies": {
14
+ "typescript": "^5.9.3"
15
+ },
16
+ "dependencies": {
17
+ "@praxisjs/core": "0.1.0",
18
+ "@praxisjs/shared": "0.1.0"
19
+ },
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "dev": "tsc --watch"
23
+ }
24
+ }
@@ -0,0 +1,21 @@
1
+ import { tween, type TweenOptions, type Tween } from "./tween";
2
+
3
+ export function Animate(options: TweenOptions = {}) {
4
+ return function (target: object, propertyKey: string): void {
5
+ const tweens = new WeakMap<object, Tween>();
6
+ const original = Object.getOwnPropertyDescriptor(target, propertyKey);
7
+
8
+ Object.defineProperty(target, propertyKey, {
9
+ get(this: object): number {
10
+ return tweens.get(this)?.value() ?? 0;
11
+ },
12
+ set(this: object, value: number): void {
13
+ if (!tweens.has(this)) tweens.set(this, tween(value, value, options));
14
+ tweens.get(this)?.target.set(value);
15
+ original?.set?.call(this, value);
16
+ },
17
+ enumerable: true,
18
+ configurable: true,
19
+ });
20
+ };
21
+ }
package/src/easings.ts ADDED
@@ -0,0 +1,27 @@
1
+ export const easings = {
2
+ linear: (t: number) => t,
3
+ easeIn: (t: number) => t * t,
4
+ easeOut: (t: number) => 1 - (1 - t) ** 2,
5
+ easeInOut: (t: number) => (t < 0.5 ? 2 * t * t : 1 - (-2 * t + 2) ** 2 / 2),
6
+ easeInCubic: (t: number) => t * t * t,
7
+ bounce: (t: number) => {
8
+ const n1 = 7.5625,
9
+ d1 = 2.75;
10
+ if (t < 1 / d1) return n1 * t * t;
11
+ if (t < 2 / d1) return n1 * (t -= 1.5 / d1) * t + 0.75;
12
+ if (t < 2.5 / d1) return n1 * (t -= 2.25 / d1) * t + 0.9375;
13
+ return n1 * (t -= 2.625 / d1) * t + 0.984375;
14
+ },
15
+ elastic: (t: number) => {
16
+ if (t === 0 || t === 1) return t;
17
+ return (
18
+ -(2 ** (10 * t - 10)) * Math.sin((t * 10 - 10.75) * ((2 * Math.PI) / 3))
19
+ );
20
+ },
21
+ } satisfies Record<string, (t: number) => number>;
22
+
23
+ export type Easing = keyof typeof easings | ((t: number) => number);
24
+
25
+ export function resolveEasing(easing: Easing): (t: number) => number {
26
+ return typeof easing === "function" ? easing : easings[easing];
27
+ }
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ export { easings, resolveEasing } from "./easings";
2
+ export type { Easing } from "./easings";
3
+
4
+ export { tween } from "./tween";
5
+ export type { Tween, TweenOptions } from "./tween";
6
+
7
+ export { spring } from "./spring";
8
+ export type { SpringOptions } from "./spring";
9
+
10
+ export { useMotion } from "./use-motion";
11
+ export type { MotionKeyframes } from "./use-motion";
12
+
13
+ export { createTransition } from "./transition";
14
+ export type { TransitionOptions } from "./transition";
15
+
16
+ export { Animate } from "./decorators";
package/src/spring.ts ADDED
@@ -0,0 +1,53 @@
1
+ import { signal, computed, effect } from "@praxisjs/core";
2
+
3
+ export interface SpringOptions {
4
+ stiffness?: number;
5
+ damping?: number;
6
+ mass?: number;
7
+ precision?: number;
8
+ }
9
+
10
+ export function spring(initial: number, options: SpringOptions = {}) {
11
+ const {
12
+ stiffness = 0.15,
13
+ damping = 0.8,
14
+ mass = 1,
15
+ precision = 0.001,
16
+ } = options;
17
+
18
+ const _value = signal(initial);
19
+ const _target = signal(initial);
20
+ let velocity = 0;
21
+ let raf: number | undefined;
22
+
23
+ function tick() {
24
+ const current = _value();
25
+ const target = _target();
26
+ const force = (target - current) * stiffness;
27
+ velocity = (velocity + force / mass) * damping;
28
+
29
+ const next = current + velocity;
30
+ _value.set(next);
31
+
32
+ if (Math.abs(target - next) > precision || Math.abs(velocity) > precision) {
33
+ raf = requestAnimationFrame(tick);
34
+ } else {
35
+ _value.set(target);
36
+ velocity = 0;
37
+ }
38
+ }
39
+
40
+ effect(() => {
41
+ void _target();
42
+ if (raf) cancelAnimationFrame(raf);
43
+ raf = requestAnimationFrame(tick);
44
+ });
45
+
46
+ return {
47
+ value: computed(() => _value()),
48
+ target: _target,
49
+ stop() {
50
+ if (raf) cancelAnimationFrame(raf);
51
+ },
52
+ };
53
+ }
@@ -0,0 +1,41 @@
1
+ export interface TransitionOptions {
2
+ name?: string;
3
+ duration?: number;
4
+ onEnter?: (el: HTMLElement) => void;
5
+ onLeave?: (el: HTMLElement) => void;
6
+ }
7
+
8
+ export function createTransition(options: TransitionOptions = {}) {
9
+ const { name = "transition", duration = 300, onEnter, onLeave } = options;
10
+
11
+ return {
12
+ enter(el: HTMLElement): Promise<void> {
13
+ onEnter?.(el);
14
+ el.classList.add(`${name}-enter-from`);
15
+ requestAnimationFrame(() => {
16
+ el.classList.remove(`${name}-enter-from`);
17
+ el.classList.add(`${name}-enter-to`);
18
+ });
19
+ return new Promise((res) =>
20
+ setTimeout(() => {
21
+ el.classList.remove(`${name}-enter-to`);
22
+ res();
23
+ }, duration),
24
+ );
25
+ },
26
+ leave(el: HTMLElement): Promise<void> {
27
+ onLeave?.(el);
28
+ el.classList.add(`${name}-leave-from`);
29
+ requestAnimationFrame(() => {
30
+ el.classList.remove(`${name}-leave-from`);
31
+ el.classList.add(`${name}-leave-to`);
32
+ });
33
+ return new Promise((res) =>
34
+ setTimeout(() => {
35
+ el.classList.remove(`${name}-leave-to`);
36
+ res();
37
+ }, duration),
38
+ );
39
+ },
40
+ };
41
+ }
package/src/tween.ts ADDED
@@ -0,0 +1,82 @@
1
+ import { signal, computed, effect } from "@praxisjs/core";
2
+ import type { Computed, Signal } from "@praxisjs/shared";
3
+
4
+ import { resolveEasing, type Easing } from "./easings";
5
+
6
+ export interface TweenOptions {
7
+ duration?: number;
8
+ easing?: Easing;
9
+ delay?: number;
10
+ }
11
+
12
+ export interface Tween {
13
+ value: Computed<number>;
14
+ target: Signal<number>;
15
+ playing: Computed<boolean>;
16
+ progress: Computed<number>;
17
+ stop(): void;
18
+ reset(): void;
19
+ }
20
+
21
+ export function tween(
22
+ from: number,
23
+ to: number,
24
+ options: TweenOptions = {},
25
+ ): Tween {
26
+ const { duration = 300, easing = "easeOut", delay = 0 } = options;
27
+ const easeFn = resolveEasing(easing);
28
+
29
+ const _value = signal(from);
30
+ const _target = signal(to);
31
+ const _progress = signal(0);
32
+ const _playing = signal(false);
33
+
34
+ let raf: number | undefined;
35
+ let startTime: number | undefined;
36
+ let startValue = from;
37
+
38
+ function animate(timestamp: number) {
39
+ startTime ??= timestamp + delay;
40
+ const elapsed = Math.max(0, timestamp - startTime);
41
+ const t = Math.min(elapsed / duration, 1);
42
+
43
+ _progress.set(t);
44
+ _value.set(startValue + (_target() - startValue) * easeFn(t));
45
+
46
+ if (t < 1) {
47
+ raf = requestAnimationFrame(animate);
48
+ } else {
49
+ _value.set(_target());
50
+ _playing.set(false);
51
+ }
52
+ }
53
+
54
+ function start() {
55
+ if (raf) cancelAnimationFrame(raf);
56
+ startValue = _value();
57
+ startTime = undefined;
58
+ _playing.set(true);
59
+ raf = requestAnimationFrame(animate);
60
+ }
61
+
62
+ effect(() => {
63
+ void _target();
64
+ start();
65
+ });
66
+
67
+ return {
68
+ value: computed(() => _value()),
69
+ target: _target,
70
+ playing: computed(() => _playing()),
71
+ progress: computed(() => _progress()),
72
+ stop() {
73
+ if (raf) cancelAnimationFrame(raf);
74
+ _playing.set(false);
75
+ },
76
+ reset() {
77
+ if (raf) cancelAnimationFrame(raf);
78
+ _value.set(from);
79
+ _progress.set(0);
80
+ },
81
+ };
82
+ }
@@ -0,0 +1,89 @@
1
+ import { resolveEasing, type Easing } from "./easings";
2
+
3
+ export interface MotionKeyframes {
4
+ opacity?: [number, number];
5
+ x?: [number, number];
6
+ y?: [number, number];
7
+ scale?: [number, number];
8
+ rotate?: [number, number];
9
+ duration?: number;
10
+ easing?: Easing;
11
+ delay?: number;
12
+ onComplete?: () => void;
13
+ }
14
+
15
+ export function useMotion(ref: { current: HTMLElement | null }) {
16
+ function animate(keyframes: MotionKeyframes): () => void {
17
+ const el: HTMLElement | null = ref.current;
18
+ if (!el) return () => undefined;
19
+ const safeEl = el;
20
+
21
+ const {
22
+ opacity,
23
+ x,
24
+ y,
25
+ scale,
26
+ rotate,
27
+ duration = 300,
28
+ easing = "easeOut",
29
+ delay = 0,
30
+ onComplete,
31
+ } = keyframes;
32
+ const easeFn = resolveEasing(easing);
33
+ let raf: number | undefined;
34
+ let startTime: number | undefined;
35
+ let cancelled = false;
36
+
37
+ function frame(ts: number) {
38
+ if (cancelled) return;
39
+ startTime ??= ts + delay;
40
+ const elapsed = Math.max(0, ts - startTime);
41
+ const t = Math.min(elapsed / duration, 1);
42
+ const e = easeFn(t);
43
+
44
+ const transforms: string[] = [];
45
+ if (x)
46
+ transforms.push(`translateX(${String(x[0] + (x[1] - x[0]) * e)}px)`);
47
+ if (y)
48
+ transforms.push(`translateY(${String(y[0] + (y[1] - y[0]) * e)}px)`);
49
+ if (scale)
50
+ transforms.push(
51
+ `scale(${String(scale[0] + (scale[1] - scale[0]) * e)})`,
52
+ );
53
+ if (rotate)
54
+ transforms.push(
55
+ `rotate(${String(rotate[0] + (rotate[1] - rotate[0]) * e)}deg)`,
56
+ );
57
+
58
+ if (transforms.length) safeEl.style.transform = transforms.join(" ");
59
+ if (opacity)
60
+ safeEl.style.opacity = String(
61
+ opacity[0] + (opacity[1] - opacity[0]) * e,
62
+ );
63
+
64
+ if (t < 1) {
65
+ raf = requestAnimationFrame(frame);
66
+ } else {
67
+ onComplete?.();
68
+ }
69
+ }
70
+
71
+ raf = requestAnimationFrame(frame);
72
+ return () => {
73
+ cancelled = true;
74
+ if (raf) cancelAnimationFrame(raf);
75
+ };
76
+ }
77
+
78
+ return {
79
+ animate,
80
+ enter: (kf: MotionKeyframes) => animate(kf),
81
+ exit: (kf: MotionKeyframes) =>
82
+ animate({
83
+ ...kf,
84
+ opacity: kf.opacity ? [kf.opacity[1], kf.opacity[0]] : undefined,
85
+ x: kf.x ? [kf.x[1], kf.x[0]] : undefined,
86
+ y: kf.y ? [kf.y[1], kf.y[0]] : undefined,
87
+ }),
88
+ };
89
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src"
6
+ },
7
+ "include": ["src"]
8
+ }