-
Notifications
You must be signed in to change notification settings - Fork 0
/
pro.js
52 lines (46 loc) · 2.04 KB
/
pro.js
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
import React, {useState} from 'https://cdn.skypack.dev/react';
import ReactDOM from 'https://cdn.skypack.dev/react-dom';
import {TiChevronLeftOutline, TiChevronRightOutline} from 'https://cdn.skypack.dev/react-icons/ti';
const CARDS = 10;
const MAX_VISIBILITY = 3;
const Card = ({title, content}) => (
<div className='card'>
<h2>{title}</h2>
<p>{content}</p>
</div>
);
const Carousel = ({children}) => {
const [active, setActive] = useState(2);
const count = React.Children.count(children);
return (
<div className='carousel'>
{active > 0 && <button className='nav left' onClick={() => setActive(i => i - 1)}><TiChevronLeftOutline/></button>}
{React.Children.map(children, (child, i) => (
<div className='card-container' style={{
'--active': i === active ? 1 : 0,
'--offset': (active - i) / 3,
'--abs-offset': Math.abs(active - i) / 3,
'pointer-events': active === i ? 'auto' : 'none',
'opacity': Math.abs(active - i) >= MAX_VISIBILITY ? '0' : '1',
'display': Math.abs(active - i) > MAX_VISIBILITY ? 'none' : 'block',
}}>
{child}
</div>
))}
{active < count - 1 && <button className='nav right' onClick={() => setActive(i => i + 1)}><TiChevronRightOutline/></button>}
</div>
);
};
const App = () => (
<div className='app'>
<Carousel>
{[...new Array(CARDS)].map((_, i) => (
<Card title={'Card ' + (i + 1)} content='Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'/>
))}
</Carousel>
</div>
);
ReactDOM.render(
<App/>,
document.body
);