Skip to content

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

TypeScript

fix(core): enforce VisuallyHidden's styling-free contract at runtime (#7826)

Aashish Mahato3001ed5bea47
diff --git a/.changeset/visuallyhidden-keeps-its-clip-block-and-its-dom-a3k7.md b/.changeset/visuallyhidden-keeps-its-clip-block-and-its-dom-a3k7.md
new file mode 100644
index 0000000000..402b54eb54
--- /dev/null
+++ b/.changeset/visuallyhidden-keeps-its-clip-block-and-its-dom-a3k7.md
@@ -0,0 +1,17 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] VisuallyHidden keeps its clip block and its DOM clean: `xstyle`, `className` and `style` no longer reach the element. The component's contract is that content in the accessibility tree stays invisible, so its clip block is meant to be non-overridable — but the implementation spread leftover props onto the element, which wrote the literal attribute `xstyle="[object Object]"` into the DOM, and let the same props arriving through a cast or a `{...consumerProps}` object take the last word on `position` and un-hide the node while assistive technology kept announcing it.
+
+Measured prop by prop against `main`'s implementation, before and after this change:
+
+- `xstyle` arrived as the attribute only — the `style` attribute stayed `null` and the clip classes were the same set in both arms — so a consumer passing it loses a stray attribute and no pixel.
+- `className` was already displaced by the clip block's own class list; it reaches the element in neither arm.
+- `style` did reach the element (`style="position: static;"`), and an inline declaration outranks a class rule, so the node was un-hidden. It stops reaching the element. That is the one rendering change here, and it moves the node back to the hidden state the contract calls non-overridable.
+
+None of the three is styling surface on `VisuallyHiddenProps` — `className` and `style` are behind its `Omit`, reaching the element only through a cast or an untyped spread, and `xstyle` is documented as ignored — so no typed call site loses an effect it was promised.
+
+`xstyle` stays declared on `VisuallyHiddenProps`, documented as ignored, because released 0.7.2 accepted it through `BaseProps`: removing it from the interface makes `<VisuallyHidden xstyle={…}>` stop compiling, and AST-017 FR31 and FR53 admit that narrowing only as a `[breaking]` change in a planned minor. It is deliberately not marked `@deprecated`: under FR28 a patch that deprecates a surface has to ship the exact migration or a codemod with it, and this fix ships neither. The removal and its migration belong to a planned minor, and the migration is to delete the prop — it applies nothing to the element.
+
+@aashish254
diff --git a/packages/core/src/VisuallyHidden/VisuallyHidden.doc.mjs b/packages/core/src/VisuallyHidden/VisuallyHidden.doc.mjs
index 245d36b4ec..239c6b93fb 100644
--- a/packages/core/src/VisuallyHidden/VisuallyHidden.doc.mjs
+++ b/packages/core/src/VisuallyHidden/VisuallyHidden.doc.mjs
@@ -23,7 +23,7 @@ export const docs = {
},
],
usage: {
- description: 'Renders content in the accessibility tree while hiding it visually. Use for accessible names on icon-only controls, aria-live announcement regions, and supplementary screen-reader context. Deliberately has no styling props; the whole point is to stay invisible.',
+ description: 'Renders content in the accessibility tree while hiding it visually. Use for accessible names on icon-only controls, aria-live announcement regions, and supplementary screen-reader context. Deliberately has no styling props; the whole point is to stay invisible. `xstyle` is still accepted and ignored, only so code written against 0.7.2 keeps compiling; a planned minor removes it.',
bestPractices: [
{ guidance: true, description: 'Use to give icon-only buttons and controls an accessible name that screen readers announce.' },
{ guidance: true, description: 'Render as a block element (as="div") with aria-live to announce dynamic updates like drag-and-drop or result counts.' },
diff --git a/packages/core/src/VisuallyHidden/VisuallyHidden.test.tsx b/packages/core/src/VisuallyHidden/VisuallyHidden.test.tsx
index da39fcbda3..25f1a4d975 100644
--- a/packages/core/src/VisuallyHidden/VisuallyHidden.test.tsx
+++ b/packages/core/src/VisuallyHidden/VisuallyHidden.test.tsx
@@ -12,6 +12,7 @@
import {describe, it, expect} from 'vitest';
import {render, screen} from '@testing-library/react';
import {createRef} from 'react';
+import * as stylex from '@stylexjs/stylex';
import {VisuallyHidden} from './VisuallyHidden';
describe('VisuallyHidden', () => {
@@ -64,6 +65,44 @@ describe('VisuallyHidden', () => {
expect(el).toHaveAttribute('role', 'status');
});
+ it('does not forward the styling props its own contract omits', () => {
+ // `className` and `style` are off the interface, and they are reached here
+ // through an untyped spread, which is how they get past the `Omit` in
+ // practice: a cast, a `{...consumerProps}` object, or plain JavaScript. The
+ // type is only the first line of defence here.
+ const stylingProps: Record<string, unknown> = {
+ xstyle: {color: 'red'},
+ className: 'consumer-track',
+ style: {position: 'static'},
+ };
+ render(<VisuallyHidden {...stylingProps}>Hidden</VisuallyHidden>);
+ const el = screen.getByText('Hidden');
+
+ // VisuallyHidden documents itself as styling-free and its clip block as
+ // non-overridable, but the implementation spreads leftover props onto the
+ // element: `xstyle` reaches the DOM as the literal string "[object Object]",
+ // and a cast `style` wins the last-word race for `position`, un-hiding the
+ // node (or clipping real content) while keeping the a11y semantics.
+ expect(el).not.toHaveAttribute('xstyle');
+ expect(el.getAttribute('class') ?? '').not.toContain('consumer-track');
+ expect(el.getAttribute('style') ?? '').not.toContain('static');
+ });
+
+ it('still accepts a typed xstyle, and ignores it', () => {
+ // Released 0.7.2 accepted `xstyle` through `BaseProps`, so narrowing it out
+ // of `VisuallyHiddenProps` would make code that compiles today stop
+ // compiling. It stays declared and documented as ignored, and the runtime
+ // drop is what keeps the stray attribute off the element. Written as a typed
+ // prop on purpose: this line is the tripwire if the interface is ever
+ // narrowed early.
+ const overrides = stylex.create({tint: {color: 'red'}});
+ render(<VisuallyHidden xstyle={overrides.tint}>Hidden</VisuallyHidden>);
+ const el = screen.getByText('Hidden');
+
+ expect(el).not.toHaveAttribute('xstyle');
+ expect(el.getAttribute('style') ?? '').not.toContain('color');
+ });
+
it('gives an icon-only control an accessible name', () => {
render(
<button type="button">
diff --git a/packages/core/src/VisuallyHidden/VisuallyHidden.tsx b/packages/core/src/VisuallyHidden/VisuallyHidden.tsx
index 8a72bb0f4b..dcbe3f5019 100644
--- a/packages/core/src/VisuallyHidden/VisuallyHidden.tsx
+++ b/packages/core/src/VisuallyHidden/VisuallyHidden.tsx
@@ -16,14 +16,23 @@
import {createElement, type ElementType, type ReactNode, type Ref} from 'react';
import type {BaseProps} from '../BaseProps';
+import type {StyleXStyles} from '@stylexjs/stylex';
import * as stylex from '@stylexjs/stylex';
/**
* VisuallyHidden is deliberately styling-free: it exists to *not* be seen, so it
- * intentionally omits `xstyle`/`className`/`style`. The clip block is fixed and
- * non-overridable — styling a visually-hidden node is always a mistake. The
- * accessibility pass-throughs from `BaseProps` (`aria-*`, `role`, `id`,
- * `data-*`, event handlers) remain, since the live-region use case needs them.
+ * omits `className`/`style` and applies a fixed, non-overridable clip block —
+ * styling a visually-hidden node is always a mistake. The accessibility
+ * pass-throughs from `BaseProps` (`aria-*`, `role`, `id`, `data-*`, event
+ * handlers) remain, since the live-region use case needs them.
+ *
+ * `xstyle` stays declared and documented as ignored, because released 0.7.2
+ * accepted it through `BaseProps`: narrowing the interface would force every
+ * consumer to edit code, which is a `[breaking]` change for a planned minor
+ * (`spec:AST-017/FR53`). It carries no `@deprecated` marker either — under
+ * `spec:AST-017/FR28` a patch may only deprecate a surface it ships an exact
+ * migration for, and this fix ships none. Dropping the prop from the interface,
+ * with the migration being to delete it, belongs to a planned minor.
*/
export interface VisuallyHiddenProps extends Omit<
BaseProps<HTMLElement>,
@@ -44,6 +53,16 @@ export interface VisuallyHiddenProps extends Omit<
* @default 'span'
*/
as?: ElementType;
+
+ /**
+ * Ignored: VisuallyHidden has no styling surface. The clip block is the only
+ * style applied, and nothing a caller passes can displace it.
+ *
+ * Accepted only so that code written against 0.7.2 keeps compiling. It is not
+ * applied to the element, and a planned minor removes it — the migration is to
+ * delete the prop.
+ */
+ xstyle?: StyleXStyles;
}
const styles = stylex.create({
@@ -90,8 +109,22 @@ export function VisuallyHidden({
children,
as: element = 'span',
ref,
- ...props
+ ...rest
}: VisuallyHiddenProps) {
+ // `className` and `style` are off the interface, and the ignored `xstyle`
+ // is off by intent. Take all three back off at runtime so a cast cannot reach
+ // the element either: `xstyle` is not a valid attribute, so spreading it writes
+ // xstyle="[object Object]" into the DOM, and a cast `style` keeps the last word
+ // on `position` and un-hides the node.
+ const {
+ xstyle: _xstyle,
+ className: _className,
+ style: _style,
+ ...props
+ } = rest as typeof rest & {
+ className?: unknown;
+ style?: unknown;
+ };
return createElement(
element,
{