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

hash-dialog-modal

v1.0.4

Published

Open and close <dialog> modals based on the URL hash value.

Downloads

10

Readme

hash-dialog-modal

Open and close <dialog> modals based on the URL hash value. A vanilla JavaScript library for all browsers and frameworks. Demo

https://user-images.githubusercontent.com/47051820/223364228-0893dfe1-ef83-4cda-833d-bdace9bb3069.mp4

Explanation

Assume a webpage with the following anchor elements.

<a href="#1">#1</a>
<a href="#2">#2</a>

The dialog will be opened if

  1. The page is initially loaded with the hash of #1 or #2.
  2. Hash change is triggered (e.g. clicking <a href="#1">).

The dialog will be not open if

  • The page is requested with #3, since there are no <a> elements with the href value of #3. The URL hash is trimmed as well.

The dialog will be closed if

  1. The Escape key is pressed (browser default).
  2. dialog.close() is called using JavaScript.

When the dialog is closed

  1. Hash is removed from the current URL using history.pushState().
  2. Corresponding <a> element is focused for keyboard accessibility.

When the dialog is opened/closed

  • The <body> can have its overflow automatically set to hidden/visible to remove scroll from the <body>. Reference options.

Usage

Import from jsDelivr - Vanilla JavaScript example

<a href="#1">#1</a>
<a href="#2">#2</a>

<dialog></dialog>

<script type="module">
  import { controlDialogWithUrlHash } from 'cdn-of-choice';

  // Select the dialog element.
  const dialog = document.querySelector('dialog');

  const updateDialogContent = () =>
    (dialog.textContent = `Current hash is '${window.location.hash}'`);

  updateDialogContent(); // Update the dialog content on initial load.

  // Dynamically update the dialog content based on the URL hash change.
  window.addEventListener('hashchange', updateDialogContent);

  // Pass the dialog element to the imported function.
  const { removeEventListeners } = controlDialogWithUrlHash(dialog, 'auto');

  // Remove all event listeners when needed.
  // - window.removeEventListener('hashchange', updateDialogContent);
  // - removeEventListeners();
</script>

Install from npm - Node.js and SvelteKit example

<script lang="ts">
  import { controlDialogWithUrlHash } from 'hash-dialog-modal';
  import { onMount } from 'svelte';

  let dialogElement: HTMLDialogElement;
  let dialogContent: string;

  const updateDialogContent = () =>
    (dialogContent = `Current hash is '${window.location.hash}'`);

  onMount(() => {
    updateDialogContent();
    const { removeEventListeners } = controlDialogWithUrlHash(
      dialogElement,
      'auto'
    );
    return removeEventListeners;
  });
</script>

<svelte:window on:hashchange={updateDialogContent} />

<a href="#1">#1</a>
<a href="#2">#2</a>

<dialog bind:this={dialogElement}>{dialogContent}</dialog>

Options

export declare const controlDialogWithUrlHash: (
  dialog: HTMLDialogElement,
  overflow: 'auto' | 'manual', // sets the overflow style of the <body>
  options?: Partial<{
    onHashRemoval: () => void; // runs on dialog close and hash removal
  }>
) => {
  removeEventListeners: () => void; // removes all event listeners
};

Advanced

What overflow auto is recommended.

// Remove scroll from the <body> when the <dialog> is opened as a modal.
if (overflow === 'auto') document.body.style.overflow = 'hidden'; // handleHash
if (overflow === 'auto') document.body.style.overflow = 'visible'; // closeDialog

Why onHashRemoval callback should be provided.

window.history.pushState(null, '', ' '); // Does not trigger on:hashchange event.
options.onHashRemoval?.(); // Therefore, the callback should be manually provided.