Skip to content

Commit 0895f50

Browse files
fix(accordion): removing auto focus on expanded in the header (#2458)
* fix(accordion): removing auto focus on expanded in the header * chore: adding changeset * fix(accordion): adding focus attribute to expand function * fix(accordion): reverting focus back to false for expanded * refactor: don't add new param to public method * docs: update changeset --------- Co-authored-by: Benny Powers <[email protected]> Co-authored-by: Benny Powers <[email protected]>
1 parent 8f4aaf5 commit 0895f50

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

.changeset/stale-boxes-watch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@patternfly/elements": patch
3+
---
4+
5+
`<pf-accordion>`: prevented `expanded` accordion headers from stealing focus when the page loads.
6+

elements/pf-accordion/BaseAccordion.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,15 @@ export abstract class BaseAccordion extends LitElement {
116116

117117
async firstUpdated() {
118118
const { headers } = this;
119-
for (const header of headers.filter(x => x.expanded)) {
120-
await this.expand(headers.indexOf(header));
121-
}
119+
headers.forEach((header, index) => {
120+
if (header.expanded) {
121+
this.#expandHeader(header, index);
122+
const panel = this.#panelForHeader(header);
123+
if (panel) {
124+
this.#expandPanel(panel);
125+
}
126+
}
127+
});
122128
}
123129

124130
/**
@@ -350,6 +356,7 @@ export abstract class BaseAccordion extends LitElement {
350356

351357
/**
352358
* Accepts a 0-based index value (integer) for the set of accordion items to expand.
359+
* Accepts an optional parent accordion to search for headers and panels.
353360
*/
354361
public async expand(index: number, parentAccordion?: BaseAccordion) {
355362
if (index === -1) {

elements/pf-accordion/pf-accordion.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ export class PfAccordion extends BaseAccordion {
4444

4545
@property({ type: Boolean, reflect: true }) fixed = false;
4646

47+
async firstUpdated() {
48+
let index: number | null = null;
49+
if (this.single) {
50+
const allHeaders = [...this.querySelectorAll('pf-accordion-header')];
51+
const lastExpanded = allHeaders.filter(x => x.hasAttribute('expanded')).pop();
52+
if (lastExpanded) {
53+
index = allHeaders.indexOf(lastExpanded);
54+
}
55+
}
56+
await super.firstUpdated();
57+
if (index !== null) {
58+
this.headers.forEach((_, i) => {
59+
this.headers.at(i)?.toggleAttribute('expanded', i === index);
60+
this.panels.at(i)?.toggleAttribute('expanded', i === index);
61+
});
62+
}
63+
}
64+
4765
override async expand(index: number, parentAccordion?: BaseAccordion) {
4866
if (index === -1) {
4967
return;
@@ -57,6 +75,7 @@ export class PfAccordion extends BaseAccordion {
5775
...allHeaders.map((header, index) => header.expanded && this.collapse(index)),
5876
]);
5977
}
78+
6079
await super.expand(index, parentAccordion);
6180
}
6281
}

elements/pf-accordion/test/pf-accordion.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ describe('<pf-accordion>', function() {
954954
[header, secondHeader] = headers;
955955
[panel, secondPanel] = panels;
956956
await allUpdates(element);
957-
await nextFrame();
957+
await aTimeout(100);
958958
});
959959
it('hides the first panel', function() {
960960
expect(panel).to.not.have.attribute('expanded');
@@ -966,6 +966,7 @@ describe('<pf-accordion>', function() {
966966
expect(panels[2]).to.not.have.attribute('expanded');
967967
});
968968
});
969+
969970
describe('with no h* tag in heading lightdom', function() {
970971
beforeEach(async function() {
971972
element = await createFixture<PfAccordion>(html`
@@ -1088,4 +1089,3 @@ describe('<pf-accordion>', function() {
10881089
});
10891090
});
10901091
});
1091-

0 commit comments

Comments
 (0)