-
Notifications
You must be signed in to change notification settings - Fork 80
/
AvoidKeyboard.tsx
83 lines (68 loc) · 1.88 KB
/
AvoidKeyboard.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
import { styled } from 'styled-components';
import { useState, useRef } from 'react';
import { Sheet, type SheetRef } from 'react-modal-sheet';
import { Button } from './common';
import { useAnimatedVirtualKeyboard } from './hooks';
export function AvoidKeyboard() {
const [isOpen, setOpen] = useState(false);
const sheetRef = useRef<SheetRef>();
const inputRef = useRef<HTMLInputElement>(null);
const { keyboardHeight, isKeyboardOpen } = useAnimatedVirtualKeyboard();
const open = () => setOpen(true);
const close = () => setOpen(false);
return (
<>
<Button onClick={open}>Avoid Keyboard</Button>
<NoteText>
Note that keyboard avoidance only works on mobile devices!
</NoteText>
<Sheet
ref={sheetRef}
isOpen={isOpen}
onClose={close}
detent="content-height"
>
<Sheet.Container>
<Sheet.Header />
<Sheet.Content style={{ paddingBottom: keyboardHeight }}>
<Sheet.Scroller>
<Content>
<p>Focus input to show virtual keyboard</p>
<Input ref={inputRef} />
{isKeyboardOpen ? (
<strong>Virtual keyboard is open!</strong>
) : (
<Button onClick={close}>Close</Button>
)}
</Content>
</Sheet.Scroller>
</Sheet.Content>
</Sheet.Container>
<Sheet.Backdrop />
</Sheet>
</>
);
}
const NoteText = styled.strong`
text-align: center;
padding: 32px;
line-height: 1.5;
font-size: 14px;
`;
const Content = styled.div`
display: flex;
flex-direction: column;
padding: 16px;
padding-top: 0px;
`;
const Input = styled.input`
padding: 12px;
font-size: 16px;
border: 2px solid #ccc;
border-radius: 8px;
margin-bottom: 16px;
outline: none;
&:focus {
border-color: #0070f3;
}
`;