Skip to content

Instantly share code, notes, and snippets.

@seothemes
Last active February 5, 2023 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seothemes/dc7a0dceaff8a5ad694682b3f2fe72d1 to your computer and use it in GitHub Desktop.
Save seothemes/dc7a0dceaff8a5ad694682b3f2fe72d1 to your computer and use it in GitHub Desktop.
Conditionally show/hide block styles depending on the currently selected variation
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