Shared TypeScript type definitions for React Spectrum components and hooks, providing common interfaces for DOM interactions, styling, accessibility, internationalization, and component behavior across the React Spectrum ecosystem
—
Essential DOM properties and ARIA attributes for building accessible web components with comprehensive event handling and element attribute management.
Core DOM properties that can be applied to any component.
/**
* Basic DOM properties available on all components
*/
interface DOMProps {
/** The element's unique identifier */
id?: string;
}
/**
* DOM properties for focusable elements
*/
interface FocusableDOMProps extends DOMProps {
/**
* Whether to exclude the element from the sequential tab order.
* Should be avoided except when alternative keyboard access is available.
*/
excludeFromTabOrder?: boolean;
}ARIA attributes for labeling and describing elements for screen readers.
/**
* ARIA labeling properties for accessibility
*/
interface AriaLabelingProps {
/** Defines a string value that labels the current element */
"aria-label"?: string;
/** Identifies the element (or elements) that labels the current element */
"aria-labelledby"?: string;
/** Identifies the element (or elements) that describes the object */
"aria-describedby"?: string;
/** Identifies the element (or elements) that provide detailed description */
"aria-details"?: string;
}
/**
* ARIA validation properties
*/
interface AriaValidationProps {
/** Identifies the element that provides an error message for the object */
"aria-errormessage"?: string;
}Comprehensive DOM properties specifically for text input elements.
/**
* DOM properties for input elements
*/
interface InputDOMProps {
/** The name of the input element, used when submitting an HTML form */
name?: string;
/** The form element to associate the input with */
form?: string;
}
/**
* Complete DOM properties for text input elements
*/
interface TextInputDOMProps extends DOMProps, InputDOMProps, TextInputDOMEvents {
/** Describes the type of autocomplete functionality the input should provide */
autoComplete?: string;
/** The maximum number of characters supported by the input */
maxLength?: number;
/** The minimum number of characters required by the input */
minLength?: number;
/** Regex pattern that the value of the input must match to be valid */
pattern?: string;
/** Content that appears in the input when it is empty */
placeholder?: string;
/** The type of input to render */
type?: "text" | "search" | "url" | "tel" | "email" | "password" | (string & {});
/** Hints at the type of data that might be entered by the user */
inputMode?: "none" | "text" | "tel" | "url" | "email" | "numeric" | "decimal" | "search";
/** Whether to enable autocorrection */
autoCorrect?: string;
/** Whether the element may be checked for spelling errors */
spellCheck?: string;
}DOM events specific to text input elements.
/**
* DOM events for text input elements
*/
interface TextInputDOMEvents {
// Clipboard events
/** Handler that is called when the user copies text */
onCopy?: ClipboardEventHandler<HTMLInputElement>;
/** Handler that is called when the user cuts text */
onCut?: ClipboardEventHandler<HTMLInputElement>;
/** Handler that is called when the user pastes text */
onPaste?: ClipboardEventHandler<HTMLInputElement>;
// Composition events
/** Handler that is called when a text composition system starts */
onCompositionStart?: CompositionEventHandler<HTMLInputElement>;
/** Handler that is called when a text composition system completes */
onCompositionEnd?: CompositionEventHandler<HTMLInputElement>;
/** Handler that is called when a new character is received in composition */
onCompositionUpdate?: CompositionEventHandler<HTMLInputElement>;
// Selection events
/** Handler that is called when text in the input is selected */
onSelect?: ReactEventHandler<HTMLInputElement>;
// Input events
/** Handler that is called when the input value is about to be modified */
onBeforeInput?: FormEventHandler<HTMLInputElement>;
/** Handler that is called when the input value is modified */
onInput?: FormEventHandler<HTMLInputElement>;
}DOM properties for link elements with router support.
/**
* DOM properties for link elements
*/
interface LinkDOMProps {
/** A URL to link to */
href?: Href;
/** Hints at the human language of the linked URL */
hrefLang?: string;
/** The target window for the link */
target?: HTMLAttributeAnchorTarget;
/** The relationship between the linked resource and the current page */
rel?: string;
/** Causes the browser to download the linked URL */
download?: boolean | string;
/** A space-separated list of URLs to ping when the link is followed */
ping?: string;
/** How much of the referrer to send when following the link */
referrerPolicy?: HTMLAttributeReferrerPolicy;
/** Options for the configured client side router */
routerOptions?: RouterOptions;
}
// Router configuration types
interface RouterConfig {}
type Href = RouterConfig extends {href: infer H} ? H : string;
type RouterOptions = RouterConfig extends {routerOptions: infer O} ? O : never;Global DOM attributes and events that apply to all elements.
/**
* All DOM attributes supported across both HTML and SVG elements
*/
interface DOMAttributes<T = FocusableElement> extends AriaAttributes, ReactDOMAttributes<T> {
id?: string | undefined;
role?: AriaRole | undefined;
tabIndex?: number | undefined;
style?: CSSProperties | undefined;
className?: string | undefined;
}
/**
* DOM attributes for group elements
*/
interface GroupDOMAttributes extends Omit<DOMAttributes<HTMLElement>, "role"> {
role?: "group" | "region" | "presentation";
}
/**
* Global attributes that can be applied to any DOM element
*/
interface GlobalDOMAttributes<T = Element> extends GlobalDOMEvents<T> {
dir?: string | undefined;
lang?: string | undefined;
hidden?: boolean | undefined;
inert?: boolean | undefined;
translate?: "yes" | "no" | undefined;
}
/** Any focusable element, including both HTML and SVG elements */
type FocusableElement = Element & HTMLOrSVGElement;Comprehensive DOM event handlers for mouse, touch, pointer, and other interactions.
/**
* Global DOM events supported on all DOM elements
*/
interface GlobalDOMEvents<T = Element> {
// Mouse Events
onClick?: MouseEventHandler<T> | undefined;
onClickCapture?: MouseEventHandler<T> | undefined;
onAuxClick?: MouseEventHandler<T> | undefined;
onAuxClickCapture?: MouseEventHandler<T> | undefined;
onContextMenu?: MouseEventHandler<T> | undefined;
onContextMenuCapture?: MouseEventHandler<T> | undefined;
onDoubleClick?: MouseEventHandler<T> | undefined;
onDoubleClickCapture?: MouseEventHandler<T> | undefined;
onMouseDown?: MouseEventHandler<T> | undefined;
onMouseDownCapture?: MouseEventHandler<T> | undefined;
onMouseEnter?: MouseEventHandler<T> | undefined;
onMouseLeave?: MouseEventHandler<T> | undefined;
onMouseMove?: MouseEventHandler<T> | undefined;
onMouseMoveCapture?: MouseEventHandler<T> | undefined;
onMouseOut?: MouseEventHandler<T> | undefined;
onMouseOutCapture?: MouseEventHandler<T> | undefined;
onMouseOver?: MouseEventHandler<T> | undefined;
onMouseOverCapture?: MouseEventHandler<T> | undefined;
onMouseUp?: MouseEventHandler<T> | undefined;
onMouseUpCapture?: MouseEventHandler<T> | undefined;
// Touch Events
onTouchCancel?: TouchEventHandler<T> | undefined;
onTouchCancelCapture?: TouchEventHandler<T> | undefined;
onTouchEnd?: TouchEventHandler<T> | undefined;
onTouchEndCapture?: TouchEventHandler<T> | undefined;
onTouchMove?: TouchEventHandler<T> | undefined;
onTouchMoveCapture?: TouchEventHandler<T> | undefined;
onTouchStart?: TouchEventHandler<T> | undefined;
onTouchStartCapture?: TouchEventHandler<T> | undefined;
// Pointer Events
onPointerDown?: PointerEventHandler<T> | undefined;
onPointerDownCapture?: PointerEventHandler<T> | undefined;
onPointerMove?: PointerEventHandler<T> | undefined;
onPointerMoveCapture?: PointerEventHandler<T> | undefined;
onPointerUp?: PointerEventHandler<T> | undefined;
onPointerUpCapture?: PointerEventHandler<T> | undefined;
onPointerCancel?: PointerEventHandler<T> | undefined;
onPointerCancelCapture?: PointerEventHandler<T> | undefined;
onPointerEnter?: PointerEventHandler<T> | undefined;
onPointerLeave?: PointerEventHandler<T> | undefined;
onPointerOver?: PointerEventHandler<T> | undefined;
onPointerOverCapture?: PointerEventHandler<T> | undefined;
onPointerOut?: PointerEventHandler<T> | undefined;
onPointerOutCapture?: PointerEventHandler<T> | undefined;
onGotPointerCapture?: PointerEventHandler<T> | undefined;
onGotPointerCaptureCapture?: PointerEventHandler<T> | undefined;
onLostPointerCapture?: PointerEventHandler<T> | undefined;
onLostPointerCaptureCapture?: PointerEventHandler<T> | undefined;
// UI Events
onScroll?: UIEventHandler<T> | undefined;
onScrollCapture?: UIEventHandler<T> | undefined;
// Wheel Events
onWheel?: WheelEventHandler<T> | undefined;
onWheelCapture?: WheelEventHandler<T> | undefined;
// Animation Events
onAnimationStart?: AnimationEventHandler<T> | undefined;
onAnimationStartCapture?: AnimationEventHandler<T> | undefined;
onAnimationEnd?: AnimationEventHandler<T> | undefined;
onAnimationEndCapture?: AnimationEventHandler<T> | undefined;
onAnimationIteration?: AnimationEventHandler<T> | undefined;
onAnimationIterationCapture?: AnimationEventHandler<T> | undefined;
// Transition Events
onTransitionCancel?: TransitionEventHandler<T> | undefined;
onTransitionCancelCapture?: TransitionEventHandler<T> | undefined;
onTransitionEnd?: TransitionEventHandler<T> | undefined;
onTransitionEndCapture?: TransitionEventHandler<T> | undefined;
onTransitionRun?: TransitionEventHandler<T> | undefined;
onTransitionRunCapture?: TransitionEventHandler<T> | undefined;
onTransitionStart?: TransitionEventHandler<T> | undefined;
onTransitionStartCapture?: TransitionEventHandler<T> | undefined;
}Reference types for DOM elements with additional methods for safe DOM access.
/**
* DOM ref value with safe DOM node access
* @template T The type of HTML element
*/
interface DOMRefValue<T extends HTMLElement = HTMLElement> {
/** Returns the DOM node or null if not available */
UNSAFE_getDOMNode(): T | null;
}
/**
* Focusable ref value with focus capabilities
* @template T The type of HTML element for focus
* @template D The type of HTML element for DOM access
*/
interface FocusableRefValue<T extends HTMLElement = HTMLElement, D extends HTMLElement = T> extends DOMRefValue<D> {
/** Focus the element */
focus(): void;
}
/**
* Ref type for DOM elements
* @template T The type of HTML element
*/
type DOMRef<T extends HTMLElement = HTMLElement> = Ref<DOMRefValue<T>>;
/**
* Ref type for focusable elements
* @template T The type of HTML element
*/
type FocusableRef<T extends HTMLElement = HTMLElement> = Ref<FocusableRefValue<T>>;
/**
* Enhanced forwardRef type for generics support
*/
type forwardRefType = typeof forwardRef;
/**
* Ref object interface
* @template T The type of the ref value
*/
interface RefObject<T> {
current: T;
}Usage Examples:
import { DOMProps, AriaLabelingProps, FocusableDOMProps } from "@react-types/shared";
// Basic DOM component
interface BasicComponentProps extends DOMProps {
children: React.ReactNode;
}
// Accessible button with ARIA labeling
interface AccessibleButtonProps extends FocusableDOMProps, AriaLabelingProps {
children: React.ReactNode;
onPress?: () => void;
}
function AccessibleButton({
id,
excludeFromTabOrder,
"aria-label": ariaLabel,
"aria-describedby": ariaDescribedBy,
children,
onPress
}: AccessibleButtonProps) {
return (
<button
id={id}
tabIndex={excludeFromTabOrder ? -1 : undefined}
aria-label={ariaLabel}
aria-describedby={ariaDescribedBy}
onClick={onPress}
>
{children}
</button>
);
}
// Text input with comprehensive DOM props
interface TextInputProps extends TextInputDOMProps {
label?: string;
}
function TextInput({
id,
name,
type = "text",
placeholder,
maxLength,
autoComplete,
onInput,
label
}: TextInputProps) {
return (
<div>
{label && <label htmlFor={id}>{label}</label>}
<input
id={id}
name={name}
type={type}
placeholder={placeholder}
maxLength={maxLength}
autoComplete={autoComplete}
onInput={onInput}
/>
</div>
);
}Install with Tessl CLI
npx tessl i tessl/npm-react-types--shared