@oxyhq/bloom 0.46.0 → 0.49.0

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 (160) hide show
  1. package/lib/commonjs/bottom-sheet/BottomSheetBase.js +36 -12
  2. package/lib/commonjs/bottom-sheet/BottomSheetBase.js.map +1 -1
  3. package/lib/commonjs/combobox/Combobox.js +3 -3
  4. package/lib/commonjs/combobox/Combobox.js.map +1 -1
  5. package/lib/commonjs/command/Command.js +3 -3
  6. package/lib/commonjs/command/Command.js.map +1 -1
  7. package/lib/commonjs/dialog/Dialog.js +103 -8
  8. package/lib/commonjs/dialog/Dialog.js.map +1 -1
  9. package/lib/commonjs/dialog/Dialog.web.js +189 -12
  10. package/lib/commonjs/dialog/Dialog.web.js.map +1 -1
  11. package/lib/commonjs/dialog/DialogBottomSheet.js +111 -31
  12. package/lib/commonjs/dialog/DialogBottomSheet.js.map +1 -1
  13. package/lib/commonjs/dialog/DialogHeader.js +371 -0
  14. package/lib/commonjs/dialog/DialogHeader.js.map +1 -0
  15. package/lib/commonjs/dialog/DialogMorph.js +458 -0
  16. package/lib/commonjs/dialog/DialogMorph.js.map +1 -0
  17. package/lib/commonjs/dialog/index.js +14 -0
  18. package/lib/commonjs/dialog/index.js.map +1 -1
  19. package/lib/commonjs/dialog/index.web.js +14 -0
  20. package/lib/commonjs/dialog/index.web.js.map +1 -1
  21. package/lib/commonjs/index.js +11 -4
  22. package/lib/commonjs/index.js.map +1 -1
  23. package/lib/commonjs/index.web.js +11 -4
  24. package/lib/commonjs/index.web.js.map +1 -1
  25. package/lib/commonjs/react-native-className.d.js +6 -0
  26. package/lib/commonjs/react-native-className.d.js.map +1 -0
  27. package/lib/commonjs/{search-input → search}/index.js +5 -3
  28. package/lib/commonjs/search/index.js.map +1 -0
  29. package/lib/commonjs/settings-list/SettingsList.js +6 -2
  30. package/lib/commonjs/settings-list/SettingsList.js.map +1 -1
  31. package/lib/commonjs/surfaces/SurfaceHost.js +23 -9
  32. package/lib/commonjs/surfaces/SurfaceHost.js.map +1 -1
  33. package/lib/commonjs/text-field/index.js +22 -6
  34. package/lib/commonjs/text-field/index.js.map +1 -1
  35. package/lib/module/bottom-sheet/BottomSheetBase.js +36 -12
  36. package/lib/module/bottom-sheet/BottomSheetBase.js.map +1 -1
  37. package/lib/module/combobox/Combobox.js +3 -3
  38. package/lib/module/combobox/Combobox.js.map +1 -1
  39. package/lib/module/command/Command.js +3 -3
  40. package/lib/module/command/Command.js.map +1 -1
  41. package/lib/module/dialog/Dialog.js +104 -9
  42. package/lib/module/dialog/Dialog.js.map +1 -1
  43. package/lib/module/dialog/Dialog.web.js +192 -16
  44. package/lib/module/dialog/Dialog.web.js.map +1 -1
  45. package/lib/module/dialog/DialogBottomSheet.js +113 -33
  46. package/lib/module/dialog/DialogBottomSheet.js.map +1 -1
  47. package/lib/module/dialog/DialogHeader.js +362 -0
  48. package/lib/module/dialog/DialogHeader.js.map +1 -0
  49. package/lib/module/dialog/DialogMorph.js +451 -0
  50. package/lib/module/dialog/DialogMorph.js.map +1 -0
  51. package/lib/module/dialog/index.js +2 -0
  52. package/lib/module/dialog/index.js.map +1 -1
  53. package/lib/module/dialog/index.web.js +2 -0
  54. package/lib/module/dialog/index.web.js.map +1 -1
  55. package/lib/module/index.js +2 -2
  56. package/lib/module/index.js.map +1 -1
  57. package/lib/module/index.web.js +2 -2
  58. package/lib/module/index.web.js.map +1 -1
  59. package/lib/module/react-native-className.d.js +20 -0
  60. package/lib/module/react-native-className.d.js.map +1 -0
  61. package/lib/module/{search-input → search}/index.js +6 -2
  62. package/lib/module/search/index.js.map +1 -0
  63. package/lib/module/settings-list/SettingsList.js +6 -2
  64. package/lib/module/settings-list/SettingsList.js.map +1 -1
  65. package/lib/module/surfaces/SurfaceHost.js +23 -9
  66. package/lib/module/surfaces/SurfaceHost.js.map +1 -1
  67. package/lib/module/text-field/index.js +22 -6
  68. package/lib/module/text-field/index.js.map +1 -1
  69. package/lib/typescript/commonjs/bottom-sheet/BottomSheetBase.d.ts +31 -1
  70. package/lib/typescript/commonjs/bottom-sheet/BottomSheetBase.d.ts.map +1 -1
  71. package/lib/typescript/commonjs/combobox/Combobox.d.ts +1 -1
  72. package/lib/typescript/commonjs/command/Command.d.ts +1 -1
  73. package/lib/typescript/commonjs/dialog/Dialog.d.ts.map +1 -1
  74. package/lib/typescript/commonjs/dialog/Dialog.web.d.ts.map +1 -1
  75. package/lib/typescript/commonjs/dialog/DialogBottomSheet.d.ts +2 -2
  76. package/lib/typescript/commonjs/dialog/DialogBottomSheet.d.ts.map +1 -1
  77. package/lib/typescript/commonjs/dialog/DialogHeader.d.ts +86 -0
  78. package/lib/typescript/commonjs/dialog/DialogHeader.d.ts.map +1 -0
  79. package/lib/typescript/commonjs/dialog/DialogMorph.d.ts +150 -0
  80. package/lib/typescript/commonjs/dialog/DialogMorph.d.ts.map +1 -0
  81. package/lib/typescript/commonjs/dialog/index.d.ts +4 -1
  82. package/lib/typescript/commonjs/dialog/index.d.ts.map +1 -1
  83. package/lib/typescript/commonjs/dialog/index.web.d.ts +4 -1
  84. package/lib/typescript/commonjs/dialog/index.web.d.ts.map +1 -1
  85. package/lib/typescript/commonjs/dialog/types.d.ts +105 -0
  86. package/lib/typescript/commonjs/dialog/types.d.ts.map +1 -1
  87. package/lib/typescript/commonjs/icons/common.d.ts +1 -0
  88. package/lib/typescript/commonjs/icons/common.d.ts.map +1 -1
  89. package/lib/typescript/commonjs/index.d.ts +3 -3
  90. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  91. package/lib/typescript/commonjs/index.web.d.ts +3 -3
  92. package/lib/typescript/commonjs/index.web.d.ts.map +1 -1
  93. package/lib/typescript/commonjs/input-group/types.d.ts +1 -1
  94. package/lib/typescript/commonjs/{search-input → search}/index.d.ts +1 -1
  95. package/lib/typescript/commonjs/search/index.d.ts.map +1 -0
  96. package/lib/typescript/commonjs/surfaces/SurfaceHost.d.ts.map +1 -1
  97. package/lib/typescript/commonjs/surfaces/types.d.ts +26 -1
  98. package/lib/typescript/commonjs/surfaces/types.d.ts.map +1 -1
  99. package/lib/typescript/commonjs/text-field/index.d.ts +2 -1
  100. package/lib/typescript/commonjs/text-field/index.d.ts.map +1 -1
  101. package/lib/typescript/module/bottom-sheet/BottomSheetBase.d.ts +31 -1
  102. package/lib/typescript/module/bottom-sheet/BottomSheetBase.d.ts.map +1 -1
  103. package/lib/typescript/module/combobox/Combobox.d.ts +1 -1
  104. package/lib/typescript/module/command/Command.d.ts +1 -1
  105. package/lib/typescript/module/dialog/Dialog.d.ts.map +1 -1
  106. package/lib/typescript/module/dialog/Dialog.web.d.ts.map +1 -1
  107. package/lib/typescript/module/dialog/DialogBottomSheet.d.ts +2 -2
  108. package/lib/typescript/module/dialog/DialogBottomSheet.d.ts.map +1 -1
  109. package/lib/typescript/module/dialog/DialogHeader.d.ts +86 -0
  110. package/lib/typescript/module/dialog/DialogHeader.d.ts.map +1 -0
  111. package/lib/typescript/module/dialog/DialogMorph.d.ts +150 -0
  112. package/lib/typescript/module/dialog/DialogMorph.d.ts.map +1 -0
  113. package/lib/typescript/module/dialog/index.d.ts +4 -1
  114. package/lib/typescript/module/dialog/index.d.ts.map +1 -1
  115. package/lib/typescript/module/dialog/index.web.d.ts +4 -1
  116. package/lib/typescript/module/dialog/index.web.d.ts.map +1 -1
  117. package/lib/typescript/module/dialog/types.d.ts +105 -0
  118. package/lib/typescript/module/dialog/types.d.ts.map +1 -1
  119. package/lib/typescript/module/icons/common.d.ts +1 -0
  120. package/lib/typescript/module/icons/common.d.ts.map +1 -1
  121. package/lib/typescript/module/index.d.ts +3 -3
  122. package/lib/typescript/module/index.d.ts.map +1 -1
  123. package/lib/typescript/module/index.web.d.ts +3 -3
  124. package/lib/typescript/module/index.web.d.ts.map +1 -1
  125. package/lib/typescript/module/input-group/types.d.ts +1 -1
  126. package/lib/typescript/module/{search-input → search}/index.d.ts +1 -1
  127. package/lib/typescript/module/search/index.d.ts.map +1 -0
  128. package/lib/typescript/module/surfaces/SurfaceHost.d.ts.map +1 -1
  129. package/lib/typescript/module/surfaces/types.d.ts +26 -1
  130. package/lib/typescript/module/surfaces/types.d.ts.map +1 -1
  131. package/lib/typescript/module/text-field/index.d.ts +2 -1
  132. package/lib/typescript/module/text-field/index.d.ts.map +1 -1
  133. package/package.json +8 -8
  134. package/src/__tests__/BottomSheet.test.tsx +47 -0
  135. package/src/__tests__/DialogBottomSheet.test.tsx +30 -5
  136. package/src/__tests__/DialogMorph.test.tsx +230 -0
  137. package/src/bottom-sheet/BottomSheetBase.tsx +72 -11
  138. package/src/combobox/Combobox.tsx +3 -3
  139. package/src/command/Command.tsx +3 -3
  140. package/src/dialog/Dialog.tsx +111 -14
  141. package/src/dialog/Dialog.web.tsx +186 -25
  142. package/src/dialog/DialogBottomSheet.tsx +122 -30
  143. package/src/dialog/DialogHeader.tsx +429 -0
  144. package/src/dialog/DialogMorph.tsx +576 -0
  145. package/src/dialog/index.ts +4 -0
  146. package/src/dialog/index.web.ts +4 -0
  147. package/src/dialog/types.ts +106 -0
  148. package/src/index.ts +3 -1
  149. package/src/index.web.ts +3 -1
  150. package/src/input-group/types.ts +1 -1
  151. package/src/react-native-className.d.ts +38 -0
  152. package/src/{search-input → search}/index.tsx +8 -5
  153. package/src/settings-list/SettingsList.tsx +6 -2
  154. package/src/surfaces/SurfaceHost.tsx +17 -1
  155. package/src/surfaces/types.ts +26 -1
  156. package/src/text-field/index.tsx +24 -5
  157. package/lib/commonjs/search-input/index.js.map +0 -1
  158. package/lib/module/search-input/index.js.map +0 -1
  159. package/lib/typescript/commonjs/search-input/index.d.ts.map +0 -1
  160. package/lib/typescript/module/search-input/index.d.ts.map +0 -1
@@ -6,12 +6,25 @@ import React, {
6
6
  useMemo,
7
7
  useRef,
8
8
  } from 'react';
9
- import { View, type StyleProp, type ViewStyle } from 'react-native';
9
+ import {
10
+ useWindowDimensions,
11
+ View,
12
+ type StyleProp,
13
+ type ViewStyle,
14
+ } from 'react-native';
10
15
 
11
16
  import { BottomSheet, type BottomSheetRef } from '../bottom-sheet';
12
17
  import { useTheme } from '../theme/use-theme';
13
18
  import { Context } from './context';
14
19
  import { DialogBody } from './DialogContent';
20
+ import {
21
+ DIALOG_NAV_BAR_HEIGHT,
22
+ DialogHeaderProvider,
23
+ DialogLargeTitle,
24
+ DialogNavHeader,
25
+ useDialogHeaderController,
26
+ } from './DialogHeader';
27
+ import { DialogMorphContent, useDialogMorph } from './DialogMorph';
15
28
  import {
16
29
  DEFAULT_DIALOG_CONTENT_PADDING,
17
30
  DEFAULT_MAX_HEIGHT_RATIO,
@@ -43,15 +56,19 @@ import type { DialogControlProps, DialogProps } from './types';
43
56
  export function DialogBottomSheet({
44
57
  control,
45
58
  open: controlledOpen,
59
+ startOpen,
46
60
  onClose,
47
61
  testID,
48
62
  title,
49
63
  description,
50
64
  actions,
65
+ header,
51
66
  maxHeightRatio = DEFAULT_MAX_HEIGHT_RATIO,
52
67
  showHandle = true,
53
68
  dismissOnBackdrop = true,
54
69
  contentPadding = DEFAULT_DIALOG_CONTENT_PADDING,
70
+ scrollable,
71
+ morph,
55
72
  style,
56
73
  panelStyle,
57
74
  panelClassName,
@@ -62,10 +79,12 @@ export function DialogBottomSheet({
62
79
  }: DialogBottomSheetProps) {
63
80
  const isControlled = controlledOpen !== undefined;
64
81
  const theme = useTheme();
82
+ const { height: viewportHeight } = useWindowDimensions();
65
83
  const ref = useRef<BottomSheetRef>(null);
66
84
  const closeCallbacks = useRef<(() => void)[]>([]);
67
85
  const titleId = useId();
68
86
  const descriptionId = useId();
87
+ const headerController = useDialogHeaderController();
69
88
 
70
89
  // Read the latest controlled flag / `onClose` inside stable callbacks without
71
90
  // re-binding them (the context + imperative handle depend on `close` staying
@@ -144,6 +163,19 @@ export function DialogBottomSheet({
144
163
  [dismissOnBackdrop],
145
164
  );
146
165
 
166
+ const scrollableResolved = scrollable ?? true;
167
+
168
+ // Size morphing across an in-place content swap. The sheet is anchored at the
169
+ // bottom, so animating its height makes it grow/shrink from that edge; its
170
+ // width is fixed by the placement. The sheet's own `maxHeight` (the ratio
171
+ // percentage + the safe-area clamp inside `BottomSheet`) does the exact
172
+ // capping — the viewport is just the bound the reshape may not exceed.
173
+ const morphState = useDialogMorph({
174
+ enabled: morph !== false,
175
+ measurable: scrollableResolved,
176
+ maxHeight: viewportHeight,
177
+ });
178
+
147
179
  // `panelStyle` paints the sheet surface (e.g. a brand background). It composes
148
180
  // AFTER the BottomSheet's internal background so a host palette wins; the
149
181
  // shape (radius, max height) is supplied here and remains overridable.
@@ -157,48 +189,105 @@ export function DialogBottomSheet({
157
189
  maxHeight: maxHeightPercent,
158
190
  },
159
191
  panelStyle,
192
+ // Drives the card's `height` only while a morph is in flight; at rest it
193
+ // resolves to `height: 'auto'` — the sheet's normal content sizing.
194
+ morphState.panelStyle,
160
195
  ];
161
- }, [theme.colors.background, maxHeightRatio, panelStyle]);
196
+ }, [theme.colors.background, maxHeightRatio, panelStyle, morphState.panelStyle]);
162
197
 
163
198
  // Does this dialog render bloom's declarative chrome (title / description /
164
- // actions), or is it pure custom `children`? Pure custom children own their
165
- // own layout and scrolling, so we render them through a NON-scrollable
166
- // BottomSheet body otherwise the sheet's internal ScrollView would wrap a
167
- // child that already contains its own scroller, producing a scroll-in-scroll
168
- // (double scroll container). Declarative dialogs are short and keep the
169
- // sheet's default scrollable body so they degrade gracefully on small
170
- // viewports. This only changes what `DialogBottomSheet` passes to
171
- // `BottomSheet`; the standalone `BottomSheet` API and its `scrollable` default
172
- // are unchanged for direct consumers.
199
+ // actions)? This no longer gates the SCROLL decision (that is now the explicit
200
+ // `scrollable` prop below) it only opts declarative dialogs into the
201
+ // gorhom-style pan coordination for their internal ScrollView. A declarative
202
+ // dialog overflowing on a small viewport needs the coordinated pan so the body
203
+ // scrolls instead of the drag stealing every vertical gesture; pure-custom
204
+ // children keep the legacy always-active pan.
173
205
  const hasDeclarativeChrome =
174
206
  title !== undefined ||
175
207
  description !== undefined ||
176
208
  (actions !== undefined && actions.length > 0);
177
209
 
210
+ const dialogBody = (
211
+ <DialogBody
212
+ titleId={titleId}
213
+ descriptionId={descriptionId}
214
+ title={title}
215
+ description={description}
216
+ actions={actions}
217
+ >
218
+ {children}
219
+ </DialogBody>
220
+ );
221
+
222
+ // Nav-header mode: the Dialog OWNS a sticky gradient nav bar + a large
223
+ // collapsing title over the sheet's OWN scroll content. The bar floats via the
224
+ // BottomSheet `headerOverlay` slot; the sheet's scroll offset feeds the shared
225
+ // controller (`scrollY`) that drives the small-title cross-fade. `contentPadding`
226
+ // is dropped (the large title + screens own their padding).
227
+ const headerBody = header ? (
228
+ scrollableResolved ? (
229
+ <>
230
+ <DialogLargeTitle controller={headerController} header={header} />
231
+ <DialogHeaderProvider controller={headerController}>{dialogBody}</DialogHeaderProvider>
232
+ </>
233
+ ) : (
234
+ <>
235
+ <View style={{ height: DIALOG_NAV_BAR_HEIGHT }} />
236
+ <DialogHeaderProvider controller={headerController}>{dialogBody}</DialogHeaderProvider>
237
+ </>
238
+ )
239
+ ) : null;
240
+
178
241
  return (
179
242
  <BottomSheet
180
243
  ref={ref}
244
+ // Seed the sheet's open state so a fresh mount that should be open renders
245
+ // visible on its FIRST commit instead of relying on a present()-in-effect
246
+ // that races the mount and flashes blank. In controlled mode that seed is
247
+ // `controlledOpen`; in imperative mode it is the surface's `startOpen`
248
+ // intent — which is what lets a responsive Dialog survive a resize
249
+ // placement swap (centered card → bottom sheet) STILL OPEN.
250
+ open={isControlled ? controlledOpen : startOpen}
181
251
  onDismiss={handleDismiss}
182
252
  onDismissAttempt={onDismissAttempt}
183
253
  enablePanDownToClose
184
- showHandle={showHandle}
185
- // Pure custom children own scrolling opt OUT of BottomSheet's internal
186
- // ScrollView so there is exactly ONE scroll container (no scroll-in-scroll).
187
- scrollable={hasDeclarativeChrome}
188
- // Declarative dialogs (title/description/actions) that overflow render a
189
- // tall body inside BottomSheet's internal ScrollView. Opt those sheets into
254
+ // Nav-header mode hides the drag handle (the nav bar sits at the very top);
255
+ // drag-to-dismiss still works via the coordinated body pan.
256
+ showHandle={header ? false : showHandle}
257
+ // Content scrolls inside the sheet's internal ScrollView by default; the
258
+ // Dialog owns the scroll boundary. A surface whose content owns its own
259
+ // scroller (a FlatList / VirtualizedList) passes `scrollable={false}` to
260
+ // opt OUT so there is exactly ONE scroll container (no scroll-in-scroll,
261
+ // no "VirtualizedList inside a plain ScrollView" warning).
262
+ scrollable={scrollableResolved}
263
+ // Declarative dialogs (title/description/actions) that overflow — AND every
264
+ // nav-header page (its scroll content overflows by design) — need
190
265
  // gorhom-style pan coordination: the drag-to-dismiss pan only activates
191
266
  // when the ScrollView is at the top AND the finger moves down >8dp —
192
267
  // otherwise the ScrollView owns the touch. Without this the always-active
193
268
  // legacy body-pan steals vertical drags and a tall body cannot scroll
194
269
  // (dragging up dismisses the sheet / bleeds into the parent instead). The
195
270
  // standalone `BottomSheet` API keeps its legacy `manualActivation={false}`
196
- // default, so direct consumers are unaffected. Gated on the same flag as
197
- // `scrollable` because only the internal-ScrollView path needs it.
198
- manualActivation={hasDeclarativeChrome}
271
+ // default, so direct consumers are unaffected.
272
+ manualActivation={header ? scrollableResolved : hasDeclarativeChrome}
273
+ // Feed the sheet's scroll offset into the nav header's collapse controller.
274
+ scrollY={header ? headerController.scrollY : undefined}
275
+ // Float the sticky nav bar over the scroll body (clipped to the sheet top).
276
+ headerOverlay={
277
+ header ? (
278
+ <DialogNavHeader
279
+ controller={headerController}
280
+ header={header}
281
+ onDismiss={close}
282
+ collapse={scrollableResolved}
283
+ />
284
+ ) : undefined
285
+ }
199
286
  // Stronger dim than a lone sheet so an underlying sheet's handle/content
200
287
  // doesn't bleed through when a Dialog is stacked over one.
201
288
  backdropOpacity={SHEET_BACKDROP_OPACITY + 0.3}
289
+ // Measures the sheet card — the size a morph starts from.
290
+ onLayout={morphState.onPanelLayout}
202
291
  style={sheetStyle}
203
292
  >
204
293
  <Context.Provider value={context}>
@@ -210,7 +299,9 @@ export function DialogBottomSheet({
210
299
  {...(containerClassName ? ({ className: containerClassName } as Record<string, string>) : {})}
211
300
  {...(panelClassName ? ({ className: panelClassName } as Record<string, string>) : {})}
212
301
  style={[
213
- { padding: contentPadding },
302
+ // Nav-header mode: the large title + screens own their padding, so the
303
+ // wrapper adds none. Otherwise the Dialog's uniform content padding.
304
+ header ? null : { padding: contentPadding },
214
305
  // `containerStyle` carries the host's CSS-var theme scope; it wraps
215
306
  // the content subtree so descendants read the scoped palette.
216
307
  containerStyle,
@@ -218,15 +309,12 @@ export function DialogBottomSheet({
218
309
  panelStyle,
219
310
  ]}
220
311
  >
221
- <DialogBody
222
- titleId={titleId}
223
- descriptionId={descriptionId}
224
- title={title}
225
- description={description}
226
- actions={actions}
227
- >
228
- {children}
229
- </DialogBody>
312
+ {/* The morph layer is its own node INSIDE the class-name'd container:
313
+ a className'd component's `onLayout` never fires on web, and this
314
+ one has to measure the incoming frame. */}
315
+ <DialogMorphContent morph={morphState}>
316
+ {header ? headerBody : dialogBody}
317
+ </DialogMorphContent>
230
318
  </View>
231
319
  </Context.Provider>
232
320
  </BottomSheet>
@@ -242,15 +330,19 @@ export type DialogBottomSheetProps = Pick<
242
330
  DialogProps,
243
331
  | 'control'
244
332
  | 'open'
333
+ | 'startOpen'
245
334
  | 'onClose'
246
335
  | 'testID'
247
336
  | 'title'
248
337
  | 'description'
249
338
  | 'actions'
339
+ | 'header'
250
340
  | 'maxHeightRatio'
251
341
  | 'showHandle'
252
342
  | 'dismissOnBackdrop'
253
343
  | 'contentPadding'
344
+ | 'scrollable'
345
+ | 'morph'
254
346
  | 'style'
255
347
  | 'panelStyle'
256
348
  | 'panelClassName'
@@ -0,0 +1,429 @@
1
+ import React, {
2
+ createContext,
3
+ memo,
4
+ useContext,
5
+ useLayoutEffect,
6
+ useMemo,
7
+ useSyncExternalStore,
8
+ } from 'react';
9
+ import {
10
+ StyleSheet,
11
+ View,
12
+ type LayoutChangeEvent,
13
+ type StyleProp,
14
+ type ViewStyle,
15
+ } from 'react-native';
16
+ import Animated, {
17
+ Extrapolation,
18
+ interpolate,
19
+ useAnimatedStyle,
20
+ useSharedValue,
21
+ type SharedValue,
22
+ } from 'react-native-reanimated';
23
+
24
+ import {
25
+ ChevronLeft_Stroke2_Corner0_Rounded,
26
+ TimesLarge_Stroke2_Corner0_Rounded,
27
+ } from '../icons';
28
+ import { FrostedIconButton } from '../frosted-icon-button';
29
+ import { useTheme } from '../theme/use-theme';
30
+ import { H1, Text } from '../typography';
31
+ import type { DialogHeaderConfig } from './types';
32
+
33
+ export type { DialogHeaderConfig } from './types';
34
+
35
+ // ---------------------------------------------------------------------------
36
+ // Dialog-owned navigation header (iOS large-title collapse).
37
+ //
38
+ // This is a DISTINCT surface mode from the declarative
39
+ // `title`/`description`/`actions` centered-heading path: it turns the Dialog
40
+ // into a scrolling page with a sticky, gradient top bar (left + right slots)
41
+ // and a large collapsing title that lives in the Dialog's OWN scroll content.
42
+ // As the large title scrolls under the bar, a small title cross-fades into the
43
+ // bar center.
44
+ //
45
+ // Screens render NO header of their own — they declare their title/subtitle
46
+ // (via the surface registry) or contribute runtime slots (a Save action, a
47
+ // dynamic title) through {@link useDialogHeader}.
48
+ // ---------------------------------------------------------------------------
49
+
50
+ /** Interactive nav-bar row height (px). Buttons + collapsed title live here. */
51
+ export const DIALOG_NAV_BAR_HEIGHT = 52;
52
+
53
+ /** Extra gradient tail (px) below the bar, over which scrolled content fades. */
54
+ const GRADIENT_FADE = 20;
55
+
56
+ /** Total gradient overlay height (bar + fade tail). */
57
+ export const DIALOG_HEADER_OVERLAY_HEIGHT = DIALOG_NAV_BAR_HEIGHT + GRADIENT_FADE;
58
+
59
+ /**
60
+ * Top inset applied to the Dialog's scroll content so the large title starts
61
+ * BELOW the whole gradient overlay (fully clear at rest, then scrolls under it).
62
+ */
63
+ export const DIALOG_HEADER_CONTENT_TOP = DIALOG_HEADER_OVERLAY_HEIGHT;
64
+
65
+ /** Horizontal gutter — matches the `screen-margin` (20px) SDK screens use. */
66
+ const HEADER_H_PADDING = 20;
67
+
68
+ /** Circular icon-button diameter for the default back / close affordances. */
69
+ const NAV_BUTTON_SIZE = 36;
70
+
71
+ // --- Runtime override store (child screen → nav bar / large title) ----------
72
+ //
73
+ // The nav bar overlay and the in-content large title live in DIFFERENT subtrees
74
+ // (the bar is a sibling ABOVE the scroller; the large title + the screen are
75
+ // inside it), so a plain React ancestor context cannot bridge a screen's runtime
76
+ // contribution up to the bar. We use a tiny external store instead: the screen
77
+ // writes its override through {@link useDialogHeader}; the bar and large title
78
+ // read it via `useSyncExternalStore`. Only those two re-render — never the
79
+ // screen subtree — and there is no shared-ancestor constraint.
80
+
81
+ interface HeaderStore {
82
+ getSnapshot: () => DialogHeaderConfig | null;
83
+ setOverride: (next: DialogHeaderConfig | null) => void;
84
+ subscribe: (listener: () => void) => () => void;
85
+ }
86
+
87
+ function configsEqual(a: DialogHeaderConfig | null, b: DialogHeaderConfig | null): boolean {
88
+ if (a === b) return true;
89
+ if (!a || !b) return false;
90
+ return (
91
+ a.title === b.title &&
92
+ a.titleContent === b.titleContent &&
93
+ a.subtitle === b.subtitle &&
94
+ a.largeTitle === b.largeTitle &&
95
+ a.left === b.left &&
96
+ a.right === b.right &&
97
+ a.onBack === b.onBack &&
98
+ a.showClose === b.showClose
99
+ );
100
+ }
101
+
102
+ function createHeaderStore(): HeaderStore {
103
+ let override: DialogHeaderConfig | null = null;
104
+ const listeners = new Set<() => void>();
105
+ return {
106
+ getSnapshot: () => override,
107
+ setOverride: (next) => {
108
+ if (configsEqual(override, next)) return;
109
+ override = next;
110
+ for (const l of listeners) l();
111
+ },
112
+ subscribe: (listener) => {
113
+ listeners.add(listener);
114
+ return () => listeners.delete(listener);
115
+ },
116
+ };
117
+ }
118
+
119
+ /**
120
+ * The per-surface header controller — the scroll offset + measured large-title
121
+ * height shared values plus the runtime-override store. Created once per Dialog
122
+ * (via {@link useDialogHeaderController}) and threaded into the scroller (writes
123
+ * `scrollY`), the nav bar, the large title, and the child provider.
124
+ */
125
+ export interface DialogHeaderController {
126
+ scrollY: SharedValue<number>;
127
+ largeTitleHeight: SharedValue<number>;
128
+ store: HeaderStore;
129
+ }
130
+
131
+ /** Create the header controller for one Dialog. Stable for the Dialog's life. */
132
+ export function useDialogHeaderController(): DialogHeaderController {
133
+ const scrollY = useSharedValue(0);
134
+ const largeTitleHeight = useSharedValue(0);
135
+ const store = useMemo(() => createHeaderStore(), []);
136
+ return useMemo(
137
+ () => ({ scrollY, largeTitleHeight, store }),
138
+ [scrollY, largeTitleHeight, store],
139
+ );
140
+ }
141
+
142
+ const DialogHeaderContext = createContext<DialogHeaderController | null>(null);
143
+
144
+ /**
145
+ * Wraps the Dialog's scroll content so a mounted screen can contribute header
146
+ * slots via {@link useDialogHeader}. Only the child subtree needs this — the nav
147
+ * bar and large title read the same controller directly.
148
+ */
149
+ export function DialogHeaderProvider({
150
+ controller,
151
+ children,
152
+ }: {
153
+ controller: DialogHeaderController;
154
+ children: React.ReactNode;
155
+ }): React.ReactElement {
156
+ return (
157
+ <DialogHeaderContext.Provider value={controller}>
158
+ {children}
159
+ </DialogHeaderContext.Provider>
160
+ );
161
+ }
162
+
163
+ /**
164
+ * Contribute header content from within a Dialog surface — a dynamic title, a
165
+ * Save action, custom slots. Merges over the surface's static (registry) header
166
+ * config; unmounting clears the contribution. Slot nodes (`left`/`right`) MUST
167
+ * be referentially stable (memoize them) so the effect does not thrash.
168
+ *
169
+ * No-op outside a header-mode Dialog (e.g. a confirm dialog), so a screen can
170
+ * call it unconditionally.
171
+ */
172
+ export function useDialogHeader(config: DialogHeaderConfig | null | undefined): void {
173
+ const controller = useContext(DialogHeaderContext);
174
+ const store = controller?.store;
175
+ // Set synchronously in the commit's layout phase so the bar/title fill in
176
+ // BEFORE the browser paints — no first-frame flash of an empty bar.
177
+ useLayoutEffect(() => {
178
+ if (!store) return;
179
+ store.setOverride(config ?? null);
180
+ return () => store.setOverride(null);
181
+ // Slot nodes are compared by identity; callers memoize them.
182
+ }, [
183
+ store,
184
+ config?.title,
185
+ config?.titleContent,
186
+ config?.subtitle,
187
+ config?.largeTitle,
188
+ config?.left,
189
+ config?.right,
190
+ config?.onBack,
191
+ config?.showClose,
192
+ ]);
193
+ }
194
+
195
+ /** Read the merged (base + runtime-override) header config, reactively. */
196
+ function useMergedHeaderConfig(
197
+ base: DialogHeaderConfig,
198
+ store: HeaderStore,
199
+ ): DialogHeaderConfig {
200
+ const override = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
201
+ return useMemo(() => (override ? { ...base, ...override } : base), [base, override]);
202
+ }
203
+
204
+ // --- Nav bar overlay --------------------------------------------------------
205
+
206
+ /**
207
+ * The sticky nav bar: gradient background + left / center (collapsing title) /
208
+ * right slots. Absolutely positioned at the top of the Dialog surface, painted
209
+ * ABOVE the scroll content. Reads the scroll offset to cross-fade the small
210
+ * title in as the large title scrolls under it.
211
+ *
212
+ * `onDismiss` is the Dialog's own close (the default right-slot close button
213
+ * calls it) — passed explicitly rather than read from the dialog context because
214
+ * the bar is rendered as a sibling of the scroller, outside that context on the
215
+ * bottom-sheet path.
216
+ */
217
+ export const DialogNavHeader = memo(function DialogNavHeader({
218
+ controller,
219
+ header,
220
+ onDismiss,
221
+ collapse = true,
222
+ style,
223
+ }: {
224
+ controller: DialogHeaderController;
225
+ header: DialogHeaderConfig;
226
+ onDismiss: () => void;
227
+ /**
228
+ * Whether the small title cross-fades in on scroll (paired with an in-content
229
+ * {@link DialogLargeTitle}). Defaults to `true`. Set `false` for a surface
230
+ * whose content owns its own scroller (no Dialog scroll offset to drive the
231
+ * collapse): the small title is then always visible — a plain titled nav bar.
232
+ */
233
+ collapse?: boolean;
234
+ style?: StyleProp<ViewStyle>;
235
+ }) {
236
+ const theme = useTheme();
237
+ const config = useMergedHeaderConfig(header, controller.store);
238
+ const { scrollY, largeTitleHeight } = controller;
239
+
240
+ // A branded `titleContent` owns the bar centre outright: it is always visible,
241
+ // so there is nothing to collapse and no large in-content title to collapse under.
242
+ const hasLargeTitle =
243
+ collapse && !config.titleContent && (config.largeTitle ?? true) && !!config.title;
244
+
245
+ const titleStyle = useAnimatedStyle(() => {
246
+ // When there is no large title to collapse under, the bar title is always
247
+ // shown (a plain, non-collapsing nav bar). Otherwise it cross-fades in as the
248
+ // large title scrolls up under the bar.
249
+ if (!hasLargeTitle) return { opacity: 1, transform: [{ translateY: 0 }] };
250
+ const threshold = largeTitleHeight.value > 0 ? largeTitleHeight.value : 44;
251
+ const start = threshold * 0.35;
252
+ const end = threshold * 0.9;
253
+ return {
254
+ opacity: interpolate(scrollY.value, [start, end], [0, 1], Extrapolation.CLAMP),
255
+ transform: [
256
+ { translateY: interpolate(scrollY.value, [start, end], [10, 0], Extrapolation.CLAMP) },
257
+ ],
258
+ };
259
+ // Reanimated on RN-Web has no worklets plugin: every shared value the mapper
260
+ // reads MUST be in the deps array or it freezes on the first frame.
261
+ }, [scrollY, largeTitleHeight, hasLargeTitle]);
262
+
263
+ const left =
264
+ config.left ??
265
+ (config.onBack ? (
266
+ <FrostedIconButton
267
+ onPress={config.onBack}
268
+ accessibilityLabel="Go back"
269
+ icon={<ChevronLeft_Stroke2_Corner0_Rounded size="md" />}
270
+ />
271
+ ) : null);
272
+
273
+ const right =
274
+ config.right ??
275
+ (config.showClose !== false ? (
276
+ <FrostedIconButton
277
+ onPress={onDismiss}
278
+ accessibilityLabel="Close"
279
+ icon={<TimesLarge_Stroke2_Corner0_Rounded size="md" />}
280
+ />
281
+ ) : null);
282
+
283
+ return (
284
+ <View
285
+ pointerEvents="box-none"
286
+ style={[styles.overlay, style]}
287
+ >
288
+ {/* Opaque (surface bg) at the top → transparent at the bottom, so scrolled
289
+ content fades out under the bar. Pure NativeWind — no SVG dependency. */}
290
+ <View
291
+ pointerEvents="none"
292
+ className="bg-gradient-to-b from-bg to-transparent"
293
+ style={[StyleSheet.absoluteFill, { height: DIALOG_HEADER_OVERLAY_HEIGHT }]}
294
+ />
295
+ <View pointerEvents="box-none" style={styles.navRow}>
296
+ <View style={styles.side}>{left}</View>
297
+ <Animated.View
298
+ pointerEvents="none"
299
+ testID="dialog-nav-title"
300
+ style={[styles.centerTitle, titleStyle]}
301
+ >
302
+ {config.titleContent ?? null}
303
+ {!config.titleContent && config.title ? (
304
+ <Text numberOfLines={1} style={[styles.smallTitle, { color: theme.colors.text }]}>
305
+ {config.title}
306
+ </Text>
307
+ ) : null}
308
+ {/* The subtitle rides along ONLY in the static (non-collapsing) bar —
309
+ own-scroller screens whose subtitle is a short count. A large-title
310
+ screen keeps a clean single-line collapsed title, and a branded
311
+ `titleContent` bar carries no supporting copy at all. */}
312
+ {!hasLargeTitle && !config.titleContent && config.subtitle ? (
313
+ <Text
314
+ numberOfLines={1}
315
+ style={[styles.smallSubtitle, { color: theme.colors.textSecondary }]}
316
+ >
317
+ {config.subtitle}
318
+ </Text>
319
+ ) : null}
320
+ </Animated.View>
321
+ <View style={[styles.side, styles.sideRight]}>{right}</View>
322
+ </View>
323
+ </View>
324
+ );
325
+ });
326
+
327
+ // --- In-content large title -------------------------------------------------
328
+
329
+ /**
330
+ * The large collapsing title, rendered at the TOP of the Dialog's scroll content
331
+ * (before the screen body). Owns the content top inset (so the title starts
332
+ * below the gradient overlay) and measures its own text height into the
333
+ * controller so the nav bar knows when to cross-fade the small title in.
334
+ */
335
+ export const DialogLargeTitle = memo(function DialogLargeTitle({
336
+ controller,
337
+ header,
338
+ }: {
339
+ controller: DialogHeaderController;
340
+ header: DialogHeaderConfig;
341
+ }) {
342
+ const theme = useTheme();
343
+ const config = useMergedHeaderConfig(header, controller.store);
344
+ const { largeTitleHeight } = controller;
345
+
346
+ // A branded `titleContent` lives in the bar and never collapses, so the
347
+ // surface starts flush under the bar with no large title above it.
348
+ const hasLargeTitle = !config.titleContent && (config.largeTitle ?? true) && !!config.title;
349
+
350
+ const onLayout = (e: LayoutChangeEvent) => {
351
+ largeTitleHeight.value = e.nativeEvent.layout.height;
352
+ };
353
+
354
+ if (!hasLargeTitle) {
355
+ // No large title: just inset the content below the nav bar.
356
+ return <View style={{ height: DIALOG_NAV_BAR_HEIGHT }} />;
357
+ }
358
+
359
+ return (
360
+ <>
361
+ <View style={{ height: DIALOG_HEADER_CONTENT_TOP }} />
362
+ <View onLayout={onLayout} testID="dialog-large-title" style={styles.largeTitleBlock}>
363
+ <H1 style={[styles.largeTitle, { color: theme.colors.text }]}>{config.title}</H1>
364
+ {config.subtitle ? (
365
+ <Text style={[styles.largeSubtitle, { color: theme.colors.textSecondary }]}>
366
+ {config.subtitle}
367
+ </Text>
368
+ ) : null}
369
+ </View>
370
+ </>
371
+ );
372
+ });
373
+
374
+ const styles = StyleSheet.create({
375
+ overlay: {
376
+ position: 'absolute',
377
+ top: 0,
378
+ left: 0,
379
+ right: 0,
380
+ height: DIALOG_HEADER_OVERLAY_HEIGHT,
381
+ zIndex: 10,
382
+ },
383
+ navRow: {
384
+ height: DIALOG_NAV_BAR_HEIGHT,
385
+ flexDirection: 'row',
386
+ alignItems: 'center',
387
+ paddingHorizontal: HEADER_H_PADDING - 4,
388
+ },
389
+ side: {
390
+ minWidth: NAV_BUTTON_SIZE,
391
+ flexDirection: 'row',
392
+ alignItems: 'center',
393
+ },
394
+ sideRight: {
395
+ justifyContent: 'flex-end',
396
+ },
397
+ centerTitle: {
398
+ flex: 1,
399
+ alignItems: 'center',
400
+ justifyContent: 'center',
401
+ paddingHorizontal: 8,
402
+ },
403
+ smallTitle: {
404
+ fontSize: 16,
405
+ fontWeight: '600',
406
+ letterSpacing: -0.2,
407
+ lineHeight: 20,
408
+ },
409
+ smallSubtitle: {
410
+ fontSize: 12,
411
+ fontWeight: '400',
412
+ lineHeight: 14,
413
+ },
414
+ largeTitleBlock: {
415
+ paddingHorizontal: HEADER_H_PADDING,
416
+ paddingBottom: 4,
417
+ },
418
+ largeTitle: {
419
+ fontSize: 32,
420
+ lineHeight: 38,
421
+ letterSpacing: -0.5,
422
+ fontWeight: '700',
423
+ },
424
+ largeSubtitle: {
425
+ fontSize: 16,
426
+ lineHeight: 22,
427
+ marginTop: 4,
428
+ },
429
+ });