feat: upgrade nextjs
This commit is contained in:
1 parent
750d5b12c8
commit
29f6de300b
16 files changed
+365
-344
No files matched your search
@@ -1,9 +1,10 @@
|
||||
'use client';
|
||||
|
||||
import { forwardRef, ReactNode, startTransition, useEffect, useRef, useState } from 'react';
|
||||
import { StateSnapshot, Virtuoso, VirtuosoHandle, VirtuosoProps } from 'react-virtuoso';
|
||||
import { forwardRef, ReactNode, startTransition, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
||||
import { Virtuoso, VirtuosoHandle, VirtuosoProps } from 'react-virtuoso';
|
||||
|
||||
import Card from '@/components/Movie/Card';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
const components: VirtuosoProps<Movie, number>['components'] = {
|
||||
Item: (props) => <div {...props} className='shrink-0' style={{ ...props.style, width: props.context }} />,
|
||||
@@ -29,29 +30,55 @@ export const Measurer = ({ children }: { children: (width: number) => ReactNode
|
||||
);
|
||||
};
|
||||
|
||||
let snapshot: StateSnapshot;
|
||||
|
||||
export const Carousel = ({
|
||||
movies,
|
||||
width: initialWidth,
|
||||
index,
|
||||
onIndexChange
|
||||
}: {
|
||||
movies?: Movie[];
|
||||
width: number;
|
||||
index: number;
|
||||
onIndexChange: (index: number) => void;
|
||||
}) => {
|
||||
const timeout = useRef<NodeJS.Timeout>();
|
||||
const isMobile = matchMedia('(max-width: 500px)').matches;
|
||||
const ref = useRef<VirtuosoHandle>(null);
|
||||
const initialIndex = useRef(index);
|
||||
const [snap, setSnap] = useState(true);
|
||||
|
||||
const width = isMobile ? initialWidth : 300;
|
||||
const height = isMobile ? (width * 3) / 2 : 450;
|
||||
|
||||
useEffect(() => {
|
||||
const virtuoso = ref.current;
|
||||
return () => virtuoso?.getState((state) => (snapshot = state));
|
||||
useLayoutEffect(() => {
|
||||
if (!ref.current) return;
|
||||
|
||||
ref.current.scrollIntoView({ index: initialIndex.current });
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const scrollTo = (event: globalThis.KeyboardEvent) => {
|
||||
if (event.key === 'ArrowLeft') {
|
||||
if (index <= 0) return;
|
||||
setSnap(false);
|
||||
ref.current?.scrollIntoView({ index: index - 1, behavior: 'smooth' });
|
||||
}
|
||||
|
||||
if (event.key === 'ArrowRight') {
|
||||
if (index >= (movies?.length || 0) - 1) return;
|
||||
setSnap(false);
|
||||
ref.current?.scrollIntoView({ index: index + 1, behavior: 'smooth' });
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('keydown', scrollTo);
|
||||
|
||||
return () => {
|
||||
document.removeEventListener('keydown', scrollTo);
|
||||
setTimeout(() => setSnap(true), 100);
|
||||
};
|
||||
}, [index, movies?.length]);
|
||||
|
||||
return (
|
||||
<Virtuoso
|
||||
ref={ref}
|
||||
@@ -62,7 +89,6 @@ export const Carousel = ({
|
||||
horizontalDirection
|
||||
context={width}
|
||||
increaseViewportBy={154}
|
||||
restoreStateFrom={snapshot}
|
||||
components={components}
|
||||
itemContent={(index) => {
|
||||
clearTimeout(timeout.current);
|
||||
@@ -73,7 +99,7 @@ export const Carousel = ({
|
||||
startTransition(() => onIndexChange(currentIndex));
|
||||
}, 50);
|
||||
|
||||
return <Card className='snap-start' movie={movies?.[index]} />;
|
||||
return <Card className={cn(snap && 'snap-start')} movie={movies?.[index]} />;
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user