Skip to content

allow opt out of focus management #71

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

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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