Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): a Tall sheet reads focus inside an open shadow root (#10388)

Christopher Chedeau0ad1e8d5e3ed
diff --git a/.changeset/sheet-shadow-root-focus-ride.md b/.changeset/sheet-shadow-root-focus-ride.md
new file mode 100644
index 0000000000..2bb2c5f3f6
--- /dev/null
+++ b/.changeset/sheet-shadow-root-focus-ride.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] BottomSheet: in iOS Safari, focus moving from a Tall sheet's field to a plain button inside a web component now rides the content down from the blur, as focus moving to any other control that brings up no keyboard does. A frame, or a custom element that can hold a field the page cannot see, still keeps the content in place.
+
+@vjeux
diff --git a/packages/core/src/BottomSheet/BottomSheet.test.tsx b/packages/core/src/BottomSheet/BottomSheet.test.tsx
index d4ca3c6e40..864cfcaace 100644
--- a/packages/core/src/BottomSheet/BottomSheet.test.tsx
+++ b/packages/core/src/BottomSheet/BottomSheet.test.tsx
@@ -3738,8 +3738,9 @@ describe('BottomSheet', () => {
expect(body.scrollTop).toBe(248);
});
- // A field the sheet cannot see: one in an open shadow host or in a frame.
- // Focus on it leaves `document.activeElement` on its holder.
+ // A field behind a holder: one inside an open shadow root, or in a frame.
+ // Focus on it leaves `document.activeElement` on the holder. The sheet
+ // reads into the open root, and a frame can hold a field it cannot read.
const hiddenFields = [
['an open shadow host', 'shadow host'],
['a frame', 'frame'],
@@ -3767,6 +3768,53 @@ describe('BottomSheet', () => {
return {...sheet, focusHiddenField};
}
+ it('rides the content down when focus moves from a field to a plain button inside an open shadow host, as a blur does', async () => {
+ const {body, input, report, drawnTop} = renderIOSSheet({
+ picker: <div title="Editor" />,
+ });
+ const host = screen.getByTitle('Editor');
+ const button = host
+ .attachShadow({mode: 'open'})
+ .appendChild(document.createElement('button'));
+ act(() => input.focus());
+ report(500);
+ act(() => button.focus());
+ expect(document.activeElement).toBe(host);
+ await nextFrame();
+ expect(drawnTop(performance.now() + 450)).toBeCloseTo(0, 0);
+ expect(keyboardInsetOf(body)).toBe(348);
+ expect(body.scrollTop).toBe(248);
+ });
+
+ it('rides the content down from the blur once focus settles on a plain button inside an open shadow host', async () => {
+ const {body, input, report, drawnTop} = renderIOSSheet({
+ picker: <div title="Editor" />,
+ });
+ const host = screen.getByTitle('Editor');
+ const button = host
+ .attachShadow({mode: 'open'})
+ .appendChild(document.createElement('button'));
+ act(() => input.focus());
+ report(500);
+ // The browser runs the blur's frame after focus settles on the button.
+ const frames: FrameRequestCallback[] = [];
+ vi.mocked(requestAnimationFrame).mockImplementation(callback => {
+ frames.push(callback);
+ return frames.length;
+ });
+ act(() => button.focus());
+ expect(document.activeElement).toBe(host);
+ vi.mocked(requestAnimationFrame).mockImplementation(callback => {
+ callback(0);
+ return 1;
+ });
+ act(() => frames.splice(0).forEach(frame => frame(0)));
+ await nextFrame();
+ expect(drawnTop(performance.now() + 450)).toBeCloseTo(0, 0);
+ expect(keyboardInsetOf(body)).toBe(348);
+ expect(body.scrollTop).toBe(248);
+ });
+
it.each(hiddenFields)(
'starts no ride when focus moves from a field into %s, whose own field can keep the keyboard up',
async (_, kind) => {
diff --git a/packages/core/src/keyboard/README.md b/packages/core/src/keyboard/README.md
index c55c9e579a..5487156da3 100644
--- a/packages/core/src/keyboard/README.md
+++ b/packages/core/src/keyboard/README.md
@@ -147,10 +147,11 @@ that times motion with it. Everything here is internal to
the content to its place on the same spring. A field focused before the
keyboard is down rides it back up. Focus moving to a select starts no ride:
iOS puts the select's picker where the keyboard was. Nor does focus moving
- into a frame, an open shadow host or a custom element, whose own field can
- keep the keyboard up. Firefox for Android takes the same path, but reports
- the hide once, at its end, and the content moves at that report (AST-099
- Gap 12). Safari's and Firefox's reveals are unchanged.
+ to a field inside an open shadow root, or into a frame or a custom element
+ whose field the page cannot see. A plain control inside an open shadow root
+ rides as any control does. Firefox for Android takes the same path, but
+ reports the hide once, at its end, and the content moves at that report
+ (AST-099 Gap 12). Safari's and Firefox's reveals are unchanged.
- **When the keyboard goes:** the body's scroll goes back to where it was
when the keyboard came up, within the range left once the inset drops. A
field that keeps focus, as after iOS's downward keyboard dismissal, is then
diff --git a/packages/core/src/keyboard/useMobileKeyboard.ts b/packages/core/src/keyboard/useMobileKeyboard.ts
index b5348d35f2..a9a3e88977 100644
--- a/packages/core/src/keyboard/useMobileKeyboard.ts
+++ b/packages/core/src/keyboard/useMobileKeyboard.ts
@@ -23,6 +23,7 @@ import {
type MotionHandle,
} from '../motion/animator';
import {KEYBOARD_SPRING} from '../motion/uikit/springs';
+import {deepActiveElement} from '../utils/flatTree';
import {subscribeInput} from '../utils/inputStream';
import {layoutViewportHeight} from '../utils/layoutViewport';
import {isDocument, isNode} from '../utils/nodeType';
@@ -140,10 +141,11 @@ function isVisualViewportRecovered(): boolean {
return getKeyboardHeight(window) <= 0.5;
}
-// Whether focus on `target` may keep the keyboard up. A control that brings
-// one up does, and so can a frame, an open shadow host or a custom element,
-// through its own field.
-function mayKeepKeyboard(target: Element | null): boolean {
+// Whether the focused control may keep the keyboard up, read inside open
+// shadow roots. A frame or a custom element can hold a field the page cannot
+// see. An open shadow host that has focus itself also counts.
+function mayKeepKeyboard(): boolean {
+ const target = deepActiveElement(document);
return target != null && (bringsUpKeyboard(target) || hidesField(target));
}
@@ -536,14 +538,14 @@ export function useMobileKeyboard(
const settle = keepDrawnPlace();
applyKeyboardGeometry(keyboardGeometry);
settle();
- // Focus left for a control that brings up no keyboard, so the
- // keyboard goes down now. A select's picker takes the keyboard's
- // place instead. A frame, an open shadow host or a custom element
- // can hold a field that keeps it up.
+ // Focus left for a control that brings up no keyboard, inside an
+ // open shadow root too, so the keyboard goes down now. A select's
+ // picker takes the keyboard's place instead. A frame or a custom
+ // element can hold a field the page cannot see, which keeps it up.
if (
isExpandedRef.current &&
hasKeyboardLayoutRef.current &&
- !mayKeepKeyboard(document.activeElement)
+ !mayKeepKeyboard()
) {
holdThroughUnguidedHide();
}
@@ -691,9 +693,9 @@ export function useMobileKeyboard(
// inset drops and the scroll goes back with the translate in one commit.
// So nothing moves then. A ride not yet landed goes on from there at the
// same speed. A control that takes focus first and brings up a keyboard
- // or a picker keeps that space covered, so the content rides back up.
- // Focus moving into a frame, an open shadow host or a custom element
- // does too: a field there can keep the keyboard.
+ // or a picker keeps that space covered, so the content rides back up. A
+ // field inside an open shadow root does too, and so can a frame or a
+ // custom element, whose field the page cannot see.
const holdThroughUnguidedHide = () => {
const animator = unguidedAnimator();
if (animator == null || heldHide != null) {
@@ -714,7 +716,7 @@ export function useMobileKeyboard(
if (
active instanceof HTMLElement &&
body.contains(active) &&
- mayKeepKeyboard(active)
+ mayKeepKeyboard()
) {
hold.release();
void rideKeyboardSpring(animator, 0);