Skip to content
Merged
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
7 changes: 7 additions & 0 deletions .changeset/active-element-ref-callback-solid2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@solid-primitives/active-element": patch
---

Fix `focus()` throwing `CLEANUP_IN_FORBIDDEN_SCOPE` when used as a JSX `ref` callback (#1143).

Solid 2 forbids `onCleanup` in a `ref` callback, and `focus()` registered its listeners with `makeEventListener`, which calls it. The callback now uses plain `addEventListener` and returns its teardown. Each element keeps its own teardown, so one `focus()` value works on several elements, and all of them are also removed with the owner of the `focus()` call because the runtime ignores a ref callback's return value.
9 changes: 9 additions & 0 deletions .changeset/cursor-ref-callback-solid2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@solid-primitives/cursor": patch
---

Fix `cursorRef` throwing `PRIMITIVE_IN_EFFECT_CALLBACK` when used as a JSX `ref` callback (#1143).

Solid 2 forbids creating effects inside a `ref` callback, and `cursorRef` created one there via `createElementCursor`. The effect is now created when `cursorRef(...)` is evaluated (the component body); the callback only registers the element and returns a teardown. The cursor is restored when the element unmounts or the owner is disposed, and one `cursorRef` value can be attached to several elements.

`cursorRef` also mints a matching owner on the server, so the hydration ids of elements rendered after it stay aligned with the client.
11 changes: 11 additions & 0 deletions .changeset/form-ref-callbacks-solid2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@solid-primitives/form": patch
---

Fix `form.ref` and `form.bind()` throwing `CLEANUP_IN_FORBIDDEN_SCOPE` (and `PRIMITIVE_IN_EFFECT_CALLBACK`) when used as JSX `ref` callbacks (#1143).

Solid 2 runs a `ref` callback in a restricted context where `onCleanup` and creating roots, memos or effects are forbidden. `ref` and `bind` registered their listeners with `makeEventListener` (which calls `onCleanup`) and `bind` created its DOM-sync effect inside the callback. In a hydrated app the throw aborted hydration, so the submit handler was never attached and a native GET submission put the fields, including passwords, into the URL.

Listeners are now plain `addEventListener` calls inside the ref callback, which still returns its cleanup. The field → DOM sync effect is created when `form.bind("name")` is evaluated (the component body), and the listeners are also torn down with the owner of `createForm` / `bind()`, because the runtime ignores a ref callback's return value. The unused `@solid-primitives/event-listener` dependency is removed.

A `bind()` callback created without an owner is also torn down, together with its sync root, when the owner that mounted the ref is disposed.
5 changes: 5 additions & 0 deletions .changeset/lifecycle-ref-callback-solid2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solid-primitives/lifecycle": patch
---

Fix `onElementConnect` throwing `CLEANUP_IN_FORBIDDEN_SCOPE` inside a `ref` callback (#1143). It no longer calls `onCleanup` and now returns a disposer. Add `createConnectRef` for `ref` usage that also cleans up with the owner.
7 changes: 7 additions & 0 deletions .changeset/mediastream-ref-callback-solid2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@solid-primitives/mediastream": patch
---

Fix the README `createStream` and `createScreen` examples, which created an effect inside a JSX `ref` callback and threw `PRIMITIVE_IN_EFFECT_CALLBACK` in Solid 2 (#1143). They now keep the element in a signal via `ref={setVideo}` and assign `srcObject` from an effect in the component body. Documentation only, no source change.

The examples also no longer wrap in `<Loading>` (`stream()` is a plain signal, so it never shows a fallback; they render their own loading state) and `createScreen` now starts from a click, since `getDisplayMedia` requires a user gesture. The same inaccurate `<Loading>` claim is removed from the JSDoc.
9 changes: 9 additions & 0 deletions .changeset/mutation-observer-ref-callback-solid2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@solid-primitives/mutation-observer": patch
---

Fix `mutationObserver(options, callback)` throwing `PRIMITIVE_IN_EFFECT_CALLBACK` when used as a JSX `ref` (#1143).

The observer was created inside the ref callback, where Solid 2 forbids `onSettled`, `onCleanup` and other primitives. It is now created once when `mutationObserver(...)` is evaluated (the component body) and the ref callback only calls `observe`. It disconnects with the owner of that call, and one factory value can be used on several elements.

`createMutationObserver` (and so `mutationObserver`) also mints a matching owner on the server, so the hydration ids of elements rendered after it stay aligned with the client.
7 changes: 7 additions & 0 deletions .changeset/pagination-ref-callback-solid2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@solid-primitives/pagination": patch
---

Fix the `createInfiniteScroll` loader throwing `CLEANUP_IN_FORBIDDEN_SCOPE` when used as a JSX `ref` callback (#1143).

The loader called `onCleanup` inside the ref callback, which Solid 2 forbids. It now observes the element (unobserving a previous sentinel) and returns a teardown; observation is also torn down with the owner of `createInfiniteScroll`.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ See the [migration guide](https://primitives.solidjs.community/migration) and th
|[controlled-signal](https://github.com/solidjs-community/solid-primitives/tree/main/packages/controlled-signal#readme)|[![STAGE](https://img.shields.io/endpoint?style=for-the-badge&label=&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-3.json)](https://github.com/solidjs-community/solid-primitives/blob/main/CONTRIBUTING.md#contribution-process)|[createControllableSignal](https://github.com/solidjs-community/solid-primitives/tree/main/packages/controlled-signal#createcontrollablesignal)<br />[createControllableBooleanSignal](https://github.com/solidjs-community/solid-primitives/tree/main/packages/controlled-signal#createcontrollablebooleansignal)<br />[createControllableArraySignal](https://github.com/solidjs-community/solid-primitives/tree/main/packages/controlled-signal#createcontrollablearraysignal)<br />[createControllableSetSignal](https://github.com/solidjs-community/solid-primitives/tree/main/packages/controlled-signal#createcontrollablesetsignal)<br />[createToggleState](https://github.com/solidjs-community/solid-primitives/tree/main/packages/controlled-signal#createtogglestate)|[![SIZE](https://img.shields.io/badge/size-528_B-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/controlled-signal)|[![VERSION](https://img.shields.io/npm/v/@solid-primitives/controlled-signal?style=for-the-badge&label=)](https://www.npmjs.com/package/@solid-primitives/controlled-signal)|✓|
|[deep](https://github.com/solidjs-community/solid-primitives/tree/main/packages/deep#readme)|[![STAGE](https://img.shields.io/endpoint?style=for-the-badge&label=&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-3.json)](https://github.com/solidjs-community/solid-primitives/blob/main/CONTRIBUTING.md#contribution-process)|[trackDeep](https://github.com/solidjs-community/solid-primitives/tree/main/packages/deep#trackdeep)<br />[trackStore](https://github.com/solidjs-community/solid-primitives/tree/main/packages/deep#trackstore)<br />[captureStoreUpdates](https://github.com/solidjs-community/solid-primitives/tree/main/packages/deep#capturestoreupdates)|[![SIZE](https://img.shields.io/badge/size-954_B-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/deep)|[![VERSION](https://img.shields.io/npm/v/@solid-primitives/deep?style=for-the-badge&label=)](https://www.npmjs.com/package/@solid-primitives/deep)|✓|
|[destructure](https://github.com/solidjs-community/solid-primitives/tree/main/packages/destructure#readme)|[![STAGE](https://img.shields.io/endpoint?style=for-the-badge&label=&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-3.json)](https://github.com/solidjs-community/solid-primitives/blob/main/CONTRIBUTING.md#contribution-process)|[destructure](https://github.com/solidjs-community/solid-primitives/tree/main/packages/destructure#destructure)|[![SIZE](https://img.shields.io/badge/size-507_B-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/destructure)|[![VERSION](https://img.shields.io/npm/v/@solid-primitives/destructure?style=for-the-badge&label=)](https://www.npmjs.com/package/@solid-primitives/destructure)|✓|
|[lifecycle](https://github.com/solidjs-community/solid-primitives/tree/main/packages/lifecycle#readme)|[![STAGE](https://img.shields.io/endpoint?style=for-the-badge&label=&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-3.json)](https://github.com/solidjs-community/solid-primitives/blob/main/CONTRIBUTING.md#contribution-process)|[createIsMounted](https://github.com/solidjs-community/solid-primitives/tree/main/packages/lifecycle#createismounted)<br />[isHydrated](https://github.com/solidjs-community/solid-primitives/tree/main/packages/lifecycle#ishydrated)<br />[onElementConnect](https://github.com/solidjs-community/solid-primitives/tree/main/packages/lifecycle#onelementconnect)|[![SIZE](https://img.shields.io/badge/size-306_B-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/lifecycle)|[![VERSION](https://img.shields.io/npm/v/@solid-primitives/lifecycle?style=for-the-badge&label=)](https://www.npmjs.com/package/@solid-primitives/lifecycle)|✓|
|[lifecycle](https://github.com/solidjs-community/solid-primitives/tree/main/packages/lifecycle#readme)|[![STAGE](https://img.shields.io/endpoint?style=for-the-badge&label=&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-3.json)](https://github.com/solidjs-community/solid-primitives/blob/main/CONTRIBUTING.md#contribution-process)|[createIsMounted](https://github.com/solidjs-community/solid-primitives/tree/main/packages/lifecycle#createismounted)<br />[isHydrated](https://github.com/solidjs-community/solid-primitives/tree/main/packages/lifecycle#ishydrated)<br />[onElementConnect](https://github.com/solidjs-community/solid-primitives/tree/main/packages/lifecycle#onelementconnect)<br />[createConnectRef](https://github.com/solidjs-community/solid-primitives/tree/main/packages/lifecycle#createconnectref)|[![SIZE](https://img.shields.io/badge/size-306_B-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/lifecycle)|[![VERSION](https://img.shields.io/npm/v/@solid-primitives/lifecycle?style=for-the-badge&label=)](https://www.npmjs.com/package/@solid-primitives/lifecycle)|✓|
|[map](https://github.com/solidjs-community/solid-primitives/tree/main/packages/map#readme)|[![STAGE](https://img.shields.io/endpoint?style=for-the-badge&label=&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-3.json)](https://github.com/solidjs-community/solid-primitives/blob/main/CONTRIBUTING.md#contribution-process)|[ReactiveMap](https://github.com/solidjs-community/solid-primitives/tree/main/packages/map#reactivemap)<br />[ReactiveWeakMap](https://github.com/solidjs-community/solid-primitives/tree/main/packages/map#reactiveweakmap)|[![SIZE](https://img.shields.io/badge/size-732_B-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/map)|[![VERSION](https://img.shields.io/npm/v/@solid-primitives/map?style=for-the-badge&label=)](https://www.npmjs.com/package/@solid-primitives/map)|✓|
|[memo](https://github.com/solidjs-community/solid-primitives/tree/main/packages/memo#readme)|[![STAGE](https://img.shields.io/endpoint?style=for-the-badge&label=&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-3.json)](https://github.com/solidjs-community/solid-primitives/blob/main/CONTRIBUTING.md#contribution-process)|[createLatest](https://github.com/solidjs-community/solid-primitives/tree/main/packages/memo#createlatest)<br />[createLatestMany](https://github.com/solidjs-community/solid-primitives/tree/main/packages/memo#createlatestmany)<br />[createWritableMemo](https://github.com/solidjs-community/solid-primitives/tree/main/packages/memo#createwritablememo)<br />[createLazyMemo](https://github.com/solidjs-community/solid-primitives/tree/main/packages/memo#createlazymemo)<br />[createPureReaction](https://github.com/solidjs-community/solid-primitives/tree/main/packages/memo#createpurereaction)<br />[createMemoCache](https://github.com/solidjs-community/solid-primitives/tree/main/packages/memo#creatememocache)<br />[createReducer](https://github.com/solidjs-community/solid-primitives/tree/main/packages/memo#createreducer)|[![SIZE](https://img.shields.io/badge/size-855_B-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/memo)|[![VERSION](https://img.shields.io/npm/v/@solid-primitives/memo?style=for-the-badge&label=)](https://www.npmjs.com/package/@solid-primitives/memo)|✓|
|[mutable](https://github.com/solidjs-community/solid-primitives/tree/main/packages/mutable#readme)|[![STAGE](https://img.shields.io/endpoint?style=for-the-badge&label=&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-3.json)](https://github.com/solidjs-community/solid-primitives/blob/main/CONTRIBUTING.md#contribution-process)|[createMutable](https://github.com/solidjs-community/solid-primitives/tree/main/packages/mutable#createmutable)<br />[modifyMutable](https://github.com/solidjs-community/solid-primitives/tree/main/packages/mutable#modifymutable)|[![SIZE](https://img.shields.io/badge/size-1.14_kB-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/mutable)|[![VERSION](https://img.shields.io/npm/v/@solid-primitives/mutable?style=for-the-badge&label=)](https://www.npmjs.com/package/@solid-primitives/mutable)|✓|
Expand Down
2 changes: 1 addition & 1 deletion packages/active-element/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const [active, setActive] = createSignal(false)
#### Definition

```ts
function focus(callback: (isActive: boolean) => void): (target: Element) => void;
function focus(callback: (isActive: boolean) => void): (target: Element) => VoidFunction;
```

## Changelog
Expand Down
27 changes: 17 additions & 10 deletions packages/active-element/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Accessor, onCleanup } from "solid-js";
import { type Accessor, getOwner, onCleanup } from "solid-js";
import { isServer } from "@solidjs/web";
import { createHydratableSignal } from "@solid-primitives/utils";
import { makeEventListener } from "@solid-primitives/event-listener";
Expand Down Expand Up @@ -55,17 +55,24 @@ export function createActiveElement(): Accessor<Element | null> {
* const [active, setActive] = createSignal(false)
* <input ref={focus(setActive)} />
*/
export function focus(callback: (isActive: boolean) => void): (target: Element) => void {
if (isServer) return () => {};
let cleanup: (() => void) | null = null;
onCleanup(() => cleanup?.());
export function focus(callback: (isActive: boolean) => void): (target: Element) => VoidFunction {
if (isServer) return () => () => {};
// One teardown per element, so a single focus() value can be attached to several elements.
const teardowns = new Set<VoidFunction>();
// The runtime ignores a ref callback's return value, so the factory owner tears down too.
if (getOwner()) onCleanup(() => [...teardowns].forEach(t => t()));
return (target: Element) => {
callback(document.activeElement === target);
const c1 = makeEventListener(target, "blur", () => callback(false), true);
const c2 = makeEventListener(target, "focus", () => callback(true), true);
cleanup = () => {
c1();
c2();
const onBlur = () => callback(false);
const onFocus = () => callback(true);
target.addEventListener("blur", onBlur, true);
target.addEventListener("focus", onFocus, true);
const teardown = () => {
target.removeEventListener("blur", onBlur, true);
target.removeEventListener("focus", onFocus, true);
teardowns.delete(teardown);
};
teardowns.add(teardown);
return teardown;
};
}
46 changes: 46 additions & 0 deletions packages/active-element/test/hydration.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, it, expect, afterEach } from "vitest";
import {
renderHydrationRoundTrip,
type HydrationRoundTripResult,
} from "../../../scripts/test-utils/hydration-harness.ts";

// A client-side owner created in the factory needs a matching server owner, or the hydration
// ids of everything after it shift and later elements miss their server-rendered twins.
const app = (imports: string, setup: string, tag: string) => `
import { createUniqueId } from "solid-js";
${imports}
export default function App() {
${setup}
const id = createUniqueId();
return (
<div>
<${tag} id="a" ref={ref}>box</${tag}>
<span id="uid">{id}</span>
</div>
);
}
`;

let result: HydrationRoundTripResult | undefined;
afterEach(() => {
result?.cleanup();
result = undefined;
});

async function expectAligned(source: string) {
result = await renderHydrationRoundTrip(source, import.meta.dirname);
const serverId = /id="uid">([^<]*)</.exec(result.html)?.[1];
expect(result.container.querySelector("#uid")!.textContent).toBe(serverId);
expect(result.consoleMessages).toEqual([]);
}

describe("focus() hydration", () => {
it("keeps hydration ids aligned", () =>
expectAligned(
app(
`import { focus } from "@solid-primitives/active-element";`,
`const ref = focus(() => {});`,
"input",
),
));
});
101 changes: 101 additions & 0 deletions packages/active-element/test/ref.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { describe, it, expect, vi, afterEach } from "vitest";
import { render } from "@solidjs/web";
import { focus } from "../src/index.js";

// Regression for #1143: focus() must work as a real JSX ref, where onCleanup is forbidden.

let disposers: Array<() => void> = [];

function mount(App: () => any) {
const container = document.createElement("div");
document.body.appendChild(container);
const dispose = render(App, container);
const unmount = () => {
dispose();
container.remove();
};
disposers.push(unmount);
return { container, unmount };
}

afterEach(() => {
disposers.forEach(d => d());
disposers = [];
});

const fire = (el: Element, type: "focus" | "blur") => el.dispatchEvent(new FocusEvent(type));

describe("focus as a JSX ref callback", () => {
it("mounts a static ref without throwing and reports focus changes", () => {
const cb = vi.fn();
let container!: HTMLElement;
expect(() => {
({ container } = mount(() => <input ref={focus(cb)} />));
}).not.toThrow();
const input = container.querySelector("input")!;
expect(cb).toHaveBeenLastCalledWith(false);
fire(input, "focus");
expect(cb).toHaveBeenLastCalledWith(true);
fire(input, "blur");
expect(cb).toHaveBeenLastCalledWith(false);
});

it("works inside an array of refs", () => {
const cb = vi.fn();
const other = vi.fn();
const { container } = mount(() => <input ref={[focus(cb), other]} />);
const input = container.querySelector("input")!;
expect(other).toHaveBeenCalledWith(input);
fire(input, "focus");
expect(cb).toHaveBeenLastCalledWith(true);
});

it("keeps per-element state when one focus() value is attached to two elements", () => {
const cb = vi.fn();
const { container, unmount } = mount(() => {
const f = focus(cb);
return (
<div>
<input id="a" ref={f} />
<input id="b" ref={f} />
</div>
);
});
const a = container.querySelector("#a")!;
const b = container.querySelector("#b")!;
cb.mockClear();
fire(a, "focus");
fire(b, "focus");
expect(cb).toHaveBeenCalledTimes(2);
unmount();
cb.mockClear();
fire(a, "focus");
fire(b, "focus");
expect(cb).not.toHaveBeenCalled();
});

it("removes listeners when the owner is disposed", () => {
const cb = vi.fn();
const { container, unmount } = mount(() => <input ref={focus(cb)} />);
const input = container.querySelector("input")!;
unmount();
cb.mockClear();
fire(input, "focus");
fire(input, "blur");
expect(cb).not.toHaveBeenCalled();
});

it("does not throw without an owner and returns a teardown", () => {
const cb = vi.fn();
const el = document.createElement("input");
const ref = focus(cb) as (t: Element) => unknown;
const teardown = ref(el) as () => void;
fire(el, "focus");
expect(cb).toHaveBeenLastCalledWith(true);
teardown();
teardown();
cb.mockClear();
fire(el, "focus");
expect(cb).not.toHaveBeenCalled();
});
});
Loading
Loading