@humanspeak/svelte-motion 0.3.1 → 0.3.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/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/utils/cycle.d.ts +35 -0
- package/dist/utils/cycle.js +48 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export { anticipate, backIn, backInOut, backOut, circIn, circInOut, circOut, cub
|
|
|
6
6
|
export { clamp, distance, distance2D, interpolate, mix, pipe, progress, wrap } from 'motion';
|
|
7
7
|
export type { DragAxis, DragConstraints, DragControls, DragInfo, DragTransition, MotionAnimate, MotionInitial, MotionOnDirectionLock, MotionOnDragTransitionEnd, MotionOnInViewEnd, MotionOnInViewStart, MotionTransition, MotionWhileDrag, MotionWhileFocus, MotionWhileHover, MotionWhileInView, MotionWhileTap, ReducedMotionConfig, Variants } from './types';
|
|
8
8
|
export { useAnimationFrame } from './utils/animationFrame';
|
|
9
|
+
export { useCycle } from './utils/cycle';
|
|
10
|
+
export type { Cycle, CycleState } from './utils/cycle';
|
|
9
11
|
export { createDragControls } from './utils/dragControls';
|
|
10
12
|
export { useMotionTemplate } from './utils/motionTemplate';
|
|
11
13
|
export { useMotionValue } from './utils/motionValue';
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ export { anticipate, backIn, backInOut, backOut, circIn, circInOut, circOut, cub
|
|
|
8
8
|
// Re-export utility functions
|
|
9
9
|
export { clamp, distance, distance2D, interpolate, mix, pipe, progress, wrap } from 'motion';
|
|
10
10
|
export { useAnimationFrame } from './utils/animationFrame';
|
|
11
|
+
export { useCycle } from './utils/cycle';
|
|
11
12
|
export { createDragControls } from './utils/dragControls';
|
|
12
13
|
export { useMotionTemplate } from './utils/motionTemplate';
|
|
13
14
|
export { useMotionValue } from './utils/motionValue';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Readable } from 'svelte/store';
|
|
2
|
+
export type Cycle = (next?: number) => void;
|
|
3
|
+
export type CycleState<T> = [Readable<T>, Cycle];
|
|
4
|
+
/**
|
|
5
|
+
* Cycles through a series of values. Mirrors Framer Motion's `useCycle`.
|
|
6
|
+
*
|
|
7
|
+
* Returns a tuple `[value, cycle]`:
|
|
8
|
+
*
|
|
9
|
+
* - `value` is a Svelte readable store of the current item; subscribe with
|
|
10
|
+
* `$value` in templates.
|
|
11
|
+
* - `cycle()` advances to the next item, wrapping back to index `0` when it
|
|
12
|
+
* passes the end.
|
|
13
|
+
* - `cycle(i)` jumps to the item at index `i`. The index is taken as-is to
|
|
14
|
+
* match `framer-motion` — out-of-range values yield `items[i]`, which
|
|
15
|
+
* may be `undefined`.
|
|
16
|
+
*
|
|
17
|
+
* Calls that resolve to the current index are no-ops and do not notify
|
|
18
|
+
* subscribers, matching React `useState`'s `Object.is` bail-out semantics.
|
|
19
|
+
*
|
|
20
|
+
* @param items - Items to cycle through. Must include at least one item.
|
|
21
|
+
* @returns A `[Readable<T>, Cycle]` tuple.
|
|
22
|
+
* @see https://motion.dev/docs/react-use-cycle
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```svelte
|
|
26
|
+
* <script>
|
|
27
|
+
* import { motion, useCycle } from '@humanspeak/svelte-motion'
|
|
28
|
+
*
|
|
29
|
+
* const [x, cycleX] = useCycle(0, 50, 100)
|
|
30
|
+
* </script>
|
|
31
|
+
*
|
|
32
|
+
* <motion.div animate={{ x: $x }} onclick={() => cycleX()} />
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare const useCycle: <T>(...items: T[]) => CycleState<T>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { wrap } from 'motion';
|
|
2
|
+
import { writable } from 'svelte/store';
|
|
3
|
+
/**
|
|
4
|
+
* Cycles through a series of values. Mirrors Framer Motion's `useCycle`.
|
|
5
|
+
*
|
|
6
|
+
* Returns a tuple `[value, cycle]`:
|
|
7
|
+
*
|
|
8
|
+
* - `value` is a Svelte readable store of the current item; subscribe with
|
|
9
|
+
* `$value` in templates.
|
|
10
|
+
* - `cycle()` advances to the next item, wrapping back to index `0` when it
|
|
11
|
+
* passes the end.
|
|
12
|
+
* - `cycle(i)` jumps to the item at index `i`. The index is taken as-is to
|
|
13
|
+
* match `framer-motion` — out-of-range values yield `items[i]`, which
|
|
14
|
+
* may be `undefined`.
|
|
15
|
+
*
|
|
16
|
+
* Calls that resolve to the current index are no-ops and do not notify
|
|
17
|
+
* subscribers, matching React `useState`'s `Object.is` bail-out semantics.
|
|
18
|
+
*
|
|
19
|
+
* @param items - Items to cycle through. Must include at least one item.
|
|
20
|
+
* @returns A `[Readable<T>, Cycle]` tuple.
|
|
21
|
+
* @see https://motion.dev/docs/react-use-cycle
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```svelte
|
|
25
|
+
* <script>
|
|
26
|
+
* import { motion, useCycle } from '@humanspeak/svelte-motion'
|
|
27
|
+
*
|
|
28
|
+
* const [x, cycleX] = useCycle(0, 50, 100)
|
|
29
|
+
* </script>
|
|
30
|
+
*
|
|
31
|
+
* <motion.div animate={{ x: $x }} onclick={() => cycleX()} />
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export const useCycle = (...items) => {
|
|
35
|
+
if (items.length === 0) {
|
|
36
|
+
throw new Error('useCycle requires at least one item');
|
|
37
|
+
}
|
|
38
|
+
let index = 0;
|
|
39
|
+
const store = writable(items[0]);
|
|
40
|
+
const cycle = (next) => {
|
|
41
|
+
const target = typeof next === 'number' ? next : wrap(0, items.length, index + 1);
|
|
42
|
+
if (target === index)
|
|
43
|
+
return;
|
|
44
|
+
index = target;
|
|
45
|
+
store.set(items[target]);
|
|
46
|
+
};
|
|
47
|
+
return [{ subscribe: store.subscribe }, cycle];
|
|
48
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@humanspeak/svelte-motion",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "A Framer Motion-compatible animation library for Svelte 5 that provides smooth, hardware-accelerated animations. Features include spring physics, custom easing, and fluid transitions. Built on top of the motion library, it offers a simple API for creating complex animations with minimal code. Perfect for interactive UIs, micro-interactions, and engaging user experiences.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"svelte",
|