-
Notifications
You must be signed in to change notification settings - Fork 81
/
ScrollableSnapPoints.tsx
88 lines (75 loc) · 2.13 KB
/
ScrollableSnapPoints.tsx
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { useState, useRef } from 'react';
import { styled } from 'styled-components';
import { Sheet, type SheetRef } from 'react-modal-sheet';
import { Button } from './common';
const snapPoints = [-50, 0.5, 200, 0];
const initialSnap = 1;
export function ScrollableSnapPoints() {
const ref = useRef<SheetRef>();
const [isOpen, setOpen] = useState(false);
const snapTo = (i: number) => ref.current?.snapTo(i);
const open = () => setOpen(true);
const close = () => setOpen(false);
return (
<>
<Button onClick={open}>Scrollable + Snap points</Button>
<Sheet
ref={ref}
isOpen={isOpen}
onClose={close}
snapPoints={snapPoints}
initialSnap={initialSnap}
>
<Sheet.Container>
<Sheet.Header />
<Sheet.Content style={{ paddingBottom: ref.current?.y }}>
<Sheet.Scroller draggableAt="both">
<BoxList>
<Controls>
<Button onClick={() => snapTo(0)}>
Snap to -50 (from top)
</Button>
<Button onClick={() => snapTo(1)}>Snap to 50%</Button>
<Button onClick={() => snapTo(2)}>Snap to 200</Button>
<Button onClick={() => snapTo(3)}>Snap to 0 (close)</Button>
</Controls>
{Array.from({ length: 20 })
.fill(1)
.map((_, i) => (
<Box key={i}>{i + 1}</Box>
))}
</BoxList>
</Sheet.Scroller>
</Sheet.Content>
</Sheet.Container>
<Sheet.Backdrop />
</Sheet>
</>
);
}
const BoxList = styled.div`
display: flex;
flex-direction: column;
padding: 16px;
padding-top: 0px;
`;
const Box = styled.div`
background-color: #eee;
border-radius: 12px;
min-height: 200px;
margin-bottom: 16px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 700;
font-size: 24px;
&:last-of-type {
margin-bottom: 0px;
}
`;
const Controls = styled.div`
display: flex;
flex-direction: column;
align-items: center;
padding-bottom: 24px;
`;