Skip to content

Commit 9fc73e7

Browse files
committed
feat(keyboard): ride UIKit's keyboard motion in Safari on iOS
The keyboard layout guide reads UIKit's keyboard schedule where WebKit on iOS runs. Core's surfaces ride UIKit's measured keyboard spring: the show from Safari's one report, the hide from the blur. Records: AST-099 FR6, FR8, DEC-12.
1 parent ff55fb2 commit 9fc73e7

23 files changed

Lines changed: 1264 additions & 141 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/cli': patch
3+
---
4+
5+
[docs] The browser-support guide's on-screen keyboard section describes the ride in Safari on iOS: ChatLayout's composer and a Tall BottomSheet's focused field ride UIKit's keyboard motion, the scroll room stays until Safari reports the keyboard gone, and the ride starts once the page has loaded Core's touch support.
6+
7+
@vjeux

‎.changeset/keyboard-ride-ios.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[feat] In Safari on iOS, ChatLayout's composer and a Tall BottomSheet's focused field ride the on-screen keyboard on UIKit's own keyboard motion. They rise from Safari's report as the keyboard starts up; before, they moved up in one step. The composer also goes down from the blur that takes the keyboard down, as the sheet already did. The ride starts once the page has loaded Core's touch support, shortly after first paint; before that, they move in one step. Chrome on Android and Firefox for Android are unchanged. Under Reduce Motion nothing travels.
6+
7+
@vjeux

‎docs/architecture/react-component-runtime.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,12 @@ those contracts.
347347
and Switch work by taps, clicks and keys, with no drag tracking and no slide
348348
or spring, and a tap under a finger or under Reduce Motion changes them at
349349
once, with no cross-fade; a bottom sheet still drags, and a drag that closes
350-
it takes the stylesheet exit instead of the spring. A gesture in that window
351-
does not start the load. The bundler keeps one copy per page however many
350+
it takes the stylesheet exit instead of the spring. ChatLayout loads the touch
351+
runtime after first paint under a coarse pointer, for the keyboard ride; it
352+
adds no gesture, and under a fine pointer it never loads it. The runtime
353+
exposes Safari on iOS's keyboard schedule; the keyboard riders register it
354+
with the layout guide, so no first-load module imports it. A gesture in that
355+
window does not start the load. The bundler keeps one copy per page however many
352356
components use them. If the runtime's chunk fails to load, the page stays as
353357
it is before the load: components work by taps, clicks and keys, and no
354358
gesture retries the load. The loader's `import()` targets

‎docs/specs/AST-099-mobile-keyboard/spec.md‎

Lines changed: 44 additions & 4 deletions
Large diffs are not rendered by default.

‎packages/cli/assets/docs/browser-support.doc.mjs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ export const docs = {
154154
type: 'prose',
155155
text: 'In Chrome on Android, ChatLayout\u2019s composer rides the keyboard as Gboard slides it up and down. A Tall BottomSheet\u2019s focused field rides the keyboard as Gboard slides it up, and as Gboard slides it down after the field loses focus. The scroll room stays until the keyboard is down. A hide Chrome reports late, such as Back, which keeps the focus, rides down from Chrome\u2019s report. When the keyboard is reported, the sheet places the field just above it in that same frame, so Chrome has no covered field to pan to; if Chrome pans anyway, the sheet puts the page back when that pan ends. Chrome follows your viewport setting, which Astryx documents and never sets: your app owns its viewport `<meta>` element. By default (`interactive-widget=resizes-visual`) Chrome lays the keyboard over the page and pans to a focused field elsewhere on the page. With `interactive-widget=resizes-content` it shrinks the layout viewport instead, so content anchored to the bottom sits above the keyboard, and those components measure no keyboard and change nothing: the browser has already made the room.',
156156
},
157+
{
158+
type: 'prose',
159+
text: 'In Safari on iOS, in the browser and as a home-screen app, ChatLayout\u2019s composer and a Tall BottomSheet\u2019s focused field ride the keyboard on UIKit\u2019s own keyboard motion. They rise from Safari\u2019s report of the keyboard, which comes as the keyboard starts up, and go down from the blur that takes the keyboard down. Safari reports the keyboard gone only once it is down, so the scroll room stays until then. The ride starts once the page has loaded Core\u2019s touch support, shortly after first paint; before that, they move in one step.',
160+
},
157161
{
158162
type: 'code',
159163
lang: 'html',

‎packages/cli/test/__golden__/no-icon-set/docs-topics.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
"supporting-older-browsers",
118118
"how-these-tiers-move-over-time"
119119
],
120-
"skeleton": "463a740e90118dea23181c46a2cca01695d9295027863282835518f0bd655bdb",
120+
"skeleton": "5aafc0d8f21d2f2961fb98ee722499ec146ae6cac841b036e019c03e4cdf8073",
121121
"mentions": []
122122
},
123123
"color": {

‎packages/core/src/BottomSheet/BottomSheet.doc.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export const docs = {
152152
name: 'height',
153153
type: "'hug' | 'capped' | 'tall' | number | string",
154154
description:
155-
"How tall the sheet is. Named budgets: 'hug' fits its content up to 92% of the viewport, 'capped' is a scrolling mid-height panel (~62%), and 'tall' is a pinned near-full panel (~92%) for content that streams in. Or pass a number (px) / CSS length for a custom budget. Give snapPoints to let the user drag between heights. On shorter viewports the sheet fills the available height. Only a fully expanded 'tall' sheet provides mobile-keyboard accommodation: it stays put and scrolls each focused control above the keyboard. Hug, Capped, numeric and CSS-length heights never do, and a Tall sheet stops doing it the moment the user drags it to a shorter detent, resuming when they drag it back. Outside that state the sheet neither moves nor adds keyboard scroll space, and the browser's own focus reveal is left in place; on iOS that reveal can shift the whole page. This accommodation applies where the visual viewport shrinks for the keyboard, as in Safari on iOS, Firefox for Android and Chrome on Android, where the sheet places the focused control just above the keyboard as it comes up, and the control rides the rest of Gboard\u2019s slide up, and the slide down after it loses focus; a hide Chrome reports late, such as Back, rides down from Chrome\u2019s report (`astryx docs browser-support`).",
155+
"How tall the sheet is. Named budgets: 'hug' fits its content up to 92% of the viewport, 'capped' is a scrolling mid-height panel (~62%), and 'tall' is a pinned near-full panel (~92%) for content that streams in. Or pass a number (px) / CSS length for a custom budget. Give snapPoints to let the user drag between heights. On shorter viewports the sheet fills the available height. Only a fully expanded 'tall' sheet provides mobile-keyboard accommodation: it stays put and scrolls each focused control above the keyboard. Hug, Capped, numeric and CSS-length heights never do, and a Tall sheet stops doing it the moment the user drags it to a shorter detent, resuming when they drag it back. Outside that state the sheet neither moves nor adds keyboard scroll space, and the browser's own focus reveal is left in place; on iOS that reveal can shift the whole page. This accommodation applies where the visual viewport shrinks for the keyboard, as in Safari on iOS, Firefox for Android and Chrome on Android, where the sheet places the focused control just above the keyboard as it comes up, and the control rides the rest of Gboard\u2019s slide up, and the slide down after it loses focus; a hide Chrome reports late, such as Back, rides down from Chrome\u2019s report. In Safari on iOS the content rides UIKit\u2019s keyboard motion, up from Safari\u2019s report and down from the blur (`astryx docs browser-support`).",
156156
default: "'capped'",
157157
},
158158
{

‎packages/core/src/BottomSheet/BottomSheet.test.tsx‎

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {__focusTakeoverCallersForTest} from '../keyboard/keyboardHeight';
3939
import {__resetRevealsAtReportForTest} from '../keyboard/useMobileKeyboard';
4040
import {getKeyboardLayoutGuide} from '../keyboard/keyboardLayoutGuide';
4141
import {animatorFor} from '../motion/animator';
42+
import {timing as uikitTiming} from '../motion/uikit/timing';
4243
import {__loadTouchRuntimeForTest} from '../utils/touchRuntime';
4344

4445
// These cases drive touch motion, which comes with the shared touch runtime
@@ -194,6 +195,13 @@ function mockIOSWebKit() {
194195
maxTouchPoints: {configurable: true, value: 5},
195196
});
196197
vi.stubGlobal('navigator', navigatorMock);
198+
// WebKit on iOS: the one engine with -webkit-touch-callout. The keyboard
199+
// layout guide reads UIKit's keyboard schedule from it.
200+
vi.stubGlobal('CSS', {
201+
...globalThis.CSS,
202+
supports: (property: string, value?: string) =>
203+
property === '-webkit-touch-callout' && value === 'none',
204+
});
197205
mockPrimaryPointer('coarse');
198206
}
199207

@@ -3121,13 +3129,13 @@ describe('BottomSheet', () => {
31213129
});
31223130
});
31233131

3124-
it('subscribes no keyboard layout guide where the keyboard moves on Safari\u2019s schedule, whose transitions the sheet never rides (spec:AST-099 FR8)', () => {
3125-
// iOS Safari and Firefox for Android have no VirtualKeyboard API, so the
3126-
// guide has no profile there. The sheet subscribes no guide, and its
3127-
// room follows the keyboard's reports.
3132+
it('subscribes no keyboard layout guide where the browser has no keyboard schedule, as Firefox for Android (spec:AST-099 FR8)', () => {
3133+
// Firefox for Android has neither the VirtualKeyboard API nor UIKit's
3134+
// keyboard, so the guide has no profile there. The sheet subscribes no
3135+
// guide, and its room follows the keyboard's reports.
31283136
mockWindowHeight(800);
31293137
const viewport = mockVisualViewport(800);
3130-
mockIOSWebKit();
3138+
mockPrimaryPointer('coarse');
31313139
const guide = getKeyboardLayoutGuide(window);
31323140
const subscribe = vi.spyOn(guide, 'subscribe');
31333141
render(
@@ -3680,6 +3688,19 @@ describe('BottomSheet', () => {
36803688
});
36813689

36823690
describe('in iOS Safari, which reports the keyboard once it is gone (spec:AST-099 FR3)', () => {
3691+
// jsdom dates events on the epoch clock, but a browser dates them on
3692+
// performance.now()'s, which the keyboard guide reads them against.
3693+
let eventTime: {mockRestore: () => void} | null = null;
3694+
beforeEach(() => {
3695+
eventTime = vi
3696+
.spyOn(Event.prototype, 'timeStamp', 'get')
3697+
.mockImplementation(() => performance.now());
3698+
});
3699+
afterEach(() => {
3700+
eventTime?.mockRestore();
3701+
eventTime = null;
3702+
});
3703+
36833704
// A Tall sheet in iOS Safari: a visual viewport, no keyboard geometry
36843705
// API, and a coarse pointer. Its body, from top 100 to bottom 800, holds
36853706
// 800 px of content and the room after it. A reveal's scroll lands at
@@ -3761,11 +3782,46 @@ describe('BottomSheet', () => {
37613782
act(async () => {
37623783
await new Promise(resolve => setTimeout(resolve, 450));
37633784
});
3785+
// UIKit's keyboard spring lands at the notification's duration.
3786+
const afterTheShow = afterTheHide;
3787+
3788+
it('rides the content up from Safari\u2019s report on UIKit\u2019s keyboard spring, through the layout guide (spec:AST-099 FR8)', async () => {
3789+
const guide = getKeyboardLayoutGuide(window);
3790+
const subscribe = vi.spyOn(guide, 'subscribe');
3791+
const {body, input, report, drawnTop} = renderIOSSheet();
3792+
expect(subscribe).toHaveBeenCalled();
3793+
act(() => input.focus());
3794+
report(500);
3795+
const {start, curve} = guide.transition;
3796+
expect(curve).toMatchObject({
3797+
durationMs: uikitTiming.keyboardNotificationMs,
3798+
progress: expect.any(Function),
3799+
});
3800+
// The room and the scroll land at the report. The content is drawn
3801+
// where it was, then rises with the keyboard a little each frame, and
3802+
// lands where the reveal puts it. No frame jumps.
3803+
expect(body.scrollTop).toBe(248);
3804+
expect(keyboardInsetOf(body)).toBe(348);
3805+
expect(drawnTop(start)).toBeCloseTo(0, 6);
3806+
const samples = Array.from({length: 25}, (_, frame) =>
3807+
drawnTop(start + frame * 16),
3808+
);
3809+
for (let frame = 1; frame < samples.length; frame += 1) {
3810+
const step = (samples[frame] ?? 0) - (samples[frame - 1] ?? 0);
3811+
expect(step).toBeLessThanOrEqual(0.01);
3812+
expect(step).toBeGreaterThan(-40);
3813+
}
3814+
expect(drawnTop(start + uikitTiming.keyboardNotificationMs)).toBe(-248);
3815+
await afterTheShow();
3816+
expect(drawnTop()).toBe(-248);
3817+
subscribe.mockRestore();
3818+
});
37643819

37653820
it('rides the content down on the keyboard\u2019s spring from the blur, and the report then moves nothing', async () => {
37663821
const {body, content, input, report, drawnTop} = renderIOSSheet();
37673822
act(() => input.focus());
37683823
report(500);
3824+
await afterTheShow();
37693825
expect(body.scrollTop).toBe(248);
37703826
expect(keyboardInsetOf(body)).toBe(348);
37713827
expect(drawnTop()).toBe(-248);
@@ -3822,6 +3878,7 @@ describe('BottomSheet', () => {
38223878
});
38233879
act(() => input.focus());
38243880
report(500);
3881+
await afterTheShow();
38253882
act(() => input.blur());
38263883
await nextFrame();
38273884
// The cross-fade lands the content on a timer, so the samples are
@@ -3862,6 +3919,7 @@ describe('BottomSheet', () => {
38623919
});
38633920
act(() => input.focus());
38643921
report(500);
3922+
await afterTheShow();
38653923
const settled = drawnTop();
38663924
act(() => screen.getByLabelText('Picker').focus());
38673925
await nextFrame();
@@ -3910,6 +3968,7 @@ describe('BottomSheet', () => {
39103968
.appendChild(document.createElement('button'));
39113969
act(() => input.focus());
39123970
report(500);
3971+
await afterTheShow();
39133972
act(() => button.focus());
39143973
expect(document.activeElement).toBe(host);
39153974
await nextFrame();
@@ -3928,6 +3987,7 @@ describe('BottomSheet', () => {
39283987
.appendChild(document.createElement('button'));
39293988
act(() => input.focus());
39303989
report(500);
3990+
await afterTheShow();
39313991
// The browser runs the blur's frame after focus settles on the button.
39323992
const frames: FrameRequestCallback[] = [];
39333993
vi.mocked(requestAnimationFrame).mockImplementation(callback => {
@@ -3954,6 +4014,7 @@ describe('BottomSheet', () => {
39544014
renderWithHiddenField(kind);
39554015
act(() => input.focus());
39564016
report(500);
4017+
await afterTheShow();
39574018
const settled = drawnTop();
39584019
focusHiddenField();
39594020
await nextFrame();
@@ -3967,6 +4028,7 @@ describe('BottomSheet', () => {
39674028
const {body, input, report, drawnTop} = renderIOSSheet();
39684029
act(() => input.focus());
39694030
report(500);
4031+
await afterTheShow();
39704032
act(() => input.blur());
39714033
await nextFrame();
39724034
// Focus comes back to the field mid-slide, so the keyboard stays.
@@ -3984,6 +4046,7 @@ describe('BottomSheet', () => {
39844046
renderWithHiddenField(kind);
39854047
act(() => input.focus());
39864048
report(500);
4049+
await afterTheShow();
39874050
act(() => input.blur());
39884051
await nextFrame();
39894052
// Focus moves to a field inside the holder mid-slide, so the
@@ -3996,10 +4059,11 @@ describe('BottomSheet', () => {
39964059
},
39974060
);
39984061

3999-
it('glides the content to its new place when the keyboard goes while its field keeps focus, as a downward dismissal leaves it', () => {
4062+
it('glides the content to its new place when the keyboard goes while its field keeps focus, as a downward dismissal leaves it', async () => {
40004063
const {body, input, report, drawnTop} = renderIOSSheet();
40014064
act(() => input.focus());
40024065
report(500);
4066+
await afterTheShow();
40034067
expect(drawnTop()).toBe(-248);
40044068
// The clock holds still through the report, so the glide starts at
40054069
// `reportAt` however slowly the test runs.
@@ -4012,12 +4076,18 @@ describe('BottomSheet', () => {
40124076
} finally {
40134077
clock.mockRestore();
40144078
}
4015-
expect(keyboardInsetOf(body)).toBe(0);
4016-
expect(body.scrollTop).toBe(0);
40174079
// Drawn where it was at the report, then down on the keyboard's spring:
4018-
// two frames in, it has hardly moved.
4080+
// two frames in, it has hardly moved. The room and the scroll stay
4081+
// through the ride.
4082+
expect(keyboardInsetOf(body)).toBe(348);
40194083
expect(drawnTop(reportAt + 32)).toBeLessThan(-200);
40204084
expect(drawnTop(reportAt + 450)).toBeCloseTo(0, 0);
4085+
// Once it lands, the room drops and the scroll goes back with the
4086+
// translate in one commit, so nothing drawn moves.
4087+
await afterTheHide();
4088+
expect(keyboardInsetOf(body)).toBe(0);
4089+
expect(body.scrollTop).toBe(0);
4090+
expect(drawnTop()).toBeCloseTo(0, 0);
40214091
});
40224092
});
40234093

0 commit comments

Comments
 (0)