Skip to content

Commit 8c65abb

Browse files
committed
Delegate aria-description & aria-label
If the root `<tab-container>` element is given aria-label or aria-description this often results in a slightly weird accessibility tree because the root container is a generic role. The label/description _should_ be on the tablist role. This makes that happen by detecting the aria-* attributes on tablist and moving them appropriately.
1 parent 0779b48 commit 8c65abb

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

src/tab-container-element.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,15 @@ export class TabContainerElement extends HTMLElement {
6666
}
6767
}
6868

69+
static observedAttributes = ['vertical']
70+
6971
get #tabList() {
70-
return this.querySelector<HTMLElement>('[role=tablist]')
72+
const slot = this.#tabListSlot
73+
if (this.#tabListSlot.hasAttribute('role')) {
74+
return slot
75+
} else {
76+
return slot.assignedNodes()[0] as HTMLElement
77+
}
7178
}
7279

7380
get #tabListSlot() {
@@ -160,7 +167,17 @@ export class TabContainerElement extends HTMLElement {
160167
this.#tabListSlot.assign(...[...this.children].filter(e => e.matches('[role=tab]')))
161168
this.#tabListSlot.role = 'tablist'
162169
}
170+
const tabList = this.#tabList
171+
if (this.hasAttribute('aria-description')) {
172+
tabList.setAttribute('aria-description', this.getAttribute('aria-description')!)
173+
this.removeAttribute('aria-description')
174+
}
175+
if (this.hasAttribute('aria-label')) {
176+
tabList.setAttribute('aria-label', this.getAttribute('aria-label')!)
177+
this.removeAttribute('aria-label')
178+
}
163179
}
180+
164181
const tabs = this.#tabs
165182
const panels = Array.from(this.querySelectorAll<HTMLElement>('[role="tabpanel"]')).filter(
166183
panel => panel.closest(this.tagName) === this,

0 commit comments

Comments
 (0)