Button
Displays a button or a component that looks like a button.
Preview
Code
Installation
Copy and paste the following code into your project.
Update the import paths to match your project setup.
Usage
There is a small difference between this button and your good old button element.
To get same functionality of onClick
you have to use onPress
prop.
The visual style of the button.
Props
Button Variants
Use the variant
prop to change the appearance of the button.
Default variant is primary
.
Preview
Code
Button Sizes
Use the size
prop to change the size of the button.
Default size is md
.
Preview
Code
Button with Link
Now you dont have to use a
or Link
tag to make a button look like a link.
Preview
Code
Button Disabled
Use the isDisabled
prop to disable the button.
Preview
Code
Button Loading
Well unlike shadcn you dont have to adjust button styles and keep looking for the right spinner.
Just use the isLoading
prop to show a loading spinner.
Preview
Code
Button with Icon
Use the rightSection
or leftSection
prop to add an icon to the button.
type of rightSection
or leftSection
is ReactNode
.
Preview
Code
API Reference
Prop | Type | Default | Description |
---|---|---|---|
variant | "primary" | "secondary" | "accent" | "outline" | "destructive" | "ghost" | "primary" | Basic Style of Button |
size | "sm" | "md" | "lg" | "md" | The size of the button. |
leftSection | React.ReactNode | - | React Element before button text |
rightSection | React.ReactNode | - | React Element before button text |
isLoading | boolean | - | loading state of the button |
isDisabled | boolean | - | |
href | string | - | acts as <Link> element of react-aria |
autoFocus | boolean | — | Whether the element should receive focus on render. |
type | "button" | "submit" | 'reset' | 'button' | The behavior of the button when used in an HTML form. |
children | ReactNode | (values: ButtonRenderProps & {defaultChildren: ReactNode | undefined}) => ReactNode | - | The children of the component. A function may be provided to alter the children based on component state. |
className | string | (values: ButtonRenderProps & { defaultClassName: string | undefined }) => string | — | The CSS className for the element. A function may be provided to compute the class based on component state. |
style | CSSProperties | (values: ButtonRenderProps & { defaultStyle: CSSProperties }) => CSSProperties | — | The inline style for the element. A function may be provided to compute the style based on component state. |
Events
Name | Type | Description |
---|---|---|
onPress | (e: PressEvent) => void | Handler that is called when the press is released over the target. |
onPressStart | (e: PressEvent) => void | Handler that is called when a press interaction starts. |
onPressEnd | (e: PressEvent) => void | Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. |
onPressChange | (isPressed: boolean) => void | Handler that is called when the press state changes. |
onPressUp | (e: PressEvent) => void | Handler 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\>) => void | Handler that is called when the element receives focus. |
onBlur | (e: FocusEvent\<Target\>) => void | Handler that is called when the element loses focus. |
onFocusChange | (isFocused: boolean) => void | Handler that is called when the element's focus status changes. |
onKeyDown | (e: KeyboardEvent) => void | Handler that is called when a key is pressed. |
onKeyUp | (e: KeyboardEvent) => void | Handler that is called when a key is released. |
onHoverStart | (e: HoverEvent) => void | Handler that is called when a hover interaction starts. |
onHoverEnd | (e: HoverEvent) => void | Handler that is called when a hover interaction ends. |
onHoverChange | (isHovering: boolean) => void | Handler that is called when the hover state changes. |
Accessibility
Name | Type | Description |
---|---|---|
id | string | The element's unique identifier. See MDN. |
excludeFromTabOrder | boolean | Whether 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-expanded | boolean | 'true' | 'false' | Indicates whether the element, or another grouping element it controls, is currently expanded or collapsed. |
aria-haspopup | boolean | '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-controls | string | Identifies the element (or elements) whose contents or presence are controlled by the current element. |
aria-pressed | boolean | 'true' | 'false' | 'mixed' | Indicates the current "pressed" state of toggle buttons. |
aria-label | string | Defines a string value that labels the current element. |
aria-labelledby | string | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | Identifies the element (or elements) that describes the object. |
aria-details | string | Identifies the element (or elements) that provide a detailed, extended description for the object. |