Buttons allow users to take actions, and make choices, with a single tap.
Buttons communicate actions that users can take. They are typically placed throughout your UI, in places like:
The Button
comes with three variants: text (default), contained, and outlined.
{{ādemoā: āBasicButtons.jsā}}
Text buttons are typically used for less-pronounced actions, including those located: in dialogs, in cards. In cards, text buttons help maintain an emphasis on card content.
{{ādemoā: āTextButtons.jsā}}
Contained buttons are high-emphasis, distinguished by their use of elevation and fill. They contain actions that are primary to your app.
{{ādemoā: āContainedButtons.jsā}}
You can remove the elevation with the disableElevation
prop.
{{ādemoā: āDisableElevation.jsā}}
Outlined buttons are medium-emphasis buttons. They contain actions that are important but arenāt the primary action in an app.
Outlined buttons are also a lower emphasis alternative to contained buttons, or a higher emphasis alternative to text buttons.
{{ādemoā: āOutlinedButtons.jsā}}
All components accept an onClick
handler that is applied to the root DOM element.
<Button
onClick={() => {
alert('clicked');
}}
>
Click me
</Button>
Note that the documentation avoids mentioning native props (there are a lot) in the API section of the components.
{{ādemoā: āColorButtons.jsā}}
In addition to using the default button colors, you can add custom ones, or disable any you donāt need. See the Adding new colors examples for more info.
For larger or smaller buttons, use the size
prop.
{{ādemoā: āButtonSizes.jsā}}
Sometimes you might want to have icons for certain buttons to enhance the UX of the application as we recognize logos more easily than plain text. For example, if you have a delete button you can label it with a dustbin icon.
{{ādemoā: āIconLabelButtons.jsā}}
Icon buttons are commonly found in app bars and toolbars.
Icons are also appropriate for toggle buttons that allow a single choice to be selected or deselected, such as adding or removing a star to an item.
{{ādemoā: āIconButtons.jsā}}
For larger or smaller icon buttons, use the size
prop.
{{ādemoā: āIconButtonSizes.jsā}}
Use color
prop to apply theme color palette to component.
{{ādemoā: āIconButtonColors.jsā}}
To create a file upload button, turn the button into a label using component="label"
and then create a visually-hidden input with type file
.
{{ādemoā: āInputFileUpload.jsā}}
Here are some examples of customizing the component. You can learn more about this in the overrides documentation page.
import * as React from 'react';
import { styled } from '@mui/material/styles';
import Button, { ButtonProps } from '@mui/material/Button';
import Stack from '@mui/material/Stack';
import { purple } from '@mui/material/colors';
const BootstrapButton = styled(Button)({
boxShadow: 'none',
textTransform: 'none',
fontSize: 16,
padding: '6px 12px',
border: '1px solid',
lineHeight: 1.5,
backgroundColor: '#0063cc',
borderColor: '#0063cc',
fontFamily: [
'-apple-system',
'BlinkMacSystemFont',
'"Segoe UI"',
'Roboto',
'"Helvetica Neue"',
'Arial',
'sans-serif',
'"Apple Color Emoji"',
'"Segoe UI Emoji"',
'"Segoe UI Symbol"',
].join(','),
'&:hover': {
backgroundColor: '#0069d9',
borderColor: '#0062cc',
boxShadow: 'none',
},
'&:active': {
boxShadow: 'none',
backgroundColor: '#0062cc',
borderColor: '#005cbf',
},
'&:focus': {
boxShadow: '0 0 0 0.2rem rgba(0,123,255,.5)',
},
});
const ColorButton = styled(Button)<ButtonProps>(({ theme }) => ({
color: theme.palette.getContrastText(purple[500]),
backgroundColor: purple[500],
'&:hover': {
backgroundColor: purple[700],
},
}));
export default function CustomizedButtons() {
return (
<Stack spacing={2} direction="row">
<ColorButton variant="contained">Custom CSS</ColorButton>
<BootstrapButton variant="contained" disableRipple>
Bootstrap
</BootstrapButton>
</Stack>
);
}
šØ If you are looking for inspiration, you can check MUI Treasuryās customization examples.
The Text Buttons, Contained Buttons, Floating Action Buttons and Icon Buttons are built on top of the same component: the ButtonBase
.
You can take advantage of this lower-level component to build custom interactions.
{{ādemoā: āButtonBaseDemo.jsā}}
One frequent use case is to perform navigation on the client only, without an HTTP round-trip to the server.
The ButtonBase
component provides the component
prop to handle this use case.
Here is a more detailed guide.
The ButtonBase component sets pointer-events: none;
on disabled buttons, which prevents the appearance of a disabled cursor.
If you wish to use not-allowed
, you have two options:
<button>
element:.MuiButtonBase-root:disabled {
cursor: not-allowed;
pointer-events: auto;
}
However:
pointer-events: none;
back when you need to display tooltips on disabled elements.<a>
element.<span style={{ cursor: 'not-allowed' }}>
<Button component={Link} disabled>
disabled
</Button>
</span>
This has the advantage of supporting any element, for instance, a link <a>
element.
@mui/lab
offers loading buttons that can show loading state and disable interactions.
{{ādemoā: āLoadingButtons.jsā}}
Toggle the loading switch to see the transition between the different states.
{{ādemoā: āLoadingButtonsTransition.jsā}}