| 1 |
/** |
| 2 |
* JavaScript code for the HelpBox and Help components. |
| 3 |
* |
| 4 |
* @package TablePress |
| 5 |
* @subpackage Edit Screen |
| 6 |
* @author Tobias Bäthge |
| 7 |
* @since 3.1.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* WordPress dependencies. |
| 12 |
*/ |
| 13 |
import { useRef, useState } from 'react'; |
| 14 |
import { createPortal } from 'react-dom'; |
| 15 |
import { |
| 16 |
Button, |
| 17 |
Icon, |
| 18 |
Modal, |
| 19 |
} from '@wordpress/components'; |
| 20 |
import { help } from '@wordpress/icons'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Returns the HelpBox component's JSX markup. |
| 24 |
* |
| 25 |
* @param {Object} props Function parameters. |
| 26 |
* @param {string} props.title The title of the HelpBox. |
| 27 |
* @param {Object} props.buttonProps Additional props for the Button. |
| 28 |
* @param {Object} props.modalProps Additional props for the Modal. |
| 29 |
* @param {Object} props.children The Help content. |
| 30 |
* @return {Object} HelpBox component. |
| 31 |
*/ |
| 32 |
export const HelpBox = ( { title, buttonProps= {}, modalProps = {}, children } ) => { |
| 33 |
const [ modalOpen, setModalOpen ] = useState( false ); |
| 34 |
const openModal = () => setModalOpen( true ); |
| 35 |
const closeModal = () => setModalOpen( false ); |
| 36 |
|
| 37 |
return ( |
| 38 |
<> |
| 39 |
<Button |
| 40 |
variant="secondary" |
| 41 |
icon={ help } |
| 42 |
size="small" |
| 43 |
onClick={ openModal } |
| 44 |
{ ...buttonProps } |
| 45 |
/> |
| 46 |
{ modalOpen && ( |
| 47 |
<Modal |
| 48 |
size="small" |
| 49 |
icon={ <Icon icon={ help } style={ { display: 'flex', marginRight: '4px' } }/> } |
| 50 |
title={ title } |
| 51 |
onRequestClose={ closeModal } |
| 52 |
{ ...modalProps } |
| 53 |
> |
| 54 |
{ children } |
| 55 |
</Modal> |
| 56 |
) } |
| 57 |
</> |
| 58 |
); |
| 59 |
}; |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns the Help component's JSX markup. |
| 63 |
* |
| 64 |
* @param {Object} props Function parameters. |
| 65 |
* @param {string} props.section The section on the "Edit" screen. |
| 66 |
* @param {string} props.title The title of the module. |
| 67 |
* @param {Object} props.buttonProps Additional props for the Button. |
| 68 |
* @param {Object} props.modalProps Additional props for the Modal. |
| 69 |
* @param {Object} props.children The Help content. |
| 70 |
* @return {Object} Help component. |
| 71 |
*/ |
| 72 |
export const Help = ( { section, title, buttonProps = {}, modalProps = {}, children } ) => { |
| 73 |
// Store a reference to the Help Box container, which is moved in the DOM, to hold the Portal. |
| 74 |
const helpContainer = useRef( null ); |
| 75 |
|
| 76 |
// Create a container for the Help Box and move it to the desired position in the DOM. |
| 77 |
if ( ! helpContainer.current ) { |
| 78 |
helpContainer.current = document.createElement( 'div' ); |
| 79 |
helpContainer.current.className = 'help-container'; |
| 80 |
document.getElementById( `tablepress-${section}-section` ).closest( '.postbox' ).querySelector( 'h2.hndle' ).appendChild( helpContainer.current ); |
| 81 |
} |
| 82 |
|
| 83 |
return createPortal( |
| 84 |
<HelpBox |
| 85 |
title={ title } |
| 86 |
buttonProps={ buttonProps } |
| 87 |
modalProps={ modalProps } |
| 88 |
> |
| 89 |
{ children } |
| 90 |
</HelpBox>, |
| 91 |
helpContainer.current, |
| 92 |
); |
| 93 |
}; |
| 94 |
|