Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try dynamically moving panel to styles when there are no settings. #76

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ function addInspectorControls( BlockEdit ) {
addFilter(
'editor.BlockEdit',
'block-visibility/add-inspector-controls',
addInspectorControls,
100 // We want Visibility to appear right above Advanced controls
addInspectorControls
);

/**
Expand Down
24 changes: 21 additions & 3 deletions src/editor/inspector-controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { assign, isEmpty } from 'lodash';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { withFilters, Spinner } from '@wordpress/components';
import {
withFilters,
Spinner,
__experimentalUseSlotFills as useSlotFills,
} from '@wordpress/components';
import { InspectorControls } from '@wordpress/block-editor';
import { withSelect } from '@wordpress/data';

Expand Down Expand Up @@ -42,10 +46,24 @@ function VisibilityInspectorControls( props ) {
widgetAreaRestricted,
} = props;

// Determine how many other panels have been slotted into the Settings tab.
// see: https://github.com/WordPress/gutenberg/blob/d27c43fda419995a80b1cbdeafe2b24d9a0e2164/packages/block-editor/src/components/inspector-controls-tabs/use-inspector-controls-tabs.js#L52
const settingFills = [
...( useSlotFills( 'InspectorControls' ) || [] ),
...( useSlotFills( 'InspectorControlsPosition' ) || [] ),
];

console.log( settingFills );

// This is not consistent. When the page loads, the Visibility panel is not counted.
// But after clicking around to various blocks, the Editor starts counting the
// Visibility panel as a setting Slot.
const tab = settingFills.length > 1 ? 'settings' : 'styles';

// Display a default panel with spinner when settings and variables are loading.
if ( settings === 'fetching' || variables === 'fetching' ) {
return (
<InspectorControls group="settings">
<InspectorControls group={ tab }>
<div className="block-visibility__controls-panel">
<div className="controls-panel-header">
<h2>{ __( 'Visibility', 'block-visibility' ) }</h2>
Expand Down Expand Up @@ -137,7 +155,7 @@ function VisibilityInspectorControls( props ) {
// of SlotFill, and is "inside" of a <SlotFillProvider> component.
// Therefore we can freely use SlofFill without needing to add the
// provider ourselves.
<InspectorControls group="settings">
<InspectorControls group={ tab }>
<div className="block-visibility__controls-panel">
<ControlsPanel
blockAtts={ blockAtts }
Expand Down