first project version mrDecor
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
'use client';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { useMotionValue, animate, motion } from 'motion/react';
|
||||
import { useState, useEffect } from 'react';
|
||||
import useMeasure from 'react-use-measure';
|
||||
|
||||
export type InfiniteSliderProps = {
|
||||
children: React.ReactNode;
|
||||
gap?: number;
|
||||
speed?: number;
|
||||
speedOnHover?: number;
|
||||
direction?: 'horizontal' | 'vertical';
|
||||
reverse?: boolean;
|
||||
className?: string;
|
||||
};
|
||||
|
||||
export function InfiniteSlider({
|
||||
children,
|
||||
gap = 16,
|
||||
speed = 100,
|
||||
speedOnHover,
|
||||
direction = 'horizontal',
|
||||
reverse = false,
|
||||
className,
|
||||
}: InfiniteSliderProps) {
|
||||
const [currentSpeed, setCurrentSpeed] = useState(speed);
|
||||
const [ref, { width, height }] = useMeasure();
|
||||
const translation = useMotionValue(0);
|
||||
const [isTransitioning, setIsTransitioning] = useState(false);
|
||||
const [key, setKey] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
let controls;
|
||||
const size = direction === 'horizontal' ? width : height;
|
||||
const contentSize = size + gap;
|
||||
const from = reverse ? -contentSize / 2 : 0;
|
||||
const to = reverse ? 0 : -contentSize / 2;
|
||||
|
||||
const distanceToTravel = Math.abs(to - from);
|
||||
const duration = distanceToTravel / currentSpeed;
|
||||
|
||||
if (isTransitioning) {
|
||||
const remainingDistance = Math.abs(translation.get() - to);
|
||||
const transitionDuration = remainingDistance / currentSpeed;
|
||||
|
||||
controls = animate(translation, [translation.get(), to], {
|
||||
ease: 'linear',
|
||||
duration: transitionDuration,
|
||||
onComplete: () => {
|
||||
setIsTransitioning(false);
|
||||
setKey((prevKey) => prevKey + 1);
|
||||
},
|
||||
});
|
||||
} else {
|
||||
controls = animate(translation, [from, to], {
|
||||
ease: 'linear',
|
||||
duration: duration,
|
||||
repeat: Infinity,
|
||||
repeatType: 'loop',
|
||||
repeatDelay: 0,
|
||||
onRepeat: () => {
|
||||
translation.set(from);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return controls?.stop;
|
||||
}, [
|
||||
key,
|
||||
translation,
|
||||
currentSpeed,
|
||||
width,
|
||||
height,
|
||||
gap,
|
||||
isTransitioning,
|
||||
direction,
|
||||
reverse,
|
||||
]);
|
||||
|
||||
const hoverProps = speedOnHover
|
||||
? {
|
||||
onHoverStart: () => {
|
||||
setIsTransitioning(true);
|
||||
setCurrentSpeed(speedOnHover);
|
||||
},
|
||||
onHoverEnd: () => {
|
||||
setIsTransitioning(true);
|
||||
setCurrentSpeed(speed);
|
||||
},
|
||||
}
|
||||
: {};
|
||||
|
||||
return (
|
||||
<div className={cn('overflow-hidden', className)}>
|
||||
<motion.div
|
||||
className='flex w-max'
|
||||
style={{
|
||||
...(direction === 'horizontal'
|
||||
? { x: translation }
|
||||
: { y: translation }),
|
||||
gap: `${gap}px`,
|
||||
flexDirection: direction === 'horizontal' ? 'row' : 'column',
|
||||
}}
|
||||
ref={ref}
|
||||
{...hoverProps}
|
||||
>
|
||||
{children}
|
||||
{children}
|
||||
</motion.div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
'use client';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { HTMLMotionProps, motion } from 'motion/react';
|
||||
|
||||
export const GRADIENT_ANGLES = {
|
||||
top: 0,
|
||||
right: 90,
|
||||
bottom: 180,
|
||||
left: 270,
|
||||
};
|
||||
|
||||
export type ProgressiveBlurProps = {
|
||||
direction?: keyof typeof GRADIENT_ANGLES;
|
||||
blurLayers?: number;
|
||||
className?: string;
|
||||
blurIntensity?: number;
|
||||
} & HTMLMotionProps<'div'>;
|
||||
|
||||
export function ProgressiveBlur({
|
||||
direction = 'bottom',
|
||||
blurLayers = 8,
|
||||
className,
|
||||
blurIntensity = 0.25,
|
||||
...props
|
||||
}: ProgressiveBlurProps) {
|
||||
const layers = Math.max(blurLayers, 2);
|
||||
const segmentSize = 1 / (blurLayers + 1);
|
||||
|
||||
return (
|
||||
<div className={cn('relative', className)}>
|
||||
{Array.from({ length: layers }).map((_, index) => {
|
||||
const angle = GRADIENT_ANGLES[direction];
|
||||
const gradientStops = [
|
||||
index * segmentSize,
|
||||
(index + 1) * segmentSize,
|
||||
(index + 2) * segmentSize,
|
||||
(index + 3) * segmentSize,
|
||||
].map(
|
||||
(pos, posIndex) =>
|
||||
`rgba(255, 255, 255, ${posIndex === 1 || posIndex === 2 ? 1 : 0}) ${pos * 100}%`
|
||||
);
|
||||
|
||||
const gradient = `linear-gradient(${angle}deg, ${gradientStops.join(
|
||||
', '
|
||||
)})`;
|
||||
|
||||
return (
|
||||
<motion.div
|
||||
key={index}
|
||||
className='pointer-events-none absolute inset-0 rounded-[inherit]'
|
||||
style={{
|
||||
maskImage: gradient,
|
||||
WebkitMaskImage: gradient,
|
||||
backdropFilter: `blur(${index * blurIntensity}px)`,
|
||||
WebkitBackdropFilter: `blur(${index * blurIntensity}px)`,
|
||||
}}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user