-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
packages/edit-site/src/components/template-part-converter/convert-to-sticky-group.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 || | ||
! isStickySupported || | ||
hasParents || | ||
isPositionDisabled | ||
) { | ||
return null; | ||
} | ||
|
||
// Allow converting a single template part block to a group. | ||
return ( | ||
<MenuItem | ||
onClick={ () => { | ||
onConvertToGroup(); | ||
onClose(); | ||
} } | ||
> | ||
{ __( 'Make sticky' ) } | ||
</MenuItem> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!