2.0.3 • Published 3 years ago
toast-lib-react v2.0.3
toast-lib-react allows you to create notification in 5 steps
Installation
npm i toast-lib-react
yarn add toast-lib-react
Features
- Settings of toast place
- Settings of toast appearance animation
- Settings of toast title
- Settings of toast message
- Settings of toast type
- Settings of autoClose ability and time
Example
import { ToastPortal, useToastRef } from 'toast-lib-react'
const SuccessSettings = {
title: 'Success',
mode: 'success',
message: 'This is example of success toast',
animationType: 'move',
position: 'topRight',
}
const PortalSettings = {
autoClose: false,
autoCloseTime: 3000,
position: 'topRight',
margin: 'none',
}
export const Example = () => {
const { toastRef, addToast } = useToastRef()
const addSuccess = () => addToast({
...SuccessSettings })
return (
<>
<button onClick={addSuccess}> Test Success Toast</button>
<ToastPortal ref={toastRef} {...PortalSettings} />
</>
)
}
Steps to create a Toast
- Import functions from the library
import { ToastPortal, useToastREf } from 'toast-lib-react'
- Create objects with settings for Toast and ToastPortal
const ToastSettings = {
title: 'Success',
mode: 'success',
message: 'This is example of success toast',
animationType: 'move',
position: 'topRight',
}
const PortalSettings = {
autoClose: false,
autoCloseTime: 3000,
position: 'topRight',
margin: 'none',
}
- Distract
toastRef
andaddToast
fromuseToastRef()
inside your component
const { toastRef, addToast } = useToastRef()
- Create event handler with Toast settings
ToastSettings
and call the Toast
const addSuccess = () => addToast({
...DefaultSuccessProperties })
<button onClick={addSuccess}> Test Success Toast </button>
- Place
ToastPortal
withuseToastRef
andPortalSettings
return (
<>
<button onClick={addSuccess}>
Test Success Toast
</button>
<ToastPortal
ref={toastRef}
{...PortalSettings} />
</>
)
Available settings
Toast:
title
- any textmode
- 'info', 'success', 'warning', 'error'message
- any text'backgroundColor
- stringanimationType
- 'fade', 'scale', 'move'position
- 'topRight', 'topLeft', 'bottomRight', bottomLeft'
TaostPortal:
autoClose
- booleanautoCloseTime
- numberposition
- 'topRight', 'topLeft', 'bottomRight', bottomLeft'margin
- 'none', 'small'(5px), 'medium'(10px), 'large'(20px)