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

@dashdot/react-hooks-slider

v0.0.6

Published

A headless, customizable slider for React using hooks.

Downloads

11

Readme

Dashdot React Hooks Slider

@dashdot/react-hooks-slider

NPM Version GitHub release GitHub license GitHub pull-requests GitHub issues


A headless, customizable slider for React using hooks.

Getting Started

To get started, add @dashdot/react-hooks-slider to your project:

Using npm:

npm install --save @dashdot/react-hooks-slider

Using yarn:

yarn add @dashdot/react-hooks-slider

Please note that @dashdot/react-hooks-slider requires react@^16.0.0 as a peer dependency.

Usage

The package exports a single hook, useSlider, which can be used to control a slider. The hook returns an object containing various state values and functions for controlling the slider. The only requirements for using the hook are a container element and an array of slide elements, which are passed as refs.

const {
    /* Basic usage */
    activeSlide,
    totalSlides,
    position, // returns the position using percentage
    isDisabled, // happens when slides are smaller than container
    hasPreviousSlide,
    hasNextSlide,

    /* Handlers */
    handleNextSlide, // go to next slide
    handlePreviousSlide, // go to previous slide
    handleSlideSelect, // navigate to specific slide

    /* Swiping (when using framer-motion) */
    isSwiping,
    swipingProps

    /* Refs */
    containerRef,
    slideRefs,
} = useSlider(
    initialSlideIndex, // defaults to 0
    autoplaySpeed, // time in ms to go to next slide. Defaults to 0 = inactive
    slidesAreRendered, // passes if the slides are rendered. This fixes layout issues with conditional rendering. Defaults to true
}

Examples

Basic usage

import { useSlider } from '@dashdot/react-hooks-slider';


const {
    position,
    containerRef,
    slideRefs,
} = useSlider();

return (
    <div ref={containerRef}
        style={{
            whiteSpace: 'nowrap',
            display: 'flex',
            flexWrap: 'nowrap',
            width: '100%',
        }}
        >
        {slides.map((slide, index) => (
            <div
                key={slide.id}
                ref={(ref) => { slideRefs.current[index] = ref }}
                style={{
                    x: position,
                    width: '25rem',
                    height: '25rem',
                }}
            >
                {slide.content}
            </div>
        ))}
    </div>
);

3rd party animation / styling

You can use any sort of animation or styling. The example below uses tailwind and framer-motion.

const {
    position,
    hasPreviousSlide,
    hasNextSlide,
    handleNextSlide,
    handlePreviousSlide,
    slideRefs,
    containerRef,
    isDisabled,
    swipingProps,
} = useCarousel()

return (
    <motion.div
        ref={containerRef}
        animate={{ x: isDisabled || position === null ? 0 : `${position}%` }}
        transition={{ duration: 0.4, ease: 'easeOut' }}
        className="whitespace-nowrap flex flex-nowrap w-full touch-pan-y"
        {...swipingProps}
    >
        {images.map((image, index) => (
            <div
                key={image.filename}
                ref={(ref) => { slideRefs.current[index] = ref }}
                className="pr-4 lg:pr-6 last:pr-0 flex-grow-0 flex-shrink-0 w-[365px] h-[288px] relative"
            >
                <div className="relative w-full h-full">
                    <Image
                        maxWidth={640}
                        src={image.filename}
                        alt={image.alt}
                        layout="fill"
                        objectFit="cover"
                        draggable={false}
                    />
                </div>
            </div>
        ))}
    </motion.div>
)

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

About us

Dashdot We shape, build and grow ambitious digital products.

License

This project is licensed under the MIT License - see the LICENSE.md file for details