JGuang

Snackbar

Snackbar · Material UI

Preview
npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1

Snackbar

Show transient feedback without interrupting the main task.

This guide covers Material UI Core with the default Emotion engine; extensions and additional demos are explicitly classified.

Use case and business boundary

A timer requests dismissal; the host must update open. Notifications do not implement queues, undo or backend success.

Native composition and scope

Snackbar, SnackbarContent

Preserve native Material UI composition and types; do not substitute JGuang UI/Base UI/Radix APIs. Example entries are not extra components.

Native installation and initialization

npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1

Use native npm and Emotion, not an invented JGuang UI Registry URL. Place examples inside native ThemeProvider; choose CssBaseline or ScopedCssBaseline for the project. Inspect React, existing providers, lockfile and SSR first. Never overwrite files or conceal peer conflicts. This batch verified React 19.2.8; React 18 and below require matching react-is. See the included bilingual setup guide.

Practical native parameters

These selected parameters are not a flattened universal API. Preserve the native declaring owner, slot structure, nullable values and generics.

Owner.propPractical roleDeclaration source
Snackbar.openControlled native visibility; it does not fetch data or execute actions.Snackbar
Snackbar.onCloseNative request to close; reason/event signatures differ and the host updates open.Snackbar
Snackbar.autoHideDurationMilliseconds before native Snackbar requests closing; update open in the host.Snackbar
Snackbar.anchorOriginNative vertical/horizontal origin pair.Snackbar
Snackbar.messageHost-supplied Snackbar message, not a generated backend result.Snackbar

Runnable native examples

Machine keys select example entries, not universal variant props. Native demo JSX/data stay unchanged; JGuang-authored acceptance fixtures are explicitly labelled.

  • Introduction · SimpleSnackbar · upstream native demo
  • Position · PositionedSnackbar · upstream native demo
  • Content · LongTextSnackbar · upstream native demo
  • Automatic dismiss · AutohideSnackbar · upstream native demo
  • Transitions · TransitionsSnackbar · upstream native demo
  • Use with Alerts · CustomizedSnackbars · upstream native demo
  • Use with Floating Action Buttons · FabIntegrationSnackbar · upstream native demo
  • Consecutive Snackbars · ConsecutiveSnackbars · upstream native demo

Explicitly unsupported additional examples

These remain in the upstream inventory, not silently skipped or presented as working previews. Additional dependency/framework acceptance is required.

  • notistack · IntegrationNotistack: notistack

Actual usage example

Unmodified pinned native demo; copy its required local helper files together.

import * as React from 'react';
import Button from '@mui/material/Button';
import Snackbar, { SnackbarCloseReason } from '@mui/material/Snackbar';
import IconButton from '@mui/material/IconButton';
import CloseIcon from '@mui/icons-material/Close';

export default function SimpleSnackbar() {
  const [open, setOpen] = React.useState(false);

  const handleClick = () => {
    setOpen(true);
  };

  const handleClose = (
    event: React.SyntheticEvent | Event,
    reason?: SnackbarCloseReason,
  ) => {
    if (reason === 'clickaway') {
      return;
    }

    setOpen(false);
  };

  const action = (
    <React.Fragment>
      <Button color="secondary" size="small" onClick={handleClose}>
        UNDO
      </Button>
      <IconButton
        size="small"
        aria-label="close"
        color="inherit"
        onClick={handleClose}
      >
        <CloseIcon fontSize="small" />
      </IconButton>
    </React.Fragment>
  );

  return (
    <div>
      <Button onClick={handleClick}>Open Snackbar</Button>
      <Snackbar
        open={open}
        autoHideDuration={6000}
        onClose={handleClose}
        message="Note archived"
        action={action}
      />
    </div>
  );
}

Example dependency boundaries

react, @mui/material/Button, @mui/material/Snackbar, @mui/material/IconButton, @mui/icons-material/Close, @mui/material/Grid, @mui/material/Box, @mui/material/Stack, @mui/material/SnackbarContent, @mui/material/Fade, @mui/material/Slide, @mui/material/Grow, @mui/material/transitions, @mui/material/Alert, @mui/material/AppBar, @mui/material/CssBaseline, @mui/material/GlobalStyles, @mui/material/Toolbar, @mui/icons-material/Menu, @mui/material/Typography, @mui/material/Fab, @mui/icons-material/Add

Direct Icons/System/Utils example imports require their matching 9.4.0 packages. Relative data/helpers are part of the example closure, not extra components. Remote images and simulated operations do not supply a backend.

Pinned upstream API metadata reference

These tables retain the upstream metadata, not a newly inferred generic type system. Conditional generics, inherited props and callback details are defined by the source declarations below. Theme defaults may override metadata defaults.

Snackbar

import Snackbar from '@mui/material/Snackbar';
import { Snackbar } from '@mui/material';
PropRequiredUpstream metadata type / constraintMetadata default
actionnonode—
anchorOriginno{ horizontal: 'center' | 'left' | 'right', vertical: 'bottom' | 'top' }{ vertical: 'bottom', horizontal: 'left' }
autoHideDurationnonumbernull
childrennoelement—
classesnoobject—
disableWindowBlurListenernoboolfalse
keynoany—
messagenonode—
onClosenofunc—
opennobool—
resumeHideDurationnonumber—
slotPropsno{ clickAwayListener?: func | object, content?: func | object, root?: func | object, transition?: func | object }{}
slotsno{ clickAwayListener?: elementType, content?: elementType, root?: elementType, transition?: elementType }{}
sxnoArray<func | object | bool> | func | object—
transitionDurationnonumber | { appear?: number, enter?: number, exit?: number }{ enter: theme.transitions.duration.enteringScreen, exit: theme.transitions.duration.leavingScreen, }

SnackbarContent

import SnackbarContent from '@mui/material/SnackbarContent';
import { SnackbarContent } from '@mui/material';

Native inheritance: Paper. Parent/DOM props are not fabricated as additional own props.

PropRequiredUpstream metadata type / constraintMetadata default
actionnonode—
classesnoobject—
messagenonode—
rolenostring'alert'
sxnoArray<func | object | bool> | func | object—

Native source declarations

Declarations/imports are extracted from pinned source without flattening unions or external inheritance. Original English comments/types are source reference, not a claim of translated upstream prose. Shared types stay in their native packages.

vendor/src/Snackbar/Snackbar.d.ts

import * as React from 'react';

import { SxProps } from '@mui/system';

import ClickAwayListener from '../ClickAwayListener';

import { Theme } from '../styles';

import { InternalStandardProps as StandardProps } from '../internal';

import { SnackbarContentProps } from '../SnackbarContent';

import { TransitionProps } from '../transitions/types';

import { SnackbarClasses } from './snackbarClasses';

import { CreateSlotsAndSlotProps, SlotComponentProps, SlotProps } from '../utils/types';

export interface SnackbarSlots {
  /**
   * The component that renders the root slot.
   * @default 'div'
   */
  root: React.ElementType;
  /**
   * The component that renders the content slot.
   * @default SnackbarContent
   */
  content: React.ElementType;
  /**
   * The component that renders the clickAwayListener slot.
   * @default ClickAwayListener
   */
  clickAwayListener: React.ElementType;
  /**
   * The component that renders the transition.
   * [Follow this guide](/material-ui/transitions/#transition-slots) to learn more about the requirements for this component.
   * @default Grow
   */
  transition: React.ElementType;
}

export interface SnackbarRootSlotPropsOverrides {}

export interface SnackbarContentSlotPropsOverrides {}

export interface SnackbarClickAwayListenerSlotPropsOverrides {}

export interface SnackbarTransitionSlotPropsOverrides {}

export type SnackbarSlotsAndSlotProps = CreateSlotsAndSlotProps<
  SnackbarSlots,
  {
    /**
     * Props forwarded to the root slot.
     * By default, the available props are based on the div element.
     */
    root: SlotProps<'div', SnackbarRootSlotPropsOverrides, SnackbarOwnerState>;
    /**
     * Props forwarded to the content slot.
     * By default, the available props are based on the [SnackbarContent](https://mui.com/material-ui/api/snackbar-content/#props) component.
     */
    content: SlotProps<
      React.ElementType<SnackbarContentProps>,
      SnackbarContentSlotPropsOverrides,
      SnackbarOwnerState
    >;
    /**
     * Props forwarded to the clickAwayListener slot.
     * By default, the available props are based on the [ClickAwayListener](https://mui.com/material-ui/api/click-away-listener/#props) component.
     */
    clickAwayListener: SlotComponentProps<
      typeof ClickAwayListener,
      SnackbarClickAwayListenerSlotPropsOverrides,
      SnackbarOwnerState
    >;
    /**
     * Props applied to the transition element.
     * By default, the element is based on the [Grow](https://mui.com/material-ui/api/grow/#props) component.
     */
    transition: SlotComponentProps<
      React.ElementType<TransitionProps>,
      TransitionProps & SnackbarTransitionSlotPropsOverrides,
      SnackbarOwnerState
    >;
  }
>;

export interface SnackbarOrigin {
  vertical: 'top' | 'bottom';
  horizontal: 'left' | 'center' | 'right';
}

export type SnackbarCloseReason = 'timeout' | 'clickaway' | 'escapeKeyDown';

export interface SnackbarProps
  extends
    Omit<StandardProps<React.HTMLAttributes<HTMLDivElement>>, 'slots' | 'slotProps'>,
    SnackbarSlotsAndSlotProps {
  /**
   * The action to display. It renders after the message, at the end of the snackbar.
   */
  action?: SnackbarContentProps['action'] | undefined;
  /**
   * The anchor of the `Snackbar`.
   * On smaller screens, the component grows to occupy all the available width,
   * the horizontal alignment is ignored.
   * @default { vertical: 'bottom', horizontal: 'left' }
   */
  anchorOrigin?: SnackbarOrigin | undefined;
  /**
   * 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.
   * @default null
   */
  autoHideDuration?: number | null | undefined;
  /**
   * Replace the `SnackbarContent` component.
   */
  children?: React.ReactElement<unknown, any> | undefined;
  /**
   * Override or extend the styles applied to the component.
   */
  classes?: Partial<SnackbarClasses> | undefined;
  /**
   * If `true`, the `autoHideDuration` timer will expire even if the window is not focused.
   * @default false
   */
  disableWindowBlurListener?: boolean | undefined;
  /**
   * 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.
   */
  key?: any;
  /**
   * The message to display.
   */
  message?: SnackbarContentProps['message'] | undefined;
  /**
   * 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`.
   *
   * @param {React.SyntheticEvent<any> | Event} event The event source of the callback.
   * @param {string} reason Can be: `"timeout"` (`autoHideDuration` expired), `"clickaway"`, or `"escapeKeyDown"`.
   */
  onClose?:
    ((event: React.SyntheticEvent<any> | Event, reason: SnackbarCloseReason) => void) | undefined;
  /**
   * If `true`, the component is shown.
   */
  open?: boolean | undefined;
  /**
   * 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.
   */
  resumeHideDuration?: number | undefined;
  /**
   * The system prop that allows defining system overrides as well as additional CSS styles.
   */
  sx?: SxProps<Theme> | undefined;
  /**
   * The duration for the transition, in milliseconds.
   * You may specify a single timeout for all transitions, or individually with an object.
   * @default {
   *   enter: theme.transitions.duration.enteringScreen,
   *   exit: theme.transitions.duration.leavingScreen,
   * }
   */
  transitionDuration?: TransitionProps['timeout'] | undefined;
}

export default function Snackbar(props: SnackbarProps): React.JSX.Element;

export interface SnackbarOwnerState extends Omit<SnackbarProps, 'slots' | 'slotProps'> {}

vendor/src/SnackbarContent/SnackbarContent.d.ts

import * as React from 'react';

import { SxProps } from '@mui/system';

import { Theme } from '../styles';

import { InternalStandardProps as StandardProps } from '../internal';

import { PaperProps } from '../Paper';

import { SnackbarContentClasses } from './snackbarContentClasses';

export interface SnackbarContentProps extends StandardProps<PaperProps, 'children'> {
  /**
   * The action to display. It renders after the message, at the end of the snackbar.
   */
  action?: React.ReactNode;
  /**
   * Override or extend the styles applied to the component.
   */
  classes?: Partial<SnackbarContentClasses> | undefined;
  /**
   * The message to display.
   */
  message?: React.ReactNode;
  /**
   * The ARIA role attribute of the element.
   * @default 'alert'
   */
  role?: PaperProps['role'] | undefined;
  /**
   * The system prop that allows defining system overrides as well as additional CSS styles.
   */
  sx?: SxProps<Theme> | undefined;
}

export default function SnackbarContent(props: SnackbarContentProps): React.JSX.Element;

Post-install acceptance

Run strict types, production build and real interactions. Check keyboard labels/focus, disabled/error/read-only semantics where natively supported, both themes, narrow screens and reduced motion. Verify callbacks against the real host data; do not declare installation complete from copying a prompt. Initialization fixtures also require server HTML and warning-free hydration.

Source and license

https://github.com/mui/material-ui/tree/809a7717b4c050ba3f69b75300689f07c050a16e

MIT · @mui/material@9.4.0. Vendor originals and license are preserved. Full original upstream prose remains in vendor/docs and is not represented as fully translated.