Skip to content

ComponentsActions

SourceSource

Button

Displays a Discord-styled button with brand, secondary, success, danger and link variants.

The button carries five intents, five sizes and an icon shape. Which one you reach for is a statement about what the action does, not about how it should look — danger is destructive, success is confirming, link is navigation that happens to sit in a row of buttons.

Root class newt-btn
import { Button } from "@/registry/bases/newt/ui/button"

/* Inline so the demo never reaches out to the network for a placeholder. */
const EMOJI =
  "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Ccircle cx='8' cy='8' r='7' fill='%23f0b232'/%3E%3C/svg%3E"

export default function ButtonDemo() {
  return (
    <div className="flex flex-wrap items-center gap-3">
      <Button variant="primary">Primary</Button>
      <Button variant="secondary">Secondary</Button>
      <Button variant="success">Success</Button>
      <Button variant="danger">Danger</Button>
      <Button variant="secondary" emoji={EMOJI}>
        With emoji
      </Button>
      <Button variant="link">Link button</Button>
    </div>
  )
}
<script setup lang="ts">
import { Button } from "~~/registry/bases/newt/ui/button"

/* Inline so the demo never reaches out to the network for a placeholder. */
const EMOJI =
  "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Ccircle cx='8' cy='8' r='7' fill='%23f0b232'/%3E%3C/svg%3E"
</script>

<template>
  <div class="flex flex-wrap items-center gap-3">
    <Button variant="primary">Primary</Button>
    <Button variant="secondary">Secondary</Button>
    <Button variant="success">Success</Button>
    <Button variant="danger">Danger</Button>
    <Button variant="secondary" :emoji="EMOJI">With emoji</Button>
    <Button variant="link">Link button</Button>
  </div>
</template>

Installation

npx newtui@latest add buttonnpx shadcn@latest add @newtui/buttonnpx shadcn-vue@latest add @newtui/buttonpnpm dlx newtui@latest add buttonpnpm dlx shadcn@latest add @newtui/buttonpnpm dlx shadcn-vue@latest add @newtui/buttonyarn dlx newtui@latest add buttonyarn dlx shadcn@latest add @newtui/buttonyarn dlx shadcn-vue@latest add @newtui/buttonbunx newtui@latest add buttonbunx shadcn@latest add @newtui/buttonbunx shadcn-vue@latest add @newtui/buttondeno run -A npm:newtui@latest add buttondeno run -A npm:shadcn@latest add @newtui/buttondeno run -A npm:shadcn-vue@latest add @newtui/buttonvlx newtui@latest add buttonvlx shadcn@latest add @newtui/buttonvlx shadcn-vue@latest add @newtui/buttonvp dlx newtui@latest add buttonvp dlx shadcn@latest add @newtui/buttonvp dlx shadcn-vue@latest add @newtui/buttonnubx newtui@latest add buttonnubx shadcn@latest add @newtui/buttonnubx shadcn-vue@latest add @newtui/buttonnlx newtui@latest add buttonnlx shadcn@latest add @newtui/buttonnlx shadcn-vue@latest add @newtui/buttonupx newtui@latest add buttonupx shadcn@latest add @newtui/buttonupx shadcn-vue@latest add @newtui/button

The shadcn CLI needs the @newtui registry in your components.json. Set it up.

Install the dependencies

npm install class-variance-authoritypnpm add class-variance-authorityyarn add class-variance-authoritybun add class-variance-authoritydeno add npm:class-variance-authorityvlt install class-variance-authorityvp add class-variance-authoritynub add class-variance-authorityni class-variance-authorityupm add class-variance-authority

Copy the source into your project

import { cva, type VariantProps } from "class-variance-authority"
import * as React from "react"

import { cn } from "@/lib/utils"

/*
 * Leaving a hover is quick (`duration-instant`, ease-in); arriving is a little
 * slower (`duration-base`, ease-out). The border is transparent: it takes no
 * colour, but a forced-colours mode draws it, so the button keeps an edge.
 */
const buttonVariants = cva(
  "relative box-border inline-flex max-w-full cursor-pointer select-none items-center justify-center gap-[var(--newt-space-1)] whitespace-nowrap border border-solid border-transparent font-[family-name:var(--newt-font-sans)] font-[number:var(--newt-font-weight-medium)] no-underline transition-[background-color,color,border-color,opacity] duration-instant ease-newt-in hover:duration-base hover:ease-newt-out focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-focus-ring disabled:cursor-not-allowed disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:cursor-not-allowed aria-disabled:opacity-50 motion-reduce:transition-none [&>*]:transition-opacity [&>*]:duration-slow [&>*]:ease-newt-out motion-reduce:[&>*]:transition-none",
  {
    variants: {
      variant: {
        primary:
          "bg-newt-brand text-newt-on-accent hover:bg-newt-brand-hover active:bg-newt-brand-active",
        secondary:
          "bg-newt-bg-hover text-newt-text-primary hover:bg-newt-bg-active",
        danger: "bg-newt-danger text-newt-on-accent hover:bg-newt-danger-hover",
        success: "bg-newt-success text-newt-on-accent hover:brightness-90",
        link: "bg-transparent text-newt-text-link hover:underline",
      },
      /*
       * Each size sets the line height after the text size: `cn` drops a
       * `leading-*` that comes before a text size class. The padding is one
       * pixel short of the space token, which the transparent border makes up.
       */
      size: {
        xs: "min-h-newt-control-xs rounded-[var(--newt-radius-sm)] px-[calc(var(--newt-space-2)-1px)] py-[calc(var(--newt-space-1)-1px)] text-[length:var(--newt-font-size-xs)] leading-[var(--newt-line-height-tight)]",
        sm: "min-h-newt-control-sm rounded-[var(--newt-radius-md)] px-[calc(var(--newt-space-3)-1px)] py-[calc(var(--newt-space-1)-1px)] text-[length:var(--newt-font-size-sm)] leading-[var(--newt-line-height-tight)]",
        md: "min-h-newt-control-md rounded-[var(--newt-radius-md)] px-[calc(var(--newt-space-4)-1px)] py-[calc(var(--newt-space-2)-1px)] text-[length:var(--newt-font-size-sm)] leading-[var(--newt-line-height-tight)]",
        /*
         * 32px, the height of a button in a message's action row. The height
         * is fixed, so an emoji image, a leading icon or the link variant
         * cannot make one button of the row taller or shorter.
         */
        message:
          "h-[32px] rounded-[var(--newt-radius-md)] px-[calc(var(--newt-space-4)-1px)] py-0 text-[length:var(--newt-font-size-sm)] leading-[var(--newt-line-height-tight)]",
        lg: "min-h-newt-control-md rounded-[var(--newt-radius-md)] px-[calc(var(--newt-space-6)-1px)] py-[calc(var(--newt-space-3)-1px)] text-[length:var(--newt-font-size-md)] leading-[var(--newt-line-height-tight)]",
        icon: "h-newt-control-sm w-[var(--newt-control-sm)] rounded-[var(--newt-radius-full)] p-[var(--newt-space-1)] text-[length:var(--newt-font-size-sm)] leading-[var(--newt-line-height-tight)]",
      },
      fullWidth: {
        true: "w-full",
      },
      /*
       * The label stays in place, invisible, so the button keeps its width
       * and its accessible name; the dots sit over it.
       */
      loading: {
        true: "cursor-wait [&>:not([data-button-dots])]:opacity-0",
      },
      /* Set when the button has a text label; an icon-only button has none. */
      labelled: {
        true: "",
      },
    },
    compoundVariants: [
      /*
       * A labelled button is never narrower than this, so a row of short
       * labels ("OK", "No") still reads as a row of buttons.
       */
      {
        variant: ["primary", "secondary", "danger", "success"],
        size: ["xs", "sm"],
        labelled: true,
        class: "min-w-[60px]",
      },
      {
        variant: ["primary", "secondary", "danger", "success"],
        size: ["md", "lg"],
        labelled: true,
        class: "min-w-[100px]",
      },
      {
        variant: "link",
        size: ["xs", "sm", "md", "message", "lg"],
        class: "px-0",
      },
    ],
    defaultVariants: {
      variant: "primary",
      size: "md",
    },
  }
)

const dotsKeyframes = `
@keyframes newt-button-dots-in { from { opacity: 0; } }
@keyframes newt-button-dot {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(0.8); opacity: 0.3; }
}`

/* Three pulsing dots over the hidden label while the button loads. */
const LoadingDots = () => (
  <span
    data-button-dots=""
    aria-hidden="true"
    className="absolute inset-0 flex items-center justify-center gap-[2px] animate-[newt-button-dots-in_var(--newt-duration-slow)_var(--newt-ease-out)] motion-reduce:animate-none"
  >
    <style>{dotsKeyframes}</style>
    {[0, 0.2, 0.4].map((delay) => (
      <span
        key={delay}
        className="h-[6px] w-[6px] rounded-[var(--newt-radius-full)] bg-current animate-[newt-button-dot_1.4s_ease-in-out_infinite] motion-reduce:animate-none"
        style={{ animationDelay: `${delay}s` }}
      />
    ))}
  </span>
)

/* The "opens elsewhere" glyph Discord puts on link buttons. */
const LaunchIcon = () => (
  <svg
    viewBox="0 0 24 24"
    aria-hidden="true"
    focusable="false"
    className="h-4 w-4 shrink-0"
  >
    <path
      fill="currentColor"
      d="M10 5a1 1 0 0 0 0 2h6.59L4.3 19.3a1 1 0 1 0 1.4 1.4L18 8.42V15a1 1 0 1 0 2 0V6a1 1 0 0 0-1-1h-9Z"
    />
  </svg>
)

export interface ButtonProps
  extends
    React.ButtonHTMLAttributes<HTMLButtonElement>,
    VariantProps<typeof buttonVariants> {
  /**
   * The element or component to render. Defaults to `button`, or to `a` when
   * `href` is set; pass a router link component to keep client navigation.
   */
  as?: React.ElementType
  /** Render a link to this URL. */
  href?: string
  /** Link target, with `href`. */
  target?: React.AnchorHTMLAttributes<HTMLAnchorElement>["target"]
  /** Link relationship, with `href`. */
  rel?: string
  /** Emoji image URL, rendered before the label. */
  emoji?: string
  /** Accessible name for `emoji`; empty (the default) hides it from readers. */
  emojiAlt?: string
  /**
   * Content before the label, such as an icon component or a unicode emoji.
   * It sits in its own box, so the button's gap separates it from the label.
   */
  leading?: React.ReactNode
  /** Content after the label, before the link-out glyph. */
  trailing?: React.ReactNode
  /** Link-out glyph after the label. Defaults on for `variant="link"`. */
  launchIcon?: boolean
  /**
   * Show three dots over the label and ignore clicks. The button keeps its
   * width, its accessible name and its focus, and sets `aria-busy`.
   */
  loading?: boolean
  /** Fill the width of the container. */
  fullWidth?: boolean
}

/*
 * No box without content: an empty box would still take a gap and push an
 * emoji-only button off centre. `toArray` drops `null`, `undefined` and
 * booleans, so `leading={cond && <Icon />}` leaves no box when `cond` is false.
 */
function renders(node: React.ReactNode): boolean {
  return React.Children.toArray(node).length > 0
}

function stopClick(event: React.MouseEvent<HTMLElement>) {
  event.preventDefault()
}

const Button = React.forwardRef<HTMLElement, ButtonProps>(
  (
    {
      as,
      className,
      variant,
      size,
      type = "button",
      href,
      disabled,
      emoji,
      emojiAlt = "",
      leading,
      trailing,
      launchIcon,
      loading = false,
      fullWidth = false,
      onClick,
      children,
      ...props
    },
    ref
  ) => {
    const Tag: React.ElementType = as ?? (href === undefined ? "button" : "a")
    const isButton = Tag === "button"
    const showLaunch = launchIcon ?? variant === "link"
    /*
     * A loading button stays focusable, so it must stop the click itself,
     * including the one `Enter` and `Space` fire. So must a disabled router
     * component, or a disabled link the caller gave a handler: neither has a
     * native `disabled`. Every other button gets the caller's handler as is,
     * so a Server Component can render it.
     */
    const stopsClick =
      loading ||
      (disabled === true &&
        !isButton &&
        (as !== undefined || onClick !== undefined))
    return (
      <Tag
        ref={ref}
        type={isButton ? type : undefined}
        /*
         * A link has no `disabled`: it loses its `href` instead, which takes
         * it out of the tab order, and says so with `aria-disabled`.
         */
        href={disabled ? undefined : href}
        disabled={isButton ? disabled : undefined}
        aria-disabled={!isButton && disabled ? true : undefined}
        aria-busy={loading || undefined}
        className={cn(
          buttonVariants({
            variant,
            size,
            fullWidth,
            loading,
            labelled: renders(children),
          }),
          className
        )}
        {...props}
        onClick={stopsClick ? stopClick : onClick}
      >
        {emoji ? (
          <img
            src={emoji}
            alt={emojiAlt}
            aria-hidden={emojiAlt === "" ? true : undefined}
            draggable={false}
            className="h-[1.375em] w-[1.375em] shrink-0 object-contain align-bottom"
          />
        ) : null}
        {renders(leading) ? (
          <span className="inline-flex shrink-0 items-center">{leading}</span>
        ) : null}
        {renders(children) ? (
          <span className="truncate">{children}</span>
        ) : null}
        {renders(trailing) ? (
          <span className="inline-flex shrink-0 items-center">{trailing}</span>
        ) : null}
        {showLaunch ? <LaunchIcon /> : null}
        {loading ? <LoadingDots /> : null}
      </Tag>
    )
  }
)
Button.displayName = "Button"

export { Button, buttonVariants }
<script setup lang="ts">
import type { ButtonHTMLAttributes, Component, HTMLAttributes } from "vue"
import {
  Comment,
  computed,
  Fragment,
  isVNode,
  type VNode,
  type VNodeArrayChildren,
} from "vue"

import { cn } from "@/lib/utils"

import { type ButtonVariants, buttonVariants } from "."

const {
  as,
  href,
  variant,
  size,
  type = "button",
  disabled = false,
  class: className,
  emoji,
  emojiAlt = "",
  /*
   * `launchIcon` needs `undefined` to mean "let the variant decide" — but Vue
   * casts an absent prop typed `boolean` to `false` unless it has a default.
   * The default `undefined` is what keeps that cast from firing, so the `??`
   * below ever sees anything but `false`.
   */
  launchIcon = undefined,
  loading = false,
  fullWidth = false,
} = defineProps<{
  /**
   * The element or component to render. Defaults to `button`, or to `a`
   * when `href` is set; pass `NuxtLink` or `RouterLink` to keep client
   * navigation.
   */
  as?: string | Component
  /** Render a link to this URL. */
  href?: string
  variant?: ButtonVariants["variant"]
  size?: ButtonVariants["size"]
  type?: ButtonHTMLAttributes["type"]
  disabled?: boolean
  class?: HTMLAttributes["class"]
  /** Emoji image URL, rendered before the label. */
  emoji?: string
  /** Accessible name for `emoji`; empty (the default) hides it from readers. */
  emojiAlt?: string
  /** Link-out glyph after the label. Defaults on for `variant="link"`. */
  launchIcon?: boolean
  /**
   * Show three dots over the label and ignore clicks. The button keeps its
   * width, its accessible name and its focus, and sets `aria-busy`.
   */
  loading?: boolean
  /** Fill the width of the container. */
  fullWidth?: boolean
}>()

const emit = defineEmits<{
  /** Not emitted while the button is disabled or loading. */
  click: [event: MouseEvent]
}>()

const slots = defineSlots<{
  /** The label. */
  default?: () => VNode[]
  /** Content before the label, such as an icon component or a unicode emoji. */
  leading?: () => VNode[]
  /** Content after the label, before the link-out glyph. */
  trailing?: () => VNode[]
}>()

/*
 * Whether a slot renders anything. A slot that is passed but empty, such as
 * `<template #leading><Icon v-if="cond" /></template>`, yields only a comment
 * or an empty fragment, and must not leave a box that takes a gap. Of the
 * other child values, these render nothing; any other text renders.
 */
const NOTHING = new Set<unknown>([null, undefined, true, false, ""])

function visible(nodes: VNodeArrayChildren): boolean {
  return nodes.some((node) => {
    if (Array.isArray(node)) return visible(node)
    if (!isVNode(node)) return !NOTHING.has(node)
    if (node.type === Comment) return false
    if (node.type === Fragment) {
      return Array.isArray(node.children) && visible(node.children)
    }
    return true
  })
}

function renders(slot: (() => VNode[]) | undefined): boolean {
  return slot !== undefined && visible(slot())
}

const showLaunch = computed(() => launchIcon ?? variant === "link")
const tag = computed(() => as ?? (href === undefined ? "button" : "a"))
/*
 * A link has no `disabled`: it loses its `href` instead, which takes it out of
 * the tab order, and says so with `aria-disabled`.
 */
const isButton = computed(() => tag.value === "button")

/*
 * A loading button stays focusable, so it must stop the click itself,
 * including the one `Enter` and `Space` fire. So must a disabled link or
 * router component, which has no native `disabled`.
 */
function onClick(event: MouseEvent) {
  if (loading || disabled) {
    event.preventDefault()
    return
  }
  emit("click", event)
}

const dotDelays = [0, 0.2, 0.4]
</script>

<template>
  <component
    :is="tag"
    :type="isButton ? type : undefined"
    :href="disabled ? undefined : href"
    :disabled="isButton ? disabled : undefined"
    :aria-disabled="!isButton && disabled ? true : undefined"
    :aria-busy="loading || undefined"
    :class="
      cn(
        buttonVariants({
          variant,
          size,
          fullWidth,
          loading,
          labelled: renders(slots.default),
        }),
        className
      )
    "
    @click="onClick"
  >
    <img
      v-if="emoji"
      :src="emoji"
      :alt="emojiAlt"
      :aria-hidden="emojiAlt === '' ? true : undefined"
      :draggable="false"
      class="h-[1.375em] w-[1.375em] shrink-0 object-contain align-bottom"
    />
    <!--
      Leading and trailing content sit in their own box, so the button's gap
      separates an icon component or a unicode emoji from the label. No label
      box without a label: an empty box would still take a gap.
    -->
    <span
      v-if="renders(slots.leading)"
      class="inline-flex shrink-0 items-center"
    >
      <slot name="leading" />
    </span>
    <span v-if="renders(slots.default)" class="truncate"><slot /></span>
    <span
      v-if="renders(slots.trailing)"
      class="inline-flex shrink-0 items-center"
    >
      <slot name="trailing" />
    </span>
    <!-- The "opens elsewhere" glyph Discord puts on link buttons. -->
    <svg
      v-if="showLaunch"
      viewBox="0 0 24 24"
      aria-hidden="true"
      focusable="false"
      class="h-4 w-4 shrink-0"
    >
      <path
        fill="currentColor"
        d="M10 5a1 1 0 0 0 0 2h6.59L4.3 19.3a1 1 0 1 0 1.4 1.4L18 8.42V15a1 1 0 1 0 2 0V6a1 1 0 0 0-1-1h-9Z"
      />
    </svg>
    <!-- Three pulsing dots over the hidden label while the button loads. -->
    <span
      v-if="loading"
      data-button-dots=""
      aria-hidden="true"
      class="absolute inset-0 flex items-center justify-center gap-[2px] animate-[newt-button-dots-in_var(--newt-duration-slow)_var(--newt-ease-out)] motion-reduce:animate-none"
    >
      <span
        v-for="delay in dotDelays"
        :key="delay"
        class="h-[6px] w-[6px] rounded-[var(--newt-radius-full)] bg-current animate-[newt-button-dot_1.4s_ease-in-out_infinite] motion-reduce:animate-none"
        :style="{ animationDelay: `${delay}s` }"
      />
    </span>
  </component>
</template>

<style>
@keyframes newt-button-dots-in {
  from {
    opacity: 0;
  }
}
@keyframes newt-button-dot {
  0%,
  100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(0.8);
    opacity: 0.3;
  }
}
</style>
import { cva, type VariantProps } from "class-variance-authority"

export { default as Button } from "./Button.vue"

/*
 * Leaving a hover is quick (`duration-instant`, ease-in); arriving is a little
 * slower (`duration-base`, ease-out). The border is transparent: it takes no
 * colour, but a forced-colours mode draws it, so the button keeps an edge.
 */
export const buttonVariants = cva(
  "relative box-border inline-flex max-w-full cursor-pointer select-none items-center justify-center gap-[var(--newt-space-1)] whitespace-nowrap border border-solid border-transparent font-[family-name:var(--newt-font-sans)] font-[number:var(--newt-font-weight-medium)] no-underline transition-[background-color,color,border-color,opacity] duration-instant ease-newt-in hover:duration-base hover:ease-newt-out focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-focus-ring disabled:cursor-not-allowed disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:cursor-not-allowed aria-disabled:opacity-50 motion-reduce:transition-none [&>*]:transition-opacity [&>*]:duration-slow [&>*]:ease-newt-out motion-reduce:[&>*]:transition-none",
  {
    variants: {
      variant: {
        primary:
          "bg-newt-brand text-newt-on-accent hover:bg-newt-brand-hover active:bg-newt-brand-active",
        secondary:
          "bg-newt-bg-hover text-newt-text-primary hover:bg-newt-bg-active",
        danger: "bg-newt-danger text-newt-on-accent hover:bg-newt-danger-hover",
        success: "bg-newt-success text-newt-on-accent hover:brightness-90",
        link: "bg-transparent text-newt-text-link hover:underline",
      },
      /*
       * Each size sets the line height after the text size: `cn` drops a
       * `leading-*` that comes before a text size class. The padding is one
       * pixel short of the space token, which the transparent border makes up.
       */
      size: {
        xs: "min-h-newt-control-xs rounded-[var(--newt-radius-sm)] px-[calc(var(--newt-space-2)-1px)] py-[calc(var(--newt-space-1)-1px)] text-[length:var(--newt-font-size-xs)] leading-[var(--newt-line-height-tight)]",
        sm: "min-h-newt-control-sm rounded-[var(--newt-radius-md)] px-[calc(var(--newt-space-3)-1px)] py-[calc(var(--newt-space-1)-1px)] text-[length:var(--newt-font-size-sm)] leading-[var(--newt-line-height-tight)]",
        md: "min-h-newt-control-md rounded-[var(--newt-radius-md)] px-[calc(var(--newt-space-4)-1px)] py-[calc(var(--newt-space-2)-1px)] text-[length:var(--newt-font-size-sm)] leading-[var(--newt-line-height-tight)]",
        /*
         * 32px, the height of a button in a message's action row. The height
         * is fixed, so an emoji image, a leading icon or the link variant
         * cannot make one button of the row taller or shorter.
         */
        message:
          "h-[32px] rounded-[var(--newt-radius-md)] px-[calc(var(--newt-space-4)-1px)] py-0 text-[length:var(--newt-font-size-sm)] leading-[var(--newt-line-height-tight)]",
        lg: "min-h-newt-control-md rounded-[var(--newt-radius-md)] px-[calc(var(--newt-space-6)-1px)] py-[calc(var(--newt-space-3)-1px)] text-[length:var(--newt-font-size-md)] leading-[var(--newt-line-height-tight)]",
        icon: "h-newt-control-sm w-[var(--newt-control-sm)] rounded-[var(--newt-radius-full)] p-[var(--newt-space-1)] text-[length:var(--newt-font-size-sm)] leading-[var(--newt-line-height-tight)]",
      },
      fullWidth: {
        true: "w-full",
      },
      /*
       * The label stays in place, invisible, so the button keeps its width
       * and its accessible name; the dots sit over it.
       */
      loading: {
        true: "cursor-wait [&>:not([data-button-dots])]:opacity-0",
      },
      /* Set when the button has a text label; an icon-only button has none. */
      labelled: {
        true: "",
      },
    },
    compoundVariants: [
      /*
       * A labelled button is never narrower than this, so a row of short
       * labels ("OK", "No") still reads as a row of buttons.
       */
      {
        variant: ["primary", "secondary", "danger", "success"],
        size: ["xs", "sm"],
        labelled: true,
        class: "min-w-[60px]",
      },
      {
        variant: ["primary", "secondary", "danger", "success"],
        size: ["md", "lg"],
        labelled: true,
        class: "min-w-[100px]",
      },
      {
        variant: "link",
        size: ["xs", "sm", "md", "message", "lg"],
        class: "px-0",
      },
    ],
    defaultVariants: {
      variant: "primary",
      size: "md",
    },
  }
)

export type ButtonVariants = VariantProps<typeof buttonVariants>

Update the import paths

The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.

No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Button to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.

No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Button to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.

Usage

import { Button } from "@/components/ui/button"

export function Example() {
  return <Button />
}
<script setup lang="ts">
import { Button } from "@/components/ui/button"
</script>

<template>
  <Button />
</template>

Examples

Sizes

xs, sm and md set a minimum height from the control tokens: 24px, 32px and 40px. md is the default. lg has more padding and larger text than md. A button with a label is at least 60px wide at xs and sm, and 100px at md and lg. message is a fixed 32px, the size of a button in a message’s action row. icon is a 32px circle for a lone glyph. An icon button has no visible text, so give it an aria-label.

In the HTML flavour, a bare newt-btn is the md size, and newt-btn--xs, newt-btn--sm, newt-btn--md, newt-btn--message and newt-btn--lg pick one.

Root class newt-btn
import { Button } from "@/registry/bases/newt/ui/button"

/* `icon` has no visible text, so the label moves to `aria-label`. */
export default function ButtonSizesDemo() {
  return (
    <div className="flex flex-wrap items-center gap-3">
      <Button size="xs">Extra small</Button>
      <Button size="sm">Small</Button>
      <Button size="message">Message</Button>
      <Button size="md">Medium</Button>
      <Button size="lg">Large</Button>
      <Button size="icon" variant="secondary" aria-label="Add a reaction">
        <svg
          viewBox="0 0 24 24"
          aria-hidden="true"
          focusable="false"
          className="block h-5 w-5"
        >
          <path
            fill="currentColor"
            d="M11 5a1 1 0 1 1 2 0v6h6a1 1 0 1 1 0 2h-6v6a1 1 0 1 1-2 0v-6H5a1 1 0 1 1 0-2h6V5Z"
          />
        </svg>
      </Button>
    </div>
  )
}
<script setup lang="ts">
import { Button } from "~~/registry/bases/newt/ui/button"
</script>

<template>
  <!-- `icon` has no visible text, so the label moves to `aria-label`. -->
  <div class="flex flex-wrap items-center gap-3">
    <Button size="xs">Extra small</Button>
    <Button size="sm">Small</Button>
    <Button size="message">Message</Button>
    <Button size="md">Medium</Button>
    <Button size="lg">Large</Button>
    <Button size="icon" variant="secondary" aria-label="Add a reaction">
      <svg
        viewBox="0 0 24 24"
        aria-hidden="true"
        focusable="false"
        class="block h-5 w-5"
      >
        <path
          fill="currentColor"
          d="M11 5a1 1 0 1 1 2 0v6h6a1 1 0 1 1 0 2h-6v6a1 1 0 1 1-2 0v-6H5a1 1 0 1 1 0-2h6V5Z"
        />
      </svg>
    </Button>
  </div>
</template>

Loading

loading puts three pulsing dots over the label and ignores clicks until it is turned off. The label stays in place, invisible, so the button keeps its width and its accessible name. The button keeps focus and sets aria-busy="true". A loading button sets its own click handler, so render it from a Client Component. Click the first button to see it load for two seconds.

In the HTML flavour, add newt-btn--loading and aria-busy="true", wrap the text in newt-btn__label, and put a newt-btn__dots span with three empty spans after it. Include button.js to stop the click.

Root class newt-btn
"use client"

import * as React from "react"

import { Button } from "@/registry/bases/newt/ui/button"

/*
 * The first button loads for two seconds after a click. While it loads it
 * keeps its width and its focus, and a second click does nothing.
 */
export default function ButtonLoadingDemo() {
  const [saving, setSaving] = React.useState(false)

  React.useEffect(() => {
    if (!saving) return
    const timer = window.setTimeout(() => setSaving(false), 2000)
    return () => window.clearTimeout(timer)
  }, [saving])

  return (
    <div className="flex flex-wrap items-center gap-3">
      <Button loading={saving} onClick={() => setSaving(true)}>
        Save changes
      </Button>
      <Button variant="secondary" loading>
        Loading
      </Button>
      <Button variant="danger" size="sm" loading>
        Delete
      </Button>
    </div>
  )
}
<script setup lang="ts">
import { ref, watchEffect } from "vue"
import { Button } from "~~/registry/bases/newt/ui/button"

/*
 * The first button loads for two seconds after a click. While it loads it
 * keeps its width and its focus, and a second click does nothing.
 */
const saving = ref(false)

watchEffect((onCleanup) => {
  if (!saving.value) return
  const timer = window.setTimeout(() => (saving.value = false), 2000)
  onCleanup(() => window.clearTimeout(timer))
})
</script>

<template>
  <div class="flex flex-wrap items-center gap-3">
    <Button :loading="saving" @click="saving = true">Save changes</Button>
    <Button variant="secondary" loading>Loading</Button>
    <Button variant="danger" size="sm" loading>Delete</Button>
  </div>
</template>

Full width

fullWidth (full-width in Vue templates) makes the button fill its container, for a narrow form or a dialog footer on a phone. In the HTML flavour, add newt-btn--full.

Root class newt-btn
import { Button } from "@/registry/bases/newt/ui/button"

/* `fullWidth` fills the container, here a narrow form column. */
export default function ButtonFullWidthDemo() {
  return (
    <div className="flex w-full max-w-[320px] flex-col gap-2">
      <Button fullWidth>Continue</Button>
      <Button variant="secondary" fullWidth>
        Back
      </Button>
    </div>
  )
}
<script setup lang="ts">
import { Button } from "~~/registry/bases/newt/ui/button"
</script>

<template>
  <!-- `full-width` fills the container, here a narrow form column. -->
  <div class="flex w-full max-w-[320px] flex-col gap-2">
    <Button full-width>Continue</Button>
    <Button variant="secondary" full-width>Back</Button>
  </div>
</template>

Leading and trailing content

Put an icon component or a unicode emoji before the label with leading, or after it with trailing. In Vue these are the #leading and #trailing slots. Each sits in its own box, so the button’s gap separates it from the label. Hide an emoji with aria-hidden="true" when the label already says the action. The emoji prop still takes an image URL.

Root class newt-btn
import { Button } from "@/registry/bases/newt/ui/button"

/* Any icon component works the same way; this one is inline to stay offline. */
const PlusIcon = () => (
  <svg
    viewBox="0 0 24 24"
    aria-hidden="true"
    focusable="false"
    className="h-4 w-4"
  >
    <path
      fill="currentColor"
      d="M11 5a1 1 0 1 1 2 0v6h6a1 1 0 1 1 0 2h-6v6a1 1 0 1 1-2 0v-6H5a1 1 0 1 1 0-2h6V5Z"
    />
  </svg>
)

/* A unicode emoji is text: hide it when the label already says the action. */
export default function ButtonSlotsDemo() {
  return (
    <div className="flex flex-wrap items-center gap-3">
      <Button size="message" variant="secondary" leading={<PlusIcon />}>
        Add role
      </Button>
      <Button size="message" leading={<span aria-hidden="true">🎉</span>}>
        Celebrate
      </Button>
      <Button
        size="message"
        variant="success"
        trailing={<span aria-hidden="true">✅</span>}
      >
        Accept
      </Button>
    </div>
  )
}
<script setup lang="ts">
import { Button } from "~~/registry/bases/newt/ui/button"
</script>

<template>
  <!--
    Any icon component works the same way; this one is inline to stay
    offline. A unicode emoji is text: hide it when the label already says the
    action.
  -->
  <div class="flex flex-wrap items-center gap-3">
    <Button size="message" variant="secondary">
      <template #leading>
        <svg
          viewBox="0 0 24 24"
          aria-hidden="true"
          focusable="false"
          class="h-4 w-4"
        >
          <path
            fill="currentColor"
            d="M11 5a1 1 0 1 1 2 0v6h6a1 1 0 1 1 0 2h-6v6a1 1 0 1 1-2 0v-6H5a1 1 0 1 1 0-2h6V5Z"
          />
        </svg>
      </template>
      Add role
    </Button>
    <Button size="message">
      <template #leading><span aria-hidden="true">🎉</span></template>
      Celebrate
    </Button>
    <Button size="message" variant="success">
      Accept
      <template #trailing><span aria-hidden="true">✅</span></template>
    </Button>
  </div>
</template>

Disabled

disabled dims the button and the browser stops it taking clicks or focus. On a link there is nothing native to disable, so the button drops its href and sets aria-disabled instead.

Root class newt-btn
import { Button } from "@/registry/bases/newt/ui/button"

/*
 * A button is disabled by the browser. A link has no `disabled`, so the last
 * one loses its `href` and carries `aria-disabled` instead.
 */
export default function ButtonDisabledDemo() {
  return (
    <div className="flex flex-wrap items-center gap-3">
      <Button disabled>Save changes</Button>
      <Button variant="secondary" disabled>
        Cancel
      </Button>
      <Button variant="danger" disabled>
        Delete server
      </Button>
      <Button variant="link" href="/docs/components/button" disabled>
        Read the guide
      </Button>
    </div>
  )
}
<script setup lang="ts">
import { Button } from "~~/registry/bases/newt/ui/button"
</script>

<template>
  <!--
    A button is disabled by the browser. A link has no `disabled`, so the last
    one loses its `href` and carries `aria-disabled` instead.
  -->
  <div class="flex flex-wrap items-center gap-3">
    <Button disabled>Save changes</Button>
    <Button variant="secondary" disabled>Cancel</Button>
    <Button variant="danger" disabled>Delete server</Button>
    <Button variant="link" href="/docs/components/button" disabled>
      Read the guide
    </Button>
  </div>
</template>

Pass href and any variant renders an <a>, for an action that goes somewhere. launchIcon adds the link-out glyph to a variant that lacks it, and launchIcon={false} removes it from variant="link" when the destination is on the same site.

Root class newt-btn
import { Button } from "@/registry/bases/newt/ui/button"

/*
 * `href` turns any variant into an `<a>`. The link-out glyph is on by default
 * for `variant="link"` only; `launchIcon` adds or removes it.
 */
export default function ButtonLinkDemo() {
  return (
    <div className="flex flex-wrap items-center gap-3">
      <Button href="/docs/components/button">Get started</Button>
      <Button
        variant="secondary"
        href="https://example.com"
        target="_blank"
        rel="noreferrer"
        launchIcon
      >
        Open dashboard
      </Button>
      <Button variant="link" href="https://example.com">
        Terms of service
      </Button>
      <Button variant="link" href="/docs/components/button" launchIcon={false}>
        Skip for now
      </Button>
    </div>
  )
}
<script setup lang="ts">
import { Button } from "~~/registry/bases/newt/ui/button"
</script>

<template>
  <!--
    `href` turns any variant into an `<a>`. The link-out glyph is on by default
    for `variant="link"` only; `launchIcon` adds or removes it.
  -->
  <div class="flex flex-wrap items-center gap-3">
    <Button href="/docs/components/button">Get started</Button>
    <Button
      variant="secondary"
      href="https://example.com"
      target="_blank"
      rel="noreferrer"
      launch-icon
    >
      Open dashboard
    </Button>
    <Button variant="link" href="https://example.com">Terms of service</Button>
    <Button variant="link" href="/docs/components/button" :launch-icon="false">
      Skip for now
    </Button>
  </div>
</template>

API reference

Button

PropType
variant?"primary" | "secondary" | "success" | "danger" | "link"

What the action does. Never picked for its colour.

Type"primary" | "secondary" | "success" | "danger" | "link"
Default"primary"
size?"xs" | "sm" | "md" | "message" | "lg" | "icon"

Height, padding and text size. `xs`, `sm` and `md` are at least 24px, 32px and 40px high. `message` is 32px high, the size of a button in a message. `icon` makes it a circle.

Type"xs" | "sm" | "md" | "message" | "lg" | "icon"
Default"md"
loading?boolean

Show three dots over the label and ignore clicks. The width, the accessible name and focus stay. Sets `aria-busy`.

Typeboolean
Defaultfalse
fullWidth?boolean

Fill the width of the container.

Typeboolean
Defaultfalse
emoji?string

Image URL rendered before the label.

Typestring
leading?React.ReactNode (React) / #leading slot (Vue)

Content before the label, such as an icon component or a unicode emoji.

TypeReact.ReactNode (React) / #leading slot (Vue)
trailing?React.ReactNode (React) / #trailing slot (Vue)

Content after the label, before the link-out glyph.

TypeReact.ReactNode (React) / #trailing slot (Vue)
emojiAlt?string

Accessible name for the emoji. Empty hides it from readers, which is right when the label already says it.

Typestring
Default""
launchIcon?boolean

Link-out glyph after the label.

Typeboolean
Defaultvariant === "link"
href?string | undefined

Render a link (`<a href>`) instead of a button. A disabled link drops its `href` and sets `aria-disabled`.

Typestring | undefined
as?React.ElementType (React) / string | Component (Vue)

The element or component to render, such as a router link, to keep client-side navigation.

TypeReact.ElementType (React) / string | Component (Vue)
Default"button", or "a" with `href`

Every part also accepts the native attributes of the element it renders, and a className that is merged rather than replaced.

Accessibility

It renders a real <button>, so Enter and Space activate it and disabled is honoured by the browser rather than emulated. An icon-only button (size="icon" with no text) has no visible text: give it an aria-label. The focus ring is a 2px --newt-focus-ring outline at 2px offset, and it is not removed on :focus-visible.

A loading button is not disabled. It stays in the tab order and keeps focus, so a keyboard user does not lose their place. It sets aria-busy="true", and its click handler does not run. The label stays in the accessibility tree, so the accessible name does not change. If the result of the action matters, announce it in a live region of your own. The dots stop pulsing when the user asks for reduced motion.

Tokens

Button reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-btn.

  • --newt-space-1
  • --newt-control-md
  • --newt-space-2
  • --newt-space-4
  • --newt-radius-md
  • --newt-font-sans
  • --newt-font-weight-medium
  • --newt-font-size-sm
  • --newt-line-height-tight
  • --newt-duration-instant
  • --newt-ease-in
  • --newt-duration-base
  • --newt-ease-out
  • --newt-focus-ring
  • --newt-text-link
  • --newt-brand
  • --newt-on-accent
  • --newt-brand-hover
  • --newt-brand-active
  • --newt-bg-hover
  • --newt-text-primary
  • --newt-bg-active
  • --newt-danger
  • --newt-danger-hover
  • --newt-success
  • --newt-online
  • --newt-control-xs
  • --newt-radius-sm
  • --newt-font-size-xs
  • --newt-control-sm
  • --newt-space-3
  • --newt-space-6
  • --newt-font-size-md
  • --newt-radius-full
  • --newt-duration-slow

Changelog

  1. featadd app-invite component#56