Search code examples
wordpresswordpress-gutenberg

Detect opening of PluginSidebar in Wordpress Gutenberg


I'm looking to reinitialise some settings and state data when my PluginSidebar is reopened, but I'm struggling to find anything useful in wp.data core/editor or similar that I could use to best create a subscription.

Does gutenberg provide any such data where I can check to see if the side panel is open or shut, so that I could fire a function of my choice every time it opens?

At present, I have a mutationObserver in place listening to see if it opens, which is quite clunky.

Some pseudo-code of my preferred approach.

subscribe(() => {
    if (select('core/editor').isPluginSidebarOpen()) {
        open = true
    } else {
        open = false
    }
})

Solution

  • Your pseudo-code is so very close.. the function exists and is called isPluginSidebarOpened and comes from core/edit-post, eg:

    import { subscribe, select } from '@wordpress/data';
    
    subscribe(() => {
        if (select('core/edit-post').isPluginSidebarOpened()) {
            // Is open..
        } else {
            // Is closed..
        }
    });