react-modal-navigation 1.1.4 → 1.1.6

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/dist/style.css CHANGED
@@ -63,3 +63,17 @@
63
63
  .pmn-exiting.pmn-place-bottom .pmn-dialog{animation-name:pmn-bottom-out}
64
64
  .pmn-no-animation,.pmn-no-animation .pmn-dialog{animation:none!important}
65
65
  @media(prefers-reduced-motion:reduce){.pmn-entering,.pmn-exiting,.pmn-entering .pmn-dialog,.pmn-exiting .pmn-dialog{animation:none!important}}
66
+
67
+ /* Modal-local toast region; clipped by .pmn-dialog and never portaled outside. */
68
+ .pmn-toast-region{position:absolute;top:56px;left:12px;right:12px;z-index:80;display:flex;flex-direction:column;align-items:center;gap:8px;pointer-events:none;max-height:calc(100% - 72px);overflow:hidden}
69
+ .pmn-toast{pointer-events:auto;display:flex;align-items:flex-start;gap:10px;width:max-content;max-width:100%;min-width:min(260px,100%);padding:11px 13px;border:1px solid var(--pmn-border);border-radius:9px;background:var(--pmn-surface);color:var(--pmn-text);box-shadow:0 5px 18px rgba(0,0,0,.10);font-size:13px;line-height:1.5;overflow-wrap:anywhere}
70
+ .pmn-toast-symbol{flex:0 0 20px;display:grid;place-items:center;width:20px;height:20px;border-radius:50%;font-size:13px;font-weight:700;background:var(--pmn-hover);color:var(--pmn-muted)}
71
+ .pmn-toast-success .pmn-toast-symbol{background:#dcfce7;color:#15803d}
72
+ .pmn-toast-error .pmn-toast-symbol{background:#fee2e2;color:#b91c1c}
73
+ .pmn-toast-warning .pmn-toast-symbol{background:#fef3c7;color:#b45309}
74
+ .pmn-toast-info .pmn-toast-symbol{background:#dbeafe;color:#1d4ed8}
75
+ .pmn-toast-message{flex:1;min-width:0;white-space:pre-wrap}
76
+ .pmn-toast-close{border:0;background:transparent;color:var(--pmn-muted);cursor:pointer;font-size:19px;line-height:18px;padding:0 1px}
77
+ .pmn-toast-close:hover{color:var(--pmn-text)}
78
+
79
+ .pmn-button-danger{background:#dc2626!important;color:#fff!important;border-color:#dc2626!important}
package/docs/API.md CHANGED
@@ -516,3 +516,114 @@ modal.setLoading(true, { text: t("common.loading") });
516
516
  modal.setLoading(false);
517
517
  ```
518
518
 
519
+
520
+
521
+ ## 弹窗内部 Toast 消息提醒
522
+
523
+ `useToast()` 只在当前弹窗的范围内显示消息,不会向全局 `document.body` 添加通知。支持成功、错误、警告、信息四种状态,最多同时显示 5 条;默认 3000ms 后自动消失。
524
+
525
+ ```jsx
526
+ import { useToast } from 'react-modal-navigation';
527
+ import { useTranslation } from 'react-i18next';
528
+
529
+ function EditDevice() {
530
+ const toast = useToast();
531
+ const { t } = useTranslation();
532
+ const save = async () => {
533
+ try {
534
+ await saveDevice();
535
+ toast.success(t('device.saveSuccess'));
536
+ } catch (error) {
537
+ toast.error(t('device.saveFailed'), { duration: 5000 });
538
+ }
539
+ };
540
+ return <button onClick={save}>{t('common.save')}</button>;
541
+ }
542
+ ```
543
+
544
+ ```jsx
545
+ const toast = useToast();
546
+ toast('操作提示'); // 默认 info
547
+ toast.info('请先填写名称');
548
+ toast.success('保存成功');
549
+ toast.warning('设备暂时离线');
550
+ toast.error('保存失败', { duration: 5000 });
551
+ const id = toast('持续提醒', { duration: 0 }); // 不自动关闭
552
+ toast.dismiss(id); // 关闭指定消息
553
+ toast.clear(); // 清空当前弹窗消息
554
+ ```
555
+
556
+ `duration` 单位毫秒;设为 `0` 则不自动消失。`dismissible: false` 隐藏关闭按钮。消息区域始终限制在弹窗内,弹窗关闭时通知自动销毁。国际化文案由业务侧调用 `t()` 后传入。
557
+
558
+ ## 第一阶段稳定性增强:导航拦截与页面缓存
559
+
560
+ - `navigation.navigate(name, params)` / `replace(name, params)` / `reset(name, params)` / `goBack(result)` 返回 `Promise<boolean>`,表示导航是否真正被执行。建议 `await`,尤其在注册了异步丢弃确认时。
561
+ - `useModalUnsavedChanges(dirty, { message, onBeforeDiscard })`:在即将丢弃的页面上执行保护。`onBeforeDiscard(details)` 返回 `false` 可阻止导航。`details.action` 为 `BACK`、`NAVIGATE`、`REPLACE` 或 `RESET`;`details.from` 和 `details.to` 分别表示源路由与目标名称。
562
+ - `screens.Page.keepAlive?: boolean`:是否保留非活动页面的组件实例,默认 `true`。
563
+ - `modalOptions.maxCachedScreens?: number`:最多保留多少个非活动页面,默认无限制;`0` 表示只渲染活动页面。缓存淘汰不删除导航栈记录,重新显示时组件重新挂载。
564
+ - `ModalProvider.confirmDiscard(details)`:没有设置页面级 `onBeforeDiscard` 时的统一确认入口。
565
+
566
+ ```jsx
567
+ function Edit({ route }) {
568
+ const [dirty, setDirty] = useState(false);
569
+ const { navigation } = useModal();
570
+ useModalUnsavedChanges(dirty, { message: '修改尚未保存,确定离开吗?' });
571
+ return <button onClick={async () => {
572
+ const ok = await navigation.goBack();
573
+ if (!ok) console.log('已取消返回');
574
+ }}>返回</button>;
575
+ }
576
+ ```
577
+
578
+
579
+ ## 并发导航和状态隔离
580
+
581
+ `navigate`、`replace`、`reset`、`goBack` 均为异步方法,返回 `Promise<boolean>`。在导航确认未完成期间,重复导航返回 `false`;异步确认完成时如果弹窗已卸载或路由已变化,则取消本次操作。`setParams` 和 `setOptions` 会同步更新内部导航快照,避免同一事件循环中后续操作读取过期路由。
582
+
583
+ Loading、Toast 的消息显示状态与导航操作分开管理;仅改变 Loading 文案时不会重建 Loading 命令对象。需要注意,当前版本仍存在 Provider 级别的渲染传播,不能保证所有缓存页面完全零重渲染。
584
+
585
+
586
+ ### 导航订阅隔离
587
+
588
+ - `useModalNavigationCommands()`:只读取稳定的导航操作,不订阅 `stack/currentRoute`。
589
+ - `useModalNavigationState()`:通过 `useSyncExternalStore` 订阅 `{stack,currentRoute,canGoBack}`。
590
+ - `useModalNavigation()`:兼容旧接口,合并命令与状态,状态变化时重新渲染。
591
+ - `useModal()`:兼容原有 `{modal,navigation}` 接口,仍订阅导航状态。
592
+
593
+ 以上 Hook 需要在弹窗页面内调用。
594
+
595
+ ### Loading、Footer、Toast 状态订阅隔离
596
+
597
+ 三类状态分别使用独立的外部状态存储,互不通知对方订阅者。原有 `useModalLoading()`、`useModalFooter()`、`useToast()` API 不变。`useModalLoading()` 订阅 `isLoading`;只需要操作 Loading 时仍可使用现有命令 API。测试见 `tests/state-subscriptions.test.mjs` 和可选的 `tests/react-profiler.test.mjs`。
598
+
599
+ ## v1.1.6 渲染与并发注意事项
600
+
601
+ `useModalNavigationCommands()` 只订阅命令 Context;`useModalNavigationState()` 才订阅路由快照。`useModalLoading()` 会订阅 Loading 的 `visible` 状态;如果页面只需要显示或隐藏 Loading,可通过导航命令接口减少订阅。Toast、Footer、Loading 使用独立 Store。Footer 异步回调增加同步重入锁。建议使用 React Profiler 测试真实业务组件。
602
+
603
+
604
+ ## 路由 Hook、异步导航、Footer Actions、Toast Promise (v1.1.6)
605
+
606
+ - `useModalRoute<P>()`:返回当前组件所属的 `ModalRoute<P>`;KeepAlive 隐藏页面也返回自身路由,而非活动页面。
607
+ - `useModalParams<P>()`:返回该路由的 `params`。
608
+ - `navigation.navigateAsync<T>(name, params?)`:等待目标路由返回。`goBack(value)` 返回 `{status:'returned',value}`;替换/重置/跳转移除返回 `{status:'removed'}`;拦截失败返回 `{status:'cancelled'}`;弹窗销毁返回 `{status:'dismissed'}`。
609
+ - `useModalFooter({actions:[{key,label,variant,disabled,hidden,loading,loadingText,onClick}]})`:声明式多按钮;每个按钮有独立防重复提交状态;`loadingText` 优先于通用文案。设置 `actions` 时覆盖传统 `done/cancel` 渲染。
610
+ - `toast.promise(task, {loading,success,error}, options?)`:先显示常驻 loading toast,结束后替换为 success/error,返回任务原值,失败继续抛出原错误。`task` 支持 Promise 或函数。
611
+
612
+ 注意:`navigateAsync` 用于需要返回数据的子页面,页面移除和弹窗关闭会结束等待。
613
+
614
+
615
+ ## Loading 订阅拆分(v1.1.6)
616
+
617
+ - `useModalLoading()`:兼容旧 API,返回命令和 `isLoading`,订阅加载状态。
618
+ - `useModalLoadingCommands()`:仅获取稳定的加载命令,不订阅状态。
619
+ - `useModalLoadingState()`:返回 `{ visible, text, count }`,订阅整窗 Loading 快照。
620
+
621
+ 内部控制器:`useToastController` 负责消息队列与计时器清理;`useLoadingController` 负责 token 计数与整窗蒙版状态。两者不作为公共 API 导出。
622
+
623
+ ## TypeScript 路由参数类型
624
+
625
+ ```ts
626
+ import type { TypedModalNavigation } from 'react-modal-navigation';
627
+ type Routes = { UserDetail: { userId: number }; DeviceList: undefined };
628
+ type Navigation = TypedModalNavigation<Routes>;
629
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-modal-navigation",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "React 19 modal navigation with automatic built-in CSS, Promise results and typed hooks",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -28,7 +28,8 @@
28
28
  "prepublishOnly": "npm run build",
29
29
  "pack:check": "npm pack --dry-run",
30
30
  "typecheck": "tsc --noEmit --skipLibCheck --strict types/index.d.ts",
31
- "test": "node --test tests/*.test.mjs"
31
+ "test": "node --test tests/*.test.mjs",
32
+ "test:react": "vitest run tests/react-profiler.integration.test.jsx --environment jsdom"
32
33
  },
33
34
  "peerDependencies": {
34
35
  "react": "^19.0.0",
@@ -40,7 +41,11 @@
40
41
  "react": "^19.0.0",
41
42
  "react-dom": "^19.0.0",
42
43
  "typescript": "^5.0.0",
43
- "@types/react": "^19.0.0"
44
+ "@types/react": "^19.0.0",
45
+ "vitest": "^3.2.0",
46
+ "jsdom": "^26.1.0",
47
+ "@testing-library/react": "^16.3.0",
48
+ "react-test-renderer": "^19.0.0"
44
49
  },
45
50
  "publishConfig": {
46
51
  "access": "public"