PeakUI

Styling

The ways to restyle a PeakUI component - class names, state selectors, BEM classes, component variables and variant functions.

PeakUI components are styled by plain CSS classes. Nothing is generated at runtime, so every technique you already use with CSS and Tailwind works.

Basic Styling

Pass className to any component or part. Your classes are merged with the component's own:

import {Button} from "@peakui/react";

<Button className="rounded-full px-6">Rounded</Button>

style works as usual:

<Button style={{letterSpacing: "0.02em"}}>Spaced</Button>

There is no classNames prop. To style a part, pass className to that part:

<Fieldset className="max-w-sm">
  <Fieldset.Legend className="text-lg">Profile</Fieldset.Legend>
</Fieldset>

State-Based Styling

Components expose their state as data attributes, which you can target from Tailwind or CSS:

<Button className="data-[pressed]:scale-95 data-[hovered]:shadow-md">Press me</Button>
.button[data-pressed] {
  transform: scale(0.97);
}

@peakui/styles also registers short variants for the most common states:

VariantMatches
hovered:[data-hovered]
pressed:[data-pressed]
focus-visible-within:[data-focus-visible]
selected:[data-selected]
disabled-state:[data-disabled]
entering:[data-entering]
exiting:[data-exiting]
<Button className="pressed:scale-95 hovered:shadow-md">Press me</Button>

Render Props

For components built on a React Aria primitive, className and children also accept a function that receives the component's state:

"use client";

import {Button} from "@peakui/react";

<Button className={({isPressed}) => (isPressed ? "scale-95" : "")}>
  {({isHovered}) => (isHovered ? "Release to confirm" : "Confirm")}
</Button>

A function prop needs a client component: a server component cannot pass a function to Button.

BEM Classes

Every component is a BEM block. Variants are modifiers and parts are elements:

.button            /* block */
.button--primary   /* modifier: variant */
.button--sm        /* modifier: size */
.fieldset__legend  /* element: a part */

Override them in the components layer, after the PeakUI import:

globals.css
@import "tailwindcss";
@import "@peakui/styles";

@layer components {
  .button {
    @apply font-semibold tracking-wide;
  }

  .button--sm {
    @apply px-2;
  }
}

Component Variables

Where a component's colors or radius are meant to change, it reads them from its own custom properties. Setting the variable is safer than overriding the declaration, because hover, pressed and focus styles keep working:

@layer components {
  .button--secondary {
    --button-bg: var(--color-surface);
    --button-bg-hover: var(--color-surface-hover);
    --button-fg: var(--color-surface-foreground);
  }
}

Slots

Every rendered element carries a data-slot attribute naming the part. It is a stable hook for styling a part from its parent:

.my-form [data-slot="label"] {
  @apply text-xs tracking-wide uppercase;
}

Variant Functions

Each component's class names come from a variant function exported next to it, such as buttonVariants. Calling it returns the class string:

import {buttonVariants} from "@peakui/react";

buttonVariants({size: "sm", variant: "outline"});
// "button button--sm button--outline"

Use it to give another element the look of a component:

<a className={buttonVariants({variant: "primary"})} href="/pricing">
  See pricing
</a>

Adding Variants

Describe the variants of your own with tv, imported from @peakui/react, and let Button keep size and variant:

import type {ButtonProps, VariantProps} from "@peakui/react";

import {Button, tv} from "@peakui/react";

const myButtonVariants = tv({
  variants: {
    radius: {
      full: "rounded-full",
      sm: "rounded-sm",
    },
  },
});

interface MyButtonProps
  extends Omit<ButtonProps, "className">, VariantProps<typeof myButtonVariants> {
  className?: string;
}

export function MyButton({className, radius, ...props}: MyButtonProps) {
  return <Button className={myButtonVariants({className, radius})} {...props} />;
}

Import tv from @peakui/react (or @peakui/styles), not from your own copy of tailwind-variants, so that the variant functions and your tv are the same instance. Do not extend buttonVariants in a wrapper around Button: Button applies buttonVariants itself, so the wrapper adds only its own classes, and extending would put the size and variant classes on the element twice.

tv({extend: buttonVariants}) is the right tool when the element is not a Button, for example an <a> that should look like one. The extended function keeps size and variant and accepts your own variants on top:

import {buttonVariants, tv} from "@peakui/react";

const linkButtonVariants = tv({
  extend: buttonVariants,
  variants: {
    radius: {
      full: "rounded-full",
      sm: "rounded-sm",
    },
  },
});

<a className={linkButtonVariants({radius: "full", variant: "secondary"})} href="/pricing">
  See pricing
</a>;
// "button rounded-full button--md button--secondary"

Next Steps

  • Theming – change tokens instead of individual components.
  • Composition – parts, the render prop and custom components.

On this page