Command palette

@yasinsensoy/web

Web · Components

Button

A button that starts an action, or a link that looks like a button.

17 exports

Live example

Preview

Code

import { Button } from "@yasinsensoy/web";

<Button effect="ripple" tooltip="Saves the changes" onClick={async () => await kaydet()}>Save</Button>

Events

  1. Events appear here when you interact with the preview.

Press effects (click or Space/Enter)

Corner shape

Style × color

primary
neutral
danger
success
warning

Sizes and IconButton

Tooltip: custom text on a disabled button and an IconButton

Import

import { Button, IconButton, ButtonGroup, buttonVariants } from "@yasinsensoy/web";

Button

componentClient component

A button that starts an action, or a link that looks like a button.

  • The look has two axes: variant sets the style, tone the meaning (color). Every combination like <Button variant="soft" tone="danger"> is valid.
  • If onClick returns a Promise the button shows loading automatically; no need to keep a separate loading state.
  • With href it becomes a link; Next.js Link is given once to UIProvider, no asChild needed.
  • When type is omitted it is "button", so it doesn't submit a form by accident. Use SubmitButton to submit a form.
  • Use IconButton for icon-only buttons.
  • tooltip adds a tooltip; it also works on a disabled button.
  • shape picks the corner shape (rounded, pill, sharp), effect the effect (ripple, pulse, bounce, glow, tilt, beam…). For app-wide style, color, size and effect use UIProvider appearance.defaults.button; for your own styles appearance.variants.button (defineAppearance).
  • Inside a ButtonGroup, variant, tone, size and disabled not given come from the group.
Button(props: ButtonProps): ReactNode

Examples

<Button>Save</Button>
<Button variant="outline">Cancel</Button>
<Button variant="soft" tone="danger" icon="trash">Remove</Button>
<Button variant="gradient" shape="pill" effect="ripple">Get started</Button>
<Button variant="dashed" icon="plus">Add new</Button>

// onClick returning a Promise → loading automatically:
<Button onClick={async () => await save()}>Save</Button>

// Link:
<Button href="/pricing" variant="ghost" iconEnd={<ArrowIcon />}>Pricing</Button>

IconButton

componentClient component

A square icon-only button. With size it has the same height as text buttons; the default style is ghost (the group's style inside a ButtonGroup).

IconButton({ label, tooltip, variant, ...props }: IconButtonProps): JSX.Element

Examples

<IconButton icon="close" label="Close" />
<IconButton icon={<TrashIcon />} label="Delete" tone="danger" variant="soft" />
<IconButton icon="save" label="Save" tooltip={{ content: "Save (⌘S)", side: "bottom" }} />

ButtonGroup

componentClient component

Groups related buttons together as one piece: toolbars, split buttons (Save + ▾), view switchers, alignment and format buttons.

  • Two ways to use: put your own Button/IconButton/Menu elements with children; or pass the items array and the group draws the buttons itself. Selection mode, split buttons and the "More" menu work with items.
  • variant, tone, size, disabled pass to all buttons inside; a button's own value wins.
  • Attached (default): inner corners go away, borders overlap and a thin line separates filled buttons. With attached={false} they are spaced.
  • Selection mode: selection="single" is single-select, "multiple" multi-select; pressed buttons carry aria-pressed and look highlighted.
  • Split button: when an item has menu, that button opens a menu.
  • Keyboard: ←/→ (↑/↓ when vertical) move between buttons, Home/End go to the first and last; the direction flips on right-to-left pages.
  • Mobile: with overflow="menu", buttons that don't fit on narrow screens move into the trailing ⋯ menu (which opens as a bottom sheet); it recomputes when the width changes (rotation, side panel), and since measurements are cached the buttons don't flicker. Touch areas are at least 44 px, as on buttons.
ButtonGroup(props: ButtonGroupProps<V>): JSX.Element

Examples

// With your own buttons:
<ButtonGroup variant="outline" label="Navigation">
  <IconButton icon="chevronLeft" label="Previous" />
  <Button>Today</Button>
  <IconButton icon="chevronRight" label="Next" />
</ButtonGroup>

// Split button:
<ButtonGroup
  items={[
    { label: "Save", onClick: save },
    { label: "More save options", iconOnly: true, icon: "chevronDown",
      menu: [{ label: "Save as draft", onSelect: saveDraft }] },
  ]}
/>

// Single-select (alignment):
<ButtonGroup
  variant="outline"
  label="Alignment"
  selection="single"
  required
  value={align}
  onValueChange={setAlign}
  items={[
    { value: "left", label: "Left", icon: "alignLeft", iconOnly: true },
    { value: "center", label: "Center", icon: "alignCenter", iconOnly: true },
    { value: "right", label: "Right", icon: "alignRight", iconOnly: true },
  ]}
/>

// On narrow screens, those that don't fit go into the ⋯ menu:
<ButtonGroup variant="ghost" overflow="menu" items={tools} />

Produces button classes. Use it when you want to give the button look to another element without using the component.

Mobile behavior:

  • touch-manipulation removes double-tap zoom and the click delay.
  • On touch screens the touch area of small buttons is extended to at least 44×44 px without changing their look.
  • Shrinks slightly on press (effect: "scale"); doesn't shrink with the "reduced motion" preference.
buttonVariants({ variant, tone, size, shape, effect, square, block, appearance, className, }?: ButtonStyleOptions): string

Examples

<summary className={buttonVariants({ variant: "ghost" })}>Details</summary>

Props of a Button rendered as <button>.

Extends: Omit<ComponentProps<"button">, "onClick">

NameTypeDefaultDescription
href?undefinedNot given to render as a button; when given see ButtonAsLinkProps.
onClick?(event: MouseEvent<HTMLButtonElement>) => unknownCalled on click. If it returns a Promise (e.g. an async function) the button automatically shows "loading" until the Promise settles and a second click is blocked.
effect?ButtonEffectEffect or effects: on press scale, ripple, pulse, bounce; on hover lift, border, beam, glow, tilt, shine; or a custom effect added with defineAppearance. Can be combined as an array (["ripple", "glow"]). Defaults to UIProvider appearance.defaults.button.effect, otherwise scale (none for the link style). On keyboard press the effect plays from the button's center. See ButtonEffect.
icon?IconPropIcon at the start of the text: a name ("save") or an element. While loading a spinner takes its place, so the button doesn't shift.
iconEnd?IconPropIcon at the end of the text (e.g. an arrow).
loading?booleanShows the loading state manually: a spinner appears, the button locks and aria-busy is set. Not needed when onClick returns a Promise; the button does this itself.
tooltip?TooltipPropTooltip shown on hover, keyboard focus or long press on touch screens: text, content or settings ({ content, side, delay }). Also visible while the button is disabled; good for explaining why it can't be clicked. See TooltipProp.
variant?ButtonVariant | CustomVariant<"button"> | undefined"solid"Style: one of the built-in styles or a custom style added with defineAppearance.
tone?ButtonTone | undefinedColor. Defaults to "neutral" for outline, ghost, elevated and dashed, and "primary" for the other styles.
size?ButtonSize | undefined"md"Height.
shape?ButtonShape | undefined"rounded"Corner shape.
block?boolean | undefinedTakes the full width of its area.

Props of a Button rendered as a link.

Extends: Omit<ComponentProps<"a">, "href">

NameTypeDefaultDescription
hrefstringWhen given the button becomes a link. Internal URLs are drawn with the link component given to UIProvider (e.g. Next.js Link), external URLs like https: or mailto: with a plain <a>.
disabled?booleanShows the link as disabled; not clickable and not keyboard selectable.
effect?ButtonEffectEffect or effects: on press scale, ripple, pulse, bounce; on hover lift, border, beam, glow, tilt, shine; or a custom effect added with defineAppearance. Can be combined as an array (["ripple", "glow"]). Defaults to UIProvider appearance.defaults.button.effect, otherwise scale (none for the link style). On keyboard press the effect plays from the button's center. See ButtonEffect.
icon?IconPropIcon at the start of the text: a name ("save") or an element. While loading a spinner takes its place, so the button doesn't shift.
iconEnd?IconPropIcon at the end of the text (e.g. an arrow).
loading?booleanShows the loading state manually: a spinner appears, the button locks and aria-busy is set. Not needed when onClick returns a Promise; the button does this itself.
tooltip?TooltipPropTooltip shown on hover, keyboard focus or long press on touch screens: text, content or settings ({ content, side, delay }). Also visible while the button is disabled; good for explaining why it can't be clicked. See TooltipProp.
variant?ButtonVariant | CustomVariant<"button"> | undefined"solid"Style: one of the built-in styles or a custom style added with defineAppearance.
tone?ButtonTone | undefinedColor. Defaults to "neutral" for outline, ghost, elevated and dashed, and "primary" for the other styles.
size?ButtonSize | undefined"md"Height.
shape?ButtonShape | undefined"rounded"Corner shape.
block?boolean | undefinedTakes the full width of its area.

Props of the Button component.

type ButtonProps = ButtonAsButtonProps | ButtonAsLinkProps

Extends: Omit<ComponentProps<"button">, "onClick"> | Omit<ComponentProps<"a">, "href">

NameTypeDefaultDescription
href?undefined | stringNot given to render as a button; when given see ButtonAsLinkProps.
onClick?(event: MouseEvent<HTMLButtonElement>) => unknownCalled on click. If it returns a Promise (e.g. an async function) the button automatically shows "loading" until the Promise settles and a second click is blocked.(ButtonAsButtonProps)
effect?ButtonEffectEffect or effects: on press scale, ripple, pulse, bounce; on hover lift, border, beam, glow, tilt, shine; or a custom effect added with defineAppearance. Can be combined as an array (["ripple", "glow"]). Defaults to UIProvider appearance.defaults.button.effect, otherwise scale (none for the link style). On keyboard press the effect plays from the button's center. See ButtonEffect.
icon?IconPropIcon at the start of the text: a name ("save") or an element. While loading a spinner takes its place, so the button doesn't shift.
iconEnd?IconPropIcon at the end of the text (e.g. an arrow).
loading?booleanShows the loading state manually: a spinner appears, the button locks and aria-busy is set. Not needed when onClick returns a Promise; the button does this itself.
tooltip?TooltipPropTooltip shown on hover, keyboard focus or long press on touch screens: text, content or settings ({ content, side, delay }). Also visible while the button is disabled; good for explaining why it can't be clicked. See TooltipProp.
variant?ButtonVariant | CustomVariant<"button"> | undefined"solid"Style: one of the built-in styles or a custom style added with defineAppearance.
tone?ButtonTone | undefinedColor. Defaults to "neutral" for outline, ghost, elevated and dashed, and "primary" for the other styles.
size?ButtonSize | undefined"md"Height.
shape?ButtonShape | undefined"rounded"Corner shape.
block?boolean | undefinedTakes the full width of its area.
disabled?booleanShows the link as disabled; not clickable and not keyboard selectable.(ButtonAsLinkProps)

Props of the IconButton component.

Extends: Omit<Omit<ComponentProps<"button">, "onClick">, "children" | "iconEnd" | "block"> | Omit<Omit<ComponentProps<"a">, "href">, "children" | "iconEnd" | "block">

NameTypeDefaultDescription
href?undefined | stringNot given to render as a button; when given see ButtonAsLinkProps.
onClick?(event: MouseEvent<HTMLButtonElement>) => unknownCalled on click. If it returns a Promise (e.g. an async function) the button automatically shows "loading" until the Promise settles and a second click is blocked.(ButtonAsButtonProps)
effect?ButtonEffectEffect or effects: on press scale, ripple, pulse, bounce; on hover lift, border, beam, glow, tilt, shine; or a custom effect added with defineAppearance. Can be combined as an array (["ripple", "glow"]). Defaults to UIProvider appearance.defaults.button.effect, otherwise scale (none for the link style). On keyboard press the effect plays from the button's center. See ButtonEffect.
icon?IconPropIcon at the start of the text: a name ("save") or an element. While loading a spinner takes its place, so the button doesn't shift.
loading?booleanShows the loading state manually: a spinner appears, the button locks and aria-busy is set. Not needed when onClick returns a Promise; the button does this itself.
tooltip?TooltipPropTooltip shown on hover, keyboard focus or long press on touch screens: text, content or settings ({ content, side, delay }). Also visible while the button is disabled; good for explaining why it can't be clicked. See TooltipProp.
variant?ButtonVariant | CustomVariant<"button"> | undefined"solid"Style: one of the built-in styles or a custom style added with defineAppearance.
tone?ButtonTone | undefinedColor. Defaults to "neutral" for outline, ghost, elevated and dashed, and "primary" for the other styles.
size?ButtonSize | undefined"md"Height.
shape?ButtonShape | undefined"rounded"Corner shape.
disabled?booleanShows the link as disabled; not clickable and not keyboard selectable.(ButtonAsLinkProps)
labelstringName of the button. Screen readers read it; it shows as a Tooltip * on hover, keyboard focus or long press on touch screens. Required, because * an icon alone isn't a name.

A button in the ButtonGroup items array.

NameTypeDescription
value?VId of the item. In selection mode (selection) this is the selected value; also used as the React key. Required in selection mode.
labelReactNodeButton text. With iconOnly it must be text; it becomes the screen reader name and tooltip.
icon?IconPropIcon at the start of the text: a name or an element.
iconEnd?IconPropIcon at the end of the text.
iconOnly?booleanShows only the icon (square button, IconButton); label becomes the screen reader name and tooltip.
tone?ButtonToneColor of this button; defaults to the group's color.
disabled?booleanNot clickable. Locks this item regardless of the group's disabled value.
loading?booleanLoading state. Automatic when onClick returns a Promise.
tooltip?TooltipPropTooltip. See TooltipProp.
href?stringWhen given the button becomes a link (same as Button href).
onClick?(event: MouseEvent<HTMLElement>) => unknownCalled on click. If it returns a Promise the button shows loading automatically. In selection mode it is called after the selection changes. If the item moved into the "More" menu, it is also called when chosen there.
menu?readonly MenuEntry[]When given the button opens a menu (like the arrow part of a split button). See MenuEntry.
className?stringClasses added to the button.

What happens to buttons that don't fit in the group.

  • "none": nothing; buttons stay on one line.
  • "wrap": wrap to the next line. Only meaningful with attached={false}.
  • "menu": those that don't fit move into a trailing "More actions" (⋯) menu. The group takes the full width of its area. Works only with items and in a horizontal group.
type ButtonGroupOverflow = "none" | "wrap" | "menu"

Props of the ButtonGroup component: an action group, a single-select or a multi-select group.

type ButtonGroupProps = ButtonGroupActionProps | ButtonGroupSingleProps<V> | ButtonGroupMultipleProps<V>

Extends: Omit<ComponentProps<"div">, "children" | "defaultValue" | "onChange"> | Omit<Omit<ComponentProps<"div">, "children" | "defaultValue" | "onChange">, "items">

NameTypeDefaultDescription
selection?undefined | "single" | "multiple"Selection mode off.
value?undefined | V | null | V[]Only in selection mode.
defaultValue?undefined | V | null | V[]Only in selection mode.
onValueChange?undefined | (value: V | null) => void | (value: V[]) => voidOnly in selection mode.
required?undefined | boolean | booleanOnly in selection mode.
variant?ButtonVariantStyle of the buttons inside (a button's own variant wins).
tone?ButtonToneColor of the buttons inside.
size?ButtonSizeHeight of the buttons inside.
disabled?booleanDisables all buttons.
orientation?"horizontal" | "vertical""horizontal"Layout direction.
attached?booleantrueAre the buttons attached? When true the inner corners go away and borders merge; when false there is space between buttons.
block?booleanTakes the full width of its area; buttons share the space equally.
label?stringScreen reader name of the group (e.g. "Text alignment").
overflow?ButtonGroupOverflow"none"What happens to buttons that don't fit. See ButtonGroupOverflow.
items?readonly ButtonGroupItem[] | readonly ButtonGroupItem<V>[] | readonly ButtonGroupItem<V>[]Buttons as an array. Selection mode, split buttons (menu) and overflow="menu" work this way. When omitted, children is rendered.
children?ReactNodeYour own buttons: Button, IconButton, Menu with a button trigger. variant, tone, size, disabled not given come from the group.

Button style: how "heavy" it looks.

  • solid: filled background; the main action on the page.
  • soft: light tinted background; a secondary but colored action.
  • outline: bordered; a secondary action.
  • ghost: no background; toolbars and lists.
  • link: underlined text like a link.
  • gradient: a slightly bright gradient from the tone's color; an eye-catching filled button for showcase and campaign actions.
  • elevated: a shadowed card button raised from the surface; rises a bit on hover.
  • dashed: dashed border; placeholder actions like "add" or "create new".
type ButtonVariant = "solid" | "soft" | "outline" | "ghost" | "link" | "gradient" | "elevated" | "dashed"

Corner shape of the button.

  • rounded: the theme's corner radius (--ui-radius).
  • pill: a fully rounded pill; a circle for icon buttons.
  • sharp: no rounding, sharp corners.
type ButtonShape = "rounded" | "pill" | "sharp"

Button effect or effects. Given to the Button effect prop or app-wide with UIProvider appearance.defaults.button.effect. The default is scale (none for the link style).

All built-in effects work on buttons and can be combined (["ripple", "glow"]): on press scale, ripple, pulse, bounce; on hover lift, border, beam, glow, tilt, shine. See BuiltinEffect. Effects don't play with the "reduced motion" preference. Their colors come from the button's own color; compatible with every variant and tone.

type ButtonEffect = EffectProp

Button color: the meaning of the action. See Tone.

type ButtonTone = Tone

Button height: sm 32 px, md 40 px, lg 48 px.

type ButtonSize = Size

Options of the buttonVariants function.

NameTypeDefaultDescription
variant?ButtonVariant | CustomVariant<"button"> | undefined"solid"Style: one of the built-in styles or a custom style added with defineAppearance.
tone?ButtonTone | undefinedColor. Defaults to "neutral" for outline, ghost, elevated and dashed, and "primary" for the other styles.
size?ButtonSize | undefined"md"Height.
shape?ButtonShape | undefined"rounded"Corner shape.
effect?ButtonEffect | undefined"scale"Effect. The classes only carry the scale effect (CSS); Button draws the other effects itself. The default is none for the link style.
appearance?AppearanceConfig | undefinedAppearance settings from which custom styles are read. Defaults to the app-wide registry (getAppearance).
square?boolean | undefinedSquare button (icon only). IconButton sets this itself.
block?boolean | undefinedTakes the full width of its area.
className?ClassValueClasses to add; these win on conflicts.