Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

refactor(Stepper): stop the width observer from the list's ref cleanup, not an Effect (#9858)

Cindy Zhang21c4026777c7
diff --git a/.changeset/stepper-root-observer-ref-cleanup.md b/.changeset/stepper-root-observer-ref-cleanup.md
new file mode 100644
index 0000000000..854d1804dd
--- /dev/null
+++ b/.changeset/stepper-root-observer-ref-cleanup.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[chore] Internal: a horizontal Stepper stops measuring its list from the cleanup that the list's ref callback returns, not from an Effect. It still measures before the first paint, and it still stops when the list detaches or the Stepper turns vertical. Nothing a consumer imports or renders changes.
+
+@cixzhang
diff --git a/packages/core/src/Stepper/Stepper.test.tsx b/packages/core/src/Stepper/Stepper.test.tsx
index 9498d1918f..63e4cb648f 100644
--- a/packages/core/src/Stepper/Stepper.test.tsx
+++ b/packages/core/src/Stepper/Stepper.test.tsx
@@ -1519,6 +1519,46 @@ describe('Stepper', () => {
},
);
+ it('measures the list only while it is mounted and horizontal', () => {
+ const observed = new Set<Element>();
+ class ResizeObserverStub {
+ observe(element: Element) {
+ observed.add(element);
+ }
+ unobserve(element: Element) {
+ observed.delete(element);
+ }
+ disconnect() {
+ observed.clear();
+ }
+ }
+ vi.stubGlobal('ResizeObserver', ResizeObserverStub);
+ const stepper = (orientation: 'horizontal' | 'vertical') => (
+ <Stepper activeStep={0} orientation={orientation}>
+ <Step step={0} label="Account" />
+ <Step step={1} label="Review" />
+ </Stepper>
+ );
+ try {
+ const view = render(stepper('horizontal'));
+ const horizontalList = screen.getByRole('list');
+ expect(observed.has(horizontalList)).toBe(true);
+
+ view.rerender(stepper('vertical'));
+ expect(observed.has(horizontalList)).toBe(false);
+ expect(observed.has(screen.getByRole('list'))).toBe(false);
+
+ view.rerender(stepper('horizontal'));
+ const list = screen.getByRole('list');
+ expect(observed.has(list)).toBe(true);
+
+ view.unmount();
+ expect(observed.has(list)).toBe(false);
+ } finally {
+ vi.unstubAllGlobals();
+ }
+ });
+
it('stops the controls at both ends of the sequence', () => {
const onStepClick = vi.fn();
// Disabled the Astryx way: aria-disabled, so the control keeps its place
diff --git a/packages/core/src/Stepper/Stepper.tsx b/packages/core/src/Stepper/Stepper.tsx
index 20a775ce27..f60d2c794b 100644
--- a/packages/core/src/Stepper/Stepper.tsx
+++ b/packages/core/src/Stepper/Stepper.tsx
@@ -23,14 +23,7 @@
* - /packages/cli/assets/templates/blocks/components/Stepper/ (showcase blocks)
*/
-import {
- useCallback,
- useEffect,
- useMemo,
- useRef,
- useState,
- type ReactNode,
-} from 'react';
+import {useCallback, useMemo, useRef, useState, type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
import {spacingVars} from '../theme/tokens.stylex';
@@ -360,32 +353,22 @@ export function Stepper({
// still owns the ref and DOM pass-throughs and fills that frame, so its width
// is the effective component width to observe.
const [rootWidth, setRootWidth] = useState(0);
- const stopObservingRootRef = useRef<(() => void) | null>(null);
const attachRoot = useCallback(
(el: HTMLOListElement | null) => {
- stopObservingRootRef.current?.();
- stopObservingRootRef.current = null;
- if (el && isHorizontal) {
- // observeResize calls back once, synchronously, as it subscribes. That
- // is what keeps the collapse off the screen: a ref callback runs during
- // commit, so the width lands and the re-render happens before the
- // browser has painted the uncollapsed stepper it would otherwise show
- // first.
- stopObservingRootRef.current = observeResize(el, entry =>
- setRootWidth(entry.target.clientWidth),
- );
+ if (el == null || !isHorizontal) {
+ return;
}
+ // observeResize calls back once, synchronously, as it subscribes. That
+ // is what keeps the collapse off the screen: a ref callback runs during
+ // commit, so the width lands and the re-render happens before the
+ // browser has painted the uncollapsed stepper it would otherwise show
+ // first. React calls the returned unsubscribe when the list detaches or
+ // when the orientation changes.
+ return observeResize(el, entry => setRootWidth(entry.target.clientWidth));
},
[isHorizontal],
);
const rootRef = useMergedRefs(ref, attachRoot);
- useEffect(
- () => () => {
- stopObservingRootRef.current?.();
- stopObservingRootRef.current = null;
- },
- [],
- );
// Width is measured rather than asked of a container query because the
// answer changes what is *rendered*, not just how it looks: the labels and