fix(mobile): the 3D grade legend goes compact on the side, and a field under the keyboard gets room to scroll above it

This commit is contained in:
Maurice
2026-11-08 12:15:34 +01:00
parent b73f749880
commit 06c03cbded
3 changed files with 137 additions and 48 deletions
+45 -18
View File
@@ -1,9 +1,21 @@
import { describe, expect, it, vi } from 'vitest';
import { keepFieldInView, revealFocusedField } from './keepFieldInView';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { keepFieldInView, releaseRoom, revealFocusedField } from './keepFieldInView';
function field(tag: string, top: number, bottom: number): HTMLElement {
const el = document.createElement(tag);
const KEYBOARD = 300;
function pane(): HTMLElement {
const el = document.createElement('div');
el.style.overflowY = 'auto';
Object.defineProperty(el, 'scrollHeight', { configurable: true, value: 2000 });
Object.defineProperty(el, 'clientHeight', { configurable: true, value: 800 });
el.scrollBy = vi.fn();
document.body.appendChild(el);
return el;
}
function field(parent: HTMLElement, tag: string, top: number, bottom: number): HTMLElement {
const el = document.createElement(tag);
parent.appendChild(el);
el.getBoundingClientRect = () => ({ top, bottom }) as DOMRect;
el.scrollIntoView = vi.fn();
if (tag === 'div') el.tabIndex = 0;
@@ -11,26 +23,41 @@ function field(tag: string, top: number, bottom: number): HTMLElement {
return el;
}
afterEach(() => {
releaseRoom();
document.body.innerHTML = '';
});
describe('keepFieldInView', () => {
it('FE-NATIVE-KBFIELD-001: a field the keyboard covers is scrolled to the middle, one in view or not for typing stays', () => {
Object.defineProperty(window, 'innerHeight', { configurable: true, value: 500 });
it('FE-NATIVE-KBFIELD-001: a field under the keyboard gets room in its pane and is scrolled above it, the room goes with the keyboard', () => {
Object.defineProperty(window, 'innerHeight', { configurable: true, value: 1000 });
const scroller = pane();
field(scroller, 'input', 760, 800);
const covered = field('input', 620, 660);
revealFocusedField();
expect(covered.scrollIntoView).toHaveBeenCalledWith({ block: 'center', behavior: 'smooth' });
revealFocusedField(KEYBOARD);
expect(scroller.style.paddingBottom).toBe('300px');
// The keyboard's top is at 700; the field's bottom goes 24px above it.
expect(scroller.scrollBy).toHaveBeenCalledWith({ top: 800 - (700 - 24), behavior: 'smooth' });
const visible = field('textarea', 100, 160);
revealFocusedField();
expect(visible.scrollIntoView).not.toHaveBeenCalled();
const notTyping = field('div', 620, 660);
revealFocusedField();
expect(notTyping.scrollIntoView).not.toHaveBeenCalled();
releaseRoom();
expect(scroller.style.paddingBottom).toBe('');
});
it('FE-NATIVE-KBFIELD-002: listens for the keyboard having come up', () => {
it('FE-NATIVE-KBFIELD-003: a field in view, or one not for typing, stays where it is', () => {
Object.defineProperty(window, 'innerHeight', { configurable: true, value: 1000 });
const scroller = pane();
field(scroller, 'textarea', 100, 160);
revealFocusedField(KEYBOARD);
field(scroller, 'div', 760, 800);
revealFocusedField(KEYBOARD);
expect(scroller.scrollBy).not.toHaveBeenCalled();
expect(scroller.style.paddingBottom).toBe('');
});
it('FE-NATIVE-KBFIELD-002: listens for the keyboard coming up and going', () => {
const addListener = vi.fn(async () => ({ remove: vi.fn() }));
keepFieldInView({ addListener });
keepFieldInView({ addListener } as never);
expect(addListener).toHaveBeenCalledWith('keyboardDidShow', expect.any(Function));
expect(addListener).toHaveBeenCalledWith('keyboardWillHide', expect.any(Function));
});
});