Skip to content

Commit

Permalink
Tidy list view props and deprecate BlockNavigationDropdown (#40777)
Browse files Browse the repository at this point in the history
* Remove __experimentalFeatures prop

* Remove __experimentalPersistentListViewFeatures

* Remove __experimentalHideContainerBlockActions in favor of checking block supports

* Remove showNestedBlocks

* Remove props pass through and tidy up docs

* Deprecate BlockNavigationDropdown

* Update changelog

* Update package-lock
  • Loading branch information
talldan committed May 4, 2022
1 parent a305eb5 commit 1b91575
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 121 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/block-editor/CHANGELOG.md
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Breaking change

- `BlockNavigationDropdown` is now deprecated. Use the `Dropdown` component from the `@wordpress/components` package and the `ListView` component from this package ([#40777](https://github.com/WordPress/gutenberg/pull/40777)).
- `ListView` no longer accepts the `__experimentalFeatures`, `__experimentalPersistentListViewFeatures`, `__experimentalHideContainerBlockActions`, and `showNestedBlocks` props. Passing additional undocumented props through to `ListView` is also now disallowed. ([#40777](https://github.com/WordPress/gutenberg/pull/40777)).

## 8.6.0 (2022-04-21)

## 8.5.0 (2022-04-08)
Expand Down
20 changes: 12 additions & 8 deletions packages/block-editor/src/components/block-navigation/dropdown.js
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -36,10 +41,12 @@ function BlockNavigationDropdownToggle( {
);
}

function BlockNavigationDropdown(
{ isDisabled, __experimentalFeatures, ...props },
ref
) {
function BlockNavigationDropdown( { isDisabled, ...props }, ref ) {
deprecated( 'wp.blockEditor.BlockNavigationDropdown', {
since: '6.1',
alternative: 'wp.components.Dropdown and wp.blockEditor.ListView',
} );

const hasBlocks = useSelect(
( select ) => !! select( blockEditorStore ).getBlockCount(),
[]
Expand All @@ -65,10 +72,7 @@ function BlockNavigationDropdown(
{ __( 'List view' ) }
</p>

<ListView
showNestedBlocks
__experimentalFeatures={ __experimentalFeatures }
/>
<ListView />
</div>
) }
/>
Expand Down
58 changes: 24 additions & 34 deletions packages/block-editor/src/components/list-view/block.js
Expand Up @@ -6,6 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { hasBlockSupport } from '@wordpress/blocks';
import {
__experimentalTreeGridCell as TreeGridCell,
__experimentalTreeGridItem as TreeGridItem,
Expand All @@ -19,7 +20,7 @@ import {
useCallback,
memo,
} from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { sprintf, __ } from '@wordpress/i18n';

/**
Expand Down Expand Up @@ -66,6 +67,19 @@ function ListViewBlock( {
const { toggleBlockHighlight } = useDispatch( blockEditorStore );

const blockInformation = useBlockDisplayInformation( clientId );
const blockName = useSelect(
( select ) => select( blockEditorStore ).getBlockName( clientId ),
[ clientId ]
);

// When a block hides its toolbar it also hides the block settings menu,
// since that menu is part of the toolbar in the editor canvas.
// List View respects this by also hiding the block settings menu.
const showBlockActions = hasBlockSupport(
blockName,
'__experimentalToolbar',
true
);
const { isLocked } = useBlockLock( clientId );
const instanceId = useInstanceId( ListViewBlock );
const descriptionId = `list-view-block-select-button__${ instanceId }`;
Expand Down Expand Up @@ -98,14 +112,7 @@ function ListViewBlock( {
)
: __( 'Options' );

const {
__experimentalFeatures: withExperimentalFeatures,
__experimentalPersistentListViewFeatures: withExperimentalPersistentListViewFeatures,
__experimentalHideContainerBlockActions: hideContainerBlockActions,
isTreeGridMounted,
expand,
collapse,
} = useListViewContext();
const { isTreeGridMounted, expand, collapse } = useListViewContext();

const hasSiblings = siblingBlockCount > 0;
const hasRenderedMovers = showBlockMovers && hasSiblings;
Expand All @@ -123,27 +130,19 @@ function ListViewBlock( {
// only focus the selected list item on mount; otherwise the list would always
// try to steal the focus from the editor canvas.
useEffect( () => {
if (
withExperimentalPersistentListViewFeatures &&
! isTreeGridMounted &&
isSelected
) {
if ( ! isTreeGridMounted && isSelected ) {
cellRef.current.focus();
}
}, [] );

const highlightBlock = withExperimentalPersistentListViewFeatures
? toggleBlockHighlight
: () => {};

const onMouseEnter = useCallback( () => {
setIsHovered( true );
highlightBlock( clientId, true );
}, [ clientId, setIsHovered, highlightBlock ] );
toggleBlockHighlight( clientId, true );
}, [ clientId, setIsHovered, toggleBlockHighlight ] );
const onMouseLeave = useCallback( () => {
setIsHovered( false );
highlightBlock( clientId, false );
}, [ clientId, setIsHovered, highlightBlock ] );
toggleBlockHighlight( clientId, false );
}, [ clientId, setIsHovered, toggleBlockHighlight ] );

const selectEditorBlock = useCallback(
( event ) => {
Expand Down Expand Up @@ -174,29 +173,20 @@ function ListViewBlock( {
[ clientId, expand, collapse, isExpanded ]
);

const showBlockActions =
withExperimentalFeatures &&
// hide actions for blocks like core/widget-areas
( ! hideContainerBlockActions ||
( hideContainerBlockActions && level > 1 ) );

const hideBlockActions = withExperimentalFeatures && ! showBlockActions;

let colSpan;
if ( hasRenderedMovers ) {
colSpan = 2;
} else if ( hideBlockActions ) {
} else if ( ! showBlockActions ) {
colSpan = 3;
}

const classes = classnames( {
'is-selected': isSelected,
'is-first-selected': isFirstSelectedBlock,
'is-last-selected': isLastSelectedBlock,
'is-branch-selected':
withExperimentalPersistentListViewFeatures && isBranchSelected,
'is-branch-selected': isBranchSelected,
'is-dragging': isDragged,
'has-single-cell': hideBlockActions,
'has-single-cell': ! showBlockActions,
} );

// Only include all selected blocks if the currently clicked on block
Expand Down
16 changes: 3 additions & 13 deletions packages/block-editor/src/components/list-view/branch.js
Expand Up @@ -85,7 +85,6 @@ function ListViewBranch( props ) {
blocks,
selectBlock,
showBlockMovers,
showNestedBlocks,
selectedClientIds,
level = 1,
path = '',
Expand All @@ -95,11 +94,7 @@ function ListViewBranch( props ) {
isExpanded,
} = props;

const {
expandedState,
draggedClientIds,
__experimentalPersistentListViewFeatures,
} = useListViewContext();
const { expandedState, draggedClientIds } = useListViewContext();

const filteredBlocks = compact( blocks );
const blockCount = filteredBlocks.length;
Expand All @@ -119,19 +114,15 @@ function ListViewBranch( props ) {
);
}

const usesWindowing = __experimentalPersistentListViewFeatures;

const { itemInView } = fixedListWindow;
const blockInView =
! usesWindowing || itemInView( nextPosition );
const blockInView = itemInView( nextPosition );

const position = index + 1;
const updatedPath =
path.length > 0
? `${ path }_${ position }`
: `${ position }`;
const hasNestedBlocks =
showNestedBlocks && !! innerBlocks && !! innerBlocks.length;
const hasNestedBlocks = !! innerBlocks?.length;

const shouldExpand = hasNestedBlocks
? expandedState[ clientId ] ?? isExpanded
Expand Down Expand Up @@ -179,7 +170,6 @@ function ListViewBranch( props ) {
blocks={ innerBlocks }
selectBlock={ selectBlock }
showBlockMovers={ showBlockMovers }
showNestedBlocks={ showNestedBlocks }
level={ level + 1 }
path={ updatedPath }
listPosition={ nextPosition + 1 }
Expand Down
5 changes: 1 addition & 4 deletions packages/block-editor/src/components/list-view/context.js
Expand Up @@ -3,9 +3,6 @@
*/
import { createContext, useContext } from '@wordpress/element';

export const ListViewContext = createContext( {
__experimentalFeatures: false,
__experimentalPersistentListViewFeatures: false,
} );
export const ListViewContext = createContext( {} );

export const useListViewContext = () => useContext( ListViewContext );
50 changes: 10 additions & 40 deletions packages/block-editor/src/components/list-view/index.js
Expand Up @@ -48,33 +48,17 @@ const expanded = ( state, action ) => {
export const BLOCK_LIST_ITEM_HEIGHT = 36;

/**
* Wrap `ListViewRows` with `TreeGrid`. ListViewRows is a
* recursive component (it renders itself), so this ensures TreeGrid is only
* present at the very top of the navigation grid.
* Show a hierarchical list of blocks.
*
* @param {Object} props Components props.
* @param {Array} props.blocks Custom subset of block client IDs to be used instead of the default hierarchy.
* @param {boolean} props.showNestedBlocks Flag to enable displaying nested blocks.
* @param {boolean} props.showBlockMovers Flag to enable block movers
* @param {boolean} props.__experimentalFeatures Flag to enable experimental features.
* @param {boolean} props.__experimentalPersistentListViewFeatures Flag to enable features for the Persistent List View experiment.
* @param {boolean} props.__experimentalHideContainerBlockActions Flag to hide actions of top level blocks (like core/widget-area)
* @param {string} props.id Unique identifier for the root list element (primarily for a11y purposes).
* @param {boolean} props.isExpanded Flag to determine whether nested levels are expanded by default.
* @param {Object} ref Forwarded ref
* @param {Object} props Components props.
* @param {string} props.id An HTML element id for the root element of ListView.
* @param {Array} props.blocks Custom subset of block client IDs to be used instead of the default hierarchy.
* @param {boolean} props.showBlockMovers Flag to enable block movers
* @param {boolean} props.isExpanded Flag to determine whether nested levels are expanded by default.
* @param {Object} ref Forwarded ref
*/
function ListView(
{
blocks,
__experimentalFeatures,
__experimentalPersistentListViewFeatures,
__experimentalHideContainerBlockActions,
showNestedBlocks,
showBlockMovers,
id,
isExpanded = false,
...props
},
{ id, blocks, showBlockMovers = false, isExpanded = false },
ref
) {
const {
Expand Down Expand Up @@ -131,7 +115,7 @@ function ListView(
BLOCK_LIST_ITEM_HEIGHT,
visibleBlockCount,
{
useWindowing: __experimentalPersistentListViewFeatures,
useWindowing: true,
windowOverscan: 40,
}
);
Expand Down Expand Up @@ -181,25 +165,13 @@ function ListView(

const contextValue = useMemo(
() => ( {
__experimentalFeatures,
__experimentalPersistentListViewFeatures,
__experimentalHideContainerBlockActions,
isTreeGridMounted: isMounted.current,
draggedClientIds,
expandedState,
expand,
collapse,
} ),
[
__experimentalFeatures,
__experimentalPersistentListViewFeatures,
__experimentalHideContainerBlockActions,
isMounted.current,
draggedClientIds,
expandedState,
expand,
collapse,
]
[ isMounted.current, draggedClientIds, expandedState, expand, collapse ]
);

return (
Expand All @@ -221,12 +193,10 @@ function ListView(
<ListViewBranch
blocks={ clientIdsTree }
selectBlock={ selectEditorBlock }
showNestedBlocks={ showNestedBlocks }
showBlockMovers={ showBlockMovers }
fixedListWindow={ fixedListWindow }
selectedClientIds={ selectedClientIds }
isExpanded={ isExpanded }
{ ...props }
/>
</ListViewContext.Provider>
</TreeGrid>
Expand Down
Expand Up @@ -60,11 +60,7 @@ export default function ListViewSidebar() {
focusOnMountRef,
] ) }
>
<ListView
showNestedBlocks
__experimentalFeatures
__experimentalPersistentListViewFeatures
/>
<ListView />
</div>
</div>
);
Expand Down
Expand Up @@ -59,11 +59,7 @@ export default function ListViewSidebar() {
focusOnMountRef,
] ) }
>
<ListView
showNestedBlocks
__experimentalFeatures
__experimentalPersistentListViewFeatures
/>
<ListView />
</div>
</div>
);
Expand Down
Expand Up @@ -50,12 +50,7 @@ export default function NavigationMenu( { innerBlocks, id } ) {
}, [ updateBlockListSettings, innerBlocks ] );
return (
<>
<ListView
id={ id }
showNestedBlocks
__experimentalFeatures
__experimentalPersistentListViewFeatures
/>
<ListView id={ id } />
</>
);
}
1 change: 1 addition & 0 deletions packages/edit-widgets/package.json
Expand Up @@ -35,6 +35,7 @@
"@wordpress/compose": "file:../compose",
"@wordpress/core-data": "file:../core-data",
"@wordpress/data": "file:../data",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
"@wordpress/element": "file:../element",
"@wordpress/hooks": "file:../hooks",
Expand Down
Expand Up @@ -60,12 +60,7 @@ export default function ListViewSidebar() {
focusOnMountRef,
] ) }
>
<ListView
showNestedBlocks
__experimentalHideContainerBlockActions
__experimentalFeatures
__experimentalPersistentListViewFeatures
/>
<ListView />
</div>
</div>
);
Expand Down

0 comments on commit 1b91575

Please sign in to comment.