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

ToolsPanel: switch to plus icon when no controls present in panel body #34107

Merged
merged 12 commits into from
Oct 14, 2021
Merged
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
1 change: 1 addition & 0 deletions packages/components/src/tools-panel/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const ToolsPanelContext = createContext< ToolsPanelContextType >( {
registerPanelItem: noop,
deregisterPanelItem: noop,
flagItemCustomization: noop,
areAllOptionalControlsHidden: true,
} );

export const useToolsPanelContext = () =>
Expand Down
54 changes: 53 additions & 1 deletion packages/components/src/tools-panel/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export const _default = () => {
return (
<PanelWrapperView>
<Panel>
<ToolsPanel label="Tools Panel" resetAll={ resetAll }>
<ToolsPanel
label="Tools Panel (default example)"
resetAll={ resetAll }
>
<ToolsPanelItem
className="single-column"
hasValue={ () => !! width }
Expand Down Expand Up @@ -66,6 +69,7 @@ export const _default = () => {
hasValue={ () => !! minHeight }
label="Minimum height"
onDeselect={ () => setMinHeight( undefined ) }
isShownByDefault={ true }
>
<UnitControl
label="Minimum height"
Expand All @@ -79,6 +83,54 @@ export const _default = () => {
);
};

export const WithOptionalItemsPlusIcon = () => {
const [ height, setHeight ] = useState();
const [ width, setWidth ] = useState();

const resetAll = () => {
setHeight( undefined );
setWidth( undefined );
};

return (
<PanelWrapperView>
<Panel>
<ToolsPanel
label="Tools Panel (optional items only)"
resetAll={ resetAll }
>
<ToolsPanelItem
className="single-column"
hasValue={ () => !! width }
label="Width"
onDeselect={ () => setWidth( undefined ) }
isShownByDefault={ false }
>
<UnitControl
label="Width"
value={ width }
onChange={ ( next ) => setWidth( next ) }
/>
</ToolsPanelItem>
<ToolsPanelItem
className="single-column"
hasValue={ () => !! height }
label="Height"
onDeselect={ () => setHeight( undefined ) }
isShownByDefault={ false }
>
<UnitControl
label="Height"
value={ height }
onChange={ ( next ) => setHeight( next ) }
/>
</ToolsPanelItem>
</ToolsPanel>
</Panel>
</PanelWrapperView>
);
};

const { Fill: ToolsPanelItems, Slot } = createSlotFill( 'ToolsPanelSlot' );
const panelId = 'unique-tools-panel-id';

Expand Down
56 changes: 53 additions & 3 deletions packages/components/src/tools-panel/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,17 @@ const renderPanel = () => {
);
};

// Helper to find the menu button and simulate a user click.
/**
* Helper to find the menu button and simulate a user click.
*
* @return {HTMLElement} The menuButton.
*/
const openDropdownMenu = () => {
const menuButton = screen.getByLabelText( defaultProps.label );
const menuButton = screen.getByRole( 'button', {
name: /view([\w\s]+)options/i,
} );
fireEvent.click( menuButton );
return menuButton;
};

// Opens dropdown then selects the menu item by label before simulating a click.
Expand Down Expand Up @@ -212,7 +219,7 @@ describe( 'ToolsPanel', () => {
it( 'should render panel menu when at least one panel item', () => {
renderPanel();

const menuButton = screen.getByLabelText( defaultProps.label );
const menuButton = openDropdownMenu();
expect( menuButton ).toBeInTheDocument();
} );

Expand Down Expand Up @@ -509,4 +516,47 @@ describe( 'ToolsPanel', () => {
expect( items[ 1 ] ).toHaveTextContent( 'Item 2' );
} );
} );

describe( 'panel header icon toggle', () => {
const optionalControls = {
attributes: { value: false },
hasValue: jest.fn().mockImplementation( () => {
return !! optionalControls.attributes.value;
} ),
label: 'Optional',
onDeselect: jest.fn(),
onSelect: jest.fn(),
isShownByDefault: false,
};

it( 'should render appropriate icons for the dropdown menu', async () => {
render(
<ToolsPanel { ...defaultProps }>
<ToolsPanelItem { ...optionalControls }>
<div>Optional control</div>
</ToolsPanelItem>
</ToolsPanel>
);

// There are unactivated, optional menu items in the Tools Panel dropdown.
const optionsHiddenIcon = screen.getByRole( 'button', {
name: 'View and add options',
} );

expect( optionsHiddenIcon ).toBeInTheDocument();

await selectMenuItem( optionalControls.label );

// There are now NO unactivated, optional menu items in the Tools Panel dropdown.
expect(
screen.queryByRole( 'button', { name: 'View and add options' } )
).not.toBeInTheDocument();

const optionsDisplayedIcon = screen.getByRole( 'button', {
name: 'View options',
} );

expect( optionsDisplayedIcon ).toBeInTheDocument();
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type { Ref } from 'react';
/**
* WordPress dependencies
*/
import { check, reset, moreVertical } from '@wordpress/icons';
import { __, sprintf } from '@wordpress/i18n';
import { check, reset, moreVertical, plus } from '@wordpress/icons';
import { __, _x, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
Expand Down Expand Up @@ -118,6 +118,7 @@ const ToolsPanelHeader = (
const {
dropdownMenuClassName,
hasMenuItems,
areAllOptionalControlsHidden,
label: labelText,
menuItems,
resetAll,
Expand All @@ -131,14 +132,21 @@ const ToolsPanelHeader = (

const defaultItems = Object.entries( menuItems?.default || {} );
const optionalItems = Object.entries( menuItems?.optional || {} );
const dropDownMenuIcon = areAllOptionalControlsHidden ? plus : moreVertical;
const dropDownMenuLabelText = areAllOptionalControlsHidden
? _x(
'View and add options',
'Button label to reveal tool panel options'
)
: _x( 'View options', 'Button label to reveal tool panel options' );

return (
<h2 { ...headerProps } ref={ forwardedRef }>
{ labelText }
{ hasMenuItems && (
<DropdownMenu
icon={ moreVertical }
label={ labelText }
icon={ dropDownMenuIcon }
label={ dropDownMenuLabelText }
menuProps={ { className: dropdownMenuClassName } }
>
{ ( { onClose = noop } ) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ export function useToolsPanelHeader(
return cx( styles.DropdownMenu );
}, [] );

const { menuItems, hasMenuItems } = useToolsPanelContext();
const {
menuItems,
hasMenuItems,
areAllOptionalControlsHidden,
} = useToolsPanelContext();

return {
...otherProps,
dropdownMenuClassName,
hasMenuItems,
areAllOptionalControlsHidden,
menuItems,
className: classes,
};
Expand Down
11 changes: 5 additions & 6 deletions packages/components/src/tools-panel/tools-panel/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,15 @@ export function useToolsPanel(
} );
};

// Track whether all optional controls are displayed or not.
// If no optional controls are present, then none are hidden and this will
// be `false`.
// Whether all optional menu items are hidden or not must be tracked
// in order to later determine if the panel display is empty and handle
// conditional display of a plus icon to indicate the presence of further
// menu items.
const [
areAllOptionalControlsHidden,
setAreAllOptionalControlsHidden,
] = useState( false );

// We need to track whether any optional menu items are active to later
// determine whether the panel is currently empty and any inner wrapper
// should be hidden.
useEffect( () => {
if ( menuItems.optional ) {
const optionalItems = Object.entries( menuItems.optional );
Expand Down Expand Up @@ -203,6 +201,7 @@ export function useToolsPanel(
registerPanelItem,
deregisterPanelItem,
flagItemCustomization,
areAllOptionalControlsHidden,
hasMenuItems: !! panelItems.length,
isResetting: isResetting.current,
shouldRenderPlaceholderItems,
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/tools-panel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export type ToolsPanelContext = {
flagItemCustomization: ( label: string ) => void;
isResetting: boolean;
shouldRenderPlaceholderItems: boolean;
areAllOptionalControlsHidden: boolean;
};

export type ToolsPanelControlsGroupProps = {
Expand Down