Skip to content

Commit 7e3d22c

Browse files
committed
Add a attributeChangedCallback to header button
Even though we don't expect header buttons to change levels dynamically during an application lifetime, we should ensure that it works as expected. So I've added an `attributeChangedCallback` to the header button element. The callback changes the level style based on the `level` attribute.
1 parent b395a41 commit 7e3d22c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ class MarkdownButtonElement extends HTMLElement {
113113
class MarkdownHeaderButtonElement extends MarkdownButtonElement {
114114
connectedCallback() {
115115
const level = parseInt(this.getAttribute('level') || '3', 10)
116+
this.#setLevelStyle(level)
117+
}
118+
119+
static get observedAttributes() {
120+
return ['level']
121+
}
122+
123+
attributeChangedCallback(name: string, oldValue: string, newValue: string) {
124+
if (name !== 'level') return
125+
const level = parseInt(newValue || '3', 10)
126+
this.#setLevelStyle(level)
127+
}
128+
129+
#setLevelStyle(level: number) {
116130
if (level < 1 || level > 6) {
117131
return
118132
}

test/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,15 @@ describe('markdown-toolbar-element', function () {
860860
clickToolbar('md-header[level="10"]')
861861
assert.equal('|title|', visualValue())
862862
})
863+
864+
it('dynamically changes header level based on the level attribute', function () {
865+
setVisualValue('|title|')
866+
const headerButton = document.querySelector('md-header[level="1"]')
867+
headerButton.setAttribute('level', '2')
868+
headerButton.click()
869+
870+
assert.equal('## |title|', visualValue())
871+
})
863872
})
864873
})
865874
})

0 commit comments

Comments
 (0)