Skip to content

Commit e343a0b

Browse files
authored
Merge pull request #71 from github/allow-opt-out-of-focus-management
allow opt out of focus management
2 parents ab679fc + 3e4a515 commit e343a0b

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

Diff for: src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ class MarkdownToolbarElement extends HTMLElement {
303303

304304
function onToolbarFocus({target}: FocusEvent) {
305305
if (!(target instanceof Element)) return
306+
if (target.hasAttribute('data-no-focus')) return
306307
target.removeAttribute('tabindex')
307308
let tabindex = '0'
308309
for (const button of getButtons(target)) {
@@ -319,6 +320,7 @@ function focusKeydown(event: KeyboardEvent) {
319320
if (key !== 'ArrowRight' && key !== 'ArrowLeft' && key !== 'Home' && key !== 'End') return
320321
const toolbar = event.currentTarget
321322
if (!(toolbar instanceof HTMLElement)) return
323+
if (toolbar.hasAttribute('data-no-focus')) return
322324
const buttons = getButtons(toolbar)
323325
const index = buttons.indexOf(event.target as HTMLElement)
324326
const length = buttons.length

Diff for: test/test.js

+8
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ describe('markdown-toolbar-element', function () {
131131
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
132132
})
133133

134+
it('does not move focus if `data-no-focus` is present', function () {
135+
document.querySelector('markdown-toolbar').setAttribute('data-no-focus', '')
136+
focusFirstButton()
137+
pushKeyOnFocussedButton('ArrowRight')
138+
assert.deepEqual(getElementsWithTabindex(0), [document.querySelector('md-bold')])
139+
assert.deepEqual(getElementsWithTabindex(0), [document.activeElement])
140+
})
141+
134142
it('cycles focus round to last element from first when ArrowLeft is pressed', function () {
135143
focusFirstButton()
136144
pushKeyOnFocussedButton('ArrowLeft')

0 commit comments

Comments
 (0)