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-hover-slideshow

v3.0.1

Published

Iterates through an image slideshow based on cursor position

Downloads

119

Readme

react-hover-slideshow

Downloads

Iterates through an image slideshow based on cursor/touch position.

Hover slideshow preview

Requirements

  • Node 10 and higher
  • React 16.8 and higher

Live demo

See the Storybook examples

Installation

react-hover-slideshow is distributed via npm which comes with Node. To install it as a dependency of your project, simply navigate to your project directory and run:

  • npm i --save react-hover-slideshow

Peer dependencies

This module also depends on your project already having react and prop-types as peer dependencies. If you don't yet have those, you can add them as dependencies of your project with this:

  • npm i --save react prop-types

  • ⚠️Warning!⚠️ - this code uses Hooks under the hood, which was introduced in React 16.8. This code will not work with older versions of React.

Usage

The simplest way to use react-hover-slideshow is to use the HoverSlideshow component:

import React from "react";
import { HoverSlideshow } from "react-hover-slideshow";

// As a function component
function MyFunctionComponent(props) {
	const imageURLs = [
		"https://example.com/1.jpg",
		"https://example.com/2.jpg",
		"https://example.com/3.jpg"
	];

	return (
		<div>
			<h3>My photo album</h3>
			<HoverSlideshow
				aria-label="My pretty picture slideshow"
				images={imageURLs}
				width="400px"
				height="300px"
			/>
		</div>
	);
}

// Or as a class component
class MyClassComponent extends React.Component {
	render() {
		const imageURLs = [
			"https://example.com/1.jpg",
			"https://example.com/2.jpg",
			"https://example.com/3.jpg"
		];

		return (
			<div>
				<h3>My photo album</h3>
				<HoverSlideshow
					aria-label="My pretty picture slideshow"
					images={imageURLs}
					width="400px"
					height="300px"
				/>
			</div>
		);
	}
}

Alternatively you can use HoverSlideshowAnimated with the same interface, which will give you a nice crossfade effect. Note that this animated variation depends on the peer dependency react-transition-group, which you can add you your project with the following:

  • npm i --save react-transition-group

Advanced usage with hooks

Behind the scenes, the helper components above use the hook useHoverSlideshow. If you wish for more control over the image component markup and CSS, I recommend making a copy of /src/HoverSlideshow.js and making this small change:

import React from "react";
import PropTypes from "prop-types";
-import useHoverSlideshow from "./useHoverSlideshow";
+import { useHoverSlideshow } from "react-hover-slideshow";

Then simply use that code as a baseline, and tweak what you like! Here's a quick guide of what's happening in the hook:

  • currentImage - determined by figuring out the user's "cursor progress" over an element, then finding the closest match in the images array passed in. For instance, if images contains two image hrefs, the first image will display until the user's cursor passes over 50% of the element (where 50% is defined horizontally by default).

  • updateHoverSlideshow - updates currentImage based on mousemove and touchmove events

  • resetHoverSlideshow - should be called when the user is no longer interacting with the element (mouseout or touchend). Resets by displaying the first image in the array.

Local development

  1. Clone this repo: git clone [email protected]:davidcalhoun/react-hover-slideshow.git
  2. Navigate to directory: cd react-hover-slideshow
  3. Install dev dependencies: npm i
  4. Check the scripts section in package.json for a list of available dev commands.