summaryrefslogtreecommitdiff
path: root/client/src/components/Home.tsx
blob: 558c1044f1298c2a5e81a3c3f293576096f14d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React from 'react';
import { useEffect, useRef, useState } from 'react';

function Home() {
  const [index, setIndex] = useState(0);
  const colors = ['#FF0000', '#00FF00', '#0000FF'];
  const timeout = useRef(0);

  useEffect(() => {
    resetTimeout();
    timeout.current = window.setTimeout(
      () =>
        setIndex((prevIndex) =>
          prevIndex === colors.length - 1 ? 0 : prevIndex + 1
        ),
      2500
    );

    return () => {
      resetTimeout();
    };
  }, [index]);

  const resetTimeout = () => {
    if (timeout.current) {
      clearTimeout(timeout.current);
    }
  };

  return (
    <div style={{ margin: 'auto', overflow: 'hidden' }}>
      <div
        style={{
          whiteSpace: 'nowrap',
          transform: `translateX(${-index * 100}%)`,
          transition: 'ease 1000ms',
        }}
      >
        {colors.map((backgroundColor, colorIndex) => (
          <div
            key={colorIndex}
            style={{
              display: 'inline-block',
              backgroundColor,
              height: '80vh',
              width: '100%',
            }}
          />
        ))}
      </div>
    </div>
  );
}

export default Home;