Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
make component drag scrollable in react

I need to add drag feature to my component for scroll vertically but I don't want to use additional libraries. from this source which is tutorial for horizontal implementation that I follow for vertical mode but it doesn't work. here's my implementation so far:

    const [thumbnailScroll, setThumbnailScroll] = useState({isScrolling: false, clientY: 0, scrollY: 0});
    const thumbnailRef = useRef();

    const mouseDownHandler = useCallback((e) => {
        console.log(e.clientY);
        thumbnailRef.current.style.cursor = 'grabbing';
        thumbnailRef.current.style.userSelect = 'none';

        setThumbnailScroll((prev) => ({...prev, isScrolling: true, clientY: e.clientY}));
    }, []);

    const mouseMoveHandler = useCallback(
        (e) => {
            const {clientY, scrollY, isScrolling} = thumbnailScroll;
            if (isScrolling) {
                thumbnailRef.current.scrollTop = scrollY + e.clientY - clientY;
                setThumbnailScroll((prev) => ({...prev, scrollY: scrollY + e.clientY - clientY, clientY: e.clientY}));
            }
        },
        [thumbnailScroll],
    );

    const mouseUpHandler = useCallback(() => {
        setThumbnailScroll((prev) => ({...prev, isScrolling: false}));
        thumbnailRef.current.style.cursor = 'default';
    }, []);


return (
                <div className={styles.carouselContainer}>
                    <div
                        className={styles.thumbnailContainer}
                        ref={thumbnailRef}
                        onMouseDown={mouseDownHandler}
                        onDragStart={mouseMoveHandler}
                        onMouseUp={mouseUpHandler}
                    >
                        {renderCarouselThumbnails}
                    </div>
                </div>
    );

and here is the styles:

.carouselContainer {
            display: flex;
            flex-direction: column;
            height: 100%;
            width: 100%;

            .thumbnailContainer {
                background-color: red;
                height: 100%;
                width: 20%;
                display: flex;
                align-items: center;
                flex-direction: column;
                -ms-overflow-style: none;  /* Internet Explorer 10+ */
                scrollbar-width: none;
                cursor: grab;
                overflow: auto;

                &::-webkit-scrollbar {
                    display: none;  /* Safari and Chrome */
                }

                .thumbnailImage {
                    display: inline-block;
                    width: 80px;
                    height: 60px;
                    margin-bottom: 2rem;
                }
            }
        }

the problem is it doesn't work as expected. how can I achieve this?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!