1- import { useEffect , useMemo , useRef , useState } from "react" ;
2- import { useSize } from "../../../hooks" ;
31import { Stack } from "./stack" ;
42
53import StackViewModule from "./StackView.module.scss" ;
64
7- export interface StackViewPorps {
8- height : number ;
9- }
10-
115export function StackView ( ) {
12- const { ref, width, height } = useSize ( ) ;
13- const [ slide , setSlide ] = useState ( 0 ) ;
14- const [ direction , setDirection ] = useState ( 1 ) ;
15- const [ offset , setOffset ] = useState ( 0 ) ;
16-
17- const blocks = useMemo ( ( ) => Math . min ( Math . floor ( width / 144 ) , 5 ) , [ width ] ) ;
18- const intervalRef = useRef < any > ( ) ;
19- const imageWidth = useMemo ( ( ) => width / blocks , [ blocks , width ] ) ;
20- const imageHeight = useMemo ( ( ) => height * .6 , [ height ] ) ;
21-
22- useEffect (
23- ( ) => {
24- if ( intervalRef . current ) {
25- clearInterval ( intervalRef . current ) ;
26- }
27-
28- intervalRef . current = setInterval (
29- ( ) => {
30- let newIndex = slide + direction ;
31-
32- if ( newIndex <= 0 ) {
33- newIndex = slide + 1 ;
34- } else if ( newIndex >= Stack . length / 2 ) {
35- newIndex = slide - 1 ;
36- }
37-
38- setDirection ( newIndex - slide ) ;
39- setSlide ( newIndex ) ;
40-
41- setOffset ( - imageWidth * newIndex ) ;
42- } ,
43- 2500
44- ) ;
45-
46- return ( ) => clearInterval ( intervalRef . current ) ;
47- } ,
48- [ imageWidth , slide ]
49- ) ;
50-
51- return < div
52- className = { StackViewModule . container }
53- ref = { ref }
54- >
55- {
56- isNaN ( imageWidth ) || isNaN ( imageHeight ) ? null : Stack . map (
57- ( item , i ) => < div
58- key = { item }
6+ return < div className = { StackViewModule . container } >
7+ < div className = { StackViewModule . track } >
8+ {
9+ [ ...Stack , ...Stack ] . map ( ( item , i ) => < div
10+ key = { i }
5911 className = { StackViewModule . logo }
60- style = {
61- {
62- position : 'relative' ,
63- left : offset ,
64- width : imageWidth ,
65- height : imageHeight
66- }
67- }
6812 >
6913 < img
7014 src = { item }
71- width = { imageWidth }
7215 className = { StackViewModule . sponsor }
73- style = {
74- {
75- height : imageHeight
76- }
77- }
16+ alt = ""
17+ aria-hidden = { i >= Stack . length }
7818 />
79- </ div >
80- )
81- }
19+ </ div > )
20+ }
21+ </ div >
8222 </ div > ;
83- }
23+ }
0 commit comments