-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* chore: keyboard navigation improvements * CB-5446 chore: use theme color for outline --------- Co-authored-by: Evgenia Bezborodova <139753579+EvgeniaBzzz@users.noreply.github.com>
- Loading branch information
1 parent
0e3da49
commit 0bf6783
Showing
35 changed files
with
227 additions
and
62 deletions.
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 | ||
|
||
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.