Skip to content

Commit

Permalink
[Onboarding] Fix categories scrolling for keyboard navigation (#182840)
Browse files Browse the repository at this point in the history
Closes #182778

Tracks whether user selects category using keyboard or mouse and
prevents automatic scrolling for keyboard.

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
mykolaharmash and kibanamachine committed May 10, 2024
1 parent 00ceb7c commit f3a04a2
Showing 1 changed file with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ export const OnboardingFlowForm: FunctionComponent = () => {
const packageListRef = useRef<HTMLDivElement | null>(null);
const formRef = useRef<HTMLDivElement | null>(null);
const [integrationSearch, setIntegrationSearch] = useState(searchParams.get('search') ?? '');
const selectedCategory: Category | null = searchParams.get('category') as Category | null;
const [scrollToCategory, setScrollToCategory] = useState<Category | null>(
searchParams.get('category') as Category | null
);

useEffect(() => {
if (selectedCategory === null || !hasPackageListLoaded) {
if (scrollToCategory === null || !hasPackageListLoaded) {
return;
}

Expand All @@ -107,7 +109,7 @@ export const OnboardingFlowForm: FunctionComponent = () => {
}, 10);

return () => clearTimeout(timeout);
}, [selectedCategory, hasPackageListLoaded]);
}, [scrollToCategory, hasPackageListLoaded]);

useEffect(() => {
const searchParam = searchParams.get('search') ?? '';
Expand Down Expand Up @@ -143,6 +145,8 @@ export const OnboardingFlowForm: FunctionComponent = () => {
);
const virtualSearchResults = useVirtualSearchResults();

let isSelectingCategoryWithKeyboard: boolean = false;

return (
<EuiPanel hasBorder paddingSize="xl" panelRef={formRef}>
<TitleWithIcon
Expand Down Expand Up @@ -175,9 +179,22 @@ export const OnboardingFlowForm: FunctionComponent = () => {
</>
}
checked={option.id === searchParams.get('category')}
/**
* onKeyDown and onKeyUp handlers disable
* scrolling to the category items when user
* changes the selected category using keyboard,
* which prevents our custom scroll behavior
* from conflicting with browser's native one to
* put keyboard-focused item into the view.
*/
onKeyDown={() => (isSelectingCategoryWithKeyboard = true)}
onKeyUp={() => (isSelectingCategoryWithKeyboard = false)}
onChange={() => {
setIntegrationSearch('');
setSearchParams({ category: option.id }, { replace: true });
if (!isSelectingCategoryWithKeyboard) {
setScrollToCategory(option.id);
}
}}
/>
</EuiFlexItem>
Expand Down

0 comments on commit f3a04a2

Please sign in to comment.