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

Multi-select: use writing flow container for focus #31572

Merged
merged 1 commit into from
May 6, 2021
Merged
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
69 changes: 28 additions & 41 deletions packages/block-editor/src/components/writing-flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ export default function WritingFlow( { children } ) {
const container = useRef();
const focusCaptureBeforeRef = useRef();
const focusCaptureAfterRef = useRef();
const multiSelectionContainer = useRef();

const entirelySelected = useRef();

Expand Down Expand Up @@ -283,6 +282,28 @@ export default function WritingFlow( { children } ) {
const isNavEdge = isVertical ? isVerticalEdge : isHorizontalEdge;
const { ownerDocument } = container.current;
const { defaultView } = ownerDocument;

if ( hasMultiSelection ) {
if ( keyCode === TAB ) {
// Disable focus capturing on the focus capture element, so it
// doesn't refocus this element and so it allows default behaviour
// (moving focus to the next tabbable element).
noCapture.current = true;

if ( isShift ) {
focusCaptureBeforeRef.current.focus();
} else {
focusCaptureAfterRef.current.focus();
}
} else if ( isNav ) {
const action = isShift ? expandSelection : moveSelection;
action( isReverse );
event.preventDefault();
}

return;
}

const selectedBlockClientId = getSelectedBlockClientId();

// In Edit mode, Tab should focus the first tabbable element after the
Expand Down Expand Up @@ -440,38 +461,9 @@ export default function WritingFlow( { children } ) {
}
}

function onMultiSelectKeyDown( event ) {
const { keyCode, shiftKey } = event;
const isUp = keyCode === UP;
const isDown = keyCode === DOWN;
const isLeft = keyCode === LEFT;
const isRight = keyCode === RIGHT;
const isReverse = isUp || isLeft;
const isHorizontal = isLeft || isRight;
const isVertical = isUp || isDown;
const isNav = isHorizontal || isVertical;

if ( keyCode === TAB ) {
// Disable focus capturing on the focus capture element, so it
// doesn't refocus this element and so it allows default behaviour
// (moving focus to the next tabbable element).
noCapture.current = true;

if ( shiftKey ) {
focusCaptureBeforeRef.current.focus();
} else {
focusCaptureAfterRef.current.focus();
}
} else if ( isNav ) {
const action = shiftKey ? expandSelection : moveSelection;
action( isReverse );
event.preventDefault();
}
}

useEffect( () => {
if ( hasMultiSelection && ! isMultiSelecting ) {
multiSelectionContainer.current.focus();
container.current.focus();
}
}, [ hasMultiSelection, isMultiSelecting ] );

Expand All @@ -498,7 +490,7 @@ export default function WritingFlow( { children } ) {
if ( noCapture.current ) {
noCapture.current = null;
} else if ( hasMultiSelection ) {
multiSelectionContainer.current.focus();
container.current.focus();
} else if ( getSelectedBlockClientId() ) {
lastFocus.current.focus();
} else {
Expand Down Expand Up @@ -529,21 +521,16 @@ export default function WritingFlow( { children } ) {
style={ PREVENT_SCROLL_ON_FOCUS }
/>
<div
ref={ multiSelectionContainer }
ref={ container }
className="block-editor-writing-flow"
onKeyDown={ onKeyDown }
onMouseDown={ onMouseDown }
tabIndex={ hasMultiSelection ? '0' : undefined }
aria-label={
hasMultiSelection
? __( 'Multiple selected blocks' )
: undefined
}
style={ PREVENT_SCROLL_ON_FOCUS }
onKeyDown={ onMultiSelectKeyDown }
/>
<div
ref={ container }
className="block-editor-writing-flow"
onKeyDown={ onKeyDown }
onMouseDown={ onMouseDown }
>
{ children }
</div>
Expand Down