Skip to content

Commit d62bd06

Browse files
committed
set focus management
1 parent fffacb3 commit d62bd06

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/index.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,22 +319,43 @@ function applyFromToolbar(event: Event) {
319319
applyStyle(target, style)
320320
}
321321

322+
function setFocusManagement(toolbar: MarkdownToolbarElement) {
323+
toolbar.addEventListener('keydown', focusKeydown)
324+
toolbar.setAttribute('tabindex', '0')
325+
toolbar.addEventListener('focus', onToolbarFocus, {once: true})
326+
}
327+
328+
function unsetFocusManagement(toolbar: MarkdownToolbarElement) {
329+
toolbar.removeEventListener('keydown', focusKeydown)
330+
toolbar.removeAttribute('tabindex')
331+
toolbar.removeEventListener('focus', onToolbarFocus)
332+
}
333+
322334
class MarkdownToolbarElement extends HTMLElement {
335+
static observedAttributes = ['data-no-focus']
336+
323337
connectedCallback(): void {
324338
if (!this.hasAttribute('role')) {
325339
this.setAttribute('role', 'toolbar')
326340
}
327341
if (!this.hasAttribute('data-no-focus')) {
328-
this.addEventListener('keydown', focusKeydown)
329-
this.setAttribute('tabindex', '0')
330-
this.addEventListener('focus', onToolbarFocus, {once: true})
342+
setFocusManagement(this)
331343
}
332344
this.addEventListener('keydown', keydown(applyFromToolbar))
333345
this.addEventListener('click', applyFromToolbar)
334346
}
335347

348+
attributeChangedCallback(name: string, oldValue: string, newValue: string): void {
349+
if (name !== 'data-no-focus') return
350+
if (newValue === null) {
351+
setFocusManagement(this)
352+
} else {
353+
unsetFocusManagement(this)
354+
}
355+
}
356+
336357
disconnectedCallback(): void {
337-
this.removeEventListener('keydown', focusKeydown)
358+
unsetFocusManagement(this)
338359
}
339360

340361
get field(): HTMLTextAreaElement | null {

test/test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
chai.config.truncateThreshold = Infinity
2-
31
describe('markdown-toolbar-element', function () {
42
describe('element creation', function () {
53
it('creates from document.createElement', function () {

0 commit comments

Comments
 (0)