fix some bug in some blocks
This commit is contained in:
+93
-150
@@ -4,21 +4,14 @@ import type { MotionValue } from "framer-motion";
|
||||
import {
|
||||
AnimatePresence,
|
||||
motion,
|
||||
useMotionValueEvent,
|
||||
useScroll,
|
||||
useTransform,
|
||||
} from "framer-motion";
|
||||
import React, { useEffect, useRef, useState } from "react";
|
||||
import React, { useRef, useState } from "react";
|
||||
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const getAnimationRange = (index: number, totalFeatures: number) => {
|
||||
const cardPosition = index / totalFeatures;
|
||||
|
||||
const range = [0, cardPosition, 1];
|
||||
|
||||
return range;
|
||||
};
|
||||
|
||||
interface Statistics {
|
||||
value: string;
|
||||
label: string;
|
||||
@@ -39,52 +32,15 @@ interface Testimonial {
|
||||
designation: string;
|
||||
}
|
||||
|
||||
interface AnimationWrapperProps {
|
||||
children: React.ReactNode;
|
||||
delay?: number;
|
||||
className?: string;
|
||||
duration?: number;
|
||||
}
|
||||
|
||||
const AnimationWrapper = ({
|
||||
children,
|
||||
delay = 0,
|
||||
className,
|
||||
duration = 0.3,
|
||||
}: AnimationWrapperProps) => {
|
||||
return (
|
||||
<motion.div
|
||||
initial={{
|
||||
x: -10,
|
||||
opacity: 0,
|
||||
}}
|
||||
animate={{
|
||||
x: 0,
|
||||
opacity: 1,
|
||||
}}
|
||||
exit={{
|
||||
x: 10,
|
||||
opacity: 0,
|
||||
}}
|
||||
transition={{
|
||||
duration: duration,
|
||||
ease: "easeOut",
|
||||
delay: delay,
|
||||
}}
|
||||
className={className}
|
||||
>
|
||||
{children}
|
||||
</motion.div>
|
||||
);
|
||||
};
|
||||
|
||||
interface TestimonialCardProps {
|
||||
testimonial: Testimonial;
|
||||
}
|
||||
|
||||
const TestimonialCard = ({ testimonial }: TestimonialCardProps) => {
|
||||
return (
|
||||
<div className="hidden h-96 flex-1/2 shrink-0 flex-col items-start justify-end rounded-lg border bg-muted p-6 text-start sm:flex lg:h-full">
|
||||
<div
|
||||
className="hidden h-96 min-w-[14rem] flex-[1/2] shrink-0 flex-col items-start justify-end rounded-lg border bg-muted p-6 text-start sm:flex lg:h-full lg:min-w-[16rem]"
|
||||
>
|
||||
<img
|
||||
src={testimonial.companyLogo}
|
||||
alt={testimonial.author}
|
||||
@@ -94,7 +50,7 @@ const TestimonialCard = ({ testimonial }: TestimonialCardProps) => {
|
||||
«{testimonial.quote}»
|
||||
</p>
|
||||
<p className="mt-4 text-sm font-medium">
|
||||
{testimonial.author}{" "}
|
||||
{testimonial.author}{" "}
|
||||
<span className="font-light">{testimonial.designation}</span>
|
||||
</p>
|
||||
</div>
|
||||
@@ -112,18 +68,13 @@ const ScrollProgressIndicator = ({
|
||||
activeIndex,
|
||||
scrollYProgress,
|
||||
}: ScrollProgressIndicatorProps) => {
|
||||
const range = getAnimationRange(
|
||||
Math.max(0, activeIndex - 0.5),
|
||||
totalFeatures,
|
||||
const segmentStart = activeIndex / totalFeatures;
|
||||
const segmentEnd = (activeIndex + 1) / totalFeatures;
|
||||
const width = useTransform(
|
||||
scrollYProgress,
|
||||
[segmentStart, segmentEnd],
|
||||
["0%", "100%"],
|
||||
);
|
||||
const nextRange = getAnimationRange(
|
||||
Math.min(totalFeatures, activeIndex + 0.5),
|
||||
totalFeatures,
|
||||
);
|
||||
|
||||
const currentRange = [range[1], nextRange[1]];
|
||||
|
||||
const width = useTransform(scrollYProgress, currentRange, ["0%", "100%"]);
|
||||
|
||||
return (
|
||||
<div className="mt-14 hidden w-fit items-center justify-center gap-3 rounded-full bg-muted px-6 py-4 lg:flex">
|
||||
@@ -137,14 +88,12 @@ const ScrollProgressIndicator = ({
|
||||
activeIndex > index && "bg-foreground",
|
||||
)}
|
||||
>
|
||||
{activeIndex === index && (
|
||||
{activeIndex === index ? (
|
||||
<motion.div
|
||||
style={{
|
||||
width,
|
||||
}}
|
||||
className="absolute top-0 start-0 h-full rounded-full bg-foreground"
|
||||
style={{ width }}
|
||||
className="absolute start-0 top-0 h-full rounded-full bg-foreground"
|
||||
/>
|
||||
)}
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
@@ -168,19 +117,13 @@ const FeatureCard = ({
|
||||
scrollYProgress,
|
||||
}: FeatureCardProps) => {
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
"flex w-full flex-col-reverse justify-between gap-8 px-10 py-4 text-start lg:absolute lg:top-1/2 lg:start-1/2 lg:h-full lg:-translate-x-1/2 lg:-translate-y-1/2 lg:flex-row lg:items-center lg:border-s",
|
||||
)}
|
||||
>
|
||||
<div className="flex flex-2/5 flex-col gap-8 lg:h-full lg:gap-0">
|
||||
<div className="flex flex-col gap-1">
|
||||
<AnimationWrapper>
|
||||
<p className="text-xl font-semibold">{feature.title}</p>
|
||||
</AnimationWrapper>
|
||||
<AnimationWrapper delay={0.15}>
|
||||
<p className="font-light text-pretty">{feature.subtitle}</p>
|
||||
</AnimationWrapper>
|
||||
<div className="flex w-full min-w-0 flex-col-reverse justify-between gap-8 px-4 py-4 text-start sm:px-10 lg:min-h-[28rem] lg:flex-row lg:items-stretch lg:border-s">
|
||||
<div className="flex min-w-0 flex-[2/5] flex-col gap-8 lg:gap-0">
|
||||
<div className="flex min-w-0 flex-col gap-1">
|
||||
<p className="text-xl font-semibold text-balance break-words">
|
||||
{feature.title}
|
||||
</p>
|
||||
<p className="font-light text-pretty">{feature.subtitle}</p>
|
||||
</div>
|
||||
|
||||
<ScrollProgressIndicator
|
||||
@@ -190,30 +133,23 @@ const FeatureCard = ({
|
||||
/>
|
||||
|
||||
<div className="flex flex-col gap-1 lg:mt-auto">
|
||||
<AnimationWrapper delay={0.3}>
|
||||
<p className="text-2xll font-medium">{feature.statistics.value}</p>
|
||||
</AnimationWrapper>
|
||||
<AnimationWrapper delay={0.45}>
|
||||
<p className="text-sm font-light">{feature.statistics.label}</p>
|
||||
</AnimationWrapper>
|
||||
<p className="text-2xl font-medium">{feature.statistics.value}</p>
|
||||
<p className="text-sm font-light">{feature.statistics.label}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<AnimationWrapper
|
||||
duration={0.6}
|
||||
className="flex h-96 w-full flex-3/5 items-center gap-6 lg:h-full"
|
||||
>
|
||||
<div className="flex h-96 w-full min-w-[18rem] flex-[3/5] items-center gap-6 sm:min-w-[24rem] lg:h-[28rem]">
|
||||
<img
|
||||
src={feature.visual}
|
||||
alt={feature.title}
|
||||
className={cn(
|
||||
"h-96 w-full overflow-hidden rounded-lg border object-cover lg:h-full",
|
||||
index === 0 && "flex-1/2",
|
||||
"h-full w-full min-w-0 overflow-hidden rounded-lg border object-cover",
|
||||
index === 0 && "flex-[1/2]",
|
||||
)}
|
||||
/>
|
||||
|
||||
{index === 0 && <TestimonialCard testimonial={testimonial} />}
|
||||
</AnimationWrapper>
|
||||
{index === 0 ? <TestimonialCard testimonial={testimonial} /> : null}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -280,81 +216,88 @@ const DEFAULT_TESTIMONIAL: Testimonial = {
|
||||
designation: "با هوش مصنوعی سریعتر تصمیم بگیرید",
|
||||
};
|
||||
|
||||
function getActiveFeatureIndex(progress: number, total: number) {
|
||||
if (total <= 0) return 0;
|
||||
if (progress >= 1) return total - 1;
|
||||
return Math.min(total - 1, Math.floor(progress * total));
|
||||
}
|
||||
|
||||
const Feature322 = ({
|
||||
className,
|
||||
features = DEFAULT_FEATURES,
|
||||
testimonial = DEFAULT_TESTIMONIAL,
|
||||
}: Feature322Props) => {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
const scrollRef = useRef<HTMLElement>(null);
|
||||
const { scrollYProgress } = useScroll({
|
||||
container: ref,
|
||||
container: scrollRef,
|
||||
});
|
||||
const [activeIndex, setActiveIndex] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = scrollYProgress.on("change", (latest) => {
|
||||
let minDistance = Infinity;
|
||||
let temp = 0;
|
||||
features.forEach((_, index) => {
|
||||
const range = getAnimationRange(index, features.length);
|
||||
const position = range[1];
|
||||
const distance = Math.abs(latest - position);
|
||||
if (distance < minDistance) {
|
||||
minDistance = distance;
|
||||
temp = index;
|
||||
}
|
||||
});
|
||||
setActiveIndex(temp);
|
||||
});
|
||||
useMotionValueEvent(scrollYProgress, "change", (latest) => {
|
||||
const nextIndex = getActiveFeatureIndex(latest, features.length);
|
||||
setActiveIndex((prev) => (prev === nextIndex ? prev : nextIndex));
|
||||
});
|
||||
|
||||
return () => unsubscribe();
|
||||
}, [features, scrollYProgress]);
|
||||
const activeFeature = features[activeIndex] ?? features[0];
|
||||
|
||||
return (
|
||||
<section
|
||||
ref={ref}
|
||||
ref={scrollRef}
|
||||
dir="rtl"
|
||||
lang="fa"
|
||||
className={cn("h-screen overflow-y-auto", className)}
|
||||
className={cn(
|
||||
"relative h-svh w-full min-w-[20rem] max-w-full overflow-x-clip overflow-y-auto sm:min-w-[36rem] lg:min-w-[64rem]",
|
||||
className,
|
||||
)}
|
||||
>
|
||||
<div className="container">
|
||||
<div className="sticky top-0 hidden h-screen w-full flex-col items-center justify-center gap-10 lg:flex">
|
||||
<div className="relative h-full w-full lg:h-1/2">
|
||||
<AnimatePresence mode="popLayout">
|
||||
<FeatureCard
|
||||
key={features[activeIndex]?.id ?? `feature-${activeIndex}`}
|
||||
index={activeIndex}
|
||||
feature={features[activeIndex]}
|
||||
testimonial={testimonial}
|
||||
totalFeatures={features.length}
|
||||
scrollYProgress={scrollYProgress}
|
||||
/>
|
||||
</AnimatePresence>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex w-full flex-col items-center justify-center gap-10 overflow-y-auto py-32 lg:hidden">
|
||||
<div className="relative h-full w-full lg:h-1/2">
|
||||
{features.map((feature, index) => {
|
||||
return (
|
||||
<FeatureCard
|
||||
key={feature.id}
|
||||
index={index}
|
||||
feature={feature}
|
||||
testimonial={testimonial}
|
||||
totalFeatures={features.length}
|
||||
scrollYProgress={scrollYProgress}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
<div className="container min-w-0 w-full max-w-full">
|
||||
{/* Desktop: sticky stage + scroll spacer */}
|
||||
<div className="relative hidden lg:block">
|
||||
<div className="sticky top-0 flex h-svh w-full min-w-0 flex-col items-center justify-center">
|
||||
<div className="relative w-full min-w-0 max-w-full">
|
||||
<AnimatePresence mode="wait" initial={false}>
|
||||
{activeFeature ? (
|
||||
<motion.div
|
||||
key={activeFeature.id}
|
||||
initial={{ opacity: 0, x: -12 }}
|
||||
animate={{ opacity: 1, x: 0 }}
|
||||
exit={{ opacity: 0, x: 12 }}
|
||||
transition={{ duration: 0.35, ease: "easeOut" }}
|
||||
className="w-full min-w-0"
|
||||
>
|
||||
<FeatureCard
|
||||
index={activeIndex}
|
||||
feature={activeFeature}
|
||||
testimonial={testimonial}
|
||||
totalFeatures={features.length}
|
||||
scrollYProgress={scrollYProgress}
|
||||
/>
|
||||
</motion.div>
|
||||
) : null}
|
||||
</AnimatePresence>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
style={{ height: `${Math.max(features.length, 1) * 75}vh` }}
|
||||
className="pointer-events-none w-full"
|
||||
aria-hidden
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div
|
||||
style={{
|
||||
height: `${features.length * 75}vh`,
|
||||
}}
|
||||
className="hidden lg:block"
|
||||
/>
|
||||
{/* Mobile / tablet: stacked cards, no crossfade thrash */}
|
||||
<div className="flex w-full min-w-0 flex-col items-center justify-center gap-10 py-16 lg:hidden">
|
||||
{features.map((feature, index) => (
|
||||
<FeatureCard
|
||||
key={feature.id}
|
||||
index={index}
|
||||
feature={feature}
|
||||
testimonial={testimonial}
|
||||
totalFeatures={features.length}
|
||||
scrollYProgress={scrollYProgress}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user