Skip to content

Commit

Permalink
- Improve tabs keyboard controls 2
Browse files Browse the repository at this point in the history
  • Loading branch information
duduBTW committed Nov 3, 2024
1 parent 2baf4fb commit d072a68
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/renderer/src/lib/roving-focus-group/rovingFocusGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,33 @@ export function useRovingFocusGroup(props: Params = {}) {
setCurrentStopId(firstNode.getAttribute(ITEM_DATA_ATTR)!);
};

const handleKeyUp = (
const handleListKeyUp = (
event: KeyboardEvent & {
currentTarget: DOMElement;
target: DOMElement;
},
) => {
if (!event.currentTarget.isSameNode(event.target)) {
return;
}

switch (event.key) {
case "ArrowUp":
case "ArrowLeft":
case "ArrowDown":
case "ArrowRight": {
event.preventDefault();
tryFocusFirstItem();
break;
}

default: {
break;
}
}
};

const handleItemKeyUp = (
event: KeyboardEvent & {
currentTarget: DOMElement;
target: DOMElement;
Expand Down Expand Up @@ -150,7 +176,7 @@ export function useRovingFocusGroup(props: Params = {}) {
tryFocusFirstItem,
value: currentStopId,
attrs: {
onKeyUp: handleKeyUp,
onKeyUp: handleListKeyUp,
ref: (node: HTMLElement) => {
container = node;
},
Expand Down Expand Up @@ -212,7 +238,7 @@ export function useRovingFocusGroup(props: Params = {}) {
isSelected,
tabIndex,
attrs: {
onKeyUp: handleKeyUp,
onKeyUp: handleItemKeyUp,
onClick: handleClick,
onPointerMove: handlePointerMove,
[ITEM_DATA_ATTR]: tabStopId ?? "fallback",
Expand Down

0 comments on commit d072a68

Please sign in to comment.