Skip to main content

Snackbar

The Snackbar component is used to display brief notifications or messages at the bottom of the screen. Below are examples of how to use it in Python Dash.


import dash
from dash import html, Input, Output, State, callback
import yipit_dash_mui_components as dmc

dash._dash_renderer._set_react_version('18.2.0')

app = dash.Dash(__name__)

app.layout = dmc.YipitMUIProvider(
children=[
# Example 1: Basic success snackbar
html.Div([
dmc.Button("Show Success Snackbar", id="success-btn", variant="contained"),
dmc.Snackbar(
id="success-snackbar",
message="Operation completed successfully!",
severity="success",
showCloseButton=True,
open=False,
autoHideDuration=6000
)
], style={"marginBottom": "16px"}),

# Example 2: Error snackbar with action
html.Div([
dmc.Button("Show Error Snackbar", id="error-btn", variant="contained", color="error"),
dmc.Snackbar(
id="error-snackbar",
message="An error occurred while processing your request",
severity="error",
showCloseButton=True,
open=False,
autoHideDuration=6000,
action=dmc.Button("RETRY", color="inherit", size="small")
)
], style={"marginBottom": "16px"}),

# Example 3: Info snackbar with custom icon
html.Div([
dmc.Button("Show Info Snackbar", id="info-btn", variant="contained", color="info"),
dmc.Snackbar(
id="info-snackbar",
message="New features are available!",
severity="info",
showCloseButton=True,
open=False,
autoHideDuration=6000,
icon=html.Span("info", style={"fontFamily": "Material Icons"})
)
], style={"marginBottom": "16px"}),

# Example 4: Warning snackbar
html.Div([
dmc.Button("Show Warning Snackbar", id="warning-btn", variant="contained", color="warning"),
dmc.Snackbar(
id="warning-snackbar",
message="Your subscription is about to expire",
severity="warning",
showCloseButton=True,
open=False,
autoHideDuration=6000
)
])
]
)

# Callbacks to show snackbars
@callback(
Output("success-snackbar", "open"),
Input("success-btn", "n_clicks"),
prevent_initial_call=True
)
def show_success_snackbar(n_clicks):
if n_clicks:
return True
return False

@callback(
Output("error-snackbar", "open"),
Input("error-btn", "n_clicks"),
prevent_initial_call=True
)
def show_error_snackbar(n_clicks):
if n_clicks:
return True
return False

@callback(
Output("info-snackbar", "open"),
Input("info-btn", "n_clicks"),
prevent_initial_call=True
)
def show_info_snackbar(n_clicks):
if n_clicks:
return True
return False

@callback(
Output("warning-snackbar", "open"),
Input("warning-btn", "n_clicks"),
prevent_initial_call=True
)
def show_warning_snackbar(n_clicks):
if n_clicks:
return True
return False

if __name__ == "__main__":
app.run_server(debug=True)

Props Table

Below are the main props available for the Snackbar component.

PropTypeDefaultDescription
idstring-The ID of the component, used for identifying dash components in callbacks
messagestring-The message to display in the snackbar
severity"success" | "info" | "warning" | "error"-The severity level of the alert
iconReact.ReactNode-Custom icon to display in the snackbar
actionReact.ReactNode-Action component to display (like a button)
showCloseButtonbooleanfalseIf true, shows a close button in the snackbar
openbooleanfalseIf true, the snackbar is shown
autoHideDurationnumber6000The number of milliseconds to wait before automatically closing
anchorOriginobject{ vertical: 'bottom', horizontal: 'left' }Position where the snackbar should appear
sxobject-The system prop that allows defining custom styles
TransitionComponentReact.ComponentType-The component used for the transition
TransitionPropsobject-Props applied to the Transition element

For the full list of props, refer to the official MUI documentation, as this component is based on MUI:

MUI Snackbar Documentation