Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Edit Widgets Document Tools Navigation to own component (#55778
) * Move DocumentTools to own file * Remove unused variables from edit widget header and document tools
- Loading branch information
Showing
2 changed files
with
136 additions
and
115 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
packages/edit-widgets/src/components/header/document-tools/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { __, _x } from '@wordpress/i18n'; | ||
import { Button, ToolbarItem } from '@wordpress/components'; | ||
import { | ||
NavigableToolbar, | ||
store as blockEditorStore, | ||
privateApis as blockEditorPrivateApis, | ||
} from '@wordpress/block-editor'; | ||
import { listView, plus } from '@wordpress/icons'; | ||
import { useCallback, useRef } from '@wordpress/element'; | ||
import { useViewportMatch } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import UndoButton from '../undo-redo/undo'; | ||
import RedoButton from '../undo-redo/redo'; | ||
import useLastSelectedWidgetArea from '../../../hooks/use-last-selected-widget-area'; | ||
import { store as editWidgetsStore } from '../../../store'; | ||
import { unlock } from '../../../lock-unlock'; | ||
|
||
const { useShouldContextualToolbarShow } = unlock( blockEditorPrivateApis ); | ||
|
||
function DocumentTools( { setListViewToggleElement } ) { | ||
const isMediumViewport = useViewportMatch( 'medium' ); | ||
const inserterButton = useRef(); | ||
const widgetAreaClientId = useLastSelectedWidgetArea(); | ||
const isLastSelectedWidgetAreaOpen = useSelect( | ||
( select ) => | ||
select( editWidgetsStore ).getIsWidgetAreaOpen( | ||
widgetAreaClientId | ||
), | ||
[ widgetAreaClientId ] | ||
); | ||
const { isInserterOpen, isListViewOpen } = useSelect( ( select ) => { | ||
const { isInserterOpened, isListViewOpened } = | ||
select( editWidgetsStore ); | ||
return { | ||
isInserterOpen: isInserterOpened(), | ||
isListViewOpen: isListViewOpened(), | ||
}; | ||
}, [] ); | ||
const { setIsWidgetAreaOpen, setIsInserterOpened, setIsListViewOpened } = | ||
useDispatch( editWidgetsStore ); | ||
const { selectBlock } = useDispatch( blockEditorStore ); | ||
const handleClick = () => { | ||
if ( isInserterOpen ) { | ||
// Focusing the inserter button closes the inserter popover. | ||
setIsInserterOpened( false ); | ||
} else { | ||
if ( ! isLastSelectedWidgetAreaOpen ) { | ||
// Select the last selected block if hasn't already. | ||
selectBlock( widgetAreaClientId ); | ||
// Open the last selected widget area when opening the inserter. | ||
setIsWidgetAreaOpen( widgetAreaClientId, true ); | ||
} | ||
// The DOM updates resulting from selectBlock() and setIsInserterOpened() calls are applied the | ||
// same tick and pretty much in a random order. The inserter is closed if any other part of the | ||
// app receives focus. If selectBlock() happens to take effect after setIsInserterOpened() then | ||
// the inserter is visible for a brief moment and then gets auto-closed due to focus moving to | ||
// the selected block. | ||
window.requestAnimationFrame( () => setIsInserterOpened( true ) ); | ||
} | ||
}; | ||
|
||
const toggleListView = useCallback( | ||
() => setIsListViewOpened( ! isListViewOpen ), | ||
[ setIsListViewOpened, isListViewOpen ] | ||
); | ||
|
||
const { | ||
shouldShowContextualToolbar, | ||
canFocusHiddenToolbar, | ||
fixedToolbarCanBeFocused, | ||
} = useShouldContextualToolbarShow(); | ||
// If there's a block toolbar to be focused, disable the focus shortcut for the document toolbar. | ||
// There's a fixed block toolbar when the fixed toolbar option is enabled or when the browser width is less than the large viewport. | ||
const blockToolbarCanBeFocused = | ||
shouldShowContextualToolbar || | ||
canFocusHiddenToolbar || | ||
fixedToolbarCanBeFocused; | ||
|
||
return ( | ||
<NavigableToolbar | ||
className="edit-widgets-header-toolbar" | ||
aria-label={ __( 'Document tools' ) } | ||
shouldUseKeyboardFocusShortcut={ ! blockToolbarCanBeFocused } | ||
> | ||
<ToolbarItem | ||
ref={ inserterButton } | ||
as={ Button } | ||
className="edit-widgets-header-toolbar__inserter-toggle" | ||
variant="primary" | ||
isPressed={ isInserterOpen } | ||
onMouseDown={ ( event ) => { | ||
event.preventDefault(); | ||
} } | ||
onClick={ handleClick } | ||
icon={ plus } | ||
/* translators: button label text should, if possible, be under 16 | ||
characters. */ | ||
label={ _x( | ||
'Toggle block inserter', | ||
'Generic label for block inserter button' | ||
) } | ||
/> | ||
{ isMediumViewport && ( | ||
<> | ||
<UndoButton /> | ||
<RedoButton /> | ||
<ToolbarItem | ||
as={ Button } | ||
className="edit-widgets-header-toolbar__list-view-toggle" | ||
icon={ listView } | ||
isPressed={ isListViewOpen } | ||
/* translators: button label text should, if possible, be under 16 characters. */ | ||
label={ __( 'List View' ) } | ||
onClick={ toggleListView } | ||
ref={ setListViewToggleElement } | ||
/> | ||
</> | ||
) } | ||
</NavigableToolbar> | ||
); | ||
} | ||
|
||
export default DocumentTools; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ee96542
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in ee96542.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6732567314
📝 Reported issues:
/test/e2e/specs/editor/various/multi-block-selection.spec.js