Skip to content

Commit 7901e82

Browse files
committed
refactor(cdk-experimental/accordion): moves visually hidden span to accordion trigger parent element
Updates previous change to move the visually hidden span from being a child of the accordion trigger button, to being a child of the parent wrapping the accordion trigger. This prevents it from being hidden by aria-disabled or disabled attributes when being a child of the trigger.
1 parent af971e3 commit 7901e82

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/cdk-experimental/accordion/accordion.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,25 +147,29 @@ export class CdkAccordionTrigger {
147147
constructor() {
148148
// We'll use afterRenderEffect to ensure the element is created after the host element.
149149
afterRenderEffect(() => {
150-
// Find the element that holds the label text, or the button itself.
150+
// Find the button element and its parent
151151
const buttonElement = this._elementRef.nativeElement;
152+
const parentElement = this._renderer.parentNode(buttonElement);
153+
154+
if (parentElement) {
155+
// Create a new visually hidden span element to be referenced by accordionPanel
156+
const visuallyHiddenSpan = this._renderer.createElement('span');
157+
this._renderer.addClass(visuallyHiddenSpan, 'cdk-visually-hidden');
158+
this._renderer.setAttribute(visuallyHiddenSpan, 'id', this.pattern.visuallyHiddenId());
159+
160+
// Get the button's text content and set it on the span
161+
let buttonText = '';
162+
for (const node of Array.from(buttonElement.childNodes)) {
163+
if (node instanceof Node && node.nodeType === Node.TEXT_NODE) {
164+
buttonText += node.textContent?.trim() + ' ';
165+
}
166+
}
167+
const textNode = this._renderer.createText(buttonText);
168+
this._renderer.appendChild(visuallyHiddenSpan, textNode);
152169

153-
// Create a new span element
154-
const visuallyHiddenSpan = this._renderer.createElement('span');
155-
156-
// Add the cdk-visually-hidden class
157-
this._renderer.addClass(visuallyHiddenSpan, 'cdk-visually-hidden');
158-
159-
// Set the ID for aria-labelledby
160-
this._renderer.setAttribute(visuallyHiddenSpan, 'id', this.visuallyHiddenId());
161-
162-
// Get the button's text content and set it on the span
163-
const buttonText = buttonElement.textContent?.trim() || '';
164-
const textNode = this._renderer.createText(buttonText);
165-
this._renderer.appendChild(visuallyHiddenSpan, textNode);
166-
167-
// Prepend the visually hidden span to the button element
168-
this._renderer.insertBefore(buttonElement, visuallyHiddenSpan, buttonElement.firstChild);
170+
// Insert the visually hidden span before the button, as its sibling
171+
this._renderer.insertBefore(parentElement, visuallyHiddenSpan, buttonElement);
172+
}
169173
});
170174
}
171175
}

0 commit comments

Comments
 (0)