mirror of
https://github.com/mastodon/mastodon.git
synced 2025-10-05 16:42:47 +00:00
Refactor getFocusedItemIndex
to avoid conditionals that closest
already handles (#36267)
This commit is contained in:
parent
7431c50566
commit
238d74fe81
|
@ -60,23 +60,13 @@ export function focusColumn({
|
||||||
* Get the index of the currently focused item in one of our item lists
|
* Get the index of the currently focused item in one of our item lists
|
||||||
*/
|
*/
|
||||||
export function getFocusedItemIndex() {
|
export function getFocusedItemIndex() {
|
||||||
const focusedElement = document.activeElement;
|
const focusedItem = document.activeElement?.closest('.item-list > *');
|
||||||
const itemList = focusedElement?.closest('.item-list');
|
|
||||||
|
|
||||||
if (!focusedElement || !itemList) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
let focusedItem: HTMLElement | null = null;
|
|
||||||
if (focusedElement.parentElement === itemList) {
|
|
||||||
focusedItem = focusedElement as HTMLElement;
|
|
||||||
} else {
|
|
||||||
focusedItem = focusedElement.closest('.item-list > *');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!focusedItem) return -1;
|
if (!focusedItem) return -1;
|
||||||
|
|
||||||
const items = Array.from(itemList.children);
|
const { parentElement } = focusedItem;
|
||||||
|
if (!parentElement) return -1;
|
||||||
|
|
||||||
|
const items = Array.from(parentElement.children);
|
||||||
return items.indexOf(focusedItem);
|
return items.indexOf(focusedItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user