Skip to content

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

TypeScript

feat: React 19 transitions with onChangeAction/onClickAction (#146)

Cindy Zhang3c78f1058204
diff --git a/apps/example-nextjs/package.json b/apps/example-nextjs/package.json
index 020e3d865e..2931e32115 100644
--- a/apps/example-nextjs/package.json
+++ b/apps/example-nextjs/package.json
@@ -12,16 +12,16 @@
"@xds/core": "*",
"@xds/theme-default": "*",
"next": "^15.0.0",
- "react": "^18.3.0",
- "react-dom": "^18.3.0"
+ "react": "^19.2.4",
+ "react-dom": "^19.2.4"
},
"devDependencies": {
"@babel/preset-react": "^7.25.0",
"@babel/preset-typescript": "^7.25.0",
"@stylexjs/babel-plugin": "^0.17.4",
"@stylexjs/postcss-plugin": "^0.17.4",
- "@types/react": "^18.3.0",
- "@types/react-dom": "^18.3.0",
+ "@types/react": "^19.2.14",
+ "@types/react-dom": "^19.2.3",
"typescript": "^5.7.0"
}
}
diff --git a/apps/example-vite/package.json b/apps/example-vite/package.json
index 980f247ef2..4218e68ffd 100644
--- a/apps/example-vite/package.json
+++ b/apps/example-vite/package.json
@@ -12,13 +12,13 @@
"@stylexjs/stylex": "^0.17.4",
"@xds/core": "*",
"@xds/theme-default": "*",
- "react": "^18.3.0",
- "react-dom": "^18.3.0"
+ "react": "^19.2.4",
+ "react-dom": "^19.2.4"
},
"devDependencies": {
"@stylexjs/unplugin": "^0.17.4",
- "@types/react": "^18.3.0",
- "@types/react-dom": "^18.3.0",
+ "@types/react": "^19.2.14",
+ "@types/react-dom": "^19.2.3",
"@vitejs/plugin-react": "^4.3.0",
"typescript": "^5.7.0",
"vite": "^5.4.0"
diff --git a/apps/sandbox/package.json b/apps/sandbox/package.json
index a08d4b6097..a09d91d6d5 100644
--- a/apps/sandbox/package.json
+++ b/apps/sandbox/package.json
@@ -14,8 +14,8 @@
"@xds/core": "*",
"@xds/theme-default": "*",
"next": "^15.0.0",
- "react": "^18.3.0",
- "react-dom": "^18.3.0"
+ "react": "^19.2.4",
+ "react-dom": "^19.2.4"
},
"devDependencies": {
"@babel/preset-react": "^7.25.0",
@@ -23,8 +23,8 @@
"@stylexjs/babel-plugin": "^0.17.4",
"@stylexjs/postcss-plugin": "^0.17.4",
"@types/node": "^22.0.0",
- "@types/react": "^18.3.0",
- "@types/react-dom": "^18.3.0",
+ "@types/react": "^19.2.14",
+ "@types/react-dom": "^19.2.3",
"typescript": "^5.7.0"
}
}
diff --git a/apps/storybook/package.json b/apps/storybook/package.json
index b327b461dc..865e718506 100644
--- a/apps/storybook/package.json
+++ b/apps/storybook/package.json
@@ -8,8 +8,8 @@
},
"dependencies": {
"@xds/core": "*",
- "react": "^18.3.0",
- "react-dom": "^18.3.0",
+ "react": "^19.2.4",
+ "react-dom": "^19.2.4",
"@xds/theme-default": "*",
"@xds/theme-neutral": "*"
},
diff --git a/package.json b/package.json
index 71d5345e6a..197e280d9d 100644
--- a/package.json
+++ b/package.json
@@ -35,16 +35,16 @@
"@playwright/test": "^1.48.0",
"@testing-library/user-event": "^14.5.0",
"@types/node": "^22.0.0",
- "@types/react": "^18.3.0",
- "@types/react-dom": "^18.3.0",
+ "@types/react": "^19.2.14",
+ "@types/react-dom": "^19.2.3",
"@vitejs/plugin-react": "^4.3.0",
"@vitest/coverage-v8": "^2.1.0",
"eslint": "^9.15.0",
"husky": "^9.1.7",
"jsdom": "^27.4.0",
"prettier": "^3.4.0",
- "react": "^18.3.0",
- "react-dom": "^18.3.0",
+ "react": "^19.2.4",
+ "react-dom": "^19.2.4",
"tsup": "^8.3.0",
"typescript": "^5.7.0",
"typescript-eslint": "^8.53.1",
diff --git a/packages/core/src/Button/XDSButton.tsx b/packages/core/src/Button/XDSButton.tsx
index bbe5fc2f22..2ed31fe435 100644
--- a/packages/core/src/Button/XDSButton.tsx
+++ b/packages/core/src/Button/XDSButton.tsx
@@ -14,6 +14,7 @@
import {
forwardRef,
useContext,
+ useTransition,
type ButtonHTMLAttributes,
type ReactElement,
type ReactNode,
@@ -218,6 +219,12 @@ export interface XDSButtonProps extends Omit<
* @default false
*/
isLoading?: boolean;
+ /**
+ * Async click action. Shows loading state while pending.
+ */
+ onClickAction?: (
+ e: React.MouseEvent<HTMLButtonElement>,
+ ) => void | Promise<void>;
/**
* Icon element to render in the button.
* If provided without children, button becomes icon-only (square).
@@ -273,6 +280,7 @@ export const XDSButton = forwardRef<HTMLButtonElement, XDSButtonProps>(
size = 'md',
isDisabled = false,
isLoading = false,
+ onClickAction,
icon,
children,
tooltip,
@@ -280,7 +288,9 @@ export const XDSButton = forwardRef<HTMLButtonElement, XDSButtonProps>(
},
ref,
): ReactElement => {
- const buttonDisabled = isDisabled || isLoading;
+ const [isPending, startTransition] = useTransition();
+ const isLoadingState = isLoading || isPending;
+ const buttonDisabled = isDisabled || isLoadingState;
const useLightSpinner = variant === 'primary' || variant === 'destructive';
const isIconOnly = icon != null && children == null;
@@ -289,11 +299,22 @@ export const XDSButton = forwardRef<HTMLButtonElement, XDSButtonProps>(
const themeVariantOverride =
themeContext?.theme.components?.button?.variants?.[variant];
+ const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
+ if (onClickAction) {
+ e.preventDefault();
+ startTransition(async () => {
+ await onClickAction(e);
+ });
+ }
+ props.onClick?.(e);
+ };
+
const button = (
<button
ref={ref}
disabled={buttonDisabled}
aria-label={isIconOnly ? label : undefined}
+ aria-busy={isLoadingState || undefined}
{...stylex.props(
styles.base,
sizeStyles[size],
@@ -301,10 +322,11 @@ export const XDSButton = forwardRef<HTMLButtonElement, XDSButtonProps>(
themeVariantOverride,
isIconOnly && styles.iconOnly,
buttonDisabled && styles.disabled,
- isLoading && loadingStyles.loading,
+ isLoadingState && loadingStyles.loading,
)}
- {...props}>
- {isLoading && (
+ {...props}
+ onClick={handleClick}>
+ {isLoadingState && (
<span
style={{
position: 'absolute',
diff --git a/packages/core/src/CheckboxInput/XDSCheckboxInput.tsx b/packages/core/src/CheckboxInput/XDSCheckboxInput.tsx
index 7ccf76f766..5168f47dc1 100644
--- a/packages/core/src/CheckboxInput/XDSCheckboxInput.tsx
+++ b/packages/core/src/CheckboxInput/XDSCheckboxInput.tsx
@@ -15,6 +15,8 @@ import {
forwardRef,
useContext,
useId,
+ useOptimistic,
+ useTransition,
type ChangeEvent,
type FocusEvent,
} from 'react';
@@ -33,6 +35,7 @@ import type {XDSIconType} from '../Icon';
import type {XDSInputStatus} from '../Field/types';
import {ThemeContext} from '../theme/ThemeContext';
import type {StyleXStyles as ThemeStyleXStyles} from '../theme/types';
+import {XDSSpinner} from '../Spinner';
const styles = stylex.create({
container: {
@@ -220,7 +223,19 @@ export interface XDSCheckboxInputProps {
/**
* Callback fired when the checkbox state changes.
*/
- onChange: (checked: boolean, e: ChangeEvent<HTMLInputElement>) => void;
+ onChange?: (checked: boolean, e: ChangeEvent<HTMLInputElement>) => void;
+ /**
+ * Async action on change. Fires after onChange if not prevented.
+ */
+ onChangeAction?: (
+ checked: boolean,
+ e: ChangeEvent<HTMLInputElement>,
+ ) => void | Promise<void>;
+ /**
+ * Whether the checkbox is in a loading state.
+ * @default false
+ */
+ isLoading?: boolean;
/**
* Whether the checkbox is checked, unchecked, or indeterminate.
*/
@@ -289,6 +304,8 @@ export const XDSCheckboxInput = forwardRef<
isLabelHidden = false,
description,
onChange,
+ onChangeAction,
+ isLoading = false,
value,
isDisabled = false,
isRequired = false,
@@ -309,8 +326,12 @@ export const XDSCheckboxInput = forwardRef<
const descriptionID = useId();
const statusMessageID = useId();
- const isIndeterminate = value === 'indeterminate';
- const isChecked = value === true;
+ const [, startTransition] = useTransition();
+ const [optimisticValue, setOptimisticValue] = useOptimistic(value);
+ const isBusy = isLoading || optimisticValue !== value;
+
+ const isIndeterminate = optimisticValue === 'indeterminate';
+ const isChecked = optimisticValue === true;
const isCheckedOrIndeterminate = isChecked || isIndeterminate;
// Build aria-describedby from description and status message
@@ -338,11 +359,21 @@ export const XDSCheckboxInput = forwardRef<
checked={isChecked}
disabled={isDisabled}
required={isRequired}
- onChange={e => onChange(e.target.checked, e)}
+ onChange={e => {
+ const checked = e.target.checked;
+ onChange?.(checked, e);
+ if (onChangeAction && !e.defaultPrevented) {
+ startTransition(async () => {
+ setOptimisticValue(checked);
+ await onChangeAction(checked, e);
+ });
+ }
+ }}
onFocus={onFocus}
onBlur={onBlur}
aria-describedby={ariaDescribedBy}
aria-invalid={status?.type === 'error' ? true : undefined}
+ aria-busy={isBusy || undefined}
{...stylex.props(
styles.input,
wrapperSizeStyles[size],
@@ -363,29 +394,35 @@ export const XDSCheckboxInput = forwardRef<
!isCheckedOrIndeterminate &&
styles.checkboxDisabledUnchecked,
)}>
- <svg
- viewBox="0 0 10 10"
- {...stylex.props(
- styles.checkmark,
- checkmarkSizeStyles[size],
- isChecked && styles.checkmarkVisible,
- )}>
- <path
- d="M8.5 2.5L4 7.5L1.5 5"
- stroke="currentColor"
- strokeWidth="1.5"
- fill="none"
- strokeLinecap="round"
- strokeLinejoin="round"
- />
- </svg>
- <div
- {...stylex.props(
- styles.indeterminateMark,
- indeterminateSizeStyles[size],
- isIndeterminate && styles.indeterminateMarkVisible,
- )}
- />
+ {isBusy ? (
+ <XDSSpinner size="sm" />
+ ) : (
+ <>
+ <svg
+ viewBox="0 0 10 10"
+ {...stylex.props(
+ styles.checkmark,
+ checkmarkSizeStyles[size],
+ isChecked && styles.checkmarkVisible,
+ )}>
+ <path
+ d="M8.5 2.5L4 7.5L1.5 5"
+ stroke="currentColor"
+ strokeWidth="1.5"
+ fill="none"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ />
+ </svg>
+ <div
+ {...stylex.props(
+ styles.indeterminateMark,
+ indeterminateSizeStyles[size],
+ isIndeterminate && styles.indeterminateMarkVisible,
+ )}
+ />
+ </>
+ )}
</div>
</div>
<div
diff --git a/packages/core/src/DateInput/XDSDateInput.tsx b/packages/core/src/DateInput/XDSDateInput.tsx
index a07532b980..2a4ddcae1c 100644
--- a/packages/core/src/DateInput/XDSDateInput.tsx
+++ b/packages/core/src/DateInput/XDSDateInput.tsx
@@ -19,6 +19,8 @@ import {
useCallback,
useRef,
useMemo,
+ useOptimistic,
+ useTransition,
} from 'react';
import * as stylex from '@stylexjs/stylex';
import type {XDSIconName} from '../Icon';
@@ -35,6 +37,7 @@ import {
} from '../theme/tokens.stylex';
import {XDSField, type XDSInputStatus, type XDSInputStatusType} from '../Field';
import {XDSIcon} from '../Icon';
+import {XDSSpinner} from '../Spinner';
import {
XDSCalendar,
type ISODateString,
@@ -260,7 +263,18 @@ export interface XDSDateInputProps {
* Callback fired when the date changes.
* Called with undefined when input is cleared.
*/
- onChange: (value: ISODateString | undefined) => void;
+ onChange?: (value: ISODateString | undefined) => void;
+
+ /**
+ * Async action on change. Fires after onChange.
+ */
+ onChangeAction?: (value: ISODateString | undefined) => void | Promise<void>;
+
+ /**
+ * Whether the input is in a loading state.
+ * @default false
+ */
+ isLoading?: boolean;
/**
* Minimum selectable date in ISO format.
@@ -328,6 +342,8 @@ export const XDSDateInput = forwardRef<HTMLInputElement, XDSDateInputProps>(
isDisabled = false,
value,
onChange,
+ onChangeAction,
+ isLoading = false,
min,
max,
dateConstraints,
@@ -348,6 +364,10 @@ export const XDSDateInput = forwardRef<HTMLInputElement, XDSDateInputProps>(
const inputRef = useRef<HTMLInputElement | null>(null);
const calendarRef = useRef<XDSCalendarHandle | null>(null);
+ const [, startTransition] = useTransition();
+ const [optimisticValue, setOptimisticValue] = useOptimistic(value);
+ const isBusy = isLoading || optimisticValue !== value;
+
// Status icon mapping
const statusIconMap: Record<XDSInputStatusType, XDSIconName> = {
warning: 'warning',
@@ -380,8 +400,8 @@ export const XDSDateInput = forwardRef<HTMLInputElement, XDSDateInputProps>(
if (pendingInput !== null) {
return pendingInput;
}
- return value ? formatDisplayDate(value) : '';
- }, [pendingInput, value]);
+ return optimisticValue ? formatDisplayDate(optimisticValue) : '';
+ }, [pendingInput, optimisticValue]);
// Check if current input is valid (for styling purposes)
const isInputValid = useMemo(() => {
@@ -417,14 +437,28 @@ export const XDSDateInput = forwardRef<HTMLInputElement, XDSDateInputProps>(
}
}, [isDisabled, popover]);
+ // Unified change handler that fires both onChange and onChangeAction
+ const fireChange = useCallback(
+ (newValue: ISODateString | undefined) => {
+ onChange?.(newValue);
+ if (onChangeAction) {
+ startTransition(async () => {
+ setOptimisticValue(newValue);
+ await onChangeAction(newValue);
+ });
+ }
+ },
+ [onChange, onChangeAction, startTransition, setOptimisticValue],
+ );
+
// Handle date selection from calendar
const handleDateSelect = useCallback(
(selectedDate: ISODateString) => {
- onChange(selectedDate);
+ fireChange(selectedDate);
setPendingInput(null);
popover.hide();
},
- [onChange, popover],
+ [fireChange, popover],
);
// Handle input text change - update immediately if valid
@@ -436,12 +470,12 @@ export const XDSDateInput = forwardRef<HTMLInputElement, XDSDateInputProps>(
// If the input is valid, update immediately (don't wait for blur)
const parsed = parseDateInput(newValue);
if (parsed && parsed !== value) {
- onChange(parsed);
+ fireChange(parsed);
// Navigate calendar to show the parsed date's month
calendarRef.current?.navigateTo(parsed);
}
},
- [value, onChange],
+ [value, fireChange],
);
// Handle blur - validate and clear pending input
@@ -453,7 +487,7 @@ export const XDSDateInput = forwardRef<HTMLInputElement, XDSDateInputProps>(
if (!pendingInput.trim()) {
// Empty input clears the value
if (value !== undefined) {
- onChange(undefined);
+ fireChange(undefined);
}
setPendingInput(null);
return;
@@ -463,12 +497,12 @@ export const XDSDateInput = forwardRef<HTMLInputElement, XDSDateInputProps>(
if (parsed) {
// Valid date - update if different
if (parsed !== value) {
- onChange(parsed);
+ fireChange(parsed);
}
}
// Clear pending input - display will revert to formatted value
setPendingInput(null);
- }, [pendingInput, value, onChange]);
+ }, [pendingInput, value, fireChange]);
// Handle keyboard events on input
const handleInputKeyDown = useCallback(
@@ -549,6 +583,7 @@ export const XDSDateInput = forwardRef<HTMLInputElement, XDSDateInputProps>(
aria-describedby={ariaDescribedBy}
aria-required={isRequired === true ? 'true' : undefined}
aria-invalid={status?.type === 'error' ? 'true' : undefined}
+ aria-busy={isBusy || undefined}
{...stylex.props(
styles.input,
isDisabled && styles.inputDisabled,
@@ -556,6 +591,7 @@ export const XDSDateInput = forwardRef<HTMLInputElement, XDSDateInputProps>(
inputOverride,
)}
/>
+ {isBusy && <XDSSpinner size="sm" />}
{status && (
<XDSIcon
icon={statusIconMap[status.type]}
diff --git a/packages/core/src/Layer/XDSTooltip.tsx b/packages/core/src/Layer/XDSTooltip.tsx
index 6e4cc72031..972ba7f4d5 100644
--- a/packages/core/src/Layer/XDSTooltip.tsx
+++ b/packages/core/src/Layer/XDSTooltip.tsx
@@ -55,7 +55,7 @@ export interface XDSTooltipProps {
* instead of wrapping children. This enables sibling-mode rendering,
* useful for lazy-loaded tooltips that shouldn't remount children.
*/
- anchorRef?: React.RefObject<HTMLElement>;
+ anchorRef?: React.RefObject<HTMLElement | null>;
/**
* Content to display in the tooltip.
diff --git a/packages/core/src/Selector/XDSSelector.tsx b/packages/core/src/Selector/XDSSelector.tsx
index d63f450758..58bddab0ac 100644
--- a/packages/core/src/Selector/XDSSelector.tsx
+++ b/packages/core/src/Selector/XDSSelector.tsx
@@ -13,7 +13,9 @@ import React, {
useCallback,
useId,
useMemo,
+ useOptimistic,
useRef,
+ useTransition,
type ReactNode,
} from 'react';
import * as stylex from '@stylexjs/stylex';
@@ -22,6 +24,7 @@ import {XDSIcon} from '../Icon';
import type {XDSIconName} from '../Icon';
import {XDSField} from '../Field';
import {XDSDivider} from '../Divider';
+import {XDSSpinner} from '../Spinner';
import {
colorVars,
sizeVars,
@@ -352,6 +355,17 @@ export interface XDSSelectorProps<
*/
onChange?: (value: string) => void;
+ /**
+ * Async action on change. Fires after onChange.
+ */
+ onChangeAction?: (value: string) => void | Promise<void>;
+
+ /**
+ * Whether the selector is in a loading state.
+ * @default false
+ */
+ isLoading?: boolean;
+
/**
* Placeholder text when no value is selected.
* @default 'Select...'
@@ -421,6 +435,8 @@ export function XDSSelector<T extends XDSSelectorOption>({
items,
value,
onChange,
+ onChangeAction,
+ isLoading = false,
placeholder = 'Select...',
size = 'md',
status,
@@ -438,6 +454,10 @@ export function XDSSelector<T extends XDSSelectorOption>({
const statusMessageId = useId();
const triggerRef = useRef<HTMLButtonElement>(null);
+ const [, startTransition] = useTransition();
+ const [optimisticValue, setOptimisticValue] = useOptimistic(value);
+ const isBusy = isLoading || optimisticValue !== value;
+
// Build aria-describedby
const ariaDescribedBy =
[
@@ -452,8 +472,8 @@ export function XDSSelector<T extends XDSSelectorOption>({
// Find selected item and its index for positioning
const selectedItemIndex = useMemo(() => {
- return selectableItems.findIndex(item => item.value === value);
- }, [selectableItems, value]);
+ return selectableItems.findIndex(item => item.value === optimisticValue);
+ }, [selectableItems, optimisticValue]);
const selectedItem = useMemo(() => {
return selectedItemIndex >= 0
@@ -498,7 +518,18 @@ export function XDSSelector<T extends XDSSelectorOption>({
isOpen: layer.isOpen,
onOpen: layer.show,
onClose: layer.hide,
- onSelect: onChange,
+ onSelect: useCallback(
+ (newValue: string) => {
+ onChange?.(newValue);
+ if (onChangeAction) {
+ startTransition(async () => {
+ setOptimisticValue(newValue);
+ await onChangeAction(newValue);
+ });
+ }
+ },
+ [onChange, onChangeAction, startTransition, setOptimisticValue],
+ ),
listboxId,
});
@@ -622,6 +653,7 @@ export function XDSSelector<T extends XDSSelectorOption>({
aria-describedby={ariaDescribedBy}
aria-required={isRequired ? 'true' : undefined}
aria-invalid={status?.type === 'error' ? 'true' : undefined}
+ aria-busy={isBusy || undefined}
disabled={isDisabled}
onClick={onTriggerClick}
onKeyDown={onKeyDown}
@@ -636,6 +668,7 @@ export function XDSSelector<T extends XDSSelectorOption>({
triggerOverride,
)}>
<span>{selectedItem?.label ?? placeholder}</span>
+ {isBusy && <XDSSpinner size="sm" />}
<span
{...stylex.props(
styles.triggerIcon,
diff --git a/packages/core/src/Selector/hooks.ts b/packages/core/src/Selector/hooks.ts
index 97ffa5efe2..4d7b86de58 100644
--- a/packages/core/src/Selector/hooks.ts
+++ b/packages/core/src/Selector/hooks.ts
@@ -16,8 +16,8 @@ interface UseSelectedItemOffsetOptions {
isOpen: boolean;
selectedItemIndex: number;
listboxId: string;
- listboxRef: RefObject<HTMLDivElement>;
- triggerRef: RefObject<HTMLButtonElement>;
+ listboxRef: RefObject<HTMLDivElement | null>;
+ triggerRef: RefObject<HTMLButtonElement | null>;
}
interface UseSelectedItemOffsetResult {
@@ -128,7 +128,9 @@ export function useCombobox({
}: UseComboboxOptions): UseComboboxResult {
const [highlightedIndex, setHighlightedIndex] = useState<number>(-1);
const [typeahead, setTypeahead] = useState('');
- const typeaheadTimeoutRef = useRef<ReturnType<typeof setTimeout>>();
+ const typeaheadTimeoutRef = useRef<ReturnType<typeof setTimeout> | undefined>(
+ undefined,
+ );
const getItemId = useCallback(
(index: number) => `${listboxId}-item-${index}`,
diff --git a/packages/core/src/Switch/XDSSwitch.tsx b/packages/core/src/Switch/XDSSwitch.tsx
index 35be724253..9b92c481a8 100644
--- a/packages/core/src/Switch/XDSSwitch.tsx
+++ b/packages/core/src/Switch/XDSSwitch.tsx
@@ -15,6 +15,8 @@ import {
forwardRef,
useContext,
useId,
+ useOptimistic,
+ useTransition,
type ChangeEvent,
type FocusEvent,
} from 'react';
@@ -34,6 +36,7 @@ import type {XDSIconType} from '../Icon';
import type {XDSInputStatus} from '../Field/types';
import {ThemeContext} from '../theme/ThemeContext';
import type {StyleXStyles as ThemeStyleXStyles} from '../theme/types';
+import {XDSSpinner} from '../Spinner';
// Fixed dimensions: 40px width, 24px height, 16px thumb (off), 20px thumb (on)
const SWITCH_WIDTH = 40;
@@ -192,7 +195,19 @@ export interface XDSSwitchProps {
/**
* Callback fired when the switch state changes.
*/
- onChange: (checked: boolean, e: ChangeEvent<HTMLInputElement>) => void;
+ onChange?: (checked: boolean, e: ChangeEvent<HTMLInputElement>) => void;
+ /**
+ * Async action on change. Fires after onChange if not prevented.
+ */
+ onChangeAction?: (
+ checked: boolean,
+ e: ChangeEvent<HTMLInputElement>,
+ ) => void | Promise<void>;
+ /**
+ * Whether the switch is in a loading state.
+ * @default false
+ */
+ isLoading?: boolean;
/**
* Whether the switch is on or off.
*/
@@ -274,6 +289,8 @@ export const XDSSwitch = forwardRef<HTMLInputElement, XDSSwitchProps>(
isLabelHidden = false,
description,
onChange,
+ onChangeAction,
+ isLoading = false,
value,
isDisabled = false,
isOptional = false,
@@ -297,7 +314,11 @@ export const XDSSwitch = forwardRef<HTMLInputElement, XDSSwitchProps>(
const descriptionID = useId();
const statusMessageID = useId();
- const isOn = value === true;
+ const [, startTransition] = useTransition();
+ const [optimisticValue, setOptimisticValue] = useOptimistic(value);
+ const isBusy = isLoading || optimisticValue !== value;
+
+ const isOn = optimisticValue === true;
// Build aria-describedby from description and status message
const describedByParts: string[] = [];
@@ -316,11 +337,21 @@ export const XDSSwitch = forwardRef<HTMLInputElement, XDSSwitchProps>(
checked={isOn}
disabled={isDisabled}
required={isRequired}
- onChange={e => onChange(e.target.checked, e)}
+ onChange={e => {
+ const checked = e.target.checked;
+ onChange?.(checked, e);
+ if (onChangeAction && !e.defaultPrevented) {
+ startTransition(async () => {
+ setOptimisticValue(checked);
+ await onChangeAction(checked, e);
+ });
+ }
+ }}
onFocus={onFocus}
onBlur={onBlur}
aria-describedby={ariaDescribedBy}
aria-invalid={status?.type === 'error' ? true : undefined}
+ aria-busy={isBusy || undefined}
{...stylex.props(styles.input, isDisabled && styles.inputDisabled)}
/>
<div
@@ -338,7 +369,13 @@ export const XDSSwitch = forwardRef<HTMLInputElement, XDSSwitchProps>(
isOn ? styles.thumbOn : styles.thumbOff,
thumbOverride,
)}
- />
+ style={{
+ display: 'flex',
+ alignItems: 'center',
+ justifyContent: 'center',
+ }}>
+ {isBusy && <XDSSpinner size="sm" />}
+ </div>
</div>
</div>
);
diff --git a/packages/core/src/TextArea/XDSTextArea.tsx b/packages/core/src/TextArea/XDSTextArea.tsx
index c4545b4ce6..3548d358f9 100644
--- a/packages/core/src/TextArea/XDSTextArea.tsx
+++ b/packages/core/src/TextArea/XDSTextArea.tsx
@@ -15,6 +15,8 @@ import {
forwardRef,
useContext,
useId,
+ useOptimistic,
+ useTransition,
type ChangeEvent,
type ClipboardEvent,
} from 'react';
@@ -32,6 +34,7 @@ import {
} from '../theme/tokens.stylex';
import {XDSField} from '../Field';
import {XDSIcon, type XDSIconType} from '../Icon';
+import {XDSSpinner} from '../Spinner';
import {ThemeContext} from '../theme/ThemeContext';
import type {StyleXStyles as ThemeStyleXStyles} from '../theme/types';
@@ -212,7 +215,11 @@ export interface XDSTextAreaProps {
/**
* Callback fired when the textarea value changes.
*/
- onChange: (value: string, e: ChangeEvent<HTMLTextAreaElement>) => void;
+ onChange?: (value: string, e: ChangeEvent<HTMLTextAreaElement>) => void;
+ /** Async action on change. Fires after onChange if not prevented. */
+ onChangeAction?: (value: string, e: ChangeEvent<HTMLTextAreaElement>) => void | Promise<void>;
+ /** Whether the input is in a loading state. @default false */
+ isLoading?: boolean;
/**
* The current value of the textarea.
*/
@@ -290,6 +297,8 @@ export const XDSTextArea = forwardRef<HTMLTextAreaElement, XDSTextAreaProps>(
isOptional = false,
isRequired = false,
onChange,
+ onChangeAction,
+ isLoading = false,
value,
placeholder,
rows = 3,
@@ -313,6 +322,10 @@ export const XDSTextArea = forwardRef<HTMLTextAreaElement, XDSTextAreaProps>(
const descriptionID = useId();
const statusMessageID = useId();
+ const [, startTransition] = useTransition();
+ const [optimisticValue, setOptimisticValue] = useOptimistic(value);
+ const isBusy = isLoading || optimisticValue !== value;
+
const statusIconMap: Record<XDSTextAreaStatusType, XDSIconName> = {
warning: 'warning',
error: 'xCircle',
@@ -336,6 +349,17 @@ export const XDSTextArea = forwardRef<HTMLTextAreaElement, XDSTextAreaProps>(
.filter(Boolean)
.join(' ') || undefined;
+ const handleChange = (e: ChangeEvent<HTMLTextAreaElement>) => {
+ const newValue = e.target.value;
+ onChange?.(newValue, e);
+ if (onChangeAction && !e.defaultPrevented) {
+ startTransition(async () => {
+ setOptimisticValue(newValue);
+ await onChangeAction(newValue, e);
+ });
+ }
+ };
+
return (
<XDSField
label={label}
@@ -369,8 +393,8 @@ export const XDSTextArea = forwardRef<HTMLTextAreaElement, XDSTextAreaProps>(
ref={ref}
id={id}
name={htmlName}
- value={value}
- onChange={e => onChange(e.target.value, e)}
+ value={String(optimisticValue)}
+ onChange={handleChange}
onPaste={onPaste}
placeholder={placeholder}
rows={rows}
@@ -381,12 +405,14 @@ export const XDSTextArea = forwardRef<HTMLTextAreaElement, XDSTextAreaProps>(
aria-describedby={ariaDescribedBy}
aria-required={isRequired === true ? 'true' : undefined}
aria-invalid={status?.type === 'error' ? 'true' : undefined}
+ aria-busy={isBusy || undefined}
{...stylex.props(
styles.textarea,
isDisabled && styles.textareaDisabled,
textareaOverride,
)}
/>
+ {isBusy && <XDSSpinner size="sm" />}
{status && (
<XDSIcon
icon={statusIconMap[status.type]}
diff --git a/packages/core/src/TextInput/XDSTextInput.tsx b/packages/core/src/TextInput/XDSTextInput.tsx
index e6c0dfffbf..b5f8819ffb 100644
--- a/packages/core/src/TextInput/XDSTextInput.tsx
+++ b/packages/core/src/TextInput/XDSTextInput.tsx
@@ -11,7 +11,7 @@
* - /apps/storybook/stories/TextInput.stories.tsx (storybook stories)
*/
-import {forwardRef, useContext, useId, type ChangeEvent} from 'react';
+import {forwardRef, useContext, useId, useOptimistic, useTransition, type ChangeEvent} from 'react';
import * as stylex from '@stylexjs/stylex';
import type {XDSIconName} from '../Icon';
import {
@@ -27,6 +27,7 @@ import {
} from '../theme/tokens.stylex';
import {XDSField, type XDSInputStatus, type XDSInputStatusType} from '../Field';
import {XDSIcon, type XDSIconType} from '../Icon';
+import {XDSSpinner} from '../Spinner';
const styles = stylex.create({
wrapper: {
@@ -225,7 +226,11 @@ export interface XDSTextInputProps {
/**
* Callback fired when the input value changes.
*/
- onChange: (value: string, e: ChangeEvent<HTMLInputElement>) => void;
+ onChange?: (value: string, e: ChangeEvent<HTMLInputElement>) => void;
+ /** Async action on change. Fires after onChange if not prevented. */
+ onChangeAction?: (value: string, e: ChangeEvent<HTMLInputElement>) => void | Promise<void>;
+ /** Whether the input is in a loading state. @default false */
+ isLoading?: boolean;
/**
* The current value of the input.
*/
@@ -272,6 +277,8 @@ export const XDSTextInput = forwardRef<HTMLInputElement, XDSTextInputProps>(
status,
size = 'md',
onChange,
+ onChangeAction,
+ isLoading = false,
value,
placeholder,
labelTooltip,
@@ -288,6 +295,10 @@ export const XDSTextInput = forwardRef<HTMLInputElement, XDSTextInputProps>(
const descriptionID = useId();
const statusMessageID = useId();
+ const [, startTransition] = useTransition();
+ const [optimisticValue, setOptimisticValue] = useOptimistic(value);
+ const isBusy = isLoading || optimisticValue !== value;
+
const statusIconMap: Record<XDSInputStatusType, XDSIconName> = {
warning: 'warning',
error: 'xCircle',
@@ -311,6 +322,17 @@ export const XDSTextInput = forwardRef<HTMLInputElement, XDSTextInputProps>(
.filter(Boolean)
.join(' ') || undefined;
+ const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
+ const newValue = e.target.value;
+ onChange?.(newValue, e);
+ if (onChangeAction && !e.defaultPrevented) {
+ startTransition(async () => {
+ setOptimisticValue(newValue);
+ await onChangeAction(newValue, e);
+ });
+ }
+ };
+
return (
<XDSField
label={label}
@@ -346,20 +368,22 @@ export const XDSTextInput = forwardRef<HTMLInputElement, XDSTextInputProps>(
id={id}
name={htmlName}
type="text"
- value={value}
- onChange={e => onChange(e.target.value, e)}
+ value={String(optimisticValue)}
+ onChange={handleChange}
placeholder={placeholder}
disabled={isDisabled}
autoFocus={hasAutoFocus}
aria-describedby={ariaDescribedBy}
aria-required={isRequired === true ? 'true' : undefined}
aria-invalid={status?.type === 'error' ? 'true' : undefined}
+ aria-busy={isBusy || undefined}
{...stylex.props(
styles.input,
isDisabled && styles.inputDisabled,
inputOverride,
)}
/>
+ {isBusy && <XDSSpinner size="sm" />}
{status && (
<XDSIcon
icon={statusIconMap[status.type]}
diff --git a/packages/core/src/TimeInput/XDSTimeInput.tsx b/packages/core/src/TimeInput/XDSTimeInput.tsx
index be2f01bdfd..37246aab4c 100644
--- a/packages/core/src/TimeInput/XDSTimeInput.tsx
+++ b/packages/core/src/TimeInput/XDSTimeInput.tsx
@@ -19,6 +19,8 @@ import {
useCallback,
useRef,
useMemo,
+ useOptimistic,
+ useTransition,
type KeyboardEvent,
type FocusEvent,
} from 'react';
@@ -37,6 +39,7 @@ import {
} from '../theme/tokens.stylex';
import {XDSField, type XDSInputStatus, type XDSInputStatusType} from '../Field';
import {XDSIcon} from '../Icon';
+import {XDSSpinner} from '../Spinner';
import {
type ISOTimeString,
parseTimeInput,
@@ -263,7 +266,18 @@ export interface XDSTimeInputProps {
* Callback fired when the time changes.
* Called with undefined when input is cleared.
*/
- onChange: (value: ISOTimeString | undefined) => void;
+ onChange?: (value: ISOTimeString | undefined) => void;
+
+ /**
+ * Async action on change. Fires after onChange.
+ */
+ onChangeAction?: (value: ISOTimeString | undefined) => void | Promise<void>;
+
+ /**
+ * Whether the input is in a loading state.
+ * @default false
+ */
+ isLoading?: boolean;
/**
* Minimum selectable time in ISO format.
@@ -348,6 +362,8 @@ export const XDSTimeInput = forwardRef<HTMLInputElement, XDSTimeInputProps>(
isDisabled = false,
value,
onChange,
+ onChangeAction,
+ isLoading = false,
min,
max,
hasSeconds = false,
@@ -369,6 +385,10 @@ export const XDSTimeInput = forwardRef<HTMLInputElement, XDSTimeInputProps>(
const statusMessageID = useId();
const inputRef = useRef<HTMLInputElement | null>(null);
+ const [, startTransition] = useTransition();
+ const [optimisticValue, setOptimisticValue] = useOptimistic(value);
+ const isBusy = isLoading || optimisticValue !== value;
+
// Status icon mapping
const statusIconMap: Record<XDSInputStatusType, XDSIconName> = {
warning: 'warning',
@@ -401,12 +421,28 @@ export const XDSTimeInput = forwardRef<HTMLInputElement, XDSTimeInputProps>(
const formatDisplayTime =
hourFormat === '12h' ? formatDisplayTime12h : formatDisplayTime24h;
+ // Unified change handler that fires both onChange and onChangeAction
+ const fireChange = useCallback(
+ (newValue: ISOTimeString | undefined) => {
+ onChange?.(newValue);
+ if (onChangeAction) {
+ startTransition(async () => {
+ setOptimisticValue(newValue);
+ await onChangeAction(newValue);
+ });
+ }
+ },
+ [onChange, onChangeAction, startTransition, setOptimisticValue],
+ );
+
// Display value: pending input if typing, otherwise formatted value
const displayValue = useMemo(() => {
if (pendingInput !== null) {
return pendingInput;
}
- return value ? formatDisplayTime(value, hasSeconds) : '';
+ return optimisticValue
+ ? formatDisplayTime(optimisticValue, hasSeconds)
+ : '';
}, [pendingInput, value, formatDisplayTime, hasSeconds]);
// Check if current input is valid (for styling purposes)
@@ -440,10 +476,10 @@ export const XDSTimeInput = forwardRef<HTMLInputElement, XDSTimeInputProps>(
// If the input is valid, update immediately (don't wait for blur)
const parsed = parseTimeInput(newValue, hasSeconds);
if (parsed && isTimeInRange(parsed, min, max) && parsed !== value) {
- onChange(parsed);
+ fireChange(parsed);
}
},
- [hasSeconds, min, max, value, onChange],
+ [hasSeconds, min, max, value, fireChange],
);
// Handle focus
@@ -463,7 +499,7 @@ export const XDSTimeInput = forwardRef<HTMLInputElement, XDSTimeInputProps>(
if (!pendingInput.trim()) {
// Empty input clears the value
if (value !== undefined) {
- onChange(undefined);
+ fireChange(undefined);
}
setPendingInput(null);
return;
@@ -473,13 +509,13 @@ export const XDSTimeInput = forwardRef<HTMLInputElement, XDSTimeInputProps>(
if (parsed && isTimeInRange(parsed, min, max)) {
// Valid time - update if different
if (parsed !== value) {
- onChange(parsed);
+ fireChange(parsed);
}
}
// Clear pending input - display will revert to formatted value
setPendingInput(null);
},
- [pendingInput, value, onChange, hasSeconds, min, max],
+ [pendingInput, value, fireChange, hasSeconds, min, max],
);
// Handle keyboard navigation on input
@@ -507,18 +543,18 @@ export const XDSTimeInput = forwardRef<HTMLInputElement, XDSTimeInputProps>(
// Check if within range
if (isTimeInRange(newTime, min, max)) {
- onChange(newTime);
+ fireChange(newTime);
}
}
},
- [value, hasSeconds, increment, min, max, onChange],
+ [value, hasSeconds, increment, min, max, fireChange],
);
// Handle clear button click
const handleClear = useCallback(() => {
- onChange(undefined);
+ fireChange(undefined);
inputRef.current?.focus();
- }, [onChange]);
+ }, [fireChange]);
// Combine refs
const setRefs = useCallback(
@@ -578,6 +614,7 @@ export const XDSTimeInput = forwardRef<HTMLInputElement, XDSTimeInputProps>(
aria-describedby={ariaDescribedBy}
aria-required={isRequired === true ? 'true' : undefined}
aria-invalid={status?.type === 'error' ? 'true' : undefined}
+ aria-busy={isBusy || undefined}
{...stylex.props(
styles.input,
isDisabled && styles.inputDisabled,
@@ -585,6 +622,7 @@ export const XDSTimeInput = forwardRef<HTMLInputElement, XDSTimeInputProps>(
inputOverride,
)}
/>
+ {isBusy && <XDSSpinner size="sm" />}
{hasClear && value && !isDisabled && (
<button
type="button"
diff --git a/packages/core/src/hooks/useGridFocus.ts b/packages/core/src/hooks/useGridFocus.ts
index a7c64204db..b7d1adffb5 100644
--- a/packages/core/src/hooks/useGridFocus.ts
+++ b/packages/core/src/hooks/useGridFocus.ts
@@ -61,7 +61,7 @@ export interface UseGridFocusReturn {
/**
* Ref to attach to the grid container element.
*/
- gridRef: React.RefObject<HTMLElement>;
+ gridRef: React.RefObject<HTMLElement | null>;
/**
* Key down handler to attach to the grid container.
diff --git a/packages/core/src/hooks/useListFocus.ts b/packages/core/src/hooks/useListFocus.ts
index 7ae8f0a47b..fe09784d3a 100644
--- a/packages/core/src/hooks/useListFocus.ts
+++ b/packages/core/src/hooks/useListFocus.ts
@@ -39,7 +39,7 @@ export interface UseListFocusReturn {
/**
* Ref to attach to the list container element.
*/
- listRef: React.RefObject<HTMLElement>;
+ listRef: React.RefObject<HTMLElement | null>;
/**
* Key down handler to attach to the list container.
diff --git a/yarn.lock b/yarn.lock
index 27d5f72bd0..6c52b23271 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2186,22 +2186,16 @@
dependencies:
undici-types "~6.21.0"
-"@types/prop-types@*":
- version "15.7.15"
- resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.15.tgz#e6e5a86d602beaca71ce5163fadf5f95d70931c7"
- integrity sha512-F6bEyamV9jKGAFBEmlQnesRPGOQqS2+Uwi0Em15xenOxHaf2hv6L8YCVn3rPdPJOiJfPiCnLIRyvwVaqMY3MIw==
-
-"@types/react-dom@^18.3.0":
- version "18.3.7"
- resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-18.3.7.tgz#b89ddf2cd83b4feafcc4e2ea41afdfb95a0d194f"
- integrity sha512-MEe3UeoENYVFXzoXEWsvcpg6ZvlrFNlOQ7EOsvhI3CfAXwzPfO8Qwuxd40nepsYKqyyVQnTdEfv68q91yLcKrQ==
-
-"@types/react@^18.3.0":
- version "18.3.27"
- resolved "https://registry.yarnpkg.com/@types/react/-/react-18.3.27.tgz#74a3b590ea183983dc65a474dc17553ae1415c34"
- integrity sha512-cisd7gxkzjBKU2GgdYrTdtQx1SORymWyaAFhaxQPK9bYO9ot3Y5OikQRvY0VYQtvwjeQnizCINJAenh/V7MK2w==
- dependencies:
- "@types/prop-types" "*"
+"@types/react-dom@^19.2.3":
+ version "19.2.3"
+ resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-19.2.3.tgz#c1e305d15a52a3e508d54dca770d202cb63abf2c"
+ integrity sha512-jp2L/eY6fn+KgVVQAOqYItbF0VY/YApe5Mz2F0aykSO8gx31bYCZyvSeYxCHKvzHG5eZjc+zyaS5BrBWya2+kQ==
+
+"@types/react@^19.2.14":
+ version "19.2.14"
+ resolved "https://registry.yarnpkg.com/@types/react/-/react-19.2.14.tgz#39604929b5e3957e3a6fa0001dafb17c7af70bad"
+ integrity sha512-ilcTH/UniCkMdtexkoCN0bI7pMcJDvmQFPvuPvmEaYA/NSfFTAgdUSLAoVjaRJm7+6PvcM+q1zYOwS4wTYMF9w==
+ dependencies:
csstype "^3.2.2"
"@types/resolve@^1.20.2":
@@ -3946,7 +3940,7 @@ lodash@^4.17.21:
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
-loose-envify@^1.0.0, loose-envify@^1.1.0:
+loose-envify@^1.0.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
@@ -4446,13 +4440,12 @@ react-docgen@^7.0.0:
dependencies:
scheduler "^0.27.0"
-react-dom@^18.3.0:
- version "18.3.1"
- resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.3.1.tgz#c2265d79511b57d479b3dd3fdfa51536494c5cb4"
- integrity sha512-5m4nQKp+rZRb09LNH59GM4BxTh9251/ylbKIbpe7TpGxfJ+9kv6BLkLBXIjjspbgbnIBNqlI23tRnTWT0snUIw==
+react-dom@^19.2.4:
+ version "19.2.4"
+ resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-19.2.4.tgz#6fac6bd96f7db477d966c7ec17c1a2b1ad8e6591"
+ integrity sha512-AXJdLo8kgMbimY95O2aKQqsz2iWi9jMgKJhRBAxECE4IFxfcazB2LmzloIoibJI3C12IlY20+KFaLv+71bUJeQ==
dependencies:
- loose-envify "^1.1.0"
- scheduler "^0.23.2"
+ scheduler "^0.27.0"
react-is@^17.0.1:
version "17.0.2"
@@ -4469,12 +4462,10 @@ react-refresh@^0.17.0:
resolved "https://registry.yarnpkg.com/react/-/react-19.2.3.tgz#d83e5e8e7a258cf6b4fe28640515f99b87cd19b8"
integrity sha512-Ku/hhYbVjOQnXDZFv2+RibmLFGwFdeeKHFcOTlrt7xplBnya5OGn/hIRDsqDiSUcfORsDC7MPxwork8jBwsIWA==
-react@^18.3.0:
- version "18.3.1"
- resolved "https://registry.yarnpkg.com/react/-/react-18.3.1.tgz#49ab892009c53933625bd16b2533fc754cab2891"
- integrity sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==
- dependencies:
- loose-envify "^1.1.0"
+react@^19.2.4:
+ version "19.2.4"
+ resolved "https://registry.yarnpkg.com/react/-/react-19.2.4.tgz#438e57baa19b77cb23aab516cf635cd0579ee09a"
+ integrity sha512-9nfp2hYpCwOjAN+8TZFGhtWEwgvWHXqESH8qT89AT/lWklpLON22Lc8pEtnpsZz7VmawabSU0gCjnj8aC0euHQ==
read-yaml-file@^1.1.0:
version "1.1.0"
@@ -4606,13 +4597,6 @@ saxes@^6.0.0:
dependencies:
xmlchars "^2.2.0"
-scheduler@^0.23.2:
- version "0.23.2"
- resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.23.2.tgz#414ba64a3b282892e944cf2108ecc078d115cdc3"
- integrity sha512-UOShsPwz7NrMUqhR6t0hWjFduvOzbtv7toDH1/hIrfRNIDBnnBWd0CwJTGvTpngVlmwGCdP9/Zl/tVrDqcuYzQ==
- dependencies:
- loose-envify "^1.1.0"
-
scheduler@^0.27.0:
version "0.27.0"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.27.0.tgz#0c4ef82d67d1e5c1e359e8fc76d3a87f045fe5bd"