An open source design system that's fully customizable and agent ready
TypeScript
fix(Spinner): the drawn frame follows a themed diameter (#5827)
freddymeta8c9576db008a
diff --git a/.changeset/spinner-frame-follows-diameter.md b/.changeset/spinner-frame-follows-diameter.mdnew file mode 100644index 0000000000..d3362f47e2--- /dev/null+++ b/.changeset/spinner-frame-follows-diameter.md@@ -0,0 +1,30 @@+---+'@astryxdesign/core': patch+---++[fix] Spinner: the drawn frame follows a themed `--spinner-diameter`, so a+themed ring is no longer clipped or off-centre. (#5214)++`--spinner-diameter` and `--spinner-stroke-width` set the ring in CSS, and the+box the ring sits in is composed from those same two vars — but the `<svg>` was+sized and given its `viewBox` in JS, from the size's own constants. Theming the+diameter therefore left the frame behind: the svg stayed at its default while+the box shrank around it, and an overflowing grid item aligns to start rather+than centre. Measured in Chromium across the four sizes, a themed ring rendered+1.5-3.3px off-centre with its far edge cropped.++The svg is now sized in CSS from `--_spinner-box-size` — the same composed var+the span is sized from — with no `viewBox`, so one user unit is one pixel and+the frame moves with the box. (Not a percentage: the span is a grid whose area+is not always definite in both axes, and an unresolved percentage height on an+SVG falls back to the replaced-element default of 150px.) The px `width`/+`height` attributes remain as the no-stylesheet fallback, as `r` and+`stroke-width` already were. Both circles centre on+`cx`/`cy="50%"`, and the arc's twelve-o'clock offset is a CSS rotation about+the shape's own box rather than an SVG transform about a centre in user units.++No change to the default render at any size — same box, ring, stroke and+sweep, verified against a build of `main`. What changes is that the documented+claim "the rendered box … follows automatically" is now true.++@freddymetadiff --git a/apps/storybook/rtl-audit/verified-not-applicable.json b/apps/storybook/rtl-audit/verified-not-applicable.jsonindex fe51488c70..1cc979472f 100644--- a/apps/storybook/rtl-audit/verified-not-applicable.json+++ b/apps/storybook/rtl-audit/verified-not-applicable.json@@ -1 +1,6 @@-[]+[+ {+ "component": "core/Spinner",+ "reason": "No direction-sensitive visual, layout, or behavior. The ring is a concentric SVG — two <circle> elements sharing one center, sized by diameter and stroke, with no physical inset, offset, or asymmetry to mirror. Its only layout is the optional label wrapper, which is flexDirection: column + alignItems: center, so the label stacks below the ring on both axes' terms rather than beside it. The rotation keyframe (0deg to 360deg) is deliberately absolute: a loading spinner turns the same way in every locale, and mirroring it would be a bug, not RTL support."+ }+]diff --git a/packages/core/src/Spinner/Spinner.test.tsx b/packages/core/src/Spinner/Spinner.test.tsxindex 518120e3bb..40066bd657 100644--- a/packages/core/src/Spinner/Spinner.test.tsx+++ b/packages/core/src/Spinner/Spinner.test.tsx@@ -242,12 +242,16 @@ describe('Spinner ring', () => {render(<Spinner size={size as keyof typeof SIZES} data-testid="spinner" />,);+ // No viewBox: one user unit is one px, so the attributes below mean px.+ // They are the no-stylesheet fallback; CSS sizes the frame from the box.const frame = diameter + border * 2;const svg = ring();- expect(svg.getAttribute('viewBox')).toBe(`0 0 ${frame} ${frame}`);+ expect(svg.getAttribute('viewBox')).toBeNull();+ expect(Number(svg.getAttribute('width'))).toBe(frame);+ expect(Number(svg.getAttribute('height'))).toBe(frame);for (const c of [circles().track, circles().arc]) {- expect(Number(c.getAttribute('cx'))).toBe(frame / 2);- expect(Number(c.getAttribute('cy'))).toBe(frame / 2);+ expect(c.getAttribute('cx')).toBe('50%');+ expect(c.getAttribute('cy')).toBe('50%');expect(Number(c.getAttribute('r'))).toBe(diameter / 2);expect(Number(c.getAttribute('stroke-width'))).toBe(border);}@@ -269,12 +273,33 @@ describe('Spinner ring', () => {},);+ // The regression this file could not have caught: #5214 made the RING+ // themeable and left the frame at the size's own constant, so a themed+ // diameter left the svg larger than the box it sits in. The overflowing grid+ // item then aligned to start rather than centre — measured in Chromium at+ // 1.5 to 3.3px off across the four sizes. Nothing asserted the two agreed,+ // because nothing tied them.+ it('sizes the frame from CSS, not from the size constant', () => {+ render(<Spinner data-testid="spinner" />);+ const svg = ring();+ // No viewBox, and the circles centre on the box rather than on a centre+ // in user units — together that is what lets CSS size the frame from+ // `--_spinner-box-size`, the same composed var the span is sized from, so+ // a themed diameter moves both. The px attributes remain as the+ // no-stylesheet fallback and CSS outranks them whenever one is present.+ expect(svg.getAttribute('viewBox')).toBeNull();+ for (const c of [circles().track, circles().arc]) {+ expect(c.getAttribute('cx')).toBe('50%');+ expect(c.getAttribute('cy')).toBe('50%');+ }+ });+it('starts the arc at twelve o’clock', () => {render(<Spinner data-testid="spinner" />);- const frame = SIZES.md.diameter + SIZES.md.border * 2;- expect(circles().arc.getAttribute('transform')).toBe(- `rotate(-90 ${frame / 2} ${frame / 2})`,- );+ // A CSS rotation about the shape's own box, not an SVG transform about a+ // centre in user units — the centre is `50%`, so there is no user-unit+ // number to name, and the offset has to survive a themed diameter.+ expect(circles().arc.getAttribute('transform')).toBeNull();});// The authored dash is the size's own absolute pattern, and stays that way:diff --git a/packages/core/src/Spinner/Spinner.tsx b/packages/core/src/Spinner/Spinner.tsxindex dac93b8a70..34ff23f919 100644--- a/packages/core/src/Spinner/Spinner.tsx+++ b/packages/core/src/Spinner/Spinner.tsx@@ -265,13 +265,22 @@ const styles = stylex.create({ring: {backfaceVisibility: 'hidden',display: 'block',+ // The frame is the box, read from the same composed var the span is sized+ // from — not from the size constant, which a theme cannot reach, and not+ // from a percentage, which needs the grid area to be a definite size in+ // both axes (an unresolved percentage height on an SVG falls back to the+ // replaced-element default of 150px and drops the ring below the box).+ // One expression, one source of truth, resolves in any layout.+ width: `var(${BOX_SIZE})`,+ height: `var(${BOX_SIZE})`,willChange: 'transform',- // The svg keeps the size its `viewBox` describes, so one user unit is one- // CSS pixel and the lengths below mean what they say. A themed diameter- // therefore draws a ring wider than the svg's own box — which is fine, and- // stays centered, because the box it is centered in is the span, sized- // from the same two vars. Clipping it to the default frame is the one- // thing that would break that, hence `visible`.+ // The svg is sized in CSS (100% of the span) rather than from the size+ // constant, so one user unit is one CSS pixel AND the frame follows a+ // themed diameter: the span's box is `diameter + 2 x stroke`, both public+ // vars, so ring and frame move together. Sizing it from the constant+ // instead left the svg larger than the box the moment a theme changed the+ // diameter — an overflowing grid item aligns to start rather than centre,+ // which put the ring 1.5-3.3px off across the four sizes.overflow: 'visible',// Slow the rotation dramatically under reduced-motion rather than freezing// it (a frozen spinner reads as broken), matching ProgressBar's approach.@@ -286,6 +295,11 @@ const styles = stylex.create({},circle: {fill: 'none',+ // The ring is centred on the box (cx/cy 50%), so the arc's start offset+ // pivots there too — as a CSS transform, since the SVG `transform`+ // attribute would need that centre as a number in user units.+ transformBox: 'fill-box',+ transformOrigin: 'center',strokeLinecap: 'round',// The geometry the ring is actually drawn at. `r` and `stroke-width` are// CSS properties on an SVG shape, and a CSS declaration outranks the@@ -310,6 +324,7 @@ const styles = stylex.create({// lengths keeps the default byte-identical to what it drew before.arc: {stroke: 'var(--spinner-color)',+ transform: 'rotate(-90deg)',strokeDasharray: `calc(var(${RESOLVED_DIAMETER}) * ${ARC_DASH}) calc(var(${RESOLVED_DIAMETER}) * ${ARC_GAP})`,},track: {stroke: 'var(--spinner-track-color)'},@@ -465,7 +480,6 @@ export function Spinner({}: SpinnerProps) {const {border, diameter} = SIZES[size];const frameSize = diameter + border * 2;- const center = frameSize / 2;const circumference = Math.PI * diameter;const arcLength = circumference * ARC_FRACTION;const hasLabel = label != null;@@ -521,12 +535,11 @@ export function Spinner({ref={syncRotationPhase}width={frameSize}height={frameSize}- viewBox={`0 0 ${frameSize} ${frameSize}`}aria-hidden="true"{...stylex.props(styles.ring)}><circle- cx={center}- cy={center}+ cx="50%"+ cy="50%"r={diameter / 2}strokeWidth={border}{...stylex.props(@@ -536,15 +549,14 @@ export function Spinner({)}/><circle- cx={center}- cy={center}+ cx="50%"+ cy="50%"r={diameter / 2}strokeWidth={border}// The size's own dash, for the render with no stylesheet. The rule// above composes the same lengths from the resolved diameter, so a// themed ring keeps this fraction of arc rather than this length.strokeDasharray={`${arcLength} ${circumference - arcLength}`}- transform={`rotate(-90 ${center} ${center})`}{...stylex.props(styles.circle, styles.arc)}/></svg>