Skip to main content

Breaking: Collapse is rebuilt on native details/summary

@trackunit/react-components (from 8.0.0) rebuilds Collapse on the browser's native <details> and <summary> elements. The open and close animation is now CSS-only, and keyboard handling, find-in-page and screen-reader behaviour come from the browser. Find-in-page opens a closed section that contains the match.

This is a breaking change for code that depends on the old header markup, on onToggle, or on interactive controls inside headerAddon. A migration updates what it can and lists the rest for manual review.

Why this changed​

The previous Collapse measured its content height in JavaScript and wired up role="button", aria-expanded and aria-controls by hand. A native <details> does all of that, so Collapse now uses it and the browser handles the behaviour.

When it affects you​

Nothing changes until you bump @trackunit/react-components to 8.0.0 or later. At that version:

  • onToggle has a new event type. It changed from MouseEvent<HTMLDivElement> to SyntheticEvent<HTMLElement>. In uncontrolled mode it fires from the native toggle event, after the click. It also fires for browser-initiated toggles, such as find-in-page and name groups.
  • animate={false} only turns the animation off. Children stay mounted while the section is closed. Pass the new lazy prop to render children only while the section is open, as animate={false} used to. Lazy children mount a moment after the click that opens the section, so a test that checks them right after the click has to wait for them, for example with findBy….
  • The header is a native <summary>. It no longer has role="button", aria-expanded or aria-controls. Tests that find the header by role or read aria-expanded should use the -header and -details test ids.
  • headerAddon must be non-interactive. Clicks on interactive elements inside it no longer toggle the section, and stopPropagation() in an addon no longer prevents the toggle. Move interactive controls into the content, for example a toolbar row at the top.
// before
<Collapse animate={false} label="Details" onToggle={(event: MouseEvent<HTMLDivElement>, expanded) => track(expanded)}>
<ExpensiveContent />
</Collapse>

// after
<Collapse animate={false} lazy label="Details" onToggle={(event: SyntheticEvent<HTMLElement>, expanded) => track(expanded)}>
<ExpensiveContent />
</Collapse>

Less likely to affect you, and not breaking changes in the semver sense:

  • jest-dom's toBeVisible() is now false for collapsed content. It used to pass in jsdom, which doesn't apply the old display: none class.
  • The header is a tab stop.
  • The Collapse container no longer clips overflowing content. The content wrapper still clips its own.
  • The internal DOM changed: the content id sits on a grid item inside <details>.

What the migration does​

The package includes the v8-0-0-collapse-native-details migration.

  • retypes onToggle handlers from MouseEvent<HTMLDivElement> to SyntheticEvent<HTMLElement>
  • adds lazy next to animate={false}
  • reports for manual review: Collapse elements that get their props from a spread, headerAddons that contain interactive controls, and tests that query the old role="button" header

Run these two commands in the consuming repo after bumping the dependency:

npx nx g @trackunit/migrations:migrate
npx nx g @trackunit/migrations:run-migrations

The first 8.0.x releases registered this migration under the wrong version, so upgrading from 7.x to them skipped it. Upgrade straight to the latest 8.x release to get it. If you already upgraded to an earlier 8.0.x release, a later upgrade won't run it either: update your Collapse usages by hand using the list above.

For the complete update process, see Maintaining Your Application.