0.1.3 • Published 2 years ago

use-toast-notification v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

📦 use-toast-notification

React hooks library to display notification toast.

NPM npm

Install

npm install use-toast-notification

# or

yarn add use-toast-notification

Usage

Simple Usage

import React from 'react'
import NotificationProvider, { useNotification } from 'use-toast-notification'

const App = () => {
  const notification = useNotification()

  const handleSubmit = async (e) => {
    try {
        notification.show({
            message: 'Your delivery is on its way', 
            title: 'Delivery Status',
            variant: 'success'
        })
    } catch(e){
        notification.show({
            message: 'Your delivery could not be processed', 
            title: 'Delivery Status',
            variant: 'error'
        })
    }
  }

  return (
    <div>
        <form onSubmit={handleSubmit}>
            <input name='address'/>
            <button type='submit'>Submit</button>
        </form>
    </div>
  )
}


const Main = () =>{
    return (
        <NotificationProvider
			config={{
				position: 'top-right',
				isCloseable: false,
				showTitle: true,
				showIcon: true,
				duration: 5,
			}}
		>
            <App/>
        </NotificationProvider>
    )
}


export default Main
NameTypeDefaultDescription
titleStringnotifyTitle of the notification
messageReactNodeThe body of the notification required
durationNumberconfig.durationThe duration in seconds

⚙️ Config

You can customize your notification toast

all configurations are optional

import React from 'react'
import NotificationProvider from 'use-toast-notification'

const App = () => {
  ...
}

const Main = () =>{
    return (
        <NotificationProvider
			config={{
				position: 'center',
				showClose: true,
				showIcon: false,
				showTitle: true,
				duration: 30, 
			}}
            overrideStyles={{
				cardContainer: {
                    ...
                },
                ...
			}}
		>
            <App/>
        </NotificationProvider>
    )
}

export default Main

Available Config Properties

NameTypeDefaultDescription
positionString'top-right'The position of the card
durationNumber5The duration in seconds
animationDurationNumber300The animation duration in milliseconds
isCloseableBooleanfalseToggle close button
showIconBooleantrueToggle card heading icon
showTitleBooleantrueToggle card heading (icon and title) button
closeIconReactNodeCustom close icon
successIconReactNodeCustom success icon
errorIconReactNodeCustom error icon
infoIconReactNodeCustom info icon
errorColorstring'red'color for the error variant if no custom Icon
successColorstring'green'color for the success variant if no custom Icon
infoColorstring'blue'color for the info variant if no custom Icon

position is one of 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'top-center' | 'bottom-center' | 'center';

Available overrideStyles Properties

NameTypeDescription
containerCSSPropertiesThe style for the wrapper for all notification boxes
cardContainerCSSPropertiesThe style for the container for a single notification box
cardContentCSSPropertiesThe style for the content for a single notification
iconCSSPropertiesThe icon sizes
closeButtonCSSPropertiesThe close button style
cardMessageCSSPropertiesThe style for the message of the notification
cardIconCSSPropertiesThe style for the icons container
cardTitleCSSPropertiesThe style for the title of the notification
animateInCSSPropertiesThe animation properties for displaying the notification
animateOutCSSPropertiesThe animation properties for removing the notification

License

MIT © thelamina