Skip to content

Commit 413c771

Browse files
committed
fix(accordion): trying to prevent animation based on interaction rather than render
1 parent f84c484 commit 413c771

File tree

4 files changed

+98
-103
lines changed

4 files changed

+98
-103
lines changed

core/src/components/accordion-group/accordion-group-interface.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export interface AccordionGroupChangeEventDetail<T = any> {
22
value: T;
3-
initial?: boolean;
43
}
54

65
export interface AccordionGroupCustomEvent<T = any> extends CustomEvent {

core/src/components/accordion-group/accordion-group.tsx

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,9 @@ export class AccordionGroup implements ComponentInterface {
7373
*/
7474
@Event() ionValueChange!: EventEmitter<AccordionGroupChangeEventDetail>;
7575

76-
private hasEmittedInitialValue = false;
77-
private isUserInitiatedChange = false;
78-
7976
@Watch('value')
8077
valueChanged() {
81-
this.emitValueChange(false, this.isUserInitiatedChange);
82-
this.isUserInitiatedChange = false;
78+
this.ionValueChange.emit({ value: this.value });
8379
}
8480

8581
@Watch('disabled')
@@ -164,12 +160,11 @@ export class AccordionGroup implements ComponentInterface {
164160
* it is possible for the value to be set after the Web Component
165161
* initializes but before the value watcher is set up in Stencil.
166162
* As a result, the watcher callback may not be fired.
167-
* We work around this by manually emitting a value change when the component
168-
* has loaded and the watcher is configured.
163+
* We work around this by manually calling the watcher
164+
* callback when the component has loaded and the watcher
165+
* is configured.
169166
*/
170-
if (!this.hasEmittedInitialValue) {
171-
this.emitValueChange(true);
172-
}
167+
this.valueChanged();
173168
}
174169

175170
/**
@@ -181,7 +176,6 @@ export class AccordionGroup implements ComponentInterface {
181176
* accordion group to an array.
182177
*/
183178
private setValue(accordionValue: string | string[] | null | undefined) {
184-
this.isUserInitiatedChange = true;
185179
const value = (this.value = accordionValue);
186180
this.ionChange.emit({ value });
187181
}
@@ -258,40 +252,6 @@ export class AccordionGroup implements ComponentInterface {
258252
return Array.from(this.el.querySelectorAll(':scope > ion-accordion')) as HTMLIonAccordionElement[];
259253
}
260254

261-
private emitValueChange(initial: boolean, isUserInitiated = false) {
262-
const { value, multiple } = this;
263-
264-
if (!multiple && Array.isArray(value)) {
265-
/**
266-
* We do some processing on the `value` array so
267-
* that it looks more like an array when logged to
268-
* the console.
269-
* Example given ['a', 'b']
270-
* Default toString() behavior: a,b
271-
* Custom behavior: ['a', 'b']
272-
*/
273-
printIonWarning(
274-
`[ion-accordion-group] - An array of values was passed, but multiple is "false". This is incorrect usage and may result in unexpected behaviors. To dismiss this warning, pass a string to the "value" property when multiple="false".
275-
276-
Value Passed: [${value.map((v) => `'${v}'`).join(', ')}]
277-
`,
278-
this.el
279-
);
280-
}
281-
282-
/**
283-
* Track if this is the initial value update so accordions
284-
* can skip transition animations when they first render.
285-
*/
286-
const shouldMarkInitial = initial || (!this.hasEmittedInitialValue && value !== undefined && !isUserInitiated);
287-
288-
this.ionValueChange.emit({ value, initial: shouldMarkInitial });
289-
290-
if (value !== undefined) {
291-
this.hasEmittedInitialValue = true;
292-
}
293-
}
294-
295255
render() {
296256
const { disabled, readonly, expand } = this;
297257
const mode = getIonMode(this);

core/src/components/accordion/accordion.tsx

Lines changed: 87 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,38 @@ const enum AccordionState {
4040
export class Accordion implements ComponentInterface {
4141
private accordionGroupEl?: HTMLIonAccordionGroupElement | null;
4242
private updateListener = (ev: CustomEvent<AccordionGroupChangeEventDetail>) => {
43-
const initialUpdate = ev.detail?.initial ?? false;
44-
this.updateState(initialUpdate);
43+
/**
44+
* Determine if this update will cause an actual state change.
45+
* We only want to mark as "interacted" if the state is changing.
46+
*/
47+
const accordionGroup = this.accordionGroupEl;
48+
if (accordionGroup) {
49+
const value = accordionGroup.value;
50+
const accordionValue = this.value;
51+
const shouldExpand = Array.isArray(value) ? value.includes(accordionValue) : value === accordionValue;
52+
const isExpanded = this.state === AccordionState.Expanded || this.state === AccordionState.Expanding;
53+
const stateWillChange = shouldExpand !== isExpanded;
54+
55+
/**
56+
* Only mark as interacted if:
57+
* 1. This is not the first update we've received with a defined value
58+
* 2. The state is actually changing (prevents redundant updates from enabling animations)
59+
*/
60+
if (this.hasReceivedFirstUpdate && stateWillChange) {
61+
this.hasInteracted = true;
62+
}
63+
64+
/**
65+
* Only count this as the first update if the group value is defined.
66+
* This prevents the initial undefined value from the group's componentDidLoad
67+
* from being treated as the first real update.
68+
*/
69+
if (value !== undefined) {
70+
this.hasReceivedFirstUpdate = true;
71+
}
72+
}
73+
74+
this.updateState();
4575
};
4676
private contentEl: HTMLDivElement | undefined;
4777
private contentElWrapper: HTMLDivElement | undefined;
@@ -55,12 +85,24 @@ export class Accordion implements ComponentInterface {
5585
@State() isNext = false;
5686
@State() isPrevious = false;
5787
/**
58-
* Tracks whether the component has completed its initial render.
59-
* Animations are disabled until after the first render completes.
60-
* This prevents the accordion from animating when it starts
61-
* expanded or collapsed on initial load.
88+
* Tracks whether a user-initiated interaction has occurred.
89+
* Animations are disabled until the first interaction happens.
90+
* This prevents the accordion from animating when it's programmatically
91+
* set to an expanded or collapsed state on initial load.
6292
*/
63-
@State() hasRendered = false;
93+
@State() hasInteracted = false;
94+
95+
/**
96+
* Tracks if this accordion has ever been expanded.
97+
* Used to prevent the first expansion from animating.
98+
*/
99+
private hasEverBeenExpanded = false;
100+
101+
/**
102+
* Tracks if this accordion has received its first update from the group.
103+
* Used to distinguish initial programmatic sets from user interactions.
104+
*/
105+
private hasReceivedFirstUpdate = false;
64106

65107
/**
66108
* The value of the accordion. Defaults to an autogenerated
@@ -99,7 +141,7 @@ export class Accordion implements ComponentInterface {
99141
connectedCallback() {
100142
const accordionGroupEl = (this.accordionGroupEl = this.el?.closest('ion-accordion-group'));
101143
if (accordionGroupEl) {
102-
this.updateState(true);
144+
this.updateState();
103145
addEventListener(accordionGroupEl, 'ionValueChange', this.updateListener);
104146
}
105147
}
@@ -130,18 +172,6 @@ export class Accordion implements ComponentInterface {
130172
});
131173
}
132174

133-
componentDidRender() {
134-
/**
135-
* After the first render completes, mark that we've rendered.
136-
* Setting this state property triggers a re-render, at which point
137-
* animations will be enabled. This ensures animations are disabled
138-
* only for the initial render, avoiding unwanted animations on load.
139-
*/
140-
if (!this.hasRendered) {
141-
this.hasRendered = true;
142-
}
143-
}
144-
145175
private setItemDefaults = () => {
146176
const ionItem = this.getSlottedHeaderIonItem();
147177
if (!ionItem) {
@@ -235,10 +265,16 @@ export class Accordion implements ComponentInterface {
235265
ionItem.appendChild(iconEl);
236266
};
237267

238-
private expandAccordion = (initialUpdate = false) => {
268+
private expandAccordion = () => {
239269
const { contentEl, contentElWrapper } = this;
240-
if (initialUpdate || contentEl === undefined || contentElWrapper === undefined) {
270+
271+
/**
272+
* If the content elements aren't available yet, just set the state.
273+
* This happens on initial render before the DOM is ready.
274+
*/
275+
if (contentEl === undefined || contentElWrapper === undefined) {
241276
this.state = AccordionState.Expanded;
277+
this.hasEverBeenExpanded = true;
242278
return;
243279
}
244280

@@ -250,6 +286,12 @@ export class Accordion implements ComponentInterface {
250286
cancelAnimationFrame(this.currentRaf);
251287
}
252288

289+
/**
290+
* Mark that this accordion has been expanded at least once.
291+
* This allows subsequent expansions to animate.
292+
*/
293+
this.hasEverBeenExpanded = true;
294+
253295
if (this.shouldAnimate()) {
254296
raf(() => {
255297
this.state = AccordionState.Expanding;
@@ -270,9 +312,14 @@ export class Accordion implements ComponentInterface {
270312
}
271313
};
272314

273-
private collapseAccordion = (initialUpdate = false) => {
315+
private collapseAccordion = () => {
274316
const { contentEl } = this;
275-
if (initialUpdate || contentEl === undefined) {
317+
318+
/**
319+
* If the content element isn't available yet, just set the state.
320+
* This happens on initial render before the DOM is ready.
321+
*/
322+
if (contentEl === undefined) {
276323
this.state = AccordionState.Collapsed;
277324
return;
278325
}
@@ -315,11 +362,15 @@ export class Accordion implements ComponentInterface {
315362
*/
316363
private shouldAnimate = () => {
317364
/**
318-
* Don't animate until after the first render cycle completes.
365+
* Don't animate until after the first user interaction.
319366
* This prevents animations on initial load when accordions
320-
* start in an expanded or collapsed state.
367+
* start in an expanded or collapsed state programmatically.
368+
*
369+
* Additionally, don't animate the very first expansion even if
370+
* hasInteracted is true. This handles edge cases like React StrictMode
371+
* where effects run twice and might incorrectly mark as interacted.
321372
*/
322-
if (!this.hasRendered) {
373+
if (!this.hasInteracted || !this.hasEverBeenExpanded) {
323374
return false;
324375
}
325376

@@ -344,7 +395,7 @@ export class Accordion implements ComponentInterface {
344395
return true;
345396
};
346397

347-
private updateState = async (initialUpdate = false) => {
398+
private updateState = async () => {
348399
const accordionGroup = this.accordionGroupEl;
349400
const accordionValue = this.value;
350401

@@ -357,10 +408,10 @@ export class Accordion implements ComponentInterface {
357408
const shouldExpand = Array.isArray(value) ? value.includes(accordionValue) : value === accordionValue;
358409

359410
if (shouldExpand) {
360-
this.expandAccordion(initialUpdate);
411+
this.expandAccordion();
361412
this.isNext = this.isPrevious = false;
362413
} else {
363-
this.collapseAccordion(initialUpdate);
414+
this.collapseAccordion();
364415

365416
/**
366417
* When using popout or inset,
@@ -418,6 +469,12 @@ export class Accordion implements ComponentInterface {
418469

419470
if (disabled || readonly) return;
420471

472+
/**
473+
* Mark that the user has interacted with the accordion.
474+
* This enables animations for all future state changes.
475+
*/
476+
this.hasInteracted = true;
477+
421478
if (accordionGroupEl) {
422479
/**
423480
* Because the accordion group may or may

core/src/components/accordion/test/accordion.spec.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,11 @@ it('should not animate when initial value is set before load', async () => {
218218
</ion-accordion>
219219
`;
220220

221-
const details: AccordionGroupChangeEventDetail[] = [];
222-
accordionGroup.addEventListener('ionValueChange', (event: CustomEvent<AccordionGroupChangeEventDetail>) => {
223-
details.push(event.detail);
224-
});
225-
226221
accordionGroup.value = 'first';
227222
page.body.appendChild(accordionGroup);
228223

229224
await page.waitForChanges();
230225

231-
expect(details[0]?.initial).toBe(true);
232-
233226
const firstAccordion = accordionGroup.querySelector('ion-accordion[value="first"]')!;
234227

235228
expect(firstAccordion.classList.contains('accordion-expanded')).toEqual(true);
@@ -253,27 +246,19 @@ it('should not animate when initial value is set after load', async () => {
253246
</ion-accordion>
254247
`;
255248

256-
const details: AccordionGroupChangeEventDetail[] = [];
257-
accordionGroup.addEventListener('ionValueChange', (event: CustomEvent<AccordionGroupChangeEventDetail>) => {
258-
details.push(event.detail);
259-
});
260-
261249
page.body.appendChild(accordionGroup);
262250
await page.waitForChanges();
263251

264252
accordionGroup.value = 'first';
265253
await page.waitForChanges();
266254

267-
const firstDetail = details.find((detail) => detail.value === 'first');
268-
expect(firstDetail?.initial).toBe(true);
269-
270255
const firstAccordion = accordionGroup.querySelector('ion-accordion[value="first"]')!;
271256

272257
expect(firstAccordion.classList.contains('accordion-expanded')).toEqual(true);
273258
expect(firstAccordion.classList.contains('accordion-expanding')).toEqual(false);
274259
});
275260

276-
it('should animate when accordion is first opened by user', async () => {
261+
it('should not have animated class on first expansion', async () => {
277262
const page = await newSpecPage({
278263
components: [Item, Accordion, AccordionGroup],
279264
html: `
@@ -287,20 +272,14 @@ it('should animate when accordion is first opened by user', async () => {
287272
});
288273

289274
const accordionGroup = page.body.querySelector('ion-accordion-group')!;
275+
const firstAccordion = page.body.querySelector('ion-accordion[value="first"]')!;
290276

291-
const details: AccordionGroupChangeEventDetail[] = [];
292-
accordionGroup.addEventListener('ionValueChange', (event: CustomEvent<AccordionGroupChangeEventDetail>) => {
293-
details.push(event.detail);
294-
});
295-
296-
await accordionGroup.requestAccordionToggle('first', true);
277+
// First expansion should not have the animated class
278+
accordionGroup.value = 'first';
297279
await page.waitForChanges();
298280

299-
const lastDetail = details[details.length - 1];
300-
expect(lastDetail?.initial).toBe(false);
301-
302-
const firstAccordion = accordionGroup.querySelector('ion-accordion[value="first"]')!;
303-
expect(firstAccordion.classList.contains('accordion-animated')).toEqual(true);
281+
expect(firstAccordion.classList.contains('accordion-animated')).toEqual(false);
282+
expect(firstAccordion.classList.contains('accordion-expanded')).toEqual(true);
304283
});
305284

306285
// Verifies fix for https://github.com/ionic-team/ionic-framework/issues/27047

0 commit comments

Comments
 (0)