主题
布局与主题
弹窗位置与尺寸
placement 可选值
| placement | 说明 | 默认高度 | 推荐场景 |
|---|---|---|---|
center | 居中弹窗 | 内容高度,受 maxHeight 限制 | 表单、确认框、详情 |
left | 左侧抽屉 | 视口高度减去上下缝隙 | 导航、筛选 |
right | 右侧抽屉(默认) | 视口高度减去上下缝隙 | 设备设置、编辑页 |
top | 顶部弹出层 | 内容高度 | 顶部操作面板 |
bottom | 底部弹出层 | 内容高度 | 移动端操作、筛选 |
右侧抽屉:上下左右与视口保持 5px
jsx
modal.open('deviceDrawer', { deviceId: 123 }, {
placement: 'right',
width: 520,
viewportGap: 5,
});左右抽屉的高度固定为 calc(100dvh - 10px)(viewportGap: 5 时),即距视口顶部、底部各 5px;同时距左右视口边缘至少 5px。无需传 height: '100dvh',即使传入 height,左右抽屉仍遵循视口高度规则。
jsx
// 左侧抽屉
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。
jsx
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)。
jsx
<ModalProvider registry={registry} defaultOptions={{ placement: 'center', size: 'md', viewportGap: 5 }}>
<App />
</ModalProvider>jsx
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 缝隙;需要缝隙时不要启用此项。
主题与样式覆盖
jsx
<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。
响应式、全屏与拖动调整
js
await modal.open('editUser', {}, {
placement: 'right', width: 520, resizable: true, minWidth: 320,
fullscreenable: true,
responsive: { sm: { width: '100%', placement: 'bottom' }, lg: { width: 640 } },
});
modal.setOptions({ width: 720 });
modal.setFullscreen(true);
modal.toggleFullscreen();响应式断点:sm 小于 768px,md 为 768–1023px,lg 为 1024–1279px,xl 为 1280px 及以上。useModalFullscreen() 提供全屏状态和命令。top、bottom、left、right 可设置位置偏移。
动画与关闭行为
js
const options = {
animation: true, animationDuration: 220,
pageAnimation: true, pageAnimationDuration: 260, pageAnimationDistance: 100,
closeOnOverlay: false, closeOnEsc: true, lockCloseWhileLoading: true,
ariaLabel: '编辑用户',
};动画时长使用毫秒,页面位移使用数值配置。弹窗支持焦点管理、背景滚动锁定及减少动态效果偏好。完整选项见 类型声明。