Is it possible to disable a block style?

Created on 20 May 2024, 6 months ago
Updated 7 June 2024, 6 months ago

Problem/Motivation

I want to disable the rounded style preset on the image block, is this possible in the THEME.gutenberg.yml file?
I haven't be able to figure it out.
Thanks

πŸ’¬ Support request
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @loze
  • πŸ‡¨πŸ‡¦Canada drclaw

    I think it needs to be done in javascript with wp.blocks.unregisterBlockStyle(). Although, as mentioned on that documentation page, there can be a bit of a race condition if your script tries to unregister a style before it's registered. Even using wp.domReady() I still needed a timeout to ensure the unregistration happened after the style was registered. Probably, we need to use a different event for Drupal Gutenberg but I don't know what that is.

    In the meantime, if you can handle the timeout, here's wha that looks like:

    wp.domReady( () => {
      setTimeout(() => {
        // Remove the rounded style from the core/image block.
        wp.blocks.unregisterBlockStyle( 'core/image', 'rounded' );
      }, 1000);
    } );
    
  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

    Thanks.

    I was able to do this following the register-filters.jsx example in the gutenberg_starter theme, which seems to do the trick.

    ((Drupal, wp) => {
      const {t} = Drupal;
      const {hooks, blockEditor} = wp;
      const {addFilter} = hooks;
      const {BlockControls} = blockEditor;
    
    
      /**
       * This filter will alter image block by removing its styles.
       */
      addFilter(
        'blocks.registerBlockType',
        'MYTHEME/remove-image-block-styles',
        (settings, name) => {
    
          if (name === "core/image") {
            return {
              ...settings,
              styles: []
            };
          }
          else {
            return settings;
          }
        },
      );
    })(Drupal, wp);
    
  • πŸ‡¨πŸ‡¦Canada drclaw

    Ah beauty! That's good to know - thanks for reporting back πŸ™Œ

  • Status changed to Fixed 6 months ago
  • πŸ‡΅πŸ‡ΉPortugal marcofernandes

    I'm glad the starter theme is helping, that's what it was made for πŸ˜…

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024