@astryxdesign/core 0.4.3 → 0.4.4-canary.0e8af82

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 (254) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +79 -45
  3. package/dist/Banner/Banner.d.ts +3 -0
  4. package/dist/Banner/Banner.d.ts.map +1 -1
  5. package/dist/Banner/Banner.js +13 -3
  6. package/dist/BottomSheet/BottomSheet.d.ts +75 -0
  7. package/dist/BottomSheet/BottomSheet.d.ts.map +1 -0
  8. package/dist/BottomSheet/BottomSheet.js +396 -0
  9. package/dist/BottomSheet/BottomSheetPanel.d.ts +63 -0
  10. package/dist/BottomSheet/BottomSheetPanel.d.ts.map +1 -0
  11. package/dist/BottomSheet/BottomSheetPanel.js +441 -0
  12. package/dist/BottomSheet/BottomSheetSwitcher.d.ts +61 -0
  13. package/dist/BottomSheet/BottomSheetSwitcher.d.ts.map +1 -0
  14. package/dist/BottomSheet/BottomSheetSwitcher.js +410 -0
  15. package/dist/BottomSheet/BottomSheetSwitcherContext.d.ts +21 -0
  16. package/dist/BottomSheet/BottomSheetSwitcherContext.d.ts.map +1 -0
  17. package/dist/BottomSheet/BottomSheetSwitcherContext.js +13 -0
  18. package/dist/BottomSheet/index.d.ts +11 -0
  19. package/dist/BottomSheet/index.d.ts.map +1 -0
  20. package/dist/BottomSheet/index.js +12 -0
  21. package/dist/BottomSheet/snapOffsets.d.ts +109 -0
  22. package/dist/BottomSheet/snapOffsets.d.ts.map +1 -0
  23. package/dist/BottomSheet/snapOffsets.js +206 -0
  24. package/dist/BottomSheet/useMobileKeyboard.d.ts +35 -0
  25. package/dist/BottomSheet/useMobileKeyboard.d.ts.map +1 -0
  26. package/dist/BottomSheet/useMobileKeyboard.js +373 -0
  27. package/dist/BottomSheet/useSheetGestures.d.ts +185 -0
  28. package/dist/BottomSheet/useSheetGestures.d.ts.map +1 -0
  29. package/dist/BottomSheet/useSheetGestures.js +1038 -0
  30. package/dist/Calendar/Calendar.d.ts +17 -0
  31. package/dist/Calendar/Calendar.d.ts.map +1 -1
  32. package/dist/Calendar/Calendar.js +25 -1
  33. package/dist/Calendar/hooks/useCalendarConstraints.d.ts +22 -1
  34. package/dist/Calendar/hooks/useCalendarConstraints.d.ts.map +1 -1
  35. package/dist/Calendar/hooks/useCalendarConstraints.js +26 -4
  36. package/dist/Chat/ChatComposerInput.d.ts.map +1 -1
  37. package/dist/Chat/ChatComposerInput.js +3 -4
  38. package/dist/CheckboxInput/CheckboxInput.d.ts.map +1 -1
  39. package/dist/CheckboxInput/CheckboxInput.js +9 -0
  40. package/dist/ComplexSelector/ComplexSelector.d.ts.map +1 -1
  41. package/dist/ComplexSelector/ComplexSelector.js +18 -7
  42. package/dist/ContextMenu/ContextMenu.d.ts.map +1 -1
  43. package/dist/ContextMenu/ContextMenu.js +4 -2
  44. package/dist/DateInput/DateInput.d.ts.map +1 -1
  45. package/dist/DateInput/DateInput.js +15 -2
  46. package/dist/DateRangeInput/DateRangeInput.d.ts +23 -1
  47. package/dist/DateRangeInput/DateRangeInput.d.ts.map +1 -1
  48. package/dist/DateRangeInput/DateRangeInput.js +38 -3
  49. package/dist/DateTimeInput/DateTimeInput.d.ts.map +1 -1
  50. package/dist/DateTimeInput/DateTimeInput.js +38 -3
  51. package/dist/Dialog/Dialog.d.ts.map +1 -1
  52. package/dist/Dialog/Dialog.js +2 -1
  53. package/dist/Field/FieldLabel.d.ts +2 -1
  54. package/dist/Field/FieldLabel.d.ts.map +1 -1
  55. package/dist/Field/FieldLabel.js +21 -3
  56. package/dist/Field/InputClearButton.d.ts +2 -1
  57. package/dist/Field/InputClearButton.d.ts.map +1 -1
  58. package/dist/Field/InputClearButton.js +6 -1
  59. package/dist/FormLayout/FormLayout.d.ts +24 -2
  60. package/dist/FormLayout/FormLayout.d.ts.map +1 -1
  61. package/dist/FormLayout/FormLayout.js +7 -2
  62. package/dist/FormLayout/FormLayoutContext.d.ts +15 -2
  63. package/dist/FormLayout/FormLayoutContext.d.ts.map +1 -1
  64. package/dist/FormLayout/FormLayoutContext.js +19 -4
  65. package/dist/FormLayout/index.d.ts +1 -1
  66. package/dist/FormLayout/index.d.ts.map +1 -1
  67. package/dist/Lightbox/Lightbox.d.ts.map +1 -1
  68. package/dist/Lightbox/Lightbox.js +9 -3
  69. package/dist/MobileNav/MobileNav.d.ts +12 -0
  70. package/dist/MobileNav/MobileNav.d.ts.map +1 -1
  71. package/dist/MobileNav/MobileNav.js +102 -30
  72. package/dist/MultiSelector/MultiSelector.d.ts.map +1 -1
  73. package/dist/MultiSelector/MultiSelector.js +27 -8
  74. package/dist/NumberInput/NumberInput.d.ts.map +1 -1
  75. package/dist/NumberInput/NumberInput.js +15 -2
  76. package/dist/PowerSearch/PowerSearchEditPopover.js +1 -1
  77. package/dist/RadioList/RadioList.d.ts.map +1 -1
  78. package/dist/RadioList/RadioList.js +12 -1
  79. package/dist/Selector/Selector.d.ts.map +1 -1
  80. package/dist/Selector/Selector.js +20 -4
  81. package/dist/StatusDot/StatusDot.d.ts +43 -3
  82. package/dist/StatusDot/StatusDot.d.ts.map +1 -1
  83. package/dist/StatusDot/StatusDot.js +42 -5
  84. package/dist/Switch/Switch.d.ts.map +1 -1
  85. package/dist/Switch/Switch.js +14 -1
  86. package/dist/Table/plugins/rowExpansion/useTableRowExpansion.d.ts.map +1 -1
  87. package/dist/Table/plugins/rowExpansion/useTableRowExpansion.js +7 -2
  88. package/dist/TextArea/TextArea.d.ts.map +1 -1
  89. package/dist/TextArea/TextArea.js +6 -1
  90. package/dist/TextInput/TextInput.d.ts.map +1 -1
  91. package/dist/TextInput/TextInput.js +6 -1
  92. package/dist/TimeInput/TimeInput.d.ts.map +1 -1
  93. package/dist/TimeInput/TimeInput.js +14 -2
  94. package/dist/Tokenizer/Tokenizer.js +11 -5
  95. package/dist/Tooltip/useTooltip.d.ts.map +1 -1
  96. package/dist/Tooltip/useTooltip.js +4 -1
  97. package/dist/Typeahead/BaseTypeahead.d.ts.map +1 -1
  98. package/dist/Typeahead/BaseTypeahead.js +5 -3
  99. package/dist/Typeahead/Typeahead.d.ts.map +1 -1
  100. package/dist/Typeahead/Typeahead.js +10 -1
  101. package/dist/astryx.css +21 -0
  102. package/dist/hooks/index.d.ts +8 -1
  103. package/dist/hooks/index.d.ts.map +1 -1
  104. package/dist/hooks/index.js +8 -1
  105. package/dist/hooks/useFocusTrap.d.ts +0 -11
  106. package/dist/hooks/useFocusTrap.d.ts.map +1 -1
  107. package/dist/hooks/useFocusTrap.js +1 -11
  108. package/dist/hooks/useInputStatusIcon.d.ts.map +1 -1
  109. package/dist/hooks/useInputStatusIcon.js +1 -0
  110. package/dist/hooks/useResolvedRequired.d.ts +19 -0
  111. package/dist/hooks/useResolvedRequired.d.ts.map +1 -0
  112. package/dist/hooks/useResolvedRequired.js +40 -0
  113. package/dist/index.d.ts +1 -0
  114. package/dist/index.d.ts.map +1 -1
  115. package/dist/index.js +1 -0
  116. package/dist/theme/defineTheme.d.ts +22 -4
  117. package/dist/theme/defineTheme.d.ts.map +1 -1
  118. package/dist/theme/expandColorScale.d.ts +17 -2
  119. package/dist/theme/expandColorScale.d.ts.map +1 -1
  120. package/dist/theme/expandColorScale.js +63 -33
  121. package/dist/utils/ime.d.ts +5 -0
  122. package/dist/utils/ime.d.ts.map +1 -0
  123. package/dist/utils/ime.js +63 -0
  124. package/dist/utils/index.d.ts +1 -0
  125. package/dist/utils/index.d.ts.map +1 -1
  126. package/dist/utils/index.js +2 -1
  127. package/dist/utils/plainDate.d.ts +6 -0
  128. package/dist/utils/plainDate.d.ts.map +1 -1
  129. package/dist/utils/plainDate.js +12 -0
  130. package/locales/af-ZA.json +958 -0
  131. package/locales/ar-SA.json +998 -0
  132. package/locales/ca-ES.json +974 -0
  133. package/locales/cs-CZ.json +990 -0
  134. package/locales/da-DK.json +966 -0
  135. package/locales/de-DE.json +966 -0
  136. package/locales/el-GR.json +990 -0
  137. package/locales/en.json +52 -0
  138. package/locales/es-ES.json +978 -0
  139. package/locales/fi-FI.json +990 -0
  140. package/locales/fr-FR.json +928 -0
  141. package/locales/he-IL.json +990 -0
  142. package/locales/hu-HU.json +990 -0
  143. package/locales/it-IT.json +978 -0
  144. package/locales/ja-JP.json +998 -0
  145. package/locales/ko-KR.json +990 -0
  146. package/locales/nl-NL.json +954 -0
  147. package/locales/no-NO.json +978 -0
  148. package/locales/pl-PL.json +982 -0
  149. package/locales/pseudo.json +39 -0
  150. package/locales/pt-BR.json +982 -0
  151. package/locales/pt-PT.json +982 -0
  152. package/locales/ro-RO.json +982 -0
  153. package/locales/ru-RU.json +990 -0
  154. package/locales/sr-SP.json +990 -0
  155. package/locales/sv-SE.json +986 -0
  156. package/locales/tr-TR.json +990 -0
  157. package/locales/uk-UA.json +990 -0
  158. package/locales/vi-VN.json +986 -0
  159. package/locales/zh-CN.json +998 -0
  160. package/locales/zh-TW.json +998 -0
  161. package/package.json +9 -8
  162. package/src/Banner/Banner.doc.mjs +3 -3
  163. package/src/Banner/Banner.test.tsx +40 -0
  164. package/src/Banner/Banner.tsx +27 -2
  165. package/src/BottomSheet/BottomSheet.doc.mjs +299 -0
  166. package/src/BottomSheet/BottomSheet.test.tsx +2265 -0
  167. package/src/BottomSheet/BottomSheet.tsx +559 -0
  168. package/src/BottomSheet/BottomSheetPanel.test.tsx +240 -0
  169. package/src/BottomSheet/BottomSheetPanel.tsx +601 -0
  170. package/src/BottomSheet/BottomSheetSwitcher.doc.mjs +219 -0
  171. package/src/BottomSheet/BottomSheetSwitcher.test.tsx +850 -0
  172. package/src/BottomSheet/BottomSheetSwitcher.tsx +628 -0
  173. package/src/BottomSheet/BottomSheetSwitcherContext.ts +51 -0
  174. package/src/BottomSheet/index.ts +19 -0
  175. package/src/BottomSheet/snapOffsets.test.ts +254 -0
  176. package/src/BottomSheet/snapOffsets.ts +245 -0
  177. package/src/BottomSheet/useMobileKeyboard.ts +499 -0
  178. package/src/BottomSheet/useSheetGestures.test.ts +1112 -0
  179. package/src/BottomSheet/useSheetGestures.ts +1503 -0
  180. package/src/Calendar/Calendar.doc.mjs +16 -0
  181. package/src/Calendar/Calendar.test.tsx +96 -0
  182. package/src/Calendar/Calendar.tsx +55 -0
  183. package/src/Calendar/hooks/useCalendarConstraints.ts +54 -3
  184. package/src/Chat/ChatComposerInput.tsx +3 -4
  185. package/src/CheckboxInput/CheckboxInput.tsx +6 -0
  186. package/src/ComplexSelector/ComplexSelector.tsx +15 -6
  187. package/src/ContextMenu/ContextMenu.tsx +4 -2
  188. package/src/DateInput/DateInput.test.tsx +21 -0
  189. package/src/DateInput/DateInput.tsx +12 -2
  190. package/src/DateRangeInput/DateRangeInput.doc.mjs +16 -0
  191. package/src/DateRangeInput/DateRangeInput.test.tsx +81 -1
  192. package/src/DateRangeInput/DateRangeInput.tsx +72 -1
  193. package/src/DateTimeInput/DateTimeInput.doc.mjs +4 -0
  194. package/src/DateTimeInput/DateTimeInput.test.tsx +137 -0
  195. package/src/DateTimeInput/DateTimeInput.tsx +43 -4
  196. package/src/Dialog/Dialog.tsx +2 -1
  197. package/src/Field/Field.doc.mjs +1 -0
  198. package/src/Field/FieldLabel.tsx +20 -4
  199. package/src/Field/InputClearButton.test.tsx +25 -0
  200. package/src/Field/InputClearButton.tsx +4 -1
  201. package/src/FileInput/FileInput.test.tsx +53 -9
  202. package/src/FormLayout/FormLayout.doc.mjs +13 -0
  203. package/src/FormLayout/FormLayout.test.tsx +181 -4
  204. package/src/FormLayout/FormLayout.tsx +33 -2
  205. package/src/FormLayout/FormLayoutContext.ts +22 -7
  206. package/src/FormLayout/index.ts +1 -1
  207. package/src/Lightbox/Lightbox.test.tsx +49 -28
  208. package/src/Lightbox/Lightbox.tsx +7 -3
  209. package/src/MobileNav/MobileNav.tsx +128 -37
  210. package/src/MobileNav/MobileNavCloseEdgeCases.test.tsx +426 -0
  211. package/src/MobileNav/MobileNavCloseTiming.test.ts +58 -0
  212. package/src/MobileNav/MobileNavCloseVisibility.test.tsx +172 -0
  213. package/src/MultiSelector/MultiSelector.test.tsx +195 -51
  214. package/src/MultiSelector/MultiSelector.tsx +25 -9
  215. package/src/NumberInput/NumberInput.test.tsx +18 -0
  216. package/src/NumberInput/NumberInput.tsx +12 -2
  217. package/src/PowerSearch/PowerSearchEditPopover.tsx +1 -1
  218. package/src/RadioList/RadioList.tsx +9 -1
  219. package/src/Selector/Selector.test.tsx +85 -26
  220. package/src/Selector/Selector.tsx +16 -5
  221. package/src/StatusDot/StatusDot.doc.mjs +13 -0
  222. package/src/StatusDot/StatusDot.test.tsx +115 -8
  223. package/src/StatusDot/StatusDot.tsx +74 -9
  224. package/src/Switch/Switch.test.tsx +16 -0
  225. package/src/Switch/Switch.tsx +13 -0
  226. package/src/Table/plugins/rowExpansion/useTableRowExpansion.tsx +20 -2
  227. package/src/TextArea/TextArea.tsx +3 -1
  228. package/src/TextInput/TextInput.tsx +3 -1
  229. package/src/TimeInput/TimeInput.test.tsx +23 -0
  230. package/src/TimeInput/TimeInput.tsx +11 -1
  231. package/src/Tokenizer/Tokenizer.test.tsx +50 -32
  232. package/src/Tokenizer/Tokenizer.tsx +5 -5
  233. package/src/Tooltip/useTooltip.tsx +4 -1
  234. package/src/Typeahead/BaseTypeahead.tsx +10 -3
  235. package/src/Typeahead/Typeahead.test.tsx +116 -7
  236. package/src/Typeahead/Typeahead.tsx +10 -1
  237. package/src/__tests__/stubMatchMedia.test.ts +79 -0
  238. package/src/__tests__/stubMatchMedia.ts +95 -0
  239. package/src/hooks/index.ts +9 -5
  240. package/src/hooks/useFocusTrap.ts +1 -14
  241. package/src/hooks/useInputStatusIcon.test.tsx +19 -0
  242. package/src/hooks/useInputStatusIcon.tsx +7 -0
  243. package/src/hooks/useResolvedRequired.ts +42 -0
  244. package/src/index.ts +1 -0
  245. package/src/theme/defineTheme.ts +22 -4
  246. package/src/theme/expandColorScale.test.ts +138 -0
  247. package/src/theme/expandColorScale.ts +89 -44
  248. package/src/utils/ime.test.ts +32 -0
  249. package/src/utils/ime.ts +69 -0
  250. package/src/utils/index.ts +2 -0
  251. package/src/utils/plainDate.test.ts +50 -0
  252. package/src/utils/plainDate.ts +12 -0
  253. package/dist/astryx.umd.js +0 -147
  254. package/dist/astryx.umd.js.map +0 -7
@@ -0,0 +1,2265 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file BottomSheet.test.tsx
5
+ * @input Uses vitest, @testing-library/react, BottomSheet component
6
+ * @output Unit tests for BottomSheet component behavior
7
+ * @position Core testing; validates BottomSheet.tsx implementation
8
+ *
9
+ * SYNC: When BottomSheet.tsx changes, update tests to match new behavior
10
+ */
11
+
12
+ import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';
13
+ import {act, render, screen, fireEvent} from '@testing-library/react';
14
+ import {createRef, useState} from 'react';
15
+ import {BottomSheet} from './BottomSheet';
16
+ import {BottomSheetSwitcher} from './BottomSheetSwitcher';
17
+
18
+ // A sheet has no stops unless its host asks for them, so the tests that
19
+ // exercise detents pass their own. These three — a 14% peek, a half-height
20
+ // stop, and a 92% stop the Tall budget already covers — are the geometry the
21
+ // height assertions below are written against.
22
+ const SNAP_POINTS: ReadonlyArray<number> = [0.14, 0.5, 0.92];
23
+
24
+ // jsdom doesn't implement <dialog> open/close or pointer capture; stub them.
25
+ beforeEach(() => {
26
+ HTMLDialogElement.prototype.showModal = vi.fn(function (
27
+ this: HTMLDialogElement,
28
+ ) {
29
+ this.setAttribute('open', '');
30
+ });
31
+ HTMLDialogElement.prototype.show = vi.fn(function (this: HTMLDialogElement) {
32
+ this.setAttribute('open', '');
33
+ });
34
+ HTMLDialogElement.prototype.close = vi.fn(function (this: HTMLDialogElement) {
35
+ this.removeAttribute('open');
36
+ });
37
+ if (!Element.prototype.setPointerCapture) {
38
+ Element.prototype.setPointerCapture = vi.fn();
39
+ Element.prototype.releasePointerCapture = vi.fn();
40
+ }
41
+
42
+ vi.stubGlobal(
43
+ 'matchMedia',
44
+ vi.fn().mockReturnValue({
45
+ matches: false,
46
+ media: '',
47
+ onchange: null,
48
+ addEventListener: vi.fn(),
49
+ removeEventListener: vi.fn(),
50
+ addListener: vi.fn(),
51
+ removeListener: vi.fn(),
52
+ dispatchEvent: vi.fn(),
53
+ }),
54
+ );
55
+ vi.stubGlobal(
56
+ 'requestAnimationFrame',
57
+ vi.fn((callback: FrameRequestCallback) => {
58
+ callback(0);
59
+ return 1;
60
+ }),
61
+ );
62
+ vi.stubGlobal('cancelAnimationFrame', vi.fn());
63
+ window.scrollTo = vi.fn();
64
+ });
65
+
66
+ afterEach(() => {
67
+ vi.unstubAllGlobals();
68
+ });
69
+
70
+ function getSheet(): HTMLElement {
71
+ const sheet = document.querySelector<HTMLElement>('.astryx-bottom-sheet');
72
+ if (!sheet) {
73
+ throw new Error('sheet panel not found');
74
+ }
75
+ return sheet;
76
+ }
77
+
78
+ function getBody(): HTMLElement {
79
+ const body = getSheet().lastElementChild;
80
+ if (!(body instanceof HTMLElement)) {
81
+ throw new Error('sheet scroll body not found');
82
+ }
83
+ return body;
84
+ }
85
+
86
+ function getPositioner(): HTMLElement {
87
+ const positioner = getSheet().parentElement;
88
+ if (!(positioner instanceof HTMLElement)) {
89
+ throw new Error('sheet positioner not found');
90
+ }
91
+ return positioner;
92
+ }
93
+
94
+ function rect({top, bottom}: {top: number; bottom: number}): DOMRect {
95
+ return {
96
+ x: 0,
97
+ y: top,
98
+ top,
99
+ right: 400,
100
+ bottom,
101
+ left: 0,
102
+ width: 400,
103
+ height: bottom - top,
104
+ toJSON: () => ({}),
105
+ };
106
+ }
107
+
108
+ function resizeEntry(
109
+ borderBoxHeight: number,
110
+ contentBoxHeight = borderBoxHeight,
111
+ ): ResizeObserverEntry {
112
+ // Core typechecks its tests, so this partial stands in for the full entry
113
+ // the way sharedResizeObserver does: only the fields the sheet reads.
114
+ const entry: Partial<ResizeObserverEntry> = {
115
+ borderBoxSize: [{blockSize: borderBoxHeight, inlineSize: 100}],
116
+ contentRect: rect({top: 0, bottom: contentBoxHeight}),
117
+ };
118
+ return entry as ResizeObserverEntry;
119
+ }
120
+
121
+ function mockVisualViewport(height: number, offsetTop = 0) {
122
+ const viewport = Object.assign(new EventTarget(), {
123
+ height,
124
+ offsetTop,
125
+ });
126
+ vi.stubGlobal('visualViewport', viewport);
127
+ return viewport;
128
+ }
129
+
130
+ // The layout viewport — `100dvh`, `window.innerHeight` — which is what the
131
+ // sheet's height budget and its detents are measured against. Deliberately
132
+ // separate from the visual viewport above: the mobile keyboard shrinks that
133
+ // one and leaves this one alone, and the sheet has to tell them apart.
134
+ function mockWindowHeight(height: number) {
135
+ vi.stubGlobal('innerHeight', height);
136
+ }
137
+
138
+ function mockIOSWebKit() {
139
+ const navigatorMock = Object.create(window.navigator);
140
+ Object.defineProperties(navigatorMock, {
141
+ userAgent: {
142
+ configurable: true,
143
+ value:
144
+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15',
145
+ },
146
+ maxTouchPoints: {configurable: true, value: 5},
147
+ });
148
+ vi.stubGlobal('navigator', navigatorMock);
149
+ }
150
+
151
+ interface ResizeObserverRecord {
152
+ callback: ResizeObserverCallback;
153
+ observed: Set<Element>;
154
+ }
155
+
156
+ function mockResizeObserverInstances(): ResizeObserverRecord[] {
157
+ const observers: ResizeObserverRecord[] = [];
158
+ class ResizeObserverMock {
159
+ callback: ResizeObserverCallback;
160
+ observed = new Set<Element>();
161
+
162
+ constructor(callback: ResizeObserverCallback) {
163
+ this.callback = callback;
164
+ observers.push(this);
165
+ }
166
+
167
+ observe(target: Element) {
168
+ this.observed.add(target);
169
+ }
170
+
171
+ unobserve(target: Element) {
172
+ this.observed.delete(target);
173
+ }
174
+
175
+ disconnect() {
176
+ this.observed.clear();
177
+ }
178
+ }
179
+ vi.stubGlobal('ResizeObserver', ResizeObserverMock);
180
+ return observers;
181
+ }
182
+
183
+ function finishSheetExit() {
184
+ fireEvent.transitionEnd(getSheet(), {propertyName: 'transform'});
185
+ }
186
+
187
+ function ExitHarness({
188
+ hasScrim,
189
+ snapPoints,
190
+ }: {
191
+ hasScrim?: boolean;
192
+ snapPoints?: ReadonlyArray<number>;
193
+ }) {
194
+ const [isOpen, setIsOpen] = useState(true);
195
+ return (
196
+ <BottomSheet
197
+ isOpen={isOpen}
198
+ onOpenChange={setIsOpen}
199
+ label="Filters"
200
+ snapPoints={snapPoints}
201
+ hasScrim={hasScrim}>
202
+ <button type="button" onClick={() => setIsOpen(false)}>
203
+ Close sheet
204
+ </button>
205
+ </BottomSheet>
206
+ );
207
+ }
208
+
209
+ // The grab handle is the panel's first child (decorative, aria-hidden).
210
+ function getHandle(): HTMLElement {
211
+ const handle = getSheet().querySelector<HTMLElement>('[aria-hidden="true"]');
212
+ if (!handle) {
213
+ throw new Error('grab handle not found');
214
+ }
215
+ return handle;
216
+ }
217
+
218
+ // Drive a pointer drag on the grab handle. jsdom PointerEvents don't carry
219
+ // clientY, so dispatch plain events with the coords the handlers read.
220
+ function drag(handle: HTMLElement, points: {y: number}[]) {
221
+ const [down, ...rest] = points;
222
+ fireEvent.pointerDown(handle, {
223
+ pointerId: 1,
224
+ clientY: down.y,
225
+ button: 0,
226
+ isPrimary: true,
227
+ });
228
+ for (const p of rest) {
229
+ fireEvent.pointerMove(handle, {pointerId: 1, clientY: p.y});
230
+ }
231
+ const last = points[points.length - 1];
232
+ fireEvent.pointerUp(handle, {pointerId: 1, clientY: last.y});
233
+ }
234
+
235
+ function fireTimedPointer(
236
+ target: Element,
237
+ type: 'pointerdown' | 'pointermove' | 'pointerup',
238
+ {time, y}: {time: number; y: number},
239
+ ) {
240
+ const event = new Event(type, {bubbles: true, cancelable: true});
241
+ Object.defineProperties(event, {
242
+ button: {value: 0},
243
+ clientY: {value: y},
244
+ isPrimary: {value: true},
245
+ pointerId: {value: 1},
246
+ timeStamp: {value: time},
247
+ });
248
+ return fireEvent(target, event);
249
+ }
250
+
251
+ describe('BottomSheet', () => {
252
+ it('renders children when open and applies the accessible label', () => {
253
+ render(
254
+ <BottomSheet isOpen onOpenChange={() => {}} label="Filters">
255
+ Sheet content
256
+ </BottomSheet>,
257
+ );
258
+ const dialog = screen.getByRole('dialog');
259
+ expect(dialog).toBeInTheDocument();
260
+ expect(dialog).toHaveAccessibleName('Filters');
261
+ expect(screen.getByText('Sheet content')).toBeInTheDocument();
262
+ });
263
+
264
+ it('forwards DOM props and refs to the visual panel, not the dialog host', () => {
265
+ const panelRef = createRef<HTMLDivElement>();
266
+ const onClick = vi.fn();
267
+ render(
268
+ <BottomSheet
269
+ ref={panelRef}
270
+ isOpen
271
+ onOpenChange={() => {}}
272
+ label="Filters"
273
+ data-testid="filters-panel"
274
+ data-sheet-owner="search"
275
+ className="custom-panel"
276
+ onClick={onClick}>
277
+ Content
278
+ </BottomSheet>,
279
+ );
280
+
281
+ const panel = screen.getByTestId('filters-panel');
282
+ const dialog = screen.getByRole('dialog');
283
+ expect(panelRef.current).toBe(panel);
284
+ expect(panel.tagName).toBe('DIV');
285
+ expect(panel).toHaveClass('astryx-bottom-sheet', 'custom-panel');
286
+ expect(panel).toHaveAttribute('data-sheet-owner', 'search');
287
+ expect(dialog).not.toHaveAttribute('data-testid');
288
+
289
+ fireEvent.click(panel);
290
+ expect(onClick).toHaveBeenCalledTimes(1);
291
+ });
292
+
293
+ it('keeps consumer content as the last scroll-body child', () => {
294
+ render(
295
+ <BottomSheet isOpen onOpenChange={() => {}} label="Filters">
296
+ <div data-testid="consumer-content">Sheet content</div>
297
+ </BottomSheet>,
298
+ );
299
+ const body = getBody();
300
+ const consumer = screen.getByTestId('consumer-content');
301
+
302
+ expect(body.children).toHaveLength(1);
303
+ expect(body.lastElementChild).toBe(consumer);
304
+ expect(consumer.matches(':last-child')).toBe(true);
305
+ });
306
+
307
+ it('does not show when isOpen is false', () => {
308
+ render(
309
+ <BottomSheet isOpen={false} onOpenChange={() => {}} label="Filters">
310
+ Hidden
311
+ </BottomSheet>,
312
+ );
313
+ expect(HTMLDialogElement.prototype.showModal).not.toHaveBeenCalled();
314
+ });
315
+
316
+ it('opens modally (showModal + aria-modal)', () => {
317
+ render(
318
+ <BottomSheet isOpen onOpenChange={() => {}} label="Filters">
319
+ Content
320
+ </BottomSheet>,
321
+ );
322
+ expect(HTMLDialogElement.prototype.showModal).toHaveBeenCalled();
323
+ expect(screen.getByRole('dialog')).toHaveAttribute('aria-modal', 'true');
324
+ });
325
+
326
+ it('requests close on Escape', () => {
327
+ const onOpenChange = vi.fn();
328
+ render(
329
+ <BottomSheet isOpen onOpenChange={onOpenChange} label="Filters">
330
+ Content
331
+ </BottomSheet>,
332
+ );
333
+ fireEvent.keyDown(screen.getByRole('dialog'), {key: 'Escape'});
334
+ expect(onOpenChange).toHaveBeenCalledWith(false);
335
+ });
336
+
337
+ it('requests close when the scrim (dialog element itself) is clicked', () => {
338
+ const onOpenChange = vi.fn();
339
+ render(
340
+ <BottomSheet isOpen onOpenChange={onOpenChange} label="Filters">
341
+ Content
342
+ </BottomSheet>,
343
+ );
344
+ fireEvent.click(screen.getByRole('dialog'));
345
+ expect(onOpenChange).toHaveBeenCalledWith(false);
346
+ });
347
+
348
+ it('purpose=form blocks scrim and swipe dismissal but allows Escape', () => {
349
+ const onOpenChange = vi.fn();
350
+ render(
351
+ <BottomSheet
352
+ isOpen
353
+ purpose="form"
354
+ onOpenChange={onOpenChange}
355
+ label="Edit profile">
356
+ Content
357
+ </BottomSheet>,
358
+ );
359
+ const dialog = screen.getByRole('dialog');
360
+
361
+ fireEvent.click(dialog);
362
+ drag(getHandle(), [{y: 0}, {y: 40}, {y: 120}]);
363
+
364
+ expect(onOpenChange).not.toHaveBeenCalled();
365
+ expect(dialog).toHaveStyle({'--_sheet-scrim-opacity': '1'});
366
+
367
+ fireEvent.keyDown(dialog, {key: 'Escape'});
368
+ fireEvent(dialog, new Event('cancel', {cancelable: true}));
369
+
370
+ expect(onOpenChange).toHaveBeenCalledTimes(2);
371
+ expect(onOpenChange).toHaveBeenNthCalledWith(1, false);
372
+ expect(onOpenChange).toHaveBeenNthCalledWith(2, false);
373
+ });
374
+
375
+ it('purpose=required blocks every implicit dismissal path', () => {
376
+ const onOpenChange = vi.fn();
377
+ render(
378
+ <BottomSheet
379
+ isOpen
380
+ purpose="required"
381
+ onOpenChange={onOpenChange}
382
+ label="Required action">
383
+ Content
384
+ </BottomSheet>,
385
+ );
386
+ const dialog = screen.getByRole('alertdialog');
387
+
388
+ fireEvent.click(dialog);
389
+ fireEvent.keyDown(dialog, {key: 'Escape'});
390
+ fireEvent(dialog, new Event('cancel', {cancelable: true}));
391
+ drag(getHandle(), [{y: 0}, {y: 40}, {y: 120}]);
392
+
393
+ expect(onOpenChange).not.toHaveBeenCalled();
394
+ expect(dialog).toHaveStyle({'--_sheet-scrim-opacity': '1'});
395
+ });
396
+
397
+ it('keeps a standalone modal sheet presented through its exit animation', () => {
398
+ render(<ExitHarness />);
399
+ const dialog = screen.getByRole('dialog', {name: 'Filters'});
400
+
401
+ fireEvent.click(screen.getByRole('button', {name: 'Close sheet'}));
402
+
403
+ expect(dialog).toHaveAttribute('open');
404
+ expect(dialog).toHaveAttribute('inert');
405
+ expect(dialog).toHaveAttribute('aria-hidden', 'true');
406
+ expect(dialog).not.toHaveAttribute('aria-modal');
407
+ expect(dialog).toHaveStyle({'--_sheet-scrim-opacity': '0'});
408
+ expect(document.body.style.position).toBe('fixed');
409
+
410
+ finishSheetExit();
411
+
412
+ expect(dialog).not.toHaveAttribute('open');
413
+ expect(document.body.style.position).not.toBe('fixed');
414
+ });
415
+
416
+ it('does not dismiss when the sheet surface itself is clicked', () => {
417
+ const onOpenChange = vi.fn();
418
+ render(
419
+ <BottomSheet isOpen onOpenChange={onOpenChange} label="Filters">
420
+ Content
421
+ </BottomSheet>,
422
+ );
423
+ // Only a click that lands on the dialog (the transparent area) dismisses;
424
+ // clicks bubbling up from the sheet must not.
425
+ fireEvent.click(screen.getByText('Content'));
426
+ expect(onOpenChange).not.toHaveBeenCalled();
427
+ });
428
+
429
+ describe('hasScrim={false} (non-modal)', () => {
430
+ it('opens non-modally: show() instead of showModal(), no aria-modal', () => {
431
+ render(
432
+ <BottomSheet
433
+ isOpen
434
+ onOpenChange={() => {}}
435
+ label="Filters"
436
+ hasScrim={false}>
437
+ Content
438
+ </BottomSheet>,
439
+ );
440
+ expect(HTMLDialogElement.prototype.show).toHaveBeenCalled();
441
+ expect(HTMLDialogElement.prototype.showModal).not.toHaveBeenCalled();
442
+ expect(screen.getByRole('dialog')).not.toHaveAttribute('aria-modal');
443
+ });
444
+
445
+ it('does not dismiss when the shell (dialog element itself) is clicked', () => {
446
+ // No scrim: a tap on the transparent shell must pass through to the page,
447
+ // not dismiss the sheet.
448
+ const onOpenChange = vi.fn();
449
+ render(
450
+ <BottomSheet
451
+ isOpen
452
+ onOpenChange={onOpenChange}
453
+ label="Filters"
454
+ hasScrim={false}>
455
+ Content
456
+ </BottomSheet>,
457
+ );
458
+ fireEvent.click(screen.getByRole('dialog'));
459
+ expect(onOpenChange).not.toHaveBeenCalled();
460
+ });
461
+
462
+ it('still closes on Escape while focus is inside', () => {
463
+ const onOpenChange = vi.fn();
464
+ render(
465
+ <BottomSheet
466
+ isOpen
467
+ onOpenChange={onOpenChange}
468
+ label="Filters"
469
+ hasScrim={false}>
470
+ Content
471
+ </BottomSheet>,
472
+ );
473
+ fireEvent.keyDown(screen.getByRole('dialog'), {key: 'Escape'});
474
+ expect(onOpenChange).toHaveBeenCalledWith(false);
475
+ });
476
+
477
+ it('still dismisses on a downward swipe past the threshold', () => {
478
+ const onOpenChange = vi.fn();
479
+ render(
480
+ <BottomSheet
481
+ isOpen
482
+ onOpenChange={onOpenChange}
483
+ label="Filters"
484
+ hasScrim={false}>
485
+ Content
486
+ </BottomSheet>,
487
+ );
488
+ drag(getHandle(), [{y: 0}, {y: 40}, {y: 120}]);
489
+ expect(onOpenChange).toHaveBeenCalledWith(false);
490
+ });
491
+
492
+ it('does not steal focus onto the panel on open', () => {
493
+ render(
494
+ <BottomSheet
495
+ isOpen
496
+ onOpenChange={() => {}}
497
+ label="Filters"
498
+ hasScrim={false}>
499
+ <button type="button">First action</button>
500
+ </BottomSheet>,
501
+ );
502
+ // The background stays interactive, so the sheet must not grab focus.
503
+ expect(document.activeElement).not.toBe(getSheet());
504
+ });
505
+
506
+ it('still honors a descendant with data-autofocus', () => {
507
+ render(
508
+ <BottomSheet
509
+ isOpen
510
+ onOpenChange={() => {}}
511
+ label="Filters"
512
+ hasScrim={false}>
513
+ <input data-autofocus aria-label="Search" />
514
+ </BottomSheet>,
515
+ );
516
+ expect(document.activeElement).toBe(
517
+ screen.getByRole('textbox', {name: 'Search'}),
518
+ );
519
+ });
520
+
521
+ it('keeps a standalone non-modal sheet visible until its exit ends', () => {
522
+ render(<ExitHarness hasScrim={false} />);
523
+ const dialog = screen.getByRole('dialog', {name: 'Filters'});
524
+
525
+ fireEvent.click(screen.getByRole('button', {name: 'Close sheet'}));
526
+
527
+ expect(dialog).toHaveAttribute('open');
528
+ expect(dialog).toHaveAttribute('inert');
529
+ expect(document.body.style.position).not.toBe('fixed');
530
+
531
+ finishSheetExit();
532
+
533
+ expect(dialog).not.toHaveAttribute('open');
534
+ });
535
+ });
536
+
537
+ describe('grab handle', () => {
538
+ it('renders a decorative handle hidden from assistive tech', () => {
539
+ render(
540
+ <BottomSheet isOpen onOpenChange={() => {}} label="Filters">
541
+ Content
542
+ </BottomSheet>,
543
+ );
544
+ const handle = getHandle();
545
+ expect(handle).toHaveAttribute('aria-hidden', 'true');
546
+ });
547
+ });
548
+
549
+ describe('swipe to dismiss', () => {
550
+ it('requests close when dragged past the dismiss threshold', () => {
551
+ const onOpenChange = vi.fn();
552
+ render(
553
+ <BottomSheet isOpen onOpenChange={onOpenChange} label="Filters">
554
+ Content
555
+ </BottomSheet>,
556
+ );
557
+ // No measured height in jsdom (0) -> any downward drag dismisses via
558
+ // the distance branch (offset > 0.25) once released downward.
559
+ drag(getHandle(), [{y: 0}, {y: 40}, {y: 120}]);
560
+ expect(onOpenChange).toHaveBeenCalledWith(false);
561
+ });
562
+
563
+ it('restores the sheet when a context menu interrupts an active drag', () => {
564
+ const onOpenChange = vi.fn();
565
+ render(
566
+ <BottomSheet isOpen onOpenChange={onOpenChange} label="Filters">
567
+ Content
568
+ </BottomSheet>,
569
+ );
570
+ const handle = getHandle();
571
+ const dialog = screen.getByRole('dialog');
572
+
573
+ fireEvent.pointerDown(handle, {
574
+ pointerId: 1,
575
+ clientY: 0,
576
+ button: 0,
577
+ isPrimary: true,
578
+ });
579
+ fireEvent.pointerMove(handle, {pointerId: 1, clientY: 300});
580
+ expect(getSheet().style.transform).toBe('translateY(300px)');
581
+
582
+ expect(fireEvent.contextMenu(handle)).toBe(false);
583
+
584
+ expect(getSheet().style.transform).toBe('');
585
+ expect(dialog).toHaveStyle({'--_sheet-scrim-opacity': '1'});
586
+ expect(onOpenChange).not.toHaveBeenCalled();
587
+ });
588
+ });
589
+
590
+ describe('height', () => {
591
+ it('renders for each named height without error', () => {
592
+ for (const height of ['hug', 'capped', 'tall'] as const) {
593
+ const {unmount} = render(
594
+ <BottomSheet
595
+ isOpen
596
+ onOpenChange={() => {}}
597
+ label="Filters"
598
+ height={height}>
599
+ Content
600
+ </BottomSheet>,
601
+ );
602
+ expect(screen.getByRole('dialog')).toBeInTheDocument();
603
+ unmount();
604
+ }
605
+ });
606
+
607
+ it('accepts a freeform height (number or CSS length)', () => {
608
+ for (const height of [480, '70dvh'] as const) {
609
+ const {unmount} = render(
610
+ <BottomSheet
611
+ isOpen
612
+ onOpenChange={() => {}}
613
+ label="Filters"
614
+ height={height}>
615
+ Content
616
+ </BottomSheet>,
617
+ );
618
+ expect(screen.getByRole('dialog')).toBeInTheDocument();
619
+ unmount();
620
+ }
621
+ });
622
+
623
+ it('keeps the full layout height at the peek detent', () => {
624
+ const observers = mockResizeObserverInstances();
625
+ mockVisualViewport(800);
626
+ mockWindowHeight(800);
627
+ render(
628
+ <BottomSheet
629
+ isOpen
630
+ onOpenChange={() => {}}
631
+ label="Release notes"
632
+ snapPoints={SNAP_POINTS}
633
+ height="tall">
634
+ Content
635
+ </BottomSheet>,
636
+ );
637
+ const sheet = getSheet();
638
+ const sheetObserver = observers.find(instance =>
639
+ instance.observed.has(sheet),
640
+ );
641
+ act(() => {
642
+ sheetObserver?.callback(
643
+ [resizeEntry(784, 736)],
644
+ sheetObserver as unknown as ResizeObserver,
645
+ );
646
+ });
647
+
648
+ // Settle on the shortest 112px peek: offset 736 - 112 = 624.
649
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 0, y: 0});
650
+ fireTimedPointer(getHandle(), 'pointermove', {time: 1000, y: 600});
651
+ fireTimedPointer(getHandle(), 'pointerup', {time: 2000, y: 600});
652
+ fireEvent.transitionEnd(sheet, {propertyName: 'transform'});
653
+
654
+ // A glance state does not reflow the content into a sliver: the sheet
655
+ // keeps its full layout height and slides below the viewport instead.
656
+ expect(sheet.style.height).toBe('');
657
+ expect(sheet.style.transform).toBe('translateY(624px)');
658
+ expect(getBody().style.paddingBlockEnd).toBe('');
659
+
660
+ // Dragging back up off the peek stays transform-only for the same
661
+ // reason — there is no shortened layout to restore first.
662
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 3000, y: 600});
663
+ fireTimedPointer(getHandle(), 'pointermove', {time: 4000, y: 400});
664
+ expect(sheet.style.transform).toBe('translateY(424px)');
665
+ expect(sheet.style.height).toBe('784px');
666
+ expect(getBody().style.paddingBlockEnd).toBe('');
667
+
668
+ // Releasing at the taller p50 detent resizes the scrolling area.
669
+ fireTimedPointer(getHandle(), 'pointerup', {time: 5000, y: 400});
670
+ fireEvent.transitionEnd(sheet, {propertyName: 'transform'});
671
+ expect(sheet.style.height).toBe('448px');
672
+ expect(sheet.style.transform).toBe('');
673
+ });
674
+
675
+ it('swaps height for transform without a transition when released on a detent', () => {
676
+ const observers = mockResizeObserverInstances();
677
+ mockVisualViewport(800);
678
+ mockWindowHeight(800);
679
+ render(
680
+ <BottomSheet
681
+ isOpen
682
+ onOpenChange={() => {}}
683
+ label="Release notes"
684
+ snapPoints={SNAP_POINTS}
685
+ height="tall">
686
+ Content
687
+ </BottomSheet>,
688
+ );
689
+ const sheet = getSheet();
690
+ const sheetObserver = observers.find(instance =>
691
+ instance.observed.has(sheet),
692
+ );
693
+ act(() => {
694
+ sheetObserver?.callback(
695
+ [resizeEntry(784, 736)],
696
+ sheetObserver as unknown as ResizeObserver,
697
+ );
698
+ });
699
+
700
+ // magnetize() lands a slow drag exactly on the p50 detent (offset 336),
701
+ // so the release has no travel left to animate and reconciles at once.
702
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 0, y: 0});
703
+ fireTimedPointer(getHandle(), 'pointermove', {time: 1000, y: 336});
704
+ expect(sheet.style.transform).toBe('translateY(336px)');
705
+ expect(sheet.style.height).toBe('784px');
706
+
707
+ // Hold the reconciliation frame so the intermediate render is visible
708
+ // to the assertions below.
709
+ const reconciliationFrames: FrameRequestCallback[] = [];
710
+ vi.mocked(requestAnimationFrame).mockImplementation(callback => {
711
+ reconciliationFrames.push(callback);
712
+ return reconciliationFrames.length;
713
+ });
714
+ fireTimedPointer(getHandle(), 'pointerup', {time: 2000, y: 336});
715
+
716
+ // Height and transform swap roles in this one render. That is only
717
+ // invisible with transitions off; live, the composited transform would
718
+ // animate the whole 336px swap while the height jumped, and the sheet
719
+ // would lurch away from the detent before coming back to it.
720
+ expect(sheet.style.height).toBe('448px');
721
+ expect(sheet.style.transform).toBe('');
722
+ expect(sheet.style.transition).toBe('none');
723
+
724
+ // Transitions come back for the next gesture.
725
+ act(() => reconciliationFrames.splice(0).forEach(frame => frame(0)));
726
+ expect(sheet.style.transition).toBe('');
727
+ });
728
+
729
+ it('uses transforms while dragging and resizes to the visible snapped height', () => {
730
+ const observers = mockResizeObserverInstances();
731
+ mockVisualViewport(800);
732
+ mockWindowHeight(800);
733
+ render(
734
+ <BottomSheet
735
+ isOpen
736
+ onOpenChange={() => {}}
737
+ label="Release notes"
738
+ snapPoints={SNAP_POINTS}
739
+ height="tall">
740
+ Content
741
+ </BottomSheet>,
742
+ );
743
+ const sheet = getSheet();
744
+ const sheetObserver = observers.find(instance =>
745
+ instance.observed.has(sheet),
746
+ );
747
+ act(() => {
748
+ sheetObserver?.callback(
749
+ // A Tall sheet has 736px visible height in this 800px viewport plus
750
+ // the 48px border-box reserve held below the viewport.
751
+ [resizeEntry(784, 736)],
752
+ sheetObserver as unknown as ResizeObserver,
753
+ );
754
+ });
755
+
756
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 0, y: 0});
757
+ fireTimedPointer(getHandle(), 'pointermove', {time: 1000, y: 240});
758
+
759
+ expect(sheet.style.transform).toBe('translateY(240px)');
760
+ expect(sheet.style.height).toBe('784px');
761
+ expect(sheet.style.transition).toBe('none');
762
+
763
+ fireTimedPointer(getHandle(), 'pointerup', {time: 2000, y: 240});
764
+ // Release remains transform-only until the snap finishes.
765
+ // p50 is a visible 400px sheet: 784 - 48 - 400 = 336px offset.
766
+ expect(sheet.style.transform).toBe('translateY(336px)');
767
+ expect(sheet.style.height).toBe('784px');
768
+ expect(sheet.style.transition).toBe('');
769
+ const reconciliationFrames: FrameRequestCallback[] = [];
770
+ vi.mocked(requestAnimationFrame).mockImplementation(callback => {
771
+ reconciliationFrames.push(callback);
772
+ return reconciliationFrames.length;
773
+ });
774
+ fireEvent.transitionEnd(sheet, {propertyName: 'transform'});
775
+ expect(sheet.style.transform).toBe('');
776
+ expect(sheet.style.height).toBe('448px');
777
+ // The transform reset is transition-free so it cannot produce a second
778
+ // fly-in animation after the snap has already reached its destination.
779
+ expect(sheet.style.transition).toBe('none');
780
+ expect(reconciliationFrames.length).toBeGreaterThan(0);
781
+ act(() => reconciliationFrames.splice(0).forEach(frame => frame(0)));
782
+ expect(sheet.style.transition).toBe('');
783
+
784
+ // Ignore ResizeObserver frames from the height reconciliation. The
785
+ // next drag must still use the original 784px border-box height.
786
+ act(() => {
787
+ sheetObserver?.callback(
788
+ [resizeEntry(500, 452)],
789
+ sheetObserver as unknown as ResizeObserver,
790
+ );
791
+ });
792
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 3000, y: 240});
793
+ fireTimedPointer(getHandle(), 'pointermove', {time: 4000, y: 140});
794
+ expect(sheet.style.transform).toBe('translateY(236px)');
795
+ expect(sheet.style.height).toBe('784px');
796
+ expect(getBody().style.paddingBlockEnd).toBe('336px');
797
+
798
+ // Reversing below the settled point restores the settled height and
799
+ // translates only the distance traveled from that detent. The temporary
800
+ // scroll-preservation inset leaves with the temporary expanded layout.
801
+ fireTimedPointer(getHandle(), 'pointermove', {time: 5000, y: 340});
802
+ expect(sheet.style.transform).toBe('translateY(100px)');
803
+ expect(sheet.style.height).toBe('448px');
804
+ expect(getBody().style.paddingBlockEnd).toBe('');
805
+ });
806
+
807
+ it('reconciles the snapped height immediately when transitions are disabled', () => {
808
+ const observers = mockResizeObserverInstances();
809
+ mockVisualViewport(800);
810
+ mockWindowHeight(800);
811
+ render(
812
+ <BottomSheet
813
+ isOpen
814
+ onOpenChange={() => {}}
815
+ label="Release notes"
816
+ snapPoints={SNAP_POINTS}
817
+ height="tall"
818
+ style={{transition: 'none'}}>
819
+ Content
820
+ </BottomSheet>,
821
+ );
822
+ const sheet = getSheet();
823
+ const sheetObserver = observers.find(instance =>
824
+ instance.observed.has(sheet),
825
+ );
826
+ act(() => {
827
+ sheetObserver?.callback(
828
+ [resizeEntry(784, 736)],
829
+ sheetObserver as unknown as ResizeObserver,
830
+ );
831
+ });
832
+
833
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 0, y: 0});
834
+ fireTimedPointer(getHandle(), 'pointermove', {time: 1000, y: 240});
835
+ fireTimedPointer(getHandle(), 'pointerup', {time: 2000, y: 240});
836
+
837
+ expect(sheet.style.transform).toBe('');
838
+ expect(sheet.style.height).toBe('448px');
839
+ });
840
+
841
+ it('restores the maximum height before an upward drag and reconciles at snap', () => {
842
+ const observers = mockResizeObserverInstances();
843
+ mockVisualViewport(800);
844
+ mockWindowHeight(800);
845
+ render(
846
+ <BottomSheet
847
+ isOpen
848
+ onOpenChange={() => {}}
849
+ label="Release notes"
850
+ snapPoints={SNAP_POINTS}
851
+ height="tall">
852
+ Content
853
+ </BottomSheet>,
854
+ );
855
+ const sheet = getSheet();
856
+ const sheetObserver = observers.find(instance =>
857
+ instance.observed.has(sheet),
858
+ );
859
+ act(() => {
860
+ sheetObserver?.callback(
861
+ [resizeEntry(784, 736)],
862
+ sheetObserver as unknown as ResizeObserver,
863
+ );
864
+ });
865
+
866
+ // Start at the middle 400px detent.
867
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 0, y: 0});
868
+ fireTimedPointer(getHandle(), 'pointermove', {time: 1000, y: 240});
869
+ fireTimedPointer(getHandle(), 'pointerup', {time: 2000, y: 240});
870
+ fireEvent.transitionEnd(sheet, {propertyName: 'transform'});
871
+ expect(sheet.style.height).toBe('448px');
872
+
873
+ // The upward gesture renders the full 784px surface below the viewport
874
+ // and translates it to preserve the visible top edge.
875
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 3000, y: 240});
876
+ fireTimedPointer(getHandle(), 'pointermove', {time: 4000, y: 0});
877
+ expect(sheet.style.transform).toBe('translateY(96px)');
878
+ expect(sheet.style.height).toBe('784px');
879
+ expect(getBody().style.paddingBlockEnd).toBe('336px');
880
+
881
+ // Release first animates only the transform, keeping the source layout
882
+ // and scroll range fixed for the entire snap.
883
+ fireTimedPointer(getHandle(), 'pointerup', {time: 5000, y: 0});
884
+ expect(sheet.style.transform).toBe('');
885
+ expect(sheet.style.height).toBe('784px');
886
+ expect(getBody().style.paddingBlockEnd).toBe('336px');
887
+
888
+ // At transition end, height and preservation spacing reconcile in one
889
+ // render with the same visible geometry.
890
+ fireEvent.transitionEnd(sheet, {propertyName: 'transform'});
891
+ expect(sheet.style.transform).toBe('');
892
+ expect(sheet.style.height).toBe('');
893
+ expect(getBody().style.paddingBlockEnd).toBe('336px');
894
+
895
+ // Once ordinary scrolling brings the content back within its natural
896
+ // range, the retained end padding is no longer needed and is discarded.
897
+ const body = getBody();
898
+ Object.defineProperties(body, {
899
+ clientHeight: {configurable: true, value: 600},
900
+ scrollHeight: {configurable: true, value: 1000},
901
+ scrollTop: {configurable: true, value: 64, writable: true},
902
+ });
903
+ fireEvent.scroll(body);
904
+ expect(body.style.paddingBlockEnd).toBe('');
905
+ });
906
+
907
+ it('keeps the settled height stable while dismissing', () => {
908
+ const observers = mockResizeObserverInstances();
909
+ mockVisualViewport(800);
910
+ mockWindowHeight(800);
911
+ render(<ExitHarness snapPoints={SNAP_POINTS} />);
912
+ const sheet = getSheet();
913
+ const sheetObserver = observers.find(instance =>
914
+ instance.observed.has(sheet),
915
+ );
916
+ act(() => {
917
+ sheetObserver?.callback(
918
+ [resizeEntry(784, 736)],
919
+ sheetObserver as unknown as ResizeObserver,
920
+ );
921
+ });
922
+
923
+ // First settle at the middle detent.
924
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 0, y: 0});
925
+ fireTimedPointer(getHandle(), 'pointermove', {time: 1000, y: 240});
926
+ fireTimedPointer(getHandle(), 'pointerup', {time: 2000, y: 240});
927
+ fireEvent.transitionEnd(sheet, {propertyName: 'transform'});
928
+ expect(sheet.style.height).toBe('448px');
929
+
930
+ // A later dismiss stays transform-only and preserves that settled
931
+ // scroll-area height throughout the exit.
932
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 3000, y: 240});
933
+ fireTimedPointer(getHandle(), 'pointermove', {time: 4000, y: 1000});
934
+ expect(sheet.style.transform).toBe('translateY(760px)');
935
+ expect(sheet.style.height).toBe('448px');
936
+ fireTimedPointer(getHandle(), 'pointerup', {time: 5000, y: 1000});
937
+ expect(sheet.style.height).toBe('448px');
938
+
939
+ act(() => {
940
+ sheetObserver?.callback(
941
+ [resizeEntry(200, 152)],
942
+ sheetObserver as unknown as ResizeObserver,
943
+ );
944
+ });
945
+ expect(sheet.style.height).toBe('448px');
946
+ });
947
+
948
+ it('re-resolves the settled detent when the window resizes', () => {
949
+ const observers = mockResizeObserverInstances();
950
+ mockVisualViewport(800);
951
+ mockWindowHeight(800);
952
+ render(
953
+ <BottomSheet
954
+ isOpen
955
+ onOpenChange={() => {}}
956
+ label="Release notes"
957
+ snapPoints={SNAP_POINTS}
958
+ height="tall">
959
+ Content
960
+ </BottomSheet>,
961
+ );
962
+ const sheet = getSheet();
963
+ const sheetObserver = observers.find(instance =>
964
+ instance.observed.has(sheet),
965
+ );
966
+ act(() => {
967
+ sheetObserver?.callback(
968
+ [resizeEntry(784, 736)],
969
+ sheetObserver as unknown as ResizeObserver,
970
+ );
971
+ });
972
+
973
+ // Settle on the half-height detent: 784 - 48 - 400 = 336px of travel.
974
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 0, y: 0});
975
+ fireTimedPointer(getHandle(), 'pointermove', {time: 1000, y: 336});
976
+ fireTimedPointer(getHandle(), 'pointerup', {time: 2000, y: 336});
977
+ const reconciliationFrames: FrameRequestCallback[] = [];
978
+ vi.mocked(requestAnimationFrame).mockImplementation(callback => {
979
+ reconciliationFrames.push(callback);
980
+ return reconciliationFrames.length;
981
+ });
982
+ fireEvent.transitionEnd(sheet, {propertyName: 'transform'});
983
+ act(() => reconciliationFrames.splice(0).forEach(frame => frame(0)));
984
+ // 448px of layout height shows 400px of sheet: half the 800px window.
985
+ expect(sheet.style.height).toBe('448px');
986
+
987
+ // Shrink the window. The sheet's budget is `92dvh`, so its border box
988
+ // follows: 0.92 * 600 + the 48px reserve = 600px.
989
+ vi.spyOn(sheet, 'getBoundingClientRect').mockImplementation(() =>
990
+ rect({
991
+ top: 0,
992
+ bottom: sheet.style.height
993
+ ? Number.parseFloat(sheet.style.height)
994
+ : 600,
995
+ }),
996
+ );
997
+ mockWindowHeight(600);
998
+ act(() => {
999
+ window.dispatchEvent(new Event('resize'));
1000
+ });
1001
+
1002
+ // Still the half-height detent, re-resolved against the new window:
1003
+ // 348px of layout height shows 300px, half of 600. Before this, the
1004
+ // sheet kept its 448px and showed 400px — three quarters of the window.
1005
+ expect(sheet.style.height).toBe('348px');
1006
+ expect(sheet.style.transform).toBe('');
1007
+ // Re-anchoring is not a gesture, so it does not animate.
1008
+ expect(sheet.style.transition).toBe('none');
1009
+ act(() => reconciliationFrames.splice(0).forEach(frame => frame(0)));
1010
+ expect(sheet.style.transition).toBe('');
1011
+ });
1012
+
1013
+ it('keeps its detents when the keyboard shrinks the visual viewport', () => {
1014
+ const observers = mockResizeObserverInstances();
1015
+ const viewport = mockVisualViewport(800);
1016
+ mockWindowHeight(800);
1017
+ render(
1018
+ <BottomSheet
1019
+ isOpen
1020
+ onOpenChange={() => {}}
1021
+ label="Release notes"
1022
+ snapPoints={SNAP_POINTS}
1023
+ height="tall">
1024
+ Content
1025
+ </BottomSheet>,
1026
+ );
1027
+ const sheet = getSheet();
1028
+ const sheetObserver = observers.find(instance =>
1029
+ instance.observed.has(sheet),
1030
+ );
1031
+ act(() => {
1032
+ sheetObserver?.callback(
1033
+ [resizeEntry(784, 736)],
1034
+ sheetObserver as unknown as ResizeObserver,
1035
+ );
1036
+ });
1037
+
1038
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 0, y: 0});
1039
+ fireTimedPointer(getHandle(), 'pointermove', {time: 1000, y: 336});
1040
+ fireTimedPointer(getHandle(), 'pointerup', {time: 2000, y: 336});
1041
+ fireEvent.transitionEnd(sheet, {propertyName: 'transform'});
1042
+ expect(sheet.style.height).toBe('448px');
1043
+
1044
+ // The keyboard opens: the visual viewport shrinks, the layout viewport
1045
+ // the sheet is measured in does not.
1046
+ act(() => {
1047
+ viewport.height = 500;
1048
+ viewport.dispatchEvent(new Event('resize'));
1049
+ });
1050
+ expect(sheet.style.height).toBe('448px');
1051
+
1052
+ // The next drag still snaps to the window's detents, not to fractions
1053
+ // of the space the keyboard left over.
1054
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 3000, y: 0});
1055
+ fireTimedPointer(getHandle(), 'pointermove', {time: 4000, y: 288});
1056
+ fireTimedPointer(getHandle(), 'pointerup', {time: 5000, y: 288});
1057
+ // The peek of an 800px window: 736 - 112 = 624px of travel.
1058
+ expect(sheet.style.transform).toBe('translateY(624px)');
1059
+ });
1060
+ });
1061
+
1062
+ describe('snapPoints', () => {
1063
+ // A Tall sheet in an 800px window: a 784px border box, 48px of which is
1064
+ // the reserve below the fold, so 736px of it is visible.
1065
+ function renderTallSheet(
1066
+ snapPoints?: ReadonlyArray<number | string>,
1067
+ onOpenChange: (isOpen: boolean) => void = () => {},
1068
+ ) {
1069
+ const observers = mockResizeObserverInstances();
1070
+ mockVisualViewport(800);
1071
+ mockWindowHeight(800);
1072
+ const view = render(
1073
+ <BottomSheet
1074
+ isOpen
1075
+ onOpenChange={onOpenChange}
1076
+ label="Release notes"
1077
+ snapPoints={snapPoints}
1078
+ height="tall">
1079
+ Content
1080
+ </BottomSheet>,
1081
+ );
1082
+ const sheet = getSheet();
1083
+ const sheetObserver = observers.find(instance =>
1084
+ instance.observed.has(sheet),
1085
+ );
1086
+ act(() => {
1087
+ sheetObserver?.callback(
1088
+ [resizeEntry(784, 736)],
1089
+ sheetObserver as unknown as ResizeObserver,
1090
+ );
1091
+ });
1092
+ return {sheet, view};
1093
+ }
1094
+
1095
+ function dragHandleTo(y: number) {
1096
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 0, y: 0});
1097
+ fireTimedPointer(getHandle(), 'pointermove', {time: 1000, y});
1098
+ fireTimedPointer(getHandle(), 'pointerup', {time: 2000, y});
1099
+ fireEvent.transitionEnd(getSheet(), {propertyName: 'transform'});
1100
+ }
1101
+
1102
+ it('has no stops of its own, so a released drag springs back', () => {
1103
+ const onOpenChange = vi.fn();
1104
+ const {sheet} = renderTallSheet(undefined, onOpenChange);
1105
+
1106
+ // 200px down is well short of the dismiss threshold, and there is no
1107
+ // stop to catch it, so the sheet returns to fully open.
1108
+ dragHandleTo(200);
1109
+
1110
+ expect(sheet.style.transform).toBe('');
1111
+ expect(sheet.style.height).toBe('');
1112
+ expect(onOpenChange).not.toHaveBeenCalled();
1113
+ });
1114
+
1115
+ it('rests at a stop given as a fraction of the viewport', () => {
1116
+ const {sheet} = renderTallSheet([0.5]);
1117
+
1118
+ // Half of the 800px window is 400px of visible sheet: 736 - 400 = 336px
1119
+ // of travel, taken as layout height once the snap lands.
1120
+ dragHandleTo(336);
1121
+
1122
+ expect(sheet.style.height).toBe('448px');
1123
+ expect(sheet.style.transform).toBe('');
1124
+ });
1125
+
1126
+ it('reads a percentage as the same stop as the fraction', () => {
1127
+ const {sheet} = renderTallSheet(['50%']);
1128
+ dragHandleTo(336);
1129
+ expect(sheet.style.height).toBe('448px');
1130
+ });
1131
+
1132
+ it('rests at a stop given as an absolute px length', () => {
1133
+ const {sheet} = renderTallSheet(['320px']);
1134
+
1135
+ // A 320px stop sits 736 - 320 = 416px down, whatever the window does.
1136
+ dragHandleTo(416);
1137
+
1138
+ expect(sheet.style.height).toBe('368px');
1139
+ expect(sheet.style.transform).toBe('');
1140
+ });
1141
+
1142
+ it('re-anchors to the same stop when the points change under a resting sheet', () => {
1143
+ const {sheet, view} = renderTallSheet([0.5]);
1144
+ dragHandleTo(336);
1145
+ expect(sheet.style.height).toBe('448px');
1146
+
1147
+ // Without an inline height the sheet renders its natural 92dvh budget.
1148
+ vi.spyOn(sheet, 'getBoundingClientRect').mockImplementation(() =>
1149
+ rect({
1150
+ top: 0,
1151
+ bottom: sheet.style.height
1152
+ ? Number.parseFloat(sheet.style.height)
1153
+ : 784,
1154
+ }),
1155
+ );
1156
+
1157
+ // The host moves its one stop from half the window to a quarter of it.
1158
+ // The sheet is resting on that stop, so it follows — no gesture, and
1159
+ // nothing to animate.
1160
+ view.rerender(
1161
+ <BottomSheet
1162
+ isOpen
1163
+ onOpenChange={() => {}}
1164
+ label="Release notes"
1165
+ snapPoints={[0.25]}
1166
+ height="tall">
1167
+ Content
1168
+ </BottomSheet>,
1169
+ );
1170
+
1171
+ // 200px of visible sheet is 736 - 200 = 536px of travel.
1172
+ expect(sheet.style.height).toBe('248px');
1173
+ expect(sheet.style.transform).toBe('');
1174
+ });
1175
+
1176
+ it('ignores a stop it cannot resolve, and warns which one', () => {
1177
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
1178
+ // 200 is the px mistake: a bare number is a fraction, never a length.
1179
+ const {sheet} = renderTallSheet([0.5, 200]);
1180
+
1181
+ expect(
1182
+ warn.mock.calls.some(args => String(args[0]).includes('200')),
1183
+ ).toBe(true);
1184
+
1185
+ // The stop it could read still works.
1186
+ dragHandleTo(336);
1187
+ expect(sheet.style.height).toBe('448px');
1188
+ warn.mockRestore();
1189
+ });
1190
+ });
1191
+
1192
+ describe('mobile keyboard', () => {
1193
+ it('claims a transition between fields and delivers it with preventScroll', () => {
1194
+ mockIOSWebKit();
1195
+ render(
1196
+ <BottomSheet
1197
+ isOpen
1198
+ onOpenChange={() => {}}
1199
+ label="Add a comment"
1200
+ height="tall">
1201
+ <input aria-label="Title" />
1202
+ <input aria-label="Comment" />
1203
+ </BottomSheet>,
1204
+ );
1205
+ const title = screen.getByRole('textbox', {name: 'Title'});
1206
+ const comment = screen.getByRole('textbox', {name: 'Comment'});
1207
+ title.focus();
1208
+ const focus = vi.spyOn(comment, 'focus');
1209
+
1210
+ // The keyboard's Next, Tab, and a programmatic focus() all arrive as this
1211
+ // one transition, named by relatedTarget on the outgoing blur.
1212
+ fireEvent.blur(title, {relatedTarget: comment});
1213
+
1214
+ // Delivered by us, with the browser's reveal refused.
1215
+ expect(focus).toHaveBeenCalledWith({preventScroll: true});
1216
+ });
1217
+
1218
+ it('does not park focus on a sheet that is closing', () => {
1219
+ mockIOSWebKit();
1220
+ const content = (isOpen: boolean) => (
1221
+ <BottomSheet
1222
+ isOpen={isOpen}
1223
+ onOpenChange={() => {}}
1224
+ label="Add a comment"
1225
+ height="tall">
1226
+ <input aria-label="Comment" />
1227
+ </BottomSheet>
1228
+ );
1229
+ const {rerender} = render(content(true));
1230
+ const sheet = getSheet();
1231
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1232
+ input.focus();
1233
+ const sheetFocus = vi.spyOn(sheet, 'focus');
1234
+
1235
+ // Closing blurs the field as well, and that blur names no destination —
1236
+ // the same shape as Done. There is no next tap to keep claimable here,
1237
+ // and the host is about to hand focus back to whatever opened the sheet.
1238
+ void act(() => rerender(content(false)));
1239
+
1240
+ expect(sheetFocus).not.toHaveBeenCalled();
1241
+ });
1242
+
1243
+ it('autofocuses a field without letting the browser reveal it', () => {
1244
+ mockIOSWebKit();
1245
+ const focus = vi.spyOn(HTMLInputElement.prototype, 'focus');
1246
+ render(
1247
+ <BottomSheet
1248
+ isOpen
1249
+ onOpenChange={() => {}}
1250
+ label="Add a comment"
1251
+ height="tall">
1252
+ <input aria-label="Comment" data-autofocus />
1253
+ </BottomSheet>,
1254
+ );
1255
+ // A prototype spy outlives this test unless it is put back by hand.
1256
+ const calls = [...focus.mock.calls];
1257
+ focus.mockRestore();
1258
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1259
+
1260
+ // Opening focuses the field itself, so there is no transition to claim —
1261
+ // nothing was focused to blur. The presenting call has to refuse the
1262
+ // reveal on its own, or the browser scrolls the page to show a field the
1263
+ // sheet was about to show anyway.
1264
+ expect(document.activeElement).toBe(input);
1265
+ expect(calls).toContainEqual([{preventScroll: true}]);
1266
+ });
1267
+
1268
+ it('parks focus on the sheet when the keyboard Done button takes it', () => {
1269
+ mockIOSWebKit();
1270
+ render(
1271
+ <BottomSheet
1272
+ isOpen
1273
+ onOpenChange={() => {}}
1274
+ label="Add a comment"
1275
+ height="tall">
1276
+ <input aria-label="Comment" />
1277
+ </BottomSheet>,
1278
+ );
1279
+ const sheet = getSheet();
1280
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1281
+ input.focus();
1282
+ const sheetFocus = vi.spyOn(sheet, 'focus');
1283
+
1284
+ // Done dismisses the keyboard and drops focus on the body. Left there,
1285
+ // the field is still document.activeElement on the next tap, so no
1286
+ // transition fires and the browser reveals it its own way. Parking focus
1287
+ // on the sheet keeps the next tap a transition this hook can claim.
1288
+ fireEvent.blur(input, {relatedTarget: null});
1289
+
1290
+ expect(sheetFocus).toHaveBeenCalledWith({preventScroll: true});
1291
+ });
1292
+
1293
+ it('does not alter ordinary desktop focus when the viewport is unobstructed', () => {
1294
+ mockVisualViewport(800);
1295
+ mockWindowHeight(800);
1296
+ const onFocus = vi.fn();
1297
+ render(
1298
+ <BottomSheet
1299
+ isOpen
1300
+ onOpenChange={() => {}}
1301
+ label="Add a comment"
1302
+ height="tall">
1303
+ <input aria-label="Comment" onFocus={onFocus} />
1304
+ </BottomSheet>,
1305
+ );
1306
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1307
+ const focus = vi.spyOn(input, 'focus');
1308
+
1309
+ input.focus();
1310
+
1311
+ expect(focus).not.toHaveBeenCalledWith({preventScroll: true});
1312
+ expect(onFocus).toHaveBeenCalledTimes(1);
1313
+ expect(getBody().style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1314
+ '0px',
1315
+ );
1316
+ });
1317
+
1318
+ it('keeps Tall geometry fixed while extending and cleaning up its internal scroll range', () => {
1319
+ const viewport = mockVisualViewport(500);
1320
+ render(
1321
+ <BottomSheet
1322
+ isOpen
1323
+ onOpenChange={() => {}}
1324
+ label="Add a comment"
1325
+ height="tall">
1326
+ <input aria-label="Comment" />
1327
+ </BottomSheet>,
1328
+ );
1329
+ const sheet = getSheet();
1330
+ const positioner = getPositioner();
1331
+ const body = getBody();
1332
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1333
+ vi.spyOn(sheet, 'getBoundingClientRect').mockReturnValue(
1334
+ rect({top: 0, bottom: 800}),
1335
+ );
1336
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1337
+ rect({top: 100, bottom: 800}),
1338
+ );
1339
+ vi.spyOn(input, 'getBoundingClientRect').mockImplementation(() =>
1340
+ rect({
1341
+ top: 660 - body.scrollTop,
1342
+ bottom: 700 - body.scrollTop,
1343
+ }),
1344
+ );
1345
+
1346
+ const initialRect = sheet.getBoundingClientRect();
1347
+
1348
+ input.focus();
1349
+
1350
+ expect(sheet.getBoundingClientRect()).toEqual(initialRect);
1351
+ expect(sheet.style.height).toBe('');
1352
+ expect(sheet.style.getPropertyValue('--_sheet-budget')).toBe('92dvh');
1353
+ expect(positioner.style.getPropertyValue('--_sheet-keyboard-lift')).toBe(
1354
+ '',
1355
+ );
1356
+ // 300px keyboard overlap + 48px room for Android suggestion UI.
1357
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1358
+ '348px',
1359
+ );
1360
+ // Visible bottom is 500px; preserve the same 48px focus gap.
1361
+ expect(body.scrollTop).toBe(248);
1362
+
1363
+ viewport.height = 800;
1364
+ void act(() => viewport.dispatchEvent(new Event('resize')));
1365
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1366
+ '0px',
1367
+ );
1368
+ expect(sheet.getBoundingClientRect()).toEqual(initialRect);
1369
+ expect(sheet.style.height).toBe('');
1370
+ });
1371
+
1372
+ it('smoothly scrolls a focused Tall control above the keyboard', () => {
1373
+ mockVisualViewport(500);
1374
+ render(
1375
+ <BottomSheet
1376
+ isOpen
1377
+ onOpenChange={() => {}}
1378
+ label="Add a comment"
1379
+ height="tall">
1380
+ <input aria-label="Comment" />
1381
+ </BottomSheet>,
1382
+ );
1383
+ const body = getBody();
1384
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1385
+ const scrollBy = vi.fn((options: ScrollToOptions) => {
1386
+ body.scrollTop += options.top ?? 0;
1387
+ });
1388
+ Object.defineProperty(body, 'scrollBy', {
1389
+ configurable: true,
1390
+ value: scrollBy,
1391
+ });
1392
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1393
+ rect({top: 100, bottom: 800}),
1394
+ );
1395
+ vi.spyOn(input, 'getBoundingClientRect').mockImplementation(() =>
1396
+ rect({
1397
+ top: 660 - body.scrollTop,
1398
+ bottom: 700 - body.scrollTop,
1399
+ }),
1400
+ );
1401
+
1402
+ input.focus();
1403
+
1404
+ expect(scrollBy).toHaveBeenCalledWith({top: 248, behavior: 'smooth'});
1405
+ });
1406
+
1407
+ it('does not add clearance or scroll when the viewport is unobstructed', () => {
1408
+ mockVisualViewport(800);
1409
+ mockWindowHeight(800);
1410
+ render(
1411
+ <BottomSheet
1412
+ isOpen
1413
+ onOpenChange={() => {}}
1414
+ label="Add a comment"
1415
+ height="tall">
1416
+ <input aria-label="Comment" />
1417
+ </BottomSheet>,
1418
+ );
1419
+ const body = getBody();
1420
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1421
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1422
+ rect({top: 100, bottom: 800}),
1423
+ );
1424
+ vi.spyOn(input, 'getBoundingClientRect').mockReturnValue(
1425
+ rect({top: 760, bottom: 790}),
1426
+ );
1427
+
1428
+ input.focus();
1429
+
1430
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1431
+ '0px',
1432
+ );
1433
+ expect(body.scrollTop).toBe(0);
1434
+ });
1435
+
1436
+ it('does not accommodate the keyboard at a shorter Tall detent', () => {
1437
+ mockIOSWebKit();
1438
+ const observers = mockResizeObserverInstances();
1439
+ const viewport = mockVisualViewport(800);
1440
+ mockWindowHeight(800);
1441
+ render(
1442
+ <BottomSheet
1443
+ isOpen
1444
+ onOpenChange={() => {}}
1445
+ label="Add a comment"
1446
+ snapPoints={SNAP_POINTS}
1447
+ height="tall">
1448
+ <input aria-label="Comment" />
1449
+ </BottomSheet>,
1450
+ );
1451
+ const sheet = getSheet();
1452
+ const body = getBody();
1453
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1454
+ const sheetObserver = observers.find(instance =>
1455
+ instance.observed.has(sheet),
1456
+ );
1457
+ expect(sheetObserver).toBeDefined();
1458
+ act(() => {
1459
+ sheetObserver?.callback(
1460
+ [resizeEntry(784)],
1461
+ sheetObserver as unknown as ResizeObserver,
1462
+ );
1463
+ });
1464
+
1465
+ fireTimedPointer(getHandle(), 'pointerdown', {time: 0, y: 0});
1466
+ fireTimedPointer(getHandle(), 'pointermove', {time: 1000, y: 240});
1467
+ fireTimedPointer(getHandle(), 'pointerup', {time: 2000, y: 240});
1468
+ fireEvent.transitionEnd(sheet, {propertyName: 'transform'});
1469
+ // Settled at the p50 detent: 400px of visible sheet plus the 48px
1470
+ // border-box reserve held below the viewport.
1471
+ expect(sheet.style.transform).toBe('');
1472
+ expect(sheet.style.height).toBe('448px');
1473
+
1474
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1475
+ rect({top: 500, bottom: 1200}),
1476
+ );
1477
+ vi.spyOn(input, 'getBoundingClientRect').mockReturnValue(
1478
+ rect({top: 700, bottom: 740}),
1479
+ );
1480
+ viewport.height = 500;
1481
+ const focus = vi.spyOn(input, 'focus');
1482
+ // A transition the browser would drive: at a shorter detent the hook
1483
+ // must not claim it, so nothing focuses the field but the caller.
1484
+ fireEvent.blur(sheet, {relatedTarget: input});
1485
+ expect(focus).not.toHaveBeenCalled();
1486
+
1487
+ focus.mockRestore();
1488
+ input.focus();
1489
+ void act(() => viewport.dispatchEvent(new Event('resize')));
1490
+
1491
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1492
+ '0px',
1493
+ );
1494
+ expect(body.scrollTop).toBe(0);
1495
+ // The keyboard moved nothing: the sheet still rests at that detent.
1496
+ expect(sheet.style.transform).toBe('');
1497
+ expect(sheet.style.height).toBe('448px');
1498
+ });
1499
+
1500
+ it.each([
1501
+ ['Hug', 'hug'],
1502
+ ['Capped', 'capped'],
1503
+ ['numeric', 480],
1504
+ ['custom CSS', '70dvh'],
1505
+ ] as const)(
1506
+ 'does not add keyboard behavior to a %s height',
1507
+ (_label, height) => {
1508
+ mockIOSWebKit();
1509
+ const viewport = mockVisualViewport(500);
1510
+ render(
1511
+ <BottomSheet
1512
+ isOpen
1513
+ onOpenChange={() => {}}
1514
+ label="Add a comment"
1515
+ height={height}>
1516
+ <input aria-label="Comment" />
1517
+ </BottomSheet>,
1518
+ );
1519
+ const sheet = getSheet();
1520
+ const positioner = getPositioner();
1521
+ const body = getBody();
1522
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1523
+ const focus = vi.spyOn(input, 'focus');
1524
+ const sheetFocus = vi.spyOn(sheet, 'focus');
1525
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1526
+ rect({top: 400, bottom: 800}),
1527
+ );
1528
+ vi.spyOn(input, 'getBoundingClientRect').mockReturnValue(
1529
+ rect({top: 700, bottom: 740}),
1530
+ );
1531
+ body.scrollTop = 20;
1532
+
1533
+ fireEvent.touchStart(input);
1534
+ // These heights opt out, so a browser-driven transition is left alone.
1535
+ fireEvent.blur(sheet, {relatedTarget: input});
1536
+ fireEvent.pointerDown(input, {pointerId: 1, clientY: 200});
1537
+ body.scrollTop = 120;
1538
+ fireEvent.focus(input, {relatedTarget: null});
1539
+ void act(() => viewport.dispatchEvent(new Event('resize')));
1540
+
1541
+ expect(body.scrollTop).toBe(120);
1542
+ expect(focus).not.toHaveBeenCalled();
1543
+ expect(sheetFocus).not.toHaveBeenCalled();
1544
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe('');
1545
+ expect(
1546
+ positioner.style.getPropertyValue('--_sheet-keyboard-lift'),
1547
+ ).toBe('');
1548
+ expect(sheet.style.height).toBe('');
1549
+ },
1550
+ );
1551
+
1552
+ it.each([
1553
+ ['standalone no-scrim', 'standalone', false],
1554
+ ['modal switcher', 'switcher', true],
1555
+ ['no-scrim switcher', 'switcher', false],
1556
+ ] as const)(
1557
+ 'supports Tall internal keyboard scrolling in a %s presentation',
1558
+ (_label, host, hasScrim) => {
1559
+ mockVisualViewport(500);
1560
+ const content = <input aria-label="Comment" />;
1561
+ const result =
1562
+ host === 'standalone'
1563
+ ? render(
1564
+ <BottomSheet
1565
+ isOpen
1566
+ onOpenChange={() => {}}
1567
+ label="Add a comment"
1568
+ height="tall"
1569
+ hasScrim={hasScrim}>
1570
+ {content}
1571
+ </BottomSheet>,
1572
+ )
1573
+ : render(
1574
+ <BottomSheetSwitcher
1575
+ activeSheet="comment"
1576
+ onActiveSheetChange={() => {}}
1577
+ hasScrim={hasScrim}>
1578
+ <BottomSheet
1579
+ sheetId="comment"
1580
+ label="Add a comment"
1581
+ height="tall">
1582
+ {content}
1583
+ </BottomSheet>
1584
+ </BottomSheetSwitcher>,
1585
+ );
1586
+ const body = getBody();
1587
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1588
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1589
+ rect({top: 100, bottom: 800}),
1590
+ );
1591
+ vi.spyOn(input, 'getBoundingClientRect').mockImplementation(() =>
1592
+ rect({
1593
+ top: 660 - body.scrollTop,
1594
+ bottom: 700 - body.scrollTop,
1595
+ }),
1596
+ );
1597
+
1598
+ input.focus();
1599
+
1600
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1601
+ '348px',
1602
+ );
1603
+ expect(body.scrollTop).toBe(248);
1604
+ result.unmount();
1605
+ },
1606
+ );
1607
+
1608
+ it('re-reveals a focused Tall control when content layout changes', () => {
1609
+ const observers = mockResizeObserverInstances();
1610
+ mockVisualViewport(500);
1611
+ render(
1612
+ <BottomSheet
1613
+ isOpen
1614
+ onOpenChange={() => {}}
1615
+ label="Add a comment"
1616
+ height="tall">
1617
+ <input aria-label="Comment" />
1618
+ </BottomSheet>,
1619
+ );
1620
+ const body = getBody();
1621
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1622
+ let layoutShift = 0;
1623
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1624
+ rect({top: 100, bottom: 800}),
1625
+ );
1626
+ vi.spyOn(input, 'getBoundingClientRect').mockImplementation(() =>
1627
+ rect({
1628
+ top: 660 + layoutShift - body.scrollTop,
1629
+ bottom: 700 + layoutShift - body.scrollTop,
1630
+ }),
1631
+ );
1632
+
1633
+ input.focus();
1634
+ expect(body.scrollTop).toBe(248);
1635
+
1636
+ layoutShift = 200;
1637
+ const observer = observers.find(instance => instance.observed.has(input));
1638
+ expect(observer).toBeDefined();
1639
+ act(() => {
1640
+ observer?.callback([], observer as unknown as ResizeObserver);
1641
+ });
1642
+
1643
+ expect(body.scrollTop).toBe(448);
1644
+ });
1645
+
1646
+ it('retains Tall keyboard scroll space during travel until the viewport recovers', () => {
1647
+ const viewport = mockVisualViewport(500);
1648
+ render(
1649
+ <BottomSheet
1650
+ isOpen
1651
+ onOpenChange={() => {}}
1652
+ label="Add a comment"
1653
+ height="tall">
1654
+ <input aria-label="Comment" />
1655
+ </BottomSheet>,
1656
+ );
1657
+ const sheet = getSheet();
1658
+ const positioner = getPositioner();
1659
+ const body = getBody();
1660
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1661
+ vi.spyOn(sheet, 'getBoundingClientRect').mockReturnValue(
1662
+ rect({top: 0, bottom: 800}),
1663
+ );
1664
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1665
+ rect({top: 100, bottom: 800}),
1666
+ );
1667
+ vi.spyOn(input, 'getBoundingClientRect').mockImplementation(() =>
1668
+ rect({
1669
+ top: 660 - body.scrollTop,
1670
+ bottom: 700 - body.scrollTop,
1671
+ }),
1672
+ );
1673
+
1674
+ input.focus();
1675
+ expect(body.scrollTop).toBe(248);
1676
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1677
+ '348px',
1678
+ );
1679
+
1680
+ const pointerDownAllowed = fireEvent.pointerDown(getHandle(), {
1681
+ pointerId: 1,
1682
+ clientY: 0,
1683
+ button: 0,
1684
+ isPrimary: true,
1685
+ });
1686
+ expect(pointerDownAllowed).toBe(false);
1687
+ expect(document.activeElement).toBe(input);
1688
+
1689
+ fireEvent.pointerMove(getHandle(), {pointerId: 1, clientY: 40});
1690
+
1691
+ expect(document.activeElement).toBe(sheet);
1692
+ expect(positioner.style.getPropertyValue('--_sheet-keyboard-lift')).toBe(
1693
+ '',
1694
+ );
1695
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1696
+ '348px',
1697
+ );
1698
+ expect(body.scrollTop).toBe(248);
1699
+
1700
+ viewport.height = 800;
1701
+ void act(() => viewport.dispatchEvent(new Event('resize')));
1702
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1703
+ '0px',
1704
+ );
1705
+ });
1706
+
1707
+ it('retains Tall keyboard scroll space after blur until the viewport recovers', () => {
1708
+ const viewport = mockVisualViewport(500);
1709
+ render(
1710
+ <BottomSheet
1711
+ isOpen
1712
+ onOpenChange={() => {}}
1713
+ label="Add a comment"
1714
+ height="tall">
1715
+ <input aria-label="Comment" />
1716
+ </BottomSheet>,
1717
+ );
1718
+ const body = getBody();
1719
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1720
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1721
+ rect({top: 100, bottom: 800}),
1722
+ );
1723
+ vi.spyOn(input, 'getBoundingClientRect').mockImplementation(() =>
1724
+ rect({
1725
+ top: 660 - body.scrollTop,
1726
+ bottom: 700 - body.scrollTop,
1727
+ }),
1728
+ );
1729
+ input.focus();
1730
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1731
+ '348px',
1732
+ );
1733
+
1734
+ input.blur();
1735
+
1736
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1737
+ '348px',
1738
+ );
1739
+
1740
+ viewport.height = 800;
1741
+ void act(() => viewport.dispatchEvent(new Event('resize')));
1742
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1743
+ '0px',
1744
+ );
1745
+ });
1746
+
1747
+ it('blurs the focused Tall field and retains its inset until the viewport recovers', () => {
1748
+ const viewport = mockVisualViewport(500);
1749
+ const onOpenChange = vi.fn();
1750
+ const content = (isOpen: boolean) => (
1751
+ <BottomSheet
1752
+ isOpen={isOpen}
1753
+ onOpenChange={onOpenChange}
1754
+ label="Add a comment"
1755
+ height="tall">
1756
+ <input aria-label="Comment" />
1757
+ </BottomSheet>
1758
+ );
1759
+ const {rerender} = render(content(true));
1760
+ const body = getBody();
1761
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1762
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1763
+ rect({top: 100, bottom: 800}),
1764
+ );
1765
+ vi.spyOn(input, 'getBoundingClientRect').mockImplementation(() =>
1766
+ rect({
1767
+ top: 660 - body.scrollTop,
1768
+ bottom: 700 - body.scrollTop,
1769
+ }),
1770
+ );
1771
+ input.focus();
1772
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1773
+ '348px',
1774
+ );
1775
+
1776
+ rerender(content(false));
1777
+
1778
+ expect(document.activeElement).not.toBe(input);
1779
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1780
+ '348px',
1781
+ );
1782
+
1783
+ viewport.height = 800;
1784
+ void act(() => viewport.dispatchEvent(new Event('resize')));
1785
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1786
+ '0px',
1787
+ );
1788
+ });
1789
+
1790
+ it('retains Tall keyboard scroll space through a switcher handoff until the viewport recovers', () => {
1791
+ const viewport = mockVisualViewport(500);
1792
+ const content = (activeSheet: string) => (
1793
+ <BottomSheetSwitcher
1794
+ activeSheet={activeSheet}
1795
+ onActiveSheetChange={() => {}}>
1796
+ <BottomSheet sheetId="comment" label="Add a comment" height="tall">
1797
+ <input aria-label="Comment" />
1798
+ </BottomSheet>
1799
+ <BottomSheet sheetId="confirmation" label="Confirmation">
1800
+ Confirmation
1801
+ </BottomSheet>
1802
+ </BottomSheetSwitcher>
1803
+ );
1804
+ const {rerender} = render(content('comment'));
1805
+ const body = getBody();
1806
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1807
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1808
+ rect({top: 100, bottom: 800}),
1809
+ );
1810
+ vi.spyOn(input, 'getBoundingClientRect').mockImplementation(() =>
1811
+ rect({
1812
+ top: 660 - body.scrollTop,
1813
+ bottom: 700 - body.scrollTop,
1814
+ }),
1815
+ );
1816
+ input.focus();
1817
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1818
+ '348px',
1819
+ );
1820
+
1821
+ rerender(content('confirmation'));
1822
+
1823
+ expect(document.activeElement).not.toBe(input);
1824
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1825
+ '348px',
1826
+ );
1827
+
1828
+ viewport.height = 800;
1829
+ void act(() => viewport.dispatchEvent(new Event('resize')));
1830
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1831
+ '0px',
1832
+ );
1833
+ });
1834
+
1835
+ it('puts back a document scroll the browser makes to reveal a field', () => {
1836
+ mockVisualViewport(377);
1837
+ render(
1838
+ <BottomSheet
1839
+ isOpen
1840
+ onOpenChange={() => {}}
1841
+ label="Add a comment"
1842
+ height="tall">
1843
+ <input aria-label="Comment" />
1844
+ </BottomSheet>,
1845
+ );
1846
+ const body = getBody();
1847
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1848
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1849
+ rect({top: 57, bottom: 714}),
1850
+ );
1851
+ vi.spyOn(input, 'getBoundingClientRect').mockImplementation(() =>
1852
+ rect({top: 600 - body.scrollTop, bottom: 640 - body.scrollTop}),
1853
+ );
1854
+ input.focus();
1855
+ const scrollTo = vi.mocked(window.scrollTo);
1856
+ scrollTo.mockClear();
1857
+ const scrolledBy = body.scrollTop;
1858
+
1859
+ // The page numbers an iPhone 17 produces when the browser reveals a
1860
+ // focused field in a scroll-locked, fixed sheet: it scrolls the DOCUMENT,
1861
+ // and the sheet — fixed — travels with it.
1862
+ Object.defineProperty(window, 'scrollY', {
1863
+ configurable: true,
1864
+ value: 337,
1865
+ });
1866
+ fireEvent.scroll(window);
1867
+
1868
+ expect(scrollTo).toHaveBeenCalledWith(0, 0);
1869
+ // …and the control is still inside the safe area afterwards: the sheet's
1870
+ // own scroller holds it there, so putting the document back does not
1871
+ // hide what the browser was trying to reveal.
1872
+ expect(input.getBoundingClientRect().bottom).toBeLessThanOrEqual(
1873
+ 377 - 48,
1874
+ );
1875
+ expect(body.scrollTop).toBe(scrolledBy);
1876
+ Object.defineProperty(window, 'scrollY', {configurable: true, value: 0});
1877
+ });
1878
+
1879
+ it('leaves the document alone when no keyboard is measured', () => {
1880
+ mockVisualViewport(800);
1881
+ render(
1882
+ <BottomSheet
1883
+ isOpen
1884
+ onOpenChange={() => {}}
1885
+ label="Add a comment"
1886
+ height="tall">
1887
+ <input aria-label="Comment" />
1888
+ </BottomSheet>,
1889
+ );
1890
+ const body = getBody();
1891
+ const input = screen.getByRole('textbox', {name: 'Comment'});
1892
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1893
+ rect({top: 57, bottom: 700}),
1894
+ );
1895
+ vi.spyOn(input, 'getBoundingClientRect').mockReturnValue(
1896
+ rect({top: 600, bottom: 640}),
1897
+ );
1898
+ input.focus();
1899
+ const scrollTo = vi.mocked(window.scrollTo);
1900
+ scrollTo.mockClear();
1901
+
1902
+ // An ordinary page scroll with no keyboard up is the user's, not the
1903
+ // browser's, and a non-modal sheet leaves the page scrollable.
1904
+ Object.defineProperty(window, 'scrollY', {
1905
+ configurable: true,
1906
+ value: 120,
1907
+ });
1908
+ fireEvent.scroll(window);
1909
+
1910
+ expect(scrollTo).not.toHaveBeenCalled();
1911
+ Object.defineProperty(window, 'scrollY', {configurable: true, value: 0});
1912
+ });
1913
+
1914
+ it('delivers a browser-driven transition itself, then reveals the field', () => {
1915
+ mockIOSWebKit();
1916
+ mockVisualViewport(500);
1917
+ render(
1918
+ <BottomSheet
1919
+ isOpen
1920
+ onOpenChange={() => {}}
1921
+ label="Add a comment"
1922
+ height="tall">
1923
+ <input aria-label="Title" />
1924
+ <input aria-label="Comment" />
1925
+ </BottomSheet>,
1926
+ );
1927
+ const body = getBody();
1928
+ const title = screen.getByRole('textbox', {name: 'Title'});
1929
+ const comment = screen.getByRole('textbox', {name: 'Comment'});
1930
+ const scrolls: {
1931
+ top: number;
1932
+ behavior?: ScrollBehavior;
1933
+ focusLanded: boolean;
1934
+ }[] = [];
1935
+ Object.defineProperty(body, 'scrollBy', {
1936
+ configurable: true,
1937
+ value: (options: ScrollToOptions) => {
1938
+ body.scrollTop += options.top ?? 0;
1939
+ scrolls.push({
1940
+ top: options.top ?? 0,
1941
+ behavior: options.behavior,
1942
+ focusLanded: document.activeElement === comment,
1943
+ });
1944
+ },
1945
+ });
1946
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
1947
+ rect({top: 100, bottom: 800}),
1948
+ );
1949
+ vi.spyOn(title, 'getBoundingClientRect').mockImplementation(() =>
1950
+ rect({top: 150 - body.scrollTop, bottom: 190 - body.scrollTop}),
1951
+ );
1952
+ vi.spyOn(comment, 'getBoundingClientRect').mockImplementation(() =>
1953
+ rect({top: 660 - body.scrollTop, bottom: 700 - body.scrollTop}),
1954
+ );
1955
+
1956
+ // The first field opens the keyboard and is already inside the safe
1957
+ // area, so nothing has to move for it.
1958
+ title.focus();
1959
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
1960
+ '348px',
1961
+ );
1962
+ expect(scrolls).toEqual([]);
1963
+
1964
+ // A keyboard accessory "Next" or a Tab arrives as a blur naming the
1965
+ // destination. We deliver that focus with preventScroll — refusing the
1966
+ // browser's reveal — and then reveal the field ourselves, inside the
1967
+ // sheet.
1968
+ fireEvent.blur(title, {relatedTarget: comment});
1969
+
1970
+ expect(document.activeElement).toBe(comment);
1971
+ expect(scrolls).toEqual([
1972
+ {top: 248, behavior: 'smooth', focusLanded: true},
1973
+ ]);
1974
+ expect(body.scrollTop).toBe(248);
1975
+ });
1976
+
1977
+ it('does not scroll a browser-driven transition when the viewport is unobstructed', () => {
1978
+ mockIOSWebKit();
1979
+ mockVisualViewport(800);
1980
+ render(
1981
+ <BottomSheet
1982
+ isOpen
1983
+ onOpenChange={() => {}}
1984
+ label="Add a comment"
1985
+ height="tall">
1986
+ <input aria-label="Title" />
1987
+ <input aria-label="Comment" />
1988
+ </BottomSheet>,
1989
+ );
1990
+ const body = getBody();
1991
+ const title = screen.getByRole('textbox', {name: 'Title'});
1992
+ const comment = screen.getByRole('textbox', {name: 'Comment'});
1993
+ const scrolls: number[] = [];
1994
+ Object.defineProperty(body, 'scrollBy', {
1995
+ configurable: true,
1996
+ // Apply the scroll, as the real scroller would: a reveal that has
1997
+ // already happened must read as no distance left to travel, otherwise
1998
+ // every later reveal looks like a fresh one.
1999
+ value: (options: ScrollToOptions) => {
2000
+ body.scrollTop += options.top ?? 0;
2001
+ scrolls.push(options.top ?? 0);
2002
+ },
2003
+ });
2004
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
2005
+ rect({top: 100, bottom: 800}),
2006
+ );
2007
+ vi.spyOn(title, 'getBoundingClientRect').mockReturnValue(
2008
+ rect({top: 150, bottom: 190}),
2009
+ );
2010
+ // Below the body's visible area, so a reveal has somewhere to travel.
2011
+ vi.spyOn(comment, 'getBoundingClientRect').mockImplementation(() =>
2012
+ rect({top: 860 - body.scrollTop, bottom: 900 - body.scrollTop}),
2013
+ );
2014
+
2015
+ title.focus();
2016
+ scrolls.length = 0;
2017
+ const scrollTo = vi.mocked(window.scrollTo);
2018
+ scrollTo.mockClear();
2019
+ fireEvent.blur(title, {relatedTarget: comment});
2020
+
2021
+ // With no keyboard there is no clearance to leave and nothing to race,
2022
+ // but the control still has to end up visible — brought there once, by
2023
+ // the sheet's own scroller, with the page left alone.
2024
+ expect(scrolls).toEqual([100]);
2025
+ expect(scrollTo).not.toHaveBeenCalled();
2026
+ });
2027
+
2028
+ it('leaves the page alone behind a non-modal sheet', () => {
2029
+ const layoutBottom = window.innerHeight;
2030
+ mockVisualViewport(500);
2031
+ render(
2032
+ <BottomSheet
2033
+ isOpen
2034
+ onOpenChange={() => {}}
2035
+ label="Add a comment"
2036
+ height="tall"
2037
+ hasScrim={false}>
2038
+ <input aria-label="Comment" />
2039
+ </BottomSheet>,
2040
+ );
2041
+ const body = getBody();
2042
+ const input = screen.getByRole('textbox', {name: 'Comment'});
2043
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
2044
+ rect({top: 100, bottom: layoutBottom}),
2045
+ );
2046
+ vi.spyOn(input, 'getBoundingClientRect').mockImplementation(() =>
2047
+ rect({top: 600 - body.scrollTop, bottom: 640 - body.scrollTop}),
2048
+ );
2049
+ input.focus();
2050
+ const scrollTo = vi.mocked(window.scrollTo);
2051
+ scrollTo.mockClear();
2052
+
2053
+ // Without a scrim the page behind stays scrollable, so a document scroll
2054
+ // is the user's. Putting it back would fight them.
2055
+ Object.defineProperty(window, 'scrollY', {
2056
+ configurable: true,
2057
+ value: 200,
2058
+ });
2059
+ fireEvent.scroll(window);
2060
+
2061
+ expect(scrollTo).not.toHaveBeenCalled();
2062
+ Object.defineProperty(window, 'scrollY', {configurable: true, value: 0});
2063
+ });
2064
+
2065
+ it('holds the keyboard scroll range through a pan, on the blur path', () => {
2066
+ // A fully expanded Tall sheet — the only shape this hook runs in — is
2067
+ // pinned to the layout viewport bottom, so the body's bottom IS
2068
+ // innerHeight. Giving it a cushion below that would hide every bug in
2069
+ // this file: the cushion, not the measurement, would keep the overlap
2070
+ // positive under a pan.
2071
+ const layoutBottom = window.innerHeight;
2072
+ const viewport = mockVisualViewport(500);
2073
+ render(
2074
+ <BottomSheet
2075
+ isOpen
2076
+ onOpenChange={() => {}}
2077
+ label="Add a comment"
2078
+ height="tall">
2079
+ <input aria-label="Comment" />
2080
+ </BottomSheet>,
2081
+ );
2082
+ const body = getBody();
2083
+ const input = screen.getByRole('textbox', {name: 'Comment'});
2084
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
2085
+ rect({top: 100, bottom: layoutBottom}),
2086
+ );
2087
+ vi.spyOn(input, 'getBoundingClientRect').mockImplementation(() =>
2088
+ rect({top: 660 - body.scrollTop, bottom: 700 - body.scrollTop}),
2089
+ );
2090
+ input.focus();
2091
+ const inset = body.style.getPropertyValue('--_sheet-keyboard-inset');
2092
+ expect(inset).toBe(`${layoutBottom - (500 - 48)}px`);
2093
+
2094
+ // The browser pans the page up to reveal a field: the same 500px of
2095
+ // visible page, now offset so its bottom edge coincides with the layout
2096
+ // viewport bottom — with the keyboard still on screen. Read the bottom
2097
+ // and that is indistinguishable from the keyboard closing.
2098
+ viewport.offsetTop = window.innerHeight - 500;
2099
+ input.blur();
2100
+
2101
+ // The keyboard did not change size, so neither does the scroll range.
2102
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
2103
+ inset,
2104
+ );
2105
+ });
2106
+
2107
+ it('keeps defending a pinned sheet after the browser has panned it once', () => {
2108
+ // The regression that made the device symptom permanent: one pan read as
2109
+ // "no keyboard" cleared the inset AND cleared hasKeyboardLayout, which
2110
+ // disarms the head start below — so every subsequent reveal panned, and
2111
+ // the sheet never recovered.
2112
+ const layoutBottom = window.innerHeight;
2113
+ mockIOSWebKit();
2114
+ const viewport = mockVisualViewport(500);
2115
+ render(
2116
+ <BottomSheet
2117
+ isOpen
2118
+ onOpenChange={() => {}}
2119
+ label="Add a comment"
2120
+ height="tall">
2121
+ <input aria-label="Title" />
2122
+ <input aria-label="Comment" />
2123
+ </BottomSheet>,
2124
+ );
2125
+ const body = getBody();
2126
+ const title = screen.getByRole('textbox', {name: 'Title'});
2127
+ const comment = screen.getByRole('textbox', {name: 'Comment'});
2128
+ const scrolls: {top: number; focusLanded: boolean}[] = [];
2129
+ Object.defineProperty(body, 'scrollBy', {
2130
+ configurable: true,
2131
+ value: (options: ScrollToOptions) => {
2132
+ body.scrollTop += options.top ?? 0;
2133
+ scrolls.push({
2134
+ top: options.top ?? 0,
2135
+ focusLanded: document.activeElement === comment,
2136
+ });
2137
+ },
2138
+ });
2139
+ vi.spyOn(body, 'getBoundingClientRect').mockReturnValue(
2140
+ rect({top: 100, bottom: layoutBottom}),
2141
+ );
2142
+ vi.spyOn(title, 'getBoundingClientRect').mockReturnValue(
2143
+ rect({top: 150, bottom: 190}),
2144
+ );
2145
+ vi.spyOn(comment, 'getBoundingClientRect').mockImplementation(() =>
2146
+ rect({top: 700 - body.scrollTop, bottom: 740 - body.scrollTop}),
2147
+ );
2148
+
2149
+ title.focus();
2150
+ const inset = body.style.getPropertyValue('--_sheet-keyboard-inset');
2151
+ expect(inset).toBe(`${layoutBottom - (500 - 48)}px`);
2152
+
2153
+ viewport.offsetTop = layoutBottom - 500;
2154
+ void act(() => viewport.dispatchEvent(new Event('resize')));
2155
+ expect(body.style.getPropertyValue('--_sheet-keyboard-inset')).toBe(
2156
+ inset,
2157
+ );
2158
+
2159
+ // And the next browser-driven transition is still claimed and revealed,
2160
+ // by a distance measured against the unshifted keyboard boundary.
2161
+ scrolls.length = 0;
2162
+ fireEvent.blur(title, {relatedTarget: comment});
2163
+
2164
+ expect(document.activeElement).toBe(comment);
2165
+ expect(scrolls[0]).toEqual({
2166
+ top: 740 - (500 - 48),
2167
+ focusLanded: true,
2168
+ });
2169
+ });
2170
+ });
2171
+
2172
+ describe('focus restore', () => {
2173
+ function Harness() {
2174
+ const [isOpen, setIsOpen] = useState(false);
2175
+ return (
2176
+ <>
2177
+ <button type="button" onClick={() => setIsOpen(true)}>
2178
+ Open sheet
2179
+ </button>
2180
+ <BottomSheet isOpen={isOpen} onOpenChange={setIsOpen} label="Filters">
2181
+ <button type="button" onClick={() => setIsOpen(false)}>
2182
+ Done
2183
+ </button>
2184
+ </BottomSheet>
2185
+ </>
2186
+ );
2187
+ }
2188
+
2189
+ it('restores focus to the opener after close', () => {
2190
+ render(<Harness />);
2191
+ const opener = screen.getByRole('button', {name: 'Open sheet'});
2192
+ opener.focus();
2193
+ fireEvent.click(opener);
2194
+ fireEvent.click(screen.getByRole('button', {name: 'Done'}));
2195
+ finishSheetExit();
2196
+
2197
+ expect(document.activeElement).toBe(opener);
2198
+ });
2199
+ });
2200
+
2201
+ describe('initial focus', () => {
2202
+ it('focuses the sheet panel on open, not the first control', () => {
2203
+ render(
2204
+ <BottomSheet isOpen onOpenChange={() => {}} label="Filters">
2205
+ <button type="button">First action</button>
2206
+ </BottomSheet>,
2207
+ );
2208
+ const panel = getSheet();
2209
+ expect(document.activeElement).toBe(panel);
2210
+ expect(document.activeElement).not.toBe(
2211
+ screen.getByRole('button', {name: 'First action'}),
2212
+ );
2213
+ });
2214
+
2215
+ it('honors a descendant with data-autofocus', () => {
2216
+ render(
2217
+ <BottomSheet isOpen onOpenChange={() => {}} label="Filters">
2218
+ <input data-autofocus aria-label="Search" />
2219
+ </BottomSheet>,
2220
+ );
2221
+ expect(document.activeElement).toBe(
2222
+ screen.getByRole('textbox', {name: 'Search'}),
2223
+ );
2224
+ });
2225
+ });
2226
+
2227
+ describe('accessible name', () => {
2228
+ it('warns in development when label is empty', () => {
2229
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
2230
+ render(
2231
+ <BottomSheet isOpen onOpenChange={() => {}} label="">
2232
+ Content
2233
+ </BottomSheet>,
2234
+ );
2235
+ expect(
2236
+ warn.mock.calls.some(args => String(args[0]).includes('BottomSheet')),
2237
+ ).toBe(true);
2238
+ warn.mockRestore();
2239
+ });
2240
+ });
2241
+
2242
+ describe('reduced motion', () => {
2243
+ it('opens without throwing when prefers-reduced-motion is set', () => {
2244
+ vi.stubGlobal(
2245
+ 'matchMedia',
2246
+ vi.fn().mockReturnValue({
2247
+ matches: true,
2248
+ media: '(prefers-reduced-motion: reduce)',
2249
+ onchange: null,
2250
+ addEventListener: vi.fn(),
2251
+ removeEventListener: vi.fn(),
2252
+ addListener: vi.fn(),
2253
+ removeListener: vi.fn(),
2254
+ dispatchEvent: vi.fn(),
2255
+ }),
2256
+ );
2257
+ render(
2258
+ <BottomSheet isOpen onOpenChange={() => {}} label="Filters">
2259
+ Content
2260
+ </BottomSheet>,
2261
+ );
2262
+ expect(screen.getByRole('dialog')).toBeInTheDocument();
2263
+ });
2264
+ });
2265
+ });