1. Components
  2. Buttons
  3. Button

Button

A button allows a user to perform an action, with mouse, touch, and keyboard interactions.

<Button>button</Button>

Installation

npx dotui-cli@latest add button

Usage

Button allow users to initiate an action or command with mouse, touch or keyboard interaction.
The button's label indicates the purpose of the action to the user. You may also include an icon for additional context.

Options

Variant

Use the variant prop to control the visual style of the button.
The default variant is "default".

<Button variant="default">default</Button>
<Button variant="primary">primary</Button>
<Button variant="outline">outline</Button>
<Button variant="quiet">quiet</Button>
<Button variant="success">success</Button>
<Button variant="warning">warning</Button>
<Button variant="danger">danger</Button>
<Button variant="accent">accent</Button>

Size

Use the size prop to control the size of the button.
The default size is "md".

<Button size="sm">Button</Button>
<Button size="md">Button</Button>
<Button size="lg">Button</Button>

Shape

Use the shape prop to control the shape of the button. The default variant is "rectangle".
Icon-only buttons should include an aria-label.

<Button shape="square"><UplaodIcon /></UplaodIcon>
<Button shape="circular"><UplaodIcon /></Button>

Prefix and suffix

To add additional context for a button label, such as a search icon next to the label for a search field submit, use the prefix and suffix props. Leading visuals always appear locked to the button label, even if the button is full width.

<Button prefix={<UplaodIcon />}>Upload</Button>
<Button suffix={<UplaodIcon />}>Upload</Button>

Pending

Use the isPending prop to control the loading state of the button, if you need to wait for a button's action to be completed.

<Button isPending>Button</Button>

Disabled

Use the isDisabled prop to disable the button.

<Button isDisabled>Button</Button>

To use a button as a link, use the href prop the button will automatically render a link that visually looks like a button.

API Reference

PropTypeDefaultDescription
variant"default" | "primary" | "quiet" | "outline" | "accent" | "danger" | "success" | "warning""default"The visual style of the button.
size"sm" | "md" | "lg""md"The size of the button.
shape"rectangle" | "square" | "circle""rectangle"The visual shape of the button.
prefixReact.ReactNode-A visual to display before the button text.
suffixReact.ReactNode-A visual to display after the button text.
isLoadingboolean-Whether the button is in a loading state.
isDisabledboolean-Whether the button is disabled.
namestring-Submitted as a pair with the button's value as part of the form data.
valuestring-The value associated with the button's name when it's submitted with the form data.
autoFocusboolean-Whether the element should receive focus on render.
type'button' | 'submit' | 'reset'-The behavior of the button when used in an HTML form.
hrefstring-A URL to link to.
relHTMLAttributeAnchorTarget-The relationship between the linked resource and the current page.
targetHTMLAttributeAnchorTarget-The target window for the link.
relstring-The relationship between the linked resource and the current page.
downloadboolean | string-Causes the browser to download the linked URL. A string may be provided to suggest a file name.
pingstring-A space-separated list of URLs to ping when the link is followed.
referrerPolicyHTMLAttributeReferrerPolicy-How much of the referrer to send when following the link.
routerOptionsRouterOptions-Options for the configured client side router.
childrenReactNode | (values: ButtonRenderProps & {defaultChildren: ReactNode | undefined}) => ReactNode-The children of the component. A function may be provided to alter the children based on component state.
classNamestring-The CSS className for the element.
styleCSSProperties | (values: ButtonRenderProps & {defaultStyle: CSSProperties}) => CSSProperties-The inline style for the element. A function may be provided to compute the style based on component state.
EventTypeDescription
onPress(e: PressEvent) => voidHandler that is called when the press is released over the target.
onPressStart(e: PressEvent) => voidHandler that is called when a press interaction starts.
onPressEnd(e: PressEvent) => voidHandler that is called when a press interaction ends, either over the target or when the pointer leaves the target.
onPressChange(isPressed: boolean) => voidHandler that is called when the press state changes.
onPressUp(e: PressEvent) => voidHandler that is called when a press is released over the target, regardless of whether it started on the target or not.
onFocus(e: FocusEvent<Target>) => voidHandler that is called when the element receives focus.
onBlur(e: FocusEvent<Target>) => voidHandler that is called when the element loses focus.
onFocusChange(isFocused: boolean) => voidHandler that is called when the element's focus status changes.
onKeyDown(e: KeyboardEvent) => voidHandler that is called when a key is pressed.
onKeyUp(e: KeyboardEvent) => voidHandler that is called when a key is released.
onHoverStart(e: HoverEvent) => voidHandler that is called when a hover interaction starts.
onHoverEnd(e: HoverEvent) => voidHandler that is called when a hover interaction ends.
onHoverChange(isHovering: boolean) => voidHandler that is called when the hover state changes.
Data attributeDescription
[data-hovered]Whether the button is currently hovered with a mouse.
[data-pressed]Whether the button is currently in a pressed state.
[data-focused]Whether the button is focused, either via a mouse or keyboard.
[data-focus-visible]Whether the button is keyboard focused.
[data-disabled]Whether the button is disabled.

Accessibility

Keyboard interactions

KeyDescription
SpaceActivates the button.
EnterActivates the button.

Last updated on 10/11/2024

dotUI

Bringing singularity to the web.

Built by mehdibha. The source code is available on GitHub.