-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
251 lines (247 loc) · 8.85 KB
/
index.html
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, interactive-widget=resizes-content, maximum-scale=1"
/>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Roboto:wght@500&display=swap"
rel="stylesheet"
/>
<title>Safari Virtual Keyboard Demo</title>
<style>
body {
padding: 0;
font-family: "Roboto", sans-serif;
}
body.virtual-keyboard-shown {
margin-top: var(--visual-viewport-offset-top, 0px);
}
#root {
width: 100vw;
height: calc(100 * var(--svh, 1svh));
overflow-y: scroll;
overflow-x: hidden;
}
.virtual-keyboard-shown #root,
#root:has(input:focus),
#root:has([contenteditable="true"]:focus) {
overscroll-behavior-y: contain;
}
header {
position: sticky;
top: 0;
}
footer {
position: sticky;
bottom: 0;
}
#editor {
min-height: calc(100 * var(--svh, 1svh) - 96px);
font-size: 24px;
}
#editor:has(:focus),
.virtual-keyboard-shown #editor {
min-height: calc(100 * var(--svh, 1svh) - 96px + 1px);
}
* {
box-sizing: border-box;
margin: 0;
}
button {
font-size: 16px;
padding: 8px;
appearance: none;
}
header,
footer {
display: flex;
align-items: center;
justify-content: stretch;
height: 48px;
width: 100%;
background-color: lightgray;
}
footer > button {
height: 100%;
flex: 1;
}
dialog {
border-style: none;
background-color: black;
}
#stickyDialog {
position: sticky;
bottom: 48px;
width: 100%;
}
#fixedDialog {
inset: 0;
top: var(--visual-viewport-offset-top, 0px);
bottom: calc(
100svh - var(--visual-viewport-offset-top, 0px) - var(--svh, 1svh) *
100
);
position: fixed;
margin: auto;
height: 150px;
width: 70svw;
overflow-y: scroll;
overflow-x: hidden;
overscroll-behavior-y: contain;
}
#fixedDialog > div {
min-height: 151px;
display: flex;
flex-direction: column;
}
h1 {
font-size: 24px;
}
</style>
</head>
<body>
<div id="root" class="scrollable">
<header id="header"><h1>Safari Virtual Keyboard Demo</h1></header>
<main>
<div id="editor" contenteditable="true">
<p style="color: darkred">
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.
</p>
<p style="color: darkgoldenrod">
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.
</p>
<p style="color: darkolivegreen">
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.
</p>
<p style="color: darkslateblue">
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.
</p>
</div>
</main>
<dialog id="stickyDialog">
<input placeholder="https://..." />
<button id="stickyCloseButton" type="button">Close</button>
</dialog>
<footer id="footer">
<button id="stickyButton" type="button">Sticky</button>
<button id="fixedButton" type="button">Fixed</button>
<button id="clearButton">Clear</button>
</footer>
<dialog id="fixedDialog" class="scrollable">
<div>
<button id="fixedCloseButton" type="button">Close</button>
<button type="button">1</button>
<button type="button">2</button>
<button type="button">3</button>
<button type="button">4</button>
</div>
</dialog>
</div>
</body>
<script>
(() => {
const stickyButton = document.getElementById("stickyButton");
const stickyCloseButton = document.getElementById("stickyCloseButton");
const stickyDialog = document.getElementById("stickyDialog");
stickyButton.addEventListener("click", () => {
stickyDialog.show();
});
stickyButton.addEventListener("mousedown", (e) => {e.preventDefault()});
stickyCloseButton.addEventListener("click", () => {
stickyDialog.close();
});
const fixedButton = document.getElementById("fixedButton");
const fixedCloseButton = document.getElementById("fixedCloseButton");
const fixedDialog = document.getElementById("fixedDialog");
fixedButton.addEventListener("mousedown", (e) => {e.preventDefault()});
fixedButton.addEventListener("click", () => {
fixedDialog.show();
});
fixedCloseButton.addEventListener("click", () => {
fixedDialog.close();
});
const clearButton = document.getElementById("clearButton");
const editorElem = document.getElementById("editor");
clearButton.addEventListener("mousedown", (e) => {e.preventDefault()});
clearButton.addEventListener('click', () => {
editorElem.innerHTML = '';
});
})();
(() => {
const ua = window.navigator.userAgent;
const iOS = !!ua.match(/iPad/i) || !!ua.match(/iPhone/i);
if (!iOS) return;
if (!self.visualViewport) return;
let prevHeight = undefined;
let prevOffsetTop = undefined;
let timerId = undefined;
const scrollElem = document.getElementById("root");
function handleResize(e) {
const height = self.visualViewport.height * self.visualViewport.scale;
if (prevHeight !== height) {
prevHeight = height;
requestAnimationFrame(() => {
document.documentElement.style.setProperty(
"--svh",
height * 0.01 + "px"
);
});
}
if (prevOffsetTop !== self.visualViewport.offsetTop) {
if (prevOffsetTop === undefined) {
prevOffsetTop = self.visualViewport.offsetTop;
} else {
const scrollOffset = self.visualViewport.offsetTop - prevOffsetTop;
prevOffsetTop = self.visualViewport.offsetTop;
requestAnimationFrame(() => {
if (e && e.type === "resize") {
scrollElem.scrollBy(0, scrollOffset);
}
document.documentElement.style.setProperty(
"--visual-viewport-offset-top",
self.visualViewport.offsetTop + "px"
);
});
}
}
if (height + 10 < document.documentElement.clientHeight) {
document.body.classList.add("virtual-keyboard-shown");
} else {
document.body.classList.remove("virtual-keyboard-shown");
}
}
handleResize();
self.visualViewport.addEventListener("resize", handleResize);
self.visualViewport.addEventListener("scroll", handleResize);
})();
</script>
</html>