npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@notifierjs/react

v1.1.5

Published

@notifierjs/core binding for React

Downloads

6

Readme

@notifierjs/react

@notifierjs/core binding for react

Table of contents

Installation

@notifierjs/react required @notifierjs/core as a peer dependency, make sure to install it.

Using npm:

npm install @notifierjs/core @notifierjs/react

Using yarn:

yarn add @notifierjs/core @notifierjs/react

Usage

First of all we need to create a Notifier instance. For that use useCreateNotifier hook.

import { useCreateNotifier } from '@notifierjs/react';

const notifier = useCreateNotifier();

Next you should wrap your app(or part of it) with NotifierProvider and pass Notifier instance to it.

import { NotifierProvider, useCreateNotifier } from '@notifierjs/react';

import { App } from './App';

export const App = () => {
  const notifier = useCreateNotifier<string>();

  return (
    <NotifierProvider value={notifier}>
      <App />
    </NotifierProvider>
  );
};

Then create your notifications render container, where your notifications will be displayed, you can get notifier instance passed to provider by using useNotifier hook. For handling timer here we use NotifierTimer component:

import { useNotifier, NotifierTimer } from '@notifierjs/react';

import { Notification } from './Notification';

export const NotificationsContainer = () => {
  const notifier = useNotifier<string>();

  return (
    <>
      {notifier.notifications.map((notification) => (
        <NotifierTimer notification={notification}>
          {(time) => (
            <Notification
              onClose={() => notifier.remove(notification.id)}
              onHover={() => notification.info.timer?.pause()}
              onBlur={() => notification.info.timer?.start()}
              initialTime={notification.options.autoRemoveTimeout}
              time={time}
            >
              {notification.payload}
            </Notification>
          )}
        </NotifierTimer>
      ))}
    </>
  );
};

Now you only need to add notification and you're done!

notifier.add({ id: 1, payload: 'Lorem ipsum' });

See complete working example here

Examples

Check out examples folder for more complete examples!

State managers

Check this exmaples to see how to connect @notifierjs/core with mobx state manager.

Hooks

useCreateNotifier

Create memoized instance of Notifier interface.

Can receive optional object with options relative to Notifier

import { useCreateNotifier } from '@notifierjs/react';

export const App = () => {
  const notifier = useCreateNotifier();

  return (
    // ...
  );
};

useNotifierContext

Returns current context value.

import { useNotifierContext } from '@notifierjs/react';

export const App = () => {
  const notifier = useNotifierContext();

  return (
    // ...
  );
};

useNotifier

Returns binded to react Notifier instance.

import { useNotifier, NotifierTimer } from '@notifierjs/react';

import { Notification } from './Notification';

export const NotificationsContainer = () => {
  const notifier = useNotifier();

  return (
    <>
      {notifier.notifications.map((notification) => (
        <NotifierTimer notification={notification}>
          {(time) => (
            <Notification
              onClose={() => notifier.remove(notification.id)}
              onHover={() => notification.info.timer?.pause()}
              onBlur={() => notification.info.timer?.start()}
              initialTime={notification.options.autoRemoveTimeout}
              time={time}
            >
              {notification.payload}
            </Notification>
          )}
        </NotifierTimer>
      ))}
    </>
  );
};

useNotifierTimer

Receiving notification with a timer as input, returns its binded to react value.

import { LaunchedNotification } from '@notifierjs/core';
import { useNotifierTimer } from '@notifierjs/react';

interface Props<Payload> {
  children: (time?: number) => React.ReactElement;
  notification: LaunchedNotification<Payload>;
}

export const NotifierTimer = <Payload,>({
  children,
  notification,
}: Props<Payload>): React.ReactElement => {
  const time = useNotifierTimer(notification);

  return (
    // ...
  )
};

Components

NotifierTimer

Component that receive notification and return render prop with timer that you can use to create notification progress bar.

Props:

| Name | Type | Required | Default | | -------------- | ---------------------------------------------------------------------- | -------- | ------- | | notification | LaunchedNotification<Payload>[] | yes | — |

import { useNotifier, NotifierTimer } from '@notifierjs/react';

import { Notification } from './Notification';

export const NotificationsContainer = () => {
  const notifier = useNotifier<string>();

  return (
    <>
      {notifier.notifications.map((notification) => (
        <NotifierTimer notification={notification}>
          {(time) => (
            <Notification
              onClose={() => notifier.remove(notification.id)}
              onHover={() => notification.info.timer?.pause()}
              onBlur={() => notification.info.timer?.start()}
              initialTime={notification.options.autoRemoveTimeout}
              time={time}
            >
              {notification.payload}
            </Notification>
          )}
        </NotifierTimer>
      ))}
    </>
  );
};

withNotifierTimer

Same as NotifierTimer but in HOC format. To use it your component should receive time prop with type number | undefined.

Props:

| Name | Type | Required | Default | | -------------- | ---------------------------------------------------------------------- | -------- | ------- | | notification | LaunchedNotification<Payload>[] | yes | — |

And other props that your wrapped component receive.

import { useNotifier, withNotifierTimer } from '@notifierjs/react';

import { Notification } from './Notification';

const TimerNotification = withNotifierTimer(Notification);

export const NotificationsContainer = () => {
  const notifier = useNotifier<string>();

  return (
    <>
      {notifier.notifications.map((notification) => (
        <TimerNotification
          notification={notification}
          onClose={() => notifier.remove(notification.id)}
          onHover={() => notification.info.timer?.pause()}
          onBlur={() => notification.info.timer?.start()}
          initialTime={notification.options.autoRemoveTimeout}
        >
          {notification.payload}
        </TimerNotification>
      ))}
    </>
  );
};

Maintaining

Scripts

List of available scripts to run:

  • build — build library
  • typecheck — run typescript to check types
  • test — run all tests
  • test:watch — run all tests in watch mode
  • test:coverage — run all tests with code coverage output