1.0.4 • Published 12 months ago

@frost084/use-slider-hook v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

useSlider Hook

A custom React hook for creating sliders with auto-scroll, looping, and progress tracking, ideal for carousels and image galleries.

Features

  • Auto-scroll with customizable speed
  • Loop functionality to repeat slides
  • Bullet and slide progress tracking
  • Directional navigation (next/previous)
  • Visibility detection to pause when the slider is out of view

Installation

You can install this hook via NPM:

npm install @frost084/use-slider-hook

Usage

Import the hook in your component and configure it with options as needed.

import { useSlider } from '@frost084/use-slider-hook'

const MySliderComponent = () => {
  const {
    currentSlide,
    prevIndex,
    nextIndex,
    nextSlide,
    prevSlide,
    goToSlide,
    slideTimeProgress,
    totalProgress,
    loopProgress,
    bulletProgress,
    direction,
    sliderRef
  } = useSlider({
    autoScroll: true,       // Enable automatic slide transition
    loop: true,             // Loop back to the first slide
    speed: 3000,            // Transition duration in milliseconds
    totalSlides: 5          // Total number of slides
  })

  return (
    <div ref={sliderRef}>
      <button onClick={prevSlide}>Previous</button>
      <button onClick={nextSlide}>Next</button>
      <div>Current Slide: {currentSlide}</div>
      <div>Slide Time Progress: {slideTimeProgress}%</div>
      <div>Total Progress: {totalProgress}%</div>
      <div>Loop Progress: {loopProgress}%</div>
      <div>Bullet Progress: {bulletProgress}%</div>
      <div>Direction: {direction}</div>
    </div>
  )
}

API

Hook Options

OptionTypeDefaultDescription
autoScrollbooleanfalseEnables automatic slide transition
loopbooleantrueEnables looping of slides
speednumber3000Time in ms for each slide transition
totalSlidesnumber0Total number of slides in the slider

Returned Values

ValueTypeDescription
currentSlidenumberIndex of the current slide
prevIndexnumberIndex of the previous slide
nextIndexnumberIndex of the next slide
nextSlidefunctionFunction to go to the next slide
prevSlidefunctionFunction to go to the previous slide
goToSlidefunctionFunction to go directly to a specified slide by index
slideTimeProgressnumberProgress of the current slide as a percentage (0 to 100)
totalProgressnumberProgress of the entire slider as a percentage (0 to 100)
loopProgressnumberProgress of the loop as a percentage, considering all slides in the loop
bulletProgressnumberProgress for bullets as a percentage based on the current slide
directionstringDirection of the slide transition ("next" or "prev")
sliderRefrefReference to the slider container for visibility tracking

License

This project is licensed under the MIT License.


Developed by TheFrost - Creative Frontend Developer with contributions from ChatGPT (OpenAI).