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

piral-notifications

v1.5.4

Published

Plugin for triggering notifications in Piral.

Downloads

5,210

Readme

Piral Logo

Piral Notifications · GitHub License npm version tested with jest Community Chat

This is a plugin that only has a peer dependency to piral-core. What piral-notifications brings to the table is a set of Pilet API extensions that can be used with piral or piral-core to show notifications triggered by pilets in your Piral instance.

Why and When

Quite often you'll want to show notifications (such as errors, special events, information material, etc.) in a non-obtrusive toast notification (or some other way). The piral-notifications plugin helps you to do exactly that. It provides a simple component that you can place in your layout. Together with your styling rules the notifications are then managed by the plugin. Each pilet can open as many notifications as it wants. Notifications may decay over time or stay on the screen until closed.

Alternatives: Browsers also allow to use the system's native notification API. This usually comes with the service worker/PWA modes, but could be used by pilets, too. Another way is to leave every pilet at defining its own notification system.

Video

We also have a video for this plugin:

@youtube

Documentation

The following functions are brought to the Pilet API.

showNotification()

Shows a notification inside the app shell. The notification can be permanent (to be closed by the user) or temporary (closes after a specified time).

Usage

::: summary: For pilet authors

You can use the showNotification function from the Pilet API to show a notification within the Piral instance.

Example use:

import { PiletApi } from '<name-of-piral-instance>';

export function setup(piral: PiletApi) {
  piral.showNotification('Hello from my sample pilet!', {
    type: 'info',
  });
}

:::

::: summary: For Piral instance developers

The provided library only brings API extensions for pilets to a Piral instance.

For the setup of the library itself you'll need to import createNotificationsApi from the piral-notifications package.

import { createNotificationsApi } from 'piral-notifications';

The integration looks like:

const instance = createInstance({
  // important part
  plugins: [createNotificationsApi()],
  // ...
});

Via the options the initially displayed messages can be defined. Additionally, the defaultOptions can be set up.

For example:

const instance = createInstance({
  // important part
  plugins: [createNotificationsApi({
    defaultOptions: {
      type: 'warning',
    },
    messages: [
      {
        content: 'Welcome to the future of digital services!',
        options: {
          title: 'Hello!',
          type: 'success',
        },
      },
    ],
  })],
  // ...
});

In order to host the toast notifications you'll need to embed the Notifications component somewhere in your layout.

As an example:

import { Notifications } from 'piral-notifications';

const MyLayout = ({ children }) => {
  <div>
    <Notifications />
    {children}
  </div>
};

If you want to customize the styling (which you should) make sure to register components such as NotificationsHost (shell for the notifications) or NotificationsToast (wrapper for an individual notification) via, e.g., <SetComponent name="NotificationsHost" component={MyNotificationsHost} />.

Customizing

You can customize the options available when showing another notification.

One way is to extend the standard set of options by interface merging the PiralCustomNotificationOptions interface:

import type {} from 'piral-notifications';

declare module 'piral-notifications/lib/types' {
  interface PiralCustomNotificationOptions {
    actions?: Array<'dismiss' | 'snooze'>
  }
}

// now showNotification("...", { actions: [] }) works, too

Another way is to extend the PiralCustomNotificationTypes interface. This allows you to bring in new types - incl. options that only apply for these types.

Example:

import type {} from 'piral-notifications';

declare module 'piral-notifications/lib/types' {
  interface PiralCustomNotificationTypes {
    question: {
      answers: Array<string>;
    };
  }
}

// now showNotification("...", { type: 'question', answers: ['Maybe', 'Definitely'] }) works, too

The notification types contain the available types as properties. Each property can have specific options set by their interface.

:::

License

Piral is released using the MIT license. For more information see the license file.