• Stars
    star
    130
  • Rank 277,575 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Local toast helps you to provide feedback related to particular components on page

react-local-toast

npm version npm downloads

Local toast helps you to provide feedback related to particular components on page

Demo

Features

  • Local toasts are linked to particular component in DOM.
  • Toast can be displayed on right/left/top/bottom side of component.
  • Toast can be hidden after some timout or hidden programatically.
  • Component might have multiple toasts.
  • Multiple toasts stucks vertically (even if displayed on left or right side).
  • info, success, warning, error and loading toasts out of the box.
  • You can bring your own design. Or your own Toast component. Or your custom implementation of toasts.
  • WAI-ARIA support.
  • TypeScript!

Documentation and showcase

Can be found here. Check it out, I spent a lot of effort making it 😅.

Installation

npm install react-local-toast --save
# Or if you prefer yarn
yarn add react-local-toast

Basic Usage

react-local-toast doesn't automatically inject its styles into DOM, you need to do that. In most cases it will be just:

import 'react-local-toast/dist/bundle.css';
// There is minified version too!
import 'react-local-toast/dist/bundle.min.css';

This should work fine for most of tools (Create React App included). For more specific use cases (e.g. using toasts in Shadow DOM) you might want to inject styles manually.

Now you need to wrap your application in LocalToastProvider.

import React from 'react';
import { LocalToastProvider } from 'react-local-toast';

export default () => {
    return (<LocalToastProvider>
        {/* All your components that will use local toasts should be children of this provider. */}
        <App />
    </LocalToastProvider>);
};

Local toasts are linked to particular components on page, so let's mark our component as target for local toast:

import React from 'react';
import { LocalToastTarget } from 'react-local-toast';

export const App = () => {
    return (<div>
        <p>This component should be inside LocalToastProvider</p>
        {/* Wrap your component with <LocalToastTarget> */}
        <LocalToastTarget name="btn">
            <button>Click me please!</button>
        </LocalToastTarget>
    </div>);
};

Local toast uses refs to get position of component, so in case you want to use toasts with functional components – make sure they are wrapped in React.forwardRef.

And final piece! Update your component to actually produce local toasts:

import React from 'react';
// New import here !!
import { LocalToastTarget, useLocalToast } from 'react-local-toast';

export const App = () => {
    // Use hook to show and hide toasts
    const {showToast, removeToast} = useLocalToast();

    return (<div>
        <p>This component should be inside LocalToastProvider</p>
        <LocalToastTarget name="btn">
            <button onClick={() => showToast('btn', 'Hello my first toast!')}>Click me please!</button>
        </LocalToastTarget>
    </div>);
};

In case you need to show toast from class component, you can use HOC like this:

import { LocalToastTarget, withLocalToast, LocalToastHocProps } from 'react-local-toast';

interface Props extends LocalToastHocProps {
    name: string
}

class ClassComp extends React.Component<Props, any> {
    sayHello = () => {
        this.props.showToast('class_comp', `Hello, ${this.props.name}!`)
    };
    render() {
        return (<div>
            <LocalToastTarget name='class_comp'>
                <button onClick={sayHello}>Say hello</button>
            </LocalToastTarget>
        </div>);
    }
}

// And later use thic component as you usually do
export default withLocalToast(ClassComp);

This will pass toast-related functions (exactly same as in useLocalToast hook) as props to wrapped component.

Cool, huh?

License

MIT

More Repositories

1

anori

Customizable new tab extension for Chrome, Firefox and Safari
TypeScript
322
star
2

Roselia-achievements

RenPy achievements module
Ren'Py
72
star
3

raindrop-telegram-bot

Allows you to easily share bookmarks from Raindrop.io in Telegram chats. As well as save links/photos/longreads from Telegram right into Raindrop.io. Join us, we have a nice 'reader mode' :)
Python
62
star
4

inject-react-anywhere

Inject React components into 3rd-party sites using ShadowDOM with ease (and without troubles with styles)! Particularly useful in browser extensions
TypeScript
44
star
5

webpack-react-web-extension-template

React TypeScript WebExtension template with batteries included
TypeScript
22
star
6

PyShiki

Python lib for working with http://shikimori.org/ api
Python
19
star
7

x-days-of-code

Сборник задач по программированию
18
star
8

wall-of-kanji

Procedurally generated wallpapers/posters with Japanese, Chinese or Korean characters
JavaScript
16
star
9

makyo

Modern frontend for LLMs (cloud & local) with node-based UI
TypeScript
9
star
10

obsidian-emera

🐉 Use React components and inline JavaScript directly in your Obsidian notes. Kinda like MDX.
TypeScript
9
star
11

react-redux-currency-exchanger

Handy tool for currency conversion
JavaScript
8
star
12

PomoApp

Tiny pomodoro app built with react
JavaScript
7
star
13

stylus-angled-edges

Port of SASS angled edges for Stylus
CSS
7
star
14

2ch-files-downloader

Download all files from thread at 2ch.hk
Python
4
star
15

resize-img

Console tool for image resize
Python
4
star
16

surek

Utility built on top of Docker Compose to make managing self-hosted services easier
TypeScript
3
star
17

bun-hono-drizzle-react-starter

My template for projects with Bun, Hono,Drizzle and OpenAPI/Scalar on the backend and React (through Vite) and end-to-end typing for API on the frontend.
TypeScript
3
star
18

AceConverter

Converter for fanfics from ficbook.net to fb2 or ePub books
Python
2
star
19

ExPy

Python
2
star
20

toki-dashboard

Cute dashboard to track your Toggl velocity
TypeScript
2
star