react-modal-navigation 1.1.0 → 1.1.2

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/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0
4
+
5
+ ### Features
6
+
7
+ - Modal、Drawer、Sheet 布局,支持 center、left、right、top、bottom。
8
+ - 左右抽屉使用动态视口高度,默认与视口边缘保持 5px 间距。
9
+ - 支持尺寸预设、自定义宽高、移动端全屏和主题配置。
10
+ - 自动注入组件样式,无需手动导入 CSS。
11
+ - 多页面导航、KeepAlive、路由参数和页面间结果传递。
12
+ - `modal.open` / `modal.openAsync` 支持字符串和对象两种调用形式。
13
+ - `modal.confirm` / `modal.dismiss` 明确区分确认与取消。
14
+ - `modal.enqueue` / `modal.enqueueAsync` 支持弹窗队列。
15
+ - `modal.update` 支持运行时调整弹窗配置。
16
+ - `useModalUnsavedChanges` 提供未保存表单保护。
17
+ - 根据弹窗位置匹配进入与退出动画,并支持减少动态效果设置。
18
+ - 提供 TypeScript 类型声明和中文 API 参考文档。
package/README.md CHANGED
@@ -2,20 +2,20 @@
2
2
 
3
3
  React 19 多页面弹窗导航组件库。支持 **Modal / Drawer / Sheet**、页面堆栈、KeepAlive、Promise 返回结果、异步关闭守卫、Footer、Loading、暗黑主题与自动注入样式。
4
4
 
5
- > 包名 `react-modal-navigation`,源码版本 `1.0.0`。此源码包尚未代替你在 npm 上发布;请先运行构建及浏览器回归测试。**默认不需要导入 CSS 文件**。
5
+ > 默认自动注入样式,无需手动导入 CSS 文件。
6
6
 
7
7
  ## 目录
8
8
 
9
9
  1. [安装和快速开始](#安装和快速开始)
10
10
  2. [对象形式打开弹窗](#推荐对象形式打开弹窗)
11
11
  3. [弹窗位置与尺寸](#弹窗位置与尺寸)
12
- 3. [配置优先级与响应式](#配置优先级与响应式)
12
+ 4. [配置优先级与响应式](#配置优先级与响应式)
13
13
  4. [页面导航与传参](#页面导航与传参)
14
14
  5. [Promise 结果与关闭守卫](#promise-结果与关闭守卫)
15
15
  6. [异步提交与 Loading](#异步提交与-loading)
16
16
  7. [主题与样式覆盖](#主题与样式覆盖)
17
17
  8. [事件、可访问性和注意事项](#事件可访问性和注意事项)
18
- 9. [TypeScript 和发布](#typescript-和发布)
18
+ 9. [TypeScript](#typescript)
19
19
 
20
20
  ## 安装和快速开始
21
21
 
@@ -70,6 +70,8 @@ export default function App() {
70
70
  }
71
71
  ```
72
72
 
73
+ **不需要** `import 'react-modal-navigation/style.css'`,Provider 会自动注入组件样式;业务页面可以使用任意 CSS 方案。
74
+
73
75
  ## 推荐:对象形式打开弹窗
74
76
 
75
77
  `modal.open()` 和 `modal.openAsync()` 现在都支持**字符串位置参数**及**对象参数**两种写法,原有代码不需要修改。推荐新项目采用对象形式,避免为了设置位置而传入无意义的 `{}`。
@@ -109,7 +111,7 @@ await modal.open('editUser', {}, { placement: 'bottom', height: '55dvh' });
109
111
  | --- | --- | --- | --- |
110
112
  | `name` | `string` | 是 | registry 中注册的弹窗名称 |
111
113
  | `params` | `unknown` | 否 | 传给初始 Screen 的页面参数,默认 `{}` |
112
- | `options` | `ModalOptions` | 否 | 本次打开覆盖的布局、尺寸、主题和事件配置 |
114
+ | `options` | `ModalOptions` | 否 | 当前弹窗覆盖的布局、尺寸、主题和事件配置 |
113
115
 
114
116
  **兼容规则:** 第一个参数是字符串时继续使用 `open(name, params?, overrides?)`;第一个参数是对象时使用 `open({ name, params?, options? })`。对象形式不需要再传第二、第三个参数。配置优先级仍为 `defaultOptions → modalOptions → options`。`openAsync` 的结果结构不变。
115
117
 
@@ -295,7 +297,7 @@ function SaveScreen() {
295
297
  - 若多次调用 `open()` 或存在异步关闭守卫,建议 `await` 以避免竞态。
296
298
  - 样式注入依赖浏览器 DOM;SSR 时不会直接访问 `document`。严格 CSP 环境可能需要改为静态 CSS。
297
299
 
298
- ## TypeScript 和发布
300
+ ## TypeScript
299
301
 
300
302
  公开类型位于 `types/index.d.ts`,包括 `ModalOptions`、`ModalRegistry`、`ModalNavigation`、`ModalResult` 等。
301
303
 
@@ -306,9 +308,10 @@ const registry: ModalRegistry = {
306
308
  };
307
309
  ```
308
310
 
309
- > API 更新:支持 `modal.open({ name, params, options })` 和 `modal.openAsync({ name, params, options })`,字符串形式继续兼容。
310
311
 
311
- ## 更简洁的打开方式(增强版)
312
+ 支持 `modal.open({ name, params, options })` 和 `modal.openAsync({ name, params, options })`,字符串形式继续兼容。
313
+
314
+ ## 简洁的对象调用
312
315
 
313
316
  对象调用现在还支持将布局配置直接放在顶层,`options` 不再是必需的:
314
317
 
@@ -356,10 +359,9 @@ if (result.status === 'confirmed') {
356
359
  - **防止重复提交**:在弹窗页面中使用 `useModalTask()`,并在成功后 `modal.confirm(result)`。
357
360
  - **导航返回值**:多页面流程中用 `navigation.goBack(result)`,最外层完成时用 `modal.confirm(result)`。
358
361
 
359
- > 兼容性说明:本增强版仍保持 npm 包名和源码版本 `react-modal-navigation@1.0.0`,尚未发布到 npm。发布前建议更新语义版本号,并完成实际浏览器回归测试。
360
362
 
361
363
 
362
- ## 表单未保存保护(新增)
364
+ ## 表单未保存保护
363
365
 
364
366
  在任意弹窗页面调用 `useModalUnsavedChanges(isDirty)`。关闭、取消、ESC、遮罩关闭和替换弹窗时都会检查;用户拒绝放弃修改则保留当前弹窗。KeepAlive 页面即使暂时隐藏,保护仍然生效。
365
367
 
package/docs/API.md ADDED
@@ -0,0 +1,229 @@
1
+ # API 参考手册(react-modal-navigation@1.0.0)
2
+
3
+ > 本文介绍组件库的公开接口、参数、返回值及使用约定。
4
+
5
+ ## 1. 导入与 Provider
6
+
7
+ ```tsx
8
+ import { ModalProvider, useModal, useModalTask, useModalLoading,
9
+ useModalFooter, useModalNavigation, useIsFocused,
10
+ useFocusEffect, useNavigationResult } from 'react-modal-navigation';
11
+ ```
12
+
13
+ ```tsx
14
+ <ModalProvider
15
+ registry={registry}
16
+ defaultOptions={{ placement: 'center', width: 560, viewportGap: 5 }}
17
+ theme="auto"
18
+ translate={key => translations[key] ?? key}
19
+ onError={(error, context) => console.error(context.phase, error)}
20
+ >
21
+ <App />
22
+ </ModalProvider>
23
+ ```
24
+
25
+ | 属性 | 类型 | 默认值 | 说明 |
26
+ |---|---|---|---|
27
+ | `registry` | `ModalRegistry` | 必填 | 弹窗名称到路由/屏幕配置的映射 |
28
+ | `children` | `ReactNode` | 必填 | 应用内容 |
29
+ | `defaultOptions` | `ModalOptions` | `{}` | 全局默认弹窗配置 |
30
+ | `theme` | `'auto' \| 'light' \| 'dark'` | 未指定 | 主题,auto 根据宿主主题和系统偏好 |
31
+ | `backgroundColor` | `string \| {light?,dark?}` | 未指定 | 对话框背景 |
32
+ | `translate` | `(key:string)=>string` | 内置中文翻译 | UI 文案翻译 |
33
+ | `onError` | `(error,context)=>void` | 控制台输出 | 生命周期/组件异常回调 |
34
+
35
+ 默认样式由 Provider 自动注入,不必导入 `style.css`。多个 Provider 各自维护弹窗状态;队列也分别独立。
36
+
37
+ ## 2. Registry 结构
38
+
39
+ ```tsx
40
+ const registry = {
41
+ editUser: {
42
+ initialRoute: 'Form',
43
+ modalOptions: { placement: 'right', width: 520, viewportGap: 5 },
44
+ screens: {
45
+ Form: { title: '编辑用户', component: UserForm },
46
+ Detail: { title: '用户详情', component: UserDetail },
47
+ },
48
+ },
49
+ };
50
+ ```
51
+
52
+ `initialRoute` 必须存在于 `screens`。每个 Screen 组件接收 `{ route, navigation }`;`route.params` 来自打开弹窗时的 `params` 或导航时传参。Screen 也可覆盖部分 `ModalOptions`(例如标题、Footer)。
53
+
54
+ ## 3. modal 方法完整列表
55
+
56
+ 从 `const { modal, navigation } = useModal()` 获取。`modal` 在 Provider 子树任意组件可用;`navigation` 的动作只允许在已挂载的弹窗 Screen 中调用。
57
+
58
+ | 方法/属性 | 签名 | 返回 | 行为 |
59
+ |---|---|---|---|
60
+ | `open` | `(name,params?,options?)` 或 `(config)` | `Promise<number \| null>` | 打开,已有弹窗则尝试替换;关闭守卫拒绝时为 null |
61
+ | `openAsync` | 同上 | `Promise<ModalResult>` | 等待弹窗确认、取消或被替换;被阻止时返回 dismissed/blocked |
62
+ | `enqueue` | 同上 | `Promise<number \| null>` | 进入 FIFO 队列,轮到它显示时返回 ID;清队列/卸载返回 null |
63
+ | `enqueueAsync` | 同上 | `Promise<ModalResult>` | 进入 FIFO 队列并等待最终结果;清队列/卸载返回 dismissed |
64
+ | `update` | `(patch:ModalOptions)` | `boolean` | 更新当前弹窗运行时配置,保持路由栈和 Screen 实例;无活动弹窗或正在切换时返回 false |
65
+ | `clearQueue` | `(reason?:string)` | `number` | 清除尚未显示的排队项,返回清除数量;不关闭当前弹窗 |
66
+ | `queueSize` | `number` | — | 当前等待队列长度 |
67
+ | `close` | `(result?:unknown)` | `Promise<boolean>` | 请求关闭;有 `result` 时作为 confirmed 结果,否则 dismissed |
68
+ | `confirm` | `(value?:unknown)` | `Promise<boolean>` | 明确确认,即使 value 为 undefined |
69
+ | `dismiss` | `(reason?:string)` | `Promise<boolean>` | 明确取消,默认 reason='api' |
70
+ | `setLoading` | `(value:boolean,text?:string)` | `void` | 控制当前弹窗全局 Loading |
71
+ | `isOpen` | `boolean` | — | 当前是否有弹窗 |
72
+ | `activeName` | `string \| null` | — | 当前弹窗注册名 |
73
+ | `error` | `unknown` | — | 最近一次捕获的错误 |
74
+ | `clearError` | `()=>void` | `void` | 清空错误 |
75
+
76
+ ### 3.1 打开参数
77
+
78
+ ```tsx
79
+ // 新推荐:扁平对象
80
+ modal.open({ name: 'editUser', params: { userId: 42 }, placement: 'right', width: 520 });
81
+ // 嵌套 options:优先级高于同名扁平字段
82
+ modal.open({ name: 'editUser', placement: 'left', options: { placement: 'center' } });
83
+ // 完全兼容旧形式
84
+ modal.open('editUser', { userId: 42 }, { placement: 'right' });
85
+ ```
86
+
87
+ 配置优先级:Provider 的 `defaultOptions` → Registry 的 `modalOptions` → 本次 `open` 参数 → 后续 `update` 参数。`open` 在存在活动弹窗时会尝试**替换**,不会自动排队;需要队列时显式调用 `enqueue` / `enqueueAsync`。
88
+
89
+ ### 3.2 弹窗队列
90
+
91
+ ```tsx
92
+ const first = modal.enqueueAsync({ name: 'editUser', params: { userId: 1 } });
93
+ const second = modal.enqueueAsync({ name: 'editUser', params: { userId: 2 } });
94
+ // 只有第一个弹窗关闭后,第二个才显示
95
+ const firstResult = await first;
96
+ const secondResult = await second;
97
+ ```
98
+
99
+ 队列遵循 FIFO,`onBeforeClose` 返回 false 时当前弹窗不关闭,后续队列继续等待。`onClose` 异步执行完成后才尝试打开下一个。`clearQueue('cancelled-by-user')` 会使等待的 `enqueueAsync` 返回 `{status:'dismissed',reason:'cancelled-by-user'}`,不会取消正在显示的弹窗。Provider 卸载时等待项会以 `unmount` 结束。**不要在当前弹窗尚未关闭时直接 await 一个排队的结果再尝试关闭当前弹窗**,否则会形成业务逻辑上的等待循环。
100
+
101
+ ### 3.3 动态布局
102
+
103
+ ```tsx
104
+ modal.update({ placement: 'center', width: 760, maxHeight: '85dvh' });
105
+ modal.update({ placement: 'right', width: 520, viewportGap: 5 });
106
+ ```
107
+
108
+ `update` 是**浅合并**:只覆盖传入字段,不能修改当前 modal 的 `name`、`params` 或 `screens`。如果要修改当前路由参数,使用 `navigation.setParams()`。当前更新不会重置路由堆栈,样式变化由 React 重新渲染生效。
109
+
110
+ ## 4. ModalOptions 完整参考
111
+
112
+ | 属性 | 类型 | 默认/说明 |
113
+ |---|---|---|
114
+ | `placement` | `'center' \| 'left' \| 'right' \| 'top' \| 'bottom'` | 默认 `right` |
115
+ | `size` | `'sm' \| 'md' \| 'lg' \| 'xl' \| 'full'` | 预设宽 400/560/720/960/100% |
116
+ | `width` | `number \| string` | 数字为 px;优先于 size |
117
+ | `height` | `number \| string` | left/right 会强制采用视口高度减间隙 |
118
+ | `maxWidth` | `number \| string` | 最大宽度 |
119
+ | `maxHeight` | `number \| string` | 最大高度 |
120
+ | `viewportGap` | `number` | 默认 5,left/right 四周距视口 5px;高度 `calc(100dvh - 10px)` |
121
+ | `mobileFullscreen` | `boolean` | <=640px 使用全屏样式(若启用) |
122
+ | `borderRadius` | `number \| string` | 默认 12px |
123
+ | `theme` | `'auto' \| 'light' \| 'dark'` | 自动识别宿主主题 |
124
+ | `backgroundColor` | `string \| {light?,dark?}` | 背景色或背景 class |
125
+ | `closeOnOverlay` | `boolean` | 默认 false |
126
+ | `closeOnEsc` | `boolean` | 默认 true |
127
+ | `showHeader` / `showTitle` | `boolean` | 标题栏及标题开关 |
128
+ | `showBackButton` / `showCloseButton` | `boolean` | 返回及关闭按钮开关 |
129
+ | `showHeaderBorder` / `showFooterBorder` | `boolean` | 分割线开关 |
130
+ | `showFooter` | `boolean` | 底部操作区开关 |
131
+ | `lockCloseWhileLoading` | `boolean` | 加载期间禁止关闭 |
132
+ | `className` | `string` | Dialog 类名 |
133
+ | `bodyClassName` / `headerClassName` / `footerClassName` | `string` | 各区域额外类名 |
134
+ | `ariaLabel` | `string` | 无标题时的无障碍名称 |
135
+ | `doneText` / `cancelText` | `string` | 按钮文案 |
136
+ | `doneDisabled` | `boolean` | 禁用完成按钮 |
137
+ | `loadingText` | `string` | Loading 文案 |
138
+ | `done` / `cancel` | `(context)=>void \| Promise<void>` | Footer 操作回调 |
139
+ | `renderFooter` | `(context)=>ReactNode` | 自定义 Footer |
140
+ | `renderHeader` / `renderHeaderRight` | `(context)=>ReactNode` | 自定义标题栏/右侧区域 |
141
+ | `onBeforeClose` | `(context)=>boolean \| void \| Promise<boolean \| void>` | 返回 false 阻止关闭和替换 |
142
+ | `onOpen` / `onClose` | `(context)=>void` | 打开/关闭事件 |
143
+ | `onNavigate` / `onRouteChange` | `(context)=>void` | 页面导航事件 |
144
+ | `onFocus` / `onBlur` | `(context)=>void` | 页面焦点事件 |
145
+
146
+ **左右抽屉:** `placement: 'left'/'right'` 时,宽度仍按 `width/size` 配置,但高度由视口自动计算,四周默认 5px 间隙;内部内容过长时滚动。设置 `viewportGap: 0` 可取消缝隙。
147
+
148
+ ## 5. navigation 方法
149
+
150
+ | API | 说明 |
151
+ |---|---|
152
+ | `navigate(routeName, params?)` | 跳转到 Screen;已在栈中则复用该路由并截断后续栈 |
153
+ | `replace(routeName, params?)` | 替换当前 Screen |
154
+ | `reset(routeName, params?)` | 清空路由栈并设为新根页面 |
155
+ | `setParams(partial)` | 浅合并当前 Screen 参数,不改变 route.key |
156
+ | `goBack(result?)` | 返回上一页,可携带返回数据;根页面返回 false |
157
+ | `close(result?)` | 请求关闭弹窗 |
158
+ | `setLoading(value,text?)` | 设置 Loading |
159
+ | `canGoBack` | 是否有上一页 |
160
+ | `stack` | 当前路由栈 |
161
+ | `currentRoute` | 当前路由对象 |
162
+
163
+ `useNavigationResult(callback)` 用于接收 `goBack(result)` 返回的数据;`useIsFocused()` 用于 KeepAlive 页面焦点判断;`useFocusEffect(callback)` 用于页面聚焦副作用及清理。
164
+
165
+ ## 6. 异步业务任务
166
+
167
+ ```tsx
168
+ function SaveButton({ route }) {
169
+ const { modal } = useModal();
170
+ const { run, pending, error, clearError } = useModalTask();
171
+ const save = async () => {
172
+ const result = await run(() => api.save(route.params), { loadingText: '保存中...' });
173
+ if (result.status === 'success') await modal.confirm(result.value);
174
+ };
175
+ return <><button disabled={pending} onClick={save}>保存</button>{error && <button onClick={clearError}>重试</button>}</>;
176
+ }
177
+ ```
178
+
179
+ `run` 结果:`{status:'success',value}`、`{status:'error',error}`、`{status:'busy'}`;`useModalLoading()` 还提供 `showLoading(text)` / `hideLoading(token)`,建议使用 token 防止并发任务互相关闭 Loading。
180
+
181
+ ## 7. Promise 结果和关闭守卫
182
+
183
+ `openAsync` / `enqueueAsync` 返回判别联合类型:
184
+
185
+ - `{status:'confirmed', value, reason}`:`confirm(value)` 或携带结果的 `close(value)`。
186
+ - `{status:'dismissed', reason}`:`dismiss(reason)`、Esc、遮罩关闭、队列清除、Provider 卸载、打开被阻止。
187
+ - `{status:'replaced', reason:'replace'}`:活动弹窗被 `open()` 替换。
188
+
189
+ ```tsx
190
+ modal.openAsync({
191
+ name: 'editUser',
192
+ onBeforeClose: async ({ reason }) => {
193
+ if (reason === 'confirm') return true;
194
+ return window.confirm('确认放弃未保存修改吗?');
195
+ },
196
+ });
197
+ ```
198
+
199
+ 关闭守卫是**打开时的配置回调**,适合从外部状态源读取 dirty 状态;在 Screen 内的局部 state 需要通过业务状态管理、ref 或其他共享机制与守卫同步。回调抛错时关闭会被阻止,并通过 `onError` 上报。
200
+
201
+ ## 8. 使用建议与边界
202
+
203
+ 1. `open` 是立即打开/替换;`enqueue` 是顺序打开。队列属于单个 Provider,不跨 Provider 共享。
204
+ 2. `update` 只改变弹窗选项,不更新路由参数;使用 `navigation.setParams` 更新 Screen 参数。
205
+ 3. `onBeforeClose` 为异步时会串行化关闭/替换操作;请避免在守卫内部再次等待同一弹窗关闭。
206
+ 4. 组件使用 Portal 到 `document.body`;SSR 不应在服务端尝试直接渲染已打开的弹窗。默认样式运行时注入,在严格 CSP 环境下需要额外处理 `style-src` 策略。
207
+
208
+
209
+
210
+ ## useModalUnsavedChanges(dirty, options?)
211
+
212
+ | 参数 | 类型 | 默认值 | 说明 |
213
+ |---|---|---|---|
214
+ | dirty | boolean | 必填 | 当前页面是否存在未保存修改 |
215
+ | options.message | string | 有未保存的修改,确定放弃吗? | 默认确认文案 |
216
+ | options.onBeforeDiscard | (context) => boolean \| Promise<boolean> | 未设置 | 当前页面自定义拦截,返回 false 阻止关闭 |
217
+
218
+ 检查时机:`close`、`confirm`、`dismiss`、ESC、点击遮罩、打开新弹窗替换当前弹窗。支持异步守卫和 KeepAlive 页面。保护生效时,`confirm()` 同样会先检查未保存状态;保存成功后应先清除 dirty 标志。
219
+
220
+ `ModalProvider.confirmDiscard(context)` 为全局异步确认函数,未指定时使用原生 `window.confirm`。如果需要完全自定义的确认 UI,可在回调中打开宿主应用自己的确认组件;不要使用同一个 Provider 的 `modal.open()`,否则会与正在执行的关闭守卫冲突。
221
+
222
+ ## 动画配置
223
+
224
+ | 属性 | 类型 | 默认 | 说明 |
225
+ |---|---|---|---|
226
+ | animation | boolean | true | 开关进入和退出动画 |
227
+ | animationDuration | number | 220 | 毫秒,关闭等待最多 1000ms |
228
+
229
+ 动画位置自动匹配 `placement`,左右 Drawer 保持 5px 缝隙,使用 transform 动画以避免布局抖动。遵循系统减少动态效果设置。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-modal-navigation",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
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",
@@ -18,7 +18,9 @@
18
18
  "files": [
19
19
  "dist",
20
20
  "README.md",
21
- "LICENSE"
21
+ "LICENSE",
22
+ "docs",
23
+ "CHANGELOG.md"
22
24
  ],
23
25
  "sideEffects": false,
24
26
  "scripts": {