Ark Logo

Styling

Learn how to style Ark UI components.

Overview

Ark UI is a headless component library that works with any styling solution. It provides functional styles for elements like popovers for positioning, while leaving presentation styles up to you. Some components also expose CSS variables that can be used for styling or animations.

Tip: Looking for a ready-to-use solution? Checkout Park UI for a collection of pre-designed styles based on Ark UI components.

Data Attributes

Ark UI components use data-scope and data-part attributes to target specific elements within a component. Interactive components often include data-* attributes to indicate their state. For example, here's what an open accordion item looks like:

<div data-scope="accordion" data-part="item" data-state="open"></div>

For more details on each component's data attributes, refer to their respective documentation.

Styling with CSS

When styling components with CSS, you can target the data attributes assigned to each component part for easy customization.

Styling a Part

To style a specific component part, target its data-scope and data-part attributes:

[data-scope='accordion'][data-part='item'] {
  border-bottom: 1px solid #e5e5e5;
}

Styling a State

To style a component based on its state, use the data-state attribute:

[data-scope='accordion'][data-part='item'][data-state='open'] {
  background-color: #f5f5f5;
}

Tip: If you prefer using classes instead of data attributes, utilize the class or className prop to add custom classes to Ark UI components.

Styling with Panda CSS

Panda CSS is a build-time CSS-in-JS framework that integrates seamlessly with Ark UI, providing an efficient styling solution.

Styling a part

Panda offers various ways to write styles, but in the context of Ark UI, we recommend using the defineSlotRecipe function to style a component with its different parts and variants.

import { accordionAnatomy } from '@ark-ui/anatomy'
import { defineSlotRecipe } from '@pandacss/dev'

export const accordionStyles = defineSlotRecipe({
  className: 'accordion',
  slots: accordionAnatomy.keys(),
  base: {
    item: {
      borderBottom: '1px solid #e5e5e5',
    },
  },
  defaultVariants: {},
  variants: {},
})

Styling a state

To style a component based on its state, you can use build in conditions in Panda CSS.

import { accordionAnatomy } from '@ark-ui/anatomy'
import { defineSlotRecipe } from '@pandacss/dev'

export const accordionStyles = defineSlotRecipe({
  className: 'accordion',
  slots: accordionAnatomy.keys(),
  base: {
    item: {
      borderBottom: '1px solid {colors.gray.300}',
      _open: { 
        backgroundColor: 'gray.100',
      },
    },
  },
  defaultVariants: {},
  variants: {},
})

Styling with Tailwind CSS

Tailwind CSS is a utility-first CSS framework providing a flexible way to style your components.

Styling a Part

To style a part, apply classes directly to the parts using either class or className, depending on the JavaScript framework.

<Accordion.Root>
  <Accordion.Item className="border-b border-gray-300">{/* … */}</Accordion.Item>
</Accordion.Root>

Styling a State

Leverage Tailwind CSS's variant selector to style a component based on its data-state attribute.

<Accordion.Root>
  <Accordion.Item className="border-b border-gray-300 data-[state=open]:bg-gray-100">
    {/* … */}
  </Accordion.Item>
</Accordion.Root>