Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sticky Position: Add a "Make sticky" action to the Template Part block #49085

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/block-editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { lock } from './lock-unlock';
import OffCanvasEditor from './components/off-canvas-editor';
import LeafMoreMenu from './components/off-canvas-editor/leaf-more-menu';
import { ComposedPrivateInserter as PrivateInserter } from './components/inserter';
import { default as useConvertToGroupButtonProps } from './components/convert-to-group-buttons/use-convert-to-group-button-props';
import {
hasStickyPositionSupport,
useIsPositionDisabled,
} from './hooks/position';

/**
* Private @wordpress/block-editor APIs.
Expand All @@ -18,4 +23,7 @@ lock( privateApis, {
LeafMoreMenu,
OffCanvasEditor,
PrivateInserter,
useConvertToGroupButtonProps,
hasStickyPositionSupport,
useIsPositionDisabled,
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* WordPress dependencies
*/
import { switchToBlockType } from '@wordpress/blocks';
import { MenuItem } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import {
store as blockEditorStore,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { unlock } from '../../private-apis';

const {
hasStickyPositionSupport,
useConvertToGroupButtonProps,
useIsPositionDisabled,
} = unlock( blockEditorPrivateApis );

export default function ConvertToStickyGroup( { selectedClientIds, onClose } ) {
const { replaceBlocks } = useDispatch( blockEditorStore );
const { clientIds, isGroupable, blocksSelection, groupingBlockName } =
useConvertToGroupButtonProps( selectedClientIds );

const { canRemove, hasParents } = useSelect(
( select ) => {
const { getBlockParents, canRemoveBlocks } =
select( blockEditorStore );
return {
canRemove: canRemoveBlocks( clientIds ),
hasParents: !! getBlockParents( clientIds[ 0 ] ).length,
};
},
[ clientIds ]
);

const isPositionDisabled = useIsPositionDisabled( {
name: groupingBlockName,
} );
const isStickySupported = hasStickyPositionSupport( groupingBlockName );

const onConvertToGroup = () => {
const newBlocks = switchToBlockType(
blocksSelection,
groupingBlockName
);

if ( newBlocks && newBlocks.length > 0 ) {
// Because the block is not in the store yet we can't use
// updateBlockAttributes so need to manually update attributes.
newBlocks[ 0 ].attributes.layout = {
type: 'default',
};
newBlocks[ 0 ].attributes.style = {
...( newBlocks[ 0 ].attributes.style || {} ),
position: {
type: 'sticky',
top: '0px',
},
};
replaceBlocks( clientIds, newBlocks );
}
};

// For the button to be visible, the following conditions must be met:
// - The block is groupable.
// - The block can be removed.
// - A grouping block is available.
// - The block and theme both support sticky position.
// - The block has no parents, so is at the root of the template.
if (
! isGroupable ||
! canRemove ||
! groupingBlockName ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Til about the concept of a "grouping block"! Does this mean blocks could be grouped with a different container than a Group block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically yes, but in practice the groupingBlockName is set up as the Group block when the core blocks are registered here. My understanding is that it's good when dealing with grouping (verb) behaviour to use this abstraction so that (in theory) custom editors that don't use core blocks can still work properly. In practice, I'm not sure how frequently that use case is realised, but for this PR, I thought I'd try to be consistent with the existing grouping logic!

! isStickySupported ||
hasParents ||
isPositionDisabled
) {
return null;
}

// Allow converting a single template part block to a group.
return (
<MenuItem
onClick={ () => {
onConvertToGroup();
onClose();
} }
>
{ __( 'Make sticky' ) }
</MenuItem>
);
}
15 changes: 11 additions & 4 deletions packages/edit-site/src/components/template-part-converter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
*/
import ConvertToRegularBlocks from './convert-to-regular';
import ConvertToTemplatePart from './convert-to-template-part';
import { default as ConvertToStickyGroup } from './convert-to-sticky-group';

export default function TemplatePartConverter() {
return (
Expand All @@ -36,10 +37,16 @@ function TemplatePartConverterMenuItem( { clientIds, onClose } ) {
// Allow converting a single template part to standard blocks.
if ( blocks.length === 1 && blocks[ 0 ]?.name === 'core/template-part' ) {
return (
<ConvertToRegularBlocks
clientId={ clientIds[ 0 ] }
onClose={ onClose }
/>
<>
<ConvertToRegularBlocks
clientId={ clientIds[ 0 ] }
onClose={ onClose }
/>
<ConvertToStickyGroup
selectedClientIds={ clientIds }
onClose={ onClose }
/>
</>
);
}
return <ConvertToTemplatePart clientIds={ clientIds } blocks={ blocks } />;
Expand Down