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

buttered-toast

v1.0.1

Published

An incredibly simple toast notification solution for React.

Downloads

5

Readme

buttered-toast

An incredibly simple toast notification system for React.

  • Only 1kb gzipped
  • No dependencies
  • No styling, only logic
  • No configuration
  • No nonsense
  • Render components
  • No JSX during usage

This is a solution I'm using in several projects, so I decided to extract this into its own package. The existing libraries out there require you to add JSX elements to your React tree and render them based on conditions (controlled way). I never liked such solutions, I prefer a simple function call to show my toast.

Installation

npm install buttered-toast

Usage

Add context provider

import { ToastContextProvider } from 'buttered-toast'

const App = ({ children }) => <ToastContextProvider>{children}</ToastContextProvider>

useToast hook

import { useToast } from 'buttered-toast'

const Component = () => {
    const { show } = useToast()
    return <button onClick={() => show(<>Button clicked!</>)}>Click me!</button>
}

API and Customization

Exports

  • ToastContextProvider
  • useToast
  • defaultOptions
  • ToastContext

ToastContextProvider

This is a React context provider that provides the state for useToast hooks. You must have this at least once in your React tree.

Props

  • options - An object of options to override the default options. See defaultOptions for more information.

useToast

This is a React hook that provides the show function to show a toast.

const { show } = useToast()

show

This function takes a two arguments argument. The content to show in the toast and an optional options object to override the options for this specific toast.

  • content - The content to show in the toast. This can be anything that React can render as children.
  • options (optional) - An object of options to override the default options. See defaultOptions for more information.
show(<>Button clicked!</>)
show(<MyToastStyle>Button clicked!</MyToastStyle>)

defaultOptions

An object of default options. These options can be overridden by passing an options object to the ToastContextProvider.

  • duration - The duration in milliseconds to show the toast. Defaults to 3000.
  • ref - A ref to the toast element. Defaults to null. In case a ref is provided, React's createPortal will be used to render the toast. If no ref is provided, the toast will be rendered in the ToastContextProvider.
  • Wrapper - A wrapper component to wrap the toast in. Defaults to and "empty component": ({ children }) => children.
  • wrapperProps - Props to pass to the wrapper component. Defaults to {}.

ToastContext

This is the React context that is provided by the ToastContextProvider. You can use this context to create your own.

Styling

This library does not provide any styling. You can style your toast by creating your own Toast component, and/or by styling your Wrapper element.

Advanced example

import { ToastContextProvider, defaultOptions } from 'buttered-toast'

const App = ({ children }) => (
    <ToastContextProvider
        options={{
            ...defaultOptions,
            duration: 5000,
            Wrapper: ({ children, title }) => (
                <div className="my-wrapper">
                    <div className="my-title">{title}</div>
                    {children}
                </div>
            ),
            wrapperProps: { title: 'System notification' }
        }}
    >
        {children}
    </ToastContextProvider>
)
import { useToast } from 'buttered-toast'

const Component = () => {
    const { show } = useToast()
    return (
        <button
            onClick={() =>
                show(<ToastStyle>Button clicked!</ToastStyle>, {
                    duration: 10000,
                    wrapperProps: { title: 'What just happened?' }
                })
            }
        >
            Click me!
        </button>
    )
}