-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add support for block variation styles #49728
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
Closed
Closed
Changes from all commits
Commits
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
Add support for block variation styles
- Loading branch information
commit 7dfd9b2ecdd03b45cfcc179be9542e4fdcc1b87e
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
72 changes: 72 additions & 0 deletions
72
packages/block-editor/src/components/block-list/use-block-props/use-clean-block-styles.js
This file contains hidden or 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,72 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useEffect } from '@wordpress/element'; | ||
| import { store as blocksStore } from '@wordpress/blocks'; | ||
| import { useSelect, useDispatch } from '@wordpress/data'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { store as blockEditorStore } from '../../../store'; | ||
|
|
||
| /** | ||
| * This hook checks if any applied block variation style is set, | ||
| * but the respective block variation is not active now. | ||
| * | ||
| * In this case this hook performs a side effect to remove | ||
| * that style(class name) and doesn't create an `undo` step. | ||
| * | ||
| * @param {string} clientId The block client ID. | ||
| * | ||
| */ | ||
| export default function useCleanBlockStyles( clientId ) { | ||
| const { updateBlockAttributes } = useDispatch( blockEditorStore ); | ||
| const { __unstableMarkNextChangeAsNotPersistent } = | ||
| useDispatch( blockEditorStore ); | ||
| const { styleToRemove, blockClassNames } = useSelect( | ||
| ( select ) => { | ||
| const { getBlockAttributes, getBlockName } = | ||
| select( blockEditorStore ); | ||
| const { getActiveBlockVariation, getBlockVariationStyles } = | ||
| select( blocksStore ); | ||
| const attributes = getBlockAttributes( clientId ); | ||
| if ( ! attributes.className ) { | ||
| return {}; | ||
| } | ||
| // Here we are checking if the block has a block style for a specific | ||
| // block variation. If it does and the block variation is no longer active, | ||
| // we need to remove the style class name. | ||
| const blockName = getBlockName( clientId ); | ||
| const match = getActiveBlockVariation( blockName, attributes ); | ||
| const blockVariationStyles = getBlockVariationStyles( blockName ); | ||
| return { | ||
| styleToRemove: blockVariationStyles?.find( | ||
| ( { name: styleName, variations } ) => | ||
| attributes.className.includes( | ||
| `is-style-${ styleName }` | ||
| ) && | ||
| ( ! match || ! variations.includes( match.name ) ) | ||
| )?.name, | ||
| blockClassNames: attributes.className, | ||
| }; | ||
| }, | ||
| [ clientId ] | ||
| ); | ||
| useEffect( () => { | ||
| if ( styleToRemove === undefined ) { | ||
| return; | ||
| } | ||
| const updatedClassNames = blockClassNames | ||
| .split( ' ' ) | ||
| .filter( | ||
| ( blockClassName ) => | ||
| ! blockClassName.includes( `is-style-${ styleToRemove }` ) | ||
| ) | ||
| .join( ' ' ); | ||
| __unstableMarkNextChangeAsNotPersistent(); | ||
| updateBlockAttributes( clientId, { | ||
| className: updatedClassNames, | ||
| } ); | ||
| }, [ clientId, styleToRemove, blockClassNames ] ); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
This isn't working for me when testing this branch - I'm still getting a warning in VSCode that this property isn't allowed when adding it to
block.json🤔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.
Sometimes I had that warning too in VSCode when I first check out the branch, but it goes away if I reopen that file. Probably some VSCode cache? 🤔 . Even with the warning it still works though for me? What do you mean not working?