Skip to content

Commit

Permalink
Merge pull request #24 from github/click-anything
Browse files Browse the repository at this point in the history
Trigger click on anything
  • Loading branch information
muan committed Oct 24, 2019
2 parents 94ebc1a + 4c8758d commit 128bdfc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -2,7 +2,7 @@

Trigger an action on a target element when a key or sequence of keys is pressed
on the keyboard. This triggers a focus event on form fields, or a click event on
`<a href="...">`, `<button>` and `<summary>` elements.
others.

By default, hotkeys are extracted from a target element's `data-hotkey`
attribute, but this can be overridden by passing the hotkey to the registering
Expand All @@ -29,6 +29,8 @@ wait for either `g c` or `g i`.

## Accessibility considerations

### Character Key Shortcuts

Please note that adding this functionality to your site can be a drawback for
certain users. Providing a way in your system to disable hotkeys or remap
them makes sure that those users can still use your site (given that it's
Expand All @@ -37,6 +39,10 @@ accessible to those users).
See ["Understanding Success Criterion 2.1.4: Character Key Shortcuts"](https://www.w3.org/WAI/WCAG21/Understanding/character-key-shortcuts.html)
for further reading on this topic.

### Interactive Elements

Wherever possible, hotkeys should be add to [interactive and focusable elements](https://html.spec.whatwg.org/#interactive-content). If a static element must be used, please follow the guideline in ["Adding keyboard-accessible actions to static HTML elements"](https://www.w3.org/WAI/WCAG21/Techniques/client-side-script/SCR29.html).

## Installation

```
Expand Down
17 changes: 1 addition & 16 deletions src/utils.js
Expand Up @@ -15,25 +15,10 @@ export function isFormField(element: Node): boolean {
)
}

function isActivableFormField(element: Node): boolean {
if (!(element instanceof HTMLElement)) {
return false
}

const name = element.nodeName.toLowerCase()
const type = (element.getAttribute('type') || '').toLowerCase()
return name === 'input' && (type === 'checkbox' || type === 'radio')
}

export function fireDeterminedAction(el: HTMLElement): void {
if (isFormField(el)) {
el.focus()
} else if (
(el instanceof HTMLAnchorElement && el.href) ||
el.tagName === 'BUTTON' ||
el.tagName === 'SUMMARY' ||
isActivableFormField(el)
) {
} else {
el.click()
}
}
Expand Down

0 comments on commit 128bdfc

Please sign in to comment.