| 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 domReady from '@wordpress/dom-ready'; |
| 16 |
import { addFilter } from '@wordpress/hooks'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Initializes a React component on the page. |
| 20 |
* |
| 21 |
* @param {string} rootId HTML ID of the root element for the component. |
| 22 |
* @param {Component} Component JSX component. |
| 23 |
*/ |
| 24 |
export const initializeReactComponent = ( rootId, Component ) => { |
| 25 |
domReady( () => { |
| 26 |
if ( process.env.DEVELOP ) { |
| 27 |
Component = <StrictMode>{ Component }</StrictMode>; |
| 28 |
} |
| 29 |
|
| 30 |
const root = document.getElementById( rootId ); |
| 31 |
if ( root ) { |
| 32 |
createRoot( root ).render( Component ); |
| 33 |
} |
| 34 |
} ); |
| 35 |
}; |
| 36 |
|
| 37 |
/** |
| 38 |
* Initializes a React component on the page, in a React Portal, and registers its meta box. |
| 39 |
* |
| 40 |
* @param {string} slug Slug of the component/feature module. |
| 41 |
* @param {string} screen Slug/action of the screen. |
| 42 |
* @param {Component} Component JSX component. |
| 43 |
*/ |
| 44 |
export const initializeReactComponentInPortal = ( slug, screen, Component ) => { |
| 45 |
addFilter( |
| 46 |
`tablepress.${screen}ScreenFeatures`, |
| 47 |
`tp/${slug}/${screen}-screen-feature`, |
| 48 |
( features ) => ( [ ...features, slug ] ), |
| 49 |
); |
| 50 |
|
| 51 |
addFilter( |
| 52 |
`tablepress.${screen}ScreenPortals`, |
| 53 |
`tp/${slug}/${screen}-screen-portal`, |
| 54 |
( Portals ) => { |
| 55 |
return ( props ) => ( |
| 56 |
<> |
| 57 |
<Portals { ...props } /> |
| 58 |
{ |
| 59 |
createPortal( |
| 60 |
<Component { ...props } />, |
| 61 |
document.getElementById( `tablepress-${slug}-section` ), |
| 62 |
) |
| 63 |
} |
| 64 |
</> |
| 65 |
); |
| 66 |
}, |
| 67 |
); |
| 68 |
}; |
| 69 |
|