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

react-visible-image

v1.4.5

Published

Show only images that are in the viewport

Downloads

177

Readme

React Visible Image

React Visible Image is designed to solve the problem of loading full-sized images before they're seen on the screen, which in some cases could be not at all. It uses the the IntersectionObserver API to determine if a user has scrolled past the image or not.·

Report Bug · Request Feature

Table of Contents

Getting Started

To install React Visible Image into your React app, follow these steps.

Prerequisites

This is an example of how to list things you need to use the software and how to install them.

Installation

  1. Install NPM packages
npm install react-visible-image

Usage

Using the component

The main output of this package is a simple VisibleImage component that you can use as a drop-in replacement for normal <img /> tags. Any props that can be passed to <img /> can be passed to VisibleImage.

import { VisibleImage } from `react-visible-image`

const ContentBlock = () => (
    <figure>
        <VisibleImage src="path/to/image.jpg" alt="Image description" />
        <figcaption>An informative caption</figcaption>
    </figure>
);

| Prop | Type | Description | | ----------------------- | ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------- | | forceShow | boolean | If set to true, the image will always show. If set to false, either the initialSrc will be used or nothing will. | | loadingClassName | string | Specifies a class to apply to the VisibleImage before the image has loaded | | initialSrc | string | An image to use before the image appears on screen. Useful for animation, SEO purposes and to stop the page height jumping. | | onShown | () => void | Callback when the element becomes visible. Useful for keeping the visibility as part of the app state alongside forceShow | | onVisibilityChanged | (isVisible: boolean) => void | Callback when the element's visibility changes. Useful for keeping the visibility as part of the app state alongside forceShow |


Using the hook

There are cases where using an <img /> tag doesn't fit the purpose, for example when applying a background image to an element. For these cases, the hook can be applied.

import React, { useRef } from 'react'
import { useVisible } from `react-visible-image`

const ContentBlock = () => {
    const imageSet = {
        initial: '/path/to/small/image',
        full: '/path/to/full/image',
    }
    const ref = useRef()
    const visible = useVisible(ref)
    const image = visible ? imageSet.full : imageSet.initial;

    return (
        <div ref={ref} style={{backgroundImage: `url('${image}')`}}>
            {visible &&
                <p>Seen!</p>
            ||
                <p>Not seen...</p>
            }
        </div>
    );
}

Polyfill

If you need to support browsers that don't have the IntersectionObserver API, include the polyfill by installing it using:

npm install intersection-observer

Then add it to your app with:

import "intersection-observer";

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Josh Post - @posty72

Project Link: https://github.com/posty72/react-visible-image