Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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.
7 changes: 7 additions & 0 deletions .changeset/cursor-ref-callback-solid2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@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.
9 changes: 9 additions & 0 deletions .changeset/form-ref-callbacks-solid2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@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.
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.
5 changes: 5 additions & 0 deletions .changeset/mediastream-ref-callback-solid2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@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.
7 changes: 7 additions & 0 deletions .changeset/mutation-observer-ref-callback-solid2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@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.
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;
};
}
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();
});
});
2 changes: 1 addition & 1 deletion packages/cursor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ createDragCursor(el, { grab: "crosshair", grabbing: "move" });

## `cursorRef`

A ref factory for setting a cursor inline in JSX. Accepts a static cursor value or a reactive signal. The cursor is removed when the component unmounts.
A ref factory for setting a cursor inline in JSX. Accepts a static cursor value or a reactive signal. Call it in the component body. The cursor is restored when the element unmounts or the component is disposed, and one `cursorRef` value can be attached to several elements.

```tsx repl
import { render } from "@solidjs/web";
Expand Down
Loading
Loading