0.2.12 • Published 25 days ago

buttoned-toaster v0.2.12

Weekly downloads
-
License
ISC
Repository
-
Last release
25 days ago

buttoned-toaster

In development: Simple react component library for toasts with buttons

Usage

Use ToastRack component somewhere near the top of your app:

import { ToastRack } from 'buttoned-toaster';

const App = () => {
    return(
        <div>
            <AwesomeComponents>
            <ToastRack
                position='top-left'
                duration={5000}>
        </div>
    )
}

Then use the useToastRack hook in any function component to fire and dismiss toasts

import { useToastRack } from 'buttoned-toaster'

const RegularComponent = () => {
    const toast = useToastRack();
    const [lastToastId, setLastToastId] = useState();
    const toastHandler = () => {
        const id = toast.fire({
            message: "I did it!",
            duration: 30000
        });
        setLastToastId(id);
    }

    const dismissToast = () => {
        toast.dismiss(lastToastId);
    }
    return(
        <div>
            <button onClick={toastHandler}>Toast</button>
            <button onClick={dismissToast}>Dismiss</button>
        </div>
    )
}

You can also use the default import, which won't update on every render like the useToastRack hook

import toast from 'buttoned-toaster';

...
    toast.fire({message: "fire anywhere", toastId: "unique_toast"})
...

Include buttons & don't show again

const handler = () => {
    const doThing = (id, dontshow) => {
        ///
        if(id)
            toast.dismiss(id)
    }
    const dontDoThing = (id, dontshow) => {
        ///
        if(id)
            toast.dismiss(id)
    }
    const choiceMade = await toast.dontShow('MY_KEY');

    if(choiceMade){
        choiceMade.choice ? doThing() : dontDoThing()
    }
    toast.warn({
        message: "Are you sure",
        approveFunc:doThing,
        dismissFunc:dontDoThing,
        approveTxt: "Do the thing",
        canHide: true,
        dontShowType: 'MY_KEY'
    })
}

return <button onClick={handler}>Buttoned Toaster</button>

Toast options

keytypedescrequireddefault
toastIdstringset this if you want this toast to be exclusive (ie, only one of this toast will exist at a time)nauto-generated id
durationnumin millis (infinite = 1661)n3000 if not buttoned, inifinite if buttoned
headingstringif you want the toast to have a headingnnone
messagestringtext for toast bodyn'Toast'
positionstringimport POSITIONS to access possible valuesntop-center
wideboolto get a double-wide toastnfalse
approveFunc(id)functioncalled when user clicks approve, should take toast id as a parameter, should handle toast.dismissrequired if you want a two-buttoned toastnone
dismissFunc(id)functioncalled when user clicks dismiss, should take toast id as a parameter, should handle toast.dismissndismiss()
approveTxtstringtext for approve buttonn'OK'
dismissTxtstringtext for dismiss buttonno'Cancel'
canHidebooltrue if you want 'don't show again' functionalitynfalse
dontShowTypestringwill be saved to localStorage, along with user choice, if 'don't show again' is chosenrequired if canHide is truenone
captureDontShowfunctionoptional, if you want to override the default 'don't show again' functionalitynif 'don't show again' is chosen, saves type and choice in localStorage - developer is responsible for what to do with that info
iconstringpath to assign to img.src of toast iconndepending on variant (success, error, warn, none)
moreOptionsarrayarray of objects representing multiple options, instead of approveFunc only {handler: (function), btnText: 'string', btnType: 'string'}nnone

Toaster props

proptypedescrequireddefault
positionstringdefault position for all toasts, will be overridden if an individual toast includes position propertyntop-center
durationnumdefault for all toasts, will be overridden if an individual toast includes duration propertyn3000 if not buttoned, inifinite if buttoned

Building and running demo on localhost (if cloned from github)

First install dependencies:

npm install

To build the libary component:

npm run prepublish

To run demo app:

npm run start

Dependencies

uuid

Author

arfi720

0.2.12

25 days ago

0.2.11

25 days ago

0.2.10

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago