# Introduction to Zag
Zag is a framework agnostic toolkit for implementing complex, interactive, and
accessible UI components in your design system and web applications. Works for
React, Solid and Vue.
> Zag is part of the next evolution of Chakra UI, and one of the four arms of
> the future of Chakra UI.
> [**Watch the talk here**](https://www.youtube.com/watch?v=I5xEc9t-HZg)
## Motivation
In [Chakra UI React](https://chakra-ui.com/), we've experienced too many hiccups
and bugs in the past related to how we coordinate events, manage state, and side
effects. Most these bugs are associated with the orchestration within
`useEffect`, `useMemo`, `useCallback`, etc.
These issues were replicated in our [Chakra UI Vue](https://vue.chakra-ui.com/)
pursuit as well, and created a maintenance hell for us. We're grateful for this
experience because it made us take a step back to define how we would like to
build components in the future.
We believe that most widgets should function the same way regardless of the
framework they're built with. That's why we built Zag.
> Don't re-invent the wheel, **let the machines do the work 😊**
## Why Zag?
- **Powered by state machines 🌳**: Zag is built on top of the latest ideas in
Statecharts. We don't follow the SCXML specifications, but we've created an
API that we think will help us build more complex components fast.
- **Write once, use everywhere 🦄**: The component interactions are modelled in
a framework agnostic way. We provide adapters for JS frameworks so you can use
it in React, Solid, or Vue 3.
- **Focus on accessibility ♿️**: Zag is built with accessibility in mind. We
handle many details related to keyboard interactions, focus management, aria
roles and attributes.
- **Headless ✨**: The machine APIs are completely unstyled and gives you the
control to use any styling solution you prefer.
- **Incremental adoption ✨**: Adopt the machines as you need them. Each
component machine is an NPM package and can be installed individually so you
can use them incrementally.
## Learn
[Watch the course](https://egghead.io/courses/statechart-driven-ui-components-with-zag-js-53f85394)
on Egghead to learn how to build statechart-driven UI components with Zag.js.
This course will give you a deep dive into how Zag works and how you can use it
to build complex UI components.
## Fun Facts
**Zag** means to _take a sharp change in direction_. This clearly describes our
approach of using state machines to power the logic behind UI components.
### Teasers
- When you see someone using classic react, vue or solid to build an interactive
UI component that exists in Zag, tell them to **"zag it!"** ⚡️
- Anyone using Zag will be called a **"zagger"** 💥
- The feeling you get when you use Zag will be called **"zagadat!"** 🚀
- The Zag community will be called **"zag nation"** 🔥
## Community
### Discord
To get involved with the Zag community, ask questions, and chat with the
maintainers, join our Discord.
[Join our Discord](https://zagjs.com/discord)
### Twitter
To receive updates on new components, enhancements, blog posts, and tips, follow
our Twitter account.
[Follow us on Twitter](https://twitter.com/zag_js)
## Prior art
We strongly believe in open source and the power of open collaboration. In the
past, we've been inspired by other meaningful projects and amazing people who
have inspire(d) us to keep improving our ideas.
Some of the projects we've been inspired by include:
- Chakra UI - [https://chakra-ui.com/](https://chakra-ui.com/)
- Radix UI - [https://www.radix-ui.com/](https://www.radix-ui.com/)
- Material Web Components -
[https://github.com/material-components/material-components-web](https://github.com/material-components/material-components-web)
- React Aria -
[https://react-spectrum.adobe.com/react-aria](https://react-spectrum.adobe.com/react-aria)
- Goldman Sachs Design System - [https://design.gs.com/](https://design.gs.com/)
- Reakit - [https://reakit.io/](https://reakit.io/)
- Fast - [https://fast.design/](https://fast.design/)
## Additional Thanks
- [Guillermo](https://rauchg.com/2015/pure-ui) for writing a great article that
sparked the idea for Zag.
- [Open UI](https://open-ui.org/) for inspiring the pattern behind this library
- [XState](https://xstate.js.org/) for inspiring the base implementation of the
state machine
- [Vue.js](https://vuejs.org/) and [Lit](https://lit.dev/) for inspiring new
patterns in the machine (`computed` and `watch`)
- [David Khourshid](https://twitter.com/DavidKPiano) for talking about state
machines long enough to get me started on this project
# Getting Started
Zag can be used within most JS frameworks like Vue, React and Solid.
To get Zag running, you'll need to:
1. Install the machine for the component you're interested in. Let's say you
want to use the `tooltip` machine.
```bash
npm install @zag-js/tooltip
# or
yarn add @zag-js/tooltip
```
2. Install the adapter for the framework of your choice. At the moment, Zag is
available for React, Vue 3 and Solid.js. Let's say you use React.
```bash
npm install @zag-js/react
# or
yarn add @zag-js/react
```
> Congrats! You're ready to use tooltip machine in your project.
## Using the machine
Here's an example of the tooltip machine used in a React.js project.
```jsx
import * as tooltip from "@zag-js/tooltip"
import { useMachine, normalizeProps } from "@zag-js/react"
export function Tooltip() {
const service = useMachine(tooltip.machine, { id: "1" })
const api = tooltip.connect(service, normalizeProps)
return (
<>
{api.open && (
Tooltip
)}
>
)
}
```
### Usage with Vue 3 (JSX)
Zag works seamlessly with Vue's JSX approach. Here's how to use the same tooltip
logic in Vue:
```jsx
import * as tooltip from "@zag-js/tooltip"
import { normalizeProps, useMachine } from "@zag-js/vue"
import { computed, defineComponent, h, Fragment } from "vue"
export default defineComponent({
name: "Tooltip",
setup() {
const service = useMachine(tooltip.machine, { id: "1" })
const apiRef = computed(() => tooltip.connect(service, normalizeProps))
return () => {
const api = apiRef.current
return (
<>
{api.open && (
Tooltip
)}
>
)
}
},
})
```
There are some extra functions that need to be used in order to make it work:
- `normalizeProps` - Converts the props of the component into the format that is
compatible with Vue.
- `computed` - Ensures that the tooltip's `api` is always up to date with the
current state of the machine.
### Usage with Solid.js
We love Solid.js and we've added support for it. Here's how to use the same
tooltip logic in Solid:
```jsx
import * as tooltip from "@zag-js/tooltip"
import { normalizeProps, useMachine } from "@zag-js/solid"
import { createMemo, createUniqueId, Show } from "solid-js"
export function Tooltip() {
const service = useMachine(tooltip.machine, { id: createUniqueId() })
const api = createMemo(() => tooltip.connect(service, normalizeProps))
return (
Tooltip
)
}
```
There are some extra functions that need to be used in order to make it work:
- `normalizeProps` - Converts the props of the component into the format that is
compatible with Solid.
- `createMemo` - Ensures that the tooltip's `api` is always up to date with the
current state of the machine.
### Usage with Svelte
Here's how to use the same tooltip logic in Svelte:
```html
{#if api.open}
Tooltip
{/if}
```
There are some extra functions that need to be used in order to make it work:
- `normalizeProps` - Converts the props of the component into the format that is
compatible with Svelte.
- `$derived` - Ensures that the tooltip's `api` is always up to date with the
current state of the machine.
### About prop normalization
There are subtle difference between how JSX attributes are named across
frameworks like React, Solid, Vue and Svelte. Here are some examples:
**Keydown listener**
- React and Solid: The keydown listener property is `onKeyDown`.
- Vue: The keydown listener property is `onKeydown`.
**Styles**
- React: Pass a numeric value for margin attributes like `{ marginBottom: 4 }`.
- Solid: It has to be `{ "margin-bottom": "4px" }`.
- Vue: You need to ensure the value is a string with unit.
`{ marginBottom: "4px" }`.
These little nuances between frameworks are handled automatically when you use
`normalizeProps`.
> The goal of Zag is to help you abstract the interaction and accessibility
> patterns into a statechart so you never have to re-invent the wheel.
Thanks for reading! If you're curious about how state machines work, the next
page will give you a quick overview.
# What is a state machine?
A state machine is a tool for modeling stateful, reactive systems. It is useful
for declaratively describing the behavior of an application or component.
To model any logic as using the state machine pattern, it must have:
- A finite number of states.
- A finite number of transitions between states.
## Example of state machine
Consider a simple toggle or switch component that consists of two states,
`active` and `inactive`. The initial state will be `active`
The supported transitions that can happen here:
- In the `active` state, when we click the toggle, it should transition to the
`inactive` state
- In the `inactive` state, when we click the toggle, it should transition to the
`active` state
Here's how we'll model the logic in code:
```jsx
import { createMachine } from "@zag-js/core"
const machine = createMachine({
// initial state
initialState() {
return "active"
},
// the finite states
states: {
active: {
on: {
CLICK: {
// go to inactive
target: "inactive",
},
},
},
inactive: {
on: {
CLICK: {
// go to active
target: "active",
},
},
},
},
})
```
The `machine` gives you access to these key information:
- `state`: State is a representation of the machine at a specific point in time
and contains the following properties:
- `value`: the current state value
- `context`: the current context or data stored in the machine
- `event`: the event object that triggered the transition of this current
state
- `matches(...)`: the function to check whether the machine is in a specific
state. It's similar to `state.value === `
- `send`: A function to send events or signals to the machine.
Now that we've modelled the component logic, let's map that to DOM attributes
and event handlers following the
[WAI-ARIA](https://www.w3.org/TR/wai-aria-practices-1.1/examples/checkbox/checkbox-1/checkbox-1.html)
specification for the switch component.
We'll write a function called `connect` to do this.
```jsx
function connect(service) {
const active = state.matches("active")
return {
active,
getButtonProps() {
type: "button",
role: "switch",
"aria-checked": active,
onClick() {
send("CLICK")
},
},
}
}
```
Here's how to consume the toggle machine logic and connect in React.js.
```jsx
import { useMachine, normalizeProps } from "@zag-js/react"
import { machine, connect } from "./toggle"
function Toggle() {
const service = useMachine(machine)
const api = connect(service, normalizeProps)
return (
)
}
```
That's it! Now you've learned the fundamentals of a component state machine.
# Styling
Zag's API is intentionally very low level and unstyled, giving you control over
how you want to style them. Unlike Chakra UI, it provides no styles or UI, just
behavior or logic, accessibility and helpful methods.
Each machine provides an idea of the DOM structure and styling guide to show you
how to style the states and parts.
## Styling a component part
Every component comprises of multiple parts that can be styled. For example, the
dialog component has the following parts: content, trigger, title, and backdrop.
The `data-part` attribute can be used to select and style a component part.
Here's what a sample HTML for the dialog looks like:
```html
Dialog Title
Dialog Description
```
You can style each part using the CSS attribute selector.
```css
[data-part="backdrop"] {
background-color: #000;
opacity: 0.5;
}
[data-part="content"] {
padding: 24px;
border-radius: 6px;
}
```
## Styling a state
When a component or its parts can have multiple states, we automatically attach
`data-*` attributes that represents the specific state. For example, an
accordion's trigger can have:
- `data-disabled` — When the trigger is disabled.
- `data-expanded` — When the trigger is expanded.
You can style the accordion's trigger using the CSS attribute selector.
```css
[data-part="trigger"][data-expanded] {
background: red;
}
[data-part="trigger"][data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
```
You'll see this pattern across every components within the library.
> Zag was designed to encapsulate logic, accessibility and interactions, while
> giving you full control over styling.
# Composition
## Event composition
Zag encourages the use of spread props to ensure we automatically attach all the
event handlers and properties to elements.
Sometimes, you may want to attach your own event handlers to the elements. To do
that, import the `mergeProps` utility provided by zag for your framework.
```jsx
// 1. import mergeProps utility
import { useMachine, mergeProps } from "@zag-js/react"
import * as hoverCard from "@zag-js/hover-card"
export function Toggle() {
const service = useMachine(hoverCard.machine, {
id: "1",
})
const api = hoverCard.connect(service)
// 2. write your custom event handlers
const handleHover = () => {
// do something
}
// 3. merge the props
const triggerProps = mergeProps(api.getTriggerProps(), {
onPointerEnter: handleHover,
})
return (
// 4. spread the new props
{api.open ? "Open" : "Close"}
)
}
```
## Id composition
Zag depends heavily on pure DOM queries to identify elements. This means every
element part needs to have a unique id.
Each time you initiate the machine with the `useMachine` hook, you'll need to
ensure that you provide a unique id.
In most cases, you can rely on the framework providing a unique id for each machine.
### React
```jsx
import * as accordion from "@zag-js/accordion"
import { useMachine, normalizeProps } from "@zag-js/framework"
import { useId } from "react"
function Component() {
const service = useMachine(accordion.machine, { id: useId() })
const api = machine.connect(service, normalizeProps)
// ...
}
```
See [useId](https://react.dev/reference/react/useId).
### Solid
```jsx
import * as accordion from "@zag-js/accordion"
import { useMachine, normalizeProps } from "@zag-js/solid"
import { createUniqueId } from "solid-js"
function Component() {
const service = useMachine(accordion.machine, { id: createUniqueId() })
const api = machine.connect(service, normalizeProps)
// ...
}
```
See [createUniqueId](https://docs.solidjs.com/reference/component-apis/create-unique-id).
### Vue
```html
```
See [useId](https://vuejs.org/api/composition-api-helpers#useid).
### Svelte
```html
```
See [$props.id](https://svelte.dev/docs/svelte/$props#$props.id()).
Internally, Zag maps the unique id provided to each component parts needed for
the widget to work.
In some cases, you might want to compose different machines together in a single
component. For example, you want to use the same trigger as a popover and
tooltip trigger at the same time.
To achieve this, you will need to pass custom `ids` to the machine's context.
This will ensure that calling `document.getElementById(...)` within the tooltip
and/or popover will return the same element.
```tsx {6,10}
import * as tooltip from "@zag-js/tooltip"
import * as popover from "@zag-js/popover"
function Example() {
const tooltipService = useMachine(tooltip.machine, {
ids: { trigger: "id-1" },
})
const popoverService = useMachine(popover.machine, {
ids: { trigger: "id-1" },
})
// ...
}
```
In the example above, you will notice that the popover and tooltip trigger share
the same id. That's how to compose machines together.
## Custom window environment
Internally, we use DOM query methods like `document.querySelectorAll` and
`document.getElementById` to locate elements within the machine.
In custom environments like iframe, Shadow DOM, Electron, etc., the machine
might not work as expected because `document` may not be available.
To provide the correct reference to root node or document, you can pass
`getRootNode` function it to the machine's context.
> In shadow DOM, the root node can be derived from calling
> `element.getRootNode()` method on any element.
```jsx {12,16,42}
import * as accordion from "@zag-js/accordion"
import { useMachine, normalizeProps } from "@zag-js/react"
import Frame, { useFrame } from "react-frame-component"
const data = [
{ title: "Watercraft", content: "Sample accordion content" },
{ title: "Automobiles", content: "Sample accordion content" },
{ title: "Aircraft", content: "Sample accordion content" },
]
function Accordion({ id }) {
const { document } = useFrame()
const service = useMachine(accordion.machine, {
id,
getRootNode: () => document,
})
const api = accordion.connect(service, normalizeProps)
return (
{data.map((item, index) => (
{item.content}
))}
)
}
export default function App() {
return (
ZagJs in Iframe
)
}
```
# Collection
The Collection class is designed to manage a collection of items, providing
functionalities such as sorting, searching, getting next or previous items,
converting items to values or strings, checking if an item is disabled, and
more.
> **Good to know**: This is used in the select and combobox components
## List Collection
A list collection is a collection that is based on an array of items. It is
created by passing an array of items to the constructor.
```ts
import { ListCollection } from "@zag-js/collection"
const collection = new ListCollection({
items: [
{ label: "Apple", value: "apple" },
{ label: "Banana", value: "banana" },
],
})
```
### Converting value to item
You can convert a value to an item by passing the value to the `find` or
`findMany` method.
```ts
const item = collection.find("banana")
console.log(item) // { label: "Banana", value: "banana" }
const items = collection.findMany(["apple", "banana"])
console.log(items) // [{ label: "Apple", value: "apple" }, { label: "Banana", value: "banana" }]
```
### Value Traversal
You can get the next or previous item based on a value by passing the value to
the `getNextValue` or `getPreviousValue` method.
```ts
const nextValue = collection.getNextValue("apple")
console.log(nextValue) // banana
const previousItem = collection.getPreviousValue("banana")
console.log(previousItem) // apple
```
Likewise, you can also get the first or last item by calling the `firstValue` or
`lastValue` computed properties.
```ts
console.log(collection.firstValue) // apple
console.log(collection.lastValue) // banana
```
### Check for value existence
You can check if a value exists in the collection by calling the `has` method.
```ts
const hasValue = collection.has("apple")
console.log(hasValue) // true
```
### Working with custom objects
If you are working with custom objects, you can pass a function to the
`itemToString` and `getItemValue` options to specify how to convert an item to a
string and a value, respectively.
```ts
import { ListCollection } from "@zag-js/collection"
const collection = new ListCollection({
items: [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "cherry" },
],
itemToString: (item) => item.name,
itemToValue: (item) => item.id,
})
```
To disable an item, you can pass a function to the `isItemDisabled` option.
```ts
import { ListCollection } from "@zag-js/collection"
const collection = new ListCollection({
items: [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "cherry" },
],
isItemDisabled: (item) => item.id === 2,
})
```
### Reorder items
You can reorder items by calling the `reorder` method and passing the starting
index and the ending index of the item to be moved.
```ts
const fromIndex = 1 // Banana
const toIndex = 0 // Apple
collection.reorder(fromIndex, toIndex)
console.log(collection.items) // [{ label: "Banana", value: "banana" }, { label: "Apple", value: "apple" }]
```
# Programmatic Control
In some cases, you may want to control the state or context values of a machine
programmatically via its `props` or based on certain conditions. This is
typically known as "controlling" the components.
Zag provides a number of ways to control the state of a machine
programmatically.
## Setting initial context
All machines support setting the controlled and uncontrolled values for context
properties. For example:
- `defaultOpen` and `open` for controlling the open state of disclosure
components
- `defaultValue` and `value` for controlling the value of input machines
For example, if you want an accordion to start with a specific selected value.
Here's how to achieve that:
```tsx
const service = useMachine(accordion.machine, {
defaultValue: ["item-1"],
})
```
## Controlled Usage
You can pass the context value to the `useMachine` hook directly and provide the
`onValueChange` callback to react to the changes.
```jsx
const service = useMachine(accordion.machine, {
value: props.value,
onValueChange(details) {
console.log(details)
},
})
```
## Using exposed methods
The `connect` method of the machines provide helpful methods (APIs) to change
the machine state or update its context.
> This approach is the recommended approach to programmatically update a
> machine.
Let's say we'd like to change the expanded accordion item in an accordion group.
Here's how to do that:
```jsx
function Accordion() {
// 1. Bind the machine in your framework
const service = useMachine(accordion.machine)
// 2. Call the connect function
const api = accordion.connect(service)
// 3. Use exposed methods
api.setValue("item-1")
return (...)
}
```
# LLMs.txt
## What is LLMs.txt?
We support [LLMs.txt](https://llmstxt.org/) files for making the Zag JS
documentation available to large language models (LLMs). This feature helps AI
tools better understand our component library, its APIs, and usage patterns.
## Available Routes
We provide several LLMs.txt routes to help AI tools access our documentation:
- - Contains a structured overview of all
components and their documentation links
- - Provides comprehensive documentation
including implementation details and examples
- - React-specific documentation and
implementation details
- - SolidJS-specific documentation and
implementation details
- - Vue-specific documentation and
implementation details
- - Svelte-specific documentation and
implementation details
## Usage with AI Tools
### Cursor
Use the `@Docs` feature in Cursor to include the LLMs.txt files in your project.
This helps Cursor provide more accurate code suggestions and documentation for
Zag JS components.
[Read more about @Docs in Cursor](https://docs.cursor.com/context/@-symbols/@-docs)
### Windstatic
Reference the LLMs.txt files using `@` or in your `.windsurfrules` files to
enhance Windstatic's understanding of Zag JS components.
[Read more about Windstatic Memories](https://docs.codeium.com/windsurf/memories#memories-and-rules)
### Other AI Tools
Any AI tool that supports LLMs.txt can use these routes to better understand Zag
JS. Simply point your tool to any of the routes above based on your framework of
choice.
# Frequently Asked Questions
## Why the need for `normalizeProps`?
The goal of `normalizeProps` is to convert the props of the component into the
format that is compatible with the respective framework.
It is also used to ensure that the returned properties are strongly typed.
There are subtle difference between how element attributes are named across
frameworks like React, Solid and Vue. Here are some examples:
**Keydown listener**
- React and Solid: The keydown listener property is `onKeyDown`.
- Vue: The keydown listener property is `onKeydown`.
**Styles**
- React: Pass a numeric value for margin attributes like `{ marginBottom: 4 }`.
- Solid: It has to be `{ "margin-bottom": "4px" }`.
- Vue: You need to ensure the value is a string with unit.
`{ marginBottom: "4px" }`.
These little nuances between frameworks are handled automatically when you use
`normalizeProps`.
## How can I attach custom extra event handlers to the elements?
See the approach [here](/overview/composition#event-composition).
## How can I get Zag working in a custom window environment?
See the approach [here](/overview/composition#custom-window-environment).
## What would it take to support other frameworks?
We're currently interested in supporting as many frameworks as possible. The key
requirements are:
- **Support for "spread props"**: The framework should have support for
spreading attributes and event handlers.
- **Exposed Typings**: The framework should expose the typings for the
attributes and event handlers. This is optional but would provide the best DX.
## How do I upgrade all zag dependencies?
Since we use independent versioning for each zag package, it can sometimes be
inconvenient to upgrade all dependencies individually.
You can use scoped upgrades feature in your package manager to update all zag
packages seamlessly.
```bash
pnpm up "@zag-js/*"
# or
yarn upgrade --scope @zag-js
# or
npm up @zag-js/...
```
# Migration Guide
After years of refinement and iteration, we've cemented Zag to work seamlessly
across major JavaScript frameworks.
Now, we're taking things to the next level by focusing on:
- **Performance**: Improving the runtime and rendering performance of every
component
- **Bundle Size**: Reducing the gross bundle size of each component + framework
adapters
We achieved this by **moving from an external store to native reactive
primitives** provided by each framework.
Our rigorous performance testing, which involved stress-testing with 10,000
instances of the same component, revealed roughly **1.5x - 4x** performance
improvements across components. [View Breakdown](#performance)
## Changed
The major changes are quite simple, and are listed below:
### useMachine
`useMachine` now returns a `service` object instead of a tuple of
`[state, send]`.
This change is the same across all components. Using "find and replace" will
help you migrate faster.
**Before**
```tsx
const [state, send] = useMachine(avatar.machine({ id: useId() }))
```
**After**
```tsx
const service = useMachine(avatar.machine, { id: useId() })
```
> Notice that `avatar.machine` is no longer a function, it is passed directly to
> `useMachine`.
#### Caveats
Due to the switch from `.machine()` as a function to
`.machine` as an object, the TS inference is limited for generic
components like combobox and select.
To help with this, we've exported an equivalent `.Machine` type to
help with the casting.
```ts
useMachine(combobox.machine as combobox.Machine)
```
### Controlled vs Uncontrolled value
Managing controlled and uncontrolled values is a fairly common need in most
component libraries.
Previously, we handled this by providing initial and controlled context to the
machine.
```tsx
/// 👇🏻 Default value
const [state, send] = useMachine(numberInput.machine({ value: "10" }), {
context: {
// 👇🏻 Controlled value
value: "10",
},
})
```
This can be initially confusing to users and is error prone.
Now, we've moved the logic to the machine itself. Allowing users to explicitly
provide a default value and a controlled value.
```tsx
const service = useMachine(numberInput.machine, {
// 👇🏻 Default value
defaultValue: "10",
// 👇🏻 Controlled value
value: "10",
})
```
> This change applies all component with some form of `value` prop.
### Controlled vs Uncontrolled open
Previously, we handled controlled and uncontrolled open states by providing
initial `open` state and an additional `open.controlled` property.
```tsx
// 👇🏻 Default value
const [state, send] = useMachine(dialog.machine({ open: true }), {
context: {
// 👇🏻 Controlled value
open: true,
"open.controlled": true,
},
})
```
Now, we've moved the logic to the machine itself. Allowing users to explicitly
provide a default and controlled open state.
```tsx
const service = useMachine(dialog.machine, {
// 👇🏻 Default value
defaultOpen: true,
// 👇🏻 Controlled value
open: true,
})
```
### Typings
`.Context` is now renamed to `.Props`
**Before**
```tsx
import * as accordion from "@zag-js/accordion"
interface Props extends accordion.Context {}
```
**After**
```tsx
import * as accordion from "@zag-js/accordion"
interface Props extends accordion.Props {}
```
### Toast
The toast component new requires that you create a toast store (or manager), and
pass that store to the toast group machine.
This store is to be used in userland to create and manage toasts.
> Refer to the [toast](/components/react/toast) documentation for more details.
**Before**
```tsx
const [state, send] = useMachine(
toast.group.machine({
overlap: false,
placement: "bottom",
}),
)
const toaster = toast.group.connect(state, send, normalizeProps)
// propagate the `toaster` via context and use it in your app.
toaster.create({
title: "Hello",
description: "World",
})
```
**After**
```tsx
const toaster = toast.createStore({
overlap: false,
placement: "bottom",
})
const service = useMachine(toast.group.machine, {
store: toaster,
})
// use the `toaster` store to create and manage toasts. No need for context.
toaster.create({
title: "Hello",
description: "World",
})
```
For Solid.js users, we recommend using `` exported from `@zag-js/solid`
when mapping over the toasts, instead of the `` component.
## Fixed
- **Menu**: Fix issue where context menu doesn't update positioning on
subsequent right clicks.
- **Avatar**: Fix issue where `api.setSrc` doesn't work.
- **File Upload**: Fix issue where drag-and-drop doesn't work when `directory`
is `true`.
- **Carousel**
- Fix issue where initial page is not working.
- Fix issue where pagination sync broken after using dots indicators.
## Removed
- General
- Removed `useActor` hook in favor of `useMachine` everywhere.
- Removed `open.controlled` in favor of `defaultOpen` and `open` props.
- Pagination
- `api.setCount` is removed in favor of explicitly setting the `count` prop.
- Select, Combobox
- `api.setCollection` is removed in favor of explicitly setting the
`collection` prop.
## Performance
We measured the mount performance of 10k instances of each component, and
compared the before and after.
### Avatar
**Result**: ~27% faster mount time and ~99% faster update time
#**Before**
```sh
{phase: 'mount', duration: 1007.3000000119209}
{phase: 'update', duration: 890.4000000357628}
```
**#After:**
```sh
{phase: 'mount', duration: 736.9999999403954}
{phase: 'update', duration: 1.899999976158142}
```
### Accordion
**Result**: ~61% faster mount time and no update time
**Before**
```sh
{phase: 'mount', duration: 2778.4999997913837}
{phase: 'update', duration: 2.3000000715255737}
```
**After**
```sh
{phase: 'mount', duration: 1079.0000001490116}
```
### Collapsible
**Result**: ~65% faster mount time and no update time
**Before**
```sh
{phase: 'mount', duration: 834.4000000357628}
{phase: 'update', duration: 2.1999999284744263}
```
**After**
```sh
{phase: 'mount', duration: 290.3000001013279}
```
### Dialog
**Result**: ~80% faster mount time and no update time
**Before**
```sh
{phase: 'mount', duration: 688.9000000357628}
{phase: 'update', duration: 2.0000000298023224}
```
**After**
```sh
{phase: 'mount', duration: 135.50000008940697}
```
### Editable
**Result**: ~56% faster mount time and no update time
**Before**
```sh
{phase: 'mount', duration: 1679.500000089407}
{phase: 'update', duration: 2.0000000298023224}
```
**After**
```sh
{phase: 'mount', duration: 737.5999999940395}
```
### Tooltip
**Result**: ~82% faster mount time and no update time
**Before**
```sh
{phase: 'mount', duration: 797.7999999821186}
{phase: 'update', duration: 2.5999999940395355}
```
**After**
```sh
{phase: 'mount', duration: 139.9000000357628}
```
### Presence
**Result**: ~64% faster mount time and eliminated update time
**Before**
```sh
{ phase: "mount", duration: 1414 }
{ phase: "update", duration: 0 }
```
**After**
```sh
{ phase: "mount", duration: 502 }
```
### Tabs
**Result**: ~6% faster mount time
**Before**
```sh
{ phase: "mount", duration: 4120 }
{ phase: "update", duration: 2014 }
```
**After**
```sh
{ phase: "mount", duration: 3880 }
{ phase: "nested-update", duration: 3179 }
```
## Bundle Size
We've made significant strides in reducing the bundle size of the overall
library. The core package powers all components. It is now less than 2KB
minified, a whopping **98% reduction** in size.
**Before**: 13.78 KB
**After**: 1.52 KB
## Contributors Notes
- `activities` is now renamed to `effects`
- `prop`, `context` and `refs` are now explicitly passed to the machine. Prior
to this everything was pass to the `context` object which was quite expensive
(performance wise).
- The syntax for `watch` has changed significantly, refer to the new machines to
learn how it works. It is somewhat similar to how `useEffect` works in react.
- `createMachine` is just an identity function, it doesn't do anything. The
machine work is now moved to the framework `useMachine` hook.
## Thank you
We'd like to thank the following contributors for their help in making this
release possible:
- [Segun Adebayo](https://github.com/segunadebayo) for leading the charge and
making such engineering feats possible.
- [Christian Schroeter](https://github.com/cschroeter) for providing valuable
feedback and suggestions to improve the library.
## React
# Accordion
An accordion is a vertically stacked set of interactive headings containing a
title, content snippet, or thumbnail representing a section of content.
## Resources
[Latest version: v1.7.0](https://www.npmjs.com/package/@zag-js/accordion)
[Logic Visualizer](https://zag-visualizer.vercel.app/accordion)
[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/accordion)
**Features**
- Full keyboard navigation.
- Can expand one or multiple items.
- Collapse each accordion item.
## Installation
To use the accordion machine in your project, run the following command in your
command line:
```bash
npm install @zag-js/accordion @zag-js/react
# or
yarn add @zag-js/accordion @zag-js/react
```
This command will install the framework agnostic accordion logic and the
reactive utilities for your framework of choice.
## Anatomy
To set up the accordion correctly, you'll need to understand its anatomy and how
we name its parts.
> Each part includes a `data-part` attribute to help identify them in the DOM.
## Usage
First, import the accordion package into your project
```jsx
import * as accordion from "@zag-js/accordion"
```
The accordion package exports two key functions:
- `machine` — The state machine logic for the accordion widget.
- `connect` — The function that translates the machine's state to JSX attributes
and event handlers.
> You'll also need to provide a unique `id` to the `useMachine` hook. This is
> used to ensure that every part has a unique identifier.
Next, import the required hooks and functions for your framework and use the
accordion machine in your project 🔥
```jsx
import * as accordion from "@zag-js/accordion"
import { useMachine, normalizeProps } from "@zag-js/react"
import { useId } from "react"
const data = [
{ title: "Watercraft", content: "Sample accordion content" },
{ title: "Automobiles", content: "Sample accordion content" },
{ title: "Aircraft", content: "Sample accordion content" },
]
function Accordion() {
const service = useMachine(accordion.machine, { id: useId() })
const api = accordion.connect(service, normalizeProps)
return (
{data.map((item) => (
{item.content}
))}
)
}
```
You may have noticed we wrapped each accordion trigger within an `h3`. This is
recommended by the
[WAI-ARIA](https://www.w3.org/TR/wai-aria-practices-1.1/#wai-aria-roles-states-and-properties)
design pattern to ensure the accordion has the appropriate hierarchy on the
page.
## Opening multiple accordions at once
To allow multiple items to be expanded at once, set `multiple` to `true`. This
mode implicitly sets `collapsible` to `true` and ensures that each accordion can
be expanded.
```jsx {2}
const service = useMachine(accordion.machine, {
multiple: true,
})
```
## Opening specific accordions
To set the value of the accordion(s) that should be opened initially, pass the
`value` property to the machine function.
```jsx {3,4,9}
// for multiple accordions
const service = useMachine(accordion.machine, {
multiple: true,
defaultValue: ["home"],
})
// for single accordions
const service = useMachine(accordion.machine, {
defaultValue: ["home"],
})
```
## Toggle each accordion item
To collapse an already expanded accordion item by clicking on it, set the
context's `collapsible` property to `true`.
> Note: If `multiple` is `true`, we internally set `collapsible` to be `true`.
```jsx {2}
const service = useMachine(accordion.machine, {
collapsible: true,
})
```
## Listening for changes
When the accordion value changes, the `onValueChange` callback is invoked.
```jsx {2-5}
const service = useMachine(accordion.machine, {
onValueChange(details) {
// details => { value: string[] }
console.log("selected accordion:", details.value)
},
})
```
## Disabling an accordion item
To disable a specific accordion item, pass the `disabled: true` property to the
`getItemProps`, `getItemTriggerProps` and `getItemContentProps`.
When an accordion item is disabled, it is skipped from keyboard navigation and
can't be interacted with.
```jsx
//...
Content
//...
```
You can also disable the entire accordion items by passing `disabled` to the
machine's context.
```jsx {2}
const service = useMachine(accordion.machine, {
disabled: true,
})
```
## Styling guide
Earlier, we mentioned that each accordion part has a `data-part` attribute added
to them to select and style them in the DOM.
### Open and closed state
When an accordion item is expanded or collapsed, a `data-state` attribute is set
on the item, trigger and content elements. This attribute is removed when it is
closed.
```css
[data-part="item"][data-state="open|closed"] {
/* styles for the item is open or closed state */
}
[data-part="item-trigger"][data-state="open|closed"] {
/* styles for the item is open or closed state */
}
[data-part="item-content"][data-state="open|closed"] {
/* styles for the item is open or closed state */
}
```
### Focused state
When an accordion item's trigger is focused, a `data-focus` attribute is set on
the item and content.
```css
[data-part="item"][data-focus] {
/* styles for the item's focus state */
}
[data-part="item-trigger"]:focus {
/* styles for the trigger's focus state */
}
[data-part="item-content"][data-focus] {
/* styles for the content's focus state */
}
```
## Creating Component
Create your accordion component by abstracting the machine into your own
component.
### Usage
```tsx
import { Accordion } from "./your-accordion"
function Demo() {
return (
)
}
```
### Implementation
Use the the `splitProps` utility to separate the machine's props from the
component's props.
```tsx
import * as accordion from "@zag-js/accordion"
import { useMachine, normalizeProps } from "@zag-js/react"
import { useId } from "react"
interface Item {
value: string
title: React.ReactNode
content: React.ReactNode
}
export interface AccordionProps extends Omit {
items: Item[]
}
export function Accordion(props: AccordionProps) {
const [machineProps, localProps] = accordion.splitProps(props)
const service = useMachine(accordion.machine, {
id: useId(),
...machineProps,
})
const api = accordion.connect(service, normalizeProps)
return (
{localProps.items.map((item) => (
{item.content}
))}
)
}
```
## Methods and Properties
The accordion's `api` exposes the following methods and properties:
### Machine Context
The accordion machine exposes the following context properties:
**`ids`**
Type: `Partial<{ root: string; item(value: string): string; itemContent(value: string): string; itemTrigger(value: string): string; }>`
Description: The ids of the elements in the accordion. Useful for composition.
**`multiple`**
Type: `boolean`
Description: Whether multiple accordion items can be expanded at the same time.
**`collapsible`**
Type: `boolean`
Description: Whether an accordion item can be closed after it has been expanded.
**`value`**
Type: `string[]`
Description: The controlled value of the expanded accordion items.
**`defaultValue`**
Type: `string[]`
Description: The initial value of the expanded accordion items.
Use when you don't need to control the value of the accordion.
**`disabled`**
Type: `boolean`
Description: Whether the accordion items are disabled
**`onValueChange`**
Type: `(details: ValueChangeDetails) => void`
Description: The callback fired when the state of expanded/collapsed accordion items changes.
**`onFocusChange`**
Type: `(details: FocusChangeDetails) => void`
Description: The callback fired when the focused accordion item changes.
**`orientation`**
Type: `"horizontal" | "vertical"`
Description: The orientation of the accordion items.
**`dir`**
Type: `"ltr" | "rtl"`
Description: The document's text/writing direction.
**`id`**
Type: `string`
Description: The unique identifier of the machine.
**`getRootNode`**
Type: `() => ShadowRoot | Node | Document`
Description: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.
### Machine API
The accordion `api` exposes the following methods:
**`focusedValue`**
Type: `string`
Description: The value of the focused accordion item.
**`value`**
Type: `string[]`
Description: The value of the accordion
**`setValue`**
Type: `(value: string[]) => void`
Description: Sets the value of the accordion.
**`getItemState`**
Type: `(props: ItemProps) => ItemState`
Description: Gets the state of an accordion item.
### Data Attributes
**`Root`**
**`data-scope`**: accordion
**`data-part`**: root
**`data-orientation`**: The orientation of the accordion
**`Item`**
**`data-scope`**: accordion
**`data-part`**: item
**`data-state`**: "open" | "closed"
**`data-focus`**: Present when focused
**`data-disabled`**: Present when disabled
**`data-orientation`**: The orientation of the item
**`ItemContent`**
**`data-scope`**: accordion
**`data-part`**: item-content
**`data-state`**: "open" | "closed"
**`data-disabled`**: Present when disabled
**`data-focus`**: Present when focused
**`data-orientation`**: The orientation of the item
**`ItemIndicator`**
**`data-scope`**: accordion
**`data-part`**: item-indicator
**`data-state`**: "open" | "closed"
**`data-disabled`**: Present when disabled
**`data-focus`**: Present when focused
**`data-orientation`**: The orientation of the item
**`ItemTrigger`**
**`data-scope`**: accordion
**`data-part`**: item-trigger
**`data-orientation`**: The orientation of the item
**`data-state`**: "open" | "closed"
## Accessibility
### Keyboard Interactions
**`Space`**
Description: When focus is on an trigger of a collapsed item, the item is expanded
**`Enter`**
Description: When focus is on an trigger of a collapsed section, expands the section.
**`Tab`**
Description: Moves focus to the next focusable element
**`Shift + Tab`**
Description: Moves focus to the previous focusable element
**`ArrowDown`**
Description: Moves focus to the next trigger
**`ArrowUp`**
Description: Moves focus to the previous trigger.
**`Home`**
Description: When focus is on an trigger, moves focus to the first trigger.
**`End`**
Description: When focus is on an trigger, moves focus to the last trigger.
# Angle Slider
An angle slider is a circular dial that allows users to select an angle,
typically in degrees, within a 360° range. It provides an intuitive way to
control rotations or orientations, offering accessibility features.
## Resources
[Latest version: v1.7.0](https://www.npmjs.com/package/@zag-js/angle-slider)
[Logic Visualizer](https://zag-visualizer.vercel.app/angle-slider)
[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/angle-slider)
**Features**
- Fully managed keyboard navigation.
- Supports touch or click on track to update value.
- Supports Right-to-Left directionality.
## Installation
To use the angle slider machine in your project, run the following command in
your command line:
```bash
npm install @zag-js/angle-slider @zag-js/react
# or
yarn add @zag-js/angle-slider @zag-js/react
```
This command will install the framework agnostic angle slider logic and the
reactive utilities for your framework of choice.
## Anatomy
To set up the angle slider correctly, you'll need to understand its anatomy and
how we name its parts.
> Each part includes a `data-part` attribute to help identify them in the DOM.
## Usage
First, import the angle-slider package into your project
```jsx
import * as angleSlider from "@zag-js/angle-slider"
```
The angle slider package exports two key functions:
- `machine` — The state machine logic for the angle slider widget as described
in the WAI-ARIA spec.
- `connect` — The function that translates the machine's state to JSX attributes
and event handlers.
> You'll need to provide a unique `id` to the `useMachine` hook. This is used to
> ensure that every part has a unique identifier.
Next, import the required hooks and functions for your framework and use the
angle slider machine in your project 🔥
```jsx
import * as angleSlider from "@zag-js/angle-slider"
import { normalizeProps, useMachine } from "@zag-js/react"
export function AngleSlider() {
const service = useMachine(angleSlider.machine, { id: "1" })
const api = angleSlider.connect(service, normalizeProps)
return (
)
}
```
## Setting the initial value
```jsx {2}
const service = useMachine(angleSlider.machine, {
defaultValue: 45,
})
```
## Setting the value's granularity
By default, the granularity, is `1`, meaning that the value is always an
integer. You can change the step attribute to control the granularity.
For example, If you need a value between `5` and `10`, accurate to two decimal
places, you should set the value of step to `0.01`:
```jsx {2}
const service = useMachine(angleSlider.machine, {
step: 0.01,
})
```
## Listening for changes
When the angle slider value changes, the `onValueChange` and `onValueChangeEnd`
callbacks are invoked. You can use this to setup custom behaviors in your app.
```jsx {2-7}
const service = useMachine(angleSlider.machine, {
onValueChange(details) {
console.log("value is changing to:", details)
},
onValueChangeEnd(details) {
console.log("value has changed to:", details)
},
})
```
## Usage within forms
To use angle slider within forms, use the exposed `hiddenInputProps` from the
`connect` function and ensure you pass `name` value to the machine's context. It
will render a hidden input and ensure the value changes get propagated to the
form correctly.
```jsx {2}
const service = useMachine(angleSlider.machine, {
name: "wind-direction",
})
```
## Using angle slider marks
To show marks or ticks along the angle slider track, use the exposed
`api.getMarkerProps()` method to position the angle slider marks at desired
angles.
```jsx {7-11}
//...
//...
```
## Styling guide
Earlier, we mentioned that each angle slider part has a `data-part` attribute
added to them to select and style them in the DOM.
### Disabled State
When the angle slider is disabled, the `data-disabled` attribute is added to the
root, label, control, thumb and marker.
```css
[data-part="root"][data-disabled] {
/* styles for root disabled state */
}
[data-part="label"][data-disabled] {
/* styles for label disabled state */
}
[data-part="control"][data-disabled] {
/* styles for control disabled state */
}
[data-part="thumb"][data-disabled] {
/* styles for thumb disabled state */
}
[data-part="range"][data-disabled] {
/* styles for thumb disabled state */
}
```
### Invalid State
When the slider is invalid, the `data-invalid` attribute is added to the root,
track, range, label, and thumb parts.
```css
[data-part="root"][data-invalid] {
/* styles for root invalid state */
}
[data-part="label"][data-invalid] {
/* styles for label invalid state */
}
[data-part="control"][data-invalid] {
/* styles for control invalid state */
}
[data-part="valueText"][data-invalid] {
/* styles for output invalid state */
}
[data-part="thumb"][data-invalid] {
/* styles for thumb invalid state */
}
[data-part="marker"][data-invalid] {
/* styles for marker invalid state */
}
```
### Styling the markers
```css
[data-part="marker"][data-state="(at|under|over)-value"] {
/* styles for when the value exceeds the marker's value */
}
```
## Methods and Properties
### Machine Context
The slider machine exposes the following context properties:
**`ids`**
Type: `Partial<{ root: string; thumb: string; hiddenInput: string; control: string; valueText: string; }>`
Description: The ids of the elements in the machine.
Useful for composition.
**`step`**
Type: `number`
Description: The step value for the slider.
**`value`**
Type: `number`
Description: The value of the slider.
**`defaultValue`**
Type: `number`
Description: The initial value of the slider.
Use when you don't need to control the value of the slider.
**`onValueChange`**
Type: `(details: ValueChangeDetails) => void`
Description: The callback function for when the value changes.
**`onValueChangeEnd`**
Type: `(details: ValueChangeDetails) => void`
Description: The callback function for when the value changes ends.
**`disabled`**
Type: `boolean`
Description: Whether the slider is disabled.
**`readOnly`**
Type: `boolean`
Description: Whether the slider is read-only.
**`invalid`**
Type: `boolean`
Description: Whether the slider is invalid.
**`name`**
Type: `string`
Description: The name of the slider. Useful for form submission.
**`dir`**
Type: `"ltr" | "rtl"`
Description: The document's text/writing direction.
**`id`**
Type: `string`
Description: The unique identifier of the machine.
**`getRootNode`**
Type: `() => ShadowRoot | Node | Document`
Description: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.
### Machine API
The slider `api` exposes the following methods:
**`value`**
Type: `number`
Description: The current value of the angle slider
**`valueAsDegree`**
Type: `string`
Description: The current value as a degree string
**`setValue`**
Type: `(value: number) => void`
Description: Sets the value of the angle slider
**`dragging`**
Type: `boolean`
Description: Whether the slider is being dragged.
### Data Attributes
**`Root`**
**`data-scope`**: angle-slider
**`data-part`**: root
**`data-disabled`**: Present when disabled
**`data-invalid`**: Present when invalid
**`data-readonly`**: Present when read-only
**`Label`**
**`data-scope`**: angle-slider
**`data-part`**: label
**`data-disabled`**: Present when disabled
**`data-invalid`**: Present when invalid
**`data-readonly`**: Present when read-only
**`Control`**
**`data-scope`**: angle-slider
**`data-part`**: control
**`data-disabled`**: Present when disabled
**`data-invalid`**: Present when invalid
**`data-readonly`**: Present when read-only
**`Thumb`**
**`data-scope`**: angle-slider
**`data-part`**: thumb
**`data-disabled`**: Present when disabled
**`data-invalid`**: Present when invalid
**`data-readonly`**: Present when read-only
**`Marker`**
**`data-scope`**: angle-slider
**`data-part`**: marker
**`data-value`**: The value of the item
**`data-state`**:
**`data-disabled`**: Present when disabled
### Keyboard Interactions
**`ArrowRight`**
Description: Increments the angle slider based on defined step
**`ArrowLeft`**
Description: Decrements the angle slider based on defined step
**`ArrowUp`**
Description: Decreases the value by the step amount.
**`ArrowDown`**
Description: Increases the value by the step amount.
**`Shift + ArrowUp`**
Description: Decreases the value by a larger step
**`Shift + ArrowDown`**
Description: Increases the value by a larger step
**`Home`**
Description: Sets the value to 0 degrees.
**`End`**
Description: Sets the value to 360 degrees.
# Avatar
The Avatar component is a React component that represents a user avatar or
profile picture. It displays an image or initials within container.
Avatar provides support for fallback text or elements when the image fails to
load, or when the image is not provided.
## Resources
[Latest version: v1.7.0](https://www.npmjs.com/package/@zag-js/avatar)
[Logic Visualizer](https://zag-visualizer.vercel.app/avatar)
[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/avatar)
## Installation
To use the avatar machine in your project, run the following command in your
command line:
```bash
npm install @zag-js/avatar @zag-js/react
# or
yarn add @zag-js/avatar @zag-js/react
```
This command will install the framework agnostic avatar logic and the reactive
utilities for your framework of choice.
## Anatomy
To set up the avatar correctly, you'll need to understand its anatomy and how we
name its parts.
> Each part includes a `data-part` attribute to help identify them in the DOM.
## Usage
First, import the avatar package into your project
```jsx
import * as avatar from "@zag-js/avatar"
```
The avatar package exports two key functions:
- `machine` — The state machine logic for the avatar widget.
- `connect` — The function that translates the machine's state to JSX attributes
and event handlers.
> You'll also need to provide a unique `id` to the `useMachine` hook. This is
> used to ensure that every part has a unique identifier.
Next, import the required hooks and functions for your framework and use the
avatar machine in your project 🔥
```jsx
import * as avatar from "@zag-js/avatar"
import { useMachine, normalizeProps } from "@zag-js/react"
function Avatar() {
const service = useMachine(avatar.machine, { id: "1" })
const api = avatar.connect(service, normalizeProps)
return (
PA
)
}
```
## Listening for loading status changes
When the image has loaded or failed to load, the `onStatusChange` callback is
invoked.
```jsx {2}
const service = useMachine(avatar.machine, {
onStatusChange(details) {
// details => { status: "error" | "loaded" }
},
})
```
## Styling guide
Earlier, we mentioned that each avatar part has a `data-part` attribute added to
them to select and style them in the DOM.
```css
[data-scope="avatar"][data-part="root"] {
/* Styles for the root part */
}
[data-scope="avatar"][data-part="image"] {
/* Styles for the image part */
}
[data-scope="avatar"][data-part="fallback"] {
/* Styles for the fallback part */
}
```
## Creating Component
Create your avatar component by abstracting the machine into your own component.
### Usage
```tsx
import { Avatar } from "./your-avatar"
function Demo() {
return (
)
}
```
### Implementation
Use the the `splitProps` utility to separate the machine's props from the
component's props.
```tsx
import * as avatar from "@zag-js/avatar"
import { useMachine, normalizeProps } from "@zag-js/react"
export interface AvatarProps extends Omit {
/**
* The src of the avatar image
*/
src?: string
/**
* The srcSet of the avatar image
*/
srcSet?: string
/**
* The name of the avatar
*/
name: string
}
function Avatar(props: AvatarProps) {
const [machineProps, localProps] = avatar.splitProps(props)
const service = useMachine(avatar.machine, {
id: useId(),
...machineProps,
})
const api = avatar.connect(service, normalizeProps)
return (
{getInitials(localProps.name)}
)
}
function getInitials(name: string) {
return name
.split(" ")
.map((word) => word[0])
.join("")
}
```
## Methods and Properties
### Machine Context
The avatar machine exposes the following context properties:
**`onStatusChange`**
Type: `(details: StatusChangeDetails) => void`
Description: Functional called when the image loading status changes.
**`ids`**
Type: `Partial<{ root: string; image: string; fallback: string; }>`
Description: The ids of the elements in the avatar. Useful for composition.
**`id`**
Type: `string`
Description: The unique identifier of the machine.
**`getRootNode`**
Type: `() => ShadowRoot | Node | Document`
Description: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.
**`dir`**
Type: `"ltr" | "rtl"`
Description: The document's text/writing direction.
### Machine API
The avatar `api` exposes the following methods:
**`loaded`**
Type: `boolean`
Description: Whether the image is loaded.
**`setSrc`**
Type: `(src: string) => void`
Description: Function to set new src.
**`setLoaded`**
Type: `() => void`
Description: Function to set loaded state.
**`setError`**
Type: `() => void`
Description: Function to set error state.
### Data Attributes
**`Image`**
**`data-scope`**: avatar
**`data-part`**: image
**`data-state`**: "visible" | "hidden"
**`Fallback`**
**`data-scope`**: avatar
**`data-part`**: fallback
**`data-state`**: "hidden" | "visible"
# Carousel
an accessible carousel component that leverages native CSS Scroll Snap for
smooth, performant scrolling between slides.
## Resources
[Latest version: v1.7.0](https://www.npmjs.com/package/@zag-js/carousel)
[Logic Visualizer](https://zag-visualizer.vercel.app/carousel)
[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/carousel)
**Features**
- Uses native CSS Scroll Snap.
- Supports horizontal and vertical orientations.
- Supports alignment of slides (start, center or end alignment).
- Show multiple slides at a time.
- Supports looping and auto-playing.
- Supports custom spacing between slides.
## Installation
To use the carousel machine in your project, run the following command in your
command line:
```bash
npm install @zag-js/carousel @zag-js/react
# or
yarn add @zag-js/carousel @zag-js/react
```
This command will install the framework agnostic carousel logic and the reactive
utilities for your framework of choice.
## Anatomy
To set up the carousel correctly, you'll need to understand its anatomy and how
we name its parts.
> Each part includes a `data-part` attribute to help identify them in the DOM.
## Usage
First, import the carousel package into your project
```jsx
import * as carousel from "@zag-js/carousel"
```
The carousel package exports two key functions:
- `machine` — The state machine logic for the carousel widget.
- `connect` — The function that translates the machine's state to JSX attributes
and event handlers.
> You'll also need to provide a unique `id` to the `useMachine` hook. This is
> used to ensure that every part has a unique identifier.
Next, import the required hooks and functions for your framework and use the
carousel machine in your project 🔥
> **Note:** The carousel requires that you provide a `slideCount` property in
> the machine's context. This is the number of slides in the carousel.
```jsx
import * as carousel from "@zag-js/carousel"
import { normalizeProps, useMachine } from "@zag-js/react"
const items = [
"https://tinyurl.com/5b6ka8jd",
"https://tinyurl.com/7rmccdn5",
"https://tinyurl.com/59jxz9uu",
]
export function Carousel() {
const service = useMachine(carousel.machine, {
id: "1",
slideCount: items.length,
})
const api = carousel.connect(service, normalizeProps)
return (
{items.map((image, index) => (
))}
{api.pageSnapPoints.map((_, index) => (
))}
)
}
```
## Vertical carousel
To create a vertical carousel, set the `orientation` property in the machine's
context to `vertical`.
```jsx {2}
const service = useMachine(carousel.machine, {
orientation: "vertical",
})
```
## Setting the initial slide
To set the initial slide of the carousel, pass the `page` property to the
machine's context.
The `page` corresponds to the scroll snap position index based on the layout. It
does not necessarily correspond to the index of the slide in the carousel.
```jsx {2}
const service = useMachine(carousel.machine, {
defaultPage: 2,
})
```
## Setting the number of slides to show at a time
To customize number of slides to show at a time, set the `slidesPerPage`
property in the machine's context. The value must be an integer.
```jsx {2}
const service = useMachine(carousel.machine, {
slidesPerPage: 2,
})
```
## Setting the number of slides to move at a time
To customize number of slides to move at a time, set the `slidesPerMove`
property in the machine's context. The value must be an integer or `auto`.
> If the value is `auto`, the carousel will move the number of slides equal to
> the number of slides per page.
```jsx {2}
const service = useMachine(carousel.machine, {
slidesPerMove: 2,
})
```
> Ensure the `slidesPerMove` is less than or equal to the `slidesPerPage` to
> avoid skipping slides.
## Setting the carousel should loop around
To allow looping of slides, set the `loop` property in the machine's context to
`true`.
```jsx {2}
const service = useMachine(carousel.machine, {
loop: true,
})
```
## Setting the gap between slides
To customize spacing between slides, set the `spacing` property in the machine's
context to a valid CSS unit.
```jsx {2}
const service = useMachine(carousel.machine, {
spacing: "16px",
})
```
## Listening for page changes
When the carousel page changes, the `onPageChange` callback is invoked.
```jsx {2-5}
const service = useMachine(carousel.machine, {
onPageChange(details) {
// details => { page: number }
console.log("selected page:", details.page)
},
})
```
## Dragging the carousel
To allow dragging the carousel with the mouse, set the `allowMouseDrag` property
in the machine's context to `true`.
```jsx {2}
const service = useMachine(carousel.machine, {
allowMouseDrag: true,
})
```
## Autoplaying the carousel
To allow the carousel to autoplay, set the `autoplay` property in the machine's
context to `true`.
```jsx {2}
const service = useMachine(carousel.machine, {
autoplay: true,
})
```
Alternatively, you can configure the autoplay interval by setting the `delay`
property in the machine's context.
```jsx {2}
const service = useMachine(carousel.machine, {
autoplay: { delay: 2000 },
})
```
## Styling guide
Earlier, we mentioned that each carousel part has a `data-part` attribute added
to them to select and style them in the DOM.
```css
[data-part="root"] {
/* styles for the root part */
}
[data-part="item-group"] {
/* styles for the item-group part */
}
[data-part="item"] {
/* styles for the root part */
}
[data-part="control"] {
/* styles for the control part */
}
[data-part="next-trigger"] {
/* styles for the next-trigger part */
}
[data-part="prev-trigger"] {
/* styles for the prev-trigger part */
}
[data-part="indicator-group"] {
/* styles for the indicator-group part */
}
[data-part="indicator"] {
/* styles for the indicator part */
}
[data-part="autoplay-trigger"] {
/* styles for the autoplay-trigger part */
}
```
### Active state
When a carousel's indicator is active, a `data-current` attribute is set on the
indicator.
```css
[data-part="indicator"][data-current] {
/* styles for the indicator's active state */
}
```
## Methods and Properties
The carousel's `api` exposes the following methods and properties:
### Machine Context
The carousel machine exposes the following context properties:
**`ids`**
Type: `Partial<{ root: string; item(index: number): string; itemGroup: string; nextTrigger: string; prevTrigger: string; indicatorGroup: string; indicator(index: number): string; }>`
Description: The ids of the elements in the carousel. Useful for composition.
**`translations`**
Type: `IntlTranslations`
Description: The localized messages to use.
**`slidesPerPage`**
Type: `number`
Description: The number of slides to show at a time.
**`slidesPerMove`**
Type: `number | "auto"`
Description: The number of slides to scroll at a time.
When set to `auto`, the number of slides to scroll is determined by the
`slidesPerPage` property.
**`autoplay`**
Type: `boolean | { delay: number; }`
Description: Whether to scroll automatically. The default delay is 4000ms.
**`allowMouseDrag`**
Type: `boolean`
Description: Whether to allow scrolling via dragging with mouse
**`loop`**
Type: `boolean`
Description: Whether the carousel should loop around.
**`page`**
Type: `number`
Description: The controlled page of the carousel.
**`defaultPage`**
Type: `number`
Description: The initial page to scroll to when rendered.
Use when you don't need to control the page of the carousel.
**`spacing`**
Type: `string`
Description: The amount of space between items.
**`padding`**
Type: `string`
Description: Defines the extra space added around the scrollable area,
enabling nearby items to remain partially in view.
**`onPageChange`**
Type: `(details: PageChangeDetails) => void`
Description: Function called when the page changes.
**`inViewThreshold`**
Type: `number | number[]`
Description: The threshold for determining if an item is in view.
**`snapType`**
Type: `"proximity" | "mandatory"`
Description: The snap type of the item.
**`slideCount`**
Type: `number`
Description: The total number of slides.
Useful for SSR to render the initial ating the snap points.
**`onDragStatusChange`**
Type: `(details: DragStatusDetails) => void`
Description: Function called when the drag status changes.
**`onAutoplayStatusChange`**
Type: `(details: AutoplayStatusDetails) => void`
Description: Function called when the autoplay status changes.
**`dir`**
Type: `"ltr" | "rtl"`
Description: The document's text/writing direction.
**`id`**
Type: `string`
Description: The unique identifier of the machine.
**`getRootNode`**
Type: `() => ShadowRoot | Node | Document`
Description: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.
**`orientation`**
Type: `"horizontal" | "vertical"`
Description: The orientation of the element.
### Machine API
The carousel `api` exposes the following methods:
**`page`**
Type: `number`
Description: The current index of the carousel
**`pageSnapPoints`**
Type: `number[]`
Description: The current snap points of the carousel
**`isPlaying`**
Type: `boolean`
Description: Whether the carousel is auto playing
**`isDragging`**
Type: `boolean`
Description: Whether the carousel is being dragged. This only works when `draggable` is true.
**`canScrollNext`**
Type: `boolean`
Description: Whether the carousel is can scroll to the next view
**`canScrollPrev`**
Type: `boolean`
Description: Whether the carousel is can scroll to the previous view
**`scrollToIndex`**
Type: `(index: number, instant?: boolean) => void`
Description: Function to scroll to a specific item index
**`scrollTo`**
Type: `(page: number, instant?: boolean) => void`
Description: Function to scroll to a specific page
**`scrollNext`**
Type: `(instant?: boolean) => void`
Description: Function to scroll to the next page
**`scrollPrev`**
Type: `(instant?: boolean) => void`
Description: Function to scroll to the previous page
**`getProgress`**
Type: `() => number`
Description: Returns the current scroll progress as a percentage
**`play`**
Type: `() => void`
Description: Function to start/resume autoplay
**`pause`**
Type: `() => void`
Description: Function to pause autoplay
**`isInView`**
Type: `(index: number) => boolean`
Description: Whether the item is in view
**`refresh`**
Type: `() => void`
Description: Function to re-compute the snap points
and clamp the page
### Data Attributes
**`Root`**
**`data-scope`**: carousel
**`data-part`**: root
**`data-orientation`**: The orientation of the carousel
**`ItemGroup`**
**`data-scope`**: carousel
**`data-part`**: item-group
**`data-orientation`**: The orientation of the item
**`data-dragging`**: Present when in the dragging state
**`Item`**
**`data-scope`**: carousel
**`data-part`**: item
**`data-index`**: The index of the item
**`data-inview`**: Present when in viewport
**`data-orientation`**: The orientation of the item
**`Control`**
**`data-scope`**: carousel
**`data-part`**: control
**`data-orientation`**: The orientation of the control
**`PrevTrigger`**
**`data-scope`**: carousel
**`data-part`**: prev-trigger
**`data-orientation`**: The orientation of the prevtrigger
**`NextTrigger`**
**`data-scope`**: carousel
**`data-part`**: next-trigger
**`data-orientation`**: The orientation of the nexttrigger
**`IndicatorGroup`**
**`data-scope`**: carousel
**`data-part`**: indicator-group
**`data-orientation`**: The orientation of the indicatorgroup
**`Indicator`**
**`data-scope`**: carousel
**`data-part`**: indicator
**`data-orientation`**: The orientation of the indicator
**`data-index`**: The index of the item
**`data-readonly`**: Present when read-only
**`data-current`**: Present when current
**`AutoplayTrigger`**
**`data-scope`**: carousel
**`data-part`**: autoplay-trigger
**`data-orientation`**: The orientation of the autoplaytrigger
**`data-pressed`**: Present when pressed
# Checkbox
A checkbox allows users to make a binary choice, i.e. a choice between one of
two possible mutually exclusive options.
## Resources
[Latest version: v1.7.0](https://www.npmjs.com/package/@zag-js/checkbox)
[Logic Visualizer](https://zag-visualizer.vercel.app/checkbox)
[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/checkbox)
**Features**
- Tri-state checkbox. i.e. `indeterminate` state
- Syncs with `disabled` state of fieldset
- Syncs with form `reset` events
- Can be toggled programmatically
## Installation
To use the checkbox machine in your project, run the following command in your
command line:
```bash
npm install @zag-js/checkbox @zag-js/react
# or
yarn add @zag-js/checkbox @zag-js/react
```
This command will install the framework agnostic checkbox logic and the reactive
utilities for your framework of choice.
## Anatomy
To set up the checkbox correctly, you'll need to understand its anatomy and how
we name its parts.
> Each part includes a `data-part` attribute to help identify them in the DOM.
## Usage
First, import the checkbox package into your project
```jsx
import * as checkbox from "@zag-js/checkbox"
```
The checkbox package exports two key functions:
- `machine` — The state machine logic for the checkbox widget.
- `connect` — The function that translates the machine's state to JSX attributes
and event handlers.
Next, import the required hooks and functions for your framework and use the
checkbox machine in your project 🔥
```jsx
import * as checkbox from "@zag-js/checkbox"
import { useMachine, normalizeProps } from "@zag-js/react"
import { useId } from "react"
function Checkbox() {
const service = useMachine(checkbox.machine, { id: useId() })
const api = checkbox.connect(service, normalizeProps)
return (
)
}
```
## Indeterminate checkboxes
To make a checkbox indeterminate, set the context's `indeterminate` property to
true
```jsx {2}
const service = useMachine(checkbox.machine, {
indeterminate: true,
})
```
## Disabling the checkbox
To make a checkbox disabled, set the context's `disabled` property to true
```jsx {2}
const service = useMachine(checkbox.machine, {
disabled: true,
})
```
## Making it checked by default
To make a checkbox checked by default, set the context's `checked` property to
`true`
```jsx {2}
const service = useMachine(checkbox.machine, {
defaultChecked: true,
})
```
## Listening for changes
When the checkbox value changes, the `onCheckChange` callback is invoked.
```jsx {2-5}
const service = useMachine(checkbox.machine, {
onCheckChange(details) {
// details => { checked: boolean }
console.log("checkbox is:", details.checked)
},
})
```
## Usage within forms
To use checkbox within forms, use the exposed `hiddenInputProps` from the
`connect` function and ensure you pass `name` value to the machine's context. It
will render a hidden input and ensure the value changes get propagated to the
form correctly.
```jsx {2}
const service = useMachine(checkbox.machine, {
name: "fruits",
})
```
## Styling guide
Earlier, we mentioned that each checkbox part has a `data-part` attribute added
to them to select and style them in the DOM.
### Checked state
When the checkbox input is checked, the `data-checked` attribute is added to the
root, control and label parts.
```css
[data-part="root"][data-state="checked|unchecked|indeterminate"] {
/* styles for when checkbox is checked */
}
[data-part="control"][data-state="checked|unchecked|indeterminate"] {
/* styles for when checkbox is checked */
}
[data-part="label"][data-state="checked|unchecked|indeterminate"] {
/* styles for when checkbox is checked */
}
```
### Focused State
When the checkbox input is focused, the `data-focus` attribute is added to the
root, control and label parts.
```css
[data-part="root"][data-focus] {
/* styles for root focus state */
}
[data-part="control"][data-focus] {
/* styles for control focus state */
}
[data-part="label"][data-focus] {
/* styles for label focus state */
}
```
### Disabled State
When the checkbox is disabled, the `data-disabled` attribute is added to the
root, control and label parts.
```css
[data-part="root"][data-disabled] {
/* styles for root disabled state */
}
[data-part="control"][data-disabled] {
/* styles for control disabled state */
}
[data-part="label"][data-disabled] {
/* styles for label disabled state */
}
```
### Invalid State
When the checkbox is invalid, the `data-invalid` attribute is added to the root,
control and label parts.
```css
[data-part="root"][data-invalid] {
/* styles for root invalid state */
}
[data-part="control"][data-invalid] {
/* styles for control invalid state */
}
[data-part="label"][data-invalid] {
/* styles for label invalid state */
}
```
## Methods and Properties
### Machine Context
The checkbox machine exposes the following context properties:
**`ids`**
Type: `Partial<{ root: string; hiddenInput: string; control: string; label: string; }>`
Description: The ids of the elements in the checkbox. Useful for composition.
**`disabled`**
Type: `boolean`
Description: Whether the checkbox is disabled
**`invalid`**
Type: `boolean`
Description: Whether the checkbox is invalid
**`required`**
Type: `boolean`
Description: Whether the checkbox is required
**`checked`**
Type: `CheckedState`
Description: The controlled checked state of the checkbox
**`defaultChecked`**
Type: `CheckedState`
Description: The initial checked state of the checkbox when rendered.
Use when you don't need to control the checked state of the checkbox.
**`readOnly`**
Type: `boolean`
Description: Whether the checkbox is read-only
**`onCheckedChange`**
Type: `(details: CheckedChangeDetails) => void`
Description: The callback invoked when the checked state changes.
**`name`**
Type: `string`
Description: The name of the input field in a checkbox.
Useful for form submission.
**`form`**
Type: `string`
Description: The id of the form that the checkbox belongs to.
**`value`**
Type: `string`
Description: The value of checkbox input. Useful for form submission.
**`dir`**
Type: `"ltr" | "rtl"`
Description: The document's text/writing direction.
**`id`**
Type: `string`
Description: The unique identifier of the machine.
**`getRootNode`**
Type: `() => ShadowRoot | Node | Document`
Description: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.
### Machine API
The checkbox `api` exposes the following methods:
**`checked`**
Type: `boolean`
Description: Whether the checkbox is checked
**`disabled`**
Type: `boolean`
Description: Whether the checkbox is disabled
**`indeterminate`**
Type: `boolean`
Description: Whether the checkbox is indeterminate
**`focused`**
Type: `boolean`
Description: Whether the checkbox is focused
**`checkedState`**
Type: `CheckedState`
Description: The checked state of the checkbox
**`setChecked`**
Type: `(checked: CheckedState) => void`
Description: Function to set the checked state of the checkbox
**`toggleChecked`**
Type: `() => void`
Description: Function to toggle the checked state of the checkbox
### Data Attributes
**`Root`**
**`data-active`**: Present when active or pressed
**`data-focus`**: Present when focused
**`data-focus-visible`**: Present when focused with keyboard
**`data-readonly`**: Present when read-only
**`data-hover`**: Present when hovered
**`data-disabled`**: Present when disabled
**`data-state`**: "indeterminate" | "checked" | "unchecked"
**`data-invalid`**: Present when invalid
**`Label`**
**`data-active`**: Present when active or pressed
**`data-focus`**: Present when focused
**`data-focus-visible`**: Present when focused with keyboard
**`data-readonly`**: Present when read-only
**`data-hover`**: Present when hovered
**`data-disabled`**: Present when disabled
**`data-state`**: "indeterminate" | "checked" | "unchecked"
**`data-invalid`**: Present when invalid
**`Control`**
**`data-active`**: Present when active or pressed
**`data-focus`**: Present when focused
**`data-focus-visible`**: Present when focused with keyboard
**`data-readonly`**: Present when read-only
**`data-hover`**: Present when hovered
**`data-disabled`**: Present when disabled
**`data-state`**: "indeterminate" | "checked" | "unchecked"
**`data-invalid`**: Present when invalid
**`Indicator`**
**`data-active`**: Present when active or pressed
**`data-focus`**: Present when focused
**`data-focus-visible`**: Present when focused with keyboard
**`data-readonly`**: Present when read-only
**`data-hover`**: Present when hovered
**`data-disabled`**: Present when disabled
**`data-state`**: "indeterminate" | "checked" | "unchecked"
**`data-invalid`**: Present when invalid
## Accessibility
### Keyboard Interactions
**`Space`**
Description: Toggle the checkbox
# Clipboard
The clipboard machine allows users to quickly copy content to clipboard.
## Resources
[Latest version: v1.7.0](https://www.npmjs.com/package/@zag-js/clipboard)
[Logic Visualizer](https://zag-visualizer.vercel.app/clipboard)
[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/clipboard)
## Installation
To use the clipboard machine in your project, run the following command in your
command line:
```bash
npm install @zag-js/clipboard @zag-js/react
# or
yarn add @zag-js/clipboard @zag-js/react
```
This command will install the framework agnostic clipboard logic and the
reactive utilities for your framework of choice.
## Anatomy
To set up the clipboard correctly, you'll need to understand its anatomy and how
we name its parts.
> Each part includes a `data-part` attribute to help identify them in the DOM`.
## Usage
First, import the clipboard package into your project
```jsx
import * as clipboard from "@zag-js/clipboard"
```
The clipboard package exports two key functions:
- `machine` — The state machine logic for the clipboard widget.
- `connect` — The function that translates the machine's state to JSX attributes
and event handlers.
> You'll also need to provide a unique `id` to the `useMachine` hook. This is
> used to ensure that every part has a unique identifier.
Next, import the required hooks and functions for your framework and use the
clipboard machine in your project 🔥
```tsx
import * as clipboard from "@zag-js/clipboard"
import { useMachine, normalizeProps } from "@zag-js/react"
import { ClipboardCheck, ClipboardCopyIcon } from "lucide-react"
import { useId } from "react"
function Clipboard() {
const service = useMachine(clipboard.machine, {
id: useId(),
value: "https://github.com/chakra-ui/zag",
})
const api = clipboard.connect(service, normalizeProps)
return (
)
}
```
## Setting the value to copy
You can set the value to copy by passing a `value` prop to the `machine`
context.
```jsx {2}
const service = useMachine(clipboard.machine, {
value: "Hello, world!",
})
```
## Listening to copy events
When the value is copied to the clipboard, the `onStatusChange` event is fired.
You can listen to this event and perform any action you want.
```jsx {2}
const service = useMachine(clipboard.machine, {
onStatusChange: (details) => {
console.log("Copy status changed to", details.copied)
},
})
```
## Checking if the value is copied
Use the `api.copied` property to check if the value is copied to the clipboard.
```jsx {2}
const api = clipboard.connect(service)
if (api.copied) {
console.log("Value is copied to the clipboard")
}
```
## Changing the timeout
By default, the clipboard machine will automatically reset the state to `idle`
after `3000ms`. You can change this timeout by passing a `timeout` option to the
`machine` context.
```jsx {2}
const service = useMachine(clipboard.machine, {
timeout: 5000,
})
```
## Styling guide
Earlier, we mentioned that each clipboard part has a `data-part` attribute added
to them to select and style them in the DOM.
```css
[data-scope="clipboard"][data-part="root"] {
/* styles for the root part */
}
```
## Methods and Properties
### Machine Context
The clipboard machine exposes the following context properties:
**`ids`**
Type: `Partial<{ root: string; input: string; label: string; }>`
Description: The ids of the elements in the clipboard. Useful for composition.
**`value`**
Type: `string`
Description: The controlled value of the clipboard
**`defaultValue`**
Type: `string`
Description: The initial value to be copied to the clipboard when rendered.
Use when you don't need to control the value of the clipboard.
**`onValueChange`**
Type: `(details: ValueChangeDetails) => void`
Description: The function to be called when the value changes
**`onStatusChange`**
Type: `(details: CopyStatusDetails) => void`
Description: The function to be called when the value is copied to the clipboard
**`timeout`**
Type: `number`
Description: The timeout for the copy operation
**`id`**
Type: `string`
Description: The unique identifier of the machine.
**`getRootNode`**
Type: `() => ShadowRoot | Node | Document`
Description: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.
### Machine API
The clipboard `api` exposes the following methods:
**`copied`**
Type: `boolean`
Description: Whether the value has been copied to the clipboard
**`value`**
Type: `string`
Description: The value to be copied to the clipboard
**`setValue`**
Type: `(value: string) => void`
Description: Set the value to be copied to the clipboard
**`copy`**
Type: `() => void`
Description: Copy the value to the clipboard
### Data Attributes
**`Root`**
**`data-scope`**: clipboard
**`data-part`**: root
**`data-copied`**: Present when copied state is true
**`Label`**
**`data-scope`**: clipboard
**`data-part`**: label
**`data-copied`**: Present when copied state is true
**`Control`**
**`data-scope`**: clipboard
**`data-part`**: control
**`data-copied`**: Present when copied state is true
**`Input`**
**`data-scope`**: clipboard
**`data-part`**: input
**`data-copied`**: Present when copied state is true
**`data-readonly`**: Present when read-only
**`Trigger`**
**`data-scope`**: clipboard
**`data-part`**: trigger
**`data-copied`**: Present when copied state is true
# Collapsible
A collapsible is a component which expands and collapses a panel.
## Resources
[Latest version: v1.7.0](https://www.npmjs.com/package/@zag-js/collapsible)
[Logic Visualizer](https://zag-visualizer.vercel.app/collapsible)
[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/collapsible)
**Features**
- Can be controlled or uncontrolled.
- Works for width and height collapsibles.
## Installation
To use the collapsible machine in your project, run the following command in
your command line:
```bash
npm install @zag-js/collapsible @zag-js/react
# or
yarn add @zag-js/collapsible @zag-js/react
```
This command will install the framework agnostic collapsible logic and the
reactive utilities for your framework of choice.
## Usage
First, import the collapsible package into your project
```jsx
import * as collapsible from "@zag-js/collapsible"
```
The collapsible package exports two key functions:
- `machine` — The state machine logic for the collapsible widget.
- `connect` — The function that translates the machine's state to JSX attributes
and event handlers.
> You'll also need to provide a unique `id` to the `useMachine` hook. This is
> used to ensure that every part has a unique identifier.
Next, import the required hooks and functions for your framework and use the
collapsible machine in your project 🔥
```tsx
import * as collapsible from "@zag-js/collapsible"
import { normalizeProps, useMachine } from "@zag-js/react"
import { useId } from "react"
function Collapsible() {
const service = useMachine(collapsible.machine, { id: useId() })
const api = collapsible.connect(service, normalizeProps)
return (
Collape Content
)
}
```
## Listening for changes
When the collapsible state changes, the `onOpenChange` callback is invoked.
```jsx {2-5}
const service = useMachine(collapsible.machine, {
onOpenChange(details) {
// details => { open: boolean }
console.log("collapsible open:", details.open)
},
})
```
## Disabling the collapsible
Set the `disabled` machine context property to `true` to disable the
collapsible.
```jsx {2}
const service = useMachine(collapsible.machine, {
disabled: true,
})
```
## Animating the collapsible
Use CSS animations to animate the collapsible when it expands and collapses. The
`--height` and `--width` custom properties are attached to the content part.
```css
@keyframes expand {
from {
height: 0;
}
to {
height: var(--height);
}
}
@keyframes collapse {
from {
height: var(--height);
}
to {
height: 0;
}
}
[data-scope="collapsible"][data-part="content"] {
overflow: hidden;
max-width: 400px;
}
[data-scope="collapsible"][data-part="content"][data-state="open"] {
animation: expand 110ms cubic-bezier(0, 0, 0.38, 0.9);
}
[data-scope="collapsible"][data-part="content"][data-state="closed"] {
animation: collapse 110ms cubic-bezier(0, 0, 0.38, 0.9);
}
```
## Styling guide
Earlier, we mentioned that each collapsible part has a `data-part` attribute
added to them to select and style them in the DOM.
### Open and closed state
When a collapsible is expanded or collapsed, a `data-state` attribute is set on
the root, trigger and content elements. This attribute is removed when it is
closed.
```css
[data-part="root"][data-state="open|closed"] {
/* styles for the collapsible is open or closed state */
}
[data-part="trigger"][data-state="open|closed"] {
/* styles for the collapsible is open or closed state */
}
[data-part="content"][data-state="open|closed"] {
/* styles for the collapsible is open or closed state */
}
```
### Focused state
When a collapsible's trigger is focused, a `data-focus` attribute is set on the
root, trigger and content.
```css
[data-part="root"][data-focus] {
/* styles for the item's focus state */
}
[data-part="trigger"][data-focus] {
/* styles for the content's focus state */
}
[data-part="content"][data-focus] {
/* styles for the content's focus state */
}
```
## Methods and Properties
The collapsible's `api` exposes the following methods and properties:
### Machine Context
The collapsible machine exposes the following context properties:
**`ids`**
Type: `Partial<{ root: string; content: string; trigger: string; }>`
Description: The ids of the elements in the collapsible. Useful for composition.
**`open`**
Type: `boolean`
Description: The controlled open state of the collapsible.
**`defaultOpen`**
Type: `boolean`
Description: The initial open state of the collapsible when rendered.
Use when you don't need to control the open state of the collapsible.
**`onOpenChange`**
Type: `(details: OpenChangeDetails) => void`
Description: The callback invoked when the open state changes.
**`onExitComplete`**
Type: `VoidFunction`
Description: The callback invoked when the exit animation completes.
**`disabled`**
Type: `boolean`
Description: Whether the collapsible is disabled.
**`id`**
Type: `string`
Description: The unique identifier of the machine.
**`getRootNode`**
Type: `() => ShadowRoot | Node | Document`
Description: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.
**`dir`**
Type: `"ltr" | "rtl"`
Description: The document's text/writing direction.
### Machine API
The collapsible `api` exposes the following methods:
**`open`**
Type: `boolean`
Description: Whether the collapsible is open.
**`visible`**
Type: `boolean`
Description: Whether the collapsible is visible (open or closing)
**`disabled`**
Type: `boolean`
Description: Whether the collapsible is disabled
**`setOpen`**
Type: `(open: boolean) => void`
Description: Function to open or close the collapsible.
**`measureSize`**
Type: `() => void`
Description: Function to measure the size of the content.
### Data Attributes
**`Root`**
**`data-scope`**: collapsible
**`data-part`**: root
**`data-state`**: "open" | "closed"
**`Content`**
**`data-scope`**: collapsible
**`data-part`**: content
**`data-collapsible`**:
**`data-state`**: "open" | "closed"
**`data-disabled`**: Present when disabled
**`Trigger`**
**`data-scope`**: collapsible
**`data-part`**: trigger
**`data-state`**: "open" | "closed"
**`data-disabled`**: Present when disabled
## Accessibility
Adheres to the
[Disclosure WAI-ARIA design pattern](https://www.w3.org/WAI/ARIA/apg/patterns/disclosure).
### Keyboard Interactions
**`Space`**
Description: Opens/closes the collapsible.
**`Enter`**
Description: Opens/closes the collapsible.
# Color Picker
The color picker is an input widget used to select a color value from a
predefined list or a color area.
This component builds on top of the native `` experience and
provides a more customizable and consistent user experience.
## Resources
[Latest version: v1.7.0](https://www.npmjs.com/package/@zag-js/color-picker)
[Logic Visualizer](https://zag-visualizer.vercel.app/color-picker)
[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/color-picker)
**Features**
- Support for custom color area
- Support for RGBA, HSLA, HEX, and HSBA formats
- Support for channel inputs and sliders
- Support for mouse, touch, and keyboard interactions
- Support for form submission and reset events
- Support for named css colors
## Installation
To use the color picker machine in your project, run the following command in
your command line:
```bash
npm install @zag-js/color-picker @zag-js/react
# or
yarn add @zag-js/color-picker @zag-js/react
```
This command will install the framework agnostic color picker logic and the
reactive utilities for your framework of choice.
## Anatomy
To set up the color picker correctly, you'll need to understand its anatomy and
how we name its parts.
> Each part includes a `data-part` attribute to help identify them in the DOM.
## Usage
First, import the color picker package into your project
```jsx
import * as colorPicker from "@zag-js/color-picker"
```
The color picker package exports these functions:
- `machine` — The state machine logic for the color picker widget.
- `connect` — The function that translates the machine's state to JSX attributes
and event handlers.
- `parse` - The function that parses a color string to an Color object.
Next, import the required hooks and functions for your framework and use the
color picker machine in your project 🔥
```jsx
import * as colorPicker from "@zag-js/color-picker"
import { normalizeProps, useMachine } from "@zag-js/react"
import { useId } from "react"
function ColorPicker() {
const service = useMachine(colorPicker.machine, {
id: useId(),
defaultValue: colorPicker.parse("hsl(0, 100%, 50%)"),
})
const api = colorPicker.connect(service, normalizeProps)
return (
)
}
```
## Setting the initial value
To set the initial value of the color picker, use the `value` context property.
```jsx
const [current, send] = useMachine(colorPicker.machine, {
defaultValue: colorPicker.parse("#ff0000"),
})
```
## Listening for change events
When the user selects a color using the color picker, the `onValueChange` and
`onValueChangeEnd` events will be fired.
- `onValueChange` — Fires in sync as the user selects a color
- `onValueChangeEnd` — Fires when the user stops selecting a color (useful for
debounced updates)
```jsx
const [current, send] = useMachine(colorPicker.machine, {
onValueChange: (details) => {
// details => { value: Color, valueAsString: string }
},
onValueChangeEnd: (details) => {
// details => { value: Color, valueAsString: string }
},
})
```
> When using the `onValueChange` method in React.js, you might need to use the
> `flushSync` method from `react-dom` to ensure the value is updated in sync
## Using a custom color format
By default, the color picker's output format is `rgba`. You can change this
format to either `hsla` or `hsba` by using the `format` context property.
When this property is set, the `value` and `valueAsString` properties of the
`onValueChange` event will be updated to reflect the new format.
```jsx
const [current, send] = useMachine(colorPicker.machine, {
format: "hsla",
onValueChange: (details) => {
// details => { value: HSLAColor, valueAsString: string }
},
})
```
## Showing color presets
Adding color presets in form of swatches can help users pick colors faster. To
support this, use the `getSwatchTriggerProps(...)` and `getSwatchProps(...)` to
get the props needed to show the swatches buttons.
```tsx {18-31}
const ColorPicker = () => {
const service = useMachine(colorPicker.machine, {
id: useId(),
defaultValue: colorPicker.parse("hsl(0, 100%, 50%)"),
})
const api = colorPicker.connect(service, normalizeProps)
const presets = ["#ff0000", "#00ff00", "#0000ff"]
return (
{/* ... */}
{presets.map((preset) => (
))}
)
}
```
## Disabling the color picker
To disable user interactions with the color picker, set the `disabled` context
property to `true`.
```jsx
const [current, send] = useMachine(colorPicker.machine, {
disabled: true,
})
```
## Controlling the open and closed state
To control the open and closed state of the color picker, use the `open` and
`onOpenChange` context properties.
```jsx
const [current, send] = useMachine(colorPicker.machine, {
open: true,
onOpenChange: (details) => {
// details => { open: boolean }
},
})
```
You can also leverage the `api.setOpen(...)` method to control the open and
closed state of the color picker.
## Controlling individual color channel
In some cases, you may want to allow users to control the values of each color
channel individually. You can do this using an input element or a slider
element, or both.
To support this, use the `getChannelInputProps(...)` to show the channel inputs.
> Note: Make sure you only render the channel inputs that match the `format` of
> the color picker.
```tsx {16-38}
const ColorPicker = () => {
const service = useMachine(colorPicker.machine, {
id: useId(),
defaultValue: colorPicker.parse("hsl(0, 100%, 50%)"),
})
const api = colorPicker.connect(service, normalizeProps)
return (
{/* ... */}
{api.format === "rgba" && (
R
G
B
A
)}
)
}
```
## Showing a color preview
To display the value of a color, use the `getSwatchProps(...)` and pass the
color value. To show the current color value, use the `api.value`
```tsx {13-16}
const ColorPicker = () => {
const service = useMachine(colorPicker.machine, {
id: useId(),
defaultValue: colorPicker.parse("hsl(0, 100%, 50%)"),
})
const api = colorPicker.connect(service, normalizeProps)
return (
{/* ... */}
)
}
```
> You can pass `respectAlpha: false` to show the color value without the alpha
> channel
## Adding an eyedropper
The eye dropper tool is a native browser feature that allows a user pick a color
from a current page's canvas. To support this, use the
`getEyeDropperTriggerProps(...)`.
> **Note:** The eye dropper tool only works in Chrome and Edge browsers
```tsx {16-18}
const ColorPicker = () => {
const service = useMachine(colorPicker.machine, {
id: useId(),
defaultValue: colorPicker.parse("hsl(0, 100%, 50%)"),
})
const api = colorPicker.connect(service, normalizeProps)
return (
{/* ... */}
)
}
```
## Usage within forms
To use the color picker within a form, add the `name` context property to the
machine and render the visually hidden input using the `hiddenInputProps`.
```jsx {2}
const service = useMachine(colorPicker.machine, {
name: "color-preference",
})
```
## Styling guide
Each color picker part has a `data-part` attribute added to them to help you
identify and style them easily.
### Open and closed state
When the color picker is open or closed, the `data-state` attribute is added to
the trigger, content, control parts.
```css
[data-part="control"][data-state="open|closed"] {
/* styles for control open or state */
}
[data-part="trigger"][data-state="open|closed"] {
/* styles for control open or state */
}
[data-part="content"][data-state="open|closed"] {
/* styles for control open or state */
}
```
### Focused State
When the color picker is focused, the `data-focus` attribute is added to the
control and label parts.
```css
[data-part="control"][data-focus] {
/* styles for control focus state */
}
[data-part="label"][data-focus] {
/* styles for label focus state */
}
```
### Disabled State
When the color picker is disabled, the `data-disabled` attribute is added to the
label, control, trigger and option parts.
```css
[data-part="label"][data-disabled] {
/* styles for label disabled state */
}
[data-part="control"][data-disabled] {
/* styles for control disabled state */
}
[data-part="trigger"][data-disabled] {
/* styles for trigger disabled state */
}
[data-part="swatch-trigger"][data-disabled] {
/* styles for item disabled state */
}
```
### Swatch State
When a swatch's color value matches the color picker's value, the
`data-state=checked` attribute is added to the swatch part.
```css
[data-part="swatch-trigger"][data-state="checked|unchecked"] {
/* styles for swatch's checked state */
}
```
## Methods and Properties
### Machine Context
The color picker machine exposes the following context properties:
**`ids`**
Type: `Partial<{ root: string; control: string; trigger: string; label: string; input: string; hiddenInput: string; content: string; area: string; areaGradient: string; positioner: string; formatSelect: string; areaThumb: string; channelInput(id: string): string; channelSliderTrack(id: ColorChannel): string; }>`
Description: The ids of the elements in the color picker. Useful for composition.
**`value`**
Type: `Color`
Description: The controlled color value of the color picker
**`defaultValue`**
Type: `Color`
Description: The initial color value when rendered.
Use when you don't need to control the color value of the color picker.
**`disabled`**
Type: `boolean`
Description: Whether the color picker is disabled
**`readOnly`**
Type: `boolean`
Description: Whether the color picker is read-only
**`required`**
Type: `boolean`
Description: Whether the color picker is required
**`invalid`**
Type: `boolean`
Description: Whether the color picker is invalid
**`onValueChange`**
Type: `(details: ValueChangeDetails) => void`
Description: Handler that is called when the value changes, as the user drags.
**`onValueChangeEnd`**
Type: `(details: ValueChangeDetails) => void`
Description: Handler that is called when the user stops dragging.
**`onOpenChange`**
Type: `(details: OpenChangeDetails) => void`
Description: Handler that is called when the user opens or closes the color picker.
**`name`**
Type: `string`
Description: The name for the form input
**`positioning`**
Type: `PositioningOptions`
Description: The positioning options for the color picker
**`initialFocusEl`**
Type: `() => HTMLElement`
Description: The initial focus element when the color picker is opened.
**`open`**
Type: `boolean`
Description: The controlled open state of the color picker
**`defaultOpen`**
Type: `boolean`
Description: The initial open state of the color picker when rendered.
Use when you don't need to control the open state of the color picker.
**`format`**
Type: `ColorFormat`
Description: The controlled color format to use
**`defaultFormat`**
Type: `ColorFormat`
Description: The initial color format when rendered.
Use when you don't need to control the color format of the color picker.
**`onFormatChange`**
Type: `(details: FormatChangeDetails) => void`
Description: Function called when the color format changes
**`closeOnSelect`**
Type: `boolean`
Description: Whether to close the color picker when a swatch is selected
**`openAutoFocus`**
Type: `boolean`
Description: Whether to auto focus the color picker when it is opened
**`id`**
Type: `string`
Description: The unique identifier of the machine.
**`getRootNode`**
Type: `() => Node | ShadowRoot | Document`
Description: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.
**`dir`**
Type: `"ltr" | "rtl"`
Description: The document's text/writing direction.
**`onPointerDownOutside`**
Type: `(event: PointerDownOutsideEvent) => void`
Description: Function called when the pointer is pressed down outside the component
**`onFocusOutside`**
Type: `(event: FocusOutsideEvent) => void`
Description: Function called when the focus is moved outside the component
**`onInteractOutside`**
Type: `(event: InteractOutsideEvent) => void`
Description: Function called when an interaction happens outside the component
### Machine API
The color picker `api` exposes the following methods:
**`dragging`**
Type: `boolean`
Description: Whether the color picker is being dragged
**`open`**
Type: `boolean`
Description: Whether the color picker is open
**`value`**
Type: `Color`
Description: The current color value (as a string)
**`valueAsString`**
Type: `string`
Description: The current color value (as a Color object)
**`setValue`**
Type: `(value: string | Color) => void`
Description: Function to set the color value
**`getChannelValue`**
Type: `(channel: ColorChannel) => string`
Description: Function to set the color value
**`getChannelValueText`**
Type: `(channel: ColorChannel, locale: string) => string`
Description: Function to get the formatted and localized value of a specific channel
**`setChannelValue`**
Type: `(channel: ColorChannel, value: number) => void`
Description: Function to set the color value of a specific channel
**`format`**
Type: `ColorFormat`
Description: The current color format
**`setFormat`**
Type: `(format: ColorFormat) => void`
Description: Function to set the color format
**`alpha`**
Type: `number`
Description: The alpha value of the color
**`setAlpha`**
Type: `(value: number) => void`
Description: Function to set the color alpha
**`setOpen`**
Type: `(open: boolean) => void`
Description: Function to open or close the color picker
### Data Attributes
**`Root`**
**`data-scope`**: color-picker
**`data-part`**: root
**`data-disabled`**: Present when disabled
**`data-readonly`**: Present when read-only
**`data-invalid`**: Present when invalid
**`Label`**
**`data-scope`**: color-picker
**`data-part`**: label
**`data-disabled`**: Present when disabled
**`data-readonly`**: Present when read-only
**`data-invalid`**: Present when invalid
**`data-focus`**: Present when focused
**`Control`**
**`data-scope`**: color-picker
**`data-part`**: control
**`data-disabled`**: Present when disabled
**`data-readonly`**: Present when read-only
**`data-invalid`**: Present when invalid
**`data-state`**: "open" | "closed"
**`data-focus`**: Present when focused
**`Trigger`**
**`data-scope`**: color-picker
**`data-part`**: trigger
**`data-disabled`**: Present when disabled
**`data-readonly`**: Present when read-only
**`data-invalid`**: Present when invalid
**`data-placement`**: The placement of the trigger
**`data-state`**: "open" | "closed"
**`data-focus`**: Present when focused
**`Content`**
**`data-scope`**: color-picker
**`data-part`**: content
**`data-placement`**: The placement of the content
**`data-state`**: "open" | "closed"
**`ValueText`**
**`data-scope`**: color-picker
**`data-part`**: value-text
**`data-disabled`**: Present when disabled
**`data-focus`**: Present when focused
**`Area`**
**`data-scope`**: color-picker
**`data-part`**: area
**`data-invalid`**: Present when invalid
**`data-disabled`**: Present when disabled
**`data-readonly`**: Present when read-only
**`AreaBackground`**
**`data-scope`**: color-picker
**`data-part`**: area-background
**`data-invalid`**: Present when invalid
**`data-disabled`**: Present when disabled
**`data-readonly`**: Present when read-only
**`AreaThumb`**
**`data-scope`**: color-picker
**`data-part`**: area-thumb
**`data-disabled`**: Present when disabled
**`data-invalid`**: Present when invalid
**`data-readonly`**: Present when read-only
**`ChannelSlider`**
**`data-scope`**: color-picker
**`data-part`**: channel-slider
**`data-channel`**: The color channel of the channelslider
**`data-orientation`**: The orientation of the channelslider
**`ChannelSliderTrack`**
**`data-scope`**: color-picker
**`data-part`**: channel-slider-track
**`data-channel`**: The color channel of the channelslidertrack
**`data-orientation`**: The orientation of the channelslidertrack
**`ChannelSliderLabel`**
**`data-scope`**: color-picker
**`data-part`**: channel-slider-label
**`data-channel`**: The color channel of the channelsliderlabel
**`ChannelSliderValueText`**
**`data-scope`**: color-picker
**`data-part`**: channel-slider-value-text
**`data-channel`**: The color channel of the channelslidervaluetext
**`ChannelSliderThumb`**
**`data-scope`**: color-picker
**`data-part`**: channel-slider-thumb
**`data-channel`**: The color channel of the channelsliderthumb
**`data-disabled`**: Present when disabled
**`data-orientation`**: The orientation of the channelsliderthumb
**`ChannelInput`**
**`data-scope`**: color-picker
**`data-part`**: channel-input
**`data-channel`**: The color channel of the channelinput
**`data-disabled`**: Present when disabled
**`data-invalid`**: Present when invalid
**`data-readonly`**: Present when read-only
**`EyeDropperTrigger`**
**`data-scope`**: color-picker
**`data-part`**: eye-dropper-trigger
**`data-disabled`**: Present when disabled
**`data-invalid`**: Present when invalid
**`data-readonly`**: Present when read-only
**`SwatchTrigger`**
**`data-scope`**: color-picker
**`data-part`**: swatch-trigger
**`data-state`**: "checked" | "unchecked"
**`data-value`**: The value of the item
**`data-disabled`**: Present when disabled
**`Swatch`**
**`data-scope`**: color-picker
**`data-part`**: swatch
**`data-state`**: "checked" | "unchecked"
**`data-value`**: The value of the item
## Accessibility
### Keyboard Interactions
**`Enter`**
Description: When focus is on the trigger, opens the color picker When focus is on a trigger of a swatch, selects the color (and closes the color picker) When focus is on the input or channel inputs, selects the color
**`ArrowLeft`**
Description: When focus is on the color area, decreases the hue value of the color When focus is on the channel sliders, decreases the value of the channel
**`ArrowRight`**
Description: When focus is on the color area, increases the hue value of the color When focus is on the channel sliders, increases the value of the channel
**`ArrowUp`**
Description: When focus is on the color area, increases the saturation value of the color When focus is on the channel sliders, increases the value of the channel
**`ArrowDown`**
Description: When focus is on the color area, decreases the saturation value of the color When focus is on the channel sliders, decreases the value of the channel
**`Esc`**
Description: Closes the color picker and moves focus to the trigger
# Combobox
A combobox is an input widget with an associated popup that enables users to
select a value from a collection of possible values.
## Resources
[Latest version: v1.7.0](https://www.npmjs.com/package/@zag-js/combobox)
[Logic Visualizer](https://zag-visualizer.vercel.app/combobox)
[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/combobox)
**Features**
- Support for selecting multiple values
- Support for disabled options
- Support for custom user input values
- Support for mouse, touch, and keyboard interactions
- Keyboard support for opening the combo box list box using the arrow keys,
including automatically focusing the first or last item accordingly
## Installation
To use the combobox machine in your project, run the following command in your
command line:
```bash
npm install @zag-js/combobox @zag-js/react
# or
yarn add @zag-js/combobox @zag-js/react
```
This command will install the framework agnostic combobox logic and the reactive
utilities for your framework of choice.
## Anatomy
To set up the combobox correctly, you'll need to understand its anatomy and how
we name its parts.
> Each part includes a `data-part` attribute to help identify them in the DOM.
## Usage
First, import the combobox package into your project
```jsx
import * as combobox from "@zag-js/combobox"
```
The combobox package exports these functions:
- `machine` — The state machine logic for the combobox widget.
- `connect` — The function that translates the machine's state to JSX attributes
and event handlers.
- `collection` - The function that creates a
[collection interface](/overview/collection) from an array of items.
Next, import the required hooks and functions for your framework and use the
combobox machine in your project 🔥
```jsx
import * as combobox from "@zag-js/combobox"
import { useMachine, normalizeProps } from "@zag-js/react"
import { useState, useId } from "react"
const comboboxData = [
{ label: "Zambia", code: "ZA" },
{ label: "Benin", code: "BN" },
//...
]
export function Combobox() {
const [options, setOptions] = useState(comboboxData)
const collection = combobox.collection({
items: options,
itemToValue: (item) => item.code,
itemToString: (item) => item.label,
})
const service = useMachine(combobox.machine, {
id: useId(),
collection,
onOpenChange() {
setOptions(comboboxData)
},
onInputValueChange({ inputValue }) {
const filtered = comboboxData.filter((item) =>
item.label.toLowerCase().includes(inputValue.toLowerCase()),
)
setOptions(filtered.length > 0 ? filtered : comboboxData)
},
})
const api = combobox.connect(service, normalizeProps)
return (
{options.length > 0 && (
{options.map((item) => (
{item.label}
))}
)}
)
}
```
## Setting the initial value
To set the initial value of the combobox, pass the `value` property to the
machine's context.
> The `value` property must be an array of strings. If selecting a single value,
> pass an array with a single string.
```jsx {13}
const collection = combobox.collection({
items: [
{ label: "Nigeria", value: "ng" },
{ label: "Ghana", value: "gh" },
{ label: "Kenya", value: "ke" },
//...
],
})
const service = useMachine(combobox.machine, {
id: useId(),
collection,
defaultValue: ["ng"],
})
```
## Selecting multiple values
To allow selecting multiple values, set the `multiple` property in the machine's
context to `true`.
```jsx {4}
const service = useMachine(combobox.machine, {
id: useId(),
collection,
multiple: true,
})
```
## Using a custom object format
By default, the combobox collection expects an array of items with `label` and
`value` properties. To use a custom object format, pass the `itemToString` and
`itemToValue` properties to the collection function.
- `itemToString` — A function that returns the string representation of an item.
Used to compare items when filtering.
- `itemToValue` — A function that returns the unique value of an item.
- `itemToDisabled` — A function that returns the disabled state of an item.
```jsx
const collection = combobox.collection({
// custom object format
items: [
{ id: 1, fruit: "Banana", available: true, quantity: 10 },
{ id: 2, fruit: "Apple", available: false, quantity: 5 },
{ id: 3, fruit: "Orange", available: true, quantity: 3 },
//...
],
// convert item to string
itemToString(item) {
return item.fruit
},
// convert item to value
itemToValue(item) {
return item.id
},
// convert item to disabled state
itemToDisabled(item) {
return !item.available || item.quantity === 0
},
})
// use the collection
const service = useMachine(combobox.machine, {
id: useId(),
collection,
})
```
## Rendering the selected values outside the combobox
By default, the selected values of a combobox are displayed in the input
element, when selecting multiple items, it is a better UX to render the selected
value outside the combobox.
To achieve this you need to:
- Set the `selectionBehavior` to `clear`, which clears the input value when an
item is selected.
- Set the `multiple` property to `true` to allow selecting multiple values.
- Render the selected values outside the combobox.
```jsx {4-6}
const service = useMachine(combobox.machine, {
id: useId(),
collection,
selectionBehavior: "clear",
multiple: true,
})
```
## Disabling the combobox
To make a combobox disabled, set the context's `disabled` property to `true`
```jsx {2}
const service = useMachine(combobox.machine, {
disabled: true,
})
```
## Disabling an option
To make a combobox option disabled, pass the `isItemDisabled` property to the
collection function.
```jsx {6-8}
const service = useMachine(combobox.machine, {
id: useId(),
collection: combobox.collection({
items: countries,
isItemDisabled(item) {
return item.disabled
},
}),
})
```
## Close on select
This behaviour ensures that the menu is closed when an option is selected and is
`true` by default. It's only concerned with when an option is selected with
pointer or enter key. To disable the behaviour, set the `closeOnSelect` property
in the machine's context to `false`.
```jsx {2}
const service = useMachine(combobox.machine, {
closeOnSelect: false,
})
```
## Making the combobox readonly
To make a combobox readonly, set the context's `readOnly` property to `true`
```jsx {2}
const service = useMachine(combobox.machine, {
readOnly: true,
})
```
## Listening for highlight changes
When an option is highlighted with the pointer or keyboard, use the
`onHighlightChange` property to listen for this change and do something with it.
```jsx {3-6}
const service = useMachine(combobox.machine, {
id: useId(),
onHighlightChange(details) {
// details => { value: string | null; item: CollectionItem | null }
console.log(details)
},
})
```
## Listening for value changes
When an item is selected, use `onValueChange` property to listen for this change
and do something with it.
```jsx {3-6}
const service = useMachine(combobox.machine, {
onValueChange(details) {
// details => { value: string[]; items: CollectionItem[] }
console.log(details)
},
})
```
## Usage within forms
The combobox works when placed within a form and the form is submitted. We
achieve this by:
- ensuring we emit the input event as the value changes.
- adding a `name` attribute to the input so the value can be accessed in the
`FormData`.
To get this feature working you need to pass a `name` option to the context.
```jsx {2}
const service = useMachine(combobox.machine, {
name: "countries",
})
```
## Allowing custom values
By default, the combobox only allows selecting values from the collection. To
allow custom values, set the `allowCustomValue` property in the machine's
context to `true`.
```jsx {2}
const service = useMachine(combobox.machine, {
allowCustomValue: true,
})
```
## Styling guide
Earlier, we mentioned that each combobox part has a `data-part` attribute added
to them to select and style them in the DOM.
### Open and closed state
When the combobox is open or closed, the `data-state` attribute is added to the
content,control, input and control parts.
```css
[data-part="control"][data-state="open|closed"] {
/* styles for control open or state */
}
[data-part="input"][data-state="open|closed"] {
/* styles for control open or state */
}
[data-part="trigger"][data-state="open|closed"] {
/* styles for control open or state */
}
[data-part="content"][data-state="open|closed"] {
/* styles for control open or state */
}
```
### Focused State
When the combobox is focused, the `data-focus` attribute is added to the control
and label parts.
```css
[data-part="control"][data-focus] {
/* styles for control focus state */
}
[data-part="label"][data-focus] {
/* styles for label focus state */
}
```
### Disabled State
When the combobox is disabled, the `data-disabled` attribute is added to the
label, control, trigger and option parts.
```css
[data-part="label"][data-disabled] {
/* styles for label disabled state */
}
[data-part="control"][data-disabled] {
/* styles for control disabled state */
}
[data-part="trigger"][data-disabled] {
/* styles for trigger disabled state */
}
[data-part="item"][data-disabled] {
/* styles for item disabled state */
}
```
### Invalid State
When the combobox is invalid, the `data-invalid` attribute is added to the root,
label, control and input parts.
```css
[data-part="root"][data-invalid] {
/* styles for root invalid state */
}
[data-part="label"][data-invalid] {
/* styles for label invalid state */
}
[data-part="control"][data-invalid] {
/* styles for control invalid state */
}
[data-part="input"][data-invalid] {
/* styles for input invalid state */
}
```
### Selected State
When a combobox item is selected, the `data-selected` attribute is added to the
item part.
```css
[data-part="item"][data-state="checked|unchecked"] {
/* styles for item selected state */
}
```
### Highlighted State
When a combobox item is highlighted, the `data-highlighted` attribute is added
to the item part.
```css
[data-part="item"][data-highlighted] {
/* styles for item highlighted state */
}
```
## Methods and Properties
### Machine Context
The combobox machine exposes the following context properties:
**`open`**
Type: `boolean`
Description: The controlled open state of the combobox
**`defaultOpen`**
Type: `boolean`
Description: The initial open state of the combobox when rendered.
Use when you don't need to control the open state of the combobox.
**`ids`**
Type: `Partial<{ root: string; label: string; control: string; input: string; content: string; trigger: string; clearTrigger: string; item(id: string, index?: number): string; positioner: string; itemGroup(id: string | number): string; itemGroupLabel(id: string | number): string; }>`
Description: The ids of the elements in the combobox. Useful for composition.
**`inputValue`**
Type: `string`
Description: The controlled value of the combobox's input
**`defaultInputValue`**
Type: `string`
Description: The initial value of the combobox's input when rendered.
Use when you don't need to control the value of the combobox's input.
**`name`**
Type: `string`
Description: The `name` attribute of the combobox's input. Useful for form submission
**`form`**
Type: `string`
Description: The associate form of the combobox.
**`disabled`**
Type: `boolean`
Description: Whether the combobox is disabled
**`readOnly`**
Type: `boolean`
Description: Whether the combobox is readonly. This puts the combobox in a "non-editable" mode
but the user can still interact with it
**`invalid`**
Type: `boolean`
Description: Whether the combobox is invalid
**`required`**
Type: `boolean`
Description: Whether the combobox is required
**`placeholder`**
Type: `string`
Description: The placeholder text of the combobox's input
**`defaultHighlightedValue`**
Type: `string`
Description: The initial highlighted value of the combobox when rendered.
Use when you don't need to control the highlighted value of the combobox.
**`highlightedValue`**
Type: `string`
Description: The controlled highlighted value of the combobox
**`value`**
Type: `string[]`
Description: The controlled value of the combobox's selected items
**`defaultValue`**
Type: `string[]`
Description: The initial value of the combobox's selected items when rendered.
Use when you don't need to control the value of the combobox's selected items.
**`inputBehavior`**
Type: `"autohighlight" | "autocomplete" | "none"`
Description: Defines the auto-completion behavior of the combobox.
- `autohighlight`: The first focused item is highlighted as the user types
- `autocomplete`: Navigating the listbox with the arrow keys selects the item and the input is updated
**`selectionBehavior`**
Type: `"clear" | "replace" | "preserve"`
Description: The behavior of the combobox input when an item is selected
- `replace`: The selected item string is set as the input value
- `clear`: The input value is cleared
- `preserve`: The input value is preserved
**`autoFocus`**
Type: `boolean`
Description: Whether to autofocus the input on mount
**`openOnClick`**
Type: `boolean`
Description: Whether to open the combobox popup on initial click on the input
**`openOnChange`**
Type: `boolean | ((details: InputValueChangeDetails) => boolean)`
Description: Whether to show the combobox when the input value changes
**`allowCustomValue`**
Type: `boolean`
Description: Whether to allow typing custom values in the input
**`loopFocus`**
Type: `boolean`
Description: Whether to loop the keyboard navigation through the items
**`positioning`**
Type: `PositioningOptions`
Description: The positioning options to dynamically position the menu
**`onInputValueChange`**
Type: `(details: InputValueChangeDetails) => void`
Description: Function called when the input's value changes
**`onValueChange`**
Type: `(details: ValueChangeDetails) => void`
Description: Function called when a new item is selected
**`onHighlightChange`**
Type: `(details: HighlightChangeDetails) => void`
Description: Function called when an item is highlighted using the pointer
or keyboard navigation.
**`onOpenChange`**
Type: `(details: OpenChangeDetails) => void`
Description: Function called when the popup is opened
**`translations`**
Type: `IntlTranslations`
Description: Specifies the localized strings that identifies the accessibility elements and their states
**`collection`**
Type: `ListCollection`
Description: The collection of items
**`multiple`**
Type: `boolean`
Description: Whether to allow multiple selection.
**Good to know:** When `multiple` is `true`, the `selectionBehavior` is automatically set to `clear`.
It is recommended to render the selected items in a separate container.
**`closeOnSelect`**
Type: `boolean`
Description: Whether to close the combobox when an item is selected.
**`openOnKeyPress`**
Type: `boolean`
Description: Whether to open the combobox on arrow key press
**`scrollToIndexFn`**
Type: `(details: ScrollToIndexDetails) => void`
Description: Function to scroll to a specific index
**`composite`**
Type: `boolean`
Description: Whether the combobox is a composed with other composite widgets like tabs
**`disableLayer`**
Type: `boolean`
Description: Whether to disable registering this a dismissable layer
**`navigate`**
Type: `(details: NavigateDetails) => void`
Description: Function to navigate to the selected item
**`dir`**
Type: `"ltr" | "rtl"`
Description: The document's text/writing direction.
**`id`**
Type: `string`
Description: The unique identifier of the machine.
**`getRootNode`**
Type: `() => ShadowRoot | Node | Document`
Description: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.
**`onPointerDownOutside`**
Type: `(event: PointerDownOutsideEvent) => void`
Description: Function called when the pointer is pressed down outside the component
**`onFocusOutside`**
Type: `(event: FocusOutsideEvent) => void`
Description: Function called when the focus is moved outside the component
**`onInteractOutside`**
Type: `(event: InteractOutsideEvent) => void`
Description: Function called when an interaction happens outside the component
### Machine API
The combobox `api` exposes the following methods:
**`focused`**
Type: `boolean`
Description: Whether the combobox is focused
**`open`**
Type: `boolean`
Description: Whether the combobox is open
**`inputValue`**
Type: `string`
Description: The value of the combobox input
**`highlightedValue`**
Type: `string`
Description: The value of the highlighted item
**`highlightedItem`**
Type: `V`
Description: The highlighted item
**`setHighlightValue`**
Type: `(value: string) => void`
Description: The value of the combobox input
**`syncSelectedItems`**
Type: `() => void`
Description: Function to sync the selected items with the value.
Useful when `value` is updated from async sources.
**`selectedItems`**
Type: `V[]`
Description: The selected items
**`hasSelectedItems`**
Type: `boolean`
Description: Whether there's a selected item
**`value`**
Type: `string[]`
Description: The selected item keys
**`valueAsString`**
Type: `string`
Description: The string representation of the selected items
**`selectValue`**
Type: `(value: string) => void`
Description: Function to select a value
**`setValue`**
Type: `(value: string[]) => void`
Description: Function to set the value of the combobox
**`clearValue`**
Type: `(value?: string) => void`
Description: Function to clear the value of the combobox
**`focus`**
Type: `() => void`
Description: Function to focus on the combobox input
**`setInputValue`**
Type: `(value: string) => void`
Description: Function to set the input value of the combobox
**`getItemState`**
Type: `(props: ItemProps) => ItemState`
Description: Returns the state of a combobox item
**`setOpen`**
Type: `(open: boolean) => void`
Description: Function to open or close the combobox
**`collection`**
Type: `ListCollection`
Description: Function to toggle the combobox
**`reposition`**
Type: `(options?: Partial) => void`
Description: Function to set the positioning options
**`multiple`**
Type: `boolean`
Description: Whether the combobox allows multiple selections
**`disabled`**
Type: `boolean`
Description: Whether the combobox is disabled
### Data Attributes
**`Root`**
**`data-scope`**: combobox
**`data-part`**: root
**`data-invalid`**: Present when invalid
**`data-readonly`**: Present when read-only
**`Label`**
**`data-scope`**: combobox
**`data-part`**: label
**`data-readonly`**: Present when read-only
**`data-disabled`**: Present when disabled
**`data-invalid`**: Present when invalid
**`data-focus`**: Present when focused
**`Control`**
**`data-scope`**: combobox
**`data-part`**: control
**`data-state`**: "open" | "closed"
**`data-focus`**: Present when focused
**`data-disabled`**: Present when disabled
**`data-invalid`**: Present when invalid
**`Input`**
**`data-scope`**: combobox
**`data-part`**: input
**`data-invalid`**: Present when invalid
**`data-state`**: "open" | "closed"
**`Trigger`**
**`data-scope`**: combobox
**`data-part`**: trigger
**`data-state`**: "open" | "closed"
**`data-invalid`**: Present when invalid
**`data-focusable`**:
**`data-readonly`**: Present when read-only
**`data-disabled`**: Present when disabled
**`Content`**
**`data-scope`**: combobox
**`data-part`**: content
**`data-state`**: "open" | "closed"
**`data-placement`**: The placement of the content
**`ClearTrigger`**
**`data-scope`**: combobox
**`data-part`**: clear-trigger
**`data-invalid`**: Present when invalid
**`Item`**
**`data-scope`**: combobox
**`data-part`**: item
**`data-highlighted`**: Present when highlighted
**`data-state`**: "checked" | "unchecked"
**`data-disabled`**: Present when disabled
**`data-value`**: The value of the item
**`ItemText`**
**`data-scope`**: combobox
**`data-part`**: item-text
**`data-state`**: "checked" | "unchecked"
**`data-disabled`**: Present when disabled
**`data-highlighted`**: Present when highlighted
**`ItemIndicator`**
**`data-scope`**: combobox
**`data-part`**: item-indicator
**`data-state`**: "checked" | "unchecked"
## Accessibility
Adheres to the
[Combobox WAI-ARIA design pattern](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/).
### Keyboard Interactions
**`ArrowDown`**
Description: When the combobox is closed, opens the listbox and highlights to the first option.
When the combobox is open, moves focus to the next option.
**`ArrowUp`**
Description: When the combobox is closed, opens the listbox and highlights to the last option.
When the combobox is open, moves focus to the previous option.
**`Home`**
Description: When the combobox is open, moves focus to the first option.
**`End`**
Description: When the combobox is open, moves focus to the last option.
**`Escape`**
Description: Closes the listbox.
**`Enter`**
Description: Selects the highlighted option and closes the combobox.
**`Esc`**
Description: Closes the combobox
# Date Picker
A datepicker allows users to enter a date either through text input, or by
choosing a date from the calendar.
> **Good to know**: The date picker machine is built around the
> [ISO 8601 date format](https://www.iso.org/iso-8601-date-and-time-format.html)
## Resources
[Latest version: v1.7.0](https://www.npmjs.com/package/@zag-js/date-picker)
[Logic Visualizer](https://zag-visualizer.vercel.app/date-picker)
[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/date-picker)
**Features**
- Displays a calendar view for date selection
- Support for date range selection
- Support for disabling specific dates
- Localization support
- Provides keyboard accessibility for navigating the calendar.
## Installation
To use the date-picker machine in your project, run the following command in
your command line:
```bash
npm install @zag-js/date-picker @zag-js/react
# or
yarn add @zag-js/date-picker @zag-js/react
```
This command will install the framework agnostic date-picker logic and the
reactive utilities for your framework of choice.
## Anatomy
To set up the date-picker correctly, you'll need to understand its anatomy and
how we name its parts.
> Each part includes a `data-part` attribute to help identify them in the DOM.
## Usage
First, import the date picker package into your project
```jsx
import * as datepicker from "@zag-js/date-picker"
```
The date picker package exports these key functions:
- `machine` — The state machine logic for the date-picker widget.
- `connect` — The function that translates the machine's state to JSX attributes
and event handlers.
- `parse` - The function that parses the date string into a date object. This
function uses
[`@internationalized/date`](https://react-spectrum.adobe.com/internationalized/date/CalendarDate.html)
under the hood.
> You'll also need to provide a unique `id` to the `useMachine` hook. This is
> used to ensure that every part has a unique identifier.
Next, import the required hooks and functions for your framework and use the
date-picker machine in your project 🔥
```jsx
import * as datepicker from "@zag-js/date-picker"
import { useMachine, normalizeProps, Portal } from "@zag-js/react"
import { useId } from "react"
function DatePicker() {
const service = useMachine(datepicker.machine, { id: useId() })
const api = datepicker.connect(service, normalizeProps)
return (
<>
>
)
}
```
## Setting the initial date
To set the initial value that is rendered by the date picker, set the `value`
property in the machine context.
```jsx
const service = useMachine(datepicker.machine, {
defaultValue: [datepicker.parse("2022-01-01")],
})
```
## Controlling the selected date
Use the `api.setValue` method to control the selected date in the DatePicker
component.
```jsx
// parse the date string into a date object
const nextValue = datepicker.parse("2022-01-01")
// set the new value
api.setValue(nextValue)
```
> Alternatively, you can also use the `value` and `onValueChange` callbacks to
> programmatically control the selected date.
## Controlling the open state
To manage the open state of the datepicker's dialog, we recommended using
`api.setOpen` method.
```jsx
// open the date picker
api.setOpen(true)
// close the date picker
api.setOpen(false)
```
> Alternatively, you can also use the `open` and `onOpenChange` callbacks to
> programmatically control the open state.
## Setting the min and max dates
To constrain the date range that can be selected by the user, set the `min` and
`max` properties in the machine context.
```jsx
const service = useMachine(datepicker.machine, {
min: datepicker.parse("2022-01-01"),
max: datepicker.parse("2022-12-31"),
})
```
When the min or max date value is reached, the next and prev triggers will be
disabled.
## Changing the start of the week
Set the `startOfWeek` property in the machine context to change the start of the
week. The property accepts a number from `0` to `6`, where `0` is Sunday and `6`
is Saturday.
```jsx
const service = useMachine(datepicker.machine, {
startOfWeek: 1, // Monday
})
```
## Disabling the date picker
To disable the date picker, set the `disabled` property in the machine context
to `true`.
```jsx
const service = useMachine(datepicker.machine, {
disabled: true,
})
```
## Rendering month and year pickers
To render the month and year pickers, use the `api.getMonthSelectProps` and
`api.getYearSelectProps` prop getters.
```jsx
```
## Marking unavailable dates
To mark specific dates as unavailable, set the `isDateUnavailable` function in
the machine context. This function should return `true` for dates that are
unavailable.
```jsx
const service = useMachine(datepicker.machine, {
isDateUnavailable: (date, locale) => {
// mark weekends as unavailable
return date.day === 0 || date.day === 6
},
})
```
You can also leverage the numerous helpers from
[`@internationalized/date`](https://react-spectrum.adobe.com/internationalized/date/CalendarDate.html)
to create more complex date availability rules.
```jsx
import { isWeekend } from "@internationalized/date"
const service = useMachine(datepicker.machine, {
isDateUnavailable: (date, locale) => {
// mark weekends as unavailable
return isWeekend(date, locale)
},
})
```
## Setting the calendar starting view
The calendar view is set to `day` by default. To change the starting view of the
calendar, set the `view` property in the machine context to either `day`,
`month`, or `year`.
```jsx
const service = useMachine(datepicker.machine, {
view: "month",
})
```
## Setting the read-only mode
Set the `readOnly` property in the machine context to `true` to make the date
picker read-only. This means that users can't change the selected date.
```jsx
const service = useMachine(datepicker.machine, {
readOnly: true,
})
```
## Setting the focused date
The datepicker's focused date is set to either the first selected date or
today's date by default.
To change the focused date, set the `focusedDate` property in the machine
context.
```jsx
const service = useMachine(datepicker.machine, {
focusedDate: datepicker.parse("2022-01-01"),
})
```
## Rendering the calendar inline
To render the calendar inline, we recommended setting the `open` property to
`true` and `closeOnSelect` to `false`.
```jsx
const service = useMachine(datepicker.machine, {
open: true,
closeOnSelect: false,
})
```
## Usage within a form
To use the date picker within a form, set the `name` property in the machine
context. This property is used to identify the date picker in the form data.
```jsx
const service = useMachine(datepicker.machine, {
name: "date",
})
```
## Rendering fixed number of weeks
The datepicker's calendar will render the weeks needed to display all of the
days in the month. Sometimes this can result in a jump in the UI when navigating
between different sized months (e.g., February vs. March).
To ensure the calendar renders the maximum number of weeks (6), you can set the
`fixedWeeks` prop to `true`.
```jsx
const service = useMachine(datepicker.machine, {
fixedWeeks: true,
})
```
## Listening to date changes
To listen to date changes, use the `onValueChange` callback in the machine
context.
```jsx
const service = useMachine(datepicker.machine, {
onValueChange(details) {
// details => { value: DateValue[], valueAsString: string[], view: string }
console.log("selected date:", details.valueAsString)
},
})
```
## Listening to view changes
When the calendar view changes by click on the view controls, the `onViewChange`
callback is invoked.
```jsx
const service = useMachine(datepicker.machine, {
onViewChange(details) {
// details => { view: string }
console.log("view changed to:", details.view)
},
})
```
## Styling guide
Earlier, we mentioned that each date-picker part has a `data-part` attribute
added to them to select and style them in the DOM.
```css
[data-scope="date-picker"][data-part="root"] {
/* styles for the root part */
}
[data-scope="date-picker"][data-part="input"] {
/* styles for the input part */
}
[data-scope="date-picker"][data-part="trigger"] {
/* styles for the trigger part */
}
[data-scope="date-picker"][data-part="content"] {
/* styles for the input part */
}
```
### Open State
```css
[data-scope="date-picker"][data-part="trigger"] {
&[data-state="open"] {
/* styles for the open state */
}
&[data-state="closed"] {
/* styles for the closed state */
}
}
```
### Cell States
```css
[data-scope="date-picker"][data-part="table-cell-trigger"] {
/* styles for the cell */
&[data-selected] {
/* styles for the selected date */
}
&[data-focus] {
/* styles for the focused date */
}
&[data-disabled] {
/* styles for the disabled date */
}
&[data-unavailable] {
/* styles for the unavailable date */
}
&[data-today] {
/* styles for the today date */
}
&[data-weekend] {
/* styles for the weekend date */
}
}
```
## Methods and Properties
### Machine Context
The date picker machine exposes the following context properties:
**`locale`**
Type: `string`
Description: The locale (BCP 47 language tag) to use when formatting the date.
**`translations`**
Type: `IntlTranslations`
Description: The localized messages to use.
**`ids`**
Type: `Partial<{ root: string; label(index: number): string; table(id: string): string; tableHeader(id: string): string; tableBody(id: string): string; tableRow(id: string): string; content: string; cellTrigger(id: string): string; prevTrigger(view: DateView): string; clearTrigger: string; control: string; input(index: number): string; trigger: string; monthSelect: string; yearSelect: string; positioner: string; }>`
Description: The ids of the elements in the date picker. Useful for composition.
**`name`**
Type: `string`
Description: The `name` attribute of the input element.
**`timeZone`**
Type: `string`
Description: The time zone to use
**`disabled`**
Type: `boolean`
Description: Whether the calendar is disabled.
**`readOnly`**
Type: `boolean`
Description: Whether the calendar is read-only.
**`min`**
Type: `DateValue`
Description: The minimum date that can be selected.
**`max`**
Type: `DateValue`
Description: The maximum date that can be selected.
**`closeOnSelect`**
Type: `boolean`
Description: Whether the calendar should close after the date selection is complete.
This is ignored when the selection mode is `multiple`.
**`value`**
Type: `DateValue[]`
Description: The controlled selected date(s).
**`defaultValue`**
Type: `DateValue[]`
Description: The initial selected date(s) when rendered.
Use when you don't need to control the selected date(s) of the date picker.
**`focusedValue`**
Type: `DateValue`
Description: The controlled focused date.
**`defaultFocusedValue`**
Type: `DateValue`
Description: The initial focused date when rendered.
Use when you don't need to control the focused date of the date picker.
**`numOfMonths`**
Type: `number`
Description: The number of months to display.
**`startOfWeek`**
Type: `number`
Description: The first day of the week.
`0` - Sunday
`1` - Monday
`2` - Tuesday
`3` - Wednesday
`4` - Thursday
`5` - Friday
`6` - Saturday
**`fixedWeeks`**
Type: `boolean`
Description: Whether the calendar should have a fixed number of weeks.
This renders the calendar with 6 weeks instead of 5 or 6.
**`onValueChange`**
Type: `(details: ValueChangeDetails) => void`
Description: Function called when the value changes.
**`onFocusChange`**
Type: `(details: FocusChangeDetails) => void`
Description: Function called when the focused date changes.
**`onViewChange`**
Type: `(details: ViewChangeDetails) => void`
Description: Function called when the view changes.
**`onOpenChange`**
Type: `(details: OpenChangeDetails) => void`
Description: Function called when the calendar opens or closes.
**`isDateUnavailable`**
Type: `(date: DateValue, locale: string) => boolean`
Description: Returns whether a date of the calendar is available.
**`selectionMode`**
Type: `SelectionMode`
Description: The selection mode of the calendar.
- `single` - only one date can be selected
- `multiple` - multiple dates can be selected
- `range` - a range of dates can be selected
**`format`**
Type: `(date: LocaleDetails) => string`
Description: The format of the date to display in the input.
**`parse`**
Type: `(value: string, details: LocaleDetails) => DateValue`
Description: Function to parse the date from the input back to a DateValue.
**`placeholder`**
Type: `string`
Description: The placeholder text to display in the input.
**`view`**
Type: `DateView`
Description: The view of the calendar
**`defaultView`**
Type: `DateView`
Description: The default view of the calendar
**`minView`**
Type: `DateView`
Description: The minimum view of the calendar
**`maxView`**
Type: `DateView`
Description: The maximum view of the calendar
**`positioning`**
Type: `PositioningOptions`
Description: The user provided options used to position the date picker content
**`open`**
Type: `boolean`
Description: The controlled open state of the date picker
**`defaultOpen`**
Type: `boolean`
Description: The initial open state of the date picker when rendered.
Use when you don't need to control the open state of the date picker.
**`dir`**
Type: `"ltr" | "rtl"`
Description: The document's text/writing direction.
**`id`**
Type: `string`
Description: The unique identifier of the machine.
**`getRootNode`**
Type: `() => ShadowRoot | Node | Document`
Description: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.
### Machine API
The date picker `api` exposes the following methods:
**`focused`**
Type: `boolean`
Description: Whether the input is focused
**`open`**
Type: `boolean`
Description: Whether the date picker is open
**`view`**
Type: `DateView`
Description: The current view of the date picker
**`getDaysInWeek`**
Type: `(week: number, from?: DateValue) => DateValue[]`
Description: Returns an array of days in the week index counted from the provided start date, or the first visible date if not given.
**`getOffset`**
Type: `(duration: DateDuration) => DateValueOffset`
Description: Returns the offset of the month based on the provided number of months.
**`getRangePresetValue`**
Type: `(value: DateRangePreset) => DateValue[]`
Description: Returns the range of dates based on the provided date range preset.
**`getMonthWeeks`**
Type: `(from?: DateValue) => DateValue[][]`
Description: Returns the weeks of the month from the provided date. Represented as an array of arrays of dates.
**`isUnavailable`**
Type: `(date: DateValue) => boolean`
Description: Returns whether the provided date is available (or can be selected)
**`weeks`**
Type: `DateValue[][]`
Description: The weeks of the month. Represented as an array of arrays of dates.
**`weekDays`**
Type: `WeekDay[]`
Description: The days of the week. Represented as an array of strings.
**`visibleRange`**
Type: `VisibleRange`
Description: The visible range of dates.
**`visibleRangeText`**
Type: `VisibleRangeText`
Description: The human readable text for the visible range of dates.
**`value`**
Type: `DateValue[]`
Description: The selected date.
**`valueAsDate`**
Type: `Date[]`
Description: The selected date as a Date object.
**`valueAsString`**
Type: `string[]`
Description: The selected date as a string.
**`focusedValue`**
Type: `DateValue`
Description: The focused date.
**`focusedValueAsDate`**
Type: `Date`
Description: The focused date as a Date object.
**`focusedValueAsString`**
Type: `string`
Description: The focused date as a string.
**`selectToday`**
Type: `() => void`
Description: Sets the selected date to today.
**`setValue`**
Type: `(values: CalendarDate[]) => void`
Description: Sets the selected date to the given date.
**`setFocusedValue`**
Type: `(value: CalendarDate) => void`
Description: Sets the focused date to the given date.
**`clearValue`**
Type: `() => void`
Description: Clears the selected date(s).
**`setOpen`**
Type: `(open: boolean) => void`
Description: Function to open or close the calendar.
**`focusMonth`**
Type: `(month: number) => void`
Description: Function to set the selected month.
**`focusYear`**
Type: `(year: number) => void`
Description: Function to set the selected year.
**`getYears`**
Type: `() => Cell[]`
Description: Returns the months of the year
**`getYearsGrid`**
Type: `(props?: YearGridProps) => YearGridValue`
Description: Returns the years of the decade based on the columns.
Represented as an array of arrays of years.
**`getDecade`**
Type: `() => Range`
Description: Returns the start and end years of the decade.
**`getMonths`**
Type: `(props?: MonthFormatOptions) => Cell[]`
Description: Returns the months of the year
**`getMonthsGrid`**
Type: `(props?: MonthGridProps) => MonthGridValue`
Description: Returns the months of the year based on the columns.
Represented as an array of arrays of months.
**`format`**
Type: `(value: CalendarDate, opts?: Intl.DateTimeFormatOptions) => string`
Description: Formats the given date value based on the provided options.
**`setView`**
Type: `(view: DateView) => void`
Description: Sets the view of the date picker.
**`goToNext`**
Type: `() => void`
Description: Goes to the next month/year/decade.
**`goToPrev`**
Type: `() => void`
Description: Goes to the previous month/year/decade.
**`getDayTableCellState`**
Type: `(props: DayTableCellProps) => DayTableCellState`
Description: Returns the state details for a given cell.
**`getMonthTableCellState`**
Type: `(props: TableCellProps) => TableCellState`
Description: Returns the state details for a given month cell.
**`getYearTableCellState`**
Type: `(props: TableCellProps) => TableCellState`
Description: Returns the state details for a given year cell.
# Dialog
A dialog is a window overlaid on either the primary window or another dialog
window. Content behind a modal dialog is inert, meaning that users cannot
interact with it.
## Resources
[Latest version: v1.7.0](https://www.npmjs.com/package/@zag-js/dialog)
[Logic Visualizer](https://zag-visualizer.vercel.app/dialog)
[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/dialog)
**Features**
- Supports modal and non-modal modes.
- Focus is trapped and scrolling is blocked in the modal mode.
- Provides screen reader announcements via rendered title and description.
- Pressing `Esc` closes the dialog.
## Installation
To use the dialog machine in your project, run the following command in your
command line:
```bash
npm install @zag-js/dialog @zag-js/react
# or
yarn add @zag-js/dialog @zag-js/react
```
This command will install the framework agnostic dialog logic and the reactive
utilities for your framework of choice.
## Anatomy
To use the dialog component correctly, you'll need to understand its anatomy and
how we name its parts.
> Each part includes a `data-part` attribute to help identify them in the DOM.
## Usage
First, import the dialog package into your project
```jsx
import * as dialog from "@zag-js/dialog"
```
The dialog package exports two key functions:
- `machine` — The state machine logic for the dialog widget as described in
WAI-ARIA specification.
- `connect` — The function that translates the machine's state to JSX attributes
and event handlers.
> You'll need to provide a unique `id` to the `useMachine` hook. This is used to
> ensure that every part has a unique identifier.
Next, import the required hooks and functions for your framework and use the
dialog machine in your project 🔥
```tsx
import * as dialog from "@zag-js/dialog"
import { useMachine, normalizeProps, Portal } from "@zag-js/react"
export function Dialog() {
const service = useMachine(dialog.machine, { id: "1" })
const api = dialog.connect(service, normalizeProps)
return (
<>
{api.open && (
Edit profile
Make changes to your profile here. Click save when you are done.
)}
>
)
}
```
## Managing focus within the dialog
When the dialog opens, it automatically sets focus on the first focusable
elements and traps focus within it, so that tabbing is constrained to it.
To control the element that should receive focus on open, pass the
`initialFocusEl` context (which can be an element or a function that returns an
element)
```jsx {3,7,15}
export function Dialog() {
// initial focused element ref
const inputRef = useRef(null)
const service = useMachine(dialog.machine, {
initialFocusEl: () => inputRef.current,
})
// ...
return (
//...
// ...
)
}
```
To set the element that receives focus when the dialog closes, pass the
`finalFocusEl` in the similar fashion as shown above.
## Closing the dialog on interaction outside
By default, the dialog closes when you click its overlay. You can set
`closeOnInteractOutside` to `false` if you want the modal to stay visible.
```jsx {2}
const service = useMachine(dialog.machine, {
closeOnInteractOutside: false,
})
```
You can also customize the behavior by passing a function to the
`onInteractOutside` context and calling `event.preventDefault()`
```jsx {2-7}
const service = useMachine(dialog.machine, {
onInteractOutside(event) {
const target = event.target
if (target?.closest("")) {
return event.preventDefault()
}
},
})
```
## Listening for open state changes
When the dialog is opened or closed, the `onOpenChange` callback is invoked.
```jsx {2-7}
const service = useMachine(dialog.machine, {
onOpenChange(details) {
// details => { open: boolean }
console.log("open:", details.open)
},
})
```
## Controlling the scroll behavior
When the dialog is open, it prevents scrolling on the `body` element. To disable
this behavior, set the `preventScroll` context to `false`.
```jsx {2}
const service = useMachine(dialog.machine, {
preventScroll: false,
})
```
## Creating an alert dialog
The dialog has support for dialog and alert dialog roles. It's set to `dialog`
by default. To change it's role, pass the `role: alertdialog` property to the
machine's context.
That's it! Now you have an alert dialog.
```jsx {2}
const service = useMachine(dialog.machine, {
role: "alertdialog",
})
```
> By definition, an alert dialog will contain two or more action buttons. We
> recommended setting focus to the least destructive action via `initialFocusEl`
## Styling guide
Earlier, we mentioned that each accordion part has a `data-part` attribute added
to them to select and style them in the DOM.
```css
[data-part="trigger"] {
/* styles for the trigger element */
}
[data-part="backdrop"] {
/* styles for the backdrop element */
}
[data-part="positioner"] {
/* styles for the positioner element */
}
[data-part="content"] {
/* styles for the content element */
}
[data-part="title"] {
/* styles for the title element */
}
[data-part="description"] {
/* styles for the description element */
}
[data-part="close-trigger"] {
/* styles for the close trigger element */
}
```
### Open and closed state
The dialog has two states: `open` and `closed`. You can use the `data-state`
attribute to style the dialog or trigger based on its state.
```css
[data-part="content"][data-state="open|closed"] {
/* styles for the open state */
}
[data-part="trigger"][data-state="open|closed"] {
/* styles for the open state */
}
```
## Methods and Properties
### Machine Context
The dialog machine exposes the following context properties:
**`ids`**
Type: `Partial<{ trigger: string; positioner: string; backdrop: string; content: string; closeTrigger: string; title: string; description: string; }>`
Description: The ids of the elements in the dialog. Useful for composition.
**`trapFocus`**
Type: `boolean`
Description: Whether to trap focus inside the dialog when it's opened
**`preventScroll`**
Type: `boolean`
Description: Whether to prevent scrolling behind the dialog when it's opened
**`modal`**
Type: `boolean`
Description: Whether to prevent pointer interaction outside the element and hide all content below it
**`initialFocusEl`**
Type: `() => HTMLElement`
Description: Element to receive focus when the dialog is opened
**`finalFocusEl`**
Type: `() => HTMLElement`
Description: Element to receive focus when the dialog is closed
**`restoreFocus`**
Type: `boolean`
Description: Whether to restore focus to the element that had focus before the dialog was opened
**`closeOnInteractOutside`**
Type: `boolean`
Description: Whether to close the dialog when the outside is clicked
**`closeOnEscape`**
Type: `boolean`
Description: Whether to close the dialog when the escape key is pressed
**`aria-label`**
Type: `string`
Description: Human readable label for the dialog, in event the dialog title is not rendered
**`role`**
Type: `"dialog" | "alertdialog"`
Description: The dialog's role
**`open`**
Type: `boolean`
Description: The controlled open state of the dialog
**`defaultOpen`**
Type: `boolean`
Description: The initial open state of the dialog when rendered.
Use when you don't need to control the open state of the dialog.
**`onOpenChange`**
Type: `(details: OpenChangeDetails) => void`
Description: Function to call when the dialog's open state changes
**`dir`**
Type: `"ltr" | "rtl"`
Description: The document's text/writing direction.
**`id`**
Type: `string`
Description: The unique identifier of the machine.
**`getRootNode`**
Type: `() => Node | ShadowRoot | Document`
Description: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.
**`onEscapeKeyDown`**
Type: `(event: KeyboardEvent) => void`
Description: Function called when the escape key is pressed
**`onPointerDownOutside`**
Type: `(event: PointerDownOutsideEvent) => void`
Description: Function called when the pointer is pressed down outside the component
**`onFocusOutside`**
Type: `(event: FocusOutsideEvent) => void`
Description: Function called when the focus is moved outside the component
**`onInteractOutside`**
Type: `(event: InteractOutsideEvent) => void`
Description: Function called when an interaction happens outside the component
**`persistentElements`**
Type: `(() => Element)[]`
Description: Returns the persistent elements that:
- should not have pointer-events disabled
- should not trigger the dismiss event
### Machine API
The dialog `api` exposes the following methods:
**`open`**
Type: `boolean`
Description: Whether the dialog is open
**`setOpen`**
Type: `(open: boolean) => void`
Description: Function to open or close the dialog
### Data Attributes
**`Trigger`**
**`data-scope`**: dialog
**`data-part`**: trigger
**`data-state`**: "open" | "closed"
**`Backdrop`**
**`data-scope`**: dialog
**`data-part`**: backdrop
**`data-state`**: "open" | "closed"
**`Content`**
**`data-scope`**: dialog
**`data-part`**: content
**`data-state`**: "open" | "closed"
## Accessibility
Adheres to the
[Alert and Message Dialogs WAI-ARIA design pattern](https://www.w3.org/WAI/ARIA/apg/patterns/alertdialog).
### Keyboard Interactions
**`Enter`**
Description: When focus is on the trigger, opens the dialog.
**`Tab`**
Description: Moves focus to the next focusable element within the content. Focus is trapped within the dialog.
**`Shift + Tab`**
Description: Moves focus to the previous focusable element. Focus is trapped within the dialog.
**`Esc`**
Description: Closes the dialog and moves focus to trigger or the defined final focus element
# Editable
Editable is an input field used for editing a single line of text. It renders as
static text and transforms into a text input field when then edit interaction is
triggered (click, focus, or double-click).
## Resources
[Latest version: v1.7.0](https://www.npmjs.com/package/@zag-js/editable)
[Logic Visualizer](https://zag-visualizer.vercel.app/editable)
[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/editable)
**Features**
- Use custom controls for the editable.
- Pressing `Enter` commits the input value.
- Pressing `Esc` reverts the value.
- Activate edit mode by double-clicking or focusing on the preview text.
- Auto-resize input to fit content
## Installation
To use the editable machine in your project, run the following command in your
command line:
```bash
npm install @zag-js/editable @zag-js/react
# or
yarn add @zag-js/editable @zag-js/react
```
This command will install the framework agnostic editable logic and the reactive
utilities for your framework of choice.
## Anatomy
To set up the editable correctly, you'll need to understand its anatomy and how
we name its parts.
> Each part includes a `data-part` attribute to help identify them in the DOM.
## Usage
First, import the editable package into your project
```jsx
import * as editable from "@zag-js/editable"
```
The editable package exports two key functions:
- `machine` — The state machine logic for the editable widget.
- `connect` — The function that translates the machine's state to JSX attributes
and event handlers.
> You'll need to provide a unique `id` to the `useMachine` hook. This is used to
> ensure that every part has a unique identifier.
Next, import the required hooks and functions for your framework and use the
editable machine in your project 🔥
```jsx
import * as editable from "@zag-js/editable"
import { useMachine, normalizeProps } from "@zag-js/react"
export default function Editable() {
const service = useMachine(editable.machine, { id: "1" })
const api = editable.connect(service, normalizeProps)
return (