index.js
86 lines
| 1 | import ApplyFilters from '../apply-filters/'; |
| 2 | |
| 3 | import { PanelBody } from '@wordpress/components'; |
| 4 | import { applyFilters } from '@wordpress/hooks'; |
| 5 | import useLocalStorageState from 'use-local-storage-state'; |
| 6 | |
| 7 | export default function PanelArea( props ) { |
| 8 | const { |
| 9 | title = false, |
| 10 | initialOpen = false, |
| 11 | icon, |
| 12 | className, |
| 13 | id, |
| 14 | state, |
| 15 | showPanel = true, |
| 16 | children, |
| 17 | } = props; |
| 18 | |
| 19 | const [ panels, setPanels ] = useLocalStorageState( |
| 20 | 'generateblocksPanels', { |
| 21 | ssr: true, |
| 22 | defaultValue: {}, |
| 23 | } |
| 24 | ); |
| 25 | |
| 26 | const show = applyFilters( 'generateblocks.editor.showPanel', showPanel, id, props ); |
| 27 | |
| 28 | if ( ! show ) { |
| 29 | return null; |
| 30 | } |
| 31 | |
| 32 | let hasChildren = true; |
| 33 | |
| 34 | if ( '' === children ) { |
| 35 | hasChildren = false; |
| 36 | } |
| 37 | |
| 38 | // If we have items in the panel, make sure they're not empty. |
| 39 | if ( 'object' === typeof children ) { |
| 40 | hasChildren = Object.values( children ).some( ( x ) => ( x !== null && x !== false && x !== '' ) ); |
| 41 | } |
| 42 | |
| 43 | if ( ! hasChildren ) { |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | return ( |
| 48 | <ApplyFilters name={ 'generateblocks.panel.' + id } props={ props } state={ state }> |
| 49 | { title ? ( |
| 50 | <PanelBody |
| 51 | title={ title } |
| 52 | initialOpen={ |
| 53 | 'undefined' !== typeof panels[ id ] |
| 54 | ? panels[ id ] |
| 55 | : initialOpen |
| 56 | } |
| 57 | icon={ icon } |
| 58 | className={ className } |
| 59 | onToggle={ () => { |
| 60 | const isOpen = panels[ id ] || |
| 61 | ( |
| 62 | 'undefined' === typeof panels[ id ] && |
| 63 | initialOpen |
| 64 | ); |
| 65 | |
| 66 | setPanels( { |
| 67 | ...panels, |
| 68 | [ id ]: ! isOpen, |
| 69 | } ); |
| 70 | } } |
| 71 | > |
| 72 | { |
| 73 | applyFilters( 'generateblocks.editor.panelContents', children, id, props ) |
| 74 | } |
| 75 | </PanelBody> |
| 76 | ) : ( |
| 77 | <PanelBody> |
| 78 | { |
| 79 | applyFilters( 'generateblocks.editor.panelContents', children, id, props ) |
| 80 | } |
| 81 | </PanelBody> |
| 82 | ) } |
| 83 | </ApplyFilters> |
| 84 | ); |
| 85 | } |
| 86 |