-
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.
Add support for block variation styles
- Loading branch information
1 parent
bae0d6b
commit 42f6ad6
Showing
9 changed files
with
195 additions
and
11 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
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
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 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 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
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