@sigmela/router 0.2.2 → 0.2.4

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.
package/README.md CHANGED
@@ -148,10 +148,60 @@ const stack = new NavigationStack({ header: { largeTitle: true } })
148
148
 
149
149
  Key methods:
150
150
  - `addScreen(pathPattern, componentOrNode, options?)`
151
- - `addModal(pathPattern, component, options?)` (shorthand for `stackPresentation: 'modal'`)
152
- - `addSheet(pathPattern, component, options?)` (shorthand for `stackPresentation: 'sheet'`)
151
+ - `addModal(pathPattern, componentOrStack, options?)` (shorthand for `stackPresentation: 'modal'`)
152
+ - `addSheet(pathPattern, componentOrStack, options?)` (shorthand for `stackPresentation: 'sheet'`)
153
153
  - `addStack(prefixOrStack, maybeStack?)` — compose nested stacks under a prefix
154
154
 
155
+ ### Modal Stacks (Stack in Stack)
156
+
157
+ You can pass an entire `NavigationStack` to `addModal()` or `addSheet()` to create a multi-screen flow inside a modal:
158
+
159
+ ```tsx
160
+ // Define a flow with multiple screens
161
+ const emailVerifyStack = new NavigationStack()
162
+ .addScreen('/verify', EmailInputScreen)
163
+ .addScreen('/verify/sent', EmailSentScreen);
164
+
165
+ // Mount the entire stack as a modal
166
+ const rootStack = new NavigationStack()
167
+ .addScreen('/', HomeScreen)
168
+ .addModal('/verify', emailVerifyStack);
169
+ ```
170
+
171
+ **How it works:**
172
+ - Navigating to `/verify` opens the modal with `EmailInputScreen`
173
+ - Inside the modal, `router.navigate('/verify/sent')` pushes `EmailSentScreen` within the same modal
174
+ - `router.goBack()` navigates back inside the modal stack
175
+ - `router.dismiss()` closes the entire modal from any depth
176
+
177
+ **Example screen with navigation inside modal:**
178
+
179
+ ```tsx
180
+ function EmailInputScreen() {
181
+ const router = useRouter();
182
+
183
+ return (
184
+ <View>
185
+ <Button title="Next" onPress={() => router.navigate('/verify/sent')} />
186
+ <Button title="Close" onPress={() => router.dismiss()} />
187
+ </View>
188
+ );
189
+ }
190
+
191
+ function EmailSentScreen() {
192
+ const router = useRouter();
193
+
194
+ return (
195
+ <View>
196
+ <Button title="Back" onPress={() => router.goBack()} />
197
+ <Button title="Done" onPress={() => router.dismiss()} />
198
+ </View>
199
+ );
200
+ }
201
+ ```
202
+
203
+ This pattern works recursively — you can nest stacks inside stacks to any depth.
204
+
155
205
  ### `Router`
156
206
 
157
207
  The `Router` holds navigation state and performs path matching.
@@ -169,6 +219,7 @@ Navigation:
169
219
  - `router.navigate(path)` — push
170
220
  - `router.replace(path, dedupe?)` — replace top of the active stack
171
221
  - `router.goBack()` — pop top of the active stack
222
+ - `router.dismiss()` — close the nearest modal or sheet (including all screens in a modal stack)
172
223
  - `router.reset(path)` — **web-only**: rebuild Router state as if app loaded at `path`
173
224
  - `router.setRoot(rootKey, { transition? })` — swap root at runtime (`rootKey` from `config.roots`)
174
225
 
@@ -180,8 +231,6 @@ State/subscriptions:
180
231
  - `router.subscribeRoot(cb)` — notify when root is replaced via `setRoot`
181
232
  - `router.getStackHistory(stackId)` — slice of history for a stack
182
233
 
183
- > Note: `router.getGlobalStackId()` exists but currently returns `undefined`.
184
-
185
234
  ### `TabBar`
186
235
 
187
236
  `TabBar` is a container node that renders one tab at a time.
@@ -193,12 +242,16 @@ const tabBar = new TabBar({ component: CustomTabBar, initialIndex: 0 })
193
242
  ```
194
243
 
195
244
  Key methods:
196
- - `addTab({ key, stack?, screen?, title?, icon?, selectedIcon?, ... })`
245
+ - `addTab({ key, stack?, node?, screen?, prefix?, title?, icon?, selectedIcon?, ... })`
197
246
  - `onIndexChange(index)` — switch active tab
198
247
  - `setBadge(index, badge | null)`
199
248
  - `setTabBarConfig(partialConfig)`
200
249
  - `getState()` and `subscribe(cb)`
201
250
 
251
+ Notes:
252
+ - Exactly one of `stack`, `node`, `screen` must be provided.
253
+ - Use `prefix` to mount a tab's routes under a base path (e.g. `/mail`).
254
+
202
255
  Web behavior note:
203
256
  - The built-in **web** tab bar renderer resets Router history on tab switch (to keep URL and Router state consistent) using `router.reset(firstRoutePath)`.
204
257
 
@@ -207,22 +260,25 @@ Web behavior note:
207
260
  `SplitView` renders **two stacks**: `primary` and `secondary`.
208
261
 
209
262
  - On **native**, `secondary` overlays `primary` when it has at least one screen in its history.
210
- - On **web**, the layout becomes side-by-side at a fixed breakpoint (currently `>= 640px` in CSS).
263
+ - On **web**, the layout becomes side-by-side at a fixed breakpoint (`minWidth`, default `640px`).
211
264
 
212
265
  ```tsx
213
- import { NavigationStack, SplitView } from '@sigmela/router';
266
+ import { NavigationStack, SplitView, TabBar } from '@sigmela/router';
214
267
 
215
268
  const master = new NavigationStack().addScreen('/', ThreadsScreen);
216
269
  const detail = new NavigationStack().addScreen('/:threadId', ThreadScreen);
217
270
 
218
271
  const splitView = new SplitView({
219
- minWidth: 640, // currently not used by the web renderer; kept for API compatibility
272
+ minWidth: 640,
220
273
  primary: master,
221
274
  secondary: detail,
222
275
  primaryMaxWidth: 390,
223
276
  });
224
277
 
225
- const root = new NavigationStack().addScreen('/mail', splitView);
278
+ // Mount SplitView directly as a tab (no wrapper stack needed).
279
+ const tabBar = new TabBar()
280
+ .addTab({ key: 'mail', node: splitView, prefix: '/mail', title: 'Mail' })
281
+ .addTab({ key: 'settings', stack: settingsStack, title: 'Settings' });
226
282
  ```
227
283
 
228
284
  ## Controllers
@@ -5,13 +5,22 @@ import { RouterContext } from "./RouterContext.js";
5
5
  import { ScreenStack } from "./ScreenStack/index.js";
6
6
  import { StyleSheet } from 'react-native';
7
7
  import { useSyncExternalStore, memo, useCallback, useEffect, useState } from 'react';
8
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
8
+ import { jsx as _jsx } from "react/jsx-runtime";
9
9
  const EMPTY_HISTORY = [];
10
10
  function useStackHistory(router, stackId) {
11
11
  const subscribe = useCallback(cb => stackId ? router.subscribeStack(stackId, cb) : () => {}, [router, stackId]);
12
12
  const get = useCallback(() => stackId ? router.getStackHistory(stackId) : EMPTY_HISTORY, [router, stackId]);
13
13
  return useSyncExternalStore(subscribe, get, get);
14
14
  }
15
+
16
+ /**
17
+ * Navigation component renders the root and global stacks.
18
+ *
19
+ * Modal stacks (NavigationStack added via addModal) are rendered as regular ScreenStackItems
20
+ * with their component being the StackRenderer that subscribes to its own stack history.
21
+ * This creates a clean recursive structure: stacks render their items, nested stacks
22
+ * (via childNode) render their own items through StackRenderer.
23
+ */
15
24
  export const Navigation = /*#__PURE__*/memo(({
16
25
  router,
17
26
  appearance
@@ -30,23 +39,17 @@ export const Navigation = /*#__PURE__*/memo(({
30
39
  rootId
31
40
  } = root;
32
41
  const rootTransition = router.getRootTransition();
33
- const globalId = router.getGlobalStackId();
34
42
  const rootItems = useStackHistory(router, rootId);
35
- const globalItems = useStackHistory(router, globalId);
36
43
  return /*#__PURE__*/_jsx(RouterContext.Provider, {
37
44
  value: router,
38
- children: /*#__PURE__*/_jsxs(ScreenStack, {
45
+ children: /*#__PURE__*/_jsx(ScreenStack, {
39
46
  style: styles.flex,
40
- children: [rootItems.map(item => /*#__PURE__*/_jsx(ScreenStackItem, {
47
+ children: rootItems.map(item => /*#__PURE__*/_jsx(ScreenStackItem, {
41
48
  stackId: rootId,
42
49
  item: item,
43
50
  stackAnimation: rootTransition,
44
51
  appearance: appearance
45
- }, `root-${item.key}`)), globalItems.map(item => /*#__PURE__*/_jsx(ScreenStackItem, {
46
- appearance: appearance,
47
- stackId: globalId,
48
- item: item
49
- }, `global-${item.key}`))]
52
+ }, `root-${item.key}`))
50
53
  }, rootId ?? 'root')
51
54
  });
52
55
  });
@@ -3,6 +3,7 @@
3
3
  import { nanoid } from 'nanoid/non-secure';
4
4
  import { match as pathMatchFactory } from 'path-to-regexp';
5
5
  import qs from 'query-string';
6
+ import React from 'react';
6
7
  export class NavigationStack {
7
8
  routes = [];
8
9
  children = [];
@@ -130,7 +131,18 @@ export class NavigationStack {
130
131
  return this.children.slice();
131
132
  }
132
133
  getRenderer() {
133
- return () => null;
134
+ // eslint-disable-next-line consistent-this
135
+ const stackInstance = this;
136
+ return function NavigationStackRenderer(props) {
137
+ // Lazy require to avoid circular dependency (StackRenderer imports NavigationStack)
138
+ const {
139
+ StackRenderer
140
+ } = require('./StackRenderer');
141
+ return /*#__PURE__*/React.createElement(StackRenderer, {
142
+ stack: stackInstance,
143
+ appearance: props.appearance
144
+ });
145
+ };
134
146
  }
135
147
  seed() {
136
148
  const first = this.getFirstRoute();
@@ -3,6 +3,7 @@
3
3
  import { nanoid } from 'nanoid/non-secure';
4
4
  import { Platform } from 'react-native';
5
5
  import qs from 'query-string';
6
+ import { isModalLikePresentation } from "./types.js";
6
7
  function canSwitchToRoute(node) {
7
8
  return node !== undefined && typeof node.switchToRoute === 'function';
8
9
  }
@@ -56,6 +57,9 @@ export class Router {
56
57
  }
57
58
  this.recomputeActiveRoute();
58
59
  }
60
+ isDebugEnabled() {
61
+ return this.debugEnabled;
62
+ }
59
63
  log(message, data) {
60
64
  if (this.debugEnabled) {
61
65
  if (data !== undefined) {
@@ -136,6 +140,72 @@ export class Router {
136
140
  }
137
141
  this.popFromActiveStack();
138
142
  };
143
+
144
+ /**
145
+ * Closes the nearest modal or sheet, regardless of navigation depth inside it.
146
+ * Useful when a NavigationStack is rendered inside a modal and you want to
147
+ * close the entire modal from any screen within it.
148
+ */
149
+ dismiss = () => {
150
+ // Find the nearest modal/sheet item in history (searching from end)
151
+ let modalItem = null;
152
+ for (let i = this.state.history.length - 1; i >= 0; i--) {
153
+ const item = this.state.history[i];
154
+ if (item && isModalLikePresentation(item.options?.stackPresentation)) {
155
+ modalItem = item;
156
+ break;
157
+ }
158
+ }
159
+ if (!modalItem) {
160
+ this.log('dismiss: no modal found in history');
161
+ return;
162
+ }
163
+ this.log('dismiss: closing modal', {
164
+ key: modalItem.key,
165
+ routeId: modalItem.routeId,
166
+ stackId: modalItem.stackId,
167
+ presentation: modalItem.options?.stackPresentation
168
+ });
169
+
170
+ // Handle sheet dismisser if registered
171
+ if (modalItem.options?.stackPresentation === 'sheet') {
172
+ const dismisser = this.sheetDismissers.get(modalItem.key);
173
+ if (dismisser) {
174
+ this.unregisterSheetDismisser(modalItem.key);
175
+ dismisser();
176
+ return;
177
+ }
178
+ }
179
+
180
+ // Check if modal has a childNode (NavigationStack added via addModal)
181
+ const compiled = this.registry.find(r => r.routeId === modalItem.routeId);
182
+ const childNode = compiled?.childNode;
183
+ if (childNode) {
184
+ // Modal stack: remove all items from child stack AND the modal wrapper item
185
+ const childStackId = childNode.getId();
186
+ this.log('dismiss: closing modal stack', {
187
+ childStackId,
188
+ modalKey: modalItem.key
189
+ });
190
+ const newHistory = this.state.history.filter(item => item.stackId !== childStackId && item.key !== modalItem.key);
191
+ this.setState({
192
+ history: newHistory
193
+ });
194
+
195
+ // Clear child stack's history cache
196
+ this.stackHistories.delete(childStackId);
197
+ } else {
198
+ // Simple modal: just pop the modal item
199
+ this.applyHistoryChange('pop', modalItem);
200
+ }
201
+ this.recomputeActiveRoute();
202
+ this.emit(this.listeners);
203
+
204
+ // Sync URL on web
205
+ if (this.isWebEnv()) {
206
+ this.syncUrlAfterInternalPop(modalItem);
207
+ }
208
+ };
139
209
  getState = () => {
140
210
  return this.state;
141
211
  };
@@ -154,6 +224,14 @@ export class Router {
154
224
  }
155
225
  return this.stackHistories.get(stackId) ?? EMPTY_ARRAY;
156
226
  };
227
+
228
+ /**
229
+ * Returns all history items in navigation order.
230
+ * Useful for rendering all screens including modal stacks.
231
+ */
232
+ getFullHistory = () => {
233
+ return this.state.history;
234
+ };
157
235
  subscribeStack = (stackId, cb) => {
158
236
  if (!stackId) return () => {};
159
237
  let set = this.stackListeners.get(stackId);
@@ -172,9 +250,6 @@ export class Router {
172
250
  getRootStackId() {
173
251
  return this.root?.getId();
174
252
  }
175
- getGlobalStackId() {
176
- return undefined;
177
- }
178
253
  subscribeRoot(listener) {
179
254
  this.rootListeners.add(listener);
180
255
  return () => this.rootListeners.delete(listener);
@@ -367,6 +442,14 @@ export class Router {
367
442
  }
368
443
  return;
369
444
  }
445
+
446
+ // Ensure the root-level container (e.g. TabBar) is activated for the matched route.
447
+ // This is important when the matched route belongs to a nested stack inside a composite node
448
+ // like SplitView: stackActivators won't fire for the tab itself in that case.
449
+ const rootStackId = this.root?.getId();
450
+ if (rootStackId) {
451
+ this.activateContainerForRoute(base.routeId, rootStackId);
452
+ }
370
453
  const activator = base.stackId ? this.stackActivators.get(base.stackId) : undefined;
371
454
  if (activator) {
372
455
  activator();
@@ -500,12 +583,23 @@ export class Router {
500
583
  }
501
584
  const newItem = this.createHistoryItem(base, params, query, pathname, passProps);
502
585
  this.applyHistoryChange(action, newItem);
586
+
587
+ // Seed child node if present
588
+ if (base.childNode) {
589
+ this.addChildNodeSeedsToHistory(base.routeId);
590
+ }
503
591
  };
504
592
  base.controller(controllerInput, present);
505
593
  return;
506
594
  }
507
595
  const newItem = this.createHistoryItem(base, params, query, pathname);
508
596
  this.applyHistoryChange(action, newItem);
597
+
598
+ // If the matched route has a childNode (e.g., NavigationStack added via addModal),
599
+ // seed the child stack's history so StackRenderer has items to render.
600
+ if (base.childNode) {
601
+ this.addChildNodeSeedsToHistory(base.routeId);
602
+ }
509
603
  }
510
604
  createHistoryItem(matched, params, query, pathname, passProps) {
511
605
  const normalizedParams = params && Object.keys(params).length > 0 ? params : undefined;
@@ -854,7 +948,7 @@ export class Router {
854
948
  }
855
949
  buildRegistry() {
856
950
  this.registry.length = 0;
857
- const addFromNode = (node, basePath) => {
951
+ const addFromNode = (node, basePath, inheritedOptions) => {
858
952
  const normalizedBasePath = this.normalizeBasePath(basePath);
859
953
  const baseSpecificity = this.computeBasePathSpecificity(normalizedBasePath);
860
954
  const routes = node.getNodeRoutes();
@@ -862,7 +956,16 @@ export class Router {
862
956
  if (stackId) {
863
957
  this.stackById.set(stackId, node);
864
958
  }
959
+ let isFirstRoute = true;
865
960
  for (const r of routes) {
961
+ // Merge options: first route inherits parent options (e.g., for nested stacks)
962
+ const mergedOptions = isFirstRoute && inheritedOptions ? {
963
+ ...inheritedOptions,
964
+ ...r.options
965
+ } : r.options;
966
+
967
+ // Always register the route.
968
+ // If it has a childNode, r.component is already childNode.getRenderer() (set by extractComponent).
866
969
  const compiled = {
867
970
  routeId: r.routeId,
868
971
  path: this.combinePathWithBase(r.path, normalizedBasePath),
@@ -883,7 +986,7 @@ export class Router {
883
986
  },
884
987
  component: r.component,
885
988
  controller: r.controller,
886
- options: r.options,
989
+ options: mergedOptions,
887
990
  stackId,
888
991
  childNode: r.childNode
889
992
  };
@@ -900,10 +1003,15 @@ export class Router {
900
1003
  pathnamePattern: compiled.pathnamePattern,
901
1004
  isWildcardPath: compiled.isWildcardPath,
902
1005
  baseSpecificity: compiled.baseSpecificity,
903
- stackId
1006
+ stackId,
1007
+ hasChildNode: !!compiled.childNode
904
1008
  });
1009
+ isFirstRoute = false;
1010
+
1011
+ // Also register routes from childNode (for navigation inside the nested stack)
905
1012
  if (r.childNode) {
906
1013
  const nextBaseForChild = r.isWildcardPath ? normalizedBasePath : this.joinPaths(normalizedBasePath, r.pathnamePattern);
1014
+ // Child routes don't inherit parent options - they use their own
907
1015
  addFromNode(r.childNode, nextBaseForChild);
908
1016
  }
909
1017
  }
@@ -1164,12 +1272,21 @@ export class Router {
1164
1272
  if (hasQueryPattern) {
1165
1273
  spec += 1000;
1166
1274
  }
1275
+
1276
+ // Routes with childNode AND modal/sheet presentation are "wrapper" routes
1277
+ // that should take priority over the child stack's own routes when both match.
1278
+ // This ensures addModal('/path', NavigationStack) renders the wrapper modal
1279
+ // and not the child stack's first screen directly.
1280
+ if (r.childNode && isModalLikePresentation(r.options?.stackPresentation)) {
1281
+ spec += 1;
1282
+ }
1167
1283
  this.log('matchBaseRoute candidate', {
1168
1284
  routeId: r.routeId,
1169
1285
  path: r.path,
1170
1286
  baseSpecificity: r.baseSpecificity,
1171
1287
  adjustedSpecificity: spec,
1172
1288
  hasQueryPattern,
1289
+ hasChildNode: !!r.childNode,
1173
1290
  stackId: r.stackId
1174
1291
  });
1175
1292
  if (!best || spec > best.specificity) {
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
 
3
- import { memo, useRef, useLayoutEffect, useMemo, useEffect, Children, isValidElement, Fragment } from 'react';
3
+ import { memo, useRef, useLayoutEffect, useMemo, useEffect, Children, isValidElement, Fragment, useCallback, useContext } from 'react';
4
4
  import { useTransitionMap } from 'react-transition-state';
5
5
  import { ScreenStackItemsContext, ScreenStackAnimatingContext, useScreenStackConfig } from "./ScreenStackContext.js";
6
6
  import { getPresentationTypeClass, computeAnimationType } from "./animationHelpers.js";
7
+ import { RouterContext } from "../RouterContext.js";
7
8
  import { jsx as _jsx } from "react/jsx-runtime";
8
- const devLog = (_, __) => {};
9
9
  const isScreenStackItemElement = child => {
10
10
  if (! /*#__PURE__*/isValidElement(child)) return false;
11
11
  const anyProps = child.props;
@@ -54,6 +54,12 @@ export const ScreenStack = /*#__PURE__*/memo(props => {
54
54
  transitionTime = 250,
55
55
  animated = true
56
56
  } = props;
57
+ const router = useContext(RouterContext);
58
+ const debugEnabled = router?.isDebugEnabled() ?? false;
59
+ const devLog = useCallback((msg, data) => {
60
+ if (!debugEnabled) return;
61
+ console.log(msg, data !== undefined ? JSON.stringify(data) : '');
62
+ }, [debugEnabled]);
57
63
  devLog('[ScreenStack] Render', {
58
64
  transitionTime,
59
65
  animated,
@@ -81,7 +87,7 @@ export const ScreenStack = /*#__PURE__*/memo(props => {
81
87
  stackChildrenLength: stackItems.length
82
88
  });
83
89
  return stackItems;
84
- }, [children]);
90
+ }, [children, devLog]);
85
91
  const routeKeys = useMemo(() => {
86
92
  const keys = stackChildren.map(child => {
87
93
  const item = child.props.item;
@@ -89,7 +95,7 @@ export const ScreenStack = /*#__PURE__*/memo(props => {
89
95
  });
90
96
  devLog('[ScreenStack] routeKeys', keys);
91
97
  return keys;
92
- }, [stackChildren]);
98
+ }, [devLog, stackChildren]);
93
99
  const childMap = useMemo(() => {
94
100
  const map = new Map(childMapRef.current);
95
101
  for (const child of stackChildren) {
@@ -103,7 +109,7 @@ export const ScreenStack = /*#__PURE__*/memo(props => {
103
109
  keys: Array.from(map.keys())
104
110
  });
105
111
  return map;
106
- }, [stackChildren]);
112
+ }, [devLog, stackChildren]);
107
113
  const {
108
114
  stateMap,
109
115
  toggle,
@@ -166,7 +172,7 @@ export const ScreenStack = /*#__PURE__*/memo(props => {
166
172
  exitingKeys
167
173
  });
168
174
  return result;
169
- }, [routeKeys, stateMapEntries]);
175
+ }, [devLog, routeKeys, stateMapEntries]);
170
176
  const containerClassName = useMemo(() => {
171
177
  return 'screen-stack';
172
178
  }, []);
@@ -227,7 +233,7 @@ export const ScreenStack = /*#__PURE__*/memo(props => {
227
233
  }
228
234
  lastDirectionRef.current = direction;
229
235
  devLog('[ScreenStack] === LIFECYCLE EFFECT END ===');
230
- }, [routeKeys, direction, setItem, toggle, stateMapEntries, stateMap, animateFirstScreenAfterEmpty]);
236
+ }, [routeKeys, direction, setItem, toggle, stateMapEntries, stateMap, animateFirstScreenAfterEmpty, devLog]);
231
237
  useLayoutEffect(() => {
232
238
  devLog('[ScreenStack] === CLEANUP EFFECT START ===');
233
239
  const routeKeySet = new Set(routeKeys);
@@ -253,7 +259,7 @@ export const ScreenStack = /*#__PURE__*/memo(props => {
253
259
  }
254
260
  }
255
261
  devLog('[ScreenStack] === CLEANUP EFFECT END ===');
256
- }, [routeKeys, stateMapEntries, deleteItem]);
262
+ }, [routeKeys, stateMapEntries, deleteItem, devLog]);
257
263
  useEffect(() => {
258
264
  if (!isInitialMountRef.current) return;
259
265
  const hasMountedItem = stateMapEntries.some(([, st]) => st.isMounted);
@@ -271,7 +277,7 @@ export const ScreenStack = /*#__PURE__*/memo(props => {
271
277
  isInitialMountRef.current = false;
272
278
  devLog('[ScreenStack] Initial mount completed');
273
279
  }
274
- }, [stateMapEntries, routeKeys.length, animateFirstScreenAfterEmpty]);
280
+ }, [stateMapEntries, routeKeys.length, animateFirstScreenAfterEmpty, devLog]);
275
281
 
276
282
  // Clear suppression key once it is no longer the top screen (so it can animate normally as
277
283
  // a background when new screens are pushed).
@@ -1,11 +1,14 @@
1
1
  "use strict";
2
2
 
3
+ import { isModalLikePresentation } from "../types.js";
3
4
  export function getPresentationTypeClass(presentation) {
4
5
  switch (presentation) {
5
6
  case 'push':
6
7
  return 'push';
7
8
  case 'modal':
8
9
  return 'modal';
10
+ case 'modalRight':
11
+ return 'modal-right';
9
12
  case 'transparentModal':
10
13
  return 'transparent-modal';
11
14
  case 'containedModal':
@@ -40,7 +43,7 @@ export function computeAnimationType(_key, isInStack, isTop, direction, presenta
40
43
  return 'none';
41
44
  }
42
45
  const isEntering = isInStack && isTop;
43
- const isModalLike = ['modal', 'transparentModal', 'containedModal', 'containedTransparentModal', 'fullScreenModal', 'formSheet', 'pageSheet', 'sheet'].includes(presentation);
46
+ const isModalLike = isModalLikePresentation(presentation);
44
47
  if (isModalLike) {
45
48
  if (!isInStack) {
46
49
  return getAnimationTypeForPresentation(presentation, false, direction);
@@ -48,7 +51,14 @@ export function computeAnimationType(_key, isInStack, isTop, direction, presenta
48
51
  if (isEntering) {
49
52
  return getAnimationTypeForPresentation(presentation, true, direction);
50
53
  }
51
- return 'none';
54
+
55
+ // Modal-like screen that's NOT top (background) - animate like push
56
+ // This happens when navigating inside a modal stack
57
+ if (direction === 'forward') {
58
+ return 'push-background';
59
+ } else {
60
+ return 'pop-background';
61
+ }
52
62
  }
53
63
  if (!isInStack) {
54
64
  if (direction === 'forward') {
@@ -17,6 +17,9 @@ export const ScreenStackItem = /*#__PURE__*/memo(({
17
17
  stackPresentation,
18
18
  ...screenProps
19
19
  } = item.options || {};
20
+
21
+ // On native, modalRight behaves as regular modal
22
+ const nativePresentation = stackPresentation === 'modalRight' ? 'modal' : stackPresentation;
20
23
  const route = {
21
24
  presentation: stackPresentation ?? 'push',
22
25
  params: item.params,
@@ -54,7 +57,7 @@ export const ScreenStackItem = /*#__PURE__*/memo(({
54
57
  style: StyleSheet.absoluteFill,
55
58
  contentStyle: appearance?.screen,
56
59
  headerConfig: headerConfig,
57
- stackPresentation: stackPresentation,
60
+ stackPresentation: nativePresentation,
58
61
  stackAnimation: stackAnimation ?? item.options?.stackAnimation,
59
62
  children: /*#__PURE__*/_jsx(RouteLocalContext.Provider, {
60
63
  value: route,
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
 
3
3
  import { RouteLocalContext, useRouter } from "../RouterContext.js";
4
- import { memo, useMemo } from 'react';
4
+ import { isModalLikePresentation } from "../types.js";
5
+ import { memo, useMemo, useCallback } from 'react';
5
6
  import { StyleSheet, View } from 'react-native';
6
7
  import { useScreenStackItemsContext } from "../ScreenStack/ScreenStackContext.js";
7
8
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
8
- const devLog = (_, __) => {};
9
9
  export const ScreenStackItem = /*#__PURE__*/memo(({
10
10
  item,
11
11
  appearance,
@@ -13,6 +13,11 @@ export const ScreenStackItem = /*#__PURE__*/memo(({
13
13
  }) => {
14
14
  const itemsContext = useScreenStackItemsContext();
15
15
  const router = useRouter();
16
+ const debugEnabled = router.isDebugEnabled();
17
+ const devLog = useCallback((msg, data) => {
18
+ if (!debugEnabled) return;
19
+ console.log(msg, data !== undefined ? JSON.stringify(data) : '');
20
+ }, [debugEnabled]);
16
21
  const key = item.key;
17
22
  const itemState = itemsContext.items[key];
18
23
  const presentationType = itemState?.presentationType;
@@ -21,7 +26,7 @@ export const ScreenStackItem = /*#__PURE__*/memo(({
21
26
  const transitionStatus = itemState?.transitionStatus;
22
27
  const zIndex = itemState?.zIndex ?? 0;
23
28
  const presentation = item.options?.stackPresentation ?? 'push';
24
- const isModalLike = ['modal', 'transparentModal', 'containedModal', 'containedTransparentModal', 'fullScreenModal', 'formSheet', 'pageSheet', 'sheet'].includes(presentation);
29
+ const isModalLike = isModalLikePresentation(presentation);
25
30
  const className = useMemo(() => {
26
31
  const classes = ['screen-stack-item'];
27
32
  if (presentationType) {
@@ -46,12 +51,20 @@ export const ScreenStackItem = /*#__PURE__*/memo(({
46
51
  className: classes.join(' ')
47
52
  });
48
53
  return classes.join(' ');
49
- }, [presentationType, animationType, transitionStatus, phase, item.key, item.path]);
54
+ }, [presentationType, animationType, transitionStatus, phase, devLog, item.key, item.path]);
50
55
  const mergedStyle = useMemo(() => ({
51
56
  flex: 1,
52
57
  ...style,
53
58
  zIndex
54
59
  }), [style, zIndex]);
60
+ const modalContainerStyle = useMemo(() => {
61
+ if (!isModalLike || !item.options?.maxWidth) {
62
+ return undefined;
63
+ }
64
+ return {
65
+ maxWidth: `${item.options.maxWidth}px`
66
+ };
67
+ }, [isModalLike, item.options?.maxWidth]);
55
68
  const value = {
56
69
  presentation,
57
70
  params: item.params,
@@ -67,9 +80,10 @@ export const ScreenStackItem = /*#__PURE__*/memo(({
67
80
  className: className,
68
81
  children: [isModalLike && /*#__PURE__*/_jsx("div", {
69
82
  className: "stack-modal-overlay",
70
- onClick: () => router.goBack()
83
+ onClick: () => router.dismiss()
71
84
  }), /*#__PURE__*/_jsx("div", {
72
85
  className: isModalLike ? 'stack-modal-container' : 'stack-screen-container',
86
+ style: modalContainerStyle,
73
87
  children: appearance?.screen ? /*#__PURE__*/_jsx(View, {
74
88
  style: [appearance?.screen, styles.flex],
75
89
  children: /*#__PURE__*/_jsx(RouteLocalContext.Provider, {