Button
Button component to render button or link
Import
Source
Docs
Package
Usage
Full width
If fullWidth prop is set Button will take 100% of parent width:
Left and right sections
leftSection and rightSection allow adding icons or any other element to the left and right side of the button.
When a section is added, padding on the corresponding side is reduced.
Note that leftSection and rightSection are flipped in RTL mode
(leftSection is displayed on the right and rightSection is displayed on the left).
Sections position
justify prop sets justify-content of inner element. You can use it to change the alignment of
left and right sections. For example, to spread them across the button set justify="space-between".
If you need to align just one section to the side of the button set justify to space-between
and add empty <span /> to the opposite section.
Compact size
Button supports xs – xl and compact-xs – compact-xl sizes. compact sizes have
the same font-size as xs – xl but reduced padding and height.
Gradient variant
When variant prop is set to gradient, you can control gradient with gradient prop, it accepts an object with from, to and deg properties. If thegradient prop is not set, Button will use theme.defaultGradient which can be configured on the theme object. gradient prop is ignored when variant is not gradient.
Note that variant="gradient" supports only linear gradients with two colors. If you need a more complex gradient, then use Styles API to modify Button styles.
Disabled state
To make Button disabled, set disabled prop, this will prevent any interactions with the button
and add disabled styles. If you want the button to just look disabled but still be interactive,
set data-disabled prop instead. Note that disabled styles are the same for all variants.
Disabled state when Button is link
<a /> element does not support disabled attribute. To make Button disabled when it is
rendered as a link, set data-disabled attribute instead and prevent default behavior in
onClick event handler.
Customize disabled styles
To customize disabled styles, it is recommended to use both &:disabled and &[data-disabled]
selectors:
&:disabledis used to style the button whendisabledprop is set and also when the button is disabled by the parent component (for example, whendisabledprop is set on a<fieldset />element which containsButton).&[data-disabled]is used to style the button when it is not actually disabled but should look like it is (for example,data-disabledshould be used if you need to use Tooltip with disabledButtonor whenButtonis used as a link)
Disabled button with Tooltip
onMouseLeave event is not triggered when Button is disabled, so if you need to use
Tooltip with disabled Button you need to set data-disabled prop on Button
instead of disabled. Note that it is also required to change onClick event handler to
(event) => event.preventDefault() as Button is not actually disabled and will still trigger
onClick event.
Loading state
When loading prop is set, Button will be disabled and Loader with overlay will be rendered
in the center of the button. Loader color depends on Button variant.
Loader props
You can customize Loader with loaderProps prop, it accepts all props that
Loader component has:
Styles API
Button supports Styles API, you can add styles to any inner element of the component withclassNames prop. Follow Styles API documentation to learn more.
Component Styles API
Hover over selectors to highlight corresponding elements
Example of customizing Button with Styles API and data-* attributes:
Custom variants
To add new Button variants, use data-variant attribute.
Usually new variants are added on theme, this way they are
available in all Button components in your application.
Customize variants colors
You can customize colors for Button and other components variants by adding
variantColorResolver to your theme.
autoContrast
Button supports autoContrast prop and theme.autoContrast. If autoContrast is set either on Button or on theme, content color will be adjusted to have sufficient contrast with the value specified in color prop.
Note that autoContrast feature works only if you use color prop to change background color. autoContrast works only with filled variant.
Button.Group
Note that you must not wrap child Button components with any additional elements:
Button.GroupSection
Use Button.GroupSection component to render sections that are not buttons inside Button.Group:
Polymorphic component
Button is a polymorphic component – its default root element is button, but it can be changed to any other element or component with component prop:
You can also use components in component prop, for example, Next.js Link:
Polymorphic components with TypeScript
Note that polymorphic components props types are different from regular components – they do not extend HTML element props of the default element. For example,
ButtonPropsdoes not extendReact.ComponentPropsWithoutRef'<'div'>'althoughbuttonis the default element.If you want to create a wrapper for a polymorphic component that is not polymorphic (does not support
componentprop), then your component props interface should extend HTML element props, for example:If you want your component to remain polymorphic after wrapping, use
createPolymorphicComponentfunction described in this guide.