Skip to content

Commit

Permalink
example of storing scroll position
Browse files Browse the repository at this point in the history
  • Loading branch information
gwwar committed Jun 24, 2021
1 parent b787346 commit a71419e
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion packages/block-editor/src/components/writing-flow/use-tab-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { focus } from '@wordpress/dom';
import { TAB, ESCAPE } from '@wordpress/keycodes';
import { useSelect, useDispatch } from '@wordpress/data';
import { useRefEffect, useMergeRefs } from '@wordpress/compose';
import { useRef } from '@wordpress/element';
import { useRef, useState } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -22,6 +22,19 @@ function isFormElement( element ) {
);
}

const SCROLLABLE_CONTENT = '.interface-interface-skeleton__content';

// Two divs are inserted before and after block list content. When tabbing avoid
// scrolling to top or bottom of content
const fixFocusCaptureScroll = ( focusableElement, scroll ) => {
if ( scroll >= 0 ) {
focusableElement.closest( SCROLLABLE_CONTENT ).scrollTop = scroll;
} else {
//TODO: fallback + site editor is iframed, find a better alternative
focusableElement.scrollIntoView();
}
};

export default function useTabNav() {
const container = useRef();
const focusCaptureBeforeRef = useRef();
Expand All @@ -35,6 +48,7 @@ export default function useTabNav() {
( select ) => select( blockEditorStore ).isNavigationMode(),
[]
);
const [ scroll, setScroll ] = useState( 0 );

// Don't allow tabbing to this element in Navigation mode.
const focusCaptureTabIndex = ! isNavigationMode ? '0' : undefined;
Expand All @@ -49,8 +63,12 @@ export default function useTabNav() {
noCapture.current = null;
} else if ( hasMultiSelection() ) {
container.current.focus();
//when focusing on before/after do not scroll to bottom or top of document
fixFocusCaptureScroll( container.current, scroll );
} else if ( getSelectedBlockClientId() ) {
lastFocus.current.focus();
//when focusing on before/after do not scroll to bottom or top of document
fixFocusCaptureScroll( lastFocus.current, scroll );
} else {
setNavigationMode( true );

Expand Down Expand Up @@ -124,6 +142,10 @@ export default function useTabNav() {
// (moving focus to the next tabbable element).
noCapture.current = true;

setScroll(
container?.current?.closest( SCROLLABLE_CONTENT )?.scrollTop ??
-1
);
// Focusing the focus capture element, which is located above and
// below the editor, should not scroll the page all the way up or
// down.
Expand Down

0 comments on commit a71419e

Please sign in to comment.