Skip to content

Commit

Permalink
feat: move header element to light DOM to support aria-labelledby
Browse files Browse the repository at this point in the history
Close #7
  • Loading branch information
paodb committed Oct 28, 2024
1 parent 8513f46 commit cb9ef60
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/vcf-anchor-nav-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ class AnchorNavSectionElement extends ElementMixin(ThemableMixin(PolymerElement)
</style>
<slot id="tabSlot" name="tab"></slot>
<div id="header" part="header">
<slot name="header">
<h2 id="defaultHeader">[[name]]</h2>
</slot>
<slot name="header"></slot>
</div>
<div id="content" part="content">
<slot></slot>
Expand Down Expand Up @@ -104,8 +102,10 @@ class AnchorNavSectionElement extends ElementMixin(ThemableMixin(PolymerElement)

ready() {
super.ready();
this._createHeader();
this.setAttribute('tabindex', '-1');
this.setAttribute('role', 'region');
this.setAttribute('aria-labelledby', this.headerId);
this.$.tabSlot.addEventListener('slotchange', e => this._onTabSlotChange(e));
this.addEventListener('focus', e => {
if (AnchorNavSectionElement.isSame(e.target)) {
Expand All @@ -114,6 +114,19 @@ class AnchorNavSectionElement extends ElementMixin(ThemableMixin(PolymerElement)
});
}

_createHeader() {
const customHeader = this.querySelector('h2');
if (customHeader && customHeader.assignedSlot && customHeader.assignedSlot.name == 'header') {
customHeader.setAttribute('id', this.headerId);
} else {
const header = document.createElement('h2');
header.setAttribute('slot', 'header');
header.setAttribute('id', this.headerId);
header.textContent = this.name;
this.appendChild(header);
}
}

get nav() {
return this.parentElement && (this.parentElement.tagName === 'VCF-ANCHOR-NAV' ? this.parentElement : null);
}
Expand Down Expand Up @@ -207,6 +220,10 @@ class AnchorNavSectionElement extends ElementMixin(ThemableMixin(PolymerElement)
_setDefaultId() {
if (!this.id) this.id = this.defaultId;
}

get headerId() {
return `header-${this.sectionIndex + 1}`;
}
}

customElements.define(AnchorNavSectionElement.is, AnchorNavSectionElement);

0 comments on commit cb9ef60

Please sign in to comment.