Skip to content

Don't set a tabindex=0 when the toolbar has data-no-focus attribute applied #73

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 5 commits into from
Sep 22, 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
33 changes: 27 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,20 +319,43 @@ function applyFromToolbar(event: Event) {
applyStyle(target, style)
}

function setFocusManagement(toolbar: MarkdownToolbarElement) {
toolbar.addEventListener('keydown', focusKeydown)
toolbar.setAttribute('tabindex', '0')
toolbar.addEventListener('focus', onToolbarFocus, {once: true})
}

function unsetFocusManagement(toolbar: MarkdownToolbarElement) {
toolbar.removeEventListener('keydown', focusKeydown)
toolbar.removeAttribute('tabindex')
toolbar.removeEventListener('focus', onToolbarFocus)
}

class MarkdownToolbarElement extends HTMLElement {
static observedAttributes = ['data-no-focus']

connectedCallback(): void {
if (!this.hasAttribute('role')) {
this.setAttribute('role', 'toolbar')
}
this.addEventListener('keydown', focusKeydown)
this.setAttribute('tabindex', '0')
this.addEventListener('focus', onToolbarFocus, {once: true})
if (!this.hasAttribute('data-no-focus')) {
setFocusManagement(this)
}
this.addEventListener('keydown', keydown(applyFromToolbar))
this.addEventListener('click', applyFromToolbar)
}

attributeChangedCallback(name: string, oldValue: string, newValue: string): void {
if (name !== 'data-no-focus') return
if (newValue === null) {
setFocusManagement(this)
} else {
unsetFocusManagement(this)
}
}

disconnectedCallback(): void {
this.removeEventListener('keydown', focusKeydown)
unsetFocusManagement(this)
}

get field(): HTMLTextAreaElement | null {
Expand All @@ -349,7 +372,6 @@ 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 @@ -366,7 +388,6 @@ 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this line need to be removed? I see it prevents arrow keys from functioning when the toolbar has the attribute.

const buttons = getButtons(toolbar)
const index = buttons.indexOf(event.target as HTMLElement)
const length = buttons.length
Expand Down
8 changes: 2 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('markdown-toolbar-element', function () {

describe('focus management', function () {
function focusFirstButton() {
document.querySelector('markdown-toolbar').focus()
const button = document.querySelector('md-bold')
button.focus()
}
Expand All @@ -111,10 +112,6 @@ describe('markdown-toolbar-element', function () {
return [...document.querySelectorAll(`markdown-toolbar [tabindex="${index}"]`)]
}

beforeEach(() => {
document.querySelector('markdown-toolbar').focus()
})

it('moves focus to next button when ArrowRight is pressed', function () {
focusFirstButton()
pushKeyOnFocussedButton('ArrowRight')
Expand All @@ -135,8 +132,7 @@ describe('markdown-toolbar-element', 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])
assert.lengthOf(getElementsWithTabindex(0), 0)
})

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