Skip to content

Commit

Permalink
feat: auto-insert connected button
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Aug 6, 2024
1 parent f510454 commit 7aefab8
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/pro/blocks/modal/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '@wordpress/element';

import {
createBlock,
createBlocksFromInnerBlocksTemplate
} from '@wordpress/blocks';

Expand All @@ -38,7 +39,7 @@ import {
*/
import metadata from './block.json';
import Inspector from './inspector.js';
import { blockInit, useCSSNode } from '../../../blocks/helpers/block-utility';
import { blockInit, insertBlockBelow, useCSSNode } from '../../../blocks/helpers/block-utility';
import { boxValues, _cssBlock, stringToBox } from '../../../blocks/helpers/helper-functions';
import { useDarkBackground } from '../../../blocks/helpers/utility-hooks';
import { useDispatch, useSelect } from '@wordpress/data';
Expand All @@ -62,13 +63,13 @@ const Edit = ({
return () => unsubscribe( attributes.id );
}, []);

const [ isEditing, setEditing ] = useState( false );

const {
replaceInnerBlocks,
selectBlock
selectBlock,
insertBlock
} = useDispatch( 'core/block-editor' );

const [ isEditing, setEditing ] = useState( false );
const { blockType, defaultVariation, variations } = useSelect(
select => {
const {
Expand Down Expand Up @@ -164,6 +165,30 @@ const Edit = ({
className: classnames( className, 'is-active', cssNode, { 'with-outside-button': 'outside' === attributes.closeButtonType })
});

useEffect( () => {
if ( attributes.id || attributes.anchor ) {
return;
}

// Auto-insert a connected Button Block at creation.
const modalAnchor = `modal-${ clientId.slice( 0, 8 ) }`;
const button = createBlock(
'core/buttons',
{},
[
createBlock(
'core/button',
{
text: __( 'Open Modal', 'otter-blocks' ),
anchor: modalAnchor
}
)
]
);
insertBlockBelow( clientId, button );
setAttributes({ anchor: modalAnchor });
}, [ attributes.id, attributes.anchor ]);

return (
<Fragment>
<Inspector
Expand Down Expand Up @@ -201,7 +226,11 @@ const Edit = ({
) }

<div className="otter-popup__modal_body">
<InnerBlocks />
<InnerBlocks template={[
[ 'core/image', { height: '150px' }],
[ 'core/heading', { placeholder: __( 'Modal Title' ) }],
[ 'core/paragraph', { placeholder: __( 'Modal Content' ) }]
]} />
</div>
</div>
</div>
Expand Down

0 comments on commit 7aefab8

Please sign in to comment.