Skip to content

Commit 3890d68

Browse files
vjeuxcixzhang
andauthored
fix(Layer): a capped layer over its trigger keeps its top (#10497)
* fix(Layer): a capped layer over its trigger keeps its top A layer taller than the room on both sides of its trigger sits over the trigger, where the viewport fit shifts it. The keyboard cap makes it shorter, so it fits below the trigger, and the fallback order moved it there: the field being typed into jumped down as the keyboard came up, and back up as it went. While the cap holds such a layer, the runtime now lifts it back to the block-start it had when the cap landed, with insets that move its anchor area. Taking the cap off drops the lift. * Merge branch 'main' into uikit/c23-tall-cap-keeps-top Co-authored-by: Cindy Zhang <cindyxz@meta.com>
1 parent 3231fc2 commit 3890d68

6 files changed

Lines changed: 212 additions & 24 deletions

File tree

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+
[fix] A popover taller than the room on both sides of its trigger keeps its top when the keyboard shortens it. The field being typed into stays under the caret. It used to jump below the trigger as the keyboard arrived, and back as it went.
6+
7+
@vjeux

‎packages/core/src/Layer/LayerTouch.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
* @file LayerTouch.ts
77
* @input useLayerKeyboardClearance.ts (the software keyboard's part of the
88
* viewport caps)
9-
* @output The layer runtime's touch layer: useLayerKeyboardClearance
9+
* @output The layer runtime's touch layer: useLayerKeyboardClearance and
10+
* keyboardLiftStyle
1011
* @position The one module the layer runtime imports touch-only code through,
1112
* and its only import site is useLayer.tsx. It is imported statically, as
1213
* every touch module is.
1314
*
1415
* SYNC: When modified, keep every touch-only import of useLayer.tsx here.
1516
*/
1617

17-
export {useLayerKeyboardClearance} from './useLayerKeyboardClearance';
18+
export {
19+
keyboardLiftStyle,
20+
useLayerKeyboardClearance,
21+
} from './useLayerKeyboardClearance';

‎packages/core/src/Layer/useLayer.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {resolveLayerPortalTarget} from './layerHost';
3636
import {layerTextReset} from './layerTextReset.stylex';
3737
import {layerStructureReset} from './layerStructureReset.stylex';
3838
import {layerViewportInset} from './layerViewportInset.stylex';
39-
import {useLayerKeyboardClearance} from './LayerTouch';
39+
import {keyboardLiftStyle, useLayerKeyboardClearance} from './LayerTouch';
4040
import {LAYER_ANCHORED_ATTRIBUTE} from './layerKeyboardClearance';
4141
import {settledBox} from './settledBox';
4242
import {layerInsetProperties} from './layerInset';
@@ -1761,6 +1761,7 @@ function useLayerImplementation(
17611761
// block-start edge and ends clear of the keyboard.
17621762
const {
17631763
cap: keyboardCap,
1764+
lift: keyboardLift,
17641765
inset: keyboardInset,
17651766
followLayerElement,
17661767
} = useLayerKeyboardClearance(isOpen && mode === 'context', popoverRef);
@@ -2037,6 +2038,7 @@ function useLayerImplementation(
20372038
style={{
20382039
...stylexResult.style,
20392040
...anchorStyle,
2041+
...keyboardLiftStyle(keyboardLift),
20402042
...clearanceProperty,
20412043
...clearFallbacksProperty,
20422044
...layerInsetProperties(declaredInset),
@@ -2068,6 +2070,7 @@ function useLayerImplementation(
20682070
keepsAnchorClear,
20692071
keyboardCap,
20702072
keyboardInset,
2073+
keyboardLift,
20712074
lightDismiss,
20722075
mountStore,
20732076
popoverRefCallback,

‎packages/core/src/Layer/useLayerKeyboardClearance.test.tsx‎

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
* @output Checks the software keyboard's part of the viewport caps. The cap
88
* sits 10 px above the keyboard. It is written once a rising keyboard has
99
* arrived, and taken off as a falling one starts down. A keyboard already
10-
* up at the open caps the layer before paint. The field stays in view,
10+
* up at the open caps the layer before paint. A layer the viewport fit
11+
* shifted over its trigger keeps that block-start edge through the cap.
12+
* The field stays in view,
1113
* unless a person touched the layer after the cap. A component's own
1214
* scroll keeps the reveal, and so does the next keyboard change. A layer
1315
* clear of the keyboard, a bottom-held layer and a layer in another
@@ -33,6 +35,7 @@ import type {BrowserClickWaitHandlers} from '../utils/browserClickWait';
3335
import type * as KeyboardHeightModule from '../keyboard/keyboardHeight';
3436
import {
3537
keyboardClearanceCap,
38+
keyboardLiftStyle,
3639
LAYER_ANCHORED_ATTRIBUTE,
3740
LAYER_KEYBOARD_CAP_PROPERTY,
3841
LAYER_KEYBOARD_CLEARANCE_PX,
@@ -122,6 +125,7 @@ function Harness({
122125
layerKey = 'a',
123126
entering,
124127
shiftedTop,
128+
ignoresLift = false,
125129
slidesAboveKeyboard = false,
126130
}: {
127131
isOpen?: boolean;
@@ -141,14 +145,16 @@ function Harness({
141145
* `box` and over its trigger; any shorter, it sits at `box`.
142146
*/
143147
shiftedTop?: number;
148+
/** Stays below its trigger whatever the lift's insets say. */
149+
ignoresLift?: boolean;
144150
/**
145151
* Places itself by the keyboard inset, as Selector's overlay does: while an
146152
* inset is out, the layer ends above it.
147153
*/
148154
slidesAboveKeyboard?: boolean;
149155
}) {
150156
const layerRef = useRef<HTMLDivElement>(null);
151-
const {cap, inset, followLayerElement} = useLayerKeyboardClearance(
157+
const {cap, lift, inset, followLayerElement} = useLayerKeyboardClearance(
152158
isOpen,
153159
layerRef,
154160
);
@@ -173,6 +179,8 @@ function Harness({
173179
? {}
174180
: {[LAYER_KEYBOARD_INSET_PROPERTY]: `${inset}px`}),
175181
...(entering == null ? {} : {transform: `translateY(${entering}px)`}),
182+
// As the runtime applies it: the insets move the layer's anchor area.
183+
...keyboardLiftStyle(lift),
176184
}}
177185
data-testid="layer"
178186
ref={el => {
@@ -202,7 +210,8 @@ function Harness({
202210
slidesAboveKeyboard && !Number.isNaN(keyboardInset)
203211
? Math.min(placed, VIEWPORT - keyboardInset - h)
204212
: placed;
205-
const top = (heldBottom ? box.bottom - h : start) + shift;
213+
const lifted = ignoresLift ? 0 : liftOf(el);
214+
const top = (heldBottom ? box.bottom - h : start) - lifted + shift;
206215
return {
207216
top,
208217
bottom: top + h,
@@ -292,6 +301,12 @@ const capOf = (container: HTMLElement) =>
292301
.querySelector<HTMLElement>('[data-testid="layer"]')!
293302
.style.getPropertyValue(LAYER_KEYBOARD_CAP_PROPERTY);
294303

304+
/** The lift the runtime applies, read back from the layer's insets. */
305+
function liftOf(layer: HTMLElement): number {
306+
const end = parseFloat(layer.style.insetBlockEnd);
307+
return Number.isNaN(end) ? 0 : end;
308+
}
309+
295310
const insetOf = (container: HTMLElement) =>
296311
container
297312
.querySelector<HTMLElement>('[data-testid="layer"]')!
@@ -469,18 +484,19 @@ describe('useLayerKeyboardClearance', () => {
469484
);
470485
});
471486

472-
it('takes the cap off a layer that does not read the inset at once as the keyboard goes down, even where that moves it (spec:AST-059 FR10)', () => {
487+
it('takes the cap off a layer that does not read the inset at once as the keyboard goes down, and its block-start stays (spec:AST-059 FR9, FR10)', () => {
473488
// At its full height the viewport fit places this layer over its
474-
// trigger; capped, it sits below the trigger.
489+
// trigger. Shorter, it would fit below the trigger, at 300.
475490
const {container} = render(
476491
<Harness box={{top: 300, bottom: 700}} shiftedTop={120} />,
477492
);
478493
guide.emit({kind: 'change', from: 0, to: COVER, animated: false});
479-
expect(capOf(container)).not.toBe('');
494+
expect(capOf(container)).toBe(`${VIEWPORT - COVER - 10 - 120}px`);
480495
const layer = screen.getByTestId('layer');
481-
expect(layer.getBoundingClientRect().top).toBe(300);
496+
expect(layer.getBoundingClientRect().top).toBe(120);
482497
guide.emit({kind: 'hide', from: COVER, to: 0});
483498
expect(capOf(container)).toBe('');
499+
expect(liftOf(layer)).toBe(0);
484500
expect(layer.getBoundingClientRect().top).toBe(120);
485501
});
486502

@@ -537,10 +553,32 @@ describe('useLayerKeyboardClearance', () => {
537553
expect(capOf(container)).toBe('');
538554
});
539555

540-
it('caps a layer the viewport fit pushed up over its trigger from where it starts once it is shorter', () => {
556+
it('keeps the block-start edge of a layer the viewport fit pushed up over its trigger: shorter, it fits below the trigger, and the cap lifts it back (spec:AST-059 FR9)', () => {
557+
const {container} = render(<Harness shiftedTop={40} />);
558+
guide.emit({kind: 'change', from: 0, to: COVER, animated: false});
559+
const layer = screen.getByTestId('layer');
560+
expect(capOf(container)).toBe(`${VIEWPORT - COVER - 10 - 40}px`);
561+
expect(liftOf(layer)).toBe(BOX.top - 40);
562+
expect(layer.getBoundingClientRect().top).toBe(40);
563+
expect(layer.getBoundingClientRect().bottom).toBe(VIEWPORT - COVER - 10);
564+
});
565+
566+
it('keeps that block-start edge at the next keyboard change', () => {
541567
const {container} = render(<Harness shiftedTop={40} />);
542568
guide.emit({kind: 'change', from: 0, to: COVER, animated: false});
569+
guide.emit({kind: 'change', from: COVER, to: COVER + 20, animated: false});
570+
const layer = screen.getByTestId('layer');
571+
expect(capOf(container)).toBe(`${VIEWPORT - COVER - 20 - 10 - 40}px`);
572+
expect(layer.getBoundingClientRect().top).toBe(40);
573+
});
574+
575+
it('caps a layer from below its trigger where the engine does not move it by the lift', () => {
576+
const {container} = render(<Harness shiftedTop={40} ignoresLift />);
577+
guide.emit({kind: 'change', from: 0, to: COVER, animated: false});
578+
const layer = screen.getByTestId('layer');
543579
expect(capOf(container)).toBe(CAPPED);
580+
expect(liftOf(layer)).toBe(0);
581+
expect(layer.getBoundingClientRect().top).toBe(BOX.top);
544582
});
545583

546584
it('reads a layer that is still entering without its entrance transform, in one read (spec:AST-059 FR8)', () => {
@@ -771,6 +809,7 @@ describe('useLayerKeyboardClearance', () => {
771809
advance(timing.keyboardNotificationMs + 1);
772810
// A shorter box would move its top down and still end under the keyboard.
773811
expect(capOf(container)).toBe('');
812+
expect(liftOf(screen.getByTestId('layer'))).toBe(0);
774813
});
775814

776815
it('keeps the cap through a press inside the layer until its click', () => {

‎packages/core/src/Layer/useLayerKeyboardClearance.ts‎

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @input Whether an anchor-mode layer is open, its element, and the keyboard
88
* module: its one height, and its layout guide for timing.
99
* @output Exports `useLayerKeyboardClearance` and its result type,
10-
* `keyboardClearanceCap`, `LAYER_ANCHORED_ATTRIBUTE`,
10+
* `keyboardLiftStyle`, `keyboardClearanceCap`, `LAYER_ANCHORED_ATTRIBUTE`,
1111
* `LAYER_KEYBOARD_CAP_PROPERTY`, `LAYER_KEYBOARD_OVERFLOW_PROPERTY`,
1212
* `LAYER_KEYBOARD_INSET_PROPERTY` and `LAYER_KEYBOARD_CLEARANCE_PX`
1313
* @position Layer runtime, through the layer's touch module: the software
@@ -22,7 +22,13 @@
2222
* - /docs/specs/AST-059-layer-viewport-inset/spec.md
2323
*/
2424

25-
import {useCallback, useRef, useState, type RefObject} from 'react';
25+
import {
26+
useCallback,
27+
useRef,
28+
useState,
29+
type CSSProperties,
30+
type RefObject,
31+
} from 'react';
2632
import {flushSync} from 'react-dom';
2733
import {useIsomorphicLayoutEffect} from '../hooks/useIsomorphicLayoutEffect';
2834
// The keyboard module's own files, not its index. The index also re-exports
@@ -101,10 +107,25 @@ function keepInView(layer: HTMLElement, target: Element) {
101107
}
102108
}
103109

110+
/**
111+
* The insets that move a capped layer's anchor area `lift` px toward the
112+
* block-start, or null without a lift. They hold the layer's block-start edge
113+
* where it was when the cap landed (`LayerKeyboardClearance.lift`).
114+
*/
115+
export function keyboardLiftStyle(lift: number | null): CSSProperties | null {
116+
return lift == null ? null : {insetBlockStart: -lift, insetBlockEnd: lift};
117+
}
118+
104119
/** What `useLayerKeyboardClearance` gives the layer runtime. */
105120
export interface LayerKeyboardClearance {
106121
/** The block size that keeps the layer clear of the keyboard, or null. */
107122
readonly cap: number | null;
123+
/**
124+
* While the cap holds the layer, how far its block-start edge sits above
125+
* the place the fallback order picks for the shorter box, in px. The
126+
* runtime applies it with `keyboardLiftStyle`. Otherwise null.
127+
*/
128+
readonly lift: number | null;
108129
/**
109130
* Once a software keyboard is up, the layout px between the layout
110131
* viewport's bottom and the line an anchored layer ends above. It keeps its
@@ -132,9 +153,11 @@ export function useLayerKeyboardClearance(
132153
layerRef: RefObject<HTMLElement | null>,
133154
): LayerKeyboardClearance {
134155
const [cap, setCap] = useState<number | null>(null);
156+
const [lift, setLift] = useState<number | null>(null);
135157
const [keyboardInset, setKeyboardInset] = useState<number | null>(null);
136158
// The cap the layer renders with, read by the measurement between commits.
137159
const capRef = useRef<number | null>(null);
160+
const liftRef = useRef<number | null>(null);
138161
const keyboardInsetRef = useRef<number | null>(null);
139162
// Set while the layer is open: follows the runtime when it replaces the
140163
// layer element without closing it.
@@ -188,19 +211,24 @@ export function useLayerKeyboardClearance(
188211
const commitCap = (
189212
next: number | null,
190213
nextInset = keyboardInsetRef.current,
214+
nextLift: number | null = null,
191215
) => {
192216
if (
193217
isDisposed ||
194-
(capRef.current === next && keyboardInsetRef.current === nextInset)
218+
(capRef.current === next &&
219+
keyboardInsetRef.current === nextInset &&
220+
liftRef.current === nextLift)
195221
) {
196222
return;
197223
}
198224
capRef.current = next;
199225
keyboardInsetRef.current = nextInset;
226+
liftRef.current = nextLift;
200227
// eslint-disable-next-line @eslint-react/dom-no-flush-sync -- the box is measured at its new size in the same task, before the frame paints
201228
flushSync(() => {
202229
setCap(next);
203230
setKeyboardInset(nextInset);
231+
setLift(nextLift);
204232
});
205233
sync();
206234
};
@@ -226,14 +254,27 @@ export function useLayerKeyboardClearance(
226254
return false;
227255
}
228256
commitCap(height);
229-
// A layer too tall for the room below its trigger can sit higher, over
230-
// the trigger, where the viewport fit placed it. Shorter, it goes back
231-
// below the trigger: cap it again from where it starts now.
257+
// A layer too tall for the room on both sides of its trigger sits over
258+
// the trigger, where the viewport fit shifted it. Shorter, it fits below
259+
// the trigger, and the fallback order moves it down. Lift it back, so it
260+
// keeps the block-start edge it had when the cap landed. A layer that
261+
// holds its block-end edge (placed above its trigger) moves only its
262+
// top, and is not lifted.
232263
const placed = settledBox(layer);
233-
if (placed.top > box.top + 1) {
234-
const again = keyboardClearanceCap(placed, viewport, inset);
235-
if (again != null) {
236-
commitCap(again);
264+
if (
265+
placed.top > box.top + 1 &&
266+
Math.abs(placed.bottom - box.bottom) > 1
267+
) {
268+
commitCap(height, keyboardInsetRef.current, placed.top - box.top);
269+
// An engine that does not move the layer by the lift leaves it below
270+
// the trigger: cap it from where it starts there.
271+
const lifted = settledBox(layer);
272+
if (Math.abs(lifted.top - box.top) > 1) {
273+
commitCap(height);
274+
const again = keyboardClearanceCap(placed, viewport, inset);
275+
if (again != null) {
276+
commitCap(again);
277+
}
237278
}
238279
}
239280
// The cap holds the block-start edge: below its trigger the top stays
@@ -429,6 +470,10 @@ export function useLayerKeyboardClearance(
429470
capRef.current = null;
430471
setCap(null);
431472
}
473+
if (liftRef.current != null) {
474+
liftRef.current = null;
475+
setLift(null);
476+
}
432477
if (keyboardInsetRef.current != null) {
433478
keyboardInsetRef.current = null;
434479
setKeyboardInset(null);
@@ -441,6 +486,7 @@ export function useLayerKeyboardClearance(
441486
}, []);
442487
return {
443488
cap: isOpen ? cap : null,
489+
lift: isOpen ? lift : null,
444490
inset: isOpen ? keyboardInset : null,
445491
followLayerElement,
446492
};

0 commit comments

Comments
 (0)