updated category carousel ui

This commit is contained in:
Jelaletdin12
2026-03-31 22:04:17 +05:00
parent 696853e988
commit 33214002f2
2 changed files with 80 additions and 109 deletions

View File

@@ -1,75 +1,40 @@
import React, { useRef } from "react"; import React from "react";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { Swiper, SwiperSlide } from "swiper/react";
import { Navigation, FreeMode } from "swiper/modules";
import { useGetCategoriesQuery } from "../../app/api/categories.js"; import { useGetCategoriesQuery } from "../../app/api/categories.js";
import styles from "./CategoryCarousel.module.scss"; import styles from "./CategoryCarousel.module.scss";
import "swiper/css";
import "swiper/css/navigation";
import "swiper/css/free-mode";
const CategoryCarousel = () => { const CategoryCarousel = () => {
const { data, isLoading } = useGetCategoriesQuery("tree"); const { data, isLoading } = useGetCategoriesQuery("tree");
const scrollRef = useRef(null);
const navigate = useNavigate(); const navigate = useNavigate();
const isDragging = useRef(false);
const startX = useRef(0);
const scrollLeft = useRef(0);
const hasDragged = useRef(false);
const mainCategories = const mainCategories =
data?.data?.filter((cat) => cat.parent_id === null) ?? []; data?.data?.filter((cat) => cat.parent_id === null) ?? [];
const scroll = (dir) => { const handleCardClick = (id) => {
if (!scrollRef.current) return; navigate(`/category/${id}`);
scrollRef.current.scrollBy({ left: dir * 320, behavior: "smooth" });
};
const onMouseDown = (e) => {
isDragging.current = true;
hasDragged.current = false;
startX.current = e.pageX - scrollRef.current.offsetLeft;
scrollLeft.current = scrollRef.current.scrollLeft;
scrollRef.current.style.cursor = "grabbing";
};
const onMouseMove = (e) => {
if (!isDragging.current) return;
e.preventDefault();
const x = e.pageX - scrollRef.current.offsetLeft;
const walk = x - startX.current;
if (Math.abs(walk) > 4) hasDragged.current = true;
scrollRef.current.scrollLeft = scrollLeft.current - walk;
};
const onMouseUp = () => {
isDragging.current = false;
if (scrollRef.current) scrollRef.current.style.cursor = "grab";
};
const handleCardClick = (e, slug) => {
if (hasDragged.current) {
e.preventDefault();
return;
}
navigate(`/category/${slug}`);
}; };
if (isLoading || mainCategories.length === 0) return null; if (isLoading || mainCategories.length === 0) return null;
return ( return (
<div className={styles.wrapper}> <div className={styles.wrapper}>
<button <Swiper
className={`${styles.arrow} ${styles.left}`} modules={[Navigation, FreeMode]}
onClick={() => scroll(-1)} navigation={{
aria-label="Scroll left" prevEl: `.${styles.prev}`,
> nextEl: `.${styles.next}`,
}}
</button> freeMode={{ enabled: true, momentum: true, momentumRatio: 0.5 }}
slidesPerView="auto"
<div spaceBetween={12}
className={styles.track} grabCursor={true}
ref={scrollRef} className={styles.swiper}
onMouseDown={onMouseDown}
onMouseMove={onMouseMove}
onMouseUp={onMouseUp}
onMouseLeave={onMouseUp}
> >
{mainCategories.map((cat) => { {mainCategories.map((cat) => {
const thumb = const thumb =
@@ -78,34 +43,34 @@ const CategoryCarousel = () => {
null; null;
return ( return (
<div <SwiperSlide key={cat.id} className={styles.slide}>
key={cat.id} <div
className={styles.card} className={styles.card}
onClick={(e) => handleCardClick(e, cat.slug)} onClick={() => handleCardClick(cat.id)}
> >
<div className={styles.imgWrap}> <div className={styles.imgWrap}>
{thumb ? ( {thumb ? (
<img <img
src={thumb} src={thumb}
alt={cat.name} alt={cat.name}
loading="lazy" loading="lazy"
draggable={false} draggable={false}
/> />
) : ( ) : (
<div className={styles.placeholder} /> <div className={styles.placeholder} />
)} )}
</div>
<span className={styles.name}>{cat.name}</span>
</div> </div>
<span className={styles.name}>{cat.name}</span> </SwiperSlide>
</div>
); );
})} })}
</div> </Swiper>
<button <button className={`${styles.arrow} ${styles.prev}`} aria-label="Scroll left">
className={`${styles.arrow} ${styles.right}`}
onClick={() => scroll(1)} </button>
aria-label="Scroll right" <button className={`${styles.arrow} ${styles.next}`} aria-label="Scroll right">
>
</button> </button>
</div> </div>

View File

@@ -6,33 +6,27 @@
margin: 20px 0 24px 0; margin: 20px 0 24px 0;
} }
/* ── Scrollable track ── */ /* ── Swiper overrides ── */
.track { .swiper {
display: flex;
gap: 12px;
overflow-x: auto;
scroll-snap-type: x mandatory;
-webkit-overflow-scrolling: touch;
scrollbar-width: none;
padding: 4px 2px 8px 2px;
flex: 1; flex: 1;
cursor: grab; overflow: hidden;
user-select: none; padding: 4px 2px 8px 2px !important;
&::-webkit-scrollbar { :global(.swiper-button-disabled) {
display: none; opacity: 0.3;
pointer-events: none;
} }
}
&:active { .slide {
cursor: grabbing; width: 200px !important;
} height: auto;
} }
/* ── Card ── */ /* ── Card ── */
.card { .card {
flex: 0 0 auto; width: 100%;
scroll-snap-align: start; height: 100%;
width: 180px;
border-radius: 12px; border-radius: 12px;
background: #f5f5f5; background: #f5f5f5;
overflow: hidden; overflow: hidden;
@@ -44,6 +38,7 @@
&:hover { &:hover {
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
transform: translateY(-2px); transform: translateY(-2px);
border: 1px solid #d24141;
.imgWrap img { .imgWrap img {
transform: scale(1.04); transform: scale(1.04);
@@ -54,9 +49,10 @@
/* ── Image area ── */ /* ── Image area ── */
.imgWrap { .imgWrap {
width: 100%; width: 100%;
aspect-ratio: 1 / 1; // aspect-ratio: 4 / 3;
overflow: hidden; overflow: hidden;
background: #ebebeb; background: #ebebeb;
flex-shrink: 0;
img { img {
width: 100%; width: 100%;
@@ -73,19 +69,25 @@
background: linear-gradient(135deg, #e0e0e0 0%, #d0d0d0 100%); background: linear-gradient(135deg, #e0e0e0 0%, #d0d0d0 100%);
} }
/* ── Name label ── */ /* ── Name label — sabit yükseklik ── */
.name { .name {
padding: 10px 12px 12px 12px; padding: 10px 12px 12px 12px;
font-size: 13px; font-size: 16px;
font-weight: 500; font-weight: 600;
color: #222; color: #222;
text-align: center; text-align: center;
line-height: 1.4; line-height: 1.4;
background: #fff; background: #fff;
/* 2 satır = 2 × 13px × 1.4 + padding = ~60px — tüm kartlar eşit */
min-height: 60px;
display: flex;
align-items: center;
justify-content: center;
} }
/* ── Arrow buttons ── */ /* ── Arrow buttons ── */
.arrow { .arrow {
position: absolute;
flex: 0 0 auto; flex: 0 0 auto;
width: 32px; width: 32px;
height: 32px; height: 32px;
@@ -100,8 +102,7 @@
cursor: pointer; cursor: pointer;
color: #444; color: #444;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
transition: background 0.15s, border-color 0.15s, color 0.15s, transition: background 0.15s, border-color 0.15s, color 0.15s, box-shadow 0.15s;
box-shadow 0.15s;
padding: 0; padding: 0;
z-index: 1; z-index: 1;
@@ -112,12 +113,13 @@
box-shadow: 0 2px 8px rgba(210, 65, 65, 0.35); box-shadow: 0 2px 8px rgba(210, 65, 65, 0.35);
} }
&.left { &.prev {
margin-right: 4px; margin-right: 4px;
} }
&.right { &.next {
margin-left: 4px; margin-left: 4px;
right: 0;
} }
} }
@@ -127,19 +129,23 @@
display: none; display: none;
} }
.card { .slide {
width: 140px; width: 140px !important;
} }
} }
@media (max-width: 480px) { @media (max-width: 480px) {
.slide {
width: 120px !important;
}
.card { .card {
width: 120px;
border-radius: 10px; border-radius: 10px;
} }
.name { .name {
font-size: 12px; font-size: 12px;
padding: 8px 8px 10px 8px; padding: 8px 8px 10px 8px;
min-height: 52px;
} }
} }