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

blocking-elements

v0.1.1

Published

A polyfill for the proposed blocking elements stack API

Downloads

40,492

Readme

Build Status Published on npm

Blocking Elements stack API

Implementation of proposal https://github.com/whatwg/html/issues/897

The polyfill chooses a non-colliding name (document.$blockingElements instead of document.blockingElements) as the proposal is still work in progress and hasn't yet reached consensus on the semantics and functionality (see this discussion for more details).

document.$blockingElements manages a stack of elements that inert the interaction outside them.

  • the stack can be updated with the methods push(elem), remove(elem), pop()
  • the top element (document.$blockingElements.top) and its subtree is the interactive part of the document
  • has(elem) returns if the element is a blocking element

This polyfill will:

  • search for the path of the element to block up to document.body
  • set inert to all the siblings of each parent, skipping the parents and the element's distributed content (if any)

Use this polyfill together with the wicg-inert polyfill to disable interactions on the rest of the document. See the demo page as an example.

Why not listening to events that trigger focus change?

Another approach could be to listen for events that trigger focus change (e.g. focus, blur, keydown) and prevent those if focus moves out of the blocking element.

Wrapping the focus requires to find all the focusable nodes within the top blocking element, eventually sort by tabindex, in order to find first and last focusable node.

This approach doesn't allow the focus to move outside the window (e.g. to the browser's url bar, dev console if opened, etc.), and is less robust when used with assistive technology (e.g. android talkback allows to move focus with swipe on screen, Apple Voiceover allows to move focus with special keyboard combinations).

Install & use

Blocking Elements relies on the inert attribute and uses Set objects, so make sure to include their polyfills as needed.

npm install --save babel-polyfill
npm install --save wicg-inert
npm install --save blocking-elements
<script src="./node_modules/babel-polyfill/dist/polyfill.min.js"></script>
<script src="./node_modules/wicg-inert/dist/inert.min.js"></script>
<script src="./node_modules/blocking-elements/dist/blocking-elements.min.js"></script>

<div id="container">
  <button onclick="makeBlocking(container)">make blocking</button>
  <button onclick="undoBlocking(container)">undo blocking</button>
</div>

<button>some button</button>

<script>
  function makeBlocking(element) {
    document.$blockingElements.push(element);
  }
  function undoBlocking(element) {
    document.$blockingElements.remove(element);
  }
</script>

Files

Two scripts are included:

  1. /dist/blocking-elements.min.js: minified and transpiled to ES5.

  2. /dist/blocking-elements.js: un-minified ES2017.

    If your toolchain supports Node-style module resolution (e.g. TypeScript's --moduleResolution=node), then the main blocking-elements bare module specifier resolves to this file. TypeScript declarations are also included for this module:

    import {DocumentWithBlockingElements} from 'blocking-elements';
    
    const blockingElements =
        (document as DocumentWithBlockingElements).$blockingElements;
    
    blockingElements.push(...);
    blockingElements.remove(...);

Local development

Install the dependencies with npm install and serve the resources.

Run the tests locally by navigating to http://localhost:8080/test/

Performance

Performance is dependent on the inert polyfill performance. Chrome recently landed the inert attribute implementation behind a flag.

Let's compare the how long it takes to toggle the deepest x-trap-focus inside nested x-b of the demo page (http://localhost:8080/demo/ce.html?ce=v1)

results.

document.$blockingElements with native inert is ~15x faster than polyfilled inert 🎉 🎉 🎉

| with polyfilled inert (M58) | with native inert (M60) | |----------|--------| | polyfill-inert-1.png | native-inert-1.png | | polyfill-inert-2.png | native-inert-2.png | | polyfill-inert-3.png | native-inert-3.png | | polyfill-inert-4.png | native-inert-4.png |