Menu

Components

Note

A callout in the page: a few lines the reader should notice, with a color and an icon that say what kind of thing it is.

Introduction

Note is the box in the middle of a page that says "pay attention to this": a tip in a guide, a warning about a step, the result of an action, an error to fix. Its color and icon tell what kind of thing it is before it is read, and its text says the rest.

  • Four colors, default, success, warning and danger, each with its own icon, in a soft tint or an outline. Only the border, the tint and the icon are colored: the text stays the normal color, so it is easy to read in the light and the dark theme.
  • A title and text, or either alone. The text can hold links, lists and whatever else you need.
  • A button at the end for what the reader can do about it, and a dismiss button to close it.
  • It sits in the flow of the page. Choose how it is announced with role: as a part of the page, politely, or as an interruption.

Do not rely on color alone to carry the meaning: say it in the title or the text too. For a message over the page, use a Modal, and for one that goes away on its own, a Toast.

Installation

centerUI has no CLI or registry yet: components are copied into your project. Note uses Button for its dismiss button.

# bun
bun add tailwind-variants tailwind-merge clsx

# pnpm
pnpm add tailwind-variants tailwind-merge clsx

# npm
npm install tailwind-variants tailwind-merge clsx

Add Button first (see its Manual installation), then follow the steps below.

Manual installation

Copy these files into your project and keep the paths, or adjust the imports.

1. Add the styles

src/types/note-styles.tsShow code
import { tv } from "tailwind-variants";

// Styles for Note: a callout in the page. `variant` is the color and
// `appearance` how it is used: a soft tint, or an outline. The text stays the
// normal text color and only the border, the tint and the icon are colored, so
// it reads well in the light and the dark theme.
export const noteStyles = tv({
  slots: {
    root: "flex items-start gap-3 rounded-xl border text-fg",
    icon: "mt-0.5 shrink-0 [&_svg]:size-full",
    body: "flex min-w-0 flex-1 flex-col gap-1",
    title: "m-0 font-medium leading-snug",
    description:
      "text-fg/75 leading-relaxed [&_a]:font-medium [&_a]:underline [&_a]:underline-offset-4",
    action: "shrink-0 self-center",
    dismiss: "-mt-1 -mr-1.5 shrink-0",
  },
  variants: {
    variant: {
      default: { icon: "text-muted-fg" },
      success: { icon: "text-success" },
      warning: {
        icon: "text-[color-mix(in_oklab,var(--warning)_75%,var(--fg))]",
      },
      danger: { icon: "text-danger" },
    },
    appearance: { soft: {}, outline: {} },
    size: {
      sm: { root: "gap-2.5 p-3 text-[13px]", icon: "size-4" },
      md: { root: "p-4 text-sm", icon: "size-[18px]" },
    },
  },
  compoundVariants: [
    {
      appearance: "soft",
      variant: "default",
      class: { root: "border-border bg-muted" },
    },
    {
      appearance: "soft",
      variant: "success",
      class: { root: "border-success/30 bg-success/10" },
    },
    {
      appearance: "soft",
      variant: "warning",
      class: { root: "border-warning/40 bg-warning/15" },
    },
    {
      appearance: "soft",
      variant: "danger",
      class: { root: "border-danger/30 bg-danger/10" },
    },
    {
      appearance: "outline",
      variant: "default",
      class: { root: "border-border bg-transparent" },
    },
    {
      appearance: "outline",
      variant: "success",
      class: { root: "border-success/50 bg-transparent" },
    },
    {
      appearance: "outline",
      variant: "warning",
      class: { root: "border-warning/60 bg-transparent" },
    },
    {
      appearance: "outline",
      variant: "danger",
      class: { root: "border-danger/50 bg-transparent" },
    },
  ],
  defaultVariants: { variant: "default", appearance: "soft", size: "md" },
});

2. Add the Note file

src/components/ui/note/note.tsxShow code
"use client";

import { type ReactNode, useState } from "react";
import {
  AlertTriangleIcon,
  CheckCircleIcon,
  InfoIcon,
  XCircleIcon,
  XIcon,
} from "@/components/icons";
import { Button } from "@/components/ui/button/button";
import { cn } from "@/lib/cn";
import { noteStyles } from "@/types/note-styles";

type Variant = "default" | "success" | "warning" | "danger";

const defaultIcons: Record<Variant, ReactNode> = {
  default: <InfoIcon />,
  success: <CheckCircleIcon />,
  warning: <AlertTriangleIcon />,
  danger: <XCircleIcon />,
};

export interface NoteProps {
  /** The text under the title, or all of the text when there is no title. It can hold links. */
  children?: ReactNode;
  /** The first line, in bolder type. */
  title?: ReactNode;
  /**
   * The color, which also says what kind of note it is: neutral information,
   * success, a warning, or something that went wrong.
   */
  variant?: Variant;
  /** How the color is used: a soft tint, or just an outline. */
  appearance?: "soft" | "outline";
  size?: "sm" | "md";
  /** Replaces the icon of the variant. `false` leaves the icon out. */
  icon?: ReactNode | false;
  /** A control at the end, such as a button: "Undo", "Fix it", "Learn more". */
  action?: ReactNode;
  /** Add a button that closes the note. */
  isDismissible?: boolean;
  /** Called when the note is dismissed. */
  onDismiss?: () => void;
  /** Names the dismiss button. Defaults to "Dismiss". */
  dismissLabel?: string;
  /** Whether it is shown, for a controlled note. Pair it with `onDismiss`. */
  isOpen?: boolean;
  /** Whether it is shown at first, for an uncontrolled note. Defaults to `true`. */
  defaultOpen?: boolean;
  /**
   * How it is announced. `note` (the default) is read in its place, as a part
   * of the page. `status` is announced politely when it appears, and `alert`
   * interrupts, for something the user must know right away.
   */
  role?: "note" | "status" | "alert";
  className?: string;
}

/**
 * A callout in the page: a few lines that say something the reader should
 * notice, with a color and an icon that tell what kind of thing it is. A tip,
 * a warning about a step, the result of an action, an error to fix. It can
 * have a button, and it can be dismissed.
 *
 * It sits in the flow of the page and does not float. For a message that
 * covers the page, use a modal, and for one that goes away by itself, a toast.
 *
 * @example
 * <Note variant="warning" title="Your trial ends in 3 days">
 *   Add a payment method to keep your projects.
 * </Note>
 */
export function Note({
  children,
  title,
  variant = "default",
  appearance,
  size,
  icon,
  action,
  isDismissible = false,
  onDismiss,
  dismissLabel = "Dismiss",
  isOpen,
  defaultOpen = true,
  role = "note",
  className,
}: NoteProps) {
  const [inner, setInner] = useState(defaultOpen);
  const open = isOpen ?? inner;
  if (!open) return null;

  const styles = noteStyles({ variant, appearance, size });
  const mark = icon === undefined ? defaultIcons[variant] : icon;

  return (
    <div role={role} className={cn(styles.root(), className)}>
      {mark ? (
        <span aria-hidden="true" className={styles.icon()}>
          {mark}
        </span>
      ) : null}
      <div className={styles.body()}>
        {title ? <p className={styles.title()}>{title}</p> : null}
        {children ? (
          <div className={styles.description()}>{children}</div>
        ) : null}
      </div>
      {action ? <div className={styles.action()}>{action}</div> : null}
      {isDismissible ? (
        <Button
          isIcon
          variant="ghost"
          size="xs"
          aria-label={dismissLabel}
          onPress={() => {
            setInner(false);
            onDismiss?.();
          }}
          className={styles.dismiss()}
        >
          <XIcon />
        </Button>
      ) : null}
    </div>
  );
}

It imports InfoIcon, CheckCircleIcon, AlertTriangleIcon, XCircleIcon and XIcon from @/components/icons. Add those icons, or swap them for your own.

3. Use it

import { Note } from "@/components/ui/note/note";

Anatomy

An icon, a body with a title and text, and optionally an action and a dismiss button.

<Note
  variant="warning"
  title="Your trial ends in 3 days"
  action={<Button size="sm">Add a card</Button>}
  isDismissible
>
  Add a payment method to keep your projects.
</Note>
PartRendered asNotes
Notediv with a roleThe box, with a border and a tint in the color of the variant.
Iconspan, hidden from screen readersOne for each variant. Replace it with icon, or remove it with icon={false}.
TitlepThe first line, in bolder type.
TextdivThe children. It can hold links, lists and other elements.
ActiondivA control at the end, such as a button.
Dismiss buttonButtonAt the corner, with isDismissible.

Examples

Usage

Heads up

You can change this later in your settings.

Variants

The color, and the icon that goes with it: information, success, a warning, and something that went wrong.

Did you know?

Notes can hold links, lists and buttons.

Your changes were saved

Everyone on the team can see them now.

Your trial ends in 3 days

Add a payment method to keep your projects.

We could not send the invite

The address looks wrong. Check it and try again.

Appearance

A soft tint, or just an outline for a lighter touch.

Soft

A tint and a border in the color of the variant.

Outline

Only the border, on the page's own background.

Sizes

Small

Tighter, for a form or a table cell.

Medium

The default.

Title or text only

Both are optional. A short message can be all title, or all text.

Changes are saved as you type.

Copied to the clipboard

Rich content

The text is a container: put paragraphs, lists and links in it. Links are underlined and stay readable on the tint.

This API is going away

It will be removed in version 3. Move to the new one before then.

With an action

A button at the end, for what the reader can do about it.

Your payment failed

The card was declined. Try again or use another card.

Nothing pressed yet.

Dismissible

isDismissible adds a button that hides the note. It keeps its own state, and onDismiss runs when it is pressed, to remember that the reader closed it.

A tip you can close

It will not come back until the page is loaded again.

Controlled

Keep whether it is shown in your own state with isOpen and onDismiss, to show it again or to keep it closed.

Profile saved

Your changes are live.

isOpen: true

Icon

Every variant has an icon. Replace it with your own, or leave it out.

New notifications

You have 3 unread messages.

Featured

Your project is on the front page this week.

Plain

No icon, only text.

Role

role decides how a screen reader treats it. "note", the default, is a part of the page and is read in its place. "status" is announced politely when it appears, for the result of an action. "alert" interrupts, for something the reader must know right away. Use the last one sparingly.

Tip

Read where it stands in the page.

Saved

Announced politely when it appears.

Component API

Note takes the props below.

PropTypeDefault
childrenReactNode-
titleReactNode-
variant"default" | "success" | "warning" | "danger""default"
appearance"soft" | "outline""soft"
size"sm" | "md""md"
iconReactNode | falsethe icon of the variant
actionReactNode-
isDismissiblebooleanfalse
onDismiss() => void-
dismissLabelstring"Dismiss"
isOpenboolean-
defaultOpenbooleantrue
role"note" | "status" | "alert""note"
classNamestring-