Skip to content

Commit 87a3506

Browse files
committed
feat: move header element to light DOM to support aria-labelledby
Close #7
1 parent 24c6b6b commit 87a3506

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/vcf-anchor-nav-section.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ class AnchorNavSectionElement extends ElementMixin(ThemableMixin(PolymerElement)
6565
</style>
6666
<slot id="tabSlot" name="tab"></slot>
6767
<div id="header" part="header">
68-
<slot name="header">
69-
<h2 id="defaultHeader">[[name]]</h2>
70-
</slot>
68+
<slot name="header"></slot>
7169
</div>
7270
<div id="content" part="content">
7371
<slot></slot>
@@ -104,8 +102,10 @@ class AnchorNavSectionElement extends ElementMixin(ThemableMixin(PolymerElement)
104102

105103
ready() {
106104
super.ready();
105+
this._createHeader();
107106
this.setAttribute('tabindex', '-1');
108107
this.setAttribute('role', 'region');
108+
this.setAttribute('aria-labelledby', this.headerId);
109109
this.$.tabSlot.addEventListener('slotchange', e => this._onTabSlotChange(e));
110110
this.addEventListener('focus', e => {
111111
if (AnchorNavSectionElement.isSame(e.target)) {
@@ -114,6 +114,19 @@ class AnchorNavSectionElement extends ElementMixin(ThemableMixin(PolymerElement)
114114
});
115115
}
116116

117+
_createHeader() {
118+
const customHeader = this.querySelector('h2');
119+
if (customHeader && customHeader.assignedSlot && customHeader.assignedSlot.name == 'header') {
120+
customHeader.setAttribute('id', this.headerId);
121+
} else {
122+
const header = document.createElement('h2');
123+
header.setAttribute('slot', 'header');
124+
header.setAttribute('id', this.headerId);
125+
header.textContent = this.name;
126+
this.appendChild(header);
127+
}
128+
}
129+
117130
get nav() {
118131
return this.parentElement && (this.parentElement.tagName === 'VCF-ANCHOR-NAV' ? this.parentElement : null);
119132
}
@@ -207,6 +220,10 @@ class AnchorNavSectionElement extends ElementMixin(ThemableMixin(PolymerElement)
207220
_setDefaultId() {
208221
if (!this.id) this.id = this.defaultId;
209222
}
223+
224+
get headerId() {
225+
return `header-${this.sectionIndex + 1}`;
226+
}
210227
}
211228

212229
customElements.define(AnchorNavSectionElement.is, AnchorNavSectionElement);

0 commit comments

Comments
 (0)