Replies: 2 comments
-
I’m doubtful there is. It seems like the cleanest thing would be to enqueue your script conditionally with help from the if ('editWidgets' in window.wp) {/*…*/} |
Beta Was this translation helpful? Give feedback.
-
|
Expanding on @stokesman's direction — there are two reliable techniques and one new-ish selector, depending on how you want to gate the UI. 1. Check which editor's data store existsEach editor registers its own store, and stores are registered only when that editor is actually running. So the truthy / falsy existence of these stores is the cleanest runtime signal: import { select } from '@wordpress/data'
function getEditorContext() {
if (select('core/edit-widgets')) return 'widgets' // wp-admin/widgets.php (block widgets screen)
if (select('core/customize-widgets')) return 'customizer' // Customizer → Widgets panel
if (select('core/edit-site')) return 'site-editor' // Full Site Editor / hybrid theme
if (select('core/edit-post')) return 'post-editor' // Gutenberg post/page edit
return 'unknown'
}Then gate your const ctx = getEditorContext()
return (
ctx === 'widgets' || ctx === 'customizer'
? <InspectorControls>…your panel…</InspectorControls>
: null
)This works in 2. Enqueue the script conditionally on the serverThe most efficient version of this is @stokesman's suggestion: don't ship the JS at all to contexts where it's not needed. The add_action( 'admin_enqueue_scripts', function ( $hook ) {
$screen = get_current_screen();
if ( ! $screen ) return;
// Widgets block editor screen → widgets.php
// Customizer runs on customize.php with screen->id === 'customize'
$in_widgets_context = in_array( $screen->id, [ 'widgets', 'customize' ], true );
if ( $in_widgets_context ) {
wp_enqueue_script(
'my-widgets-panel',
plugins_url( 'build/widgets-panel.js', __FILE__ ),
[ 'wp-blocks', 'wp-data', 'wp-hooks', 'wp-i18n', 'wp-block-editor' ],
filemtime( plugin_dir_path( __FILE__ ) . 'build/widgets-panel.js' ),
true
);
}
} );This is what Core itself does for editor-specific assets — skip shipping the JS and the 3.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am currently developing a block extension that needs to display an additional sidebar panel via
InspectorControls. However, this panel is specifically designed for the widgets editor (wp-admin/widgets.phpor the Customizer) and should not appear when the user is in the standard post/site editor.Is there a selector in
@wordpress/datathat returns the current admin screen context?Beta Was this translation helpful? Give feedback.
All reactions