# mui-simple-snackbars

> Turning MUI snackbars easier to use.

Latest version **0.6.0** (published 2017-09-21) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install mui-simple-snackbars
pnpm add mui-simple-snackbars
yarn add mui-simple-snackbars
bun add mui-simple-snackbars
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2017-09-21 |
| First published | 2017-08-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Andrey Luiz |
| Maintainers | andreyluiz |
| Keywords | mui, snackbars, react, ui, material-ui, material-design |

## Links

- npm: https://www.npmjs.com/package/mui-simple-snackbars
- Repository: https://github.com/andreyluiz/mui-simple-snackbars
- Issues: https://github.com/andreyluiz/mui-simple-snackbars/issues
- npm.io page: https://npm.io/package/mui-simple-snackbars

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.6.0 (latest) — 2017-09-21
- 0.5.0 — 2017-09-13
- 0.4.0 — 2017-08-30
- 0.3.0 — 2017-08-17
- 0.2.0 — 2017-08-17
- 0.1.0 — 2017-08-03

## README

# mui-simple-snackbars
Turning Material UI Snackbars simple to use.

## Installation
> npm install --save mui-simple-snackbars

or

> yarn add mui-simple-snackbars

## Usage

Simply:

```jsx
import show from 'mui-simple-snackbars';

export default () => (
  <button
    onClick={() => {
      show('Hey Hey!');
    }}
  />
);
```

Result:

![Imgur](http://i.imgur.com/BKPGfZu.png)

## Inspiration

mui-simple-snackbars was inspired by [mui-simple-dialogs](https://github.com/andreyluiz/mui-simple-dialogs). The API is similar, and the way snackbars work is also similar, with the difference that snackbars pops out from the screen, while dialogs wait for user action.

Material UI Snackbars are awesome. They are simple to use too. Check this example:

```jsx
import React, { Component } from 'react';
import Snackbar from 'material-ui/Snackbar';

type Props = {
  open: boolean,
  onAction: Function,
  onClose: Function,
};

export default class UserDeletedSnackbar extends Component {
  props: Props

  render() {
    const {
      open,
      onAction,
      onClose,
    } = this.props;
    return (
      <Snackbar
        open={open}
        message="User deleted."
        action="undo"
        onActionTouchTap={onAction}
        autoHideDuration={3000}
        onRequestClose={onClose}
      />
    );
  }
}

```

To use this snackbar, we could implement this code in the parent component:

```jsx
import React, { Component } from 'react';
import { UserDeletedSnackbar } from './components';

export default class User extends Component {
  constructor(props) {
    super(props);
    this.state = { snackbarOpen: false };
  }

  openSnackbar = () => {
    this.setState({ snackbarOpen: true });
  }

  closeSnackbar = () => {
    this.setState({ snackbarOpen: false });
  }

  deleteUser = (id) => {
    this.props.deleteUser(id)
      .then(() => {
        this.openSnackbar();             // Here the magic begins
      });
  }

  undoLastUserDelete = () => {
    this.props.undoLastUserDelete()
      .then(() => {
        // do stuff
      });
  }

  render() {
    return (
      <div>
        <ul>
        {this.props.users.map(user => (
          <li>
            {user.name} - 
              <a href="#" onClick={() => { this.deleteUser(user.id) }}>Delete</a>
          </li>
        ))}
        </ul>
        
        <DeleteUserDialog
          open={this.state.snackbarOpen}
          onClose={this.closeSnackbar}
          onAction={this.undoUserDelete}
        />
      </div>
    );
  }
}
```

We have some problems with this approach:

1. We rely on parent's state to define if the snackbar is visible or not (`snackbarOpen` state property);
3. The snackbar is mounted with the parent component and stays in the DOM even when we are not using it;
4. We create a lot of functions to handle a simple snackbar.

When the application begins to scale, it turns out that Material UI Snackbars become a problem. mui-simple-snackbars ease this task by providing an API to spawn snackbars where and when you need them. Once used, they are wiped from your DOM.

Here is the same example above implemented with mui-simple-snackbars API:

```jsx
import React, { Component } from 'react';
import snackbar from 'mui-simple-snackbars';

export default class User extends Component {
  deleteUser = (id) => {
    this.props.deleteUser(id)
      .then(() => {
        return this.openSnackbar('User deleted.', 'undo'); // Here the magic begins
      })
      .then((actionClicked) => {
        if (actionClicked) {                               // True for action clicked.
          this.props.undoLastUserDelete()
            .then(() => {
              // do stuff
            });
        }
      });
  }

  render() {
    return (
      <div>
        <ul>
        {this.props.users.map(user => (
          <li>
            {user.name} - 
              <a href="#" onClick={() => { this.deleteUser(user.id) }}>Delete</a>
          </li>
        ))}
        </ul>
      </div>
    );
  }
}
```

Simpler, huh?

## API

### `show(message: string, [options]): Promise`

Returns a Promise that is resolved once the snackbar hides.

#### Options

**Option**|**Description**|**Type**|**Default**
-----|-----|-----|-----
`duration`|For how many milisseconds the snackbar should be visible.|`Number`|3000
`theme`|The Material UI custom theme.|`Object`|null

### `showWithAction(message: string, action: string, [options]): Promise`

Returns a Promise that is resolved once the snackbar hides or when the user clicks the action. The resolve parameter is a `boolean`. `true` for Action clicked.

#### Options

**Option**|**Description**|**Type**|**Default**
-----|-----|-----|-----
`duration`|For how many milisseconds the snackbar should be visible.|`Number`|3000
`theme`|The Material UI custom theme.|`Object`|null

## Contributing

Felt inspired or found a bug? File a bug or create a Pull Request. I'll be happy to receive any insight or code suggestion.

---
_Source: https://npm.io/package/mui-simple-snackbars · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
