Skip to content

Commit

Permalink
feat: add switch mouse scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
hamster1963 committed Oct 22, 2024
1 parent b316875 commit 0729aef
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions components/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { cn } from "@/lib/utils";
import { motion } from "framer-motion";
import React from "react";
import React, { useRef, useEffect } from "react";

export default function Switch({
allTag,
Expand All @@ -13,8 +13,36 @@ export default function Switch({
nowTag: string;
setTag: (tag: string) => void;
}) {
const scrollRef = useRef<HTMLDivElement>(null);

useEffect(() => {
const container = scrollRef.current;
if (!container) return;

const isOverflowing = container.scrollWidth > container.clientWidth;

if (!isOverflowing) {
return;
}

const onWheel = (e: WheelEvent) => {
e.preventDefault();
e.stopPropagation();
container.scrollLeft += e.deltaY;
};

container.addEventListener("wheel", onWheel, { passive: false });

return () => {
container.removeEventListener("wheel", onWheel);
};
}, []);

return (
<div className="scrollbar-hidden z-50 flex flex-col items-start overflow-x-scroll rounded-[50px]">
<div
ref={scrollRef}
className="scrollbar-hidden z-50 flex flex-col items-start overflow-x-scroll rounded-[50px]"
>
<div className="flex items-center gap-1 rounded-[50px] bg-stone-100 p-[3px] dark:bg-stone-800">
{allTag.map((tag) => (
<div
Expand All @@ -24,7 +52,7 @@ export default function Switch({
"relative cursor-pointer rounded-3xl px-2.5 py-[8px] text-[13px] font-[600] transition-all duration-500",
nowTag === tag
? "text-black dark:text-white"
: "text-stone-400 dark:text-stone-500",
: "text-stone-400 dark:text-stone-500"
)}
>
{nowTag === tag && (
Expand Down

0 comments on commit 0729aef

Please sign in to comment.