From 9ac9edc61ec38f68765ce091f03d10cd9f6d5be4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Mon, 27 Apr 2026 05:44:56 -0700 Subject: [PATCH] Add MutationObserver and IntersectionObserver types in bom.js.flow (#56624) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56624 Adds Flow type definitions for `MutationObserver` (and its related types `MutationRecord` and `MutationObserverInit`) and `IntersectionObserver` (and its related types `IntersectionObserverEntry`, `IntersectionObserverCallback` and `IntersectionObserverOptions`) to React Native's library definitions, reflecting the features supported by the React Native implementations. Changelog: [internal] Reviewed By: fabriziocucci Differential Revision: D102597515 fbshipit-source-id: 86ff8d90aeb26f7f950a968b93596808da834951 --- packages/react-native/flow/bom.js.flow | 92 +++++++++++++++++++ .../IntersectionObserverMDNExample.js | 4 - 2 files changed, 92 insertions(+), 4 deletions(-) diff --git a/packages/react-native/flow/bom.js.flow b/packages/react-native/flow/bom.js.flow index c6855f3df77..d6466e09d6f 100644 --- a/packages/react-native/flow/bom.js.flow +++ b/packages/react-native/flow/bom.js.flow @@ -327,6 +327,98 @@ declare class DOMRectList { [index: number]: DOMRect; } +declare class MutationRecord { + // Always an empty NodeList for `attributes` and `characterData` mutations. + // React Native currently only supports `childList`, so this contains the + // added nodes for that mutation type. + +addedNodes: NodeList; + // Always `null` in React Native (only `childList` mutations are supported). + +attributeName: null; + // Always `null` in React Native (only `childList` mutations are supported). + +nextSibling: null; + // Always `null` in React Native (only `childList` mutations are supported, + // and `attributeOldValue`/`characterDataOldValue` are not supported). + +oldValue: null; + // Always `null` in React Native (only `childList` mutations are supported). + +previousSibling: null; + // Always an empty NodeList for `attributes` and `characterData` mutations. + // React Native currently only supports `childList`, so this contains the + // removed nodes for that mutation type. + +removedNodes: NodeList; + +target: Node; + // React Native currently only supports `childList` mutations. + +type: 'childList'; +} + +// React Native currently only supports `childList` mutations, so `childList` +// is required and must be `true`. The `attributes`/`attributeFilter`/ +// `attributeOldValue`/`characterData`/`characterDataOldValue` options are not +// supported and will throw if provided. +declare type MutationObserverInit = { + +childList: true, + +subtree?: boolean, + ... +}; + +declare class MutationObserver { + constructor( + callback: ( + arr: Array, + observer: MutationObserver, + ) => unknown, + ): void; + disconnect(): void; + observe(target: Node, options: MutationObserverInit): void; +} + +declare type IntersectionObserverEntry = { + +boundingClientRect: DOMRectReadOnly, + +intersectionRatio: number, + +intersectionRect: DOMRectReadOnly, + +isIntersecting: boolean, + // Always non-null in React Native. + +rootBounds: DOMRectReadOnly, + // React Native-specific extension. Equivalent to `intersectionRatio` but + // computed against the `rnRootThreshold` root-relative thresholds. + +rnRootIntersectionRatio: number, + +target: Element, + +time: DOMHighResTimeStamp, + ... +}; + +declare type IntersectionObserverCallback = ( + entries: Array, + observer: IntersectionObserver, +) => unknown; + +declare type IntersectionObserverOptions = { + root?: Node | null, + rootMargin?: string, + threshold?: number | Array, + // React Native-specific extension. Thresholds expressed as a fraction of the + // root's size (instead of the target's size). + rnRootThreshold?: number | Array, + ... +}; + +// The `delay`, `scrollMargin` and `trackVisibility` options are not supported +// in React Native and will throw if provided. +declare class IntersectionObserver { + constructor( + callback: IntersectionObserverCallback, + options?: IntersectionObserverOptions, + ): void; + disconnect(): void; + observe(target: Element): void; + +root: Element | null; + +rootMargin: string; + // React Native-specific extension. The thresholds expressed as a fraction of + // the root's size (set via the `rnRootThreshold` option). + +rnRootThresholds: ReadonlyArray | null; + +thresholds: ReadonlyArray; + unobserve(target: Element): void; +} + declare class CloseEvent extends Event { code: number; reason: string; diff --git a/packages/rn-tester/js/examples/IntersectionObserver/IntersectionObserverMDNExample.js b/packages/rn-tester/js/examples/IntersectionObserver/IntersectionObserverMDNExample.js index 5673c516830..557c77a1a90 100644 --- a/packages/rn-tester/js/examples/IntersectionObserver/IntersectionObserverMDNExample.js +++ b/packages/rn-tester/js/examples/IntersectionObserver/IntersectionObserverMDNExample.js @@ -8,8 +8,6 @@ * @format */ -import type IntersectionObserverType from 'react-native/src/private/webapis/intersectionobserver/IntersectionObserver'; - import {RNTesterThemeContext} from '../../components/RNTesterTheme'; import * as React from 'react'; import { @@ -21,8 +19,6 @@ import { } from 'react'; import {Button, ScrollView, StyleSheet, Text, View} from 'react-native'; -declare var IntersectionObserver: Class; - export const name = 'IntersectionObserver MDN Example'; export const title = name; export const description =