feat: redesign card scroller
This commit is contained in:
2 files changed
+146
-76
No files matched your search
@@ -1,50 +1,81 @@
|
|||||||
|
@use "@/styles/variables.scss" as *;
|
||||||
|
|
||||||
|
.card-scroller-container {
|
||||||
|
margin-bottom: 2.5em;
|
||||||
|
position: relative;
|
||||||
|
|
||||||
|
// Ensure the container handles overflow if needed, but usually Carousel handles it
|
||||||
|
}
|
||||||
|
|
||||||
.card-scroller {
|
.card-scroller {
|
||||||
// height: 20em;
|
// Override carousel default outline
|
||||||
|
&:focus {
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
&-slide {
|
&-slide {
|
||||||
overflow: visible;
|
padding-right: 0.5em; // Gap between cards (handled by slide padding instead of gap for carousel)
|
||||||
display: flex;
|
// With react-multi-carousel, better to rely on their gap logic or item class
|
||||||
}
|
|
||||||
&-button {
|
|
||||||
position: absolute !important;
|
|
||||||
height: 100%;
|
|
||||||
font-size: 1em !important;
|
|
||||||
width: $page-margin;
|
|
||||||
&.right{
|
|
||||||
border-radius: $border-radius-default 0 0 $border-radius-default !important;
|
|
||||||
right: -$page-margin;
|
|
||||||
}
|
|
||||||
&.left{
|
|
||||||
border-radius: 0 $border-radius-default $border-radius-default 0 !important;
|
|
||||||
left: -$page-margin;
|
|
||||||
}
|
|
||||||
.material-symbols-rounded{
|
|
||||||
font-size: 3.2em;
|
|
||||||
--wght: 200;
|
|
||||||
--opsz: 48;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
&-item {
|
||||||
|
padding-right: 0.6em; // Consistent gap
|
||||||
}
|
}
|
||||||
|
|
||||||
&-header-container {
|
&-header-container {
|
||||||
display: flex;
|
display: flex;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
margin-bottom: 1em;
|
||||||
|
min-height: 40px;
|
||||||
}
|
}
|
||||||
|
|
||||||
&-heading {
|
&-heading {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: flex-start;
|
justify-content: flex-start;
|
||||||
gap: 0.35em;
|
gap: 0.5em;
|
||||||
position: relative;
|
position: relative;
|
||||||
margin-bottom: 0.25em !important;
|
font-weight: 600 !important;
|
||||||
|
|
||||||
&-decoration {
|
&-decoration {
|
||||||
width: 0.75em;
|
width: 4px;
|
||||||
height: 2px;
|
height: 24px;
|
||||||
background: white;
|
background: $clr-accent-default;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
&-controls {
|
||||||
|
display: flex;
|
||||||
|
gap: 0.5em;
|
||||||
|
|
||||||
|
.scroller-nav-btn {
|
||||||
|
background-color: rgba(255, 255, 255, 0.05);
|
||||||
|
border: 1px solid rgba(255, 255, 255, 0.05);
|
||||||
|
color: rgba(255, 255, 255, 0.7);
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
width: 36px;
|
||||||
|
height: 36px;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
background-color: rgba(255, 255, 255, 0.1);
|
||||||
|
color: #fff;
|
||||||
|
border-color: rgba(255, 255, 255, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disabled state
|
||||||
|
&.Mui-disabled {
|
||||||
|
background-color: transparent;
|
||||||
|
border-color: transparent;
|
||||||
|
color: rgba(255, 255, 255, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.material-symbols-rounded {
|
||||||
|
font-size: 1.5em;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
&-container .MuiButtonGroup-outlined{
|
|
||||||
gap: 1em;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,31 +1,61 @@
|
|||||||
import React, { type ReactNode } from "react";
|
import React, { type ReactNode, useRef, useState } from "react";
|
||||||
|
|
||||||
import Carousel from "react-multi-carousel";
|
import Carousel from "react-multi-carousel";
|
||||||
import "react-multi-carousel/lib/styles.css";
|
import "react-multi-carousel/lib/styles.css";
|
||||||
|
|
||||||
|
import { Box } from "@mui/material";
|
||||||
import IconButton from "@mui/material/IconButton";
|
import IconButton from "@mui/material/IconButton";
|
||||||
import Typography from "@mui/material/Typography";
|
import Typography from "@mui/material/Typography";
|
||||||
|
|
||||||
import "./cardScroller.scss";
|
import "./cardScroller.scss";
|
||||||
|
|
||||||
//@ts-ignore - I don't know the prop type for react-multi-carousel's customRightArrow and customLeftArrow
|
// Custom Header + Button Group component
|
||||||
function RightButton(props) {
|
const ScrollerHeader = ({
|
||||||
const { carouselState, children, rtl, ...restProps } = props;
|
onNext,
|
||||||
|
onPrevious,
|
||||||
|
title,
|
||||||
|
headingProps,
|
||||||
|
disableDecoration,
|
||||||
|
currentSlide = 0,
|
||||||
|
isNextDisabled = false,
|
||||||
|
}: any) => {
|
||||||
|
// Basic disable logic for visual feedback
|
||||||
|
const isFirst = currentSlide === 0;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<IconButton className="card-scroller-button right" {...restProps}>
|
<div
|
||||||
<div className="material-symbols-rounded">chevron_right</div>
|
className={`card-scroller-header-container ${disableDecoration ? "hidden-decoration" : ""}`}
|
||||||
|
>
|
||||||
|
<Typography
|
||||||
|
variant="h5"
|
||||||
|
color="textPrimary"
|
||||||
|
className="card-scroller-heading"
|
||||||
|
{...headingProps}
|
||||||
|
>
|
||||||
|
<div className="card-scroller-heading-decoration" />
|
||||||
|
{title}
|
||||||
|
</Typography>
|
||||||
|
|
||||||
|
<div className="card-scroller-controls">
|
||||||
|
<IconButton
|
||||||
|
onClick={onPrevious}
|
||||||
|
disabled={isFirst}
|
||||||
|
size="small"
|
||||||
|
className="scroller-nav-btn"
|
||||||
|
>
|
||||||
|
<span className="material-symbols-rounded">chevron_left</span>
|
||||||
</IconButton>
|
</IconButton>
|
||||||
);
|
<IconButton
|
||||||
}
|
onClick={onNext}
|
||||||
//@ts-ignore - I don't know the prop type for react-multi-carousel's customRightArrow and customLeftArrow
|
disabled={isNextDisabled}
|
||||||
function LeftButton(props) {
|
size="small"
|
||||||
const { carouselState, children, rtl, ...restProps } = props;
|
className="scroller-nav-btn"
|
||||||
return (
|
>
|
||||||
<IconButton className="card-scroller-button left" {...restProps}>
|
<span className="material-symbols-rounded">chevron_right</span>
|
||||||
<div className="material-symbols-rounded">chevron_left</div>
|
|
||||||
</IconButton>
|
</IconButton>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
type CardScrollerProps = {
|
type CardScrollerProps = {
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
@@ -44,9 +74,13 @@ export default function CardScroller({
|
|||||||
disableDecoration = false,
|
disableDecoration = false,
|
||||||
boxProps,
|
boxProps,
|
||||||
}: CardScrollerProps) {
|
}: CardScrollerProps) {
|
||||||
|
const carouselRef = useRef<any>(null);
|
||||||
|
const [currentSlide, setCurrentSlide] = useState(0);
|
||||||
|
const [slidesToShow, setSlidesToShow] = useState(displayCards);
|
||||||
|
const totalItems = React.Children.count(children);
|
||||||
|
|
||||||
const responsive = {
|
const responsive = {
|
||||||
superLargeDesktop: {
|
superLargeDesktop: {
|
||||||
// the naming can be any, depends on you.
|
|
||||||
breakpoint: { max: 4000, min: 3000 },
|
breakpoint: { max: 4000, min: 3000 },
|
||||||
items: displayCards + 1,
|
items: displayCards + 1,
|
||||||
slidesToSlide: displayCards + 1,
|
slidesToSlide: displayCards + 1,
|
||||||
@@ -55,62 +89,67 @@ export default function CardScroller({
|
|||||||
desktop: {
|
desktop: {
|
||||||
breakpoint: { max: 3000, min: 925 },
|
breakpoint: { max: 3000, min: 925 },
|
||||||
items: displayCards,
|
items: displayCards,
|
||||||
slidesToSlide: displayCards, // optional, default to 1.
|
slidesToSlide: displayCards,
|
||||||
partialVisibilityGutter: 30,
|
partialVisibilityGutter: 30,
|
||||||
},
|
},
|
||||||
tablet: {
|
tablet: {
|
||||||
breakpoint: { max: 925, min: 600 },
|
breakpoint: { max: 925, min: 600 },
|
||||||
items: displayCards - 3,
|
items: displayCards - 3,
|
||||||
slidesToSlide: displayCards - 3, // optional, default to 1.
|
slidesToSlide: displayCards - 3,
|
||||||
partialVisibilityGutter: 20,
|
partialVisibilityGutter: 20,
|
||||||
},
|
},
|
||||||
mobile: {
|
mobile: {
|
||||||
breakpoint: { max: 600, min: 424 },
|
breakpoint: { max: 600, min: 424 },
|
||||||
items: displayCards - 5,
|
items: displayCards - 5,
|
||||||
slidesToSlide: displayCards - 5, // optional, default to 1.
|
slidesToSlide: displayCards - 5,
|
||||||
partialVisibilityGutter: 10,
|
partialVisibilityGutter: 10,
|
||||||
},
|
},
|
||||||
smallScreen: {
|
smallScreen: {
|
||||||
breakpoint: { max: 424, min: 0 },
|
breakpoint: { max: 424, min: 0 },
|
||||||
items: 1,
|
items: 1,
|
||||||
slidesToSlide: 1, // optional, default to 1.
|
slidesToSlide: 1,
|
||||||
partialVisibilityGutter: 10,
|
partialVisibilityGutter: 10,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
const handleNext = () => {
|
||||||
<div
|
if (carouselRef.current) {
|
||||||
{...boxProps}
|
carouselRef.current.next();
|
||||||
className="card-scroller-container"
|
|
||||||
style={{ marginBottom: "1.2em" }}
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
style={{ marginBottom: "1.2em" }}
|
|
||||||
className={
|
|
||||||
disableDecoration
|
|
||||||
? "card-scroller-header-container hidden-decoration"
|
|
||||||
: "card-scroller-header-container"
|
|
||||||
}
|
}
|
||||||
>
|
};
|
||||||
<Typography
|
|
||||||
variant="h5"
|
const handlePrevious = () => {
|
||||||
color="textPrimary"
|
if (carouselRef.current) {
|
||||||
className="card-scroller-heading"
|
carouselRef.current.previous();
|
||||||
{...headingProps}
|
}
|
||||||
>
|
};
|
||||||
<div className="card-scroller-heading-decoration" /> {title}
|
|
||||||
</Typography>
|
return (
|
||||||
</div>
|
<div {...boxProps} className="card-scroller-container">
|
||||||
|
<ScrollerHeader
|
||||||
|
title={title}
|
||||||
|
headingProps={headingProps}
|
||||||
|
disableDecoration={disableDecoration}
|
||||||
|
onNext={handleNext}
|
||||||
|
onPrevious={handlePrevious}
|
||||||
|
currentSlide={currentSlide}
|
||||||
|
isNextDisabled={currentSlide + slidesToShow >= totalItems}
|
||||||
|
/>
|
||||||
<Carousel
|
<Carousel
|
||||||
|
ref={carouselRef}
|
||||||
swipeable
|
swipeable
|
||||||
draggable
|
draggable
|
||||||
responsive={responsive}
|
responsive={responsive}
|
||||||
arrows
|
arrows={false}
|
||||||
className="card-scroller"
|
className="card-scroller"
|
||||||
customTransition="all .6s"
|
customTransition="transform 400ms ease-in-out"
|
||||||
transitionDuration={600}
|
transitionDuration={400}
|
||||||
customRightArrow={<RightButton />}
|
containerClass="card-scroller-track"
|
||||||
customLeftArrow={<LeftButton />}
|
itemClass="card-scroller-item"
|
||||||
|
beforeChange={(nextSlide, state) => {
|
||||||
|
setCurrentSlide(nextSlide);
|
||||||
|
setSlidesToShow(state.slidesToShow);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</Carousel>
|
</Carousel>
|
||||||
|
|||||||
Reference in new issue
Block a user