Skip to content

Commit

Permalink
Update to the patterns package
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Aug 15, 2023
1 parent b359cfe commit 1bb5555
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 54 deletions.
31 changes: 16 additions & 15 deletions packages/patterns/src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { BlockSettingsMenuControls } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand All @@ -11,20 +10,22 @@ import PatternConvertButton from './pattern-convert-button';
import PatternsManageButton from './patterns-manage-button';

export default function PatternsMenuItems( { rootClientId } ) {
const clientIds = useSelect(
( select ) => select( blockEditorStore ).getSelectedBlockClientIds(),
[]
);

return (
<>
<PatternConvertButton
clientIds={ clientIds }
rootClientId={ rootClientId }
/>
{ clientIds.length === 1 && (
<PatternsManageButton clientId={ clientIds[ 0 ] } />
<BlockSettingsMenuControls>
{ ( { onClose, selectedClientIds } ) => (
<>
<PatternConvertButton
clientIds={ selectedClientIds }
rootClientId={ rootClientId }
onClose={ onClose }
/>
{ selectedClientIds.length === 1 && (
<PatternsManageButton
clientId={ selectedClientIds[ 0 ] }
/>
) }
</>
) }
</>
</BlockSettingsMenuControls>
);
}
61 changes: 28 additions & 33 deletions packages/patterns/src/components/pattern-convert-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
* WordPress dependencies
*/
import { hasBlockSupport, isReusableBlock } from '@wordpress/blocks';
import {
BlockSettingsMenuControls,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useState } from '@wordpress/element';
import { MenuItem } from '@wordpress/components';
import { symbol } from '@wordpress/icons';
Expand All @@ -24,9 +21,14 @@ import CreatePatternModal from './create-pattern-modal';
* @param {Object} props Component props.
* @param {string[]} props.clientIds Client ids of selected blocks.
* @param {string} props.rootClientId ID of the currently selected top-level block.
* @param {()=>void} props.onClose Callback to close the menu.
* @return {import('@wordpress/element').WPComponent} The menu control or null.
*/
export default function PatternConvertButton( { clientIds, rootClientId } ) {
export default function PatternConvertButton( {
clientIds,
rootClientId,
onClose,
} ) {
const { createSuccessNotice } = useDispatch( noticesStore );
const [ isModalOpen, setIsModalOpen ] = useState( false );
const canConvert = useSelect(
Expand Down Expand Up @@ -103,34 +105,27 @@ export default function PatternConvertButton( { clientIds, rootClientId } ) {
setIsModalOpen( false );
};
return (
<BlockSettingsMenuControls>
{ ( { onClose } ) => (
<>
<MenuItem
icon={ symbol }
onClick={ () => setIsModalOpen( true ) }
>
{ __( 'Create pattern' ) }
</MenuItem>
{ isModalOpen && (
<CreatePatternModal
clientIds={ clientIds }
onSuccess={ ( pattern ) => {
handleSuccess( pattern );
onClose();
} }
onError={ () => {
setIsModalOpen( false );
onClose();
} }
onClose={ () => {
setIsModalOpen( false );
onClose();
} }
/>
) }
</>
<>
<MenuItem icon={ symbol } onClick={ () => setIsModalOpen( true ) }>
{ __( 'Create pattern' ) }
</MenuItem>
{ isModalOpen && (
<CreatePatternModal
clientIds={ clientIds }
onSuccess={ ( pattern ) => {
handleSuccess( pattern );
onClose();
} }
onError={ () => {
setIsModalOpen( false );
onClose();
} }
onClose={ () => {
setIsModalOpen( false );
onClose();
} }
/>
) }
</BlockSettingsMenuControls>
</>
);
}
9 changes: 3 additions & 6 deletions packages/patterns/src/components/patterns-manage-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { isReusableBlock } from '@wordpress/blocks';
import { useSelect, useDispatch } from '@wordpress/data';
import {
BlockSettingsMenuControls,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { addQueryArgs } from '@wordpress/url';
import { store as coreStore } from '@wordpress/core-data';

Expand Down Expand Up @@ -62,7 +59,7 @@ function PatternsManageButton( { clientId } ) {
}

return (
<BlockSettingsMenuControls>
<>
<MenuItem href={ managePatternsUrl }>
{ __( 'Manage patterns' ) }
</MenuItem>
Expand All @@ -73,7 +70,7 @@ function PatternsManageButton( { clientId } ) {
: __( 'Detach pattern' ) }
</MenuItem>
) }
</BlockSettingsMenuControls>
</>
);
}

Expand Down

0 comments on commit 1bb5555

Please sign in to comment.