Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/tween-solid2-migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"@solid-primitives/tween": major
---

Migrate to Solid.js v2.0 (beta.10)

## Breaking Changes

**Peer dependency**: `solid-js@^2.0.0-beta.10` and `@solidjs/web@^2.0.0-beta.10` are now required.

### `@solid-primitives/tween`

- `isServer` now imported from `@solidjs/web` (not `solid-js/web`)
- `createEffect` updated to the two-argument compute/apply form required by Solid 2.0 — the removed `on()` helper is no longer used
- Cleanup (cancelling the in-flight `requestAnimationFrame`) is now returned from the apply function instead of calling `onCleanup`
- `current()` snapshot at tween start is now read inside `untrack()` to silence strict-mode warnings about untracked reads in apply callbacks
2 changes: 2 additions & 0 deletions packages/tween/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ npm install @solid-primitives/tween
yarn add @solid-primitives/tween
```

Requires `solid-js@^2.0.0-beta.10` and `@solidjs/web@^2.0.0-beta.10`.

## How to use it

```ts
Expand Down
6 changes: 4 additions & 2 deletions packages/tween/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@
"test:ssr": "pnpm run vitest --mode ssr"
},
"peerDependencies": {
"solid-js": "^1.6.12"
"@solidjs/web": "^2.0.0-beta.10",
"solid-js": "^2.0.0-beta.10"
},
"typesVersions": {},
"devDependencies": {
"solid-js": "^1.9.7"
"@solidjs/web": "2.0.0-beta.10",
"solid-js": "2.0.0-beta.10"
}
}
24 changes: 11 additions & 13 deletions packages/tween/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createSignal, createEffect, onCleanup, on } from "solid-js";
import { isServer } from "solid-js/web";
import { createSignal, createEffect, untrack } from "solid-js";
import { isServer } from "@solidjs/web";

export type TweenProps = {
duration?: number;
Expand Down Expand Up @@ -53,17 +53,15 @@ export default function createTween(
}

createEffect(
on(
target,
() => {
start = performance.now();
startValue = current();
delta = target() - startValue;
cancelId = requestAnimationFrame(tick);
onCleanup(() => cancelAnimationFrame(cancelId));
},
{ defer: true },
),
() => target(),
newTarget => {
start = performance.now();
startValue = untrack(current);
delta = newTarget - startValue;
cancelId = requestAnimationFrame(tick);
return () => cancelAnimationFrame(cancelId);
},
{ defer: true },
);

return current;
Expand Down
23 changes: 19 additions & 4 deletions packages/tween/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRoot, createSignal } from "solid-js";
import { createRoot, createSignal, flush } from "solid-js";
import { describe, expect, it, vi, afterEach } from "vitest";
import createTween from "../src/index.js";

Expand Down Expand Up @@ -47,9 +47,11 @@ describe("animation", () => {
expect(tweened()).toBe(0);

setSource(100);
expect(tweened()).toBe(0);
flush(); // run the effect, register RAF
expect(tweened()).toBe(0); // RAF hasn't fired yet

_flush_raf(start + 200);
_flush_raf(start + 200); // elapsed >= duration(100), setCurrent(target())
flush(); // commit the signal write
expect(tweened()).toBe(100);

dispose();
Expand All @@ -66,18 +68,23 @@ describe("animation", () => {

const start = performance.now();
setValue(100);
flush(); // run the effect
expect(tweened()).toBe(0);

_flush_raf(start + 25);
flush();
expect(tweened()).toBeCloseTo(25, 0);

_flush_raf(start + 50);
flush();
expect(tweened()).toBeCloseTo(50, 0);

_flush_raf(start + 75);
flush();
expect(tweened()).toBeCloseTo(75, 0);

_flush_raf(start + 100);
flush();
expect(tweened()).toBeCloseTo(100, 0);

dispose();
Expand All @@ -94,18 +101,23 @@ describe("animation", () => {

const start = performance.now();
setValue(100);
flush(); // run the effect
expect(tweened()).toBe(0);

_flush_raf(start + 25);
flush();
expect(tweened()).toBeCloseTo(6.25, 0);

_flush_raf(start + 50);
flush();
expect(tweened()).toBeCloseTo(25, 0);

_flush_raf(start + 75);
flush();
expect(tweened()).toBeCloseTo(56.25, 0);

_flush_raf(start + 100);
flush();
expect(tweened()).toBeCloseTo(100, 0);

dispose();
Expand All @@ -122,12 +134,15 @@ describe("animation", () => {
const start = performance.now();

setValue(100);
flush(); // run the effect, register RAF
_flush_raf(start + 600);
flush();
expect(tweened()).toBeCloseTo(60, 0);

setValue(0);

flush(); // cleanup old RAF, run effect with new target, register RAF
_flush_raf(start + 500);
flush();
expect(tweened()).toBeCloseTo(30, 0);

dispose();
Expand Down
19 changes: 19 additions & 0 deletions packages/tween/test/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, test, expect } from "vitest";
import { createSignal } from "solid-js";
import createTween from "../src/index.js";

describe("createTween", () => {
test("doesn't break in SSR", () => {
const [value] = createSignal(42);
const tweened = createTween(value, { duration: 100 });
expect(tweened()).toBe(42);
});

test("returns the target signal on SSR", () => {
const [value, setValue] = createSignal(0);
const tweened = createTween(value, {});
expect(tweened()).toBe(0);
setValue(100);
expect(tweened()).toBe(100);
});
});
7 changes: 5 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.