react-modal-navigation 1.0.1 → 1.1.1
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 +18 -0
- package/README.md +105 -5
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +29 -1
- package/dist/index.js +490 -414
- package/dist/style.css +28 -0
- package/docs/API.md +229 -0
- package/package.json +4 -2
package/dist/style.css
CHANGED
|
@@ -29,3 +29,31 @@
|
|
|
29
29
|
@media (prefers-reduced-motion: reduce) {
|
|
30
30
|
.pmn-spinner { animation-duration: 2s; }
|
|
31
31
|
}
|
|
32
|
+
|
|
33
|
+
/* Placement-aware enter/exit transitions. No layout jumps, reduced-motion safe. */
|
|
34
|
+
@keyframes pmn-fade-in { from { opacity:0 } to { opacity:1 } }
|
|
35
|
+
@keyframes pmn-fade-out { from { opacity:1 } to { opacity:0 } }
|
|
36
|
+
@keyframes pmn-scale-in { from { opacity:0; transform:translateY(10px) scale(.97) } to { opacity:1; transform:none } }
|
|
37
|
+
@keyframes pmn-scale-out { from { opacity:1; transform:none } to { opacity:0; transform:translateY(10px) scale(.97) } }
|
|
38
|
+
@keyframes pmn-right-in { from { transform:translateX(105%) } to { transform:none } }
|
|
39
|
+
@keyframes pmn-right-out { from { transform:none } to { transform:translateX(105%) } }
|
|
40
|
+
@keyframes pmn-left-in { from { transform:translateX(-105%) } to { transform:none } }
|
|
41
|
+
@keyframes pmn-left-out { from { transform:none } to { transform:translateX(-105%) } }
|
|
42
|
+
@keyframes pmn-top-in { from { transform:translateY(-105%) } to { transform:none } }
|
|
43
|
+
@keyframes pmn-top-out { from { transform:none } to { transform:translateY(-105%) } }
|
|
44
|
+
@keyframes pmn-bottom-in { from { transform:translateY(105%) } to { transform:none } }
|
|
45
|
+
@keyframes pmn-bottom-out { from { transform:none } to { transform:translateY(105%) } }
|
|
46
|
+
.pmn-entering{animation:pmn-fade-in var(--pmn-animation-duration) ease-out both}
|
|
47
|
+
.pmn-exiting{animation:pmn-fade-out var(--pmn-animation-duration) ease-in both;pointer-events:none}
|
|
48
|
+
.pmn-entering .pmn-dialog{animation:pmn-scale-in var(--pmn-animation-duration) cubic-bezier(.2,.8,.2,1) both}
|
|
49
|
+
.pmn-exiting .pmn-dialog{animation:pmn-scale-out var(--pmn-animation-duration) ease-in both}
|
|
50
|
+
.pmn-entering.pmn-place-right .pmn-dialog{animation-name:pmn-right-in}
|
|
51
|
+
.pmn-exiting.pmn-place-right .pmn-dialog{animation-name:pmn-right-out}
|
|
52
|
+
.pmn-entering.pmn-place-left .pmn-dialog{animation-name:pmn-left-in}
|
|
53
|
+
.pmn-exiting.pmn-place-left .pmn-dialog{animation-name:pmn-left-out}
|
|
54
|
+
.pmn-entering.pmn-place-top .pmn-dialog{animation-name:pmn-top-in}
|
|
55
|
+
.pmn-exiting.pmn-place-top .pmn-dialog{animation-name:pmn-top-out}
|
|
56
|
+
.pmn-entering.pmn-place-bottom .pmn-dialog{animation-name:pmn-bottom-in}
|
|
57
|
+
.pmn-exiting.pmn-place-bottom .pmn-dialog{animation-name:pmn-bottom-out}
|
|
58
|
+
.pmn-no-animation,.pmn-no-animation .pmn-dialog{animation:none!important}
|
|
59
|
+
@media(prefers-reduced-motion:reduce){.pmn-entering,.pmn-exiting,.pmn-entering .pmn-dialog,.pmn-exiting .pmn-dialog{animation:none!important}}
|
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.
|
|
3
|
+
"version": "1.1.1",
|
|
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": {
|