Skip to content
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

feat(RovingFocus): add support for up/down arrows and home/end buttons #2206

Merged
merged 11 commits into from
Aug 12, 2024
5 changes: 5 additions & 0 deletions .changeset/bright-knives-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@digdir/designsystemet-react": patch
---

RovingIndex: add support for up/down arrows and home/end buttons
mimarz marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 14 additions & 2 deletions packages/react/src/utilities/RovingFocus/RovingFocusItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,24 @@ export const RovingFocusItem = forwardRef<HTMLElement, RovingFocusItemProps>(
const items = getOrderedItems();
let nextItem: RovingFocusElement | undefined;

if (e.key === 'ArrowRight') {
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
nextItem = getNextFocusableValue(items, focusValue);
e.preventDefault();
}

if (e.key === 'ArrowLeft') {
if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
Barsnes marked this conversation as resolved.
Show resolved Hide resolved
nextItem = getPrevFocusableValue(items, focusValue);
e.preventDefault();
}

if (e.key === 'Home') {
nextItem = items[0];
e.preventDefault();
}

if (e.key === 'End') {
nextItem = items[items.length - 1];
e.preventDefault();
}

nextItem?.element.focus();
Expand Down
Loading