Skip to content

Commit db9aa9d

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 093c9fc commit db9aa9d

24 files changed

Lines changed: 1143 additions & 139 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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,10 @@ 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. A gesture in that
353+
window does not start the load. The bundler keeps one copy per page however many
352354
components use them. If the runtime's chunk fails to load, the page stays as
353355
it is before the load: components work by taps, clicks and keys, and no
354356
gesture retries the load. `utils/touchRuntimeModules.tsx` is the one module

‎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: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ 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 {uikitKeyboardSchedule} from '../keyboard/uikitKeyboardSchedule';
43+
import {timing as uikitTiming} from '../motion/uikit/timing';
4244
import {__loadTouchRuntimeForTest} from '../utils/touchRuntime';
4345

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

@@ -3121,13 +3130,13 @@ describe('BottomSheet', () => {
31213130
});
31223131
});
31233132

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.
3133+
it('subscribes no keyboard layout guide where the browser has no keyboard schedule, as Firefox for Android (spec:AST-099 FR8)', () => {
3134+
// Firefox for Android has neither the VirtualKeyboard API nor UIKit's
3135+
// keyboard, so the guide has no profile there. The sheet subscribes no
3136+
// guide, and its room follows the keyboard's reports.
31283137
mockWindowHeight(800);
31293138
const viewport = mockVisualViewport(800);
3130-
mockIOSWebKit();
3139+
mockPrimaryPointer('coarse');
31313140
const guide = getKeyboardLayoutGuide(window);
31323141
const subscribe = vi.spyOn(guide, 'subscribe');
31333142
render(
@@ -3680,6 +3689,19 @@ describe('BottomSheet', () => {
36803689
});
36813690

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

37653818
it('rides the content down on the keyboard\u2019s spring from the blur, and the report then moves nothing', async () => {
37663819
const {body, content, input, report, drawnTop} = renderIOSSheet();
37673820
act(() => input.focus());
37683821
report(500);
3822+
await afterTheShow();
37693823
expect(body.scrollTop).toBe(248);
37703824
expect(keyboardInsetOf(body)).toBe(348);
37713825
expect(drawnTop()).toBe(-248);
@@ -3822,6 +3876,7 @@ describe('BottomSheet', () => {
38223876
});
38233877
act(() => input.focus());
38243878
report(500);
3879+
await afterTheShow();
38253880
act(() => input.blur());
38263881
await nextFrame();
38273882
// The cross-fade lands the content on a timer, so the samples are
@@ -3862,6 +3917,7 @@ describe('BottomSheet', () => {
38623917
});
38633918
act(() => input.focus());
38643919
report(500);
3920+
await afterTheShow();
38653921
const settled = drawnTop();
38663922
act(() => screen.getByLabelText('Picker').focus());
38673923
await nextFrame();
@@ -3910,6 +3966,7 @@ describe('BottomSheet', () => {
39103966
.appendChild(document.createElement('button'));
39113967
act(() => input.focus());
39123968
report(500);
3969+
await afterTheShow();
39133970
act(() => button.focus());
39143971
expect(document.activeElement).toBe(host);
39153972
await nextFrame();
@@ -3928,6 +3985,7 @@ describe('BottomSheet', () => {
39283985
.appendChild(document.createElement('button'));
39293986
act(() => input.focus());
39303987
report(500);
3988+
await afterTheShow();
39313989
// The browser runs the blur's frame after focus settles on the button.
39323990
const frames: FrameRequestCallback[] = [];
39333991
vi.mocked(requestAnimationFrame).mockImplementation(callback => {
@@ -3954,6 +4012,7 @@ describe('BottomSheet', () => {
39544012
renderWithHiddenField(kind);
39554013
act(() => input.focus());
39564014
report(500);
4015+
await afterTheShow();
39574016
const settled = drawnTop();
39584017
focusHiddenField();
39594018
await nextFrame();
@@ -3967,6 +4026,7 @@ describe('BottomSheet', () => {
39674026
const {body, input, report, drawnTop} = renderIOSSheet();
39684027
act(() => input.focus());
39694028
report(500);
4029+
await afterTheShow();
39704030
act(() => input.blur());
39714031
await nextFrame();
39724032
// Focus comes back to the field mid-slide, so the keyboard stays.
@@ -3984,6 +4044,7 @@ describe('BottomSheet', () => {
39844044
renderWithHiddenField(kind);
39854045
act(() => input.focus());
39864046
report(500);
4047+
await afterTheShow();
39874048
act(() => input.blur());
39884049
await nextFrame();
39894050
// Focus moves to a field inside the holder mid-slide, so the
@@ -3996,10 +4057,11 @@ describe('BottomSheet', () => {
39964057
},
39974058
);
39984059

3999-
it('glides the content to its new place when the keyboard goes while its field keeps focus, as a downward dismissal leaves it', () => {
4060+
it('glides the content to its new place when the keyboard goes while its field keeps focus, as a downward dismissal leaves it', async () => {
40004061
const {body, input, report, drawnTop} = renderIOSSheet();
40014062
act(() => input.focus());
40024063
report(500);
4064+
await afterTheShow();
40034065
expect(drawnTop()).toBe(-248);
40044066
// The clock holds still through the report, so the glide starts at
40054067
// `reportAt` however slowly the test runs.
@@ -4012,12 +4074,18 @@ describe('BottomSheet', () => {
40124074
} finally {
40134075
clock.mockRestore();
40144076
}
4015-
expect(keyboardInsetOf(body)).toBe(0);
4016-
expect(body.scrollTop).toBe(0);
40174077
// Drawn where it was at the report, then down on the keyboard's spring:
4018-
// two frames in, it has hardly moved.
4078+
// two frames in, it has hardly moved. The room and the scroll stay
4079+
// through the ride.
4080+
expect(keyboardInsetOf(body)).toBe(348);
40194081
expect(drawnTop(reportAt + 32)).toBeLessThan(-200);
40204082
expect(drawnTop(reportAt + 450)).toBeCloseTo(0, 0);
4083+
// Once it lands, the room drops and the scroll goes back with the
4084+
// translate in one commit, so nothing drawn moves.
4085+
await afterTheHide();
4086+
expect(keyboardInsetOf(body)).toBe(0);
4087+
expect(body.scrollTop).toBe(0);
4088+
expect(drawnTop()).toBeCloseTo(0, 0);
40214089
});
40224090
});
40234091

0 commit comments

Comments
 (0)