-
Notifications
You must be signed in to change notification settings - Fork 398
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
CB-5446 chore: keyboard navigation improvements #2935
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
webapp/packages/core-blocks/src/useListKeyboardNavigation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* CloudBeaver - Cloud Database Manager | ||
* Copyright (C) 2020-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0. | ||
* you may not use this file except in compliance with the License. | ||
*/ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { EventContext } from '@cloudbeaver/core-events'; | ||
|
||
export const EventKeyboardNavigationFlag = EventContext.create('useListKeyboardNavigation'); | ||
|
||
export function useListKeyboardNavigation<T extends HTMLElement>(elementsSelector = '[tabindex]:not(:disabled)'): (obj: T | null) => void { | ||
const [ref, setRef] = useState<T | null>(null); | ||
|
||
useEffect(() => { | ||
if (!ref) { | ||
return; | ||
} | ||
|
||
const getFocusableElements = () => { | ||
const allFocusableElements = Array.from(ref.querySelectorAll(elementsSelector)) as HTMLElement[]; | ||
return allFocusableElements; | ||
}; | ||
|
||
// Function to reset tabindex on all elements and set it to 0 on aria-selected="true" | ||
const resetTabindex = () => { | ||
const focusableElements = getFocusableElements(); | ||
focusableElements.forEach(el => { | ||
if (el.getAttribute('aria-selected') === 'true') { | ||
el.setAttribute('tabindex', '0'); | ||
} else { | ||
el.setAttribute('tabindex', '-1'); | ||
} | ||
}); | ||
}; | ||
|
||
const handleKeyDown = (e: KeyboardEvent) => { | ||
if (EventContext.has(e, EventKeyboardNavigationFlag) || !['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown'].includes(e.key)) { | ||
return; | ||
} | ||
|
||
const focusableElements = getFocusableElements(); | ||
let currentIndex = focusableElements.findIndex(el => el === document.activeElement); | ||
|
||
if (document.activeElement !== ref && currentIndex === -1) { | ||
return; | ||
} | ||
|
||
let newIndex = currentIndex; | ||
|
||
EventContext.set(e, EventKeyboardNavigationFlag); | ||
e.preventDefault(); | ||
|
||
switch (e.key) { | ||
case 'ArrowRight': | ||
case 'ArrowDown': | ||
newIndex = (currentIndex + 1) % focusableElements.length; // Move to next element | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets move to |
||
|
||
break; | ||
case 'ArrowLeft': | ||
case 'ArrowUp': | ||
if (currentIndex === -1) { | ||
currentIndex = 0; | ||
} | ||
newIndex = (currentIndex - 1 + focusableElements.length) % focusableElements.length; // Move to previous element | ||
|
||
break; | ||
default: | ||
return; | ||
} | ||
|
||
// // Reset all tabindex to -1 | ||
focusableElements.forEach(el => el.setAttribute('tabindex', '-1')); | ||
|
||
// Set the new element's tabindex to 0 and focus it | ||
const nextElement = focusableElements[newIndex]; | ||
nextElement?.setAttribute('tabindex', '0'); | ||
nextElement?.focus(); | ||
}; | ||
|
||
const handleFocusOut = (e: FocusEvent) => { | ||
// Check if the focus moved outside the container | ||
if (!ref.contains(e.relatedTarget as Node)) { | ||
resetTabindex(); | ||
} | ||
}; | ||
|
||
ref.addEventListener('keydown', handleKeyDown); | ||
ref.addEventListener('focusout', handleFocusOut); | ||
|
||
return () => { | ||
ref.removeEventListener('keydown', handleKeyDown); | ||
ref.removeEventListener('focusout', handleFocusOut); | ||
}; | ||
}, [ref, elementsSelector]); | ||
|
||
return setRef; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
.tabList { | ||
display: flex; | ||
box-sizing: border-box; | ||
outline: none; | ||
} | ||
|
||
.underline { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,4 @@ | |
flex: 0 0 auto; | ||
min-width: 150px; | ||
overflow: auto; | ||
outline: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this set of elements can include elements without
aria-selected
attribute. Is this okay to settabIndex
for them also to-1
? Or it is supposed to be only for elements witharia-selected
attribute?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is okay, if element don't have this attribute then tabindex=-1 is okay, but here more problems over -1 and 0 because sometimes you can't restore initial value (when you don't have aria-selected or when element was tabindex=0 initially)
but for now seems that it works, maybe we will found bugs related to this function later