Skip to content
Closed
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
24 changes: 0 additions & 24 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,18 +588,6 @@ _Properties_
- _isDisabled_ `boolean`: Whether or not the user should be prevented from inserting this item.
- _frecency_ `number`: Heuristic that combines frequency and recency.

### getLastFocus

Returns the element of the last element that had focus when focus left the editor canvas.

_Parameters_

- _state_ `Object`: Block editor state.

_Returns_

- `Object`: Element.

### getLastMultiSelectedBlockClientId

Returns the client ID of the last block in the multi-selection set, or null if there is no multi-selection.
Expand Down Expand Up @@ -1663,18 +1651,6 @@ _Parameters_
- _clientId_ `string`: The block's clientId.
- _hasControlledInnerBlocks_ `boolean`: True if the block's inner blocks are controlled.

### setLastFocus

Action that sets the element that had focus when focus leaves the editor canvas.

_Parameters_

- _lastFocus_ `Object`: The last focused element.

_Returns_

- `Object`: Action object.

### setNavigationMode

Action that enables or disables the navigation mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
useEffect,
useCallback,
} from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import { focus } from '@wordpress/dom';
import { useShortcut } from '@wordpress/keyboard-shortcuts';
Expand All @@ -18,7 +17,7 @@ import { ESCAPE } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { useLastFocus } from '../../utils/use-last-focus';

function hasOnlyToolbarItem( elements ) {
const dataProp = 'toolbarItem';
Expand Down Expand Up @@ -169,7 +168,8 @@ function useToolbarFocus( {
};
}, [ initialIndex, initialFocusOnMount, onIndexChange, toolbarRef ] );

const { getLastFocus } = useSelect( blockEditorStore );
const { lastFocus } = useLastFocus();

/**
* Handles returning focus to the block editor canvas when pressing escape.
*/
Expand All @@ -178,7 +178,6 @@ function useToolbarFocus( {

if ( focusEditorOnEscape ) {
const handleKeyDown = ( event ) => {
const lastFocus = getLastFocus();
if ( event.keyCode === ESCAPE && lastFocus?.current ) {
// Focus the last focused element when pressing escape.
event.preventDefault();
Expand All @@ -193,7 +192,7 @@ function useToolbarFocus( {
);
};
}
}, [ focusEditorOnEscape, getLastFocus, toolbarRef ] );
}, [ focusEditorOnEscape, lastFocus, toolbarRef ] );
}

export default function NavigableToolbar( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useRef } from '@wordpress/element';
*/
import { store as blockEditorStore } from '../../store';
import { isInSameBlock, isInsideRootBlock } from '../../utils/dom';
import { useLastFocus } from '../../utils/use-last-focus';

export default function useTabNav() {
const container = useRef();
Expand All @@ -20,17 +21,13 @@ export default function useTabNav() {

const { hasMultiSelection, getSelectedBlockClientId, getBlockCount } =
useSelect( blockEditorStore );
const { setNavigationMode, setLastFocus } = useDispatch( blockEditorStore );
const { lastFocus, setLastFocus } = useLastFocus();
const { setNavigationMode } = useDispatch( blockEditorStore );
const isNavigationMode = useSelect(
( select ) => select( blockEditorStore ).isNavigationMode(),
[]
);

const lastFocus = useSelect(
( select ) => select( blockEditorStore ).getLastFocus(),
[]
);

// Don't allow tabbing to this element in Navigation mode.
const focusCaptureTabIndex = ! isNavigationMode ? '0' : undefined;

Expand All @@ -45,7 +42,7 @@ export default function useTabNav() {
} else if ( hasMultiSelection() ) {
container.current.focus();
} else if ( getSelectedBlockClientId() ) {
lastFocus.current.focus();
lastFocus?.current.focus();
} else {
setNavigationMode( true );

Expand Down Expand Up @@ -163,7 +160,7 @@ export default function useTabNav() {
}

function onFocusOut( event ) {
setLastFocus( { ...lastFocus, current: event.target } );
setLastFocus( event.target );

const { ownerDocument } = node;

Expand Down
15 changes: 0 additions & 15 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1919,18 +1919,3 @@ export function unsetBlockEditingMode( clientId = '' ) {
clientId,
};
}

/**
* Action that sets the element that had focus when focus leaves the editor canvas.
*
* @param {Object} lastFocus The last focused element.
*
*
* @return {Object} Action object.
*/
export function setLastFocus( lastFocus = null ) {
return {
type: 'LAST_FOCUS',
lastFocus,
};
}
11 changes: 0 additions & 11 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2946,14 +2946,3 @@ export const isGroupable = createRegistrySelector(
);
}
);

/**
* Returns the element of the last element that had focus when focus left the editor canvas.
*
* @param {Object} state Block editor state.
*
* @return {Object} Element.
*/
export function getLastFocus( state ) {
return state.lastFocus;
}
18 changes: 18 additions & 0 deletions packages/block-editor/src/utils/use-last-focus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* WordPress dependencies
*/
import { createRef } from '@wordpress/element';

/**
* Returns the element of the last element that had focus when focus left the editor canvas.
*
* @return {Object} Object with a lastFocus ref and setLastFocus.
*/
const lastFocus = createRef();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, that is not a good approach because this is a global singleton, meaning that if you have multiple block editors in the same page, you'll end up with conflicts (they'll be sharing the same ref)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't consider multiple block editors on one page. Do you have any recommended routes to explore? I'll look at it again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar enough with what we're trying to achieve here but a private selector/action could work without changing any logic.


export function useLastFocus() {
const setLastFocus = ( node ) => {
lastFocus.current = node;
};
return { lastFocus, setLastFocus };
}