1.1.1 • Published 3 years ago

mui-gotit v1.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

mui-gotit

forthebadge Netlify Status

Enhanced Snackbars for Material UI 5 and React 17 with imperative flavor. Inpsired by notistack - Working with Material UI 5 Theming and sx property.

This library will be kept as simple as possible to allow easy upgrades to newer versions of MUI and allow anybody to customize this library to their need.

Live Demo

Installation

  1. npm i mui-gotit
import { Gotit, GotitContext } from "mui-gotit";
import { useContext } from "react";
/** @jsxImportSource @emotion/react */
import { css } from "@emotion/react";
import Alert from "@mui/material/Alert";
import { createTheme, ThemeProvider } from "@mui/material/styles";
import { blue } from "@mui/material/colors";
import Button from "@mui/material/Button";

const theme = createTheme({
  palette: {
    primary: {
      main: blue[500]
    }
  }
});

function ExampleNotificator() {
  const gotitContext = useContext(GotitContext);
  return (
    <Button
      onClick={() => {
        gotitContext.displayNotification({
          snackbar: {
            open: true,
            autoHideDuration: 4000,
            anchorOrigin: { vertical: "bottom", horizontal: "right" }
          },
          gotit: {
            group: "main",
            stackDirection: "top",
            space: 10,
            component: (
              <Alert
                severity={"success"}
                sx={{ backgroundColor: "primary.dark" }}
              >
                <div
                  css={css`
                    color: white;
                  `}
                >
                  {`a simple mui-gotit snack wit a random number `}
                  {Math.random()}
                </div>
              </Alert>
            )
          }
        });
      }}
    >
      display a notification
    </Button>
  );
}

export default function App() {
  return (
    <div className="App">
      <ThemeProvider theme={theme}>
        <Gotit debug={false}>
          <ExampleNotificator />
        </Gotit>
      </ThemeProvider>
    </div>
  );
}

Docs

You can access the following functions from the gotitContext.

gotitContext.displayNotification(option)

The option argument looks like this:

let option = 
{
    // the snackbar properties passed to the MUI <Snackbar/> element.
    snackbar: {
      open: true,
      autoHideDuration: 4000,
      anchorOrigin: { vertical: "top", horizontal: "right" }
    },
    gotit: {
      // group: string. Used to logically group notifications. All notifications of a group
      // are subject to the css transitions
      group: "app-main",
      // stackDirection: string. One of "top" or "bottom". If "top" is assigned, then the notification will
      // stack upwards. If "bottom" is assigned, they will stack downwards.
      stackDirection: "bottom",
      // maxSnackbars: integer. The max number of notifications that are displayed in a group.
      // The oldest notification will be removed if the max nr of notifications is reached.
      maxSnackbars: 3,
      // zIndexBase: int. The minimum z-index. Helps to put Snackbars on top of modals. 
      zIndexBase: 9999,
      // space: number. The distance in pixels from the previous notification
      space: 15,
      // emotionCssString: string. The cssString passed to emotion as a property of the
      // Snackbar element. e.g. <Snackbar css={css`${<<theEmotionCssString>>}`}/> 
      emotionCssString: `
          color: red;
          .MuiSnackbar-root {
            color: red;
          }
          .MuiSnackbarContent-root {
            color: orange;
          }
          .MuiSnackbarContent-message {
            color: orange;
          }
          .MuiSnackbarContent-action {
            color: orange;
          }
          `,
      // component: a react component. Pass any react element as a child of the Snackbar.
      component: (
        <Alert
          severity={"error"}
          sx={{ backgroundColor: "primary.dark" }}
        >
          <div
            css={css`
              color: blue;
            `}
          >
            {`a simple mui-gotit snack `}
            {Math.random()}
          </div>
        </Alert>
      )
    }
}

returns the option containing the id of the notification, useful to close it later. E.g.

let handleOption = gotitContext.displayNotification({...});
gotitContext.removeNotification(handleOption.gotit.id);

gotitContext.removeNotification(notificationHandle)

Removes the notification. Pass in the return value of the displayNotification function.

gotitContext.removeNotificationGroup(notificationGroup)

Removes all the notification of the group.

let handleOption = gotitContext.displayNotification({
  // ...other code
  gotit: { group: 'myGroup'}
  // ...other code
});
gotitContext.removeNotification(handleOption.gotit.group);
// or
gotitContext.removeNotification('myGroup');

Hack on it

  1. Edit src/gotit-pragma-automatic.js
  2. Start the demo app in watch mode: npm run watch
  3. Run Jest and Cypress tests npm run test
  4. Build the library running npm run rollup
  5. Publish it on npm: npm publish

Sponsors

This project is used and sponsored by the Cardano platform https://141x.io. If you like it, send some ADA to the address addr1q9dta74g2axw39zf440w67vd0dyq7md4360q4cah3ev72q2hy6re7yfwwt4y246wh3r867l6sjnnlzwseug2t4jp97ps2ajcnh and text me to get listed as a sponsor.

1.1.1

3 years ago

1.1.0

3 years ago

1.0.91

3 years ago

1.0.94

3 years ago

1.0.93

3 years ago

1.0.92

3 years ago

1.0.90

3 years ago

1.0.89

3 years ago

1.0.88

3 years ago

1.0.87

3 years ago

1.0.86

3 years ago

1.0.85

3 years ago

1.0.84

3 years ago

1.0.83

3 years ago

1.0.82

3 years ago

1.0.81

3 years ago

1.0.79

3 years ago

1.0.78

3 years ago

1.0.77

3 years ago

1.0.76

3 years ago

1.0.75

3 years ago

1.0.74

3 years ago

1.0.73

3 years ago

1.0.72

3 years ago

1.0.71

3 years ago

1.0.70

3 years ago

1.0.69

3 years ago

1.0.68

3 years ago

1.0.67

3 years ago

1.0.66

3 years ago

1.0.65

3 years ago

1.0.64

3 years ago

1.0.63

3 years ago

1.0.62

3 years ago

1.0.61

3 years ago

1.0.60

3 years ago

1.0.59

3 years ago

1.0.58

3 years ago

1.0.57

3 years ago

1.0.56

3 years ago

1.0.55

3 years ago

1.0.54

3 years ago

1.0.52

3 years ago

1.0.51

3 years ago

1.0.50

3 years ago

1.0.48

3 years ago

1.0.47

3 years ago

1.0.46

3 years ago

1.0.45

3 years ago

1.0.44

3 years ago

1.0.43

3 years ago

1.0.41

3 years ago

1.0.40

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.0

3 years ago