Button

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

Features

  • Mouse, touch, keyboard, and focus interactions are normalized to ensure consistency across browsers and devices.
  • Uses a native <button> element under the hood, with support for the Space and Enter keys.
  • Hover, press, and keyboard focus states are provided for easy styling. These states only apply when interacting with an appropriate input device, unlike CSS pseudo classes.

Parts and their API

Button

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

Props

PropTypeDefaultDescription
formstring-The <form> element to associate the button with. The value of this attribute must be the id of a <form> in the same document.
formActionstring-The URL that processes the information submitted by the button. Overrides the action attribute of the button's form owner.
formEncTypestring-Indicates how to encode the form data that is submitted.
formMethodstring-Indicates the HTTP method used to submit the form.
formNoValidateboolean-Indicates that the form is not to be validated when it is submitted.
formTargetstring-Overrides the target attribute of the button's form owner.
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.
isDisabledboolean-Whether the button is disabled.
autoFocusboolean-Whether the element should receive focus on render.
type'button' | 'submit' | 'reset''button'The behavior of the button when used in an HTML form.
childrenReactNode | (values: ButtonRenderProps) => ReactNode-The children of the component. A function may be provided to alter the children based on component state.
classNamestring | (values: ButtonRenderProps) => string-The CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties | (values: ButtonRenderProps) => CSSProperties-The inline style for the element. A function may be provided to compute the style based on component state.

State

Render PropCSS SelectorDescription
isHovered[data-hovered]Whether the button is currently hovered with a mouse.
isPressed[data-pressed]Whether the button is currently in a pressed state.
isFocused[data-focused]Whether the button is focused, either via a mouse or keyboard.
isFocusVisible[data-focus-visible]Whether the button is keyboard focused.
isDisabled[data-disabled]Whether the button is disabled.

Events

NameTypeDescription
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.

Layout

NameTypeDescription
slotstring | nullA slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Accessibility

NameTypeDescription
idstringThe element's unique identifier.
excludeFromTabOrderbooleanWhether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available.
aria-expandedboolean | 'true' | 'false'Indicates whether the element, or another grouping element it controls, is currently expanded or collapsed.
aria-haspopupboolean | 'menu' | 'listbox' | 'tree' | 'grid' | 'dialog' | 'true' | 'false'Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element.
aria-controlsstringIdentifies the element (or elements) whose contents or presence are controlled by the current element.
aria-pressedboolean | 'true' | 'false' | 'mixed'Indicates the current "pressed" state of toggle buttons.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.

Usage

ButtonExample.tsx
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
<div className="flex gap-6">
<Button onPress={() => alert('Hello world!')}>Click me</Button>
<Button onPress={() => alert('Hello world!')} variant="secondary">
Click me
</Button>
<Button onPress={() => alert('Hello world!')} variant="danger">
Click me
</Button>
<Button onPress={() => alert('Hello world!')} variant="ghost">
Click me
</Button>
</div>
<div className="mt-8 flex gap-6">
<Button isDisabled onPress={() => alert('Hello world!')}>
Click me
</Button>
<Button isDisabled onPress={() => alert('Hello world!')} variant="secondary">
Click me
</Button>
<Button isDisabled onPress={() => alert('Hello world!')} variant="danger">
Click me
</Button>
<Button isDisabled onPress={() => alert('Hello world!')} variant="ghost">
Click me
</Button>
</div>

Here's the <Button /> component in action.

Source

Styling

tailwind.css
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
/* ### Buttons ### */
@layer components {
/* Button */
/* For cross-browser consistency, React Aria's button give us the button's state using data attributes. We'll be using that instead of hover:, focus-visible:, disabled: and other pseudo class selectors. */
.button {
@apply inline-flex items-center justify-center whitespace-nowrap rounded-md text-size-sm font-semibold ring-offset-background transition-colors;
/* States */
&[data-focused] {
@apply outline-none;
}
&[data-focus-visible] {
@apply outline-none ring-2 ring-ring ring-offset-2;
}
&[data-disabled] {
@apply pointer-events-none opacity-50;
}
/* Variants */
&.primary {
@apply border bg-brand text-muted-50;
&[data-hovered] {
@apply bg-brand/90;
}
&[data-pressed] {
@apply bg-brand-bg-hover;
}
}
&.secondary {
@apply border bg-muted-50 text-muted-800;
&[data-hovered] {
@apply bg-muted-100;
}
&[data-pressed] {
@apply bg-muted-200;
}
}
&.danger {
@apply bg-danger-bg text-danger-foreground;
&[data-hovered] {
@apply bg-danger-foreground/10;
}
&[data-pressed] {
@apply bg-danger-foreground/20;
}
}
&.ghost {
&[data-hovered] {
@apply bg-muted-100;
}
&[data-pressed] {
@apply bg-muted-300;
}
}
/* Sizes */
&.lg {
@apply h-14 px-10 text-size-md;
}
&.md {
@apply h-10 px-4 py-2;
}
&.sm {
@apply h-9 px-3;
}
}
}