0.2.12 • Published 25 days ago
buttoned-toaster v0.2.12
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
key | type | desc | required | default |
---|---|---|---|---|
toastId | string | set this if you want this toast to be exclusive (ie, only one of this toast will exist at a time) | n | auto-generated id |
duration | num | in millis (infinite = 1661) | n | 3000 if not buttoned, inifinite if buttoned |
heading | string | if you want the toast to have a heading | n | none |
message | string | text for toast body | n | 'Toast' |
position | string | import POSITIONS to access possible values | n | top-center |
wide | bool | to get a double-wide toast | n | false |
approveFunc(id) | function | called when user clicks approve, should take toast id as a parameter, should handle toast.dismiss | required if you want a two-buttoned toast | none |
dismissFunc(id) | function | called when user clicks dismiss, should take toast id as a parameter, should handle toast.dismiss | n | dismiss() |
approveTxt | string | text for approve button | n | 'OK' |
dismissTxt | string | text for dismiss button | no | 'Cancel' |
canHide | bool | true if you want 'don't show again' functionality | n | false |
dontShowType | string | will be saved to localStorage, along with user choice, if 'don't show again' is chosen | required if canHide is true | none |
captureDontShow | function | optional, if you want to override the default 'don't show again' functionality | n | if 'don't show again' is chosen, saves type and choice in localStorage - developer is responsible for what to do with that info |
icon | string | path to assign to img.src of toast icon | n | depending on variant (success, error, warn, none) |
moreOptions | array | array of objects representing multiple options, instead of approveFunc only {handler: (function), btnText: 'string', btnType: 'string'} | n | none |
Toaster props
prop | type | desc | required | default |
---|---|---|---|---|
position | string | default position for all toasts, will be overridden if an individual toast includes position property | n | top-center |
duration | num | default for all toasts, will be overridden if an individual toast includes duration property | n | 3000 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
Author
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