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

Don't call preventDefault() if the hotkey-fire event has been cancelled #125

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/index.ts
Expand Up @@ -52,8 +52,7 @@ function keyDownHandler(event: KeyboardEvent) {
}

if (elementToFire && shouldFire) {
fireDeterminedAction(elementToFire, sequenceTracker.path)
event.preventDefault()
if (fireDeterminedAction(elementToFire, sequenceTracker.path)) event.preventDefault()
}

sequenceTracker.reset()
Expand Down
6 changes: 4 additions & 2 deletions src/utils.ts
Expand Up @@ -21,15 +21,17 @@ export function isFormField(element: Node): boolean {
)
}

export function fireDeterminedAction(el: HTMLElement, path: readonly NormalizedHotkeyString[]): void {
export function fireDeterminedAction(el: HTMLElement, path: readonly NormalizedHotkeyString[]): boolean {
const delegateEvent = new CustomEvent('hotkey-fire', {cancelable: true, detail: {path}})
const cancelled = !el.dispatchEvent(delegateEvent)
if (cancelled) return
if (cancelled) return false

if (isFormField(el)) {
el.focus()
} else {
el.click()
}
return true
}

export function expandHotkeyToEdges(hotkey: string): NormalizedHotkeyString[][] {
Expand Down
8 changes: 6 additions & 2 deletions test/test.js
Expand Up @@ -46,8 +46,10 @@ describe('hotkey', function () {
describe('single key support', function () {
it('triggers buttons that have a `data-hotkey` attribute', function () {
setHTML('<button id="button1" data-hotkey="b">Button 1</button>')
document.dispatchEvent(new KeyboardEvent('keydown', {key: 'b'}))
const event = new KeyboardEvent('keydown', {cancelable: true, key: 'b'})
document.dispatchEvent(event)
assert.include(elementsActivated, 'button1')
assert.ok(event.defaultPrevented, 'should call preventDefault on keydown event')
})

it('triggers buttons that get hotkey passed in as second argument', function () {
Expand Down Expand Up @@ -118,8 +120,10 @@ describe('hotkey', function () {
it('wont trigger action if the hotkey-fire event is cancelled', function () {
setHTML('<button id="button1" data-hotkey="Shift+B">Button 1</button>')
document.querySelector('#button1').addEventListener('hotkey-fire', event => event.preventDefault())
document.dispatchEvent(new KeyboardEvent('keydown', {shiftKey: true, code: 'KeyB', key: 'B'}))
const event = new KeyboardEvent('keydown', {cancelable: true, shiftKey: true, code: 'KeyB', key: 'B'})
document.dispatchEvent(event)
assert.notInclude(elementsActivated, 'button1')
assert.equal(event.defaultPrevented, false, 'should not prevent the keydown event when cancelled')
})

it('supports comma as a hotkey', function () {
Expand Down