Ariakit Documentation

repository·main·Indexed 27 days ago

https://github.com/ariakit/ariakit

A library for building accessible, themeable interfaces with unstyled React components. It features a Tailwind CSS v4 plugin for semantic design systems, component providers and stores for state management, and a flexible render prop system for custom component composition.

Tokens
93.9K
Snippets
274
Records
497
Agent score
93%

What's inside Ariakit

  1. Overview of Ariakit toolkit

    main

    Ariakit is a toolkit providing accessible components, styles, and examples for web applications. It offers specialized packages for different frameworks and styling approaches.

    Core Packages

    • @ariakit/react: The primary React integration.
    • @ariakit/solid: Experimental support for SolidJS.
    • @ariakit/tailwind: Experimental support for Tailwind CSS.

    For detailed documentation, component lists, and usage examples, visit Ariakit.com.

  2. Use the Menu component

    main

    The Menu component allows users to access a set of commands, actions, links, or settings within a dropdown. It follows the WAI-ARIA Menu and Menu Button patterns.

    Use Menu when the purpose is to trigger actions (e.g., an 'Edit' menu). If the purpose is to select a single value from a list (e.g., selecting a country), use the Select component instead.

  3. Use the Ariakit Form components

    main

    Ariakit provides a set of accessible form components and hooks for React to manage form state, submission, and validation. The components are designed to work with the WAI-ARIA Form Role and support both built-in browser validation and custom JavaScript validation.

    <FormProvider>
      <Form>
        <FormGroup>
          <FormGroupLabel />
          <FormLabel />
          <FormControl />
          <FormInput />
          <FormCheckbox />
          <FormDescription />
          <FormError />
          <FormPush />
          <FormRemove />
        </FormGroup>
        <FormRadioGroup>
          <FormRadio />
        </FormRadioGroup>
        <FormReset />
        <FormSubmit />
      </Form>
    </FormProvider>
  4. Handle state changes with state setters

    main

    Component providers accept callback functions for state changes. These callbacks are named after the state property they modify, prefixed with set (e.g., setValue, onOpenChange, onToggle). They are invoked with the new state whenever an update occurs.

    <SelectProvider
      setValue={(value) => {
        console.log(value);
      }}
    >
  5. Use the Popover component

    main

    The Popover component displays a popup dialog positioned relative to an anchor element. It supports both modal and non-modal modes and can optionally be rendered via a React portal.

    To use the Popover, you must wrap your components in a PopoverProvider. The component structure typically includes an anchor, a disclosure element to trigger the popover, and the popover content itself which can contain an arrow, heading, description, and dismissal controls.

    usePopoverStore()
    usePopoverContext()
    
    <PopoverProvider>
      <PopoverAnchor />
      <PopoverDisclosure />
      <Popover>
        <PopoverArrow />
        <PopoverHeading />
        <PopoverDescription />
        <PopoverDismiss />
      </Popover>
    </PopoverProvider>
  6. Prefer `interface` over `type` for component props

    main

    When defining TypeScript prop types for components, use interface instead of type. Interfaces support extends, which allows for better error detection when merging types. For example, using type with an intersection (&) might silently introduce invalid types, whereas an interface with extends will immediately trigger a TypeScript error. Additionally, JSDoc tags like @default merge more cleanly with interface than with type.

    // ❌ Bad, produces an invalid type without error
    type CheckboxProps = React.ComponentPropsWithoutRef<"input"> & {
      onChange?: (value: boolean) => void;
    };
    
    // ❗ Interface immediately detects the error
    interface CheckboxProps extends React.ComponentPropsWithoutRef<"input"> {
      onChange?: (value: boolean) => void;
    }
    
    // ✅ Good, fixed
    interface CheckboxProps extends Omit<
      React.ComponentPropsWithoutRef<"input">,
      "onChange"
    > {
      onChange?: (value: boolean) => void;
    }
  7. Pass a custom store to a Component Provider

    main

    If you need fine-grained control, you can combine component providers with component stores by passing a manually created store to the provider via the store prop.

    const select = useSelectStore({ defaultValue: "Banana" });
    const value = useStoreState(select, "value");
    
    <SelectProvider store={select}>
  8. Migrate Frame utilities to v0.2

    main

    In ariakit-tailwind v0.2, all frame shape utilities require the ak-frame base class. Frame presets now use a /token suffix to set the token scope for padding and radius.

    Preset Mapping:

    • ak-frame-field $\rightarrow$ ak-frame ak-frame-field/field
    • ak-frame-card $\rightarrow$ ak-frame ak-frame-card/card
    • ak-frame-container $\rightarrow$ ak-frame ak-frame-container/container
    • ak-frame-dialog $\rightarrow$ ak-frame ak-frame-dialog/dialog
    • ak-frame-badge $\rightarrow$ ak-frame ak-frame-badge/badge

    Padding and Overrides:

    • Padding is now a separate utility: ak-frame ak-frame-p-N (previously ak-frame/N).
    • For cover utilities, split the padding: ak-frame ak-frame-cover ak-frame-p-N (previously ak-frame-cover/N).
    • When using variant prefixes (e.g., sm:), apply the prefix to each utility: sm:ak-frame sm:ak-frame-cover sm:ak-frame-p-1.
    • Frame preset padding overrides require the explicit base class: ak-frame ak-frame-field/1 (previously ak-frame-field/1).

    Other Frame Changes:

    • ak-frame-cover-start / ak-frame-cover-end $\rightarrow$ ak-frame ak-frame-start / ak-frame-end.
    • Force modifiers: ak-frame ak-frame-force ak-frame-TYPE/TYPE (previously ak-frame-force-TYPE).
    • ak-frame-overflow has been removed. Use ak-frame ak-frame-cover instead, which handles padding and border combinations automatically.
    - ak-frame-field
    + ak-frame ak-frame-field/field
    
    - ak-frame/2
    + ak-frame ak-frame-p-2
    
    - ak-frame-cover/1.5
    + ak-frame ak-frame-cover ak-frame-p-1.5
    
    - sm:ak-frame-cover/1
    + sm:ak-frame sm:ak-frame-cover sm:ak-frame-p-1
    
    - ak-frame-force-dialog
    + ak-frame ak-frame-force ak-frame-dialog/dialog
    
    - ak-frame-overflow
    + ak-frame ak-frame-cover