Skip to content

Commit

Permalink
Merge pull request #71 from github/allow-opt-out-of-focus-management
Browse files Browse the repository at this point in the history
allow opt out of focus management
  • Loading branch information
keithamus authored Jul 17, 2023
2 parents ab679fc + 3e4a515 commit e343a0b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ class MarkdownToolbarElement extends HTMLElement {

function onToolbarFocus({target}: FocusEvent) {
if (!(target instanceof Element)) return
if (target.hasAttribute('data-no-focus')) return
target.removeAttribute('tabindex')
let tabindex = '0'
for (const button of getButtons(target)) {
Expand All @@ -319,6 +320,7 @@ function focusKeydown(event: KeyboardEvent) {
if (key !== 'ArrowRight' && key !== 'ArrowLeft' && key !== 'Home' && key !== 'End') return
const toolbar = event.currentTarget
if (!(toolbar instanceof HTMLElement)) return
if (toolbar.hasAttribute('data-no-focus')) return
const buttons = getButtons(toolbar)
const index = buttons.indexOf(event.target as HTMLElement)
const length = buttons.length
Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ describe('markdown-toolbar-element', function () {
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
})

it('does not move focus if `data-no-focus` is present', function () {
document.querySelector('markdown-toolbar').setAttribute('data-no-focus', '')
focusFirstButton()
pushKeyOnFocussedButton('ArrowRight')
assert.deepEqual(getElementsWithTabindex(0), [document.querySelector('md-bold')])
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
})

it('cycles focus round to last element from first when ArrowLeft is pressed', function () {
focusFirstButton()
pushKeyOnFocussedButton('ArrowLeft')
Expand Down

0 comments on commit e343a0b

Please sign in to comment.