1.3.2 • Published 3 years ago

react-visible-image v1.3.2

Weekly downloads
4
License
MIT
Repository
github
Last release
3 years ago

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>
);
PropTypeDescription
forceShowbooleanIf set to true, the image will always show. If set to false, either the initialSrc will be used or nothing will.
loadingClassNamestringSpecifies a class to apply to the VisibleImage before the image has loaded
initialSrcstringAn image to use before the image appears on screen. Useful for animation, SEO purposes and to stop the page height jumping.
onShown() => voidCallback when the element becomes visible. Useful for keeping the visibility as part of the app state alongside forceShow
onVisibilityChanged(isVisible: boolean) => voidCallback 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";

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/amazing-feature)
  3. Commit your Changes (git commit)
  4. Push to the Branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

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

Contact

Josh Post - @posty72

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

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.6.0

6 years ago

0.5.2

6 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.10

7 years ago

0.1.9

7 years ago