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

preact-intersection-observer

v2.3.6

Published

A lightweight Preact implementation of Intersection Observer API

Downloads

312

Readme

preact-intersection-observer

A lightweight Preact implementation of the Intersection Observer API that is fast and easy to use. With a gzipped size of less than 0.4kb, this package provides a simple way to detect when an element is within the viewport.

Installation

npm i preact-intersection-observer --save

Usage

useObserver

const [ref, inView, entry] = useObserver(options);

The useObserver hook allows you to observe an element's visibility within the viewport. It takes an optional options object, but ref must reference the element that you want to observe. inView is a boolean that indicates whether the element is within the viewport, and entry returns the IntersectionObserverEntry object. Both inView and entry will update when the element enters or exits the viewport.

ViewportObserver

  <ViewportObserver
    as="section"
    options={options}
    render={({ inView, entry }) => (...)}
  />

The ViewportObserver component is similar to the useObserver hook, but it is typically used when multiple observers are needed within the same component. It also takes an optional options object and a render prop that returns the inView, and entry values, as well as an as prop that allows you to specify the HTML element that will be used as the wrapper, it defaults to div.

API

Options

The options object is used in both the useObserver hook, and the ViewportObserver component.

| Name | Type | Default | Description | | ------------- | --------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | rootMargin | string | 0px | Allows you to grow or shrink the area around the root element's bounding box, specified in the same format as the CSS margin property. | | threshold | number | 0.0 | The target visibility percentage in view before triggering. The value can range from 0 to 1, with 0 meaning that even a single visible pixel is sufficient, 0.25 representing 25% visibility, and 1 representing 100% visibility. | | triggerOnce | boolean | false | If true, the observer will trigger only once. | | defaultInView | boolean | false | Specifies if the element defaults to being in view or not. This can be useful if you want an element to be visible at first, but then disappear when it goes out of view. |

ViewportObserver props

| Name | Type | Default | Description | | ------ | ---------------------------------------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | as | string | div | This prop allows you to specify the type of HTML element that will be used to wrap the content passed to the ViewportObserver component. By default, it will be a div element, but you can change it to any valid HTML element, such as a section, article, or span. | | render | ({inView, entry}) => ComponentChildren | undefined | This prop is a function that receives an object containing the inView boolean, and the root entry of the container. It expects that you return a component that will be rendered by the ViewportObserver. |

Examples

JavaScript example

import { h } from "preact";
import { useObserver, ViewportObserver } from "preact-intersection-observer";

// useObserver hook
export const Example = () => {
  const [ref, inView, entry] = useObserver();

  return <div ref={ref}>...</div>;
};

// ViewportObserver
export const Example2 = () => (
  <ViewportObserver
    render={({ inView, entry }) => (...)}
  />
);

TypeScript example

import { FunctionalComponent, h } from "preact";
import { useObserver, ViewportObserver } from "preact-intersection-observer";

// useObserver hook
export const Example: FunctionalComponent = () => {
  const [ref, inView, entry] = useObserver<HTMLDivElement>();

  return <div ref={ref}>...</div>;
};

// ViewportObserver
export const Example2: FunctionalComponent = () => (
  <ViewportObserver
    render={({ inView, entry }) => (...)}
  />
);