From 97e7c962c4bf31edc57d97f5cacd673b34219ff8 Mon Sep 17 00:00:00 2001 From: gjulivan Date: Mon, 16 Nov 2026 22:26:20 +0000 Subject: [PATCH] fix(accordion-web): animate groups with grid rows instead of measured height Group content was animated by measuring it with getBoundingClientRect and writing an inline height, kept up to date by a debounced ResizeObserver. The height lagged behind content changes, so content was cut off for a few frames, and groups that load content when expanded opened empty and jumped. A group toggled while hidden (e.g. in an inactive tab) never got transitionend and stayed stuck at 0px. The group is now a grid whose content row transitions between 0fr and 1fr, so the browser sizes the content at all times. The content wrapper clips during the animation and is visibility: hidden when collapsed. The DOM and class names are unchanged, so Atlas and custom theme selectors still match. - transitionend only completes on the group's own grid-template-rows - a fallback timer completes the toggle when transitionend does not fire; with no transition (reduced motion, theme override) it completes at once - "load when expanded" content renders at the start of the expand - prefers-reduced-motion disables the transition - remove the ResizeObserver hook Co-Authored-By: Claude Opus 5.5 --- .../accordion-web/CHANGELOG.md | 4 + .../src/components/AccordionGroup.tsx | 99 +++++++----- .../components/__tests__/Accordion.spec.tsx | 6 - .../__tests__/AccordionGroup.spec.tsx | 145 +++++++++++++++++- .../accordion-web/src/ui/accordion-main.scss | 33 +++- .../accordion-web/src/utils/resizeObserver.ts | 36 ----- 6 files changed, 233 insertions(+), 90 deletions(-) delete mode 100644 packages/pluggableWidgets/accordion-web/src/utils/resizeObserver.ts diff --git a/packages/pluggableWidgets/accordion-web/CHANGELOG.md b/packages/pluggableWidgets/accordion-web/CHANGELOG.md index a902336419..9474901640 100644 --- a/packages/pluggableWidgets/accordion-web/CHANGELOG.md +++ b/packages/pluggableWidgets/accordion-web/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +### Fixed + +- We fixed an issue where the content of an accordion group could be cut off or jump when its height changed, for example when validation messages appeared. Groups set to load content when expanded now also show their content from the start of the expand animation. + ## [2.3.6] - 2026-09-15 ### Fixed diff --git a/packages/pluggableWidgets/accordion-web/src/components/AccordionGroup.tsx b/packages/pluggableWidgets/accordion-web/src/components/AccordionGroup.tsx index 1162dcb96e..ffa6b2fc86 100644 --- a/packages/pluggableWidgets/accordion-web/src/components/AccordionGroup.tsx +++ b/packages/pluggableWidgets/accordion-web/src/components/AccordionGroup.tsx @@ -1,7 +1,16 @@ import classNames from "classnames"; -import { Key, KeyboardEvent, ReactElement, ReactNode, useCallback, useEffect, useRef, useState } from "react"; +import { + Key, + KeyboardEvent, + ReactElement, + ReactNode, + TransitionEvent, + useCallback, + useEffect, + useRef, + useState +} from "react"; import { LoadContentEnum } from "typings/AccordionProps"; -import { CallResizeObserver, useDebouncedResizeObserver } from "../utils/resizeObserver"; import "../ui/accordion-main.scss"; export const enum Target { @@ -37,60 +46,65 @@ export function AccordionGroup(props: AccordionGroupProps): ReactElement | null const [renderCollapsed, setRenderCollapsed] = useState(props.collapsed); const previousRenderCollapsed = useRef(renderCollapsed); const animatingContent = useRef(false); + const fallbackTimerRef = useRef>(undefined); const rootRef = useRef(null); - const contentWrapperRef = useRef(null); - const contentRef = useRef(null); const renderContent = useRef(loadContent === "always"); - renderContent.current ||= !renderCollapsed; - useDebouncedResizeObserver(CallResizeObserver, { renderCollapsed, contentWrapperRef, contentRef }); + // Render content as soon as expanding starts, so it is present during the whole animation. + renderContent.current ||= !props.collapsed || !renderCollapsed; + + const clearFallbackTimer = useCallback((): void => { + clearTimeout(fallbackTimerRef.current); + fallbackTimerRef.current = undefined; + }, []); + + useEffect(() => clearFallbackTimer, [clearFallbackTimer]); const completeTransitioning = useCallback((): void => { - if (contentWrapperRef.current && rootRef.current && animatingContent.current) { + if (rootRef.current && animatingContent.current) { + clearFallbackTimer(); if (!renderCollapsed) { rootRef.current.classList.add("widget-accordion-group-collapsed"); rootRef.current.classList.remove("widget-accordion-group-collapsing"); setRenderCollapsed(true); } else { rootRef.current.classList.remove("widget-accordion-group-expanding"); - contentWrapperRef.current.style.height = ""; setRenderCollapsed(false); } animatingContent.current = false; } - }, [renderCollapsed]); + }, [renderCollapsed, clearFallbackTimer]); + + const onTransitionEnd = useCallback( + (event: TransitionEvent): void => { + // Ignore transitions bubbling up from the header icon or the content. + if (event.target === event.currentTarget && event.propertyName === "grid-template-rows") { + completeTransitioning(); + } + }, + [completeTransitioning] + ); useEffect(() => { - if ( - props.collapsed !== renderCollapsed && - rootRef.current && - contentWrapperRef.current && - contentRef.current && - animateContent - ) { + if (props.collapsed !== renderCollapsed && rootRef.current && animateContent) { animatingContent.current = true; if (props.collapsed) { - contentWrapperRef.current.style.height = `${contentRef.current.getBoundingClientRect().height}px`; rootRef.current.classList.add("widget-accordion-group-collapsing"); - - setTimeout(() => { - if (contentWrapperRef.current) { - contentWrapperRef.current.style.height = ""; - } - }, 50); } else { rootRef.current.classList.add("widget-accordion-group-expanding"); rootRef.current.classList.remove("widget-accordion-group-collapsed"); + } - setTimeout(() => { - if (contentWrapperRef.current && contentRef.current) { - contentWrapperRef.current.style.height = `${ - contentRef.current.getBoundingClientRect().height - }px`; - } - }, 50); + // transitionend does not fire when there is no transition (prefers-reduced-motion, a theme + // override, an unsupported browser) or when the group is not rendered (for example in a hidden tab). + const transitionTime = getTransitionTime(rootRef.current); + clearFallbackTimer(); + if (transitionTime === 0) { + completeTransitioning(); + } else { + fallbackTimerRef.current = setTimeout(completeTransitioning, transitionTime + 100); } } else if (props.collapsed !== renderCollapsed && (!animateContent || !props.visible)) { setRenderCollapsed(props.collapsed); @@ -104,7 +118,7 @@ export function AccordionGroup(props: AccordionGroupProps): ReactElement | null completeTransitioning(); } } - }, [props.collapsed, props.visible, renderCollapsed, animateContent, completeTransitioning]); + }, [props.collapsed, props.visible, renderCollapsed, animateContent, completeTransitioning, clearFallbackTimer]); useEffect(() => { if (renderCollapsed !== previousRenderCollapsed.current) { @@ -159,6 +173,7 @@ export function AccordionGroup(props: AccordionGroupProps): ReactElement | null }, props.dynamicClassName )} + onTransitionEnd={onTransitionEnd} >
-
- {renderContent.current && props.content} -
+
{renderContent.current && props.content}
); } + +/** Longest transition (duration + delay) in milliseconds currently applied to the element. */ +function getTransitionTime(element: HTMLElement): number { + const style = window.getComputedStyle(element); + const toMs = (value: string): number[] => + value.split(",").map(time => { + const parsed = parseFloat(time); + if (Number.isNaN(parsed)) { + return 0; + } + return time.trim().endsWith("ms") ? parsed : parsed * 1000; + }); + const durations = toMs(style.transitionDuration); + const delays = toMs(style.transitionDelay); + return Math.max(0, ...durations.map((duration, i) => duration + (delays[i % delays.length] ?? 0))); +} diff --git a/packages/pluggableWidgets/accordion-web/src/components/__tests__/Accordion.spec.tsx b/packages/pluggableWidgets/accordion-web/src/components/__tests__/Accordion.spec.tsx index c3732ce8ee..717e2e701a 100644 --- a/packages/pluggableWidgets/accordion-web/src/components/__tests__/Accordion.spec.tsx +++ b/packages/pluggableWidgets/accordion-web/src/components/__tests__/Accordion.spec.tsx @@ -3,12 +3,6 @@ import { act, render, RenderResult } from "@testing-library/react"; import userEvent, { UserEvent } from "@testing-library/user-event"; import { Accordion, AccordionProps } from "../Accordion"; -global.ResizeObserver = jest.fn().mockImplementation(() => ({ - observe: jest.fn(), - unobserve: jest.fn(), - disconnect: jest.fn() -})); - describe("Accordion", () => { let defaultProps: AccordionProps; let user: UserEvent; diff --git a/packages/pluggableWidgets/accordion-web/src/components/__tests__/AccordionGroup.spec.tsx b/packages/pluggableWidgets/accordion-web/src/components/__tests__/AccordionGroup.spec.tsx index d941221ef5..b47018505a 100644 --- a/packages/pluggableWidgets/accordion-web/src/components/__tests__/AccordionGroup.spec.tsx +++ b/packages/pluggableWidgets/accordion-web/src/components/__tests__/AccordionGroup.spec.tsx @@ -1,14 +1,8 @@ import "@testing-library/jest-dom"; -import { act, render, RenderResult } from "@testing-library/react"; +import { act, createEvent, fireEvent, render, RenderResult } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { AccordionGroup, AccordionGroupProps, Target } from "../AccordionGroup"; -global.ResizeObserver = jest.fn().mockImplementation(() => ({ - observe: jest.fn(), - unobserve: jest.fn(), - disconnect: jest.fn() -})); - describe("AccordionGroup", () => { const defaultAccordionGroupProps: AccordionGroupProps = { id: "id", @@ -390,4 +384,141 @@ describe("AccordionGroup", () => { expect(accordionGroup.queryByText("Widgets")).toBeInTheDocument(); }); }); + + describe("animated", () => { + const animatedProps: AccordionGroupProps = { + ...defaultAccordionGroupProps, + animateContent: true, + collapsible: true, + toggleCollapsed: jest.fn() + }; + + function mockTransitionDuration(duration: string): void { + const original = window.getComputedStyle; + jest.spyOn(window, "getComputedStyle").mockImplementation(element => { + const style = original(element); + return { ...style, transitionDuration: duration, transitionDelay: "0s" } as CSSStyleDeclaration; + }); + } + + function getGroup(result: RenderResult): HTMLElement { + return result.container.querySelector(".widget-accordion-group") as HTMLElement; + } + + function fireTransitionEnd(element: Element, propertyName = "grid-template-rows"): void { + const event = createEvent.transitionEnd(element); + Object.defineProperty(event, "propertyName", { value: propertyName }); + fireEvent(element, event); + } + + beforeEach(() => { + jest.useFakeTimers(); + }); + + afterEach(() => { + jest.restoreAllMocks(); + jest.useRealTimers(); + }); + + it("expands with a transition and completes on transitionend", () => { + mockTransitionDuration("0.2s"); + const onToggleCompletion = jest.fn(); + const result = render(); + + result.rerender( + + ); + const group = getGroup(result); + expect(group).toHaveClass("widget-accordion-group-expanding"); + expect(group).not.toHaveClass("widget-accordion-group-collapsed"); + expect(onToggleCompletion).not.toHaveBeenCalled(); + + act(() => fireTransitionEnd(group)); + + expect(group).not.toHaveClass("widget-accordion-group-expanding"); + expect(onToggleCompletion).toHaveBeenCalledTimes(1); + expect(onToggleCompletion).toHaveBeenCalledWith(false); + + act(() => jest.runAllTimers()); + expect(onToggleCompletion).toHaveBeenCalledTimes(1); + }); + + it("collapses with a transition and completes on transitionend", () => { + mockTransitionDuration("0.2s"); + const onToggleCompletion = jest.fn(); + const result = render( + + ); + + result.rerender(); + const group = getGroup(result); + expect(group).toHaveClass("widget-accordion-group-collapsing"); + expect(group).not.toHaveClass("widget-accordion-group-collapsed"); + + act(() => fireTransitionEnd(group)); + + expect(group).toHaveClass("widget-accordion-group-collapsed"); + expect(group).not.toHaveClass("widget-accordion-group-collapsing"); + expect(onToggleCompletion).toHaveBeenCalledTimes(1); + expect(onToggleCompletion).toHaveBeenCalledWith(true); + }); + + it("ignores transitionend events from other properties and from the content", () => { + mockTransitionDuration("0.2s"); + const onToggleCompletion = jest.fn(); + const result = render(); + result.rerender( + + ); + const group = getGroup(result); + + act(() => { + fireTransitionEnd(group, "border-radius"); + fireTransitionEnd(result.getByText("content")); + }); + + expect(group).toHaveClass("widget-accordion-group-expanding"); + expect(onToggleCompletion).not.toHaveBeenCalled(); + }); + + it("completes when transitionend does not fire", () => { + mockTransitionDuration("0.2s"); + const onToggleCompletion = jest.fn(); + const result = render(); + result.rerender( + + ); + + act(() => jest.advanceTimersByTime(400)); + + expect(getGroup(result)).not.toHaveClass("widget-accordion-group-expanding"); + expect(onToggleCompletion).toHaveBeenCalledTimes(1); + }); + + it("completes immediately when there is no transition, such as with reduced motion", () => { + mockTransitionDuration("0s"); + const onToggleCompletion = jest.fn(); + const result = render( + + ); + + result.rerender(); + + const group = getGroup(result); + expect(group).toHaveClass("widget-accordion-group-collapsed"); + expect(group).not.toHaveClass("widget-accordion-group-collapsing"); + expect(onToggleCompletion).toHaveBeenCalledTimes(1); + }); + + it("renders content from the start of expanding when loading content when expanded", () => { + mockTransitionDuration("0.2s"); + const result = render(); + expect(result.queryByText("content")).not.toBeInTheDocument(); + + result.rerender(); + + expect(getGroup(result)).toHaveClass("widget-accordion-group-expanding"); + expect(result.getByText("content")).toBeInTheDocument(); + }); + }); }); diff --git a/packages/pluggableWidgets/accordion-web/src/ui/accordion-main.scss b/packages/pluggableWidgets/accordion-web/src/ui/accordion-main.scss index cd058be988..56c2f2762b 100644 --- a/packages/pluggableWidgets/accordion-web/src/ui/accordion-main.scss +++ b/packages/pluggableWidgets/accordion-web/src/ui/accordion-main.scss @@ -9,6 +9,15 @@ $header-color-hover: #264ae5; border-color: $border-color; border-width: 1px; border-bottom-style: solid; + // The content row animates between 0fr and 1fr, so the browser sizes the content at all times. + display: grid; + grid-template-columns: minmax(0, 1fr); + grid-template-rows: auto 1fr; + + > .widget-accordion-group-content-wrapper { + // Lets the row shrink to 0. Holds no padding, so nothing shows when collapsed. + min-height: 0; + } &.widget-accordion-group-collapsed { > .widget-accordion-group-header > .widget-accordion-group-header-button, @@ -20,7 +29,21 @@ $header-color-hover: #264ae5; } > .widget-accordion-group-content-wrapper { - display: none; + // Keeps collapsed content out of the tab order and hidden from screen readers. + visibility: hidden; + } + } + + &.widget-accordion-group-collapsed, + &.widget-accordion-group-collapsing { + grid-template-rows: auto 0fr; + } + + &.widget-accordion-group-collapsed, + &.widget-accordion-group-expanding, + &.widget-accordion-group-collapsing { + > .widget-accordion-group-content-wrapper { + overflow: hidden; } } @@ -41,10 +64,10 @@ $header-color-hover: #264ae5; &.widget-accordion-group-expanding, &.widget-accordion-group-collapsing { - > .widget-accordion-group-content-wrapper { - height: 0; - overflow: hidden; - transition: height 0.2s ease 50ms; + transition: grid-template-rows 0.2s ease; + + @media (prefers-reduced-motion: reduce) { + transition: none; } } diff --git a/packages/pluggableWidgets/accordion-web/src/utils/resizeObserver.ts b/packages/pluggableWidgets/accordion-web/src/utils/resizeObserver.ts deleted file mode 100644 index 41b2852c36..0000000000 --- a/packages/pluggableWidgets/accordion-web/src/utils/resizeObserver.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { RefObject, useEffect, useMemo } from "react"; -import { debounce } from "@mendix/widget-plugin-platform/utils/debounce"; -import "../ui/accordion-main.scss"; - -type ResizeObserverProps = { - renderCollapsed: boolean; - contentWrapperRef: RefObject; - contentRef: RefObject; -}; - -export function CallResizeObserver(entries: ResizeObserverEntry[], props: ResizeObserverProps): void { - const { renderCollapsed, contentWrapperRef, contentRef } = props; - for (const entry of entries) { - if (entry.contentBoxSize && !renderCollapsed && contentWrapperRef.current && contentRef.current) { - contentWrapperRef.current.style.height = `${contentRef.current.getBoundingClientRect().height}px`; - } - } -} - -export function useDebouncedResizeObserver(callback: typeof CallResizeObserver, props: ResizeObserverProps): void { - const { contentRef } = props; - const [callResizeObserverDebounce] = useMemo( - () => debounce((entries: ResizeObserverEntry[]) => callback(entries, props), 32), - [callback, props] - ); - - useEffect(() => { - const observer = new ResizeObserver(callResizeObserverDebounce); - if (contentRef.current) { - observer.observe(contentRef.current); - } - return () => { - observer.disconnect(); - }; - }, [callResizeObserverDebounce, props, contentRef]); -}