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

Alternative: add inserter to Nav block offcanvas experiment #45947

Merged
merged 8 commits into from
Nov 23, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { forwardRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import Inserter from '../inserter';

export const Appender = forwardRef( ( props, ref ) => {
const { hideInserter, clientId } = useSelect( ( select ) => {
const {
getTemplateLock,
__unstableGetEditorMode,
getSelectedBlockClientId,
} = select( blockEditorStore );

const _clientId = getSelectedBlockClientId();
getdave marked this conversation as resolved.
Show resolved Hide resolved

return {
clientId: getSelectedBlockClientId(),
hideInserter:
!! getTemplateLock( _clientId ) ||
__unstableGetEditorMode() === 'zoom-out',
};
}, [] );

if ( hideInserter ) {
return null;
}

return (
<div className="offcanvas-editor__appender">
<Inserter
ref={ ref }
rootClientId={ clientId }
position="bottom right"
isAppender
__experimentalIsQuick
{ ...props }
/>
</div>
);
} );
44 changes: 34 additions & 10 deletions packages/block-editor/src/components/off-canvas-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
useMergeRefs,
__experimentalUseFixedWindowList as useFixedWindowList,
} from '@wordpress/compose';
import { __experimentalTreeGrid as TreeGrid } from '@wordpress/components';
import {
__experimentalTreeGrid as TreeGrid,
__experimentalTreeGridRow as TreeGridRow,
__experimentalTreeGridCell as TreeGridCell,
} from '@wordpress/components';
import { AsyncModeProvider, useSelect } from '@wordpress/data';
import {
useCallback,
Expand All @@ -28,6 +32,7 @@ import useListViewClientIds from './use-list-view-client-ids';
import useListViewDropZone from './use-list-view-drop-zone';
import useListViewExpandSelectedItem from './use-list-view-expand-selected-item';
import { store as blockEditorStore } from '../../store';
import { Appender } from './appender';

const expanded = ( state, action ) => {
if ( Array.isArray( action.clientIds ) ) {
Expand Down Expand Up @@ -104,9 +109,9 @@ function __ExperimentalOffCanvasEditor(
setExpandedState,
} );
const selectEditorBlock = useCallback(
( event, clientId ) => {
updateBlockSelection( event, clientId );
setSelectedTreeId( clientId );
( event, blockClientId ) => {
updateBlockSelection( event, blockClientId );
setSelectedTreeId( blockClientId );
},
[ setSelectedTreeId, updateBlockSelection ]
);
Expand All @@ -128,20 +133,26 @@ function __ExperimentalOffCanvasEditor(
);

const expand = useCallback(
( clientId ) => {
if ( ! clientId ) {
( blockClientId ) => {
if ( ! blockClientId ) {
return;
}
setExpandedState( { type: 'expand', clientIds: [ clientId ] } );
setExpandedState( {
type: 'expand',
clientIds: [ blockClientId ],
} );
},
[ setExpandedState ]
);
const collapse = useCallback(
( clientId ) => {
if ( ! clientId ) {
( blockClientId ) => {
if ( ! blockClientId ) {
return;
}
setExpandedState( { type: 'collapse', clientIds: [ clientId ] } );
setExpandedState( {
type: 'collapse',
clientIds: [ blockClientId ],
} );
},
[ setExpandedState ]
);
Expand Down Expand Up @@ -208,9 +219,22 @@ function __ExperimentalOffCanvasEditor(
shouldShowInnerBlocks={ shouldShowInnerBlocks }
selectBlockInCanvas={ selectBlockInCanvas }
/>
<TreeGridRow
level={ 1 }
setSize={ 1 }
positionInSet={ 1 }
isExpanded={ true }
>
<TreeGridCell>
{ ( treeGridCellProps ) => (
<Appender { ...treeGridCellProps } />
) }
</TreeGridCell>
</TreeGridRow>
</ListViewContext.Provider>
</TreeGrid>
</AsyncModeProvider>
);
}

export default forwardRef( __ExperimentalOffCanvasEditor );
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
//Styles for off-canvas editor, remove this line when you add some css to this file!
.offcanvas-editor__appender .block-editor-inserter__toggle {
background-color: #1e1e1e;
getdave marked this conversation as resolved.
Show resolved Hide resolved
color: #fff;
margin: $grid-unit-10 0 0 28px;
border-radius: 2px;
height: 24px;
min-width: 24px;
padding: 0;

&:hover,
&:focus {
background: var(--wp-admin-theme-color);
color: #fff;
}
}