Skip to content

An agent skill for creating detailed, interactive isometric illustrations of real-world machines and complex systems. Generates polished SVGs with optional WebGL shaders and 3D effects.

fix(react): observe replacement visibility targets

Rudy Celekli7db64503e951
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 6692197..15e2440 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -26,7 +26,11 @@ jobs:
node-version: 22
- name: Regression tests
- run: node --test scripts/*.test.mjs
+ run: |
+ test_deps="$(mktemp -d)"
+ trap 'rm -rf "$test_deps"' EXIT
+ npm install --prefix "$test_deps" --no-audit --no-fund --ignore-scripts [email protected] [email protected] [email protected] [email protected]
+ NODE_PATH="$test_deps/node_modules" node --test scripts/*.test.mjs
- name: Dial indicator
run: |
node examples/dial-indicator/build.mjs
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fe3a3ed..63b882c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,8 @@ All notable changes to Anatomy are recorded here. The format follows [Keep a Cha
### Fixed
+- Observe replacement and late-mounted DOM targets through useInView.
+
- `scripts/drive.mjs` now fails when a browser evaluation throws or rejects, rather than reporting a clean successful session.
- The arcade cabinet now accepts coins with reduced motion enabled, preserving the meter, credit and power-on events when the coin animation is skipped.
- The turning dial stops its idle tour when reduced motion is enabled while the page is open and cancels its pending automatic restart while preserving keyboard and pointer input.
diff --git a/kit/react/live.tsx b/kit/react/live.tsx
index 68ddf0d..26d6899 100644
--- a/kit/react/live.tsx
+++ b/kit/react/live.tsx
@@ -22,13 +22,27 @@ export function useStillness() {
export function useInView<T extends Element>(ref: React.RefObject<T | null>) {
const [inView, setInView] = React.useState(false);
+ const watched = React.useRef<Element | null>(null);
+ const observer = React.useRef<IntersectionObserver | null>(null);
React.useEffect(() => {
const node = ref.current;
+ if (node === watched.current) return;
+ observer.current?.disconnect();
+ observer.current = null;
+ watched.current = node;
+ setInView(false);
if (!node) return;
- const observer = new IntersectionObserver((entries) => setInView(Boolean(entries.at(-1)?.isIntersecting)), { rootMargin: MARGIN });
- observer.observe(node);
- return () => observer.disconnect();
- }, [ref]);
+ const binding = new IntersectionObserver((entries) => {
+ if (watched.current === node && observer.current === binding) setInView(Boolean(entries.at(-1)?.isIntersecting));
+ }, { rootMargin: MARGIN });
+ observer.current = binding;
+ binding.observe(node);
+ });
+ React.useEffect(() => () => {
+ observer.current?.disconnect();
+ observer.current = null;
+ watched.current = null;
+ }, []);
return inView;
}
diff --git a/scripts/react-visibility-target.test.mjs b/scripts/react-visibility-target.test.mjs
new file mode 100644
index 0000000..3a4793b
--- /dev/null
+++ b/scripts/react-visibility-target.test.mjs
@@ -0,0 +1,38 @@
+import assert from "node:assert/strict";
+import { execFile } from "node:child_process";
+import { mkdtemp, rm, writeFile } from "node:fs/promises";
+import { createRequire } from "node:module";
+import { tmpdir } from "node:os";
+import { dirname, join, resolve } from "node:path";
+import test from "node:test";
+import { promisify } from "node:util";
+const require = createRequire(import.meta.url);
+const { build } = require("esbuild");
+const nodePaths = (process.env.NODE_PATH ?? "").split(":").filter(Boolean);
+const alias = Object.fromEntries(["react", "react-dom"].map(name => [name, dirname(require.resolve(name+"/package.json", { paths: nodePaths }))]));
+const directory = await mkdtemp(join(tmpdir(), "anatomy-visibility-target-"));
+let observed;
+try {
+ const entry = `import React,{StrictMode,useRef,useState,useEffect} from 'react';import {createRoot} from 'react-dom/client';import {flushSync} from 'react-dom';import {useInView} from ${JSON.stringify(resolve("kit/react/live.tsx"))};
+function Probe({mode}){const ref=useRef(null);const [phase,setPhase]=useState(false);const visible=useInView(ref);useEffect(()=>{const timer=setTimeout(()=>setPhase(true),150);return()=>clearTimeout(timer)},[]);const absent=mode==='late'&&!phase;const distant=(mode==='swap'&&!phase)||(mode==='moved'&&phase);return React.createElement('section',{id:'probe-'+mode,'data-visible':String(visible),'data-phase':String(phase)},absent?null:React.createElement('div',{ref,key:mode==='swap'?String(phase):'stable','data-target':mode,style:{position:'absolute',top:distant?'5000px':'20px',left:'20px',width:'20px',height:'20px'}},mode));}
+for(const mode of ['swap','late','healthy','moved','strict'])flushSync(()=>createRoot(document.getElementById(mode)).render(mode==='strict'?React.createElement(StrictMode,null,React.createElement(Probe,{mode})):React.createElement(Probe,{mode})));setTimeout(()=>{window.visibilityObserved=Object.fromEntries(['swap','late','healthy','moved','strict'].map(mode=>[mode,{visible:document.querySelector('#probe-'+mode).dataset.visible,phase:document.querySelector('#probe-'+mode).dataset.phase,top:document.querySelector('[data-target="'+mode+'"]').getBoundingClientRect().top}]))},700);`;
+ await build({ stdin: { contents: entry, resolveDir: process.cwd(), loader: "jsx" }, bundle: true, outfile: join(directory,"fixture.js"), platform: "browser", nodePaths, alias });
+ const page = join(directory,"figure.html");
+ await writeFile(page,'<!doctype html><div class="iso-plate"><div id="swap"></div><div id="late"></div><div id="healthy"></div><div id="moved"></div><div id="strict"></div></div><script src="fixture.js"></script>');
+ const result = await promisify(execFile)(process.execPath,["scripts/drive.mjs",page,JSON.stringify([["eval","window.visibilityObserved"]])],{timeout:30000});
+ const payload = result.stdout.match(/eval: (.*)/);
+ assert.ok(payload, result.stdout);
+ observed = JSON.parse(payload[1]);
+ assert.ok(observed, result.stdout);
+} finally { await rm(directory,{recursive:true,force:true}); }
+test("native visibility observes a replacement node on the same ref",()=>{
+ assert.equal(observed.swap.phase,"true");assert.equal(observed.swap.top,20);assert.equal(observed.swap.visible,"true");
+});
+test("native visibility observes a target mounted after the initial empty ref",()=>{
+ assert.equal(observed.late.phase,"true");assert.equal(observed.late.top,20);assert.equal(observed.late.visible,"true");
+});
+test("an unchanged visible target stays observed",()=>assert.equal(observed.healthy.visible,"true"));
+test("the same target becoming distant retains real visibility updates",()=>{
+ assert.equal(observed.moved.top,5000);assert.equal(observed.moved.visible,"false");
+});
+test("native StrictMode effect replay preserves ordinary observation",()=>assert.equal(observed.strict.visible,"true"));