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

@zensen/popup

v2.1.0

Published

A declarative popup stack for native web components

Downloads

8

Readme

zensen-popup

A declarative popup stack manager in LitElement.

Features

  • Provides a single manager element to handle all of your popups
  • Push multiple popups onto the stack at will
  • Provides multiple stacks for different classes of popups
  • Has an intuitive interface for pushing data to popups, and retrieving

Install

Using npm:

$ npm install @zensen/popup

Using yarn:

$ yarn add @zensen/popup

Configuring, Creating, and Registering Popup Components

Configuring the popup stack component by providing a redux store object

import { configure } from '@zensen/popup'

configure(store)

Create your own popup components by extending this package's Popup base class

import { Popup } from '@zensen/popup'
import { css, html } from 'lit-element'

class ConfirmPopup extends Popup {
  static get styles () {
    return [
      super.styles,
      css`
        *,
        *::before,
        *::after {
          box-sizing: border-box;
        }

        :host {
          width: 28rem;
          height: 8rem;
        }

        .text {
          margin: 0;
          padding: 0;
        }

        .text-title {
          font-size: 2.4rem;
        }

        .text-message {
          font-size: 1.6rem;
        }
      `,
    ]
  }

  constructor () {
    super()

    this.model = {
      title: '',
      message: '',
    }

    this.__handlers = {
      confirm: () => this.onClose(true),
      cancel: () => this.onClose(false),
    }
  }

  render () {
    return html`
      <p class="text text-title">${this.model.title}</p>
      <p class="text text-message">${this.model.message}</p>

      <button @click="${this.__handlers.confirm}">Confirm</button>
      <button @click="${this.__handlers.cancel}">Cancel</button>
    `
  }
}

window.customElements.define('x-popup-confirm', ConfirmPopup)

The next step is to create a map of popup renderer functions that will be used to register each type of popup component with the popup stack:

export const POPUP_CONFIRM = 'confirm'

export const RENDERER_POPUPS = {
  [POPUP_CONFIRM]: (hide, index, layout, model, closeHandler) => html`
    <x-popup-confirm
      .index="${index}"
      .layout="${layout}"
      .model="${model}"
      .onClose="${closeHandler}"
      ?hidden="${hide}"
    ></x-popup-confirm>
  `,
}

Then, we instantiate the popup stack component in our app, assigning our renderers to it.

NOTE: It's usually a good idea to put this towards the top of your app's DOM towards as the last child of its containing element.

import './popup-confirm'

import { LitElement, html, css } from 'lit-element'

export const POPUP_CONFIRM = 'confirm'

export const RENDERER_POPUPS = {
  [POPUP_CONFIRM]: (layout, model, closeHandler) => html`
    <x-popup-confirm
      .layout="${layout}"
      .model="${model}"
      .onClose="${closeHandler}"
    ></x-popup-confirm>
  `,
}

class App extends LitElement {
  static get styles () {
    return css`
      :host {
        display: block;
      }

      .container {
        display: flex;
        width: 100%;
        height: 100%;
      }
    `
  }

  render () {
    return html`
      <div class="container">
        <!-- PUT APP CONTENT HERE -->
      </div>

      <zen-popup-stack .renderers="${RENDERER_POPUPS}"></zen-popup-stack>
    `
  }
}

window.customElements.define('x-app', App)

API

We can open popups like so:

import { openPopup } from '@zensen/popup'

const result = await openPopup(POPUP_CONFIRM, {
  title: 'Welcome',
  message: 'Would you like some annoying emails?',
})

openPopup() returns a promise, so it can be awaited. It will resolve once the popup calls its this.onClose() callback. openPopup() will return whatever is passed into this.onClose(). In the case of our Confirm Popup, it will return true when the Confirm button is clicked, or false when the Cancel button is clicked.

We can also manually remove the last popup from the stack like so:

import { popPopup } from '@zensen/popup'

popPopup()

The entire popup stack can be cleared with a single command:

import { clearPopup } from '@zensen/popup'

clearPopups()

Advanced Usage

Each popup stack is associated with a stack array in the popup reducer for redux. Each popup stack must be associated with a array. This can be done by providing each instance of zen-popup-stack their own key property:

import '@zensen/popup'

<zen-popup-stack key="popups"></zen-popup-stack>
<zen-popup-stack key="overlays"></zen-popup-stack>
<zen-popup-stack key="banners"></zen-popup-stack>

By checking your redux state, you'll notice an array key under the popup reducer for each instance: popups, overlays, and banners. If a key isn't provided, then that instance's key defaults to main, so this works as well:


import '@zensen/popup'

<zen-popup-stack></zen-popup-stack>                <!-- 'main'     -->
<zen-popup-stack key="popups"></zen-popup-stack>   <!-- 'popups'   -->
<zen-popup-stack key="overlays"></zen-popup-stack> <!-- 'overlays' -->
<zen-popup-stack key="banners"></zen-popup-stack>  <!-- 'banners'  -->

This can be useful for apps that need multiple stacks for diverse roles.

If you want to render popups without the blocker, you can add the attribute hideBlocker:

import '@zensen/popup'

<zen-popup-stack hideBlocker></zen-popup-stack>

If you want to change the color of the blocker itself, you can attach a class to the popup stack and manipulate the --backdrop-color variable as needed:

import '@zensen/popup'

static get styles() {
  return css`
    .popup-stack {
      --backdrop-color: blue;
    }
  `
}

<zen-popup-stack class="popup-stack"></zen-popup-stack>