npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-mui-snackbar-helper

v1.0.1

Published

An easier way to display SnackBars, using ContextApi and Hooks

Downloads

4

Readme

MUI Snackbar Helper

This library allows you to use Snackbar (from mui)imperatively and easily accros components. Imagine you ahve a web app with +20 pages, then you have to setup the snackbar component in each one and add multiple states to manage the opening/closing, position, message, title ... of the snackbar, or use a state with an object conatining all the props for the snackbar, not to mention pasing down callbacks to subcomponents to show the snackbar upon an action. So I created this library to simplify all this using Context API and Hooks, and you simply can control the snackbar imepartively from any component.

Installation

npm install react-mui-snackbar-helper

Setup

The Library exports SnackbarProvider, you can wrap the app with the provider then you can use it from any page or any component. alternatively you can wrap a certain portion only, for example; a certain route or subpage.

root.render(

    <React.StrictMode>
        <SnackbarProvider>
          <App />
        </SnackbarProvider>
    </React.StrictMode>

);

Usage

Hooks

  • You can use the useSnackbar Hook, it returns the following:
const {
 // helper methods start
    showMessage,
    showErrorMessage,
    showSuccessMessage,
    showWarningMessage,
    showInfoMessage,
 // helper methods end
 
    snackbarRef //ref to the AlertSnackbar component
} = useSnackbar();
  • you can use the helper methods as follows:
/**
 *	@param message:React.ReactNode
 *. @param title:React.ReactNode
 *  @param timeout:number @default(5000)  im millisecond
 */
showSuccessMessage("This is a success Message", "Success Title",2000)
showWarningMessage("This is a warning Message", "Warning Title",3000)
showInfoMessage("This is a info Message", "Info Title")
showErrorMessage("This is a error Message", "Error Title")
  • Alternatively, if you want to show message with severity from a variable you can use the showMessage helper method that takes severity as the first argument instead of using switch case with the explicit helper methods.
/**
 *	@param severity:AlertColor
 *	@param message:React.ReactNode
 *. @param title:React.ReactNode
 *  @param timeout:number @default(5000)  im millisecond
 */
showMessage("success","This is a success Message", "Success Title",2000)
showMessage("warning","This is a warning Message", "Warning Title",3000)
showMessage("info","This is a info Message", "Info Title")
showMessage("error","This is a error Message", "Error Title")

Using The Ref

snackbarRef.current?.setAlertData(
    {
        severity: 'success',
        message: "This is a success Message",
        timeout: 3000,
        title: "Success Title",
    }
)
snackbarRef.current?.open()

HOCs

The library also export SnackbarConsumer

<SnackbarConsumer>
    ({    
        showMessage,
        showErrorMessage,
        showSuccessMessage,
        showWarningMessage,
        showInfoMessage
        snackbarRef 
    })=> (
        <></>
    )
</SnackbarConsumer>

Customization

  • Any Customization to the material theme will affect the Snackbar.

  • You can use the snackbarRef to call setter methods that change the styling and the positioning.

  • You can pass props to the SnackbarProvider

Mui Snackbar Component Props

    snackbarRef?.current?.setSnackbarProps(...)

Mui Alert Component Props

    snackbarRef?.current?.setAlertProps(...)

Mui AlertTitle Component Props

    snackbarRef?.current?.setAlertTitleProps(...)