-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sticky Position: Add a "Make sticky" action to the Template Part block (
#49085) * Sticky Position: Try adding a make sticky action to the Template Part block * Hide button if the block or theme do not support sticky position * Set to 0px for consistency
- Loading branch information
1 parent
0ccb2d9
commit cae48d7
Showing
3 changed files
with
116 additions
and
4 deletions.
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
cae48d7
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.
Flaky tests detected in cae48d7.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4442406600
📝 Reported issues:
/test/e2e/specs/site-editor/template-part.spec.js