Adopt React Native 0.82 DOM Node APIs#475
Conversation
Bump the react-native peer dependency to >=0.82.0 and rewrite useStrictDOMElement to wrap the underlying RN host node in a thin Proxy instead of cloning it via Object.create / Object.defineProperties. React Native 0.82 shipped the stable DOM Node APIs that strict-dom helped drive into RN (DOM traversal, ownerDocument, getRootNode, children/childNodes, pointer-capture methods, etc.), so the native ref polyfill becomes a lightweight overlay rather than a parallel implementation. The Proxy traps only the keys strict-dom still needs to control: - nodeName: uppercase DOM name (RN exposes tagName as 'RN:View') - getBoundingClientRect and length getters: divided by the active viewportScale - <img>.complete: fallback to false when the underlying RN Image node does not expose it - <input>/<textarea> selection trio (setSelectionRange, selectionStart, selectionEnd): polyfilled on top of setSelection while RN's TextInput lacks the W3C selection API Everything else (ownerDocument, getRootNode, parentNode, children, childNodes, sibling navigation, pointer-capture, legacy measure*) forwards directly to the underlying RN node via Reflect.get. Function values are bound to the target so internal `this`-references inside RN's implementations resolve correctly. Identity caching via a WeakMap<Node, Proxy> is preserved so the same underlying RN node always yields the same wrapped ref. The selection polyfill is gated on the underlying property being absent, so the day RN exposes the W3C selection API on TextInput the polyfill self-disables. RN 0.83 also tightened the Flow types around Animated.createAnimatedComponent (now a single-type-arg generic) and made ImageProps / TextInputProps exact. The Animated factory call sites are updated to the new signature; the wide-spread of strict-dom's ReactNativeProps onto the exact host components is suppressed with targeted $FlowFixMe annotations (real follow-up tracked separately). Adds 10 ref tests in tests/html/html-refs-test.native.js documenting the contract: uppercase nodeName, getBoundingClientRect pass-through at scale=1, getBoundingClientRect scaled when viewportScale != 1, the DOM Node API pass-through (ownerDocument / getRootNode / childNodes / children), identity stability of the strict ref across renders, and the <img>.complete fallback (both when omitted and when provided). Bundle size: native/index.js drops ~363 minified / ~69 brotli bytes. Web build is byte-identical.
Bring apps/expo-app and apps/platform-tests onto a real RN >=0.82.0 runtime so they exercise the new useStrictDOMElement Proxy path against the DOM Node APIs from the previous commit. Note: there is no Expo SDK that pairs exactly with RN 0.82 (SDK 53 = RN 0.79, SDK 54 ~= RN 0.81, SDK 55 = RN 0.83). SDK 55 / RN 0.83.6 still satisfies the library's >=0.82.0 peer dep and ships the same DOM Node API surface, so it is the closest landing zone. Co-traveling dependency versions (@expo/metro-runtime, expo-build-properties, expo-status-bar, react-native-web, etc.) come from `npx expo install --check` for SDK 55; no hand-rolled versions. Also pin react / react-dom / react-test-renderer to ~19.2.0 across the workspace root and the two library packages' devDependencies. RN 0.82+ requires React >=19.1.1, and aligning the workspace devDeps avoids a multiple-React-instances error in the jest suite that would otherwise surface once the apps hoist React 19.2.x at the root.
|
Related follow-up: #477: switches strict-dom from the deep import |
|
Hey, thanks for this! It looks good, however can we use the object clone approach rather than the Proxy? I know that using a Proxy here is much more ergonomic than the way the code is currently written, but the performance of using Proxy is substantially slower than the existing code. Due to the very dynamic nature of a Proxy hermes (the js runtime most react native applications use) is unable to apply the optimization static code can benefit from |
|
Restored the original approach and improved a couple of things! |
RN 0.82 settled the DOM Node prototype hierarchy, so we can stop cloning the host node. Object.create(node) makes the raw node the wrapper's prototype; strict-dom defines its overrides on top; everything else falls through. Symbol-keyed internals like INSTANCE_HANDLE_KEY stay reachable through the chain, so RN's prototype methods work when called on the wrapper. Changes vs the previous clone: * No descriptor snapshot. Reads stay in sync with the node. * No try/catch fallback. defineProperty on a fresh object can't fail in normal use. * getBoundingClientRect and the length getters only install when viewportScale isn't 1. Scale 1 skips them. * nodeName is a value descriptor now, since tagName.toUpperCase() doesn't change for a given wrapper. * writable: true removed from the value descriptors. DOM spec is read-only for nodeName, getBoundingClientRect and setSelectionRange, so strict-mode assignments throw now. * configurable: true on every override. instanceof still works: the chain is one link longer (wrapper to node to ReactNativeElement.prototype) but the class prototype is still on it. Ten tests in html-refs-test.native.js pass.
8fbae4f to
7e32435
Compare
Summary
Bumps
react-nativepeer dep to>=0.82.0and adopts the stable DOM Node APIs from RN 0.82. The native ref polyfill becomes an overlay on top of RN's host node instead of a parallel object that re-implements DOM traversal.The first commit on this branch (4a49d1f) wrapped the node in a
Proxy. After review feedback that Hermes can't inline through Proxy traps, follow-up 8fbae4f replaces it withObject.create(node)prototype delegation: strict-dom defines its overrides as own properties on the wrapper, and everything else delegates to the node via the prototype chain. Same observable behavior, static hidden class Hermes can optimize, no per-access bound-function allocations.Tracks #462.
What's still polyfilled
nodeName: uppercase DOM name. RN exposestagNameas'RN:View'.getBoundingClientRectand the length getters (offsetWidth,clientHeight, ...): divided by the activeviewportScale. Only installed when scale isn't 1, so the common path skips them.<img>.complete: returnsfalsewhen the underlying Image node doesn't expose it.<input>/<textarea>setSelectionRange/selectionStart/selectionEnd: polyfilled oversetSelection. Each one is gated on the underlying property being missing, so the polyfill switches itself off the day RN exposes the W3C API.What forwards to the RN node
ownerDocument,getRootNode,parentNode,parentElement,children,childNodes, sibling navigation,contains,compareDocumentPosition, pointer-capture, legacymeasure*andsetNativeProps. With the node as the wrapper's prototype, RN's prototype methods invoked on the wrapper seethis === wrapper; the symbol-keyed internals they consult (INSTANCE_HANDLE_KEY,OWNER_DOCUMENT_KEYfromNodeInternals.js) are reachable through the chain back to the node, so RN's implementations resolve their backing state correctly.instanceofkeeps working. The chain iswrapper → node → ReactNativeElement.prototype → ReadOnlyElement.prototype → ReadOnlyNode.prototype, one link longer than the pre-PR clone but the class prototypes are still on it. Verified on a live iPad simulator (wrapper.constructor.name === 'ReactNativeElement') and in Chrome ('HTMLDivElement').Identity caching via
WeakMap<Node, Node>is preserved, so the same RN node yields the same wrapped ref across renders.Descriptor flags
configurable: trueis set on every override soObject.definePropertycan redefine them later (tests, devtools, or a future fix to viewport-scale memoization). Value descriptors (nodeName,getBoundingClientRect,setSelectionRange) are notwritable, matching the DOM spec for read-only properties. Strict-mode assignments likewrapper.setSelectionRange = ...now throwTypeErrorinstead of silently shadowing. Demoed live in the example apps.Breaking
Peer dep
react-native >=0.79.5→>=0.82.0. Apps on older RN stay on the previous react-strict-dom release.Notes for reviewers
Hermes / Proxy concern. Addressed in 8fbae4f. The delegation variant has a 1-to-15 own-property hidden class (size depends on viewport scale and tag), no traps, no per-access binding. The CSS benchmarks in
benchmarks/don't exercise the ref read path, so the existing numbers aren't informative for this change. Happy to add a targeted micro-benchmark if useful.Expo SDK. No SDK pairs exactly with RN 0.82 (SDK 53 = 0.79, SDK 54 ≈ 0.81, SDK 55 = 0.83). The example apps move to SDK 55 / RN 0.83.6, which satisfies the new peer dep and ships the same DOM Node surface.
React devDeps. RN 0.82+ requires React
>=19.1.1. The workspace root and library packages' devDependencies pinreact/react-dom/react-test-rendererto~19.2.0to keep a single React instance across the monorepo.Flow types in 0.83.
Animated.createAnimatedComponentis now a single-type-arg generic; fixed by dropping the over-specified two-type-arg form.ImageProps/TextInputPropsbecame exact; the wide spread of strict-dom'sReactNativePropsis covered by two targeted$FlowFixMeannotations. Narrowing that spread properly is a separate follow-up.Ten ref tests in
tests/html/html-refs-test.native.jspin every wrapped-ref behavior: uppercase nodeName, gBCR pass-through at scale 1 and scaled at non-1, DOM Node API pass-through (ownerDocument,getRootNode,childNodes,children), strict-ref identity stability,<img>.completefallback and pass-through, and the input selection trio. All ten pass against both the original Proxy commit and the prototype-delegation follow-up.One pre-existing latent issue noted.
memoizedStrictRefsis keyed only onnode, not on(node, viewportScale). A node rendered first outsideViewportProviderand then inside (or with a changing scale) keeps the wrapper from the first wrap. Predates this PR; calling out for a follow-up. Not blocking. I worked around it locally in a realtime demo by puttingkey={viewportWidth}on the inner reporter to force a remount.