Button
Buttons let users take actions and make choices with a single tap.
Introduction
The Button component replaces the native HTML <button>
element, and offers expanded options for styling and accessibility.
Component
import { Button } from '@mui/base/Button';
The Button behaves similar to the native HTML <button>
, so it wraps around the text that will be displayed on its surface.
The following demo shows how to create and style two basic buttons.
Notice that the second button cannot be clicked due to the disabled
prop:
Anatomy
The Button component is composed of a root <button>
slot with no interior slots:
<button class="BaseButton-root">
<!-- button text goes here -->
</button>
Custom structure
Use the slots.root
prop to override the root slot with a custom element:
<Button slots={{ root: 'div' }} />
If you provide a non-interactive element such as a <span>
, the Button component will automatically add the necessary accessibility attributes.
Compare the attributes on the <span>
in this demo with the Button from the previous demo—try inspecting them both with your browser's dev tools:
Usage with TypeScript
In TypeScript, you can specify the custom component type used in the slots.root
as a generic parameter of the unstyled component. This way, you can safely provide the custom root's props directly on the component:
<Button<typeof CustomComponent> slots={{ root: CustomComponent }} customProp />
The same applies for props specific to custom primitive elements:
<Button<'img'> slots={{ root: 'img' }} src="button.png" />
Hook
import { useButton } from '@mui/base/useButton';
The useButton
hook lets you apply the functionality of a Button to a fully custom component.
It returns props to be placed on the custom component, along with fields representing the component's internal state.
Hooks do not support slot props, but they do support customization props.
The following demo shows how to build the same buttons as those found in the Component section above, but with the useButton
hook:
If you use a ref to store a reference to the button, pass it to the useButton
's ref
parameter, as shown in the demo above.
It will get merged with a ref used internally in the hook.
Customization
Custom elements
The Button accepts a wide range of custom elements beyond HTML elements. You can even use SVGs, as shown in the demo below:
Using with links
The following demo illustrates how to use the Button as a link, whether using the Base UI Button itself for the href
, or with the Next.js Link component:
Focus on disabled buttons
Similarly to the native HTML <button>
element, the Button component can't receive focus when it's disabled.
This may reduce its accessibility, as screen readers won't be able to announce the existence and state of the button.
The focusableWhenDisabled
prop lets you change this behavior.
When this prop is set, the underlying Button does not set the disabled
prop.
Instead, aria-disabled
is used, which makes the Button focusable.
This should be used whenever the disabled Button needs to be read by screen readers.
Base UI uses this prop internally in menu items, making it possible to use the keyboard to navigate to disabled items (in compliance with ARIA guidelines).
The following demo shows how the focusableWhenDisabled
prop works—use the Tab key to navigate within this document to see that only the second Button accepts the focus:
The focusableWhenDisabled
prop works the same when the root slot is customized, except that the aria-disabled
attribute is used no regardless of the prop's state.
The ability to receive focus is controlled internally by the tabindex
attribute.