PluginProbe
TablePress – Tables in WordPress made easy / 3.0.4
TablePress – Tables in WordPress made easy v3.0.4
3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.4.4 All 44 releases
tablepress / admin / js / common / react-loader.js

react-loader.js in TablePress – Tables in WordPress made easy 3.0.4, at admin/js/common/react-loader.js

66 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Common functions for loading React components in TablePress JS.
3 *
4 * @package TablePress
5 * @subpackage Views JavaScript
6 * @author Tobias Bäthge
7 * @since 2.2.0
8 */
9
10 /**
11 * WordPress dependencies.
12 */
13 import { StrictMode } from 'react';
14 import { createPortal, createRoot } from 'react-dom';
15 import { addFilter } from '@wordpress/hooks';
16
17 /**
18 * Initializes a React component on the page.
19 *
20 * @param {string} rootId HTML ID of the root element for the component.
21 * @param {Component} Component JSX component.
22 */
23 export const initializeReactComponent = ( rootId, Component ) => {
24 if ( process.env.DEVELOP ) {
25 Component = <StrictMode>{ Component }</StrictMode>;
26 }
27
28 const root = document.getElementById( rootId );
29 if ( root ) {
30 createRoot( root ).render( Component );
31 }
32 };
33
34 /**
35 * Initializes a React component on the page, in a React Portal, and registers its meta box.
36 *
37 * @param {string} slug Slug of the component/feature module.
38 * @param {string} screen Slug/action of the screen.
39 * @param {Component} Component JSX component.
40 */
41 export const initializeReactComponentInPortal = ( slug, screen, Component ) => {
42 addFilter(
43 `tablepress.${screen}ScreenFeatures`,
44 `tp/${slug}/${screen}-screen-feature`,
45 ( features ) => ( [ ...features, slug ] ),
46 );
47
48 addFilter(
49 `tablepress.${screen}ScreenPortals`,
50 `tp/${slug}/${screen}-screen-portal`,
51 ( Portals ) => {
52 return ( props ) => (
53 <>
54 <Portals { ...props } />
55 {
56 createPortal(
57 <Component { ...props } />,
58 document.getElementById( `tablepress-${slug}-section` ),
59 )
60 }
61 </>
62 );
63 },
64 );
65 };
66