What is @zag-js/react?
@zag-js/react is a collection of accessible, headless UI components for React. It provides a set of hooks and utilities to build custom UI components with accessibility and state management in mind.
What are @zag-js/react's main functionalities?
Accordion
The Accordion feature allows you to create collapsible sections of content. The useAccordion hook provides the necessary props to manage the state and accessibility of the accordion items.
import { useAccordion } from '@zag-js/react';
function AccordionComponent() {
const { getAccordionProps, getItemProps } = useAccordion();
return (
<div {...getAccordionProps()}>
<div {...getItemProps({ index: 0 })}>Item 1</div>
<div {...getItemProps({ index: 1 })}>Item 2</div>
</div>
);
}
Tabs
The Tabs feature provides a way to create tabbed interfaces. The useTabs hook manages the state and accessibility of the tabs and their corresponding panels.
import { useTabs } from '@zag-js/react';
function TabsComponent() {
const { getTabListProps, getTabProps, getTabPanelProps } = useTabs();
return (
<div>
<div {...getTabListProps()}>
<button {...getTabProps({ index: 0 })}>Tab 1</button>
<button {...getTabProps({ index: 1 })}>Tab 2</button>
</div>
<div {...getTabPanelProps({ index: 0 })}>Content 1</div>
<div {...getTabPanelProps({ index: 1 })}>Content 2</div>
</div>
);
}
Dialog
The Dialog feature helps in creating modal dialogs. The useDialog hook provides the necessary props to control the dialog's open/close state and ensure it is accessible.
import { useDialog } from '@zag-js/react';
function DialogComponent() {
const { getDialogProps, getTriggerProps } = useDialog();
return (
<div>
<button {...getTriggerProps()}>Open Dialog</button>
<div {...getDialogProps()}>This is a dialog</div>
</div>
);
}
Other packages similar to @zag-js/react
headlessui
Headless UI is a set of completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS. It provides similar functionality to @zag-js/react but is more tightly integrated with Tailwind CSS, making it a great choice for projects already using Tailwind.
react-aria
React Aria is a library of React Hooks that provides accessible UI primitives. It offers similar headless components and focuses on accessibility, much like @zag-js/react, but with a broader range of low-level hooks for building custom components.