akanjs 3.3.0-rc.27 → 3.3.0-rc.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/keyboardRide.ts +120 -28
- package/package.json +1 -1
- package/types/client/keyboardRide.d.ts +4 -2
package/client/keyboardRide.ts
CHANGED
|
@@ -4,7 +4,16 @@ const RIDE_SELECTOR = "[data-akan-keyboard-ride]";
|
|
|
4
4
|
const OVERLAY_SELECTOR = "[data-akan-keyboard-overlay]";
|
|
5
5
|
const END_SLACK_PX = 2;
|
|
6
6
|
|
|
7
|
+
const GLIDE_ATTRIBUTE = "data-akan-keyboard-glide";
|
|
8
|
+
|
|
7
9
|
const bottomOf = (element: HTMLElement) => element.getBoundingClientRect().bottom;
|
|
10
|
+
|
|
11
|
+
const isReversed = (scroller: HTMLElement) => getComputedStyle(scroller).flexDirection === "column-reverse";
|
|
12
|
+
const isAtEnd = (scroller: HTMLElement) =>
|
|
13
|
+
isReversed(scroller)
|
|
14
|
+
? Math.abs(scroller.scrollTop) <= END_SLACK_PX
|
|
15
|
+
: scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight <= END_SLACK_PX;
|
|
16
|
+
const endOf = (scroller: HTMLElement) => (isReversed(scroller) ? 0 : scroller.scrollHeight - scroller.clientHeight);
|
|
8
17
|
const isInset = (rider: HTMLElement) => rider.dataset.akanKeyboardRide === "inset";
|
|
9
18
|
|
|
10
19
|
/**
|
|
@@ -16,15 +25,18 @@ const isInset = (rider: HTMLElement) => rider.dataset.akanKeyboardRide === "inse
|
|
|
16
25
|
* up, since the shorter box would clip a rider still on its way, and keys going down give the taller box at once while
|
|
17
26
|
* the riders start from where they were drawn. When the focused field sits in `data-akan-keyboard-overlay`, the page
|
|
18
27
|
* keeps the whole screen, as a native chat screen does, so what lies under the glass keys stays drawn: the riders go
|
|
19
|
-
* where the shorter box would put them and stay there
|
|
20
|
-
*
|
|
28
|
+
* where the shorter box would put them and stay there. An `inset` rider (a list's content) is not carried: it takes the
|
|
29
|
+
* rise as bottom padding at once, so the list still scrolls to its top, and a list resting at its end scrolls the rise
|
|
30
|
+
* with the keys, and back down with them. While it does, its scroller carries `data-akan-keyboard-glide`: a list's own
|
|
31
|
+
* keep-at-end logic yields to it.
|
|
21
32
|
*/
|
|
22
33
|
export class KeyboardRide {
|
|
23
34
|
static #committed = 0;
|
|
24
35
|
static #target = 0;
|
|
25
36
|
static #isOverlay = false;
|
|
26
37
|
static #timer: ReturnType<typeof setTimeout> | null = null;
|
|
27
|
-
static #insets = new Map<HTMLElement, { paddingBottom: string; rise: number }>();
|
|
38
|
+
static #insets = new Map<HTMLElement, { paddingBottom: string; base: string; rise: number }>();
|
|
39
|
+
static #glides = new Map<HTMLElement, number>();
|
|
28
40
|
|
|
29
41
|
static move(height: number, durationMs: number) {
|
|
30
42
|
if (height === KeyboardRide.#target) return;
|
|
@@ -44,23 +56,34 @@ export class KeyboardRide {
|
|
|
44
56
|
if (KeyboardRide.#timer) clearTimeout(KeyboardRide.#timer);
|
|
45
57
|
KeyboardRide.#timer = null;
|
|
46
58
|
|
|
47
|
-
const
|
|
59
|
+
const shown = [...document.querySelectorAll<HTMLElement>(RIDE_SELECTOR)].filter(
|
|
48
60
|
(element) => element.getClientRects().length > 0,
|
|
49
61
|
);
|
|
50
62
|
|
|
63
|
+
const lists = isOverlay ? shown.filter(isInset) : [];
|
|
64
|
+
const riders = shown.filter((rider) => !lists.includes(rider));
|
|
65
|
+
KeyboardRide.#stopGlides();
|
|
66
|
+
const listEnds = lists.map((list) => KeyboardRide.#scrollersAtEnd([list]).at(0) ?? null);
|
|
67
|
+
|
|
51
68
|
const drawn = riders.map((rider) => bottomOf(rider) - (KeyboardRide.#insets.get(rider)?.rise ?? 0));
|
|
52
69
|
for (const rider of riders) {
|
|
53
70
|
rider.style.transition = "none";
|
|
54
71
|
rider.style.transform = "";
|
|
55
72
|
}
|
|
56
|
-
KeyboardRide.#releaseInsets();
|
|
57
|
-
const scrollers = KeyboardRide.#scrollersAtEnd(
|
|
73
|
+
KeyboardRide.#releaseInsets(lists);
|
|
74
|
+
const scrollers = KeyboardRide.#scrollersAtEnd(shown);
|
|
58
75
|
const previous = KeyboardRide.#committed;
|
|
59
76
|
const isHeld = !isOverlay && height > previous;
|
|
60
77
|
KeyboardRide.#commit(height, scrollers);
|
|
61
78
|
const settled = riders.map(bottomOf);
|
|
79
|
+
const listsSettled = lists.map(bottomOf);
|
|
62
80
|
if (isHeld || isOverlay) KeyboardRide.#commit(isOverlay ? 0 : previous, scrollers);
|
|
63
81
|
const laid = isHeld || isOverlay ? riders.map(bottomOf) : settled;
|
|
82
|
+
lists.forEach((list, idx) => {
|
|
83
|
+
const scroller = listEnds[idx] ?? null;
|
|
84
|
+
if (height > 0) KeyboardRide.#raiseList(list, bottomOf(list) - (listsSettled[idx] ?? 0), scroller, durationMs);
|
|
85
|
+
else KeyboardRide.#lowerList(list, scroller, durationMs);
|
|
86
|
+
});
|
|
64
87
|
riders.forEach((rider, idx) => {
|
|
65
88
|
rider.style.transform = `translateY(${drawn[idx] - laid[idx]}px)`;
|
|
66
89
|
});
|
|
@@ -73,9 +96,7 @@ export class KeyboardRide {
|
|
|
73
96
|
KeyboardRide.#timer = setTimeout(() => {
|
|
74
97
|
KeyboardRide.#timer = null;
|
|
75
98
|
if (isHeld) KeyboardRide.#commit(height, KeyboardRide.#scrollersAtEnd(riders));
|
|
76
|
-
const
|
|
77
|
-
if (isKept) KeyboardRide.#holdInsets(riders, settled, laid);
|
|
78
|
-
const settling = riders.filter((rider) => !isKept || isInset(rider));
|
|
99
|
+
const settling = isOverlay && height > 0 ? [] : riders;
|
|
79
100
|
for (const rider of settling) {
|
|
80
101
|
rider.style.transition = "none";
|
|
81
102
|
rider.style.transform = "";
|
|
@@ -90,34 +111,105 @@ export class KeyboardRide {
|
|
|
90
111
|
root.style.setProperty("--akan-keyboard-cover", `${height}px`);
|
|
91
112
|
root.toggleAttribute("data-akan-keyboard-layout", height > 0);
|
|
92
113
|
KeyboardRide.#committed = height;
|
|
93
|
-
for (const scroller of scrollers) scroller.scrollTop = scroller
|
|
114
|
+
for (const scroller of scrollers) scroller.scrollTop = endOf(scroller);
|
|
94
115
|
}
|
|
95
116
|
|
|
96
|
-
static #
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
117
|
+
static #raiseList(list: HTMLElement, rise: number, scroller: HTMLElement | null, durationMs: number) {
|
|
118
|
+
const held = KeyboardRide.#insets.get(list);
|
|
119
|
+
const base = held?.base ?? getComputedStyle(list).paddingBottom;
|
|
120
|
+
if (rise <= 0) return;
|
|
121
|
+
KeyboardRide.#insets.set(list, { paddingBottom: held?.paddingBottom ?? list.style.paddingBottom, base, rise });
|
|
122
|
+
list.style.paddingBottom = `calc(${base} + ${rise}px)`;
|
|
123
|
+
const outer = KeyboardRide.#scrollerOf(list);
|
|
124
|
+
|
|
125
|
+
if (outer && isReversed(outer)) outer.scrollTop -= rise - (held?.rise ?? 0);
|
|
126
|
+
if (scroller) KeyboardRide.#glide(scroller, endOf(scroller), durationMs);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
static #lowerList(list: HTMLElement, scroller: HTMLElement | null, durationMs: number) {
|
|
130
|
+
const held = KeyboardRide.#insets.get(list);
|
|
131
|
+
if (!held) return;
|
|
132
|
+
const outer = KeyboardRide.#scrollerOf(list);
|
|
133
|
+
const release = () => {
|
|
134
|
+
list.style.paddingBottom = held.paddingBottom;
|
|
135
|
+
KeyboardRide.#insets.delete(list);
|
|
136
|
+
if (outer && isReversed(outer)) outer.scrollTop += held.rise;
|
|
137
|
+
};
|
|
138
|
+
if (!scroller) return release();
|
|
139
|
+
const to = isReversed(scroller) ? -held.rise : Math.max(0, scroller.scrollTop - held.rise);
|
|
140
|
+
KeyboardRide.#glide(scroller, to, durationMs, release);
|
|
105
141
|
}
|
|
106
142
|
|
|
107
|
-
static #releaseInsets() {
|
|
108
|
-
for (const [rider, { paddingBottom }] of KeyboardRide.#insets)
|
|
109
|
-
|
|
143
|
+
static #releaseInsets(kept: HTMLElement[]) {
|
|
144
|
+
for (const [rider, { paddingBottom }] of KeyboardRide.#insets) {
|
|
145
|
+
if (kept.includes(rider)) continue;
|
|
146
|
+
rider.style.paddingBottom = paddingBottom;
|
|
147
|
+
KeyboardRide.#insets.delete(rider);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static #glide(scroller: HTMLElement, to: number, durationMs: number, done?: () => void) {
|
|
152
|
+
const from = scroller.scrollTop;
|
|
153
|
+
scroller.setAttribute(GLIDE_ATTRIBUTE, "");
|
|
154
|
+
const ease = KeyboardRide.#easeOf(nativeKeyboardEasing());
|
|
155
|
+
const startedAt = performance.now();
|
|
156
|
+
const step = (now: number) => {
|
|
157
|
+
const progress = durationMs > 0 ? Math.min(1, (now - startedAt) / durationMs) : 1;
|
|
158
|
+
scroller.scrollTop = from + (to - from) * ease(progress);
|
|
159
|
+
if (progress < 1) {
|
|
160
|
+
KeyboardRide.#glides.set(scroller, requestAnimationFrame(step));
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
KeyboardRide.#glides.delete(scroller);
|
|
164
|
+
done?.();
|
|
165
|
+
scroller.removeAttribute(GLIDE_ATTRIBUTE);
|
|
166
|
+
};
|
|
167
|
+
KeyboardRide.#glides.set(scroller, requestAnimationFrame(step));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
static #stopGlides() {
|
|
171
|
+
for (const [scroller, frame] of KeyboardRide.#glides) {
|
|
172
|
+
cancelAnimationFrame(frame);
|
|
173
|
+
scroller.removeAttribute(GLIDE_ATTRIBUTE);
|
|
174
|
+
}
|
|
175
|
+
KeyboardRide.#glides.clear();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
static #easeOf(easing: string) {
|
|
179
|
+
const parsed = easing
|
|
180
|
+
.replace(/^cubic-bezier\(|\)$/g, "")
|
|
181
|
+
.split(",")
|
|
182
|
+
.map(Number);
|
|
183
|
+
const [x1, y1, x2, y2] = parsed.length === 4 && parsed.every(Number.isFinite) ? parsed : [0.25, 0.1, 0.25, 1];
|
|
184
|
+
const curve = (a: number, b: number, t: number) => 3 * a * (1 - t) ** 2 * t + 3 * b * (1 - t) * t ** 2 + t ** 3;
|
|
185
|
+
return (progress: number) => {
|
|
186
|
+
let low = 0;
|
|
187
|
+
let high = 1;
|
|
188
|
+
let t = progress;
|
|
189
|
+
for (let step = 0; step < 24; step++) {
|
|
190
|
+
const x = curve(x1, x2, t);
|
|
191
|
+
if (Math.abs(x - progress) < 1e-4) break;
|
|
192
|
+
if (x < progress) low = t;
|
|
193
|
+
else high = t;
|
|
194
|
+
t = (low + high) / 2;
|
|
195
|
+
}
|
|
196
|
+
return curve(y1, y2, t);
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
static #scrollerOf(rider: HTMLElement) {
|
|
201
|
+
for (let element = rider.parentElement; element; element = element.parentElement) {
|
|
202
|
+
const { overflowY } = getComputedStyle(element);
|
|
203
|
+
if (overflowY === "auto" || overflowY === "scroll") return element;
|
|
204
|
+
}
|
|
205
|
+
return null;
|
|
110
206
|
}
|
|
111
207
|
|
|
112
208
|
static #scrollersAtEnd(riders: HTMLElement[]) {
|
|
113
209
|
const scrollers = new Set<HTMLElement>();
|
|
114
210
|
for (const rider of riders) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (overflowY !== "auto" && overflowY !== "scroll") continue;
|
|
118
|
-
if (element.scrollHeight - element.scrollTop - element.clientHeight <= END_SLACK_PX) scrollers.add(element);
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
211
|
+
const scroller = KeyboardRide.#scrollerOf(rider);
|
|
212
|
+
if (scroller && isAtEnd(scroller)) scrollers.add(scroller);
|
|
121
213
|
}
|
|
122
214
|
return [...scrollers];
|
|
123
215
|
}
|
package/package.json
CHANGED
|
@@ -7,8 +7,10 @@
|
|
|
7
7
|
* up, since the shorter box would clip a rider still on its way, and keys going down give the taller box at once while
|
|
8
8
|
* the riders start from where they were drawn. When the focused field sits in `data-akan-keyboard-overlay`, the page
|
|
9
9
|
* keeps the whole screen, as a native chat screen does, so what lies under the glass keys stays drawn: the riders go
|
|
10
|
-
* where the shorter box would put them and stay there
|
|
11
|
-
*
|
|
10
|
+
* where the shorter box would put them and stay there. An `inset` rider (a list's content) is not carried: it takes the
|
|
11
|
+
* rise as bottom padding at once, so the list still scrolls to its top, and a list resting at its end scrolls the rise
|
|
12
|
+
* with the keys, and back down with them. While it does, its scroller carries `data-akan-keyboard-glide`: a list's own
|
|
13
|
+
* keep-at-end logic yields to it.
|
|
12
14
|
*/
|
|
13
15
|
export declare class KeyboardRide {
|
|
14
16
|
#private;
|