Skip to content

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

TypeScript

fix(core): keep hidden-label descriptions in aria-describedby (#4003)

Baraa Hamodi30b6a8c58356
diff --git a/.changeset/choice-hidden-label-describedby.md b/.changeset/choice-hidden-label-describedby.md
new file mode 100644
index 0000000000..12d6760cbc
--- /dev/null
+++ b/.changeset/choice-hidden-label-describedby.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] CheckboxInput/Switch: descriptions stay linked via aria-describedby when the label is visually hidden, instead of being orphaned in the DOM.
+
+@bhamodi
diff --git a/packages/core/src/CheckboxInput/CheckboxInput.test.tsx b/packages/core/src/CheckboxInput/CheckboxInput.test.tsx
index ff2df4247f..22707f4445 100644
--- a/packages/core/src/CheckboxInput/CheckboxInput.test.tsx
+++ b/packages/core/src/CheckboxInput/CheckboxInput.test.tsx
@@ -248,6 +248,22 @@ describe('CheckboxInput', () => {
expect(screen.getByLabelText('Select row')).toBeInTheDocument();
});
+ it('keeps description linked via aria-describedby when isLabelHidden', () => {
+ render(
+ <CheckboxInput
+ label="Select row"
+ isLabelHidden
+ description="Selects this row for bulk actions"
+ value={false}
+ onChange={() => {}}
+ />,
+ );
+ const checkbox = screen.getByRole('checkbox');
+ const description = screen.getByText('Selects this row for bulk actions');
+ expect(description.id).not.toBe('');
+ expect(checkbox.getAttribute('aria-describedby')).toContain(description.id);
+ });
+
it('shows label visually by default', () => {
render(
<CheckboxInput label="Accept terms" value={false} onChange={() => {}} />,
diff --git a/packages/core/src/CheckboxInput/CheckboxInput.tsx b/packages/core/src/CheckboxInput/CheckboxInput.tsx
index 987bf525d7..3ca54c15c8 100644
--- a/packages/core/src/CheckboxInput/CheckboxInput.tsx
+++ b/packages/core/src/CheckboxInput/CheckboxInput.tsx
@@ -446,9 +446,11 @@ export function CheckboxInput({
);
// Build aria-describedby from description and status message
- // Only include descriptionID when the element actually renders
+ // Only include descriptionID when the element actually renders.
+ // FieldLabel renders the description (with descriptionID) even when the
+ // label is visually hidden — it's sr-only, so keep it linked.
const describedByParts: string[] = [];
- if (description && !isLabelHidden) {
+ if (description) {
describedByParts.push(descriptionID);
}
if (status?.message) {
diff --git a/packages/core/src/Switch/Switch.test.tsx b/packages/core/src/Switch/Switch.test.tsx
index cc0b17e8fd..05c9c71cb5 100644
--- a/packages/core/src/Switch/Switch.test.tsx
+++ b/packages/core/src/Switch/Switch.test.tsx
@@ -197,6 +197,22 @@ describe('Switch', () => {
expect(screen.getByLabelText('Toggle row')).toBeInTheDocument();
});
+ it('keeps description linked via aria-describedby when isLabelHidden', () => {
+ render(
+ <Switch
+ label="Toggle row"
+ isLabelHidden
+ description="Enables sync for this row"
+ value={false}
+ onChange={() => {}}
+ />,
+ );
+ const switchEl = screen.getByRole('switch');
+ const description = screen.getByText('Enables sync for this row');
+ expect(description.id).not.toBe('');
+ expect(switchEl.getAttribute('aria-describedby')).toContain(description.id);
+ });
+
it('shows label visually by default', () => {
render(
<Switch label="Enable notifications" value={false} onChange={() => {}} />,
diff --git a/packages/core/src/Switch/Switch.tsx b/packages/core/src/Switch/Switch.tsx
index 3f5c65b325..efe15f1883 100644
--- a/packages/core/src/Switch/Switch.tsx
+++ b/packages/core/src/Switch/Switch.tsx
@@ -393,9 +393,11 @@ export function Switch({
});
// Build aria-describedby from description and status message
- // Only include descriptionID when the element actually renders
+ // Only include descriptionID when the element actually renders.
+ // FieldLabel renders the description (with descriptionID) even when the
+ // label is visually hidden — it's sr-only, so keep it linked.
const describedByParts: string[] = [];
- if (description && !isLabelHidden) {
+ if (description) {
describedByParts.push(descriptionID);
}
if (status?.message) {