Button | Mantine

Button component to render button or link

If the fullWidth prop is set, the Button will take 100% of the parent width:

leftSection and rightSection allow adding icons or any other element to the left and right sides 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).

The justify prop sets the justify-content of the 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 an empty <span /> to the opposite section.

Button supports xs – xl and compact-xs – compact-xl sizes. compact sizes have the same font size as xs – xl but with reduced padding and height.

When the variant prop is set to gradient , you can control the gradient with the gradient prop, which accepts an object with from , to and deg properties. If the gradient prop is not set, Button will use theme.defaultGradient which can be configured on the theme object . The 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, use the Styles API to modify Button styles.

To make a Button disabled, set the 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 the data-disabled prop instead. Note that disabled styles are the same for all variants.

The <a /> element does not support the disabled attribute. To make a Button disabled when it is rendered as a link, set the data-disabled attribute instead and prevent default behavior in the onClick event handler.

To customize disabled styles, it is recommended to use both &:disabled and &[data-disabled] selectors:

The onMouseLeave event is not triggered when a Button is disabled, so if you need to use a Tooltip with a disabled Button , you need to set the data-disabled prop on the Button instead of disabled . Note that it is also required to change the onClick event handler to (event) => event.preventDefault() as the Button is not actually disabled and will still trigger the onClick event.

When the loading prop is set, the Button will be disabled and a Loader with overlay will be rendered in the center of the button. Loader color depends on the Button variant.

You can customize the Loader with the loaderProps prop, which accepts all props that the Loader component has:

Button supports the Styles API ; you can add styles to any inner element of the component with the classNames prop. Follow the 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 :

To add new Button variants, use the data-variant attribute. Usually new variants are added to the theme , this way they are available in all Button components in your application.

You can customize colors for Button and other component variants by adding variantColorResolver to your theme.

Button supports the autoContrast prop and theme.autoContrast . If autoContrast is set either on Button or on the theme, the content color will be adjusted to have sufficient contrast with the value specified in the color prop.

Note that the autoContrast feature works only if you use the color prop to change the background color. autoContrast works only with the filled variant.

Note that you must not wrap child Button components with any additional elements:

Use Button.GroupSection component to render sections that are not buttons inside Button.Group :

Button is a polymorphic component – its default root element is button , but it can be changed to any other element or component with the component prop:

You can also use components in the component prop, for example, Next.js Link :

Polymorphic components with TypeScript

Note that polymorphic component prop types are different from regular components – they do not extend HTML element props of the default element. For example, Button Props does not extend React.ComponentProps '<' div '>' although button is the default element.

If you want to create a wrapper for a polymorphic component that is not polymorphic (does not support the component prop), then your component props interface should extend HTML element props, for example:

If you want your component to remain polymorphic after wrapping, use the polymorphic function described in this guide .

Recommended articles