- 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
tags.md docs/
1# Tags23Tag components for displaying and managing collections of labels, categories, or metadata with full accessibility support including keyboard navigation and screen reader announcements.45## Capabilities67### Tag89Provides individual tag behavior with optional dismiss functionality and keyboard navigation.1011```typescript { .api }12/**13* Provides tag behavior and accessibility14* @param props - Tag configuration options15* @param state - Tag state from useTagState16* @param ref - Ref to the tag element17* @returns Tag props and state18*/19function useTag(props: AriaTagProps, state: TagState, ref: RefObject<Element>): TagAria;2021interface AriaTagProps {22/** Tag content/label */23children: ReactNode;24/** Whether the tag can be removed */25isDismissible?: boolean;26/** Whether the tag is disabled */27isDisabled?: boolean;28/** Whether the tag is selected (for selectable tags) */29isSelected?: boolean;30/** Handler called when the tag is dismissed */31onDismiss?: () => void;32/** Handler called when the tag is selected */33onSelect?: () => void;34/** Text content of the tag for accessibility */35textValue?: string;36/** ARIA label for the dismiss button */37dismissLabel?: string;38}3940interface TagAria {41/** Props to spread on the tag element */42tagProps: DOMAttributes<Element>;43/** Props for the dismiss button (if dismissible) */44dismissButtonProps: DOMAttributes<Element>;45/** Whether the tag is currently focused */46isFocused: boolean;47/** Whether the tag is currently hovered */48isHovered: boolean;49}50```5152**Usage Example:**5354```typescript55import { useTag } from "react-aria";56import { useTagState } from "react-stately";5758function Tag(props) {59let state = useTagState(props);60let ref = useRef();61let { tagProps, dismissButtonProps, isFocused } = useTag(props, state, ref);6263return (64<div65{...tagProps}66ref={ref}67className={`tag ${isFocused ? 'focused' : ''} ${props.isSelected ? 'selected' : ''}`}68>69<span className="tag-content">{props.children}</span>70{props.isDismissible && (71<button {...dismissButtonProps} className="tag-dismiss">72×73</button>74)}75</div>76);77}78```7980### Tag Group8182Manages a collection of tags with keyboard navigation, selection state, and batch operations.8384```typescript { .api }85/**86* Provides tag group behavior for managing collections of tags87* @param props - Tag group configuration88* @param state - Tag group state from useTagGroupState89* @param ref - Ref to the tag group element90* @returns Tag group props and utilities91*/92function useTagGroup(props: AriaTagGroupProps, state: TagGroupState, ref: RefObject<Element>): TagGroupAria;9394interface AriaTagGroupProps {95/** Collection of tag items */96items?: Iterable<TagItem>;97/** Whether multiple tags can be selected */98selectionMode?: 'none' | 'single' | 'multiple';99/** Currently selected tag keys */100selectedKeys?: Iterable<Key>;101/** Default selected keys (uncontrolled) */102defaultSelectedKeys?: Iterable<Key>;103/** Handler called when selection changes */104onSelectionChange?: (keys: Set<Key>) => void;105/** Handler called when a tag is dismissed */106onDismiss?: (key: Key) => void;107/** Whether the tag group is disabled */108isDisabled?: boolean;109/** Whether tags are read-only */110isReadOnly?: boolean;111/** Maximum number of tags to display */112maxTags?: number;113/** Label for the tag group */114label?: ReactNode;115/** Description for the tag group */116description?: ReactNode;117/** Error message for validation */118errorMessage?: ReactNode;119}120121interface TagGroupAria {122/** Props to spread on the tag group container */123tagGroupProps: DOMAttributes<Element>;124/** Props for the group label */125labelProps: DOMAttributes<Element>;126/** Props for the description */127descriptionProps: DOMAttributes<Element>;128/** Props for the error message */129errorMessageProps: DOMAttributes<Element>;130}131```132133**Usage Example:**134135```typescript136import { useTagGroup } from "react-aria";137import { useTagGroupState } from "react-stately";138139function TagGroup(props) {140let state = useTagGroupState(props);141let ref = useRef();142let { tagGroupProps, labelProps, descriptionProps } = useTagGroup(props, state, ref);143144return (145<div {...tagGroupProps} ref={ref} className="tag-group">146{props.label && (147<label {...labelProps} className="tag-group-label">148{props.label}149</label>150)}151<div className="tag-list">152{state.collection.map((item) => (153<Tag154key={item.key}155item={item}156state={state}157isDismissible={!props.isReadOnly}158isSelected={state.selectionManager.isSelected(item.key)}159onDismiss={() => props.onDismiss?.(item.key)}160/>161))}162</div>163{props.description && (164<div {...descriptionProps} className="tag-group-description">165{props.description}166</div>167)}168</div>169);170}171```172173## Types174175```typescript { .api }176interface TagState {177/** Whether the tag is selected */178isSelected: boolean;179/** Whether the tag is focused */180isFocused: boolean;181/** Whether the tag is hovered */182isHovered: boolean;183/** Whether the tag can be dismissed */184isDismissible: boolean;185}186187interface TagGroupState {188/** Collection of tag items */189collection: Collection<TagItem>;190/** Selection manager for the tag group */191selectionManager: SelectionManager;192/** Whether the tag group is disabled */193isDisabled: boolean;194/** Add a new tag to the group */195addTag: (tag: TagItem) => void;196/** Remove a tag from the group by key */197removeTag: (key: Key) => void;198/** Clear all tags */199clear: () => void;200}201202interface TagItem {203/** Unique identifier for the tag */204key: Key;205/** Tag content */206content: ReactNode;207/** Text representation for accessibility */208textValue: string;209/** Whether this tag is dismissible */210isDismissible?: boolean;211/** Whether this tag is disabled */212isDisabled?: boolean;213}214```