Skip to content

Commit 00787b0

Browse files
authored
Prevent autoscrolling of focused collection elements when focus isn't in the non virtualized collection (#4133)
1 parent cb1fc23 commit 00787b0

File tree

2 files changed

+7
-23
lines changed

2 files changed

+7
-23
lines changed

packages/@react-aria/selection/src/useSelectableCollection.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {DOMAttributes, FocusableElement, FocusStrategy, KeyboardDelegate} from '
1414
import {FocusEvent, Key, KeyboardEvent, RefObject, useEffect, useRef} from 'react';
1515
import {focusSafely, getFocusableTreeWalker} from '@react-aria/focus';
1616
import {focusWithoutScrolling, mergeProps, scrollIntoView, scrollIntoViewport, useEvent} from '@react-aria/utils';
17+
import {getInteractionModality} from '@react-aria/interactions';
1718
import {isCtrlKeyPressed, isNonContiguousSelectionModifier} from './utils';
1819
import {MultipleSelectionManager} from '@react-stately/selection';
1920
import {useLocale} from '@react-aria/i18n';
@@ -356,14 +357,17 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
356357
// If not virtualized, scroll the focused element into view when the focusedKey changes.
357358
// When virtualized, Virtualizer handles this internally.
358359
useEffect(() => {
359-
if (!isVirtualized && manager.focusedKey && scrollRef?.current) {
360+
let modality = getInteractionModality();
361+
if (!isVirtualized && manager.isFocused && manager.focusedKey != null && scrollRef?.current) {
360362
let element = scrollRef.current.querySelector(`[data-key="${manager.focusedKey}"]`) as HTMLElement;
361363
if (element) {
362364
scrollIntoView(scrollRef.current, element);
363-
scrollIntoViewport(element, {containingElement: ref.current});
365+
if (modality === 'keyboard') {
366+
scrollIntoViewport(element, {containingElement: ref.current});
367+
}
364368
}
365369
}
366-
}, [isVirtualized, scrollRef, manager.focusedKey, ref]);
370+
}, [isVirtualized, scrollRef, manager.focusedKey, manager.isFocused, ref]);
367371

368372
let handlers = {
369373
onKeyDown,

packages/@react-spectrum/tabs/test/Tabs.test.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,6 @@ describe('Tabs', function () {
353353
it('collapses when it can\'t render all the tabs horizontally', function () {
354354
let target = [HTMLDivElement.prototype, 'getBoundingClientRect'];
355355
let mockCalls = [
356-
function () {
357-
return {
358-
left: 0
359-
};
360-
},
361-
function () {
362-
return {
363-
right: 0
364-
};
365-
},
366356
function () {
367357
return {
368358
left: 0,
@@ -657,16 +647,6 @@ describe('Tabs', function () {
657647
it('disabled tabs cannot be selected via collapse picker', function () {
658648
let target = [HTMLDivElement.prototype, 'getBoundingClientRect'];
659649
let mockCalls = [
660-
function () {
661-
return {
662-
left: 0
663-
};
664-
},
665-
function () {
666-
return {
667-
right: 0
668-
};
669-
},
670650
function () {
671651
return {
672652
left: 0,

0 commit comments

Comments
 (0)