Skip to content
WebJs
Menu

Displays a button or a component that looks like a button.

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';
import { ArrowUpIcon } from '#lib/ui/lucide.ts';

export default function ButtonDemo() {
  return html`
    <div class="flex flex-wrap items-center gap-2 md:flex-row">
      <button type="button" data-slot="button" class=${buttonClass({ variant: 'outline' })}>Button</button>
      <button type="button" data-slot="button" aria-label="Submit" class=${buttonClass({ variant: 'outline', size: 'icon' })}>${ArrowUpIcon()}</button>
    </div>
  `;
}
View Code

Installation

pnpm dlx @webjsdev/ui add button

Usage

import { buttonClass } from '#components/ui/button.ts';
<button class=${buttonClass({ variant: 'outline' })}>Button</button>

Cursor

Tailwind v4 switched from cursor: pointer to cursor: default for the button component.

If you want to keep the cursor: pointer behavior, add the following code to your CSS file:

styles/globals.css
@layer base {
  button:not(:disabled),
  [role="button"]:not(:disabled) {
    cursor: pointer;
  }
}

Size

Use the size option to change the size of the button.

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';
import { ArrowUpRightIcon } from '#lib/ui/lucide.ts';

export default function ButtonSize() {
  return html`
    <div class="flex flex-col items-start gap-8 sm:flex-row">
      <div class="flex items-start gap-2">
        <button type="button" data-slot="button" class=${buttonClass({ variant: 'outline', size: 'xs' })}>Extra Small</button>
        <button type="button" data-slot="button" aria-label="Submit" class=${buttonClass({ variant: 'outline', size: 'icon-xs' })}>${ArrowUpRightIcon()}</button>
      </div>
      <div class="flex items-start gap-2">
        <button type="button" data-slot="button" class=${buttonClass({ variant: 'outline', size: 'sm' })}>Small</button>
        <button type="button" data-slot="button" aria-label="Submit" class=${buttonClass({ variant: 'outline', size: 'icon-sm' })}>${ArrowUpRightIcon()}</button>
      </div>
      <div class="flex items-start gap-2">
        <button type="button" data-slot="button" class=${buttonClass({ variant: 'outline' })}>Default</button>
        <button type="button" data-slot="button" aria-label="Submit" class=${buttonClass({ variant: 'outline', size: 'icon' })}>${ArrowUpRightIcon()}</button>
      </div>
      <div class="flex items-start gap-2">
        <button type="button" data-slot="button" class=${buttonClass({ variant: 'outline', size: 'lg' })}>Large</button>
        <button type="button" data-slot="button" aria-label="Submit" class=${buttonClass({ variant: 'outline', size: 'icon-lg' })}>${ArrowUpRightIcon()}</button>
      </div>
    </div>
  `;
}
View Code

Default

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';

export default function ButtonDefault() {
  return html`
    <button type="button" data-slot="button" class=${buttonClass()}>Button</button>
  `;
}
View Code

Outline

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';

export default function ButtonOutline() {
  return html`
    <button type="button" data-slot="button" class=${buttonClass({ variant: 'outline' })}>Outline</button>
  `;
}
View Code

Secondary

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';

export default function ButtonSecondary() {
  return html`
    <button type="button" data-slot="button" class=${buttonClass({ variant: 'secondary' })}>Secondary</button>
  `;
}
View Code

Ghost

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';

export default function ButtonGhost() {
  return html`
    <button type="button" data-slot="button" class=${buttonClass({ variant: 'ghost' })}>Ghost</button>
  `;
}
View Code

Destructive

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';

export default function ButtonDestructive() {
  return html`
    <button type="button" data-slot="button" class=${buttonClass({ variant: 'destructive' })}>Destructive</button>
  `;
}
View Code
import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';

export default function ButtonLink() {
  return html`
    <button type="button" data-slot="button" class=${buttonClass({ variant: 'link' })}>Link</button>
  `;
}
View Code

Icon

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';
import { CircleFadingArrowUpIcon } from '#lib/ui/lucide.ts';

export default function ButtonIcon() {
  return html`
    <button type="button" data-slot="button" aria-label="Upgrade" class=${buttonClass({ variant: 'outline', size: 'icon' })}>${CircleFadingArrowUpIcon()}</button>
  `;
}
View Code

With Icon

Remember to add the data-icon="inline-start" or data-icon="inline-end" attribute to the icon for the correct spacing.

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';
import { GitBranchIcon } from '#lib/ui/lucide.ts';

export default function ButtonWithIcon() {
  return html`
    <button type="button" data-slot="button" class=${buttonClass({ variant: 'outline', size: 'sm' })}>${GitBranchIcon()} New Branch</button>
  `;
}
View Code

Rounded

Use the rounded-full class to make the button rounded.

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';
import { cn } from '#lib/utils/cn.ts';
import { ArrowUpIcon } from '#lib/ui/lucide.ts';

export default function ButtonRounded() {
  return html`
    <div class="flex flex-col gap-8">
      <button type="button" data-slot="button" aria-label="Scroll up" class=${cn(buttonClass({ variant: 'outline', size: 'icon' }), 'rounded-full')}>${ArrowUpIcon()}</button>
    </div>
  `;
}
View Code

Spinner

Render a spinner() inside the button to show a loading state, and mark the button disabled with aria-busy="true" while it waits.

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';
import { spinner } from '#components/ui/spinner.ts';

export default function ButtonSpinner() {
  return html`
    <div class="flex gap-2">
      <button type="button" data-slot="button" disabled aria-busy="true" class=${buttonClass({ variant: 'outline' })}>
        ${spinner({ decorative: true })}
        Generating
      </button>
      <button type="button" data-slot="button" disabled aria-busy="true" class=${buttonClass({ variant: 'secondary' })}>
        Downloading
        ${spinner({ decorative: true })}
      </button>
    </div>
  `;
}
View Code

Button Group

To create a button group, use the buttonGroupClass() helper. See the Button Group documentation for more details.

A class helper styles any element, so a link that looks like a button is a link with the button's classes.

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';

// shadcn's asChild renders the child element with the button's classes.
// A class helper does the same with no wrapper: put it on the link.
export default function ButtonAsChild() {
  return html`<a href="/login" data-slot="button" class=${buttonClass()}>Login</a>`;
}
View Code

RTL

To enable RTL support, see the RTL configuration guide.

API Reference

buttonClass

buttonClass() returns the classes for a button. Put them on a native <button>, or on any element that should look like one. buttonVariants() is an alias of buttonClass() that also merges a className.

OptionTypeDefault
variant"default" | "outline" | "ghost" | "destructive" | "secondary" | "link""default"
size"default" | "xs" | "sm" | "lg" | "icon" | "icon-xs" | "icon-sm" | "icon-lg""default"