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

alpinejs-notify

v1.0.4

Published

Simple notifications in your projects using Alpine JS ๐Ÿ™‹โ€โ™€๏ธ

Downloads

455

Readme

Alpine JS Notifications

Simple notifications in your projects using Alpine JS ๐Ÿ™‹โ€โ™€๏ธ

Install

With a CDN

<script
  defer
  src="https://unpkg.com/alpinejs-notify@latest/dist/notifications.min.js"
></script>

<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>

With a Package Manager

yarn add -D alpinejs-notify

npm install -D alpinejs-notify
import Alpine from 'alpinejs'
import notifications from 'alpinejs-notify'

Alpine.plugin(notifications)

Alpine.start()

Example

Let's create a simple notification that appears in the top right of the page and disappears after 5s.

<div x-data>
  <div
    id="notificationWrapper"
    class="fixed top-4 w-64 right-4 space-y-2"
  ></div>

  <button
    x-on:click="$notify('Hello there, I am a notification!', {
    wrapperId: 'notificationWrapper',
    templateId: 'notificationAlert',
    autoClose: 3000,
    autoRemove: 4000
  })"
  >
    Notify
  </button>

  <template id="notificationAlert">
    <div role="alert" class="text-white bg-red-500 p-4">
      {notificationText}
    </div>
  </template>
</div>

Options

notificationText

This is the string that will be rendered in the notification.

It is not part of the {}

wrapperId

This is the wrapping element of the notification.

templateId

This is the notification component HTML that will be added to the wrapper.

templateFile

This is an alternative choice to templateId which will get the notification component HTML from the file specified.

autoClose

This will set the attribute data-notify-show to false once the duration (in milliseconds) is up.

autoRemove

Here you have two options.

Duration

If you use a duration (in milliseconds) then it will remove the notification from the DOM once duration is up.

This works for most situations, however it can get a little tricky calculating when the removal should happen when working with animations. This is what the boolean approach solves.

Boolean

Using a boolean will trigger the removal of the notification once the animation has ended. It will play the animation in full and then remove once complete.

classNames

A string of classes to add to the notification.

Default Options

You don't need to pass the same options for multiple notifications. If all your notifications are using the options from the example you can do this instead.

<script>
  window.notificationOptions = {
    wrapperId: 'notificationWrapper',
    templateId: 'notificationAlert',
    autoClose: 3000,
    autoRemove: 4000,
  }
</script>

Then all notifications that don't specify their own notificationOptions will use this.

Animating Notifications

In this example I'll be using Tailwind CSS, but you can easily replicate this with CSS.

Let's say you want the notification to slide in from the right and then slide out, you could do the following.

<template id="notificationAlert">
  <div
    role="alert"
    class="text-white bg-red-500 p-4 data-[notify-show=true]:animate-slide-in data-[notify-show=false]:animate-slide-out"
  >
    {notificationText}
  </div>
</template>

The animate-slide- classes have been added to the Tailwind CSS config.

module.exports = {
  theme: {
    extend: {
      animation: {
        'slide-in': 'slide-in 0.15s ease-in forwards',
        'slide-out': 'slide-out 0.15s ease-in forwards',
      },
      keyframes: {
        'slide-in': {
          '0%': { transform: 'translateX(100%)' },
          '100%': { transform: 'translateX(0)' },
        },
        'slide-out': {
          '0%': { transform: 'translateX(0)' },
          '100%': { transform: 'translateX(100%)' },
        },
      },
    },
  },
}

Dismiss Notification

If you want to have dismissible notifications you can add Alpine JS logic to your notification template.

<template id="notificationAlert">
  <div
    x-data
    role="alert"
    class="text-white bg-red-500 p-4 data-[notify-show=true]:animate-slide-in data-[notify-show=false]:animate-slide-out"
  >
    {notificationText}
    <button x-on:click="$root.setAttribute('data-notify-show', false)">
      Close
    </button>
  </div>
</template>

Using a File

If preferred, you can create HTML files for your notification templates.

<div id="notificationWrapper" class="fixed top-4 w-64 right-4 space-y-2"></div>

<button
  x-on:click="$notify('Hello there, I am a notification!', {
    wrapperId: 'notificationWrapper',
    templateFile: 'notification.html',
    autoClose: 3000,
    autoRemove: 4000
  })"
>
  Notify
</button>

This is now looking for a file called notification.html which might look something like this.

<div role="alert" class="text-white bg-red-500 p-4"> {notificationText} </div>

It works the exact same as templateId but it means you don't have <template> tags in your HTML for your notification templates.

Stats