Skip to content

Commit dd748e2

Browse files
committed
Merge branch 'main' into svelte-html
2 parents 6bf6c74 + 9828634 commit dd748e2

File tree

11 files changed

+616
-18
lines changed

11 files changed

+616
-18
lines changed

documentation/docs/06-runtime/01-stores.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const userState = $state({
4949
```svelte
5050
<!--- file: App.svelte --->
5151
<script>
52-
import { userState } from './state.svelte';
52+
import { userState } from './state.svelte.js';
5353
</script>
5454
5555
<p>User name: {userState.name}</p>

documentation/docs/06-runtime/02-context.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const myGlobalState = $state({
2222
```svelte
2323
<!--- file: App.svelte --->
2424
<script>
25-
import { myGlobalState } from './state.svelte';
25+
import { myGlobalState } from './state.svelte.js';
2626
// ...
2727
</script>
2828
```

packages/svelte/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# svelte
22

3+
## 5.8.1
4+
5+
### Patch Changes
6+
7+
- fix: reinstate missing prefersReducedMotion export ([#14586](https://github.com/sveltejs/svelte/pull/14586))
8+
9+
## 5.8.0
10+
11+
### Minor Changes
12+
13+
- feat: add `Spring` and `Tween` classes to `svelte/motion` ([#11519](https://github.com/sveltejs/svelte/pull/11519))
14+
315
## 5.7.1
416

517
### Patch Changes

packages/svelte/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "svelte",
33
"description": "Cybernetically enhanced web apps",
44
"license": "MIT",
5-
"version": "5.7.1",
5+
"version": "5.8.1",
66
"type": "module",
77
"types": "./types/index.d.ts",
88
"engines": {

packages/svelte/src/internal/shared/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ export function run_all(arr) {
4444
}
4545
}
4646

47+
/**
48+
* TODO replace with Promise.withResolvers once supported widely enough
49+
* @template T
50+
*/
51+
export function deferred() {
52+
/** @type {(value: T) => void} */
53+
var resolve;
54+
55+
/** @type {(reason: any) => void} */
56+
var reject;
57+
58+
/** @type {Promise<T>} */
59+
var promise = new Promise((res, rej) => {
60+
resolve = res;
61+
reject = rej;
62+
});
63+
64+
// @ts-expect-error
65+
return { promise, resolve, reject };
66+
}
67+
4768
/**
4869
* @template V
4970
* @param {V} value

packages/svelte/src/motion/private.d.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { Spring } from './public.js';
2-
3-
export interface TickContext<T> {
1+
export interface TickContext {
42
inv_mass: number;
53
dt: number;
6-
opts: Spring<T>;
4+
opts: {
5+
stiffness: number;
6+
damping: number;
7+
precision: number;
8+
};
79
settled: boolean;
810
}
911

@@ -14,8 +16,22 @@ export interface SpringOpts {
1416
}
1517

1618
export interface SpringUpdateOpts {
19+
/**
20+
* @deprecated Only use this for the spring store; does nothing when set on the Spring class
21+
*/
1722
hard?: any;
23+
/**
24+
* @deprecated Only use this for the spring store; does nothing when set on the Spring class
25+
*/
1826
soft?: string | number | boolean;
27+
/**
28+
* Only use this for the Spring class; does nothing when set on the spring store
29+
*/
30+
instant?: boolean;
31+
/**
32+
* Only use this for the Spring class; does nothing when set on the spring store
33+
*/
34+
preserveMomentum?: number;
1935
}
2036

2137
export type Updater<T> = (target_value: T, value: T) => T;
Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,88 @@
1-
import { Readable } from '../store/public.js';
2-
import { SpringUpdateOpts, TweenedOptions, Updater } from './private.js';
1+
import { Readable, type Unsubscriber } from '../store/public.js';
2+
import { SpringUpdateOpts, TweenedOptions, Updater, SpringOpts } from './private.js';
3+
4+
// TODO we do declaration merging here in order to not have a breaking change (renaming the Spring interface)
5+
// this means both the Spring class and the Spring interface are merged into one with some things only
6+
// existing on one side. In Svelte 6, remove the type definition and move the jsdoc onto the class in spring.js
37

48
export interface Spring<T> extends Readable<T> {
5-
set: (new_value: T, opts?: SpringUpdateOpts) => Promise<void>;
9+
set(new_value: T, opts?: SpringUpdateOpts): Promise<void>;
10+
/**
11+
* @deprecated Only exists on the legacy `spring` store, not the `Spring` class
12+
*/
613
update: (fn: Updater<T>, opts?: SpringUpdateOpts) => Promise<void>;
14+
/**
15+
* @deprecated Only exists on the legacy `spring` store, not the `Spring` class
16+
*/
17+
subscribe(fn: (value: T) => void): Unsubscriber;
718
precision: number;
819
damping: number;
920
stiffness: number;
1021
}
1122

23+
/**
24+
* A wrapper for a value that behaves in a spring-like fashion. Changes to `spring.target` will cause `spring.current` to
25+
* move towards it over time, taking account of the `spring.stiffness` and `spring.damping` parameters.
26+
*
27+
* ```svelte
28+
* <script>
29+
* import { Spring } from 'svelte/motion';
30+
*
31+
* const spring = new Spring(0);
32+
* </script>
33+
*
34+
* <input type="range" bind:value={spring.target} />
35+
* <input type="range" bind:value={spring.current} disabled />
36+
* ```
37+
* @since 5.8.0
38+
*/
39+
export class Spring<T> {
40+
constructor(value: T, options?: SpringOpts);
41+
42+
/**
43+
* Create a spring whose value is bound to the return value of `fn`. This must be called
44+
* inside an effect root (for example, during component initialisation).
45+
*
46+
* ```svelte
47+
* <script>
48+
* import { Spring } from 'svelte/motion';
49+
*
50+
* let { number } = $props();
51+
*
52+
* const spring = Spring.of(() => number);
53+
* </script>
54+
* ```
55+
*/
56+
static of<U>(fn: () => U, options?: SpringOpts): Spring<U>;
57+
58+
/**
59+
* Sets `spring.target` to `value` and returns a `Promise` that resolves if and when `spring.current` catches up to it.
60+
*
61+
* If `options.instant` is `true`, `spring.current` immediately matches `spring.target`.
62+
*
63+
* If `options.preserveMomentum` is provided, the spring will continue on its current trajectory for
64+
* the specified number of milliseconds. This is useful for things like 'fling' gestures.
65+
*/
66+
set(value: T, options?: SpringUpdateOpts): Promise<void>;
67+
68+
damping: number;
69+
precision: number;
70+
stiffness: number;
71+
/**
72+
* The end value of the spring.
73+
* This property only exists on the `Spring` class, not the legacy `spring` store.
74+
*/
75+
target: T;
76+
/**
77+
* The current value of the spring.
78+
* This property only exists on the `Spring` class, not the legacy `spring` store.
79+
*/
80+
get current(): T;
81+
}
82+
1283
export interface Tweened<T> extends Readable<T> {
1384
set(value: T, opts?: TweenedOptions<T>): Promise<void>;
1485
update(updater: Updater<T>, opts?: TweenedOptions<T>): Promise<void>;
1586
}
1687

17-
export * from './index.js';
88+
export { prefersReducedMotion, spring, tweened, Tween } from './index.js';

0 commit comments

Comments
 (0)