kevinfarrugia / react-glider

A ReactJS wrapper for Glider.js
https://react-glider.vercel.app/
MIT License
208 stars 42 forks source link

Possible to programmatically navigate to a slide via code? #142

Closed haveamission closed 1 year ago

haveamission commented 1 year ago

Is it possible to have an external event that lets a user go to a specific slide?

Something like the parent library's:

scrollTo

function

kevinfarrugia commented 1 year ago

It can be done using the scrollItem method.

Check the demo: https://react-glider.vercel.app/#ref_exposes_glider

const gliderRef = React.useRef < GliderMethods > null;

return (
  <button
    type="button"
    onClick={() => {
      const random = Math.floor(Math.random() * 12);

      gliderRef.current?.scrollItem(random);
    }}
  >
    Scroll to random item
  </button>
);
haveamission commented 1 year ago

Perfect, worked great, thank you!