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

Fix Multiple Tooltips from Focus Toolbar Shortcut on Widget Editor #50344

Merged
merged 1 commit into from
May 5, 2023
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
Expand Up @@ -75,7 +75,7 @@ function SelectedBlockPopover( {
);
const isToolbarForced = useRef( false );
const { shouldShowContextualToolbar, canFocusHiddenToolbar } =
useShouldContextualToolbarShow( clientId );
useShouldContextualToolbarShow();

const { stopTyping } = useDispatch( blockEditorStore );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ import { unlock } from '../lock-unlock';
/**
* Returns true if the contextual block toolbar should show, or false if it should be hidden.
*
* @param {string} clientId The client ID of the block.
*
* @return {boolean} Whether the block toolbar is hidden.
*/
export function useShouldContextualToolbarShow( clientId ) {
export function useShouldContextualToolbarShow() {
const isLargeViewport = useViewportMatch( 'medium' );

const { shouldShowContextualToolbar, canFocusHiddenToolbar } = useSelect(
const {
shouldShowContextualToolbar,
canFocusHiddenToolbar,
fixedToolbarCanBeFocused,
} = useSelect(
( select ) => {
const {
__unstableGetEditorMode,
Expand All @@ -31,14 +33,19 @@ export function useShouldContextualToolbarShow( clientId ) {
getBlock,
getSettings,
isNavigationMode,
getSelectedBlockClientId,
getFirstMultiSelectedBlockClientId,
} = unlock( select( blockEditorStore ) );

const isEditMode = __unstableGetEditorMode() === 'edit';
const hasFixedToolbar = getSettings().hasFixedToolbar;
const isDistractionFree = getSettings().isDistractionFree;
const hasClientId = !! clientId;
const selectedBlockId =
getFirstMultiSelectedBlockClientId() ||
getSelectedBlockClientId();
const hasSelectedBlockId = !! selectedBlockId;
const isEmptyDefaultBlock = isUnmodifiedDefaultBlock(
getBlock( clientId ) || {}
getBlock( selectedBlockId ) || {}
);

const _shouldShowContextualToolbar =
Expand All @@ -48,13 +55,13 @@ export function useShouldContextualToolbarShow( clientId ) {
isLargeViewport &&
! isMultiSelecting() &&
! isTyping() &&
hasClientId &&
hasSelectedBlockId &&
! isEmptyDefaultBlock &&
! isBlockInterfaceHidden();

const _canFocusHiddenToolbar =
isEditMode &&
hasClientId &&
hasSelectedBlockId &&
! _shouldShowContextualToolbar &&
! hasFixedToolbar &&
! isDistractionFree &&
Expand All @@ -63,13 +70,16 @@ export function useShouldContextualToolbarShow( clientId ) {
return {
shouldShowContextualToolbar: _shouldShowContextualToolbar,
canFocusHiddenToolbar: _canFocusHiddenToolbar,
fixedToolbarCanBeFocused:
( hasFixedToolbar || ! isLargeViewport ) && selectedBlockId,
};
},
[ clientId, isLargeViewport ]
[ isLargeViewport ]
);

return {
shouldShowContextualToolbar,
canFocusHiddenToolbar,
fixedToolbarCanBeFocused,
};
}
25 changes: 8 additions & 17 deletions packages/edit-post/src/components/header/header-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,15 @@ function HeaderToolbar() {
showIconLabels,
isListViewOpen,
listViewShortcut,
selectedBlockId,
hasFixedToolbar,
} = useSelect( ( select ) => {
const {
hasInserterItems,
getBlockRootClientId,
getBlockSelectionEnd,
getSelectedBlockClientId,
getFirstMultiSelectedBlockClientId,
getSettings,
} = select( blockEditorStore );
const { hasInserterItems, getBlockRootClientId, getBlockSelectionEnd } =
select( blockEditorStore );
const { getEditorSettings } = select( editorStore );
const { getEditorMode, isFeatureActive, isListViewOpened } =
select( editPostStore );
const { getShortcutRepresentation } = select( keyboardShortcutsStore );

return {
hasFixedToolbar: getSettings().hasFixedToolbar,
selectedBlockId:
getSelectedBlockClientId() ||
getFirstMultiSelectedBlockClientId(),
// This setting (richEditingEnabled) should not live in the block editor's setting.
isInserterEnabled:
getEditorMode() === 'visual' &&
Expand All @@ -83,14 +71,17 @@ function HeaderToolbar() {

const isLargeViewport = useViewportMatch( 'medium' );
const isWideViewport = useViewportMatch( 'wide' );
const { shouldShowContextualToolbar, canFocusHiddenToolbar } =
useShouldContextualToolbarShow( selectedBlockId );
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 ||
( ( hasFixedToolbar || ! isLargeViewport ) && selectedBlockId );
fixedToolbarCanBeFocused;
/* translators: accessibility text for the editor toolbar */
const toolbarAriaLabel = __( 'Document tools' );

Expand Down
18 changes: 18 additions & 0 deletions packages/edit-widgets/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Button, ToolbarItem, VisuallyHidden } from '@wordpress/components';
import {
NavigableToolbar,
store as blockEditorStore,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { PinnedItems } from '@wordpress/interface';
import { listView, plus } from '@wordpress/icons';
Expand All @@ -22,6 +23,7 @@ import RedoButton from './undo-redo/redo';
import MoreMenu from '../more-menu';
import useLastSelectedWidgetArea from '../../hooks/use-last-selected-widget-area';
import { store as editWidgetsStore } from '../../store';
import { unlock } from '../../private-apis';

function Header() {
const isMediumViewport = useViewportMatch( 'medium' );
Expand Down Expand Up @@ -70,6 +72,19 @@ function Header() {
[ setIsListViewOpened, isListViewOpen ]
);

const { useShouldContextualToolbarShow } = unlock( blockEditorPrivateApis );
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 (
<>
<div className="edit-widgets-header">
Expand All @@ -90,6 +105,9 @@ function Header() {
<NavigableToolbar
className="edit-widgets-header-toolbar"
aria-label={ __( 'Document tools' ) }
shouldUseKeyboardFocusShortcut={
! blockToolbarCanBeFocused
}
>
<ToolbarItem
ref={ inserterButton }
Expand Down