-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Patterns: Apply layout and alignment to synced patterns in the editor #54416
Merged
aaronrobertshaw
merged 5 commits into
trunk
from
fix/synced-pattern-alignment-and-layout
Sep 14, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f4a52aa
Patterns: Fix editor layout and alignment for synced patterns
aaronrobertshaw 2ed5d52
Fix pattern root padding issue
aaronrobertshaw 93c0c0f
Update expected styles in theme.json tests
aaronrobertshaw b8bb619
Remove useState in favour of ref
aaronrobertshaw 13046ae
Improve ref naming
aaronrobertshaw 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
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
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
|
@@ -21,9 +26,52 @@ import { | |
InspectorControls, | ||
useBlockProps, | ||
Warning, | ||
privateApis as blockEditorPrivateApis, | ||
} from '@wordpress/block-editor'; | ||
import { useRef, useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../lock-unlock'; | ||
|
||
const fullAlignments = [ 'full', 'wide', 'left', 'right' ]; | ||
|
||
const useInferredLayout = ( blocks, parentLayout ) => { | ||
const initialInferredAlignmentRef = useRef(); | ||
|
||
export default function ReusableBlockEdit( { attributes: { ref } } ) { | ||
return useMemo( () => { | ||
// Exit early if the pattern's blocks haven't loaded yet. | ||
if ( ! blocks?.length ) { | ||
return {}; | ||
} | ||
|
||
let alignment = initialInferredAlignmentRef.current; | ||
|
||
// Only track the initial alignment so that temporarily removed | ||
// alignments can be reapplied. | ||
if ( alignment === undefined ) { | ||
const isConstrained = parentLayout?.type === 'constrained'; | ||
const hasFullAlignment = blocks.some( ( block ) => | ||
fullAlignments.includes( block.attributes.align ) | ||
); | ||
|
||
alignment = isConstrained && hasFullAlignment ? 'full' : null; | ||
initialInferredAlignmentRef.current = alignment; | ||
} | ||
|
||
const layout = alignment ? parentLayout : undefined; | ||
|
||
return { alignment, layout }; | ||
}, [ blocks, parentLayout ] ); | ||
}; | ||
|
||
export default function ReusableBlockEdit( { | ||
name, | ||
attributes: { ref }, | ||
__unstableParentLayout: parentLayout, | ||
} ) { | ||
const { useLayoutClasses } = unlock( blockEditorPrivateApis ); | ||
const hasAlreadyRendered = useHasRecursion( ref ); | ||
const { record, hasResolved } = useEntityRecord( | ||
'postType', | ||
|
@@ -45,12 +93,20 @@ export default function ReusableBlockEdit( { attributes: { ref } } ) { | |
ref | ||
); | ||
|
||
const { alignment, layout } = useInferredLayout( blocks, parentLayout ); | ||
const layoutClasses = useLayoutClasses( { layout }, name ); | ||
|
||
const blockProps = useBlockProps( { | ||
className: 'block-library-block__reusable-block-container', | ||
className: classnames( | ||
'block-library-block__reusable-block-container', | ||
layout && layoutClasses, | ||
{ [ `align${ alignment }` ]: alignment } | ||
), | ||
} ); | ||
|
||
const innerBlocksProps = useInnerBlocksProps( blockProps, { | ||
value: blocks, | ||
layout, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Passing the inferred layout here allows the pattern's inner blocks to still apply their alignment and layout classes appropriately. |
||
onInput, | ||
onChange, | ||
renderAppender: blocks?.length | ||
|
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.
Generating the layout styles directly allows us to avoid;