Skip to content

Snackbar API

API reference docs for the React Snackbar component. Learn about the props, CSS, and other APIs of this exported module.

Component demos

Import

import Snackbar from '@mui/joy/Snackbar';
// or
import { Snackbar } from '@mui/joy';
Learn about the difference by reading this guide on minimizing bundle size.

Props

openRequired

If true, the component is shown.

Type:bool


anchorOrigin

The anchor of the Snackbar. On smaller screens, the component grows to occupy all the available width, the horizontal alignment is ignored.

Type:{ horizontal: 'center'
| 'left'
| 'right', vertical: 'bottom'
| 'top' }

Default:{ vertical: 'bottom', horizontal: 'right' }


animationDuration

The duration of the animation in milliseconds. This value is used to control the length of time it takes for an animation to complete one cycle. It is also utilized for delaying the unmount of the component. Provide this value if you have your own animation so that we can precisely time the component's unmount to match your custom animation.

Type:number

Default:300


autoHideDuration

The number of milliseconds to wait before automatically calling the onClose function. onClose should then set the state of the open prop to hide the Snackbar. This behavior is disabled by default with the null value.

Type:number

Default:null


color

The color of the component. It supports those theme colors that make sense for this component.

To learn how to add your own colors, check out Themed components—Extend colors.

Type:'danger'
| 'neutral'
| 'primary'
| 'success'
| 'warning'

Default:'neutral'


component

The component used for the root node. Either a string to use a HTML element or a component.

Type:elementType


disableWindowBlurListener

If true, the autoHideDuration timer will expire even if the window is not focused.

Type:bool

Default:false


endDecorator

Element placed after the children.

Type:node


invertedColors

If true, the children with an implicit color prop invert their colors to match the component's variant and color.

Type:bool

Default:false


key

When displaying multiple consecutive snackbars using a single parent-rendered <Snackbar/>, add the key prop to ensure independent treatment of each message. For instance, use <Snackbar key={message} />. Otherwise, messages might update in place, and features like autoHideDuration could be affected.

Type:any


onClose

Callback fired when the component requests to be closed. Typically onClose is used to set state in the parent component, which is used to control the Snackbar open prop. The reason parameter can optionally be used to control the response to onClose, for example ignoring clickaway.

Type:func

Signature:
function(event: React.SyntheticEvent | Event, reason: string) => void
  • event The event source of the callback.
  • reason Can be: "timeout" (autoHideDuration expired), "clickaway", or "escapeKeyDown".

onUnmount

A callback fired when the component is about to be unmounted.

Type:func


resumeHideDuration

The number of milliseconds to wait before dismissing after user interaction. If autoHideDuration prop isn't specified, it does nothing. If autoHideDuration prop is specified but resumeHideDuration isn't, we default to autoHideDuration / 2 ms.

Type:number


size

The size of the component.

To learn how to add custom sizes to the component, check out Themed components—Extend sizes.

Type:'sm'
| 'md'
| 'lg'

Default:'md'


slotProps

The props used for each slot inside.

Type:{ clickAway?: func
| { children: element, disableReactTree?: bool, mouseEvent?: 'onClick'
| 'onMouseDown'
| 'onMouseUp'
| 'onPointerDown'
| 'onPointerUp'
| false, onClickAway: func, touchEvent?: 'onTouchEnd'
| 'onTouchStart'
| false }, endDecorator?: func
| object, root?: func
| object, startDecorator?: func
| object }

Default:{}


slots

The components used for each slot inside.

See Slots API below for more details.

Type:{ clickAway?: elementType, endDecorator?: elementType, root?: elementType, startDecorator?: elementType }

Default:{}


startDecorator

Element placed before the children.

Type:node


sx

The system prop that allows defining system overrides as well as additional CSS styles.

See the `sx` page for more details.

Type:Array<func
| object
| bool>
| func
| object


variant

The global variant to use.

To learn how to add your own variants, check out Themed components—Extend variants.

Type:'outlined'
| 'plain'
| 'soft'
| 'solid'

Default:'outlined'


The ref is forwarded to the root element.

Theme default props

You can use JoySnackbar to change the default props of this component with the theme.


Slots

To learn how to customize the slot, check out the Overriding component structure guide.

root

The component that renders the root.

Class name: .MuiSnackbar-root

Default component: 'div'


startDecorator

The component that renders the start decorator.

Class name: .MuiSnackbar-startDecorator

Default component: 'span'


endDecorator

The component that renders the end decorator.

Class name: .MuiSnackbar-endDecorator

Default component: 'span'


clickAway

The component that renders the click away.

Default component: ClickAwayListener


CSS classes

These class names are useful for styling with CSS. They are applied to the component's slots when specific states are triggered.

.MuiSnackbar-anchorOriginBottomCenter

Styles applied to the root element if anchorOrigin={{ 'bottom', 'center' }}.

Rule name:anchorOriginBottomCenter


.MuiSnackbar-anchorOriginBottomLeft

Styles applied to the root element if anchorOrigin={{ 'bottom', 'left' }}.

Rule name:anchorOriginBottomLeft


.MuiSnackbar-anchorOriginBottomRight

Styles applied to the root element if anchorOrigin={{ 'bottom', 'right' }}.

Rule name:anchorOriginBottomRight


.MuiSnackbar-anchorOriginTopCenter

Styles applied to the root element if anchorOrigin={{ 'top', 'center' }}.

Rule name:anchorOriginTopCenter


.MuiSnackbar-anchorOriginTopLeft

Styles applied to the root element if anchorOrigin={{ 'top', 'left' }}.

Rule name:anchorOriginTopLeft


.MuiSnackbar-anchorOriginTopRight

Styles applied to the root element if anchorOrigin={{ 'top', 'right' }}.

Rule name:anchorOriginTopRight


.MuiSnackbar-colorDanger

Class name applied to the root element if color="danger".

Rule name:colorDanger


.MuiSnackbar-colorNeutral

Class name applied to the root element if color="neutral".

Rule name:colorNeutral


.MuiSnackbar-colorPrimary

Class name applied to the root element if color="primary".

Rule name:colorPrimary


.MuiSnackbar-colorSuccess

Class name applied to the root element if color="success".

Rule name:colorSuccess


.MuiSnackbar-colorWarning

Class name applied to the root element if color="warning".

Rule name:colorWarning


.MuiSnackbar-sizeLg

Class name applied to the root element if size="lg".

Rule name:sizeLg


.MuiSnackbar-sizeMd

Class name applied to the root element if size="md".

Rule name:sizeMd


.MuiSnackbar-sizeSm

Class name applied to the root element if size="sm".

Rule name:sizeSm


.MuiSnackbar-variantOutlined

Class name applied to the root element if variant="outlined".

Rule name:variantOutlined


.MuiSnackbar-variantPlain

Class name applied to the root element if variant="plain".

Rule name:variantPlain


.MuiSnackbar-variantSoft

Class name applied to the root element if variant="soft".

Rule name:variantSoft


.MuiSnackbar-variantSolid

Class name applied to the root element if variant="solid".

Rule name:variantSolid



You can override the style of the component using one of these customization options: