Skip to content

Commit 72c0c73

Browse files
committed
feat: use data-md-button to select for focus management
This allows non-MarkdownButtonElement instances to be part of the focus management cycle
1 parent 987b5a2 commit 72c0c73

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ function keydown(fn: KeyboardEventHandler): KeyboardEventHandler {
99
}
1010
if (key === 'ArrowRight' || key === 'ArrowLeft' || key === 'Home' || key === 'End') {
1111
const target = event.currentTarget
12-
if (!(target instanceof MarkdownButtonElement)) return
12+
if (!(target instanceof HTMLElement)) return
13+
if (!target.hasAttribute('data-md-button')) return
1314
const toolbar = target.closest('markdown-toolbar')
1415
if (!(toolbar instanceof MarkdownToolbarElement)) return
1516

1617
const buttons = []
17-
for (const button of toolbar.children) {
18-
if (!(button instanceof MarkdownButtonElement)) continue
18+
for (const button of toolbar.querySelectorAll('[data-md-button]')) {
1919
button.setAttribute('tabindex', '-1')
2020
buttons.push(button)
2121
}
@@ -54,6 +54,7 @@ class MarkdownButtonElement extends HTMLElement {
5454
if (!this.hasAttribute('role')) {
5555
this.setAttribute('role', 'button')
5656
}
57+
this.setAttribute('data-md-button', '')
5758
}
5859

5960
click() {
@@ -252,7 +253,7 @@ class MarkdownToolbarElement extends HTMLElement {
252253
this.field.addEventListener('keydown', fn)
253254
shortcutListeners.set(this, fn)
254255
}
255-
const firstTabIndex = document.querySelector('[role="button"][tabindex]')
256+
const firstTabIndex = document.querySelector('[data-md-button]')
256257
if (firstTabIndex) firstTabIndex.setAttribute('tabindex', '0')
257258
}
258259

0 commit comments

Comments
 (0)