Skip to content

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

TypeScript

refactor(inputs): use VisuallyHidden primitive for invalid-input live regions (#3450)

Cindy Zhang6ba19bf4f551
diff --git a/packages/core/src/DateInput/DateInput.tsx b/packages/core/src/DateInput/DateInput.tsx
index 173f640ad7..954117bc99 100644
--- a/packages/core/src/DateInput/DateInput.tsx
+++ b/packages/core/src/DateInput/DateInput.tsx
@@ -44,6 +44,7 @@ import {
inputStatusFocusWithinStyles,
} from '../Field';
import {Icon} from '../Icon';
+import {VisuallyHidden} from '../VisuallyHidden';
import {Spinner} from '../Spinner';
import {Calendar, type ISODateString, type CalendarHandle} from '../Calendar';
import {useCalendarConstraints} from '../Calendar/hooks';
@@ -103,17 +104,6 @@ const styles = stylex.create({
inputInvalid: {
color: colorVars['--color-text-secondary'],
},
- visuallyHidden: {
- position: 'absolute',
- width: '1px',
- height: '1px',
- padding: 0,
- margin: '-1px',
- overflow: 'hidden',
- clip: 'rect(0, 0, 0, 0)',
- whiteSpace: 'nowrap',
- borderWidth: 0,
- },
});
const sizeStyles = stylex.create({
@@ -591,12 +581,9 @@ export function DateInput({
The value silently reverts on blur, so without this a screen-reader
user would get no feedback that their entry was rejected (WCAG 3.3.1).
*/}
- <span
- role="alert"
- aria-live="assertive"
- {...stylex.props(styles.visuallyHidden)}>
+ <VisuallyHidden as="div" role="alert" aria-live="assertive">
{!isInputValid ? 'Invalid date' : ''}
- </span>
+ </VisuallyHidden>
{hasClear && value !== undefined && !isEffectivelyDisabled && (
<button
type="button"
diff --git a/packages/core/src/DateTimeInput/DateTimeInput.tsx b/packages/core/src/DateTimeInput/DateTimeInput.tsx
index 421e3076f3..743eac8274 100644
--- a/packages/core/src/DateTimeInput/DateTimeInput.tsx
+++ b/packages/core/src/DateTimeInput/DateTimeInput.tsx
@@ -47,6 +47,7 @@ import {
inputStatusFocusWithinStyles,
} from '../Field';
import {Icon} from '../Icon';
+import {VisuallyHidden} from '../VisuallyHidden';
import {Spinner} from '../Spinner';
import {Calendar, type ISODateString, type CalendarHandle} from '../Calendar';
import {useCalendarConstraints} from '../Calendar/hooks';
@@ -146,17 +147,6 @@ const styles = stylex.create({
inputInvalid: {
color: colorVars['--color-text-secondary'],
},
- visuallyHidden: {
- position: 'absolute',
- width: '1px',
- height: '1px',
- padding: 0,
- margin: '-1px',
- overflow: 'hidden',
- clip: 'rect(0, 0, 0, 0)',
- whiteSpace: 'nowrap',
- borderWidth: 0,
- },
dateWrapper: {
flex: 1,
flexBasis: 0,
@@ -878,12 +868,9 @@ export function DateTimeInput({
screen-reader user would get no feedback that their entry was
rejected (WCAG 3.3.1).
*/}
- <span
- role="alert"
- aria-live="assertive"
- {...stylex.props(styles.visuallyHidden)}>
+ <VisuallyHidden as="div" role="alert" aria-live="assertive">
{!isDateInputValid ? 'Invalid date' : ''}
- </span>
+ </VisuallyHidden>
{hasClear && value !== undefined && !isEffectivelyDisabled && (
<button
type="button"
@@ -946,12 +933,9 @@ export function DateTimeInput({
Live region announcing invalid typed time input to assistive
technology (WCAG 3.3.1).
*/}
- <span
- role="alert"
- aria-live="assertive"
- {...stylex.props(styles.visuallyHidden)}>
+ <VisuallyHidden as="div" role="alert" aria-live="assertive">
{!isTimeInputValid ? 'Invalid time' : ''}
- </span>
+ </VisuallyHidden>
</div>
</div>
diff --git a/packages/core/src/NumberInput/NumberInput.tsx b/packages/core/src/NumberInput/NumberInput.tsx
index 01c156e0d4..b11f974deb 100644
--- a/packages/core/src/NumberInput/NumberInput.tsx
+++ b/packages/core/src/NumberInput/NumberInput.tsx
@@ -46,6 +46,7 @@ import {
inputStatusFocusWithinStyles,
} from '../Field';
import {Icon, renderIconSlot, type IconType} from '../Icon';
+import {VisuallyHidden} from '../VisuallyHidden';
import {useSize} from '../SizeContext/SizeContext';
import {useInputContainer} from '../hooks/useInputContainer';
import {useInputGroup} from '../InputGroup/InputGroupContext';
@@ -97,17 +98,6 @@ const styles = stylex.create({
inputInvalid: {
color: colorVars['--color-text-secondary'],
},
- visuallyHidden: {
- position: 'absolute',
- width: '1px',
- height: '1px',
- padding: 0,
- margin: '-1px',
- overflow: 'hidden',
- clip: 'rect(0, 0, 0, 0)',
- whiteSpace: 'nowrap',
- borderWidth: 0,
- },
units: {
fontFamily: typographyVars['--font-family-body'],
fontSize: typeScaleVars['--text-body-size'],
@@ -590,12 +580,9 @@ export function NumberInput({
The value silently reverts on blur, so without this a screen-reader
user would get no feedback that their entry was rejected (WCAG 3.3.1).
*/}
- <span
- role="alert"
- aria-live="assertive"
- {...stylex.props(styles.visuallyHidden)}>
+ <VisuallyHidden as="div" role="alert" aria-live="assertive">
{!isInputValid ? 'Invalid number' : ''}
- </span>
+ </VisuallyHidden>
{hasClear && value != null && !isDisabled && (
<button
type="button"