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

uncloakjs

v1.1.9

Published

IntersectionObserver-based lazy loader and content revealer

Downloads

18

Readme

Uncloak

  • Uses IntersectionObserver API to reveal elements, lazy load content, and autoplay/pause videos as they scroll into view
  • Supports dynamically added elements
  • 5KB minified (inc. VideoPlayer)

How it works

  • Add data-uncloak-new to any elements you would like to be picked up by Uncloak
  • Create new instance of Uncloak - this will automatically query all elements with this attribute
  • Each element will be created as an UncloakItem (or UncloakVideoItem), and:
    • Its data-uncloak-new attribute will be removed (so it won't be picked up again)
    • Be added to a a global IntersectionObserver instance (attached to Uncloak), to be revealed when its node is visible in the viewport
    • Will query for any child elements to be lazy loaded (with data-uncloak-src, data-uncloak-srcset, data-uncloak-picture attributes) - in which case, will setup its own IntersectionObserver instance with a margin of 50% around the viewport
    • If it is an UncloakVideoItem, the video will autoplay once within 25% of the viewport (and paused if outside)
  • If you dynamically add anymore elements and want them to be picked up by Uncloak:
    • Make sure they have the data-uncloak-new attribute
    • Run uncloak.findNewItems() after they've been added to the DOM

TODO: UncloakVideoItem docs

Settings

All settings are optional, defaults are listed below:

new Uncloak({
  items = []; // an array of UncloakItem / UncloakVideoItem that are to be revealed
  
  // Sent to UncloakItem
  itemOptions = {
    delayTypes: {},
    callbacks: {}
  };
});

TODO: callbacks & delay types

Example

HTML:

<div class="uncloak uncloak--cloaked" data-uncloak-new>
  <img data-uncloak-src="image.jpg">
</div>

<div class="uncloak uncloak--cloaked" data-uncloak-new>
  <iframe data-uncloak-src="https://www.youtube.com/embed/G9KQfnqukno" frameborder="0"></iframe>
</div>

JS:

import Uncloak from 'path/to/uncloak.js';

new Uncloak();

CSS:

.uncloak {
  transition: opacity 0.2s;
}
.uncloak--cloaked {
  opacity: 0;
}

Example: Iframe with poster image

HTML:

<!-- both elements have the data-uncloak-src so will be lazy loaded with Uncloak -->
<div class="uncloak uncloak--cloaked embed" data-uncloak-new>
  <!-- could add an event listener to play iframe video & hide poster on click --> 
  <button class="embed__poster">
    <img data-uncloak-src="https://placekitten.com/1440/810">
  </button>
  <iframe class="embed__iframe" data-uncloak-src="https://www.youtube.com/embed/G9KQfnqukno" frameborder="0"></iframe>
</div>

JS:

import Uncloak from 'path/to/uncloak.js';

new Uncloak();

CSS:

.uncloak {
  transition: opacity 0.2s;
}
.uncloak--cloaked {
  opacity: 0;
}

.embed {
  position: relative;
}
.embed__iframe {
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  width: 100%;
}
.embed__poster {
  position: relative;
  z-index: 2;
}

Example: Lazy picture element

  • Mark the picture element with data-uncloak-picture
  • Don't add an <img> inside the picture to prevent loading
  • Add a fallback source (via data-uncloak-ie-src) to be loaded on the <img> in IE (which doesn't support <picture>)
  • Optionally, add some alt text (data-uncloak-alt) or classes (data-uncloak-class) to be added to the <img> when it's created

HTML:

<picture data-uncloak-picture data-uncloak-ie-src="src" data-uncloak-alt="Some alt text" data-uncloak-class="img-cls-1 img-cls-2">
  <source srcset="https://placekitten.com/1440/810" media="(min-width: 1024px)">
  <source srcset="https://placekitten.com/1920/1080" media="(min-width: 1440px)">
</picture>

Browser Support

Latest Edge/FF/Chrome/Safari/iOS - see IntersectionObserver support. If you need IE support, see this polyfill.