-
Notifications
You must be signed in to change notification settings - Fork 74
/
SnapPoints.tsx
77 lines (65 loc) 路 1.93 KB
/
SnapPoints.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
import * as React from 'react';
import styled from 'styled-components';
import Sheet, { SheetRef } from '../../src';
import { Button } from './common';
import { useMetaThemeColor } from './hooks';
const snapPoints = [-50, 0.5, 200, 0];
const initialSnap = 1; // Initial snap point when sheet is opened
const SnapPoints = () => {
const ref = React.useRef<SheetRef>();
const [isOpen, setOpen] = React.useState(false);
const [snapPoint, setSnapPoint] = React.useState(initialSnap);
const snapTo = (i: number) => ref.current?.snapTo(i);
const open = () => setOpen(true);
const close = () => setOpen(false);
React.useEffect(() => {
console.log('> Current snap point', snapPoint);
}, [snapPoint]);
useMetaThemeColor({
when: isOpen,
from: '#fff',
to: '#000',
});
return (
<>
<Button onClick={open}>Bottom Sheet with Snap Points</Button>
<Sheet
ref={ref}
isOpen={isOpen}
onClose={close}
onSnap={setSnapPoint}
snapPoints={snapPoints}
initialSnap={initialSnap}
rootId="root"
>
<Sheet.Container>
<Sheet.Content>
<SheetContentWrapper>
<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>
</SheetContentWrapper>
</Sheet.Content>
</Sheet.Container>
<Sheet.Backdrop onTap={close} />
</Sheet>
</>
);
};
const SheetContentWrapper = styled.div`
height: 100%;
display: flex;
flex-direction: column;
padding: 16px;
`;
const Controls = styled.div`
display: flex;
flex-direction: column;
align-items: center;
`;
export default SnapPoints;