@wordpress/element 8.0.0 → 8.0.2-next.v.202606191442.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.
- package/CHANGELOG.md +3 -1
- package/README.md +2 -6
- package/build/index.cjs.map +1 -1
- package/build/platform.cjs.map +2 -2
- package/build/serialize.cjs.map +1 -1
- package/build-module/index.mjs.map +1 -1
- package/build-module/platform.mjs.map +2 -2
- package/build-module/serialize.mjs.map +1 -1
- package/build-types/platform.d.ts +1 -13
- package/build-types/platform.d.ts.map +1 -1
- package/package.json +10 -8
- package/src/index.ts +0 -6
- package/src/platform.ts +1 -14
- package/src/test/platform.js +10 -2
- package/build/react-polyfill-base.cjs +0 -132
- package/build/react-polyfill-base.cjs.map +0 -7
- package/build/react-polyfill.cjs +0 -80
- package/build/react-polyfill.cjs.map +0 -7
- package/build-module/react-polyfill-base.mjs +0 -104
- package/build-module/react-polyfill-base.mjs.map +0 -7
- package/build-module/react-polyfill.mjs +0 -47
- package/build-module/react-polyfill.mjs.map +0 -7
- package/build-types/react-polyfill-base.d.ts +0 -5
- package/build-types/react-polyfill-base.d.ts.map +0 -1
- package/build-types/react-polyfill.d.ts +0 -42
- package/build-types/react-polyfill.d.ts.map +0 -1
- package/src/platform.android.js +0 -21
- package/src/platform.ios.js +0 -21
- package/src/react-platform.native.js +0 -14
- package/src/react-polyfill-base.ts +0 -133
- package/src/react-polyfill.ts +0 -99
- package/src/test/find-dom-node.js +0 -109
- package/src/test/platform.native.js +0 -15
- package/src/test/react-polyfill.js +0 -77
|
@@ -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
|
-
}
|
package/src/react-polyfill.ts
DELETED
|
@@ -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,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal dependencies
|
|
3
|
-
*/
|
|
4
|
-
import Platform from '../platform';
|
|
5
|
-
|
|
6
|
-
describe( 'Platform', () => {
|
|
7
|
-
it( 'is chooses the right thing', () => {
|
|
8
|
-
const selection = Platform.select( {
|
|
9
|
-
web: 'web',
|
|
10
|
-
native: 'native',
|
|
11
|
-
} );
|
|
12
|
-
|
|
13
|
-
expect( selection ).toBe( 'native' );
|
|
14
|
-
} );
|
|
15
|
-
} );
|
|
@@ -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
|
-
} );
|