Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/spinner-audit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[fix] Spinner: an empty or blank `aria-label` or string `label` no longer leaves the loading indicator with no accessible name. An empty or blank value counts as unset, so the indicator is named by a string label that is not empty or blank, else the translated "Loading". A standalone Spinner that shows no label text and had an empty or blank name now announces "Loading" when it appears after page load, as any other standalone Spinner does; one with label text still announces that text. To silence a Spinner inside a control that already reports that it is busy, hide it with `aria-hidden`, as the docs now say.

@cixzhang
11 changes: 7 additions & 4 deletions apps/storybook/stories/Spinner.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export const Shades: Story = {
render: () => (
<HStack gap={4} vAlign="center">
<Spinner shade="default" />
<Spinner shade="subtle" />
<span style={{color: 'var(--color-text-blue)'}}>
<Spinner shade="inherit" />
</span>
<div
style={{
backgroundColor: '#1a1a2e',
Expand Down Expand Up @@ -227,10 +231,9 @@ export const ThemedHairlineStroke: Story = {

/**
* A flex host narrower than the spinner in it. The box keeps the ring's size
* and overflows the host visibly; before the fix the host compressed the box
* and the ring was cut off at its edge, silently — a sliced ring still spins,
* so nothing reported a problem. The dashed rule is the host, drawn so the
* overflow is legible.
* and overflows the host visibly. A compressed box would cut the ring off at
* its edge with no error, because a cut ring still turns. The dashed rule is
* the host, drawn so the overflow is legible.
*
* All three are ordinary layouts rather than contrived ones: a label beside a
* spinner in a narrow row, a spinner next to a sibling that will not give up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,6 @@
"spec:AST-085",
"spec:AST-085"
],
"packages/core/src/Spinner/Spinner.tsx": ["#5410"],
"packages/core/src/StatusDot/StatusDot.tsx": [
"INV11",
"INV11",
Expand Down
27 changes: 21 additions & 6 deletions packages/core/src/Spinner/Spinner.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ export const docs = {
name: 'label',
type: 'ReactNode',
description:
'Visible content below the spinner. A string label names the indicator through aria-labelledby.',
'Visible content below the spinner. A string label names the indicator through aria-labelledby, unless an aria-label is set. An empty or blank value counts as unset.',
},
{
name: 'aria-label',
type: 'string',
description:
'Accessible name for screen readers. Defaults to label (if string) or a translated "Loading" from the i18n catalog.',
'Accessible name for screen readers. An empty or blank value counts as unset. Defaults to a string label that is not empty or blank, else a translated "Loading" from the i18n catalog.',
default: "'Loading'",
},
{
Expand Down Expand Up @@ -102,6 +102,11 @@ export const docs = {
description:
'Use the "onMedia" shade when placed on dark or accent-colored backgrounds.',
},
{
guidance: true,
description:
'Hide a Spinner with aria-hidden when it sits inside a control that already reports that it is busy, so a screen reader does not announce the work twice.',
},
{
guidance: false,
description:
Expand Down Expand Up @@ -137,13 +142,13 @@ export const docsZh = {
name: 'label',
type: 'ReactNode',
description:
'旋转器下方的可见内容。未设置 aria-label 时,字符串标签提供无障碍名称。',
'旋转器下方的可见内容。未设置 aria-label 时,字符串标签提供无障碍名称。空值或仅含空白的值视为未设置。',
},
{
name: 'aria-label',
type: 'string',
description:
'屏幕阅读器的无障碍名称。默认为 label(如果是字符串)或来自 i18n 词条的已翻译 "Loading"。',
'屏幕阅读器的无障碍名称。空值或仅含空白的值视为未设置。默认为非空且不全为空白的字符串 label,否则为来自 i18n 词条的已翻译 "Loading"。',
default: "'Loading'",
},
{
Expand Down Expand Up @@ -204,6 +209,11 @@ export const docsZh = {
description:
'Use the "onMedia" shade when placed on dark or accent-colored backgrounds.',
},
{
guidance: true,
description:
'Hide a Spinner with aria-hidden when it sits inside a control that already reports that it is busy, so a screen reader does not announce the work twice.',
},
{
guidance: false,
description:
Expand Down Expand Up @@ -234,6 +244,11 @@ export const docsDense = {
description:
'Use the "onMedia" shade when placed on dark or accent-colored backgrounds.',
},
{
guidance: true,
description:
'Hide a Spinner with aria-hidden when it sits inside a control that already reports that it is busy, so a screen reader does not announce the work twice.',
},
{
guidance: false,
description:
Expand All @@ -250,9 +265,9 @@ export const docsDense = {
size: 'Spinner size: ring diameter (10px, 14px, 18px, 28px).',
shade: 'Color shade for light or dark backgrounds.',
label:
'Visible content below spinner. String provides its accessible name unless aria-label is set.',
'Visible content below spinner. A string provides its accessible name unless aria-label is set. Empty or blank counts as unset.',
'aria-label':
'A11y name for screen readers. Defaults to label or a translated "Loading".',
'A11y name for screen readers. Empty or blank counts as unset. Defaults to a string label that is not empty or blank, or a translated "Loading".',
xstyle:
'StyleX styles for layout customization. Must be stylex.create() value, not inline style.',
},
Expand Down
165 changes: 141 additions & 24 deletions packages/core/src/Spinner/Spinner.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {defineTheme} from '../theme/defineTheme';
import {generateThemeCSS} from '../theme/generateThemeRules';
import {getAllInjectedCss} from '../__tests__/forcedColors';
import {declaredValue} from '../__tests__/stylexDeclarations';
import {colorVars} from '../theme/tokens.stylex';
import * as stylex from '@stylexjs/stylex';

const {announce} = vi.hoisted(() => ({announce: vi.fn()}));
vi.mock('../hooks/useAnnounce', () => ({useAnnounce: () => announce}));
Expand Down Expand Up @@ -189,13 +191,10 @@ describe('Spinner', () => {
expect(spinner.tagName.toLowerCase()).toBe('span');
});

// The box has always been sized by an inline width/height written after the
// caller's `style`, so the component's own size wins over a `style={{width}}`
// passed in. Making the geometry themeable changed what that value is made
// of — a composed var rather than a number — and deliberately not where it is
// written: moving the sizing into a rule would have handed a caller's inline
// width a precedence over the box it has never had. This pins the precedence
// itself, which is the part a consumer could be depending on.
// The box is sized by an inline width/height written after the caller's
// `style`, so the component's own size wins over a `style={{width}}` passed
// in. A rule instead would let a caller's inline width override the box.
// These tests pin that precedence.
describe('box sizing', () => {
it.each(Object.entries(SIZES))(
'falls back to the %s frame where no stylesheet has declared the var',
Expand Down Expand Up @@ -227,12 +226,11 @@ describe('Spinner', () => {
});
});

// The themed geometry resolves in the cascade — jsdom implements no layout
// The themed geometry resolves in the cascade. jsdom implements no layout
// and no custom-property registration, so no test here can reach what a
// theme actually draws; that is verified in a browser and the numbers are in
// the PR. What IS a contract a unit test can hold is the routing: a theme
// writes an override against the documented key, and it has to come out on
// the selector the component reads from.
// theme draws, and no automated check covers it. A unit test can hold the
// routing: a theme writes an override against the documented key, and it
// comes out on the selector the component reads from.
describe('a theme reaches the spinner through its public vars', () => {
const cssFor = (
components: Parameters<typeof defineTheme>[0]['components'],
Expand Down Expand Up @@ -269,7 +267,7 @@ describe('Spinner', () => {
);
});

it('scopes a themed arc fraction to that size variant (#5819)', () => {
it('scopes a themed arc fraction to that size variant', () => {
expect(
cssFor({spinner: {'size:xl': {'--spinner-arc-fraction': '0.75'}}}),
).toContain(
Expand Down Expand Up @@ -335,12 +333,9 @@ 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.
// A frame sized from the size constant stays larger than the box when a
// theme changes the diameter. The overflowing grid item then aligns to start
// rather than centre, 1.5 to 3.3px off across the four sizes in Chromium.
it('sizes the frame from CSS, not from the size constant', () => {
render(<Spinner data-testid="spinner" />);
// No viewBox, and the circles centre on the box rather than on a centre
Expand All @@ -363,13 +358,13 @@ describe('Spinner ring', () => {
// 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();
expect(declaredValue(circles().arc, 'transform')).toBe('rotate(-90deg)');
});

// The authored dash is the size's own absolute pattern, and stays that way:
// it is what a render with no stylesheet draws, and it is byte-for-byte the
// pattern this component drew before the geometry became themeable. Scaling
// with a themed diameter is the rule's job — it composes the same two
// lengths out of the resolved diameter — and deliberately not `pathLength`'s,
// The authored dash is the size's own absolute pattern: it is what a render
// with no stylesheet draws. Scaling with a themed diameter is the rule's job
// — it composes the same two lengths out of the resolved diameter — and
// deliberately not `pathLength`'s,
// which rescales against the path length the UA measures on its own
// approximation of the circle and shortens the default arc by 0.64%.
it.each(Object.entries(SIZES))(
Expand Down Expand Up @@ -836,8 +831,130 @@ describe('shared announcement lifecycle', () => {
]);
});

it('announces the translated default for an empty aria-label', () => {
render(<Spinner aria-label="" />);
expect(announce).toHaveBeenCalledExactlyOnceWith('Loading', 'polite');
});

it('uses the accessible-name fallback for a textless ReactNode label', () => {
render(<Spinner label={<span aria-hidden="true">Decoration</span>} />);
expect(announce).toHaveBeenCalledExactlyOnceWith('Loading', 'polite');
});
});

describe('Spinner accessible name', () => {
it.each([
['an empty aria-label', {'aria-label': ''}],
['a blank aria-label', {'aria-label': ' '}],
['an empty string label', {label: ''}],
['a blank string label', {label: ' '}],
])('falls back to the translated default for %s', (_, props) => {
render(<Spinner {...props} />);
expect(screen.getByRole('progressbar')).toHaveAccessibleName('Loading');
});

it('names the indicator from a string label when the aria-label is empty', () => {
render(<Spinner aria-label="" label="Saving" />);
const indicator = screen.getByRole('progressbar');
expect(indicator).toHaveAccessibleName('Saving');
expect(indicator).not.toHaveAttribute('aria-label');
});
});

const ownershipStyles = stylex.create({
margin: {marginInlineStart: '7px'},
});

describe('Spinner element ownership', () => {
it('puts the ref, target, className and xstyle on the indicator without a label', () => {
const ref = createRef<HTMLSpanElement>();
render(
<Spinner
ref={ref}
className="app-spinner"
xstyle={ownershipStyles.margin}
/>,
);
const indicator = screen.getByRole('progressbar');
expect(ref.current).toBe(indicator);
expect(indicator).toHaveClass('astryx-spinner', 'app-spinner');
expect(declaredValue(indicator, 'margin-inline-start')).toBe('7px');
});

it('puts the ref, target, className and xstyle on the wrapper with a label', () => {
const ref = createRef<HTMLSpanElement>();
render(
<Spinner
ref={ref}
label="Saving"
className="app-spinner"
xstyle={ownershipStyles.margin}
/>,
);
const indicator = screen.getByRole('progressbar');
const wrapper = ref.current as HTMLElement;
expect(wrapper).not.toBe(indicator);
expect(wrapper.contains(indicator)).toBe(true);
expect(wrapper).toHaveClass('astryx-spinner', 'app-spinner');
expect(indicator).not.toHaveClass('astryx-spinner');
expect(indicator).not.toHaveClass('app-spinner');
expect(declaredValue(wrapper, 'margin-inline-start')).toBe('7px');
expect(declaredValue(indicator, 'margin-inline-start')).toBeNull();
});

it('declares the public vars on the wrapper, not the indicator, with a label', () => {
render(<Spinner label="Saving" size="xl" data-testid="spinner" />);
const wrapper = screen.getByTestId('spinner');
const indicator = screen.getByRole('progressbar');
expect(declaredValue(wrapper, '--spinner-diameter')).toBe('28px');
expect(declaredValue(wrapper, '--spinner-color')).toBe(
colorVars['--color-accent'],
);
for (const name of [
'--spinner-diameter',
'--spinner-stroke-width',
'--spinner-arc-fraction',
'--spinner-color',
'--spinner-track-color',
]) {
expect(declaredValue(indicator, name)).toBeNull();
}
});

it('hides the track and the turning arc from assistive technology', () => {
render(<Spinner data-testid="spinner" />);
const [trackSvg] = rings();
expect(trackSvg).toHaveAttribute('aria-hidden', 'true');
expect(rotor()).toHaveAttribute('aria-hidden', 'true');
});
});

describe('Spinner shade paint', () => {
it.each([
['default', colorVars['--color-accent'], colorVars['--color-track'], '1'],
[
'subtle',
colorVars['--color-text-secondary'],
colorVars['--color-track'],
'1',
],
[
'onMedia',
colorVars['--color-on-dark'],
colorVars['--color-on-dark'],
String(77 / 255),
],
['inherit', 'currentColor', 'currentColor', '0.3'],
] as const)(
'declares the %s shade arc and track colors and track opacity',
(shade, arc, track, opacity) => {
render(<Spinner shade={shade} data-testid="spinner" />);
const indicator = screen.getByTestId('spinner');
expect(declaredValue(indicator, '--spinner-color')).toBe(arc);
expect(declaredValue(indicator, '--spinner-track-color')).toBe(track);
expect(
Number(declaredValue(circles().track, 'stroke-opacity')),
).toBeCloseTo(Number(opacity), 3);
},
);
});
Loading