Scan your whole stack
From OX Research Newsroom
Are you using react-modal-navigation?
Find out what’s in your code.
Scan your apps free in minutes (opens in a new tab)Latest version: v1.3.1
react-modal-navigation was last scanned on 30 Nov 2026 at version 1.3.1. Risk score 0 of 100. No malicious behavior was detected.
| Version | Published | Status |
|---|---|---|
| 1.3.1 LATEST | 30 Nov 2026 | No rules fired |
| 1.3.0 | 26 Nov 2026 | No rules fired |
| 1.2.1 | 26 Nov 2026 | No rules fired |
| 1.2.0 | 26 Nov 2026 | Not shown |
| 1.1.11 | 25 Nov 2026 | Not shown |
Findings for v1.3.1. The score is a capped sum of the weights of the rules that fired — not CVSS, not exploitability, and not a probability. It is not comparable across packages with different scan coverage.
The score describes 1.3.1; other versions have their own rules, not their own score.
The publisher’s own README, as collected from the registry.
React 19 多页面弹窗导航组件库。支持 Modal / Drawer / Sheet、页面堆栈、KeepAlive、Promise 返回结果、异步关闭守卫、Footer、Loading、暗黑主题与自动注入样式。
默认自动注入样式,无需手动导入 CSS 文件。
支持嵌套路由、相对路径、共享父级参数、持久化 Layout 和组级守卫。详见 嵌套路由指南。共享 Layout 仍随实际缓存页面挂载,不提供独立挂载保证。
npm install react-modal-navigation
import { ModalProvider, useModal } from 'react-modal-navigation';
function Home() {
const { modal } = useModal();
return <>
<button onClick={() => modal.open('editUser')}>居中编辑</button>
<button onClick={() => modal.open('deviceDrawer')}>右侧设备抽屉</button>
</>;
}
function UserForm() {
const { navigation } = useModal();
return <button onClick={() => navigation.close({ id: 1, name: 'Ada' })}>完成</button>;
}
function DeviceHome() {
const { navigation } = useModal();
return <button onClick={() => navigation.navigate('DeviceDetail', { id: 123 })}>查看详情</button>;
}
function DeviceDetail() {
const { navigation } = useModal();
return <button onClick={() => navigation.goBack()}>返回</button>;
}
const registry = {
editUser: {
initialRoute: 'UserForm',
modalOptions: { placement: 'center', width: 600, maxHeight: '85dvh' },
screens: { UserForm: { title: '编辑用户', component: UserForm } },
},
deviceDrawer: {
initialRoute: 'DeviceHome',
modalOptions: { placement: 'right', width: 520 },
screens: {
DeviceHome: { title: '设备', component: DeviceHome },
DeviceDetail: { title: '设备详情', component: DeviceDetail },
},
},
};
export default function App() {
return <ModalProvider registry={registry}><Home /></ModalProvider>;
}
不需要 import 'react-modal-navigation/style.css',Provider 会自动注入组件样式;业务页面可以使用任意 CSS 方案。
modal.open() 和 modal.openAsync() 现在都支持字符串位置参数及对象参数两种写法,原有代码不需要修改。推荐新项目采用对象形式,避免为了设置位置而传入无意义的 {}。
const { modal } = useModal();
// 推荐:只有名称和布局配置,不需要空的 params
await modal.open({
name: 'editUser',
options: { placement: 'bottom', width: '100%', height: '55dvh' },
});
// 带页面参数
await modal.open({
name: 'editUser',
params: { userId: 1001 },
options: { placement: 'center', width: 640 },
});
// Promise 弹窗也支持同样的对象形式
const result = await modal.openAsync({
name: 'editUser',
params: { userId: 1001 },
options: { placement: 'right', width: 520 },
});
// 旧 API 保持兼容
await modal.open('editUser');
await modal.open('editUser', { userId: 1001 });
await modal.open('editUser', {}, { placement: 'bottom', height: '55dvh' });
对象字段说明:
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
name |
string |
是 | registry 中注册的弹窗名称 |
params |
unknown |
否 | 传给初始 Screen 的页面参数,默认 {} |
options |
ModalOptions |
否 | 当前弹窗覆盖的布局、尺寸、主题和事件配置 |
兼容规则: 第一个参数是字符串时继续使用 open(name, params?, overrides?);第一个参数是对象时使用 open({ name, params?, options? })。对象形式不需要再传第二、第三个参数。配置优先级仍为 defaultOptions → modalOptions → options。openAsync 的结果结构不变。
| placement | 说明 | 默认高度 | 推荐场景 |
|---|---|---|---|
center |
居中弹窗 | 内容高度,受 maxHeight 限制 | 表单、确认框、详情 |
left |
左侧抽屉 | 视口高度减去上下缝隙 | 导航、筛选 |
right |
右侧抽屉(默认) | 视口高度减去上下缝隙 | 设备设置、编辑页 |
top |
顶部弹出层 | 内容高度 | 顶部操作面板 |
bottom |
底部弹出层 | 内容高度 | 移动端操作、筛选 |
modal.open('deviceDrawer', { deviceId: 123 }, {
placement: 'right',
width: 520,
viewportGap: 5,
});
左右抽屉的高度固定为 calc(100dvh - 10px)(viewportGap: 5 时),即距视口顶部、底部各 5px;同时距左右视口边缘至少 5px。无需传 height: '100dvh',即使传入 height,左右抽屉仍遵循视口高度规则。
// 左侧抽屉
modal.open('deviceDrawer', {}, { placement: 'left', width: '35vw' });
// 居中弹窗
modal.open('editUser', {}, {
placement: 'center', width: 680, height: '70dvh', maxHeight: '85dvh',
});
// 底部 Sheet
modal.open('editUser', {}, {
placement: 'bottom', width: '100%', height: '55dvh',
});
// 顶部 Sheet
modal.open('editUser', {}, {
placement: 'top', width: 800, height: 320,
});
size 仅控制宽度,可选 sm(400px)、md(560px)、lg(720px)、xl(960px)、full(100%)。显式 width 优先于 size。不传时默认宽度 460px。
modal.open('editUser', {}, { placement: 'center', size: 'lg' });
modal.open('editUser', {}, { placement: 'center', size: 'lg', width: 640 }); // width 优先
| 属性 | 类型 | 默认 | 作用 |
|---|---|---|---|
placement |
center \| left \| right \| top \| bottom |
right |
弹窗位置 |
size |
sm \| md \| lg \| xl \| full |
未设置 | 预设宽度 |
width |
number \| string |
460 |
弹窗宽度,数字单位 px |
height |
number \| string |
自动 | 居中/顶部/底部高度;左右抽屉强制视口高度 |
maxWidth |
number \| string |
同 width | 最大宽度 |
maxHeight |
number \| string |
视口减去缝隙 | 最大高度;左右抽屉强制视口高度 |
viewportGap |
number |
5 |
视口四周留白,单位 px |
borderRadius |
number \| string |
12 |
圆角 |
mobileFullscreen |
boolean |
false |
宽度 ≤640px 时铺满屏幕,覆盖缝隙规则 |
视口边界使用 100dvh(动态视口高度),适应移动端浏览器地址栏;内容区域单独滚动,Header / Footer 不跟随内容滚动。viewportGap 应使用有限的非负数字。
从低到高:ModalProvider.defaultOptions → registry[name].modalOptions → modal.open(name, params, overrides)。
<ModalProvider registry={registry} defaultOptions={{ placement: 'center', size: 'md', viewportGap: 5 }}>
<App />
</ModalProvider>
const registry = {
users: {
initialRoute: 'List',
modalOptions: { placement: 'right', width: 520 },
screens: { List: { title: '用户列表', component: UserList } },
},
};
modal.open('users'); // 默认右侧 520px
modal.open('users', {}, { placement: 'center', width: 700 }); // 本次临时居中
modal.open('users', {}, { mobileFullscreen: true }); // 手机端全屏
注意:mobileFullscreen: true 在宽度 ≤640px 时会覆盖 placement 与 viewportGap,故不再保留 5px 缝隙;需要缝隙时不要启用此项。
function UserList() {
const { navigation } = useModal();
return <button onClick={() => navigation.navigate('UserDetail', { id: 8 })}>详情</button>;
}
function UserDetail() {
const { navigation } = useModal();
const id = navigation.currentRoute?.params?.id;
return <>
<p>用户 ID:{id}</p>
<button onClick={() => navigation.setParams({ tab: 'settings' })}>切换标签</button>
<button onClick={() => navigation.goBack({ updated: true })}>返回</button>
</>;
}
navigate 进入路由,goBack(result?) 返回上一页,replace 替换当前页,reset 重置栈,setParams 合并当前页参数。隐藏页面 KeepAlive 保留状态;建议用 useFocusEffect 管理轮询和订阅。
| 方法 | 使用示例 | 行为 |
|---|---|---|
navigate |
navigation.navigate('UserDetail', { userId: 1 }) |
跳转;已有同名页面则复用并截断后续页面 |
goBack |
navigation.goBack({ saved: true }) |
返回上一页并传回结果;根页面返回 false |
replace |
navigation.replace('Success', { id: 1 }) |
替换当前页面 |
reset |
navigation.reset('UserList') |
重置为新的根页面 |
setParams |
navigation.setParams({ tab: 'info' }) |
浅合并当前页面参数 |
close |
await navigation.close({ id: 1 }) |
关闭整个弹窗,不是返回上一页 |
navigation.canGoBack 判断能否返回;navigation.currentRoute 获取当前页面 { key, name, params };navigation.stack 获取完整导航栈。上一页使用 useNavigationResult(callback) 接收 goBack(result) 的结果,KeepAlive 页面使用 useIsFocused() / useFocusEffect() 管理焦点副作用。
完整的逐项说明、栈变化示例、返回值和注意事项请阅读 API 参考手册 · 第 5 章:页面导航。
const { modal } = useModal();
const result = await modal.openAsync('editUser');
if (result.status === 'confirmed') console.log(result.value);
else console.log(result.status, result.reason);
弹窗内部:await navigation.close({ id: 1 }) 返回确认结果。modal.open() 返回 Promise<number | null>(弹窗实例 ID 或阻止打开时 null),不是 openAsync 的业务结果。
modalOptions: {
onBeforeClose: async ({ reason }) => {
if (hasUnsavedChanges()) return window.confirm('未保存,确定关闭吗?');
return true;
},
onOpen: ctx => console.log('opened', ctx),
onClose: ctx => console.log('closed', ctx.reason),
onNavigate: ctx => console.log('navigated', ctx),
onFocus: ctx => console.log('focused', ctx),
onBlur: ctx => console.log('blurred', ctx),
}
关闭被拒绝时弹窗继续存在。替换、取消和 Provider 卸载等情况,openAsync() 返回不同状态与 reason。不要将 onClose 当作弹窗仍处于活动状态的回调。
import { useModal, useModalTask, useModalLoading } from 'react-modal-navigation';
function SaveScreen() {
const { navigation } = useModal();
const { run, pending, error } = useModalTask();
const save = async () => {
const result = await run(() => Promise.resolve({ id: 10 }), { loadingText: '保存中...' });
if (result.status === 'success') await navigation.close(result.value);
};
return <>
{error && <p role="alert">保存失败,请重试</p>}
<button disabled={pending} onClick={save}>保存</button>
</>;
}
useModalLoading().showLoading() 返回任务 token,使用 hideLoading(token) 结束对应任务;多个任务并发不会相互误关。useModalTask 阻止同一 Hook 的重复提交,但不会自动取消网络请求。
<ModalProvider registry={registry} theme="auto"
backgroundColor={{ light: '#fff', dark: '#334155' }}>
<App />
</ModalProvider>
不需要导入库的 CSS。已有 className、bodyClassName、headerClassName、footerClassName 可供业务样式覆盖;也可在业务项目中覆盖 --pmn-surface、--pmn-text、--pmn-border 等 CSS 变量。自动注入 CSS 时请注意 CSS 优先级。若站点 CSP 禁止内联样式,可改用 react-modal-navigation/style.css 静态导入,并按需配置 CSP。
closeOnOverlay 默认 false;closeOnEsc 默认 true。role="dialog"、aria-modal、Tab 焦点约束、关闭后焦点恢复。open() 或存在异步关闭守卫,建议 await 以避免竞态。document。严格 CSP 环境可能需要改为静态 CSS。公开类型位于 types/index.d.ts,包括 ModalOptions、ModalRegistry、ModalNavigation、ModalResult 等。
import type { ModalRegistry } from 'react-modal-navigation';
const registry: ModalRegistry = {
demo: { initialRoute: 'Main', modalOptions: { placement: 'right', size: 'lg' }, screens: { Main: { component: Main } } },
};
支持 modal.open({ name, params, options }) 和 modal.openAsync({ name, params, options }),字符串形式继续兼容。
对象调用现在还支持将布局配置直接放在顶层,options 不再是必需的:
const { modal } = useModal();
modal.open({ name: 'editUser', placement: 'bottom', width: '100%', height: '55dvh' });
modal.open({ name: 'editUser', params: { userId: 1001 }, placement: 'right', width: 520 });
// 原有嵌套 options 形式仍然可用
modal.open({ name: 'editUser', options: { placement: 'center', width: 640 } });
两种对象写法都适用于 modal.openAsync()。若同时提供顶层布局参数和 options,options 中的同名配置优先。字符串形式 modal.open('editUser', params, options) 保持兼容。
新增 modal.confirm(value) 和 modal.dismiss(reason),使 Promise 结果语义更加清晰,尤其是确认时没有返回值的场景。
function EditUser() {
const { modal } = useModal();
return <>
<button onClick={() => modal.confirm({ saved: true })}>保存并关闭</button>
<button onClick={() => modal.confirm()}>仅确认,不返回数据</button>
<button onClick={() => modal.dismiss('cancel')}>取消</button>
</>;
}
const result = await modal.openAsync({ name: 'editUser', placement: 'right', width: 520 });
if (result.status === 'confirmed') {
// result.value 允许为 undefined,仍属于 confirmed
} else if (result.status === 'dismissed') {
// result.reason 可能是 cancel、api、escape 等
}
confirm 和 dismiss 均遵守 onBeforeClose 异步关闭守卫;返回 Promise<boolean>,表示是否实际关闭。原有 modal.close(result) 保持不变:传入值视为确认,不传值视为取消。
Find threats in your stack.
OX Radar can check high-priority threats, like critical package CVEs, against your context.
Each incident is marked Detected or Not Detected with a remediation plan and proof all tracked in the Response Center.
The probe returned only these fields for this server — auth requirement, protocol and tool list weren’t observed, so they’re not shown (never guessed).
The publisher’s own links, as the registry reports them. These are not sources for the assessment on this page.