- Spec files
npm-react-aria
Describes: pkg:npm/react-aria@3.43.x
- Description
- Comprehensive library of unstyled React hooks providing accessible UI primitives with full WAI-ARIA compliance and internationalization support.
- Author
- tessl
- Last updated
interactions.md docs/
1# Interactions23Low-level interaction hooks for handling user input across different devices and interaction models. These hooks provide normalized event handling for mouse, touch, keyboard, and screen reader interactions.45## Capabilities67### Press89Provides press interaction handling across mouse, touch, and keyboard.1011```typescript { .api }12/**13* Provides press interaction handling14* @param props - Press configuration15* @returns Press result with event handlers16*/17function usePress(props: PressHookProps): PressResult;1819interface PressHookProps extends PressProps {20/** Ref to the target element */21ref?: RefObject<Element>;22}2324interface PressProps {25/** Handler called when press starts */26onPressStart?: (e: PressEvent) => void;27/** Handler called when press ends */28onPressEnd?: (e: PressEvent) => void;29/** Handler called when press changes */30onPressChange?: (isPressed: boolean) => void;31/** Handler called when press is released over target */32onPressUp?: (e: PressEvent) => void;33/** Handler called when press is completed */34onPress?: (e: PressEvent) => void;35/** Whether press events are disabled */36isDisabled?: boolean;37/** Whether to prevent focus on press (mobile) */38preventFocusOnPress?: boolean;39/** Whether press should cancel when pointer exits */40shouldCancelOnPointerExit?: boolean;41/** Whether text selection should be allowed during press */42allowTextSelectionOnPress?: boolean;43}4445interface PressResult {46/** Whether the element is currently pressed */47isPressed: boolean;48/** Props to spread on the target element */49pressProps: DOMAttributes<Element>;50}5152interface PressEvent {53/** Type of press event */54type: 'pressstart' | 'pressend' | 'pressup' | 'press';55/** Input type that triggered the event */56pointerType: 'mouse' | 'pen' | 'touch' | 'keyboard' | 'virtual';57/** Target element */58target: Element;59/** Whether shift key was pressed */60shiftKey: boolean;61/** Whether ctrl key was pressed */62ctrlKey: boolean;63/** Whether meta key was pressed */64metaKey: boolean;65/** Whether alt key was pressed */66altKey: boolean;67/** X coordinate relative to target */68x: number;69/** Y coordinate relative to target */70y: number;71/** Continue propagation */72continuePropagation(): void;73}74```7576### Long Press7778Provides long press interaction handling with customizable delay.7980```typescript { .api }81/**82* Provides long press interaction handling83* @param props - Long press configuration84* @returns Long press result with event handlers85*/86function useLongPress(props: LongPressProps): LongPressResult;8788interface LongPressProps {89/** Whether long press is disabled */90isDisabled?: boolean;91/** Handler called when long press is triggered */92onLongPressStart?: (e: LongPressEvent) => void;93/** Handler called when long press ends */94onLongPressEnd?: (e: LongPressEvent) => void;95/** Handler called when long press completes */96onLongPress?: (e: LongPressEvent) => void;97/** Long press threshold in milliseconds */98threshold?: number;99/** Accessibility description for long press action */100accessibilityDescription?: string;101}102103interface LongPressResult {104/** Props to spread on the target element */105longPressProps: DOMAttributes<Element>;106}107108interface LongPressEvent {109/** Type of long press event */110type: 'longpressstart' | 'longpressend' | 'longpress';111/** Input type that triggered the event */112pointerType: 'mouse' | 'pen' | 'touch' | 'keyboard' | 'virtual';113/** Target element */114target: Element;115/** Whether shift key was pressed */116shiftKey: boolean;117/** Whether ctrl key was pressed */118ctrlKey: boolean;119/** Whether meta key was pressed */120metaKey: boolean;121/** Whether alt key was pressed */122altKey: boolean;123}124```125126### Hover127128Provides hover interaction handling with proper touch device support.129130```typescript { .api }131/**132* Provides hover interaction handling133* @param props - Hover configuration134* @returns Hover result with event handlers135*/136function useHover(props: HoverProps): HoverResult;137138interface HoverProps {139/** Handler called when hover starts */140onHoverStart?: (e: HoverEvent) => void;141/** Handler called when hover ends */142onHoverEnd?: (e: HoverEvent) => void;143/** Handler called when hover changes */144onHoverChange?: (isHovering: boolean) => void;145/** Whether hover is disabled */146isDisabled?: boolean;147}148149interface HoverResult {150/** Props to spread on the target element */151hoverProps: DOMAttributes<Element>;152/** Whether the element is currently hovered */153isHovered: boolean;154}155156interface HoverEvent {157/** Type of hover event */158type: 'hoverstart' | 'hoverend';159/** Input type that triggered the event */160pointerType: 'mouse' | 'pen';161/** Target element */162target: Element;163}164```165166### Focus167168Provides focus event handling with proper event normalization.169170```typescript { .api }171/**172* Provides focus event handling173* @param props - Focus configuration174* @returns Focus result with event handlers175*/176function useFocus(props: FocusProps): FocusResult;177178interface FocusProps {179/** Whether focus events are disabled */180isDisabled?: boolean;181/** Handler called when element receives focus */182onFocus?: (e: FocusEvent) => void;183/** Handler called when element loses focus */184onBlur?: (e: FocusEvent) => void;185/** Handler called when focus changes */186onFocusChange?: (isFocused: boolean) => void;187}188189interface FocusResult {190/** Props to spread on the target element */191focusProps: DOMAttributes<Element>;192}193```194195### Focus Visible196197Provides focus-visible state management for keyboard navigation styling.198199```typescript { .api }200/**201* Provides focus-visible state management202* @param props - Focus visible configuration203* @returns Focus visible result with state204*/205function useFocusVisible(props?: FocusVisibleProps): FocusVisibleResult;206207interface FocusVisibleProps {208/** Whether focus visible is disabled */209isDisabled?: boolean;210/** Auto-focus behavior */211autoFocus?: boolean;212}213214interface FocusVisibleResult {215/** Whether focus is visible */216isFocusVisible: boolean;217}218```219220### Focus Within221222Provides focus-within state tracking for container elements.223224```typescript { .api }225/**226* Provides focus-within state tracking227* @param props - Focus within configuration228* @returns Focus within result with event handlers229*/230function useFocusWithin(props: FocusWithinProps): FocusWithinResult;231232interface FocusWithinProps {233/** Whether focus within is disabled */234isDisabled?: boolean;235/** Handler called when focus enters the container */236onFocusWithin?: (e: FocusEvent) => void;237/** Handler called when focus leaves the container */238onBlurWithin?: (e: FocusEvent) => void;239/** Handler called when focus within changes */240onFocusWithinChange?: (isFocusWithin: boolean) => void;241}242243interface FocusWithinResult {244/** Props to spread on the container element */245focusWithinProps: DOMAttributes<Element>;246/** Whether focus is within the container */247isFocusWithin: boolean;248}249```250251### Keyboard252253Provides keyboard event handling with normalized key handling.254255```typescript { .api }256/**257* Provides keyboard event handling258* @param props - Keyboard configuration259* @returns Keyboard result with event handlers260*/261function useKeyboard(props: KeyboardProps): KeyboardResult;262263interface KeyboardProps {264/** Whether keyboard events are disabled */265isDisabled?: boolean;266/** Handler called on key down */267onKeyDown?: (e: KeyboardEvent) => void;268/** Handler called on key up */269onKeyUp?: (e: KeyboardEvent) => void;270}271272interface KeyboardResult {273/** Props to spread on the target element */274keyboardProps: DOMAttributes<Element>;275}276```277278### Move279280Provides move/drag interaction handling for pointer events.281282```typescript { .api }283/**284* Provides move/drag interaction handling285* @param props - Move configuration286* @returns Move result with event handlers287*/288function useMove(props: MoveEvents): MoveResult;289290interface MoveEvents {291/** Handler called when move starts */292onMoveStart?: (e: MoveStartEvent) => void;293/** Handler called during move */294onMove?: (e: MoveMoveEvent) => void;295/** Handler called when move ends */296onMoveEnd?: (e: MoveEndEvent) => void;297}298299interface MoveResult {300/** Props to spread on the target element */301moveProps: DOMAttributes<Element>;302}303304interface MoveStartEvent {305/** Type of move event */306type: 'movestart';307/** Input type */308pointerType: 'mouse' | 'pen' | 'touch' | 'keyboard' | 'virtual';309/** Whether shift key was pressed */310shiftKey: boolean;311/** Whether ctrl key was pressed */312ctrlKey: boolean;313/** Whether meta key was pressed */314metaKey: boolean;315/** Whether alt key was pressed */316altKey: boolean;317}318319interface MoveMoveEvent {320/** Type of move event */321type: 'move';322/** Input type */323pointerType: 'mouse' | 'pen' | 'touch' | 'keyboard' | 'virtual';324/** Delta X movement */325deltaX: number;326/** Delta Y movement */327deltaY: number;328/** Whether shift key was pressed */329shiftKey: boolean;330/** Whether ctrl key was pressed */331ctrlKey: boolean;332/** Whether meta key was pressed */333metaKey: boolean;334/** Whether alt key was pressed */335altKey: boolean;336}337338interface MoveEndEvent {339/** Type of move event */340type: 'moveend';341/** Input type */342pointerType: 'mouse' | 'pen' | 'touch' | 'keyboard' | 'virtual';343/** Whether shift key was pressed */344shiftKey: boolean;345/** Whether ctrl key was pressed */346ctrlKey: boolean;347/** Whether meta key was pressed */348metaKey: boolean;349/** Whether alt key was pressed */350altKey: boolean;351}352```353354### Interact Outside355356Provides outside interaction detection for dismissing overlays.357358```typescript { .api }359/**360* Provides outside interaction detection361* @param props - Interact outside configuration362* @returns Event handlers for outside detection363*/364function useInteractOutside(props: InteractOutsideProps): void;365366interface InteractOutsideProps {367/** Ref to the element to monitor */368ref: RefObject<Element>;369/** Handler called when interaction occurs outside */370onInteractOutside?: (e: InteractOutsideEvent) => void;371/** Whether interact outside is disabled */372isDisabled?: boolean;373/** Handler to determine if interaction should be ignored */374onInteractOutsideStart?: (e: InteractOutsideEvent) => void;375}376377interface InteractOutsideEvent {378/** Type of interaction */379type: 'pointerdown' | 'pointerup' | 'focusin' | 'focusout';380/** Target element */381target: Element;382}383```384385### Focusable386387Provides focusable element behavior with keyboard and screen reader support.388389```typescript { .api }390/**391* Provides focusable element behavior392* @param props - Focusable configuration393* @param ref - Ref to the focusable element394* @returns Focusable result with props and state395*/396function useFocusable(props: FocusableOptions, ref: RefObject<Element>): FocusableAria;397398interface FocusableOptions {399/** Whether the element should exclude from tab order */400excludeFromTabOrder?: boolean;401/** Whether the element is disabled */402isDisabled?: boolean;403/** Auto-focus behavior */404autoFocus?: boolean;405}406407interface FocusableAria {408/** Props to spread on the focusable element */409focusableProps: DOMAttributes<Element>;410}411```412413### Drag and Drop414415Provides comprehensive drag and drop interaction handling.416417```typescript { .api }418/**419* Provides drag behavior for elements420* @param options - Drag configuration421* @returns Drag result with props and state422*/423function useDrag(options: DragOptions): DragResult;424425/**426* Provides drop behavior for elements427* @param options - Drop configuration428* @returns Drop result with props and state429*/430function useDrop(options: DropOptions): DropResult;431432/**433* Provides draggable collection behavior434* @param options - Draggable collection configuration435* @returns Collection drag result436*/437function useDraggableCollection(options: DraggableCollectionOptions): DroppableCollectionResult;438439/**440* Provides droppable collection behavior441* @param options - Droppable collection configuration442* @returns Collection drop result443*/444function useDroppableCollection(options: DroppableCollectionOptions): DroppableCollectionResult;445446/**447* Provides individual draggable item behavior448* @param props - Draggable item configuration449* @returns Draggable item result450*/451function useDraggableItem(props: DraggableItemProps, ref: RefObject<Element>): DraggableItemResult;452453/**454* Provides individual droppable item behavior455* @param options - Droppable item configuration456* @param ref - Ref to the droppable element457* @returns Droppable item result458*/459function useDroppableItem(options: DroppableItemOptions, ref: RefObject<Element>): DroppableItemResult;460461/**462* Provides drop indicator behavior463* @param props - Drop indicator configuration464* @param ref - Ref to the indicator element465* @returns Drop indicator result466*/467function useDropIndicator(props: DropIndicatorProps, ref: RefObject<Element>): DropIndicatorAria;468469/**470* Component for rendering drag previews471* @param props - Drag preview configuration472* @returns Drag preview component473*/474function DragPreview(props: DragPreviewProps): JSX.Element;475476/**477* Drop target delegate for list components478*/479class ListDropTargetDelegate implements DropTargetDelegate {480/** Get drop target for a point */481getDropTarget(target: DropTarget, types: Set<string>): DropTarget | null;482/** Check if drop is allowed */483isValidDropTarget(target: DropTarget, types: Set<string>): boolean;484}485486/**487* Constant for directory drag type488*/489const DIRECTORY_DRAG_TYPE: string;490491/**492* Type guard to check if drop item is a directory493* @param item - Drop item to check494* @returns Whether item is a directory495*/496function isDirectoryDropItem(item: DropItem): item is DirectoryDropItem;497498/**499* Type guard to check if drop item is a file500* @param item - Drop item to check501* @returns Whether item is a file502*/503function isFileDropItem(item: DropItem): item is FileDropItem;504505/**506* Type guard to check if drop item is text507* @param item - Drop item to check508* @returns Whether item is text509*/510function isTextDropItem(item: DropItem): item is TextDropItem;511512interface DragOptions {513/** Handler called when drag starts */514onDragStart?: (e: DragStartEvent) => void;515/** Handler called during drag */516onDragMove?: (e: DragMoveEvent) => void;517/** Handler called when drag ends */518onDragEnd?: (e: DragEndEvent) => void;519/** Get drag data for the operation */520getItems: () => DragItem[];521/** Preview component */522preview?: DragPreviewRenderer;523/** Whether drag is disabled */524isDisabled?: boolean;525}526527interface DragResult {528/** Props for the draggable element */529dragProps: DOMAttributes<Element>;530/** Whether element is currently being dragged */531isDragging: boolean;532}533534interface DropOptions {535/** Ref to the drop target element */536ref: RefObject<Element>;537/** Handler called when drop occurs */538onDrop?: (e: DropEvent) => void;539/** Handler called when drag enters */540onDropEnter?: (e: DropEnterEvent) => void;541/** Handler called when drag exits */542onDropExit?: (e: DropExitEvent) => void;543/** Handler called during drag over */544onDropMove?: (e: DropMoveEvent) => void;545/** Get drop operation for items */546getDropOperation?: (target: DropTarget, types: Set<string>, allowedOperations: DropOperation[]) => DropOperation;547/** Accept drop delegate */548acceptedDragTypes?: string[];549/** Whether drop is disabled */550isDisabled?: boolean;551}552553interface DropResult {554/** Props for the droppable element */555dropProps: DOMAttributes<Element>;556/** Whether drag is currently over */557isDropTarget: boolean;558}559```560561### Clipboard562563Provides clipboard operations with proper permissions and fallbacks.564565```typescript { .api }566/**567* Provides clipboard operations568* @param props - Clipboard configuration569* @returns Clipboard result with operations570*/571function useClipboard(props?: ClipboardProps): ClipboardResult;572573interface ClipboardProps {574/** Handler called when copy succeeds */575onCopy?: () => void;576/** Handler called when copy fails */577onError?: () => void;578}579580interface ClipboardResult {581/** Copy text to clipboard */582copy(text: string): Promise<void>;583/** Copy data items to clipboard */584copyItems(items: ClipboardItem[]): Promise<void>;585/** Paste from clipboard */586paste(): Promise<string>;587/** Whether clipboard operations are supported */588isSupported: boolean;589}590```591592### Interaction Components593594```typescript { .api }595/**596* Component for pressable elements with interaction states597*/598function Pressable(props: PressableProps): JSX.Element;599600/**601* Component for focusable elements with proper semantics602*/603function Focusable(props: FocusableProps): JSX.Element;604605interface PressableProps extends PressProps {606/** Children render prop */607children: (states: { isPressed: boolean }) => ReactNode;608/** Element type to render */609elementType?: React.ElementType;610}611612interface FocusableProps extends FocusableOptions {613/** Children content */614children: ReactNode;615/** Element type to render */616elementType?: React.ElementType;617}618```619620## Types621622```typescript { .api }623type PointerType = 'mouse' | 'pen' | 'touch' | 'keyboard' | 'virtual';624625interface KeyboardEvent {626/** Type of keyboard event */627type: 'keydown' | 'keyup';628/** Key that was pressed */629key: string;630/** Key code */631code: string;632/** Whether shift key was pressed */633shiftKey: boolean;634/** Whether ctrl key was pressed */635ctrlKey: boolean;636/** Whether meta key was pressed */637metaKey: boolean;638/** Whether alt key was pressed */639altKey: boolean;640/** Repeat count */641repeat: boolean;642/** Whether event is composing */643isComposing: boolean;644/** Target element */645target: Element;646/** Current target element */647currentTarget: Element;648/** Prevent default behavior */649preventDefault(): void;650/** Stop propagation */651stopPropagation(): void;652/** Continue propagation */653continuePropagation(): void;654}655656interface FocusEvent {657/** Type of focus event */658type: 'focus' | 'blur' | 'focusin' | 'focusout';659/** Target element */660target: Element;661/** Related target element */662relatedTarget: Element | null;663}664665type DropOperation = 'copy' | 'link' | 'move' | 'cancel';666667interface DragItem {668/** MIME types for the item */669[type: string]: string | (() => string);670}671672interface DropTarget {673/** Type of drop target */674type: 'item' | 'folder' | 'root';675/** Key of the target */676key?: Key;677/** Drop position relative to target */678dropPosition?: 'before' | 'after' | 'on';679}680681interface DragStartEvent {682/** Type of event */683type: 'dragstart';684/** X coordinate */685x: number;686/** Y coordinate */687y: number;688}689690interface DragMoveEvent {691/** Type of event */692type: 'dragmove';693/** X coordinate */694x: number;695/** Y coordinate */696y: number;697/** Delta X */698deltaX: number;699/** Delta Y */700deltaY: number;701}702703interface DragEndEvent {704/** Type of event */705type: 'dragend';706/** X coordinate */707x: number;708/** Y coordinate */709y: number;710/** Drop operation performed */711dropOperation: DropOperation;712/** Whether operation was successful */713isInternal: boolean;714}715716interface DropEvent {717/** Type of event */718type: 'drop';719/** X coordinate */720x: number;721/** Y coordinate */722y: number;723/** Drop operation */724dropOperation: DropOperation;725/** Dropped items */726items: DropItem[];727/** Drop target */728target: DropTarget;729}730```