Last active
February 5, 2023 05:53
-
-
Save seothemes/dc7a0dceaff8a5ad694682b3f2fe72d1 to your computer and use it in GitHub Desktop.
Conditionally show/hide block styles depending on the currently selected variation
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
import { addFilter } from "@wordpress/hooks"; | |
import { createHigherOrderComponent } from "@wordpress/compose"; | |
interface blockProps { | |
attributes: { | |
className: string; | |
}; | |
isSelected: boolean; | |
name: string; | |
} | |
addFilter( | |
'editor.BlockEdit', | |
'blockify/conditional-block-styles', | |
createHigherOrderComponent( BlockEdit => { | |
return ( props: blockProps ) => { | |
const { attributes, isSelected, name } = props; | |
const blockEdit = <BlockEdit { ...props } />; | |
const className = attributes.className ?? ''; | |
if ( ! isSelected || name !== 'core/image' ) { | |
return blockEdit; | |
} | |
// Get the button dom element. | |
const iconOnlyStyleButton = document.querySelector( '[aria-label="Icon Only"]' ) as HTMLButtonElement; | |
if ( ! iconOnlyStyleButton ) { | |
return blockEdit; | |
} | |
// Check for the block variation class name. | |
if ( className.includes( 'is-style-icon' ) ) { | |
iconOnlyStyleButton.style.display = 'inline-block'; | |
} else { | |
iconOnlyStyleButton.style.display = 'none'; | |
} | |
return blockEdit; | |
} | |
}, 'withConditionalBlockStyle' ), | |
9 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment