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

fix(react-listbox): Fixing click/tap focus scroll bug introduced with recent Listbox standalone changes #31235

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Fixing bug where click or tap focusing an item in an unfocused listbox would result in the listbox scrolling all the way to the top due to unrestrained initial focus logic",
"packageName": "@fluentui/react-combobox",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ describe('Listbox', () => {
</Listbox>,
);

// set initial keyboard navigating
fireEvent.keyDown(getByTestId('listbox'), { key: 'Tab' });

const firstOption = getByTestId('firstOption');
expect(getByTestId('listbox').getAttribute('aria-activedescendant')).toBeNull();

Expand All @@ -103,6 +106,9 @@ describe('Listbox', () => {
</Listbox>,
);

// set initial keyboard navigating
fireEvent.keyDown(getByTestId('listbox'), { key: 'Tab' });

const selectedOption = getByTestId('firstSelectedOption');
expect(getByTestId('listbox').getAttribute('aria-activedescendant')).toBeNull();

Expand All @@ -120,6 +126,9 @@ describe('Listbox', () => {
</Listbox>,
);

// set initial keyboard navigating
fireEvent.keyDown(getByTestId('listbox'), { key: 'Tab' });

expect(getByTestId('listbox').getAttribute('aria-activedescendant')).toBeNull();

fireEvent.focus(getByTestId('listbox'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useOptionCollection } from '../../utils/useOptionCollection';
import { useSelection } from '../../utils/useSelection';
import { optionClassNames } from '../Option/useOptionStyles.styles';
import { ListboxContext, useListboxContext_unstable } from '../../contexts/ListboxContext';
import { useOnKeyboardNavigationChange } from '@fluentui/react-tabster';

// eslint-disable-next-line @typescript-eslint/naming-convention
const UNSAFE_noLongerUsed = {
Expand Down Expand Up @@ -75,6 +76,11 @@ export const useListbox_unstable = (props: ListboxProps, ref: React.Ref<HTMLElem
const hasParentActiveDescendantContext = useHasParentActiveDescendantContext();
const activeDescendantController = hasParentActiveDescendantContext ? activeDescendantContext.controller : controller;

const isNavigatingWithKeyboardRef = React.useRef(false);
useOnKeyboardNavigationChange(
_isNavigatingWithKeyboard => (isNavigatingWithKeyboardRef.current = _isNavigatingWithKeyboard),
);

const { clearSelection, selectedOptions, selectOption } = useSelection(props);

const onKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
Expand Down Expand Up @@ -113,7 +119,11 @@ export const useListbox_unstable = (props: ListboxProps, ref: React.Ref<HTMLElem
};

const onFocus = (_event: React.FocusEvent<HTMLElement>) => {
if (hasParentActiveDescendantContext || activeDescendantController.active()) {
if (
hasParentActiveDescendantContext ||
activeDescendantController.active() ||
!isNavigatingWithKeyboardRef.current
) {
return;
}

Expand Down
Loading