14.2.2 • Published 16 days ago

@rmwc/snackbar v14.2.2

Weekly downloads
5,194
License
MIT
Repository
github
Last release
16 days ago

Snackbars

Snackbars provide brief feedback about an operation through a message at the bottom of the screen.

  • Module @rmwc/snackbar
  • Import styles:
    • Using CSS Loader
      • import '@rmwc/snackbar/styles';
    • Or include stylesheets
      • '@material/snackbar/dist/mdc.snackbar.css'
      • '@material/button/dist/mdc.button.css'
      • '@material/ripple/dist/mdc.ripple.css'
  • MDC Docs: https://material.io/develop/web/components/snackbars/

Basic Usage

You can render a snackbar in your UI and control its open state.

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

  return (
    <>
      <Snackbar
        open={open}
        onClose={(evt) => setOpen(false)}
        message="This is a new message"
        dismissesOnAction
        action={
          <SnackbarAction
            label="Dismiss"
            onClick={() => console.log('Click Me')}
          />
        }
      />

      <Button
        raised
        label="Show snackbar"
        onClick={(evt) => setOpen(!open)}
      />
    </>
  );
}
function Example() {
  const [open, setOpen] = React.useState(false);

  return (
    <>
      <Snackbar
        open={open}
        onClose={(evt) => setOpen(false)}
        message="Start aligned, open until dismissed"
        stacked
        dismissesOnAction
        action={[
          <SnackbarAction label="Yeah!" />,
          <SnackbarAction label="No..." />
        ]}
        leading
        timeout={-1}
      />

      <Button
        raised
        label="Show start-aligned"
        onClick={(evt) => setOpen(!open)}
      />
    </>
  );
}
function Example() {
  const [open, setOpen] = React.useState(false);

  return (
    <>
      <Snackbar
        open={open}
        onClose={(evt) => setOpen(false)}
        message="This is a new message"
        dismissesOnAction
        dismissIcon="close"
        timeout={-1}
      />
      <Button
        raised
        label="Show snackbar"
        onClick={(evt) => setOpen(!open)}
      />
    </>
  );
}

Usage with SnackbarQueue

While rendering the Snackbar inline works for simple cases, you'll likely have a notification system, or want to send notifications from anywhere in your app. The SnackbarQueue exists as a convenient interface for handling these cases and rendering the snackbar messages for you. If you've used the DialogQueue, the SnackbarQueue is very similar.

Setup is nice and easy, create a queue object you can pass around in your code base, pass the queues messages to the SnackbarQueue component, and then use the notify function to send notifications.

  `
// Create a file that exports your queue
// myQueue.js
import { createSnackbarQueue } from '@rmwc/snackbar';

export const queue = createSnackbarQueue();
  `
// Somewhere at the top level of your app
// Render the SnackbarQueue
import React from 'react';
import { queue } from './myQueue';

export default function App() {
  return (
    <div>
      ...
      <SnackbarQueue
        messages={queue.messages}
        // You can also pass default options to pass to your notifications
        // ie, make them all leading, stacked, etc
        leading
        stacked
      />
    </div>
  )
}

The notify function was designed to mimic the the built-in browser Notifications api and can accept most of the relevant options (icon, image, title, body, actions, ,etc). It also can accept any of the Snackbar props. Just import your queue, and call the notify method.

  `
// Somewhere else in your app
// Could be a view, your redux store, anywhere you want...
import { queue } from './myQueue';

// Simple example
queue.notify({
  title: 'Hi there'
});

// With some features
queue.notify({
  title: <b>Warning</b>,
  body: 'You have selected pizza instead icecream!',
  icon: 'warning',
  actions: [
    {
      // NotificationAction api format
      title: 'Fix It!',
      icon: 'close',
      action: 'fixit' // action will be available as evt.detail.reason in the onClose event
    },
    {
      // OR SnackbarActionProps format
      label: 'Continue...',
      icon: 'check',
      onClick: () => {}
    },
  ]
});
() => {
  const { messages, notify } = createSnackbarQueue();

  function App() {
    return (
      <div>
        <Button
          label="Notify"
          onClick={() =>
            notify({
              title: <b>Success</b>,
              body: 'You have selected pizza!',
              dismissesOnAction: true,
              icon: 'check',
              actions: [
                {
                  title: 'Dismiss'
                }
              ]
            })
          }
        />
        <SnackbarQueue messages={messages} />
      </div>
    );
  }
  return <App />;
}

Snackbar

A Snackbar component for notifications.

Props

NameTypeDescription
actionReactNode \| ReactNode[]One or more actions to add to the snackbar.
dismissIconIconPropT
dismissesOnActionbooleanWhether or not your want clicking an action to close the Snackbar.
foundationRefRef<null \| MDCSnackbarFoundation<>>Advanced: A reference to the MDCFoundation.
iconIconPropTAn icon for the snackbar
leadingboolean
messageReactNodeA string or other renderable JSX to be used as the message body.
onClose(evt: SnackbarOnCloseEventT) => voidA callback thats fired when the Snackbar hides. evt.detail = { reason?: string }
onOpen(evt: SnackbarOnOpenEventT) => voidA callback thats fired when the Snackbar shows.
openbooleanShow the Snackbar.
stackedbooleanPlaces the action underneath the message text.
timeoutnumberMilliseconds to show the Snackbar for. Set to -1 to show indefinitely.

SnackbarAction

A button for a snackbar action.

Props

NameTypeDescription
actionstringAn action returned in evt.detail.reason to the onClose handler.
childrenReactNodeContent specified as children.
dangerbooleanUsed to indicate a dangerous action.
densebooleanMake the Button dense.
disabledbooleanMake the button disabled
iconIconPropTAn Icon for the Button
labelanyContent specified as a label prop.
outlinedbooleanMake the button outlined.
raisedbooleanMake the Button raised.
rippleRipplePropTAdds a ripple effect to the component
touchbooleanMakes the button more touch friendly. This will automatically be set true if used inside of TouchTargetWrapper.
trailingIconIconPropTA trailing icon for the Button
unelevatedbooleanMake the button unelevated.
14.2.2

16 days ago

14.2.0

23 days ago

14.2.1

23 days ago

14.1.5

24 days ago

14.1.4

1 month ago

14.1.3

2 months ago

14.1.2

2 months ago

14.1.1

2 months ago

14.1.0

2 months ago

14.0.12

2 months ago

14.0.11

3 months ago

14.0.10

3 months ago

14.0.9

4 months ago

14.0.8

4 months ago

14.0.7

4 months ago

14.0.6

4 months ago

14.0.5

4 months ago

14.0.4

5 months ago

14.0.1-alpha.0

8 months ago

14.0.2-alpha.3

7 months ago

14.0.2-alpha.0

8 months ago

14.0.2-alpha.1

7 months ago

14.0.2-alpha.6

6 months ago

14.0.2-alpha.7

6 months ago

14.0.2-alpha.4

7 months ago

14.0.2-alpha.5

6 months ago

14.0.0

6 months ago

14.0.1

6 months ago

14.0.0-alpha.0

9 months ago

14.0.2

5 months ago

14.0.3

5 months ago

8.0.8

11 months ago

8.0.7

1 year ago

8.0.6

1 year ago

8.0.5

1 year ago

8.0.4

1 year ago

8.0.3

2 years ago

8.0.2

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

7.0.3

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

7.0.0

2 years ago

6.1.4

4 years ago

6.1.3

4 years ago

6.1.2

4 years ago

6.0.14

4 years ago

6.0.13

4 years ago

6.0.12

4 years ago

6.0.11

4 years ago

6.0.10

4 years ago

6.0.9

4 years ago

6.0.5

4 years ago

6.0.4

4 years ago

6.0.3

4 years ago

6.0.2

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

6.0.0-rc.5

4 years ago

6.0.0-rc.1

4 years ago

6.0.0-rc.4

4 years ago

6.0.0-rc.3

4 years ago

6.0.0-rc.2

4 years ago

6.0.0-rc.0

4 years ago

6.0.0-alpha.16

4 years ago

6.0.0-alpha.14

4 years ago

6.0.0-alpha.15

4 years ago

6.0.0-alpha.13

4 years ago

6.0.0-alpha.12

4 years ago

6.0.0-alpha.11

4 years ago

6.0.0-alpha.7

4 years ago

5.7.2

4 years ago

6.0.0-alpha.6

4 years ago

6.0.0-alpha.5

4 years ago

6.0.0-alpha.3

4 years ago

5.7.0

5 years ago

5.6.0

5 years ago

5.5.2

5 years ago

5.5.1

5 years ago

5.5.0

5 years ago

5.4.3

5 years ago

5.4.2

5 years ago

5.4.1

5 years ago

5.4.0

5 years ago

5.3.1

5 years ago

5.3.0

5 years ago

5.2.2

5 years ago

5.2.1

5 years ago

5.2.0

5 years ago

5.2.0-alpha.0

5 years ago

5.1.8

5 years ago

5.1.7

5 years ago

5.1.6

5 years ago

5.1.5

5 years ago

5.1.4

5 years ago

5.1.3

5 years ago

5.1.2

5 years ago

5.1.1

5 years ago

5.1.0

5 years ago

5.0.30-rc.0

5 years ago

5.0.29-rc.0

5 years ago

5.0.28-rc.0

5 years ago

5.0.27-rc.0

5 years ago

5.0.26-rc.0

5 years ago

5.0.25-rc.0

5 years ago

5.0.24-rc.0

5 years ago

5.0.23-rc.0

5 years ago

5.0.23-alpha.0

5 years ago

5.0.22-alpha.0

5 years ago

5.0.21-alpha.0

5 years ago

5.0.20-alpha.0

5 years ago

5.0.19-alpha.0

5 years ago

5.0.18-alpha.0

5 years ago

5.0.17-alpha.0

5 years ago

5.0.16-alpha.0

5 years ago

5.0.15-alpha.0

5 years ago

5.0.14-alpha.0

5 years ago

5.0.13-alpha.0

5 years ago

5.0.12-alpha.0

5 years ago

5.0.11-alpha.0

5 years ago

5.0.8-alpha.0

5 years ago

5.0.7-alpha.0

5 years ago

5.0.6-alpha.0

5 years ago

5.0.5-alpha.0

5 years ago

5.0.4-alpha.0

5 years ago

5.0.3-alpha.0

5 years ago

5.0.2-alpha.0

5 years ago

5.0.1-alpha.0

5 years ago

5.0.0-alpha.0

5 years ago

4.0.6

5 years ago

4.0.5

5 years ago

4.0.4

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.0.11

5 years ago

3.0.10

5 years ago

3.0.9

6 years ago

3.0.8

6 years ago

3.0.7

6 years ago

3.0.6

6 years ago

3.0.5

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.2.2

6 years ago

2.2.0

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

2.0.0-alpha.7

6 years ago

2.0.0-alpha.6

6 years ago

2.0.0-alpha.5

6 years ago

2.0.0-alpha.4

6 years ago

2.0.0-alpha.3

6 years ago

2.0.0-alpha.2

6 years ago

2.0.0-alpha.1

6 years ago

2.0.0-alpha.0

6 years ago

1.10.1-alpha.0

6 years ago

1.10.0-alpha.0

6 years ago