@wordpress/element 8.0.1 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,104 +0,0 @@
1
- // packages/element/src/react-polyfill-base.ts
2
- import { flushSync } from "react-dom";
3
- import { createRoot, hydrateRoot } from "react-dom/client";
4
- var internalsKey = "_reactInternals";
5
- var HostComponent = 5;
6
- var HostText = 6;
7
- function findCurrentFiber(fiber) {
8
- if (!fiber.alternate) {
9
- return fiber;
10
- }
11
- let node = fiber;
12
- while (node.return) {
13
- node = node.return;
14
- }
15
- if (node.stateNode.current === node) {
16
- return fiber;
17
- }
18
- return fiber.alternate;
19
- }
20
- function findHostFiber(fiber) {
21
- const current = findCurrentFiber(fiber);
22
- if (!current) {
23
- return null;
24
- }
25
- return findHostFiberImpl(current);
26
- }
27
- function findHostFiberImpl(fiber) {
28
- if (fiber.tag === HostComponent || fiber.tag === HostText) {
29
- return fiber;
30
- }
31
- let child = fiber.child;
32
- while (child) {
33
- const hostFiber = findHostFiberImpl(child);
34
- if (hostFiber) {
35
- return hostFiber;
36
- }
37
- child = child.sibling;
38
- }
39
- return null;
40
- }
41
- function findDOMNode(instance) {
42
- if (instance === null || instance === void 0) {
43
- return null;
44
- }
45
- if (instance.nodeType !== void 0) {
46
- return instance;
47
- }
48
- const fiber = instance[internalsKey];
49
- if (fiber === void 0) {
50
- if (typeof instance.render === "function") {
51
- throw new Error("Unable to find node on an unmounted component.");
52
- }
53
- const keys = Object.keys(instance).join(",");
54
- throw new Error(
55
- `Argument appears to not be a ReactComponent. Keys: ${keys}`
56
- );
57
- }
58
- const hostFiber = findHostFiber(fiber);
59
- return hostFiber?.stateNode ?? null;
60
- }
61
- var roots = /* @__PURE__ */ new WeakMap();
62
- function render(element, container, callback) {
63
- let root = roots.get(container);
64
- if (!root) {
65
- root = createRoot(container);
66
- roots.set(container, root);
67
- }
68
- flushSync(() => {
69
- root.render(element);
70
- });
71
- if (typeof callback === "function") {
72
- callback();
73
- }
74
- }
75
- function hydrate(element, container, callback) {
76
- let root = roots.get(container);
77
- if (!root) {
78
- root = hydrateRoot(container, element);
79
- roots.set(container, root);
80
- } else {
81
- root.render(element);
82
- }
83
- if (typeof callback === "function") {
84
- callback();
85
- }
86
- }
87
- function unmountComponentAtNode(container) {
88
- const root = roots.get(container);
89
- if (!root) {
90
- return false;
91
- }
92
- flushSync(() => {
93
- root.unmount();
94
- });
95
- roots.delete(container);
96
- return true;
97
- }
98
- export {
99
- findDOMNode,
100
- hydrate,
101
- render,
102
- unmountComponentAtNode
103
- };
104
- //# sourceMappingURL=react-polyfill-base.mjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/react-polyfill-base.ts"],
4
- "sourcesContent": ["import { flushSync } from 'react-dom';\nimport { createRoot, hydrateRoot } from 'react-dom/client';\nimport type { Root } from 'react-dom/client';\n\nconst internalsKey = '_reactInternals';\n\n// HostComponent fiber tag, represents a DOM element like <div>.\nconst HostComponent = 5;\nconst HostText = 6;\n\nfunction findCurrentFiber( fiber: any ): any {\n\tif ( ! fiber.alternate ) {\n\t\t// First mount \u2014 only one version exists, and it's current.\n\t\treturn fiber;\n\t}\n\n\t// Walk up to the HostRoot to figure out which tree this fiber is on.\n\tlet node = fiber;\n\twhile ( node.return ) {\n\t\tnode = node.return;\n\t}\n\n\t// The root's stateNode.current points to the current tree's root fiber.\n\tif ( node.stateNode.current === node ) {\n\t\t// We walked up the current tree, so `fiber` is already current.\n\t\treturn fiber;\n\t}\n\n\t// We walked up the alternate tree \u2014 switch to the current version.\n\treturn fiber.alternate;\n}\n\nfunction findHostFiber( fiber: any ): any {\n\tconst current = findCurrentFiber( fiber );\n\tif ( ! current ) {\n\t\treturn null;\n\t}\n\treturn findHostFiberImpl( current );\n}\n\nfunction findHostFiberImpl( fiber: any ): any {\n\tif ( fiber.tag === HostComponent || fiber.tag === HostText ) {\n\t\treturn fiber;\n\t}\n\n\tlet child = fiber.child;\n\twhile ( child ) {\n\t\tconst hostFiber = findHostFiberImpl( child );\n\t\tif ( hostFiber ) {\n\t\t\treturn hostFiber;\n\t\t}\n\t\tchild = child.sibling;\n\t}\n\n\treturn null;\n}\n\nexport function findDOMNode( instance: any ): Element | Text | null {\n\tif ( instance === null || instance === undefined ) {\n\t\treturn null;\n\t}\n\n\tif ( instance.nodeType !== undefined ) {\n\t\treturn instance as Element | Text;\n\t}\n\n\tconst fiber = instance[ internalsKey ];\n\tif ( fiber === undefined ) {\n\t\tif ( typeof instance.render === 'function' ) {\n\t\t\tthrow new Error( 'Unable to find node on an unmounted component.' );\n\t\t}\n\t\tconst keys = Object.keys( instance ).join( ',' );\n\t\tthrow new Error(\n\t\t\t`Argument appears to not be a ReactComponent. Keys: ${ keys }`\n\t\t);\n\t}\n\n\tconst hostFiber = findHostFiber( fiber );\n\treturn hostFiber?.stateNode ?? null;\n}\n\nconst roots = new WeakMap< Element, Root >();\n\nexport function render(\n\telement: React.ReactNode,\n\tcontainer: Element,\n\tcallback?: () => void\n): void {\n\tlet root = roots.get( container );\n\tif ( ! root ) {\n\t\troot = createRoot( container );\n\t\troots.set( container, root );\n\t}\n\n\tflushSync( () => {\n\t\troot.render( element );\n\t} );\n\n\tif ( typeof callback === 'function' ) {\n\t\tcallback();\n\t}\n}\n\nexport function hydrate(\n\telement: React.ReactNode,\n\tcontainer: Element,\n\tcallback?: () => void\n): void {\n\tlet root = roots.get( container );\n\tif ( ! root ) {\n\t\troot = hydrateRoot( container, element );\n\t\troots.set( container, root );\n\t} else {\n\t\troot.render( element );\n\t}\n\n\tif ( typeof callback === 'function' ) {\n\t\tcallback();\n\t}\n}\n\nexport function unmountComponentAtNode( container: Element ): boolean {\n\tconst root = roots.get( container );\n\tif ( ! root ) {\n\t\treturn false;\n\t}\n\n\tflushSync( () => {\n\t\troot.unmount();\n\t} );\n\troots.delete( container );\n\treturn true;\n}\n"],
5
- "mappings": ";AAAA,SAAS,iBAAiB;AAC1B,SAAS,YAAY,mBAAmB;AAGxC,IAAM,eAAe;AAGrB,IAAM,gBAAgB;AACtB,IAAM,WAAW;AAEjB,SAAS,iBAAkB,OAAkB;AAC5C,MAAK,CAAE,MAAM,WAAY;AAExB,WAAO;AAAA,EACR;AAGA,MAAI,OAAO;AACX,SAAQ,KAAK,QAAS;AACrB,WAAO,KAAK;AAAA,EACb;AAGA,MAAK,KAAK,UAAU,YAAY,MAAO;AAEtC,WAAO;AAAA,EACR;AAGA,SAAO,MAAM;AACd;AAEA,SAAS,cAAe,OAAkB;AACzC,QAAM,UAAU,iBAAkB,KAAM;AACxC,MAAK,CAAE,SAAU;AAChB,WAAO;AAAA,EACR;AACA,SAAO,kBAAmB,OAAQ;AACnC;AAEA,SAAS,kBAAmB,OAAkB;AAC7C,MAAK,MAAM,QAAQ,iBAAiB,MAAM,QAAQ,UAAW;AAC5D,WAAO;AAAA,EACR;AAEA,MAAI,QAAQ,MAAM;AAClB,SAAQ,OAAQ;AACf,UAAM,YAAY,kBAAmB,KAAM;AAC3C,QAAK,WAAY;AAChB,aAAO;AAAA,IACR;AACA,YAAQ,MAAM;AAAA,EACf;AAEA,SAAO;AACR;AAEO,SAAS,YAAa,UAAuC;AACnE,MAAK,aAAa,QAAQ,aAAa,QAAY;AAClD,WAAO;AAAA,EACR;AAEA,MAAK,SAAS,aAAa,QAAY;AACtC,WAAO;AAAA,EACR;AAEA,QAAM,QAAQ,SAAU,YAAa;AACrC,MAAK,UAAU,QAAY;AAC1B,QAAK,OAAO,SAAS,WAAW,YAAa;AAC5C,YAAM,IAAI,MAAO,gDAAiD;AAAA,IACnE;AACA,UAAM,OAAO,OAAO,KAAM,QAAS,EAAE,KAAM,GAAI;AAC/C,UAAM,IAAI;AAAA,MACT,sDAAuD,IAAK;AAAA,IAC7D;AAAA,EACD;AAEA,QAAM,YAAY,cAAe,KAAM;AACvC,SAAO,WAAW,aAAa;AAChC;AAEA,IAAM,QAAQ,oBAAI,QAAyB;AAEpC,SAAS,OACf,SACA,WACA,UACO;AACP,MAAI,OAAO,MAAM,IAAK,SAAU;AAChC,MAAK,CAAE,MAAO;AACb,WAAO,WAAY,SAAU;AAC7B,UAAM,IAAK,WAAW,IAAK;AAAA,EAC5B;AAEA,YAAW,MAAM;AAChB,SAAK,OAAQ,OAAQ;AAAA,EACtB,CAAE;AAEF,MAAK,OAAO,aAAa,YAAa;AACrC,aAAS;AAAA,EACV;AACD;AAEO,SAAS,QACf,SACA,WACA,UACO;AACP,MAAI,OAAO,MAAM,IAAK,SAAU;AAChC,MAAK,CAAE,MAAO;AACb,WAAO,YAAa,WAAW,OAAQ;AACvC,UAAM,IAAK,WAAW,IAAK;AAAA,EAC5B,OAAO;AACN,SAAK,OAAQ,OAAQ;AAAA,EACtB;AAEA,MAAK,OAAO,aAAa,YAAa;AACrC,aAAS;AAAA,EACV;AACD;AAEO,SAAS,uBAAwB,WAA8B;AACrE,QAAM,OAAO,MAAM,IAAK,SAAU;AAClC,MAAK,CAAE,MAAO;AACb,WAAO;AAAA,EACR;AAEA,YAAW,MAAM;AAChB,SAAK,QAAQ;AAAA,EACd,CAAE;AACF,QAAM,OAAQ,SAAU;AACxB,SAAO;AACR;",
6
- "names": []
7
- }
@@ -1,47 +0,0 @@
1
- // packages/element/src/react-polyfill.ts
2
- import deprecated from "@wordpress/deprecated";
3
- import {
4
- findDOMNode as findDOMNodeBase,
5
- render as renderBase,
6
- hydrate as hydrateBase,
7
- unmountComponentAtNode as unmountComponentAtNodeBase
8
- } from "./react-polyfill-base.mjs";
9
- function findDOMNode(instance) {
10
- deprecated("wp.element.findDOMNode", {
11
- since: "7.1",
12
- alternative: "DOM refs",
13
- link: "https://react.dev/reference/react-dom/findDOMNode"
14
- });
15
- return findDOMNodeBase(instance);
16
- }
17
- function render(element, container, callback) {
18
- deprecated("wp.element.render", {
19
- since: "6.2",
20
- alternative: "wp.element.createRoot",
21
- link: "https://react.dev/reference/react-dom/client/createRoot"
22
- });
23
- renderBase(element, container, callback);
24
- }
25
- function hydrate(element, container, callback) {
26
- deprecated("wp.element.hydrate", {
27
- since: "6.2",
28
- alternative: "wp.element.hydrateRoot",
29
- link: "https://react.dev/reference/react-dom/client/hydrateRoot"
30
- });
31
- hydrateBase(element, container, callback);
32
- }
33
- function unmountComponentAtNode(container) {
34
- deprecated("wp.element.unmountComponentAtNode", {
35
- since: "6.2",
36
- alternative: "root.unmount()",
37
- link: "https://react.dev/reference/react-dom/client/createRoot#root-unmount"
38
- });
39
- return unmountComponentAtNodeBase(container);
40
- }
41
- export {
42
- findDOMNode,
43
- hydrate,
44
- render,
45
- unmountComponentAtNode
46
- };
47
- //# sourceMappingURL=react-polyfill.mjs.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/react-polyfill.ts"],
4
- "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport {\n\tfindDOMNode as findDOMNodeBase,\n\trender as renderBase,\n\thydrate as hydrateBase,\n\tunmountComponentAtNode as unmountComponentAtNodeBase,\n} from './react-polyfill-base';\n\n/**\n * Finds the DOM node of a React component instance.\n *\n * @deprecated since WordPress 7.1.0. Use DOM refs instead.\n * @see https://react.dev/reference/react-dom/findDOMNode\n *\n * @param instance Component's instance.\n */\nexport function findDOMNode( instance: any ): Element | Text | null {\n\tdeprecated( 'wp.element.findDOMNode', {\n\t\tsince: '7.1',\n\t\talternative: 'DOM refs',\n\t\tlink: 'https://react.dev/reference/react-dom/findDOMNode',\n\t} );\n\n\treturn findDOMNodeBase( instance );\n}\n\n/**\n * Renders a given element into the target DOM node.\n *\n * @deprecated since WordPress 6.2.0. Use `createRoot` instead.\n * @see https://react.dev/reference/react-dom/render\n *\n * @param element Element to render.\n * @param container DOM node into which to render.\n * @param callback Optional callback called after render.\n */\nexport function render(\n\telement: React.ReactNode,\n\tcontainer: Element,\n\tcallback?: () => void\n): void {\n\tdeprecated( 'wp.element.render', {\n\t\tsince: '6.2',\n\t\talternative: 'wp.element.createRoot',\n\t\tlink: 'https://react.dev/reference/react-dom/client/createRoot',\n\t} );\n\n\trenderBase( element, container, callback );\n}\n\n/**\n * Hydrates a given element into the target DOM node.\n *\n * @deprecated since WordPress 6.2.0. Use `hydrateRoot` instead.\n * @see https://react.dev/reference/react-dom/hydrate\n *\n * @param element Element to hydrate.\n * @param container DOM node to hydrate.\n * @param callback Optional callback called after hydration.\n */\nexport function hydrate(\n\telement: React.ReactNode,\n\tcontainer: Element,\n\tcallback?: () => void\n): void {\n\tdeprecated( 'wp.element.hydrate', {\n\t\tsince: '6.2',\n\t\talternative: 'wp.element.hydrateRoot',\n\t\tlink: 'https://react.dev/reference/react-dom/client/hydrateRoot',\n\t} );\n\n\thydrateBase( element, container, callback );\n}\n\n/**\n * Removes any mounted element from the target DOM node.\n *\n * @deprecated since WordPress 6.2.0. Use `root.unmount()` instead.\n * @see https://react.dev/reference/react-dom/unmountComponentAtNode\n *\n * @param container DOM node in which to unmount.\n * @return Whether the node was unmounted.\n */\nexport function unmountComponentAtNode( container: Element ): boolean {\n\tdeprecated( 'wp.element.unmountComponentAtNode', {\n\t\tsince: '6.2',\n\t\talternative: 'root.unmount()',\n\t\tlink: 'https://react.dev/reference/react-dom/client/createRoot#root-unmount',\n\t} );\n\n\treturn unmountComponentAtNodeBase( container );\n}\n"],
5
- "mappings": ";AAGA,OAAO,gBAAgB;AAKvB;AAAA,EACC,eAAe;AAAA,EACf,UAAU;AAAA,EACV,WAAW;AAAA,EACX,0BAA0B;AAAA,OACpB;AAUA,SAAS,YAAa,UAAuC;AACnE,aAAY,0BAA0B;AAAA,IACrC,OAAO;AAAA,IACP,aAAa;AAAA,IACb,MAAM;AAAA,EACP,CAAE;AAEF,SAAO,gBAAiB,QAAS;AAClC;AAYO,SAAS,OACf,SACA,WACA,UACO;AACP,aAAY,qBAAqB;AAAA,IAChC,OAAO;AAAA,IACP,aAAa;AAAA,IACb,MAAM;AAAA,EACP,CAAE;AAEF,aAAY,SAAS,WAAW,QAAS;AAC1C;AAYO,SAAS,QACf,SACA,WACA,UACO;AACP,aAAY,sBAAsB;AAAA,IACjC,OAAO;AAAA,IACP,aAAa;AAAA,IACb,MAAM;AAAA,EACP,CAAE;AAEF,cAAa,SAAS,WAAW,QAAS;AAC3C;AAWO,SAAS,uBAAwB,WAA8B;AACrE,aAAY,qCAAqC;AAAA,IAChD,OAAO;AAAA,IACP,aAAa;AAAA,IACb,MAAM;AAAA,EACP,CAAE;AAEF,SAAO,2BAA4B,SAAU;AAC9C;",
6
- "names": []
7
- }
@@ -1,10 +0,0 @@
1
- /**
2
- * Finds the DOM node of a React component instance.
3
- *
4
- * @deprecated since WordPress 7.1.0. Use DOM refs instead.
5
- * @see https://react.dev/reference/react-dom/findDOMNode
6
- *
7
- * @param instance Component's instance.
8
- */
9
- export default function findDOMNode(instance: any): Element | Text | null;
10
- //# sourceMappingURL=find-dom-node.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"find-dom-node.d.ts","sourceRoot":"","sources":["../src/find-dom-node.ts"],"names":[],"mappings":"AAuDA;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,QAAQ,EAAE,GAAG,GAAI,OAAO,GAAG,IAAI,GAAG,IAAI,CA4B1E"}
@@ -1,5 +0,0 @@
1
- export declare function findDOMNode(instance: any): Element | Text | null;
2
- export declare function render(element: React.ReactNode, container: Element, callback?: () => void): void;
3
- export declare function hydrate(element: React.ReactNode, container: Element, callback?: () => void): void;
4
- export declare function unmountComponentAtNode(container: Element): boolean;
5
- //# sourceMappingURL=react-polyfill-base.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"react-polyfill-base.d.ts","sourceRoot":"","sources":["../src/react-polyfill-base.ts"],"names":[],"mappings":"AAyDA,wBAAgB,WAAW,CAAE,QAAQ,EAAE,GAAG,GAAI,OAAO,GAAG,IAAI,GAAG,IAAI,CAsBlE;AAID,wBAAgB,MAAM,CACrB,OAAO,EAAE,KAAK,CAAC,SAAS,EACxB,SAAS,EAAE,OAAO,EAClB,QAAQ,CAAC,EAAE,MAAM,IAAI,GACnB,IAAI,CAcN;AAED,wBAAgB,OAAO,CACtB,OAAO,EAAE,KAAK,CAAC,SAAS,EACxB,SAAS,EAAE,OAAO,EAClB,QAAQ,CAAC,EAAE,MAAM,IAAI,GACnB,IAAI,CAYN;AAED,wBAAgB,sBAAsB,CAAE,SAAS,EAAE,OAAO,GAAI,OAAO,CAWpE"}
@@ -1,42 +0,0 @@
1
- /**
2
- * Finds the DOM node of a React component instance.
3
- *
4
- * @deprecated since WordPress 7.1.0. Use DOM refs instead.
5
- * @see https://react.dev/reference/react-dom/findDOMNode
6
- *
7
- * @param instance Component's instance.
8
- */
9
- export declare function findDOMNode(instance: any): Element | Text | null;
10
- /**
11
- * Renders a given element into the target DOM node.
12
- *
13
- * @deprecated since WordPress 6.2.0. Use `createRoot` instead.
14
- * @see https://react.dev/reference/react-dom/render
15
- *
16
- * @param element Element to render.
17
- * @param container DOM node into which to render.
18
- * @param callback Optional callback called after render.
19
- */
20
- export declare function render(element: React.ReactNode, container: Element, callback?: () => void): void;
21
- /**
22
- * Hydrates a given element into the target DOM node.
23
- *
24
- * @deprecated since WordPress 6.2.0. Use `hydrateRoot` instead.
25
- * @see https://react.dev/reference/react-dom/hydrate
26
- *
27
- * @param element Element to hydrate.
28
- * @param container DOM node to hydrate.
29
- * @param callback Optional callback called after hydration.
30
- */
31
- export declare function hydrate(element: React.ReactNode, container: Element, callback?: () => void): void;
32
- /**
33
- * Removes any mounted element from the target DOM node.
34
- *
35
- * @deprecated since WordPress 6.2.0. Use `root.unmount()` instead.
36
- * @see https://react.dev/reference/react-dom/unmountComponentAtNode
37
- *
38
- * @param container DOM node in which to unmount.
39
- * @return Whether the node was unmounted.
40
- */
41
- export declare function unmountComponentAtNode(container: Element): boolean;
42
- //# sourceMappingURL=react-polyfill.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"react-polyfill.d.ts","sourceRoot":"","sources":["../src/react-polyfill.ts"],"names":[],"mappings":"AAeA;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAE,QAAQ,EAAE,GAAG,GAAI,OAAO,GAAG,IAAI,GAAG,IAAI,CAQlE;AAED;;;;;;;;;GASG;AACH,wBAAgB,MAAM,CACrB,OAAO,EAAE,KAAK,CAAC,SAAS,EACxB,SAAS,EAAE,OAAO,EAClB,QAAQ,CAAC,EAAE,MAAM,IAAI,GACnB,IAAI,CAQN;AAED;;;;;;;;;GASG;AACH,wBAAgB,OAAO,CACtB,OAAO,EAAE,KAAK,CAAC,SAAS,EACxB,SAAS,EAAE,OAAO,EAClB,QAAQ,CAAC,EAAE,MAAM,IAAI,GACnB,IAAI,CAQN;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAE,SAAS,EAAE,OAAO,GAAI,OAAO,CAQpE"}
@@ -1,133 +0,0 @@
1
- import { flushSync } from 'react-dom';
2
- import { createRoot, hydrateRoot } from 'react-dom/client';
3
- import type { Root } from 'react-dom/client';
4
-
5
- const internalsKey = '_reactInternals';
6
-
7
- // HostComponent fiber tag, represents a DOM element like <div>.
8
- const HostComponent = 5;
9
- const HostText = 6;
10
-
11
- function findCurrentFiber( fiber: any ): any {
12
- if ( ! fiber.alternate ) {
13
- // First mount — only one version exists, and it's current.
14
- return fiber;
15
- }
16
-
17
- // Walk up to the HostRoot to figure out which tree this fiber is on.
18
- let node = fiber;
19
- while ( node.return ) {
20
- node = node.return;
21
- }
22
-
23
- // The root's stateNode.current points to the current tree's root fiber.
24
- if ( node.stateNode.current === node ) {
25
- // We walked up the current tree, so `fiber` is already current.
26
- return fiber;
27
- }
28
-
29
- // We walked up the alternate tree — switch to the current version.
30
- return fiber.alternate;
31
- }
32
-
33
- function findHostFiber( fiber: any ): any {
34
- const current = findCurrentFiber( fiber );
35
- if ( ! current ) {
36
- return null;
37
- }
38
- return findHostFiberImpl( current );
39
- }
40
-
41
- function findHostFiberImpl( fiber: any ): any {
42
- if ( fiber.tag === HostComponent || fiber.tag === HostText ) {
43
- return fiber;
44
- }
45
-
46
- let child = fiber.child;
47
- while ( child ) {
48
- const hostFiber = findHostFiberImpl( child );
49
- if ( hostFiber ) {
50
- return hostFiber;
51
- }
52
- child = child.sibling;
53
- }
54
-
55
- return null;
56
- }
57
-
58
- export function findDOMNode( instance: any ): Element | Text | null {
59
- if ( instance === null || instance === undefined ) {
60
- return null;
61
- }
62
-
63
- if ( instance.nodeType !== undefined ) {
64
- return instance as Element | Text;
65
- }
66
-
67
- const fiber = instance[ internalsKey ];
68
- if ( fiber === undefined ) {
69
- if ( typeof instance.render === 'function' ) {
70
- throw new Error( 'Unable to find node on an unmounted component.' );
71
- }
72
- const keys = Object.keys( instance ).join( ',' );
73
- throw new Error(
74
- `Argument appears to not be a ReactComponent. Keys: ${ keys }`
75
- );
76
- }
77
-
78
- const hostFiber = findHostFiber( fiber );
79
- return hostFiber?.stateNode ?? null;
80
- }
81
-
82
- const roots = new WeakMap< Element, Root >();
83
-
84
- export function render(
85
- element: React.ReactNode,
86
- container: Element,
87
- callback?: () => void
88
- ): void {
89
- let root = roots.get( container );
90
- if ( ! root ) {
91
- root = createRoot( container );
92
- roots.set( container, root );
93
- }
94
-
95
- flushSync( () => {
96
- root.render( element );
97
- } );
98
-
99
- if ( typeof callback === 'function' ) {
100
- callback();
101
- }
102
- }
103
-
104
- export function hydrate(
105
- element: React.ReactNode,
106
- container: Element,
107
- callback?: () => void
108
- ): void {
109
- let root = roots.get( container );
110
- if ( ! root ) {
111
- root = hydrateRoot( container, element );
112
- roots.set( container, root );
113
- } else {
114
- root.render( element );
115
- }
116
-
117
- if ( typeof callback === 'function' ) {
118
- callback();
119
- }
120
- }
121
-
122
- export function unmountComponentAtNode( container: Element ): boolean {
123
- const root = roots.get( container );
124
- if ( ! root ) {
125
- return false;
126
- }
127
-
128
- flushSync( () => {
129
- root.unmount();
130
- } );
131
- roots.delete( container );
132
- return true;
133
- }
@@ -1,99 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import deprecated from '@wordpress/deprecated';
5
-
6
- /**
7
- * Internal dependencies
8
- */
9
- import {
10
- findDOMNode as findDOMNodeBase,
11
- render as renderBase,
12
- hydrate as hydrateBase,
13
- unmountComponentAtNode as unmountComponentAtNodeBase,
14
- } from './react-polyfill-base';
15
-
16
- /**
17
- * Finds the DOM node of a React component instance.
18
- *
19
- * @deprecated since WordPress 7.1.0. Use DOM refs instead.
20
- * @see https://react.dev/reference/react-dom/findDOMNode
21
- *
22
- * @param instance Component's instance.
23
- */
24
- export function findDOMNode( instance: any ): Element | Text | null {
25
- deprecated( 'wp.element.findDOMNode', {
26
- since: '7.1',
27
- alternative: 'DOM refs',
28
- link: 'https://react.dev/reference/react-dom/findDOMNode',
29
- } );
30
-
31
- return findDOMNodeBase( instance );
32
- }
33
-
34
- /**
35
- * Renders a given element into the target DOM node.
36
- *
37
- * @deprecated since WordPress 6.2.0. Use `createRoot` instead.
38
- * @see https://react.dev/reference/react-dom/render
39
- *
40
- * @param element Element to render.
41
- * @param container DOM node into which to render.
42
- * @param callback Optional callback called after render.
43
- */
44
- export function render(
45
- element: React.ReactNode,
46
- container: Element,
47
- callback?: () => void
48
- ): void {
49
- deprecated( 'wp.element.render', {
50
- since: '6.2',
51
- alternative: 'wp.element.createRoot',
52
- link: 'https://react.dev/reference/react-dom/client/createRoot',
53
- } );
54
-
55
- renderBase( element, container, callback );
56
- }
57
-
58
- /**
59
- * Hydrates a given element into the target DOM node.
60
- *
61
- * @deprecated since WordPress 6.2.0. Use `hydrateRoot` instead.
62
- * @see https://react.dev/reference/react-dom/hydrate
63
- *
64
- * @param element Element to hydrate.
65
- * @param container DOM node to hydrate.
66
- * @param callback Optional callback called after hydration.
67
- */
68
- export function hydrate(
69
- element: React.ReactNode,
70
- container: Element,
71
- callback?: () => void
72
- ): void {
73
- deprecated( 'wp.element.hydrate', {
74
- since: '6.2',
75
- alternative: 'wp.element.hydrateRoot',
76
- link: 'https://react.dev/reference/react-dom/client/hydrateRoot',
77
- } );
78
-
79
- hydrateBase( element, container, callback );
80
- }
81
-
82
- /**
83
- * Removes any mounted element from the target DOM node.
84
- *
85
- * @deprecated since WordPress 6.2.0. Use `root.unmount()` instead.
86
- * @see https://react.dev/reference/react-dom/unmountComponentAtNode
87
- *
88
- * @param container DOM node in which to unmount.
89
- * @return Whether the node was unmounted.
90
- */
91
- export function unmountComponentAtNode( container: Element ): boolean {
92
- deprecated( 'wp.element.unmountComponentAtNode', {
93
- since: '6.2',
94
- alternative: 'root.unmount()',
95
- link: 'https://react.dev/reference/react-dom/client/createRoot#root-unmount',
96
- } );
97
-
98
- return unmountComponentAtNodeBase( container );
99
- }
@@ -1,109 +0,0 @@
1
- import { act, render } from '@testing-library/react';
2
- import { Component } from '..';
3
- import { findDOMNode as findDOMNodePolyfill } from '../react-polyfill';
4
-
5
- describe.skip( 'findDOMNode', () => {
6
- it( 'should return null when passed null', () => {
7
- expect( findDOMNodePolyfill( null ) ).toBeNull();
8
- expect( console ).toHaveWarned();
9
- } );
10
-
11
- it( 'should return the element when passed a DOM element', () => {
12
- const div = document.createElement( 'div' );
13
- expect( findDOMNodePolyfill( div ) ).toBe( div );
14
- } );
15
-
16
- it( 'should track the current DOM node across re-renders', () => {
17
- class Toggle extends Component {
18
- constructor( props ) {
19
- super( props );
20
- this.state = { count: 0 };
21
- this.results = [];
22
- }
23
-
24
- componentDidMount() {
25
- this.recordNode();
26
- }
27
-
28
- componentDidUpdate() {
29
- this.recordNode();
30
- }
31
-
32
- recordNode() {
33
- const node = findDOMNodePolyfill( this );
34
- this.results.push( node.tagName );
35
- }
36
-
37
- render() {
38
- return this.state.count % 2 === 0 ? (
39
- <div>even</div>
40
- ) : (
41
- <span>odd</span>
42
- );
43
- }
44
- }
45
-
46
- let toggleRef;
47
- render( <Toggle ref={ ( ref ) => ( toggleRef = ref ) } /> );
48
- act( () => toggleRef.setState( { count: 1 } ) );
49
- act( () => toggleRef.setState( { count: 2 } ) );
50
- act( () => toggleRef.setState( { count: 3 } ) );
51
-
52
- expect( toggleRef.results ).toEqual( [ 'DIV', 'SPAN', 'DIV', 'SPAN' ] );
53
- } );
54
-
55
- it( 'should return null for an unmounted component', () => {
56
- class MyComponent extends Component {
57
- render() {
58
- return <div>hello</div>;
59
- }
60
- }
61
-
62
- let instanceRef;
63
- const { rerender } = render(
64
- <MyComponent
65
- ref={ ( ref ) => {
66
- instanceRef = ref;
67
- return () => {};
68
- } }
69
- />
70
- );
71
-
72
- expect( findDOMNodePolyfill( instanceRef ) ).toBeInstanceOf(
73
- window.HTMLDivElement
74
- );
75
-
76
- rerender( <p>replaced</p> );
77
-
78
- expect( instanceRef ).not.toBeNull();
79
- expect( findDOMNodePolyfill( instanceRef ) ).toBeNull();
80
- } );
81
-
82
- it( 'should find DOM node rendered by a nested child component', () => {
83
- function Empty() {
84
- return null;
85
- }
86
-
87
- function Inner() {
88
- return <span className="inner">hello</span>;
89
- }
90
-
91
- class Outer extends Component {
92
- render() {
93
- return (
94
- <>
95
- <Empty />
96
- <Inner />
97
- </>
98
- );
99
- }
100
- }
101
-
102
- let outerRef;
103
- render( <Outer ref={ ( ref ) => ( outerRef = ref ) } /> );
104
-
105
- const node = findDOMNodePolyfill( outerRef );
106
- expect( node ).toBeInstanceOf( window.HTMLSpanElement );
107
- expect( node.className ).toBe( 'inner' );
108
- } );
109
- } );
@@ -1,77 +0,0 @@
1
- /**
2
- * Internal dependencies
3
- */
4
- import { createElement } from '..';
5
- import { render, hydrate, unmountComponentAtNode } from '../react-polyfill';
6
-
7
- describe.skip( 'render', () => {
8
- it( 'should render into a container', () => {
9
- const container = document.createElement( 'div' );
10
-
11
- render( createElement( 'p', null, 'hello' ), container );
12
-
13
- expect( container ).toHaveTextContent( 'hello' );
14
- expect( console ).toHaveWarned();
15
- } );
16
-
17
- it( 'should update the container on subsequent renders', () => {
18
- const container = document.createElement( 'div' );
19
-
20
- render( createElement( 'p', null, 'hello' ), container );
21
- render( createElement( 'p', null, 'world' ), container );
22
-
23
- expect( container ).toHaveTextContent( 'world' );
24
- } );
25
-
26
- it( 'should call the callback after rendering', () => {
27
- const container = document.createElement( 'div' );
28
- const callback = jest.fn();
29
-
30
- render( createElement( 'p', null, 'hello' ), container, callback );
31
-
32
- expect( callback ).toHaveBeenCalledTimes( 1 );
33
- expect( container ).toHaveTextContent( 'hello' );
34
- } );
35
- } );
36
-
37
- describe.skip( 'hydrate', () => {
38
- it( 'should hydrate into a container with matching markup', () => {
39
- const container = document.createElement( 'div' );
40
- container.innerHTML = '<p>hello</p>';
41
-
42
- hydrate( createElement( 'p', null, 'hello' ), container );
43
-
44
- expect( container ).toHaveTextContent( 'hello' );
45
- expect( console ).toHaveWarned();
46
- } );
47
-
48
- it( 'should call the callback after hydrating', () => {
49
- const container = document.createElement( 'div' );
50
- container.innerHTML = '<p>hello</p>';
51
- const callback = jest.fn();
52
-
53
- hydrate( createElement( 'p', null, 'hello' ), container, callback );
54
-
55
- expect( callback ).toHaveBeenCalledTimes( 1 );
56
- } );
57
- } );
58
-
59
- describe.skip( 'unmountComponentAtNode', () => {
60
- it( 'should return false when the container has no root', () => {
61
- const container = document.createElement( 'div' );
62
-
63
- expect( unmountComponentAtNode( container ) ).toBe( false );
64
- expect( console ).toHaveWarned();
65
- } );
66
-
67
- it( 'should unmount and return true when a root exists', () => {
68
- const container = document.createElement( 'div' );
69
-
70
- render( createElement( 'p', null, 'hello' ), container );
71
- expect( container ).toHaveTextContent( 'hello' );
72
-
73
- expect( unmountComponentAtNode( container ) ).toBe( true );
74
- expect( container ).toHaveTextContent( '' );
75
- expect( unmountComponentAtNode( container ) ).toBe( false );
76
- } );
77
- } );