Skip to content

Commit

Permalink
Home and End button
Browse files Browse the repository at this point in the history
  • Loading branch information
dlnr committed Feb 8, 2024
1 parent 6fc764c commit e7cbbbd
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/react/src/Tabs/useFocusWithArrows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type { KeyboardEvent, RefObject } from 'react'
export const KeyboardKeys = {
ArrowRight: 'ArrowRight',
ArrowLeft: 'ArrowLeft',
Home: 'Home',
End: 'End',
}

const useFocusWithArrows = (ref: RefObject<HTMLDivElement>, rotating = false) => {
Expand Down Expand Up @@ -51,10 +53,26 @@ const useFocusWithArrows = (ref: RefObject<HTMLDivElement>, rotating = false) =>
break
}

case KeyboardKeys.Home: {
el = focusableEls[0]
break
}

case KeyboardKeys.End: {
el = focusableEls[focusableEls.length - 1]
break
}

default:
}

if ((e.key === KeyboardKeys.ArrowLeft || e.key === KeyboardKeys.ArrowRight) && el instanceof HTMLElement) {
if (
(e.key === KeyboardKeys.ArrowLeft ||
e.key === KeyboardKeys.ArrowRight ||
e.key === KeyboardKeys.Home ||
e.key === KeyboardKeys.End) &&
el instanceof HTMLElement
) {
el.focus()
e.preventDefault()
}
Expand Down

0 comments on commit e7cbbbd

Please sign in to comment.