AdvancedCustomFields / acf

Advanced Custom Fields
http://advancedcustomfields.com/
872 stars 182 forks source link

Force open 'Advanced' components-panel #814

Open IvanPr opened 1 year ago

IvanPr commented 1 year ago

Is there a way to force open the 'Advanced' components-panel section of the ACF Block. Perhaps there could be a way to set initialOpen via block.json?

CreativeDive commented 1 year ago

To do something like this you need javascript. Something like:

function simulateMouseEvent(element, eventType) {
    const event = new MouseEvent(eventType, {
        bubbles: true,
    });
    element.dispatchEvent(event);
}

var initializeBlock = function( $block ) {    
    var panel = document.querySelector('.block-editor-block-inspector__advanced:not(.is-opened) button');    

    if( panel ) {            
        simulateMouseEvent(panel, 'click');            
    }
}

if( window.acf ) {
    window.acf.addAction( 'render_block_preview', initializeBlock );
}