feat(catalog-drag-scroll): add interactive mouse drag-to-scroll and wheel scroll handlers to catalog category pills
This commit is contained in:
1 parent
f089f7ed91
commit
eb6318ecc1
1 file changed
+40
-2
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import React, { useState, useEffect } from "react";
|
||||
import React, { useState, useEffect, useRef } from "react";
|
||||
import SafeImage from "../../components/SafeImage";
|
||||
import { Download, Printer, ShieldCheck, Sparkles, CheckCircle2, ChevronLeft, Search, Filter } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
@@ -14,6 +14,36 @@ export default function CatalogClient() {
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const [selectedCategory, setSelectedCategory] = useState("all");
|
||||
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
const [isMouseDown, setIsMouseDown] = useState(false);
|
||||
const [startX, setStartX] = useState(0);
|
||||
const [scrollLeft, setScrollLeft] = useState(0);
|
||||
|
||||
const handleMouseDown = (e: React.MouseEvent) => {
|
||||
if (!scrollRef.current) return;
|
||||
setIsMouseDown(true);
|
||||
setStartX(e.pageX - scrollRef.current.offsetLeft);
|
||||
setScrollLeft(scrollRef.current.scrollLeft);
|
||||
};
|
||||
|
||||
const handleMouseLeaveOrUp = () => {
|
||||
setIsMouseDown(false);
|
||||
};
|
||||
|
||||
const handleMouseMove = (e: React.MouseEvent) => {
|
||||
if (!isMouseDown || !scrollRef.current) return;
|
||||
e.preventDefault();
|
||||
const x = e.pageX - scrollRef.current.offsetLeft;
|
||||
const walk = (x - startX) * 2; // scroll speed
|
||||
scrollRef.current.scrollLeft = scrollLeft - walk;
|
||||
};
|
||||
|
||||
const handleWheel = (e: React.WheelEvent) => {
|
||||
if (scrollRef.current && e.deltaY !== 0) {
|
||||
scrollRef.current.scrollLeft += e.deltaY;
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
productService.getProducts({ limit: 999 })
|
||||
.then(res => {
|
||||
@@ -97,7 +127,15 @@ export default function CatalogClient() {
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2.5 overflow-x-auto sleek-hscroll w-full md:w-auto py-2.5 px-1 scroll-smooth">
|
||||
<div
|
||||
ref={scrollRef}
|
||||
onMouseDown={handleMouseDown}
|
||||
onMouseLeave={handleMouseLeaveOrUp}
|
||||
onMouseUp={handleMouseLeaveOrUp}
|
||||
onMouseMove={handleMouseMove}
|
||||
onWheel={handleWheel}
|
||||
className={`flex items-center gap-2.5 overflow-x-auto sleek-hscroll w-full md:w-auto py-2.5 px-1 scroll-smooth select-none cursor-grab ${isMouseDown ? 'cursor-grabbing' : ''}`}
|
||||
>
|
||||
<button
|
||||
onClick={() => setSelectedCategory("all")}
|
||||
className={`px-5 py-3 rounded-2xl text-xs font-black transition-all shrink-0 shadow-xs ${
|
||||
|
||||
Reference in new issue
Block a user