Skip to content

Commit

Permalink
Move lastFocus into redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
jeryj committed Sep 6, 2023
1 parent 96b184b commit fd31f65
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
24 changes: 24 additions & 0 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,18 @@ _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 @@ -1651,6 +1663,18 @@ _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
11 changes: 8 additions & 3 deletions packages/block-editor/src/components/writing-flow/use-tab-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ export default function useTabNav() {
const container = useRef();
const focusCaptureBeforeRef = useRef();
const focusCaptureAfterRef = useRef();
const lastFocus = useRef();

const { hasMultiSelection, getSelectedBlockClientId, getBlockCount } =
useSelect( blockEditorStore );
const { setNavigationMode } = useDispatch( blockEditorStore );
const { setNavigationMode, setLastFocus } = 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 Down Expand Up @@ -158,7 +163,7 @@ export default function useTabNav() {
}

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

const { ownerDocument } = node;

Expand Down
15 changes: 15 additions & 0 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1925,3 +1925,18 @@ 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,
};
}
18 changes: 18 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,23 @@ export function blockEditingModes( state = new Map(), action ) {
return state;
}

/**
* Reducer setting last focused element
*
* @param {boolean} state Current state.
* @param {Object} action Dispatched action.
*
* @return {boolean} Updated state.
*/
export function lastFocus( state = false, action ) {
switch ( action.type ) {
case 'LAST_FOCUS':
return action.lastFocus;
}

return state;
}

const combinedReducers = combineReducers( {
blocks,
isTyping,
Expand All @@ -1929,6 +1946,7 @@ const combinedReducers = combineReducers( {
settings,
preferences,
lastBlockAttributesChange,
lastFocus,
editorMode,
hasBlockMovingClientId,
highlightedBlock,
Expand Down
11 changes: 11 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3006,3 +3006,14 @@ 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;
}

0 comments on commit fd31f65

Please sign in to comment.