CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-aria--utils

Essential utility functions and React hooks for building accessible React Aria UI components

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

id-and-refs.mddocs/

ID & Reference Management

Core utilities for generating accessible IDs and managing React refs across components. These utilities ensure proper accessibility attributes and ref handling in React Aria components.

Capabilities

useId Hook

Generates unique IDs for DOM elements with SSR safety and collision avoidance.

/**
 * Generates a unique ID for DOM elements, with SSR safety
 * @param defaultId - Optional default ID to use
 * @returns A unique string ID that's stable across renders
 */
function useId(defaultId?: string): string;

Usage Examples:

import { useId } from "@react-aria/utils";

function MyComponent() {
  const labelId = useId();
  const inputId = useId("my-input"); // Uses provided ID if no conflicts
  
  return (
    <div>
      <label id={labelId} htmlFor={inputId}>Name</label>
      <input id={inputId} aria-labelledby={labelId} />
    </div>
  );
}

mergeIds Function

Merges two IDs and triggers re-renders in hooked components when IDs change.

/**
 * Merges two IDs, prioritizing the second ID if different
 * @param idA - First ID
 * @param idB - Second ID 
 * @returns The merged ID (idB takes precedence if different from idA)
 */
function mergeIds(idA: string, idB: string): string;

Usage Examples:

import { mergeIds, useId } from "@react-aria/utils";

function FormField({ id, ...props }) {
  const defaultId = useId();
  const finalId = mergeIds(defaultId, id);
  
  return <input id={finalId} {...props} />;
}

useSlotId Hook

Generates an ID and checks if it exists in DOM after render for conditional aria-labelledby attributes.

/**
 * Generates an ID and checks if it exists in DOM after render
 * @param depArray - Optional dependency array for recalculating DOM presence
 * @returns ID string that's stable across renders
 */
function useSlotId(depArray?: ReadonlyArray<any>): string;

Usage Examples:

import { useSlotId } from "@react-aria/utils";

function Dialog({ children }) {
  const titleId = useSlotId([children]);
  
  return (
    <div 
      role="dialog" 
      aria-labelledby={titleId} // Only set if title element exists
    >
      {children}
    </div>
  );
}

function DialogTitle({ children, ...props }) {
  const id = useSlotId();
  return <h2 id={id} {...props}>{children}</h2>;
}

mergeRefs Function

Combines multiple refs (object or callback) into a single ref function.

/**
 * Combines multiple refs into a single ref function
 * @param refs - Array of refs (object refs, callback refs, or null/undefined)
 * @returns Combined ref function that updates all provided refs
 */
function mergeRefs<T>(...refs: (Ref<T> | null | undefined)[]): Ref<T>;

Usage Examples:

import { mergeRefs } from "@react-aria/utils";
import { useRef, forwardRef } from "react";

const MyInput = forwardRef<HTMLInputElement, InputProps>((props, ref) => {
  const localRef = useRef<HTMLInputElement>(null);
  const combinedRef = mergeRefs(ref, localRef);
  
  return <input ref={combinedRef} {...props} />;
});

useObjectRef Hook

Converts callback refs or forwarded refs to object refs for use with React Aria hooks.

/**
 * Converts callback refs or forwarded refs to object refs
 * @param ref - Original ref (callback, object, or null)
 * @returns Object ref that updates the original ref
 */
function useObjectRef<T>(ref?: ForwardedRef<T>): MutableRefObject<T | null>;

Usage Examples:

import { useObjectRef } from "@react-aria/utils";
import { useButton } from "@react-aria/button";
import { forwardRef } from "react";

const Button = forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
  const objectRef = useObjectRef(ref);
  const { buttonProps } = useButton(props, objectRef);
  
  return <button ref={objectRef} {...buttonProps} />;
});

useSyncRef Hook

Synchronizes a ref with a context value for keeping refs in sync across components.

/**
 * Synchronizes a ref with a context value
 * @param context - Context to sync with
 * @param ref - Ref to synchronize
 */
function useSyncRef<T>(context: Context<T>, ref: MutableRefObject<T>): void;

Usage Examples:

import { useSyncRef } from "@react-aria/utils";
import { createContext, useContext, useRef } from "react";

const MyContext = createContext<HTMLElement | null>(null);

function ContextProvider({ children }) {
  const ref = useRef<HTMLElement>(null);
  useSyncRef(MyContext, ref);
  
  return (
    <MyContext.Provider value={ref.current}>
      <div ref={ref}>
        {children}
      </div>
    </MyContext.Provider>
  );
}

Types

type Ref<T> = MutableRefObject<T> | ((instance: T | null) => void) | null;
type ForwardedRef<T> = ((instance: T | null) => void) | MutableRefObject<T | null> | null;

Install with Tessl CLI

npx tessl i tessl/npm-react-aria--utils

docs

animation-and-transitions.md

event-management.md

focus-and-accessibility.md

id-and-refs.md

index.md

links-and-navigation.md

miscellaneous-utilities.md

platform-detection.md

props-and-events.md

scrolling-and-layout.md

shadow-dom-support.md

state-and-effects.md

virtual-events-and-input.md

tile.json