@astryxdesign/core 0.7.8-canary.ffd29c5 → 0.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. package/CHANGELOG.md +132 -0
  2. package/dist/BottomSheet/BottomSheetPanel.d.ts +1 -1
  3. package/dist/BottomSheet/BottomSheetPanel.d.ts.map +1 -1
  4. package/dist/BottomSheet/BottomSheetPanel.js +30 -14
  5. package/dist/BottomSheet/bottomSheetStack.js +1 -1
  6. package/dist/BottomSheet/useSheetGestures.d.ts +3 -1
  7. package/dist/BottomSheet/useSheetGestures.d.ts.map +1 -1
  8. package/dist/BottomSheet/useSheetGestures.js +4 -0
  9. package/dist/CheckboxList/CheckboxList.d.ts +13 -2
  10. package/dist/CheckboxList/CheckboxList.d.ts.map +1 -1
  11. package/dist/CheckboxList/CheckboxList.js +10 -4
  12. package/dist/CheckboxList/CheckboxListContext.d.ts +7 -0
  13. package/dist/CheckboxList/CheckboxListContext.d.ts.map +1 -1
  14. package/dist/CheckboxList/CheckboxListContext.js +2 -1
  15. package/dist/CheckboxList/CheckboxListItem.d.ts.map +1 -1
  16. package/dist/CheckboxList/CheckboxListItem.js +14 -2
  17. package/dist/CheckboxList/requiredGroup.d.ts +22 -0
  18. package/dist/CheckboxList/requiredGroup.d.ts.map +1 -0
  19. package/dist/CheckboxList/requiredGroup.js +52 -0
  20. package/dist/DateInput/PickerWheel.d.ts +1 -1
  21. package/dist/DateInput/PickerWheel.d.ts.map +1 -1
  22. package/dist/DateInput/PickerWheel.js +27 -6
  23. package/dist/DateInput/Wheel.d.ts +2 -2
  24. package/dist/DateInput/Wheel.d.ts.map +1 -1
  25. package/dist/DateInput/Wheel.js +20 -6
  26. package/dist/DateInput/datePickerTouch.d.ts +13 -0
  27. package/dist/DateInput/datePickerTouch.d.ts.map +1 -0
  28. package/dist/DateInput/datePickerTouch.js +14 -0
  29. package/dist/DateInput/wheelOptions.d.ts +22 -2
  30. package/dist/DateInput/wheelOptions.d.ts.map +1 -1
  31. package/dist/DateInput/wheelOptions.js +14 -2
  32. package/dist/DateTimeInput/TouchDateTimeField.d.ts.map +1 -1
  33. package/dist/DateTimeInput/TouchDateTimeField.js +62 -119
  34. package/dist/HoverCard/useHoverCard.d.ts.map +1 -1
  35. package/dist/HoverCard/useHoverCard.js +6 -1
  36. package/dist/Overlay/Overlay.d.ts.map +1 -1
  37. package/dist/Overlay/Overlay.js +7 -2
  38. package/dist/PowerSearch/PowerSearch.d.ts +4 -2
  39. package/dist/PowerSearch/PowerSearch.d.ts.map +1 -1
  40. package/dist/PowerSearch/PowerSearchBottomSheet.d.ts +24 -1
  41. package/dist/PowerSearch/PowerSearchBottomSheet.d.ts.map +1 -1
  42. package/dist/PowerSearch/PowerSearchBottomSheet.js +172 -50
  43. package/dist/Resizable/ResizeHandle.d.ts.map +1 -1
  44. package/dist/Resizable/ResizeHandle.js +15 -15
  45. package/dist/Resizable/createResizable.d.ts +70 -16
  46. package/dist/Resizable/createResizable.d.ts.map +1 -1
  47. package/dist/Resizable/createResizable.js +180 -22
  48. package/dist/Resizable/separatorAttributes.d.ts +45 -0
  49. package/dist/Resizable/separatorAttributes.d.ts.map +1 -0
  50. package/dist/Resizable/separatorAttributes.js +35 -0
  51. package/dist/Resizable/useResizable.js +2 -2
  52. package/dist/TimeInput/TimeWheels.d.ts +32 -0
  53. package/dist/TimeInput/TimeWheels.d.ts.map +1 -0
  54. package/dist/TimeInput/TimeWheels.js +144 -0
  55. package/dist/TimeInput/TouchTimeField.d.ts.map +1 -1
  56. package/dist/TimeInput/TouchTimeField.js +63 -118
  57. package/dist/TimeInput/timeWheelRules.d.ts +124 -0
  58. package/dist/TimeInput/timeWheelRules.d.ts.map +1 -0
  59. package/dist/TimeInput/timeWheelRules.js +277 -0
  60. package/dist/Tokenizer/TokenizerBottomSheet.js +8 -12
  61. package/dist/Tooltip/useTooltip.d.ts.map +1 -1
  62. package/dist/Tooltip/useTooltip.js +6 -0
  63. package/dist/astryx.css +3 -1
  64. package/dist/gestures/GestureHost.d.ts.map +1 -1
  65. package/dist/gestures/GestureHost.js +3 -0
  66. package/dist/i18n/generated-english/PowerSearch/PowerSearchBottomSheet.generated.d.ts.map +1 -1
  67. package/dist/i18n/generated-english/PowerSearch/PowerSearchBottomSheet.generated.js +5 -2
  68. package/dist/i18n/generated-english/Resizable/createResizable.generated.d.ts +8 -0
  69. package/dist/i18n/generated-english/Resizable/createResizable.generated.d.ts.map +1 -0
  70. package/dist/i18n/generated-english/Resizable/createResizable.generated.js +13 -0
  71. package/dist/i18n/generated-english/TimeInput/TimeWheels.generated.d.ts +8 -0
  72. package/dist/i18n/generated-english/TimeInput/TimeWheels.generated.d.ts.map +1 -0
  73. package/dist/i18n/generated-english/TimeInput/TimeWheels.generated.js +17 -0
  74. package/dist/i18n/generated-locales/en.generated.d.ts.map +1 -1
  75. package/dist/i18n/generated-locales/en.generated.js +5 -2
  76. package/dist/i18n/generated-locales/pseudo.generated.d.ts.map +1 -1
  77. package/dist/i18n/generated-locales/pseudo.generated.js +5 -2
  78. package/dist/keyboard/index.d.ts +17 -0
  79. package/dist/keyboard/index.d.ts.map +1 -0
  80. package/dist/keyboard/index.js +19 -0
  81. package/dist/keyboard/keyboardHeight.d.ts +149 -0
  82. package/dist/keyboard/keyboardHeight.d.ts.map +1 -0
  83. package/dist/keyboard/keyboardHeight.js +432 -0
  84. package/dist/keyboard/keyboardLayoutGuide.d.ts +59 -0
  85. package/dist/keyboard/keyboardLayoutGuide.d.ts.map +1 -0
  86. package/dist/keyboard/keyboardLayoutGuide.js +207 -0
  87. package/dist/keyboard/keyboardModel.d.ts +93 -0
  88. package/dist/keyboard/keyboardModel.d.ts.map +1 -0
  89. package/dist/keyboard/keyboardModel.js +220 -0
  90. package/dist/keyboard/rideKeyboard.d.ts +42 -0
  91. package/dist/keyboard/rideKeyboard.d.ts.map +1 -0
  92. package/dist/keyboard/rideKeyboard.js +77 -0
  93. package/dist/keyboard/useMobileKeyboard.d.ts +61 -0
  94. package/dist/keyboard/useMobileKeyboard.d.ts.map +1 -0
  95. package/dist/keyboard/useMobileKeyboard.js +699 -0
  96. package/dist/motion/android/index.d.ts +16 -0
  97. package/dist/motion/android/index.d.ts.map +1 -0
  98. package/dist/motion/android/index.js +18 -0
  99. package/dist/motion/android/keyboard.d.ts +83 -0
  100. package/dist/motion/android/keyboard.d.ts.map +1 -0
  101. package/dist/motion/android/keyboard.js +96 -0
  102. package/dist/motion/uikit/datePicker.d.ts +58 -0
  103. package/dist/motion/uikit/datePicker.d.ts.map +1 -0
  104. package/dist/motion/uikit/datePicker.js +60 -0
  105. package/dist/motion/uikit/index.d.ts +4 -3
  106. package/dist/motion/uikit/index.d.ts.map +1 -1
  107. package/dist/motion/uikit/index.js +4 -3
  108. package/dist/utils/interactionOverlay.stylex.d.ts +3 -2
  109. package/dist/utils/interactionOverlay.stylex.d.ts.map +1 -1
  110. package/dist/utils/interactionOverlay.stylex.js +3 -2
  111. package/dist/utils/layoutViewport.d.ts +25 -0
  112. package/dist/utils/layoutViewport.d.ts.map +1 -0
  113. package/dist/utils/layoutViewport.js +102 -0
  114. package/dist/utils/pressFeedback.d.ts.map +1 -1
  115. package/dist/utils/pressFeedback.js +74 -9
  116. package/dist/utils/pressGesture.d.ts +4 -2
  117. package/dist/utils/pressGesture.d.ts.map +1 -1
  118. package/dist/utils/pressGesture.js +3 -1
  119. package/dist/utils/selectionLock.d.ts +10 -1
  120. package/dist/utils/selectionLock.d.ts.map +1 -1
  121. package/dist/utils/selectionLock.js +31 -5
  122. package/dist/utils/sharedMutationObserver.d.ts +2 -1
  123. package/dist/utils/sharedMutationObserver.d.ts.map +1 -1
  124. package/dist/utils/sharedMutationObserver.js +2 -1
  125. package/dist/utils/timeParser.d.ts +2 -1
  126. package/dist/utils/timeParser.d.ts.map +1 -1
  127. package/dist/utils/timeParser.js +29 -3
  128. package/locales/en.json +21 -9
  129. package/locales/pseudo.json +15 -6
  130. package/package.json +5 -5
  131. package/src/BottomSheet/BottomSheet.doc.mjs +1 -1
  132. package/src/BottomSheet/BottomSheetPanel.tsx +41 -13
  133. package/src/BottomSheet/BottomSheetSwitcher.doc.mjs +5 -0
  134. package/src/BottomSheet/bottomSheetStack.ts +1 -1
  135. package/src/BottomSheet/useSheetGestures.ts +6 -0
  136. package/src/CheckboxList/CheckboxList.doc.mjs +7 -0
  137. package/src/CheckboxList/CheckboxList.tsx +34 -2
  138. package/src/CheckboxList/CheckboxListContext.tsx +9 -1
  139. package/src/CheckboxList/CheckboxListItem.tsx +23 -2
  140. package/src/CheckboxList/requiredGroup.ts +77 -0
  141. package/src/DateInput/PickerWheel.tsx +37 -4
  142. package/src/DateInput/Wheel.tsx +30 -3
  143. package/src/DateInput/datePickerTouch.ts +15 -0
  144. package/src/DateInput/wheelOptions.ts +31 -2
  145. package/src/DateTimeInput/TouchDateTimeField.tsx +94 -178
  146. package/src/HoverCard/useHoverCard.tsx +6 -1
  147. package/src/Overlay/Overlay.tsx +7 -2
  148. package/src/PowerSearch/PowerSearch.doc.mjs +2 -2
  149. package/src/PowerSearch/PowerSearch.tsx +4 -2
  150. package/src/PowerSearch/PowerSearchBottomSheet.tsx +237 -62
  151. package/src/Resizable/ResizeHandle.tsx +18 -26
  152. package/src/Resizable/createResizable.ts +242 -27
  153. package/src/Resizable/separatorAttributes.ts +73 -0
  154. package/src/Resizable/useResizable.ts +2 -2
  155. package/src/TimeInput/TimeWheels.tsx +218 -0
  156. package/src/TimeInput/TouchTimeField.tsx +93 -157
  157. package/src/TimeInput/timeWheelRules.ts +352 -0
  158. package/src/Tokenizer/TokenizerBottomSheet.tsx +8 -12
  159. package/src/Tooltip/useTooltip.tsx +6 -0
  160. package/src/gestures/GestureHost.ts +5 -4
  161. package/src/i18n/generated-english/PowerSearch/PowerSearchBottomSheet.generated.ts +5 -2
  162. package/src/i18n/generated-english/Resizable/createResizable.generated.ts +14 -0
  163. package/src/i18n/generated-english/TimeInput/TimeWheels.generated.ts +18 -0
  164. package/src/i18n/generated-locales/en.generated.ts +6 -4
  165. package/src/i18n/generated-locales/pseudo.generated.ts +6 -4
  166. package/src/keyboard/README.md +197 -0
  167. package/src/keyboard/index.ts +48 -0
  168. package/src/keyboard/keyboardHeight.ts +543 -0
  169. package/src/keyboard/keyboardLayoutGuide.ts +266 -0
  170. package/src/keyboard/keyboardModel.ts +301 -0
  171. package/src/keyboard/rideKeyboard.ts +100 -0
  172. package/src/keyboard/useMobileKeyboard.ts +866 -0
  173. package/src/motion/MEASUREMENTS.md +10 -0
  174. package/src/motion/README.md +2 -0
  175. package/src/motion/android/index.ts +18 -0
  176. package/src/motion/android/keyboard.ts +96 -0
  177. package/src/motion/uikit/datePicker.ts +60 -0
  178. package/src/motion/uikit/index.ts +4 -3
  179. package/src/utils/interactionOverlay.stylex.ts +3 -2
  180. package/src/utils/layoutViewport.ts +104 -0
  181. package/src/utils/pressFeedback.ts +99 -21
  182. package/src/utils/pressGesture.ts +4 -2
  183. package/src/utils/selectionLock.ts +40 -6
  184. package/src/utils/sharedMutationObserver.ts +2 -1
  185. package/src/utils/timeParser.ts +40 -3
  186. package/dist/BottomSheet/useMobileKeyboard.d.ts +0 -21
  187. package/dist/BottomSheet/useMobileKeyboard.d.ts.map +0 -1
  188. package/dist/BottomSheet/useMobileKeyboard.js +0 -413
  189. package/src/BottomSheet/useMobileKeyboard.ts +0 -542
@@ -1,542 +0,0 @@
1
- // Copyright (c) Meta Platforms, Inc. and affiliates.
2
-
3
- 'use client';
4
-
5
- /**
6
- * @file useMobileKeyboard.ts
7
- * @input Uses React effects and refs supplied by BottomSheet
8
- * @output Exports internal useMobileKeyboard hook
9
- * @position Internal to BottomSheet; not exported from the core entry point
10
- */
11
-
12
- // A fully expanded, explicitly Tall sheet gets a keyboard-aware internal
13
- // scroll range, and the sheet itself stays still. Other detents and heights
14
- // opt out. Tall-sheet travel or a close blurs the field and dismisses the
15
- // keyboard.
16
- //
17
- // On iOS the browser reveals a focused field by scrolling the document, and
18
- // a fixed sheet moves with it, so the page lurches. The reveal belongs to the
19
- // focus operation, so refuse it there: take over the focus change on the
20
- // capture-phase blur, focus with preventScroll, and then scroll the control
21
- // into view in the sheet's own scroller. A tap, the keyboard's Next, Tab and
22
- // focus() all pass through that one change.
23
-
24
- import {useEffect, useRef, type RefObject} from 'react';
25
-
26
- const MOBILE_KEYBOARD_INSET_VAR = '--_sheet-keyboard-inset';
27
- const NON_TEXT_INPUT_TYPES = new Set([
28
- 'button',
29
- 'checkbox',
30
- 'color',
31
- 'file',
32
- 'hidden',
33
- 'image',
34
- 'radio',
35
- 'range',
36
- 'reset',
37
- 'submit',
38
- ]);
39
-
40
- interface UseMobileKeyboardOptions {
41
- bodyRef: RefObject<HTMLElement | null>;
42
- bottomClearance: number;
43
- isEnabled: boolean;
44
- isFullyExpanded: boolean;
45
- isPageScrollLocked: boolean;
46
- isSheetTraveling: boolean;
47
- isOpen: boolean;
48
- isPresented: boolean;
49
- sheetRef: RefObject<HTMLDivElement | null>;
50
- }
51
-
52
- interface KeyboardGeometry {
53
- bodyBottom: number;
54
- }
55
-
56
- function getObstructionTop(): number {
57
- return window.visualViewport?.height ?? window.innerHeight;
58
- }
59
-
60
- // The keyboard is gone once the visible band reaches the layout viewport bottom.
61
- function isVisualViewportRecovered(): boolean {
62
- return getObstructionTop() >= window.innerHeight - 0.5;
63
- }
64
-
65
- function isIOSWebKit(): boolean {
66
- const userAgent = window.navigator.userAgent;
67
- return (
68
- /iPad|iPhone|iPod/.test(userAgent) ||
69
- (userAgent.includes('Macintosh') && window.navigator.maxTouchPoints > 1)
70
- );
71
- }
72
-
73
- function isTextEntryControl(element: Element | null): element is HTMLElement {
74
- if (element instanceof HTMLTextAreaElement) {
75
- return !element.disabled && !element.readOnly;
76
- }
77
- if (element instanceof HTMLInputElement) {
78
- return (
79
- !element.disabled &&
80
- !element.readOnly &&
81
- !NON_TEXT_INPUT_TYPES.has(
82
- (element.getAttribute('type') ?? 'text').toLowerCase(),
83
- )
84
- );
85
- }
86
- return (
87
- element instanceof HTMLElement &&
88
- element.matches('[contenteditable]:not([contenteditable="false"])')
89
- );
90
- }
91
-
92
- function findTextEntryControl(
93
- target: EventTarget | null,
94
- body: HTMLElement,
95
- ): HTMLElement | null {
96
- if (!(target instanceof Element)) {
97
- return null;
98
- }
99
-
100
- const direct = target.closest(
101
- 'input, textarea, [contenteditable]:not([contenteditable="false"])',
102
- );
103
- if (body.contains(direct) && isTextEntryControl(direct)) {
104
- return direct;
105
- }
106
-
107
- const label = target.closest('label');
108
- const control = label instanceof HTMLLabelElement ? label.control : null;
109
- return body.contains(control) && isTextEntryControl(control) ? control : null;
110
- }
111
-
112
- export function useMobileKeyboard({
113
- bodyRef,
114
- bottomClearance,
115
- isEnabled,
116
- isFullyExpanded,
117
- isPageScrollLocked,
118
- isSheetTraveling,
119
- isOpen,
120
- isPresented,
121
- sheetRef,
122
- }: UseMobileKeyboardOptions): void {
123
- const hasKeyboardLayoutRef = useRef(false);
124
- const retainKeyboardLayoutRef = useRef(false);
125
- const isFullyExpandedRef = useRef(isFullyExpanded);
126
- const isOpenRef = useRef(isOpen);
127
- isFullyExpandedRef.current = isFullyExpanded;
128
- isOpenRef.current = isOpen;
129
-
130
- useEffect(() => {
131
- if (!isEnabled || !isSheetTraveling) {
132
- return;
133
- }
134
- const sheet = sheetRef.current;
135
- const activeElement = document.activeElement;
136
- if (
137
- sheet &&
138
- activeElement instanceof HTMLElement &&
139
- activeElement !== sheet &&
140
- sheet.contains(activeElement)
141
- ) {
142
- // Keep the current keyboard scroll range while focusing the sheet
143
- // dismisses the keyboard. Viewport resize events unwind that layout as
144
- // the visual viewport recovers, avoiding a content jump on the first
145
- // drag frame.
146
- retainKeyboardLayoutRef.current = hasKeyboardLayoutRef.current;
147
- sheet.focus({preventScroll: true});
148
- }
149
- }, [isEnabled, isSheetTraveling, sheetRef]);
150
-
151
- // Syncs the on-screen keyboard with the sheet. When a presented sheet closes,
152
- // the focused field blurs, so the keyboard closes.
153
- useEffect(() => {
154
- if (!isEnabled || !isPresented || isOpen) {
155
- return;
156
- }
157
- const sheet = sheetRef.current;
158
- const activeElement = document.activeElement;
159
- if (
160
- sheet &&
161
- activeElement instanceof HTMLElement &&
162
- activeElement !== sheet &&
163
- sheet.contains(activeElement)
164
- ) {
165
- retainKeyboardLayoutRef.current = hasKeyboardLayoutRef.current;
166
- activeElement.blur();
167
- }
168
- }, [isEnabled, isOpen, isPresented, sheetRef]);
169
-
170
- // Syncs the body's end inset with the visual viewport, so the on-screen
171
- // keyboard does not cover the body.
172
- useEffect(() => {
173
- const body = bodyRef.current;
174
- const sheet = sheetRef.current;
175
- if (!isEnabled || !isPresented || !body) {
176
- return;
177
- }
178
-
179
- let keyboardGeometry: KeyboardGeometry | null = null;
180
- let keyboardObstructionTop: number | null = null;
181
- let bodyScrollTopAtKeyboard: number | null = null;
182
- let documentScrollAtKeyboard: {x: number; y: number} | null = null;
183
- const ownsFocusTransitions = isIOSWebKit();
184
-
185
- const clearKeyboardLayout = () => {
186
- body.style.setProperty(MOBILE_KEYBOARD_INSET_VAR, '0px');
187
- if (bodyScrollTopAtKeyboard != null) {
188
- body.scrollTop = bodyScrollTopAtKeyboard;
189
- }
190
- keyboardGeometry = null;
191
- keyboardObstructionTop = null;
192
- bodyScrollTopAtKeyboard = null;
193
- documentScrollAtKeyboard = null;
194
- hasKeyboardLayoutRef.current = false;
195
- retainKeyboardLayoutRef.current = false;
196
- };
197
-
198
- const scrollBodyBy = (distance: number, smoothly: boolean) => {
199
- if (typeof body.scrollBy !== 'function') {
200
- body.scrollTop += distance;
201
- return;
202
- }
203
- const reduceMotion =
204
- typeof window.matchMedia === 'function' &&
205
- window.matchMedia('(prefers-reduced-motion: reduce)').matches;
206
- body.scrollBy({
207
- top: distance,
208
- // Not 'auto': that defers to the element's computed `scroll-behavior`,
209
- // so a consumer's `scroll-behavior: smooth` would animate a scroll this
210
- // hook needs to land in the same frame.
211
- behavior: smoothly && !reduceMotion ? 'smooth' : 'instant',
212
- });
213
- };
214
-
215
- // Scroll `control` into the part of the body the keyboard leaves visible,
216
- // reading live geometry each time.
217
- const scrollControlIntoSafeArea = (
218
- control: HTMLElement,
219
- smoothly: boolean,
220
- ) => {
221
- const measuredBodyRect = body.getBoundingClientRect();
222
- const measuredControlRect = control.getBoundingClientRect();
223
- const obstructionTop = getObstructionTop();
224
- const overlap = Math.max(0, measuredBodyRect.bottom - obstructionTop);
225
- const clearance = overlap > 0 ? bottomClearance : 0;
226
- const safeTop = measuredBodyRect.top;
227
- const safeBottom = Math.min(
228
- measuredBodyRect.bottom,
229
- obstructionTop - clearance,
230
- );
231
- if (safeBottom <= safeTop) {
232
- return;
233
- }
234
-
235
- if (measuredControlRect.bottom > safeBottom) {
236
- scrollBodyBy(measuredControlRect.bottom - safeBottom, smoothly);
237
- } else if (measuredControlRect.top < safeTop) {
238
- scrollBodyBy(measuredControlRect.top - safeTop, smoothly);
239
- }
240
- };
241
-
242
- const applyKeyboardGeometry = (geometry: KeyboardGeometry) => {
243
- const obstructionTop = getObstructionTop();
244
- // A collapsed detent can extend the body below the layout viewport even
245
- // after the keyboard closes, so body overlap alone cannot identify
246
- // recovery. Once the visual viewport is full height again, release the
247
- // retained keyboard layout unconditionally.
248
- if (isVisualViewportRecovered()) {
249
- clearKeyboardLayout();
250
- return;
251
- }
252
- const overlap = Math.max(0, geometry.bodyBottom - obstructionTop);
253
- if (overlap === 0) {
254
- clearKeyboardLayout();
255
- return;
256
- }
257
-
258
- const inset = Math.max(
259
- 0,
260
- geometry.bodyBottom - (obstructionTop - bottomClearance),
261
- );
262
- body.style.setProperty(MOBILE_KEYBOARD_INSET_VAR, `${inset}px`);
263
- };
264
-
265
- // Take the focus transition over before the browser performs it.
266
- //
267
- // `blur` is dispatched in the capture phase ahead of the browser's own
268
- // focus step, and it names the destination in `relatedTarget`. Focusing it
269
- // here with preventScroll settles the transition: the browser's step finds
270
- // the element already active, so it dispatches nothing further and has
271
- // nothing to reveal.
272
- const claimFocusTransition = (event: FocusEvent) => {
273
- if (!isFullyExpandedRef.current) {
274
- return;
275
- }
276
- const destination = findTextEntryControl(event.relatedTarget, body);
277
- if (destination) {
278
- if (destination !== document.activeElement) {
279
- // Revealing the field is not this handler's job: focusing it raises
280
- // focusin, and the viewport resize that follows the keyboard raises
281
- // another — both already schedule the reveal below, which knows the
282
- // safe area and scrolls only the sheet's own body.
283
- destination.focus({preventScroll: true});
284
- }
285
- return;
286
- }
287
-
288
- // Focus left for nothing — the keyboard's Done button parks it on the
289
- // body. Park it on the sheet instead, so re-tapping the same field is
290
- // still a transition this handler sees. Left on the body, the field is
291
- // already `document.activeElement` on the next tap, no blur fires, and
292
- // the browser reveals it its own way.
293
- //
294
- // Only while the sheet is open: closing blurs the field too, and there
295
- // is no next tap to keep claimable — the host restores focus to whatever
296
- // opened the sheet.
297
- const origin = findTextEntryControl(event.target, body);
298
- if (isOpenRef.current && origin && !event.relatedTarget) {
299
- sheet?.focus({preventScroll: true});
300
- }
301
- };
302
-
303
- const revealFocusedControl = () => {
304
- const obstructionTop = getObstructionTop();
305
- const activeElement = document.activeElement;
306
- const focusedControl =
307
- activeElement instanceof HTMLElement &&
308
- body.contains(activeElement) &&
309
- isTextEntryControl(activeElement)
310
- ? activeElement
311
- : null;
312
- // The keyboard may close while the text field remains focused (for
313
- // example, a downward keyboard dismissal gesture on iOS). Run the full
314
- // cleanup path so the temporary spacer and the body scroll it enabled
315
- // are both restored. Then reveal the still-focused control against the
316
- // recovered sheet, because it may not be the field that opened the
317
- // keyboard and therefore may sit outside the restored scroll position.
318
- if (hasKeyboardLayoutRef.current && isVisualViewportRecovered()) {
319
- clearKeyboardLayout();
320
- if (isFullyExpandedRef.current && focusedControl) {
321
- scrollControlIntoSafeArea(focusedControl, false);
322
- }
323
- return;
324
- }
325
- if (!isFullyExpandedRef.current || focusedControl == null) {
326
- if (retainKeyboardLayoutRef.current && keyboardGeometry) {
327
- applyKeyboardGeometry(keyboardGeometry);
328
- return;
329
- }
330
- clearKeyboardLayout();
331
- return;
332
- }
333
-
334
- // An interactively dismissed iOS keyboard can leave the accessory bar
335
- // visible, so the visual viewport grows without reaching full height.
336
- // Restore the pre-keyboard body position as soon as that recovery starts;
337
- // the ordinary safe-area pass below may still scroll a low focused field
338
- // just enough for whatever obstruction remains. Advance the remembered
339
- // obstruction so later content mutations do not repeat this recovery.
340
- if (
341
- hasKeyboardLayoutRef.current &&
342
- keyboardObstructionTop != null &&
343
- obstructionTop > keyboardObstructionTop + 0.5 &&
344
- bodyScrollTopAtKeyboard != null
345
- ) {
346
- body.scrollTop = bodyScrollTopAtKeyboard;
347
- keyboardObstructionTop = obstructionTop;
348
- }
349
-
350
- retainKeyboardLayoutRef.current = false;
351
-
352
- const measuredBodyRect = body.getBoundingClientRect();
353
- const overlap = Math.max(0, measuredBodyRect.bottom - obstructionTop);
354
- // The extra clearance leaves room for mobile suggestion UI, but only
355
- // while the visual viewport actually overlaps the sheet. With no
356
- // obstruction, ordinary desktop and hardware-keyboard focus must not
357
- // shift an already visible control.
358
- const clearance = overlap > 0 ? bottomClearance : 0;
359
- keyboardGeometry =
360
- overlap > 0
361
- ? {
362
- bodyBottom: measuredBodyRect.bottom,
363
- }
364
- : null;
365
- // Where the document sits with the keyboard up and nothing yet shifted:
366
- // the position handleDocumentScroll returns to. Captured on the
367
- // transition only — a reveal that runs after the browser has already
368
- // scrolled would otherwise record the shifted position as correct.
369
- if (overlap > 0 && !hasKeyboardLayoutRef.current) {
370
- keyboardObstructionTop = obstructionTop;
371
- bodyScrollTopAtKeyboard = body.scrollTop;
372
- if (isPageScrollLocked) {
373
- documentScrollAtKeyboard = {x: window.scrollX, y: window.scrollY};
374
- }
375
- }
376
- hasKeyboardLayoutRef.current = overlap > 0;
377
-
378
- const inset =
379
- overlap > 0
380
- ? Math.max(0, measuredBodyRect.bottom - (obstructionTop - clearance))
381
- : 0;
382
- body.style.setProperty(MOBILE_KEYBOARD_INSET_VAR, `${inset}px`);
383
-
384
- scrollControlIntoSafeArea(focusedControl, overlap > 0);
385
- };
386
-
387
- let animationFrame = 0;
388
- const scheduleReveal = () => {
389
- cancelAnimationFrame(animationFrame);
390
- animationFrame = requestAnimationFrame(revealFocusedControl);
391
- };
392
-
393
- // Resuming the app re-reveals the focused field with no focus event to
394
- // claim, so the reveal above never runs and the document scrolls — taking
395
- // the fixed sheet with it. Put that scroll back on the event that reports
396
- // it and re-reveal inside the sheet.
397
- //
398
- // Only while the page is locked, which is the only state in which a
399
- // document scroll cannot be the user's own: behind a non-modal sheet the
400
- // page stays scrollable, and reverting there would fight them.
401
- const handleDocumentScroll = () => {
402
- const expected = documentScrollAtKeyboard;
403
- if (
404
- expected == null ||
405
- (window.scrollX === expected.x && window.scrollY === expected.y)
406
- ) {
407
- return;
408
- }
409
- window.scrollTo(expected.x, expected.y);
410
- const activeElement = document.activeElement;
411
- if (
412
- activeElement instanceof HTMLElement &&
413
- body.contains(activeElement) &&
414
- isTextEntryControl(activeElement)
415
- ) {
416
- scrollControlIntoSafeArea(activeElement, false);
417
- }
418
- };
419
-
420
- const resizeObserver =
421
- typeof ResizeObserver === 'undefined'
422
- ? null
423
- : new ResizeObserver(scheduleReveal);
424
- const refreshObservedLayout = () => {
425
- if (!resizeObserver) {
426
- return;
427
- }
428
- resizeObserver.disconnect();
429
-
430
- // Body/sheet box changes include public height and xstyle updates. The
431
- // generated inset pseudo-element changes only scroll overflow, so these
432
- // observations do not feed the keyboard inset back into sheet geometry.
433
- const targets = new Set<Element>([body]);
434
- if (sheet) {
435
- targets.add(sheet);
436
- }
437
- for (const child of body.children) {
438
- targets.add(child);
439
- }
440
- const activeElement = document.activeElement;
441
- if (
442
- activeElement instanceof HTMLElement &&
443
- body.contains(activeElement)
444
- ) {
445
- for (
446
- let element: HTMLElement | null = activeElement;
447
- element && element !== body;
448
- element = element.parentElement
449
- ) {
450
- targets.add(element);
451
- }
452
- }
453
- for (const target of targets) {
454
- resizeObserver.observe(target);
455
- }
456
- };
457
- const mutationObserver =
458
- typeof MutationObserver === 'undefined'
459
- ? null
460
- : new MutationObserver(records => {
461
- // Ignore the internal inset written on the body. Consumer DOM,
462
- // text, class, style, or visibility changes can all move the
463
- // focused control without a viewport event.
464
- if (
465
- records.every(
466
- record =>
467
- record.type === 'attributes' && record.target === body,
468
- )
469
- ) {
470
- return;
471
- }
472
- refreshObservedLayout();
473
- scheduleReveal();
474
- });
475
- const handleFocusIn = () => {
476
- refreshObservedLayout();
477
- scheduleReveal();
478
- };
479
- const handleFocusOut = () => {
480
- // Keep the added scroll range while the keyboard animates away. A focus
481
- // transition to another text-entry control cancels this in focusin.
482
- retainKeyboardLayoutRef.current = hasKeyboardLayoutRef.current;
483
- scheduleReveal();
484
- };
485
- const handleSheetTransitionEnd = (event: TransitionEvent) => {
486
- if (event.target === sheet && event.propertyName === 'transform') {
487
- scheduleReveal();
488
- }
489
- };
490
-
491
- const viewport = window.visualViewport;
492
- if (ownsFocusTransitions) {
493
- // `blur` does not bubble, so the capture phase is the only place to hear
494
- // every one of them — and it runs before the browser's own focus step,
495
- // which is the only window in which the transition can still be claimed.
496
- document.addEventListener('blur', claimFocusTransition, true);
497
- }
498
- body.addEventListener('focusin', handleFocusIn);
499
- body.addEventListener('focusout', handleFocusOut);
500
- sheet?.addEventListener('transitionend', handleSheetTransitionEnd);
501
- viewport?.addEventListener('resize', scheduleReveal);
502
- viewport?.addEventListener('scroll', scheduleReveal);
503
- window.addEventListener('scroll', handleDocumentScroll);
504
- window.addEventListener('resize', scheduleReveal);
505
- mutationObserver?.observe(body, {
506
- attributes: true,
507
- characterData: true,
508
- childList: true,
509
- subtree: true,
510
- });
511
- refreshObservedLayout();
512
- // The dialog enters the top layer in a later effect. Wait until the next
513
- // frame so flex geometry is final before measuring the body.
514
- scheduleReveal();
515
-
516
- return () => {
517
- cancelAnimationFrame(animationFrame);
518
- if (ownsFocusTransitions) {
519
- document.removeEventListener('blur', claimFocusTransition, true);
520
- }
521
- body.removeEventListener('focusin', handleFocusIn);
522
- body.removeEventListener('focusout', handleFocusOut);
523
- sheet?.removeEventListener('transitionend', handleSheetTransitionEnd);
524
- viewport?.removeEventListener('resize', scheduleReveal);
525
- viewport?.removeEventListener('scroll', scheduleReveal);
526
- window.removeEventListener('scroll', handleDocumentScroll);
527
- window.removeEventListener('resize', scheduleReveal);
528
- resizeObserver?.disconnect();
529
- mutationObserver?.disconnect();
530
- body.style.removeProperty(MOBILE_KEYBOARD_INSET_VAR);
531
- hasKeyboardLayoutRef.current = false;
532
- retainKeyboardLayoutRef.current = false;
533
- };
534
- }, [
535
- bodyRef,
536
- bottomClearance,
537
- isEnabled,
538
- isPageScrollLocked,
539
- isPresented,
540
- sheetRef,
541
- ]);
542
- }