Skip to content

Commit

Permalink
Merge pull request #31 from borisbelmar/develop
Browse files Browse the repository at this point in the history
1.2.10
  • Loading branch information
borisbelmar authored May 9, 2023
2 parents 750d1d4 + ae2842b commit 205531d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ At the top of your application, you need to initialize the module. This will add
```typescript
import { initArrowNavigation } from '@arrow-navigation/core'

initArrowNavigation()
initArrowNavigation({
preventScroll: true // Prevent the default behavior of the arrow keys to scroll the page. The default value is true
})
```

Then, you can use the `getArrowNavigation` to access the API.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@arrow-navigation/core",
"version": "1.2.9",
"version": "1.2.10",
"description": "Zero-dependency library to navigate through UI elements using the keyboard arrow keys built with Typescript",
"main": "./lib/index.js",
"module": "./lib/index.mjs",
Expand Down
7 changes: 4 additions & 3 deletions src/arrowNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export const ERROR_MESSAGES = {

export function initArrowNavigation ({
errorOnReinit = false,
debug = false
debug = false,
preventScroll = true
}: ArrowNavigationOptions = {}) {
const state: ArrowNavigationState = {
currentElement: null,
Expand All @@ -38,7 +39,7 @@ export function initArrowNavigation ({
const changeFocusElementHandler = (nextElement: FocusableElement, direction?: Direction) => {
const prevElement = getCurrentElement(state) as FocusableElement
state.currentElement = nextElement.id
nextElement.el.focus()
nextElement.el.focus({ preventScroll })
changeFocusEventHandler({
nextElement,
prevElement,
Expand All @@ -60,7 +61,7 @@ export function initArrowNavigation ({

const onKeyPress = getArrowPressHandler(state, changeFocusElementHandler)

const onGlobalFocus = (event: FocusEvent) => globalFocusHandler(state, event)
const onGlobalFocus = (event: FocusEvent) => globalFocusHandler(state, event, preventScroll)

window.addEventListener('keydown', onKeyPress)
window.addEventListener('focus', onGlobalFocus, true)
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/globalFocusHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('globalFocusHandler', () => {
state.currentElement = 'element-0-0'
const focusMock = jest.fn();
(getCurrentElement(state) as FocusableElement).el.focus = focusMock
globalFocusHandler(state, event)
globalFocusHandler(state, event, true)
expect(state.currentElement).toBe('element-0-0')
expect(focusMock).toHaveBeenCalled()
})
Expand All @@ -25,15 +25,15 @@ describe('globalFocusHandler', () => {
state.currentElement = 'element-0-0'
const focusMock = jest.fn();
(getCurrentElement(state) as FocusableElement).el.focus = focusMock
globalFocusHandler(state, event)
globalFocusHandler(state, event, true)
expect(state.currentElement).toBe('element-0-0')
expect(focusMock).not.toHaveBeenCalled()
})

it('should not focus the current element if not exists on state', () => {
const event = { target: { id: 'non-existent' } } as unknown as FocusEvent
state.currentElement = 'non-existent-current'
globalFocusHandler(state, event)
globalFocusHandler(state, event, true)
expect(state.currentElement).toBe('non-existent-current')
expect(getCurrentElement(state)).toBe(null)
})
Expand Down
8 changes: 6 additions & 2 deletions src/handlers/globalFocusHandler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import getCurrentElement from '@/utils/getCurrentElement'
import { ArrowNavigationState } from '@/types'

const globalFocusHandler = (state: ArrowNavigationState, event: FocusEvent) => {
const globalFocusHandler = (
state: ArrowNavigationState,
event: FocusEvent,
preventScroll: boolean
) => {
const target = event.target as HTMLElement
const currentElement = getCurrentElement(state)
if (!currentElement) return
if (target && target.id !== currentElement.id) {
currentElement.el.focus()
currentElement.el.focus({ preventScroll })
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export type ArrowNavigationState = {
export type ArrowNavigationOptions = {
debug?: boolean
errorOnReinit?: boolean
preventScroll?: boolean
}

export type GetNextOptions = {
Expand Down

0 comments on commit 205531d

Please sign in to comment.