react-modal-navigation 1.1.2 → 1.1.3

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 CHANGED
@@ -16,3 +16,5 @@
16
16
  - `useModalUnsavedChanges` 提供未保存表单保护。
17
17
  - 根据弹窗位置匹配进入与退出动画,并支持减少动态效果设置。
18
18
  - 提供 TypeScript 类型声明和中文 API 参考文档。
19
+
20
+ - 支持通过 `modal.open({ name, screen, params })` 指定初始页面,兼容异步与队列调用。
package/README.md CHANGED
@@ -229,6 +229,21 @@ function UserDetail() {
229
229
 
230
230
  `navigate` 进入路由,`goBack(result?)` 返回上一页,`replace` 替换当前页,`reset` 重置栈,`setParams` 合并当前页参数。隐藏页面 KeepAlive 保留状态;建议用 `useFocusEffect` 管理轮询和订阅。
231
231
 
232
+ ### 页面跳转方法速查
233
+
234
+ | 方法 | 使用示例 | 行为 |
235
+ |---|---|---|
236
+ | `navigate` | `navigation.navigate('UserDetail', { userId: 1 })` | 跳转;已有同名页面则复用并截断后续页面 |
237
+ | `goBack` | `navigation.goBack({ saved: true })` | 返回上一页并传回结果;根页面返回 `false` |
238
+ | `replace` | `navigation.replace('Success', { id: 1 })` | 替换当前页面 |
239
+ | `reset` | `navigation.reset('UserList')` | 重置为新的根页面 |
240
+ | `setParams` | `navigation.setParams({ tab: 'info' })` | 浅合并当前页面参数 |
241
+ | `close` | `await navigation.close({ id: 1 })` | 关闭整个弹窗,不是返回上一页 |
242
+
243
+ `navigation.canGoBack` 判断能否返回;`navigation.currentRoute` 获取当前页面 `{ key, name, params }`;`navigation.stack` 获取完整导航栈。上一页使用 `useNavigationResult(callback)` 接收 `goBack(result)` 的结果,KeepAlive 页面使用 `useIsFocused()` / `useFocusEffect()` 管理焦点副作用。
244
+
245
+ 完整的逐项说明、栈变化示例、返回值和注意事项请阅读 **[API 参考手册 · 第 5 章:页面导航](docs/API.md#5-页面导航-api完整指南)**。
246
+
232
247
  ## Promise 结果与关闭守卫
233
248
 
234
249
  ```jsx
@@ -405,3 +420,42 @@ modal.open({ name: 'quickInfo', placement: 'center', animation: false });
405
420
  ```
406
421
 
407
422
  `animationDuration` 单位毫秒;系统启用 `prefers-reduced-motion: reduce` 时自动关闭动画。退出动画结束后才触发 `onClose` 和队列中的下一个弹窗。
423
+
424
+ ## 打开时指定初始页面
425
+
426
+ 在同一个弹窗中注册多个页面,默认使用 `initialRoute`。通过 `screen` 可以在打开时覆盖初始页面,不需要先打开列表再导航。
427
+
428
+ ```jsx
429
+ const registry = {
430
+ userModal: {
431
+ initialRoute: 'UserList',
432
+ screens: {
433
+ UserList: { component: UserList, title: '用户列表' },
434
+ UserDetail: { component: UserDetail, title: '用户详情' },
435
+ UserEdit: { component: UserEdit, title: '编辑用户' },
436
+ },
437
+ },
438
+ };
439
+
440
+ modal.open({ name: 'userModal' }); // 打开 UserList
441
+ modal.open({
442
+ name: 'userModal',
443
+ screen: 'UserDetail',
444
+ params: { userId: 1001 },
445
+ placement: 'right',
446
+ width: 600,
447
+ });
448
+
449
+ // Promise 和队列 API 也支持 screen
450
+ const result = await modal.openAsync({
451
+ name: 'userModal',
452
+ screen: 'UserEdit',
453
+ params: { userId: 1001 },
454
+ });
455
+ ```
456
+
457
+ - `screen` 必须是 `registry[name].screens` 中注册的页面名称。
458
+ - 省略 `screen` 时使用 `initialRoute`;`params` 始终传给选中的初始页面。
459
+ - 未注册的页面会抛出错误,不会静默回退。
460
+ - 传统字符串调用 `modal.open('userModal', params, options)` 保持不变。
461
+ - `screen` 只在打开时生效,打开后切换页面请使用 `navigation.navigate()`。
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("react/jsx-runtime"),t=require("react"),Ne=require("react-dom"),Re=t.createContext(null),be=t.createContext(null),de=t.createContext(null),ye=t.createContext(null),Ee=t.createContext(null);let Le=0;const ge=(e,n)=>({key:`modal-route-${++Le}`,name:e,params:n??{}}),Me=(e,n)=>[ge(e,n)];function Te(e,n){switch(n.type){case"RESET":return Me(n.name,n.params);case"NAVIGATE":{const o=e.findIndex(c=>c.name===n.name);return o<0?[...e,ge(n.name,n.params)]:e.slice(0,o+1).map((c,l)=>l===o?{...c,params:n.params??{}}:c)}case"SET_PARAMS":return e.map((o,c)=>c===e.length-1?{...o,params:{...o.params??{},...n.params??{}}}:o);case"BACK":return e.length>1?e.slice(0,-1):e;case"REPLACE":return e.length?[...e.slice(0,-1),ge(n.name,n.params)]:e;default:return e}}const we=()=>{if(typeof document>"u")return"light";const e=document.documentElement,n=document.body;return e.classList.contains("dark")||n.classList.contains("dark")||e.dataset.theme==="dark"||n.dataset.theme==="dark"?"dark":e.classList.contains("light")||n.classList.contains("light")||e.dataset.theme==="light"||n.dataset.theme==="light"?"light":window.matchMedia?.("(prefers-color-scheme: dark)").matches?"dark":"light"};let ie=0;const Z=[];let pe=null;function Be(){if(ie++,ie===1){const n=document.documentElement,o=document.body,c={html:n.style.overflow,body:o.style.overflow,padding:o.style.paddingRight,width:o.style.getPropertyValue("--modal-scrollbar-width")},l=Math.max(0,window.innerWidth-n.clientWidth),h=parseFloat(getComputedStyle(o).paddingRight)||0;l&&(o.style.paddingRight=`${h+l}px`),o.style.setProperty("--modal-scrollbar-width",`${l}px`),n.style.overflow="hidden",o.style.overflow="hidden",pe=()=>{n.style.overflow=c.html,o.style.overflow=c.body,o.style.paddingRight=c.padding,c.width?o.style.setProperty("--modal-scrollbar-width",c.width):o.style.removeProperty("--modal-scrollbar-width")}}let e=!1;return()=>{e||(e=!0,ie=Math.max(0,ie-1),ie||(pe?.(),pe=null))}}const Pe='a[href],button:not([disabled]),input:not([disabled]),textarea:not([disabled]),select:not([disabled]),[tabindex]:not([tabindex="-1"])',Fe=e=>!e.closest('[inert], [hidden], [aria-hidden="true"]')&&e.getClientRects().length>0&&getComputedStyle(e).visibility!=="hidden"&&getComputedStyle(e).display!=="none";function qe({dialogRef:e,options:n,requestClose:o,children:c,exiting:l=!1}){const h=t.useRef(null),y=t.useRef(null),v=t.useRef(o);v.current=o;const f=t.useRef(n.closeOnEsc);f.current=n.closeOnEsc;const[S,k]=t.useState(we),g=n.theme==="light"||n.theme==="dark"?n.theme:S;t.useEffect(()=>{if(n.theme==="light"||n.theme==="dark")return;const T=()=>k(we()),G=new MutationObserver(T);for(const N of[document.documentElement,document.body])G.observe(N,{attributes:!0,attributeFilter:["class","data-theme"]});const w=window.matchMedia?.("(prefers-color-scheme: dark)");return w?.addEventListener?.("change",T),T(),()=>{G.disconnect(),w?.removeEventListener?.("change",T)}},[n.theme]),t.useEffect(()=>{y.current=document.activeElement;const T=Be(),G=h.current;Z.push(G),e.current?.focus({preventScroll:!0});const w=E=>{const x=e.current;if(!x||Z.at(-1)!==G)return;if(E.key==="Escape"&&f.current!==!1){E.preventDefault(),v.current("escape");return}if(E.key!=="Tab")return;const L=[...x.querySelectorAll(Pe)].filter(Fe);if(!L.length){E.preventDefault(),x.focus();return}const I=L[0],M=L.at(-1);x.contains(document.activeElement)?E.shiftKey&&(document.activeElement===I||document.activeElement===x)?(E.preventDefault(),M.focus()):!E.shiftKey&&(document.activeElement===M||document.activeElement===x)&&(E.preventDefault(),I.focus()):(E.preventDefault(),(E.shiftKey?M:I).focus())},N=E=>{Z.at(-1)===G&&e.current&&!e.current.contains(E.target)&&e.current.focus({preventScroll:!0})};return document.addEventListener("keydown",w),document.addEventListener("focusin",N),()=>{document.removeEventListener("keydown",w),document.removeEventListener("focusin",N);const E=Z.at(-1)===G,x=Z.indexOf(G);if(x!==-1&&Z.splice(x,1),T(),E){const L=y.current;L?.isConnected&&!L.closest("[inert], [hidden]")?L.focus?.({preventScroll:!0}):Z.at(-1)?.querySelector("[role=dialog]")?.focus?.({preventScroll:!0})}}},[e]);const m=n.backgroundColor??n.backgroudColor,p=typeof m=="string"?m:m?.[g],b=typeof p=="string"&&/^(#|rgb\(|rgba\(|hsl\(|hsla\(|oklch\(|color\(|var\()/.test(p.trim())?p:null,q={sm:400,md:560,lg:720,xl:960,full:"100%"},B=["center","left","right","top","bottom"].includes(n.placement)?n.placement:"right",P=T=>typeof T=="number"?`${T}px`:T,F=P(n.width??q[n.size]??460),C=P(n.height),A=P(n.maxHeight),R=P(n.maxWidth),O=Number(n.viewportGap??5),$=Number.isFinite(O)?Math.max(0,O):5,H=typeof n.borderRadius=="number"?`${n.borderRadius}px`:n.borderRadius??"12px";return Ne.createPortal(d.jsx("div",{ref:h,className:`pmn-overlay ${l?"pmn-exiting":"pmn-entering"} ${n.animation===!1?"pmn-no-animation":""} pmn-place-${B} ${n.mobileFullscreen?"pmn-mobile-fullscreen":""}`,style:{padding:$,"--pmn-animation-duration":`${Math.min(1e3,Math.max(0,Number(n.animationDuration??220)||0))}ms`},onMouseDown:T=>{T.target===T.currentTarget&&n.closeOnOverlay===!0&&v.current("overlay")},children:d.jsx("section",{ref:e,tabIndex:-1,role:"dialog","aria-modal":"true","aria-label":n.ariaLabel??"Dialog","data-modal-theme":g,className:`pmn-dialog ${g==="dark"?"dark":""} ${!b&&p?p:""} ${n.className??""}`,style:{"--pmn-width":F,"--pmn-gap":`${$}px`,...C!==void 0?{"--pmn-height":C}:{},...A!==void 0?{"--pmn-max-height":A}:{},...R!==void 0?{"--pmn-max-width":R}:{},borderRadius:H,...b?{backgroundColor:b}:{}},children:c})}),document.body)}function ke({back:e}){return d.jsx("svg",{"aria-hidden":"true",viewBox:"0 0 24 24",width:"20",height:"20",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e?d.jsx("path",{d:"m15 18-6-6 6-6"}):d.jsx("path",{d:"M18 6 6 18M6 6l12 12"})})}function De({options:e,screen:n,route:o,navigation:c,requestClose:l,translate:h}){if(e.showHeader===!1)return null;const y=h??(m=>m),v=n.showTitle??e.showTitle??!0,f=typeof n.title=="function"?n.title({t:y,route:o,navigation:c}):n.title??o.name,S={route:o,navigation:c,close:l,canGoBack:c.canGoBack,title:f,t:y},k=n.renderHeader??e.renderHeader;if(k)return k(S);const g=n.renderHeaderRight??e.renderHeaderRight;return d.jsxs("header",{className:`pmn-header ${n.showHeaderBorder??e.showHeaderBorder?"pmn-border-bottom":""} ${e.headerClassName??""}`,children:[e.showBackButton!==!1&&c.canGoBack&&d.jsx("button",{type:"button",className:"pmn-icon-button","aria-label":y("modal.back"),onClick:()=>c.goBack(),children:d.jsx(ke,{back:!0})}),v?d.jsx("h2",{children:f}):d.jsx("div",{className:"pmn-header-spacer"}),g?.(S),e.showCloseButton!==!1&&d.jsx("button",{type:"button",className:"pmn-icon-button","aria-label":y("modal.close"),onClick:()=>l("header"),children:d.jsx(ke,{})})]})}function Ae({screen:e,options:n,override:o,route:c,navigation:l,requestClose:h,translate:y,onError:v}){const[f,S]=t.useState(!1),k=y??(R=>R),g={...n,...e,...o},{showFooter:m,renderFooter:p,done:b,cancel:q,cancle:B}=g,P=q??B,F={route:c,navigation:l,close:h,t:k};if(m===!1)return null;const C=`pmn-footer ${g.showFooterBorder?"pmn-border-top":""} ${g.footerClassName??""}`;if(typeof p=="function")return d.jsx("footer",{className:C,children:p({...F,pending:f})});if(typeof b!="function"&&typeof P!="function")return null;const A=async R=>{if(!(f||typeof R!="function")){S(!0);try{await R(F)}catch(O){v?.(O,{phase:"footer",route:c})}finally{S(!1)}}};return d.jsxs("footer",{className:C,children:[typeof P=="function"&&d.jsx("button",{type:"button",disabled:f,onClick:()=>A(P),className:"pmn-button pmn-button-secondary",children:g.cancelText??k("modal.cancel")}),typeof b=="function"&&d.jsx("button",{type:"button",disabled:f||g.doneDisabled===!0,onClick:()=>A(b),className:"pmn-button pmn-button-primary",children:f?g.loadingText??k("modal.processing"):g.doneText??k("modal.done")})]})}function Oe({loading:e,translate:n}){if(!e.visible)return null;const c=(n??(h=>h))("modal.loading"),l=e.text??(c==="modal.loading"?"加载中...":c);return d.jsx("div",{className:"pmn-loading-overlay",role:"status","aria-live":"polite","aria-busy":"true",children:d.jsxs("div",{className:"pmn-loading-card",children:[d.jsx("span",{className:"pmn-spinner","aria-hidden":"true"}),d.jsx("span",{children:l})]})})}class $e extends t.Component{state={error:null};static getDerivedStateFromError(n){return{error:n}}componentDidCatch(n,o){this.props.onError?.(n,{phase:"render",route:this.props.route,componentStack:o.componentStack})}componentDidUpdate(n){n.route?.key!==this.props.route?.key&&this.state.error&&this.setState({error:null})}render(){return this.state.error?d.jsxs("div",{role:"alert",className:"pmn-error-screen",children:[d.jsx("strong",{children:"页面加载失败"}),d.jsx("p",{children:this.state.error.message}),d.jsx("button",{type:"button",className:"pmn-button pmn-button-secondary",onClick:()=>this.setState({error:null}),children:"重试"})]}):this.props.children}}function Ie({route:e,screen:n,navigation:o,focused:c,resultEvent:l,setFooter:h,loadingApi:y,bodyClassName:v,notifyError:f,registerDirtyGuard:S}){const k=n.component,g=t.useMemo(()=>({isFocused:c,resultEvent:l,routeKey:e.key,setFooter:h}),[c,l,e.key,h]),m=t.useMemo(()=>({isLoading:c&&y.isLoading,setLoading:(p,b)=>y.setScreenLoading(e.key,p,b),showLoading:p=>y.begin(e.key,p),hideLoading:p=>y.end(e.key,p)}),[c,y,e.key]);return d.jsx(Ee.Provider,{value:{routeKey:e.key,registerDirtyGuard:S},children:d.jsx(de.Provider,{value:g,children:d.jsx(ye.Provider,{value:m,children:d.jsx("div",{hidden:!c,inert:!c,"data-modal-route-key":e.key,tabIndex:-1,className:`pmn-body ${v??""}`,children:d.jsx($e,{route:e,onError:f,children:d.jsx(k,{route:e,navigation:o})})})})})})}function je({config:e,params:n,options:o,translate:c,onRequestClose:l,onOpened:h,registerLoading:y,notifyError:v,error:f,clearError:S,registerDirtyGuard:k,exiting:g=!1}){const[m,p]=t.useReducer(Te,null,()=>Me(e.initialRoute,n)),[b,q]=t.useState({}),[B,P]=t.useState({}),[,F]=t.useState(0),C=t.useRef(new Map),A=t.useRef(0),R=t.useRef(new Map),O=t.useRef(null),$=t.useRef(null),H=t.useRef(!1),T=t.useRef(!1),G=t.useRef(0),w=m.at(-1),N=w.key,E=t.useRef(N);E.current=N;const x=e.screens,L=t.useCallback(()=>F(r=>r+1),[]),I=t.useCallback((r,a)=>{if(r!=="global"&&E.current!==r)return null;const u=++A.current;return C.current.set(u,{owner:r,text:a}),L(),u},[L]),M=t.useCallback((r,a)=>{if(a!=null)C.current.get(a)?.owner===r&&C.current.delete(a);else for(const[u,j]of C.current)j.owner===r&&C.current.delete(u);L()},[L]),z=t.useCallback((r,a)=>{r?(M("global"),I("global",a)):M("global")},[I,M]),K=t.useCallback((r,a,u)=>{a?(M(r),I(r,u)):M(r)},[I,M]),_=[...C.current.values()],Y={visible:_.length>0,text:_.at(-1)?.text??null,count:_.length},U=t.useRef(Y);U.current=Y;const Q=t.useMemo(()=>({isLoading:Y.visible,setLoading:z,setScreenLoading:K,begin:I,end:M}),[Y.visible,z,K,I,M]),te=t.useRef(y);te.current=y,t.useEffect(()=>(te.current?.({setLoading:z}),()=>te.current?.(null)),[z]);const le=t.useCallback((r,a)=>P(u=>{if(a===null){if(!(r in u))return u;const j={...u};return delete j[r],j}return u[r]===a?u:{...u,[r]:a}}),[]),J=t.useCallback(()=>{const r=document.activeElement;if(!(r instanceof HTMLElement)||!O.current?.contains(r))return;const a=r.closest("[data-modal-route-key]");a&&R.current.set(a.dataset.modalRouteKey,r),O.current.focus({preventScroll:!0})},[]),ne=t.useCallback(r=>{if(!Object.prototype.hasOwnProperty.call(x,r))throw new Error(`[react-modal-navigation] Unknown modal screen: ${r}`)},[x]),V=t.useCallback((r,a)=>{try{Promise.resolve(o[r]?.(a)).catch(u=>v(u,{phase:r}))}catch(u){v(u,{phase:r})}},[o,v]),W=t.useCallback((r,a,u)=>{ne(a),J(),p({type:r,name:a,params:u})},[ne,J]),re=t.useCallback((r,a)=>W("NAVIGATE",r,a),[W]),oe=t.useCallback((r,a)=>W("REPLACE",r,a),[W]),ae=t.useCallback((r,a)=>{q({}),W("RESET",r,a)},[W]),se=t.useCallback(r=>{if(!r||typeof r!="object"||Array.isArray(r))throw new TypeError("[react-modal-navigation] setParams expects an object");p({type:"SET_PARAMS",params:r})},[]),ce=t.useCallback(r=>{if(m.length<=1||H.current)return!1;if(H.current=!0,r!==void 0){const a=m.at(-2);q(u=>({...u,[a.key]:{id:++G.current,data:r}}))}return J(),p({type:"BACK"}),!0},[m,J]),X=t.useCallback((r,a)=>U.current.visible&&o.lockCloseWhileLoading!==!1?Promise.resolve(!1):l(r,a,{route:w,stack:m}),[o.lockCloseWhileLoading,l,w,m]),s=t.useCallback(r=>X("api",r),[X]),i=t.useMemo(()=>({navigate:re,replace:oe,reset:ae,setParams:se,goBack:ce,close:s,setLoading:z,canGoBack:m.length>1,stack:m,currentRoute:w}),[re,oe,ae,se,ce,s,z,m,w]);return t.useLayoutEffect(()=>{H.current=!1;const r=[...O.current?.querySelectorAll("[data-modal-route-key]")??[]].find(j=>j.dataset.modalRouteKey===N);if(!r)return;const a=R.current.get(N);(a?.isConnected&&r.contains(a)&&!a.matches(":disabled")&&!a.closest("[inert], [hidden]")?a:r).focus({preventScroll:!0})},[N]),t.useEffect(()=>{let r=!1;for(const[u,j]of C.current)j.owner!=="global"&&j.owner!==N&&(C.current.delete(u),r=!0);r&&L();const a=new Set(m.map(u=>u.key));for(const u of R.current.keys())a.has(u)||R.current.delete(u)},[N,m,L]),t.useEffect(()=>{if($.current?.key!==N){if($.current){const r={from:$.current,to:w,stack:m};V("onBlur",{route:$.current,...r}),V("onNavigate",r),V("onRouteChange",r)}V("onFocus",{route:w,stack:m}),$.current=w}},[w,N,m,V]),t.useEffect(()=>()=>{C.current.clear(),R.current.clear()},[]),t.useEffect(()=>{T.current||(T.current=!0,h?.({route:w,stack:m}))},[]),d.jsx(be.Provider,{value:i,children:d.jsx(ye.Provider,{value:Q,children:d.jsxs(qe,{dialogRef:O,options:o,requestClose:X,exiting:g,children:[d.jsx(De,{options:o,screen:x[w.name],route:w,navigation:i,requestClose:X,translate:c}),d.jsx("main",{className:"pmn-main",children:m.map(r=>d.jsx(Ie,{route:r,screen:x[r.name],navigation:i,focused:r.key===N,resultEvent:b[r.key]??null,bodyClassName:o.bodyClassName,setFooter:le,loadingApi:Q,notifyError:v,registerDirtyGuard:k},r.key))}),d.jsx(Ae,{screen:x[w.name],options:o,override:B[N],route:w,navigation:i,requestClose:X,translate:c,onError:v},N),d.jsx(Oe,{loading:Y,translate:c}),f&&d.jsxs("div",{className:"pmn-error",role:"alert",children:[d.jsx("span",{children:f.message||String(f)}),d.jsx("button",{type:"button",onClick:S,"aria-label":"Dismiss error",children:"×"})]})]})})})}const He=".pmn-overlay,.pmn-dialog,.pmn-dialog *{box-sizing:border-box}.pmn-overlay{position:fixed;inset:0;z-index:1000;display:flex;overscroll-behavior:none;background:#00000073}.pmn-place-right{justify-content:flex-end;align-items:stretch}.pmn-place-left{justify-content:flex-start;align-items:stretch}.pmn-place-center{justify-content:center;align-items:center}.pmn-place-top{justify-content:center;align-items:flex-start}.pmn-place-bottom{justify-content:center;align-items:flex-end}.pmn-dialog{--pmn-surface:#fff;--pmn-body:#fff;--pmn-text:#0f172a;--pmn-muted:#475569;--pmn-border:#e2e8f0;--pmn-hover:#f1f5f9;--pmn-overlay-card:rgba(255,255,255,.92);color-scheme:light;position:relative;display:flex;flex-direction:column;min-height:0;width:100%;max-width:min(calc(100vw - var(--pmn-gap)*2),var(--pmn-max-width, var(--pmn-width)));max-height:min(calc(100dvh - var(--pmn-gap)*2),var(--pmn-max-height, 100dvh));height:var(--pmn-height, 100%);overflow:hidden;outline:none;box-shadow:0 25px 50px -12px #00000040;background:var(--pmn-surface);color:var(--pmn-text)}.pmn-place-left .pmn-dialog,.pmn-place-right .pmn-dialog{height:calc(100dvh - var(--pmn-gap)*2);max-height:calc(100dvh - var(--pmn-gap)*2)}.pmn-place-center .pmn-dialog{height:var(--pmn-height, auto);min-height:150px}.pmn-place-top .pmn-dialog,.pmn-place-bottom .pmn-dialog{height:var(--pmn-height, auto);min-height:0}.pmn-dialog[data-modal-theme=dark]{--pmn-surface:#334155;--pmn-body:#334155;--pmn-text:#f1f5f9;--pmn-muted:#cbd5e1;--pmn-border:#475569;--pmn-hover:#1e293b;--pmn-overlay-card:rgba(51,65,85,.94);color-scheme:dark}.pmn-main{min-height:0;flex:1;overflow:hidden;display:flex;flex-direction:column;background:transparent}.pmn-body{height:100%;min-height:0;overflow-y:auto;overscroll-behavior:contain;padding:16px;flex:1}.pmn-body[hidden]{display:none!important}.pmn-header{display:flex;flex-shrink:0;height:64px;align-items:center;gap:12px;padding:0 20px;background:transparent}.pmn-header h2{font-size:18px;font-weight:600;min-width:0;flex:1;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;margin:0}.pmn-header-spacer{flex:1}.pmn-border-bottom{border-bottom:1px solid var(--pmn-border)}.pmn-icon-button{display:inline-flex;align-items:center;justify-content:center;padding:8px;border:0;background:transparent;color:inherit;border-radius:8px;cursor:pointer}.pmn-icon-button:hover{background:var(--pmn-hover)}.pmn-footer{display:flex;flex-shrink:0;align-items:center;justify-content:flex-end;gap:12px;padding:16px 20px;background:transparent}.pmn-border-top{border-top:1px solid var(--pmn-border)}.pmn-button{padding:8px 16px;border-radius:8px;font-size:14px;font-weight:500;cursor:pointer}.pmn-button:disabled{opacity:.5;cursor:not-allowed}.pmn-button-secondary{border:1px solid var(--pmn-border);background:transparent;color:var(--pmn-text)}.pmn-button-secondary:hover:not(:disabled){background:var(--pmn-hover)}.pmn-button-primary{border:1px solid #2563eb;background:#2563eb;color:#fff}.pmn-button-primary:hover:not(:disabled){background:#1d4ed8}.pmn-loading-overlay{position:absolute;inset:0;z-index:50;display:flex;align-items:center;justify-content:center;background:var(--pmn-overlay-card);-webkit-backdrop-filter:blur(1px);backdrop-filter:blur(1px)}.pmn-loading-card{display:flex;flex-direction:column;align-items:center;gap:12px;max-width:85%;padding:20px 28px;border-radius:12px;background:var(--pmn-surface);box-shadow:0 10px 20px #00000026}.pmn-spinner{width:36px;height:36px;border:3px solid #2563eb;border-top-color:transparent;border-radius:50%;animation:pmn-spin 1s linear infinite}@keyframes pmn-spin{to{transform:rotate(360deg)}}.pmn-error{position:absolute;z-index:60;bottom:8px;left:12px;right:12px;display:flex;justify-content:space-between;gap:12px;padding:12px;border:1px solid #fca5a5;border-radius:8px;background:#fef2f2;color:#991b1b;font-size:13px}.pmn-error button{border:0;background:transparent;font-size:20px;cursor:pointer;color:inherit}.pmn-dialog input:not([type=checkbox]):not([type=radio]),.pmn-dialog textarea,.pmn-dialog select{color:var(--pmn-text);border-color:var(--pmn-border)}.pmn-error-screen{padding:24px;color:var(--pmn-text)}.pmn-error-screen p{color:#dc2626;overflow-wrap:anywhere}@media(max-width:640px){.pmn-overlay.pmn-mobile-fullscreen{padding:0!important}.pmn-mobile-fullscreen .pmn-dialog{max-width:100%;width:100%;height:100dvh;max-height:100dvh;border-radius:0!important}}@media(prefers-reduced-motion:reduce){.pmn-spinner{animation-duration:2s}}@keyframes pmn-fade-in{0%{opacity:0}to{opacity:1}}@keyframes pmn-fade-out{0%{opacity:1}to{opacity:0}}@keyframes pmn-scale-in{0%{opacity:0;transform:translateY(10px) scale(.97)}to{opacity:1;transform:none}}@keyframes pmn-scale-out{0%{opacity:1;transform:none}to{opacity:0;transform:translateY(10px) scale(.97)}}@keyframes pmn-right-in{0%{transform:translate(105%)}to{transform:none}}@keyframes pmn-right-out{0%{transform:none}to{transform:translate(105%)}}@keyframes pmn-left-in{0%{transform:translate(-105%)}to{transform:none}}@keyframes pmn-left-out{0%{transform:none}to{transform:translate(-105%)}}@keyframes pmn-top-in{0%{transform:translateY(-105%)}to{transform:none}}@keyframes pmn-top-out{0%{transform:none}to{transform:translateY(-105%)}}@keyframes pmn-bottom-in{0%{transform:translateY(105%)}to{transform:none}}@keyframes pmn-bottom-out{0%{transform:none}to{transform:translateY(105%)}}.pmn-entering{animation:pmn-fade-in var(--pmn-animation-duration) ease-out both}.pmn-exiting{animation:pmn-fade-out var(--pmn-animation-duration) ease-in both;pointer-events:none}.pmn-entering .pmn-dialog{animation:pmn-scale-in var(--pmn-animation-duration) cubic-bezier(.2,.8,.2,1) both}.pmn-exiting .pmn-dialog{animation:pmn-scale-out var(--pmn-animation-duration) ease-in both}.pmn-entering.pmn-place-right .pmn-dialog{animation-name:pmn-right-in}.pmn-exiting.pmn-place-right .pmn-dialog{animation-name:pmn-right-out}.pmn-entering.pmn-place-left .pmn-dialog{animation-name:pmn-left-in}.pmn-exiting.pmn-place-left .pmn-dialog{animation-name:pmn-left-out}.pmn-entering.pmn-place-top .pmn-dialog{animation-name:pmn-top-in}.pmn-exiting.pmn-place-top .pmn-dialog{animation-name:pmn-top-out}.pmn-entering.pmn-place-bottom .pmn-dialog{animation-name:pmn-bottom-in}.pmn-exiting.pmn-place-bottom .pmn-dialog{animation-name:pmn-bottom-out}.pmn-no-animation,.pmn-no-animation .pmn-dialog{animation:none!important}@media(prefers-reduced-motion:reduce){.pmn-entering,.pmn-exiting,.pmn-entering .pmn-dialog,.pmn-exiting .pmn-dialog{animation:none!important}}",Ce="react-modal-navigation-styles",he=new WeakMap;function Ge(){t.useInsertionEffect(()=>{if(typeof document>"u")return;const e=document;let n=he.get(e);if(!n){let o=e.getElementById(Ce);const c=!o;o||(o=e.createElement("style"),o.id=Ce,o.setAttribute("data-react-modal-navigation",""),o.textContent=He,e.head.appendChild(o)),n={element:o,owned:c,count:0},he.set(e,n)}return n.count+=1,()=>{n.count-=1,n.count===0&&(n.owned&&n.element.remove(),he.delete(e))}},[])}function ue(e,n,o){if(typeof e=="string"){if(!e.trim())throw new TypeError("[react-modal-navigation] modal name must not be empty");return{name:e,params:n??{},options:o??{}}}if(e===null||typeof e!="object"||Array.isArray(e))throw new TypeError("[react-modal-navigation] open expects a modal name or a configuration object");const{name:c,params:l={},options:h={},...y}=e;if(typeof c!="string"||!c.trim())throw new TypeError("[react-modal-navigation] open config requires a non-empty string name");if(h===null||typeof h!="object"||Array.isArray(h))throw new TypeError("[react-modal-navigation] open config options must be an object");return{name:c,params:l,options:{...y,...h}}}const ze={placement:"right",showHeader:!0,showBackButton:!0,showCloseButton:!0,viewportGap:5,borderRadius:12,closeOnOverlay:!1,closeOnEsc:!0},Ke=e=>({"modal.loading":"加载中...","modal.processing":"处理中...","modal.back":"返回","modal.close":"关闭","modal.cancel":"取消","modal.done":"完成"})[e]??e;function Ue({registry:e,children:n,defaultOptions:o={},backgroundColor:c,theme:l,translate:h=Ke,onError:y,confirmDiscard:v}){Ge();const[f,S]=t.useState(null),[k,g]=t.useState(null),[m,p]=t.useState(0),b=t.useRef(null),q=t.useRef(new Map),B=t.useRef([]),P=t.useRef(null),F=t.useRef(null),C=t.useRef(0),A=t.useRef(null),R=t.useRef(!1),O=t.useRef(0),$=t.useRef(null),H=t.useRef(new Map),[T,G]=t.useState(!1),w=t.useRef(v);w.current=v;const N=t.useCallback((s,i)=>(H.current.set(s,i),()=>{H.current.get(s)===i&&H.current.delete(s)}),[]),E=t.useCallback(async s=>{for(const i of H.current.values()){if(!i.dirty)continue;if((i.onBeforeDiscard?await i.onBeforeDiscard(s):w.current?await w.current(s):typeof window<"u"&&typeof window.confirm=="function"?window.confirm(i.message||"有未保存的修改,确定放弃吗?"):!1)===!1)return!1}return!0},[]),x=t.useRef(!0),L=t.useRef(null),I=t.useRef(y);I.current=y;const M=t.useCallback((s,i)=>{x.current&&g(s);try{I.current?I.current(s,i):console.error("[react-modal-navigation]",s)}catch(r){console.error("[react-modal-navigation] onError failed",r)}},[]),z=t.useCallback((s,i)=>{const r=q.current.get(s);q.current.delete(s),r?.(i)},[]),K=t.useCallback(s=>({...ze,...o,...l!==void 0?{theme:l}:{},...c!==void 0?{backgroundColor:c}:{},...s.config.modalOptions,...s.overrides}),[o,l,c]),_=f?K(f):null,Y=t.useRef(null);Y.current=_,L.current=_;const U=t.useCallback(async(s="api",i,r={})=>{const a=b.current;if(!a||A.current||R.current)return!1;A.current=a.id,R.current=!0;const u=L.current??K(a),j={...r,reason:s,result:i,name:a.name};try{if(!await E(j)||await u.onBeforeClose?.(j)===!1||b.current?.id!==a.id||!x.current||(G(!0),await new Promise(D=>{const ee=u.animation===!1||typeof window<"u"&&window.matchMedia?.("(prefers-reduced-motion: reduce)").matches?0:Math.max(0,Number(u.animationDuration??220)||0);setTimeout(D,Math.min(ee,1e3))}),!x.current||b.current?.id!==a.id))return!1;b.current=null,$.current=null,S(null),G(!1),H.current.clear(),z(a.id,r.confirmed||i!==void 0?{status:"confirmed",value:i,reason:s}:{status:"dismissed",reason:s});try{await u.onClose?.(j)}catch(D){M(D,{phase:"onClose",name:a.name})}return!0}catch(D){return M(D,{phase:"onBeforeClose",name:a.name}),!1}finally{A.current===a.id&&(A.current=null),R.current=!1,!b.current&&x.current&&queueMicrotask(()=>F.current?.())}},[K,M,z,E]),Q=t.useCallback(async(s,i={},r={},a)=>{const u=e?.[s];if(!u)throw new Error(`[react-modal-navigation] Unknown modal: ${s}`);if(!u.screens?.[u.initialRoute])throw new Error(`[react-modal-navigation] Missing initial screen '${u.initialRoute}' in '${s}'`);if(!x.current||R.current||A.current)return null;R.current=!0;const j=O.current;try{const D=b.current;if(D){const ve=L.current??K(D),me={reason:"replace",name:D.name,nextName:s};try{if(!await E(me)||await ve.onBeforeClose?.(me)===!1)return null}catch(fe){return M(fe,{phase:"onBeforeClose",name:D.name}),null}if(!x.current||j!==O.current||b.current?.id!==D.id)return null;b.current=null,$.current=null,S(null),H.current.clear(),z(D.id,{status:"replaced",reason:"replace"});try{await ve.onClose?.(me)}catch(fe){M(fe,{phase:"onClose",name:D.name})}if(!x.current||j!==O.current)return null}const ee={id:++C.current,name:s,params:i,config:u,overrides:r};return b.current=ee,$.current=null,a&&q.current.set(ee.id,a),g(null),S(ee),ee.id}finally{R.current=!1}},[e,K,M,z,E]);P.current=Q;const te=t.useCallback(async()=>{if(!x.current||b.current||R.current||A.current)return;const s=B.current.shift();if(s){p(B.current.length);try{const i=await P.current(s.args.name,s.args.params,s.args.options,s.resolveResult);i===null?(s.resolveResult?.({status:"dismissed",reason:"blocked"}),s.resolveId?.(null),queueMicrotask(()=>F.current?.())):s.resolveId?.(i)}catch(i){s.reject?.(i),queueMicrotask(()=>F.current?.())}}},[]);F.current=te;const le=t.useCallback((s,i,r)=>{const a=ue(s,i,r);return e?.[a.name]?new Promise((u,j)=>{B.current.push({args:a,resolveId:u,reject:j}),p(B.current.length),queueMicrotask(()=>F.current?.())}):Promise.reject(new Error(`[react-modal-navigation] Unknown modal: ${a.name}`))},[e]),J=t.useCallback((s,i,r)=>{const a=ue(s,i,r);return e?.[a.name]?new Promise((u,j)=>{B.current.push({args:a,resolveResult:u,reject:j}),p(B.current.length),queueMicrotask(()=>F.current?.())}):Promise.reject(new Error(`[react-modal-navigation] Unknown modal: ${a.name}`))},[e]),ne=t.useCallback(s=>{if(!s||typeof s!="object"||Array.isArray(s))throw new TypeError("[react-modal-navigation] update expects an options object");const i=b.current;if(!i||R.current)return!1;const r={...i,overrides:{...i.overrides,...s}};return b.current=r,S(r),L.current=K(r),!0},[K]),V=t.useCallback((s="queue-cleared")=>{const i=B.current.splice(0);p(0);for(const r of i)r.resolveId?.(null),r.resolveResult?.({status:"dismissed",reason:s});return i.length},[]),W=t.useCallback((s,i,r)=>{const a=ue(s,i,r);return Q(a.name,a.params,a.options)},[Q]),re=t.useCallback((s,i,r)=>{const a=ue(s,i,r);return new Promise((u,j)=>{Q(a.name,a.params,a.options,u).then(D=>{D===null&&u({status:"dismissed",reason:"blocked"})},j)})},[Q]),oe=t.useCallback(s=>U("api",s),[U]),ae=t.useCallback((s="api")=>U(s),[U]),se=t.useCallback(s=>U("confirm",s,{confirmed:!0}),[U]),ce=t.useCallback((s,i)=>$.current?.setLoading(s,i),[]),X=t.useMemo(()=>({open:W,openAsync:re,enqueue:le,enqueueAsync:J,update:ne,clearQueue:V,close:oe,dismiss:ae,confirm:se,setLoading:ce,queueSize:m,isOpen:!!f,activeName:f?.name??null,error:k,clearError:()=>g(null)}),[W,re,le,J,ne,V,oe,ae,se,ce,f,k,m]);return t.useEffect(()=>(x.current=!0,()=>{x.current=!1,O.current+=1,b.current=null;for(const i of q.current.values())i({status:"dismissed",reason:"unmount"});q.current.clear(),H.current.clear();const s=B.current.splice(0);for(const i of s)i.resolveId?.(null),i.resolveResult?.({status:"dismissed",reason:"unmount"})}),[]),d.jsxs(Re.Provider,{value:X,children:[n,f&&d.jsx(je,{config:f.config,params:f.params,options:_,translate:h,notifyError:M,error:k,clearError:()=>g(null),registerLoading:s=>{$.current=s},registerDirtyGuard:N,exiting:T,onRequestClose:U,onOpened:s=>{try{const i=Y.current?.onOpen?.(s);Promise.resolve(i).catch(r=>M(r,{phase:"onOpen"}))}catch(i){M(i,{phase:"onOpen"})}}},f.id)]})}function We(){const e=t.useContext(Re),n=t.useContext(be);if(!e)throw new Error("[react-modal-navigation] useModal requires <ModalProvider>");return t.useMemo(()=>{const o=()=>{if(!n)throw new Error("[react-modal-navigation] navigation requires an active modal screen; use modal.open() outside a modal");return n};return{...e,modal:e,navigation:{navigate:(...l)=>o().navigate(...l),replace:(...l)=>o().replace(...l),reset:(...l)=>o().reset(...l),setParams:(...l)=>o().setParams(...l),goBack:(...l)=>o().goBack(...l),close:(...l)=>o().close(...l),get canGoBack(){return n?.canGoBack??!1},get stack(){return n?.stack??[]},get currentRoute(){return n?.currentRoute??null}}}},[e,n])}function Ye(){const e=t.useContext(be);if(!e)throw new Error("useModalNavigation requires an active modal screen");return e}function xe(){const e=t.useContext(de);if(!e)throw new Error("useIsFocused must be called inside a modal screen");return e.isFocused}function Qe(e){const n=xe();t.useEffect(()=>{if(n)return e()},[n,e])}function Ve(e){const n=t.useContext(de);if(!n)throw new Error("useNavigationResult requires an active modal screen");const o=t.useRef(e),c=t.useRef(null);t.useEffect(()=>{o.current=e},[e]),t.useEffect(()=>{!n.isFocused||!n.resultEvent||c.current!==n.resultEvent.id&&(c.current=n.resultEvent.id,o.current(n.resultEvent.data))},[n.isFocused,n.resultEvent])}function _e(e={}){const n=t.useContext(de);if(!n)throw new Error("[react-modal-navigation] useModalFooter requires an active modal screen");const{setFooter:o,routeKey:c}=n,l=t.useRef(e);l.current=e;const h=t.useMemo(()=>(...C)=>l.current.done?.(...C),[]),y=t.useMemo(()=>(...C)=>(l.current.cancel??l.current.cancle)?.(...C),[]),v=t.useMemo(()=>(...C)=>l.current.renderFooter?.(...C),[]),f=typeof e.done=="function",S=typeof(e.cancel??e.cancle)=="function",k=typeof e.renderFooter=="function",{showFooter:g,showFooterBorder:m,doneDisabled:p,doneText:b,cancelText:q,loadingText:B,footerClassName:P}=e,F=t.useMemo(()=>({showFooter:g,showFooterBorder:m,doneDisabled:p,doneText:b,cancelText:q,loadingText:B,footerClassName:P,...f?{done:h}:{},...S?{cancel:y}:{},...k?{renderFooter:v}:{}}),[g,m,p,b,q,B,P,f,S,k,h,y,v]);t.useEffect(()=>{o(c,F)},[c,o,F]),t.useEffect(()=>()=>o(c,null),[c,o])}function Se(){const e=t.useContext(ye);if(!e)throw new Error("[react-modal-navigation] useModalLoading requires an active modal screen");return e}function Je(){const e=Se(),n=xe(),o=t.useRef(!0),c=t.useRef(!1),[l,h]=t.useState(!1),[y,v]=t.useState(null),f=t.useRef(e);return f.current=e,t.useEffect(()=>(o.current=!0,()=>{o.current=!1}),[]),{run:t.useCallback(async(k,g={})=>{if(c.current)return{status:"busy"};if(typeof k!="function")throw new TypeError("[react-modal-navigation] run requires a function");c.current=!0,o.current&&(h(!0),v(null));const m=f.current.showLoading(g.loadingText);try{const p=await k();return o.current&&await g.onSuccess?.(p),{status:"success",value:p}}catch(p){if(o.current){v(p);try{await g.onError?.(p)}catch(b){console.error("[react-modal-navigation] onError failed",b)}}if(g.throwOnError)throw p;return{status:"error",error:p}}finally{m!=null&&f.current.hideLoading(m),c.current=!1,o.current&&h(!1)}},[]),pending:l,error:y,clearError:()=>v(null),isFocused:n}}function Xe(e,n={}){const o=t.useContext(Ee);if(!o)throw new Error("[react-modal-navigation] useModalUnsavedChanges must be used inside a modal screen");const c=t.useRef({dirty:e,...n});c.current={dirty:e,...n};const{routeKey:l,registerDirtyGuard:h}=o;t.useEffect(()=>h(l,{get dirty(){return!!c.current.dirty},get message(){return c.current.message},get onBeforeDiscard(){return c.current.onBeforeDiscard}}),[l,h])}exports.ModalNavigator=je;exports.ModalProvider=Ue;exports.useFocusEffect=Qe;exports.useIsFocused=xe;exports.useModal=We;exports.useModalFooter=_e;exports.useModalLoading=Se;exports.useModalNavigation=Ye;exports.useModalTask=Je;exports.useModalUnsavedChanges=Xe;exports.useNavigationResult=Ve;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=require("react/jsx-runtime"),t=require("react"),Te=require("react-dom"),Me=t.createContext(null),xe=t.createContext(null),fe=t.createContext(null),ve=t.createContext(null),je=t.createContext(null);let Be=0;const ye=(e,n)=>({key:`modal-route-${++Be}`,name:e,params:n??{}}),Se=(e,n)=>[ye(e,n)];function Pe(e,n){switch(n.type){case"RESET":return Se(n.name,n.params);case"NAVIGATE":{const o=e.findIndex(s=>s.name===n.name);return o<0?[...e,ye(n.name,n.params)]:e.slice(0,o+1).map((s,c)=>c===o?{...s,params:n.params??{}}:s)}case"SET_PARAMS":return e.map((o,s)=>s===e.length-1?{...o,params:{...o.params??{},...n.params??{}}}:o);case"BACK":return e.length>1?e.slice(0,-1):e;case"REPLACE":return e.length?[...e.slice(0,-1),ye(n.name,n.params)]:e;default:return e}}const Ce=()=>{if(typeof document>"u")return"light";const e=document.documentElement,n=document.body;return e.classList.contains("dark")||n.classList.contains("dark")||e.dataset.theme==="dark"||n.dataset.theme==="dark"?"dark":e.classList.contains("light")||n.classList.contains("light")||e.dataset.theme==="light"||n.dataset.theme==="light"?"light":window.matchMedia?.("(prefers-color-scheme: dark)").matches?"dark":"light"};let se=0;const J=[];let ge=null;function Fe(){if(se++,se===1){const n=document.documentElement,o=document.body,s={html:n.style.overflow,body:o.style.overflow,padding:o.style.paddingRight,width:o.style.getPropertyValue("--modal-scrollbar-width")},c=Math.max(0,window.innerWidth-n.clientWidth),x=parseFloat(getComputedStyle(o).paddingRight)||0;c&&(o.style.paddingRight=`${x+c}px`),o.style.setProperty("--modal-scrollbar-width",`${c}px`),n.style.overflow="hidden",o.style.overflow="hidden",ge=()=>{n.style.overflow=s.html,o.style.overflow=s.body,o.style.paddingRight=s.padding,s.width?o.style.setProperty("--modal-scrollbar-width",s.width):o.style.removeProperty("--modal-scrollbar-width")}}let e=!1;return()=>{e||(e=!0,se=Math.max(0,se-1),se||(ge?.(),ge=null))}}const qe='a[href],button:not([disabled]),input:not([disabled]),textarea:not([disabled]),select:not([disabled]),[tabindex]:not([tabindex="-1"])',De=e=>!e.closest('[inert], [hidden], [aria-hidden="true"]')&&e.getClientRects().length>0&&getComputedStyle(e).visibility!=="hidden"&&getComputedStyle(e).display!=="none";function Ae({dialogRef:e,options:n,requestClose:o,children:s,exiting:c=!1}){const x=t.useRef(null),g=t.useRef(null),v=t.useRef(o);v.current=o;const f=t.useRef(n.closeOnEsc);f.current=n.closeOnEsc;const[C,w]=t.useState(Ce),y=n.theme==="light"||n.theme==="dark"?n.theme:C;t.useEffect(()=>{if(n.theme==="light"||n.theme==="dark")return;const N=()=>w(Ce()),$=new MutationObserver(N);for(const R of[document.documentElement,document.body])$.observe(R,{attributes:!0,attributeFilter:["class","data-theme"]});const U=window.matchMedia?.("(prefers-color-scheme: dark)");return U?.addEventListener?.("change",N),N(),()=>{$.disconnect(),U?.removeEventListener?.("change",N)}},[n.theme]),t.useEffect(()=>{g.current=document.activeElement;const N=Fe(),$=x.current;J.push($),e.current?.focus({preventScroll:!0});const U=h=>{const k=e.current;if(!k||J.at(-1)!==$)return;if(h.key==="Escape"&&f.current!==!1){h.preventDefault(),v.current("escape");return}if(h.key!=="Tab")return;const B=[...k.querySelectorAll(qe)].filter(De);if(!B.length){h.preventDefault(),k.focus();return}const I=B[0],S=B.at(-1);k.contains(document.activeElement)?h.shiftKey&&(document.activeElement===I||document.activeElement===k)?(h.preventDefault(),S.focus()):!h.shiftKey&&(document.activeElement===S||document.activeElement===k)&&(h.preventDefault(),I.focus()):(h.preventDefault(),(h.shiftKey?S:I).focus())},R=h=>{J.at(-1)===$&&e.current&&!e.current.contains(h.target)&&e.current.focus({preventScroll:!0})};return document.addEventListener("keydown",U),document.addEventListener("focusin",R),()=>{document.removeEventListener("keydown",U),document.removeEventListener("focusin",R);const h=J.at(-1)===$,k=J.indexOf($);if(k!==-1&&J.splice(k,1),N(),h){const B=g.current;B?.isConnected&&!B.closest("[inert], [hidden]")?B.focus?.({preventScroll:!0}):J.at(-1)?.querySelector("[role=dialog]")?.focus?.({preventScroll:!0})}}},[e]);const j=n.backgroundColor??n.backgroudColor,u=typeof j=="string"?j:j?.[y],p=typeof u=="string"&&/^(#|rgb\(|rgba\(|hsl\(|hsla\(|oklch\(|color\(|var\()/.test(u.trim())?u:null,O={sm:400,md:560,lg:720,xl:960,full:"100%"},L=["center","left","right","top","bottom"].includes(n.placement)?n.placement:"right",P=N=>typeof N=="number"?`${N}px`:N,F=P(n.width??O[n.size]??460),q=P(n.height),E=P(n.maxHeight),T=P(n.maxWidth),D=Number(n.viewportGap??5),G=Number.isFinite(D)?Math.max(0,D):5,A=typeof n.borderRadius=="number"?`${n.borderRadius}px`:n.borderRadius??"12px";return Te.createPortal(m.jsx("div",{ref:x,className:`pmn-overlay ${c?"pmn-exiting":"pmn-entering"} ${n.animation===!1?"pmn-no-animation":""} pmn-place-${L} ${n.mobileFullscreen?"pmn-mobile-fullscreen":""}`,style:{padding:G,"--pmn-animation-duration":`${Math.min(1e3,Math.max(0,Number(n.animationDuration??220)||0))}ms`},onMouseDown:N=>{N.target===N.currentTarget&&n.closeOnOverlay===!0&&v.current("overlay")},children:m.jsx("section",{ref:e,tabIndex:-1,role:"dialog","aria-modal":"true","aria-label":n.ariaLabel??"Dialog","data-modal-theme":y,className:`pmn-dialog ${y==="dark"?"dark":""} ${!p&&u?u:""} ${n.className??""}`,style:{"--pmn-width":F,"--pmn-gap":`${G}px`,...q!==void 0?{"--pmn-height":q}:{},...E!==void 0?{"--pmn-max-height":E}:{},...T!==void 0?{"--pmn-max-width":T}:{},borderRadius:A,...p?{backgroundColor:p}:{}},children:s})}),document.body)}function Ee({back:e}){return m.jsx("svg",{"aria-hidden":"true",viewBox:"0 0 24 24",width:"20",height:"20",fill:"none",stroke:"currentColor",strokeWidth:"2",children:e?m.jsx("path",{d:"m15 18-6-6 6-6"}):m.jsx("path",{d:"M18 6 6 18M6 6l12 12"})})}function Oe({options:e,screen:n,route:o,navigation:s,requestClose:c,translate:x}){if(e.showHeader===!1)return null;const g=x??(j=>j),v=n.showTitle??e.showTitle??!0,f=typeof n.title=="function"?n.title({t:g,route:o,navigation:s}):n.title??o.name,C={route:o,navigation:s,close:c,canGoBack:s.canGoBack,title:f,t:g},w=n.renderHeader??e.renderHeader;if(w)return w(C);const y=n.renderHeaderRight??e.renderHeaderRight;return m.jsxs("header",{className:`pmn-header ${n.showHeaderBorder??e.showHeaderBorder?"pmn-border-bottom":""} ${e.headerClassName??""}`,children:[e.showBackButton!==!1&&s.canGoBack&&m.jsx("button",{type:"button",className:"pmn-icon-button","aria-label":g("modal.back"),onClick:()=>s.goBack(),children:m.jsx(Ee,{back:!0})}),v?m.jsx("h2",{children:f}):m.jsx("div",{className:"pmn-header-spacer"}),y?.(C),e.showCloseButton!==!1&&m.jsx("button",{type:"button",className:"pmn-icon-button","aria-label":g("modal.close"),onClick:()=>c("header"),children:m.jsx(Ee,{})})]})}function $e({screen:e,options:n,override:o,route:s,navigation:c,requestClose:x,translate:g,onError:v}){const[f,C]=t.useState(!1),w=g??(T=>T),y={...n,...e,...o},{showFooter:j,renderFooter:u,done:p,cancel:O,cancle:L}=y,P=O??L,F={route:s,navigation:c,close:x,t:w};if(j===!1)return null;const q=`pmn-footer ${y.showFooterBorder?"pmn-border-top":""} ${y.footerClassName??""}`;if(typeof u=="function")return m.jsx("footer",{className:q,children:u({...F,pending:f})});if(typeof p!="function"&&typeof P!="function")return null;const E=async T=>{if(!(f||typeof T!="function")){C(!0);try{await T(F)}catch(D){v?.(D,{phase:"footer",route:s})}finally{C(!1)}}};return m.jsxs("footer",{className:q,children:[typeof P=="function"&&m.jsx("button",{type:"button",disabled:f,onClick:()=>E(P),className:"pmn-button pmn-button-secondary",children:y.cancelText??w("modal.cancel")}),typeof p=="function"&&m.jsx("button",{type:"button",disabled:f||y.doneDisabled===!0,onClick:()=>E(p),className:"pmn-button pmn-button-primary",children:f?y.loadingText??w("modal.processing"):y.doneText??w("modal.done")})]})}function Ie({loading:e,translate:n}){if(!e.visible)return null;const s=(n??(x=>x))("modal.loading"),c=e.text??(s==="modal.loading"?"加载中...":s);return m.jsx("div",{className:"pmn-loading-overlay",role:"status","aria-live":"polite","aria-busy":"true",children:m.jsxs("div",{className:"pmn-loading-card",children:[m.jsx("span",{className:"pmn-spinner","aria-hidden":"true"}),m.jsx("span",{children:c})]})})}class He extends t.Component{state={error:null};static getDerivedStateFromError(n){return{error:n}}componentDidCatch(n,o){this.props.onError?.(n,{phase:"render",route:this.props.route,componentStack:o.componentStack})}componentDidUpdate(n){n.route?.key!==this.props.route?.key&&this.state.error&&this.setState({error:null})}render(){return this.state.error?m.jsxs("div",{role:"alert",className:"pmn-error-screen",children:[m.jsx("strong",{children:"页面加载失败"}),m.jsx("p",{children:this.state.error.message}),m.jsx("button",{type:"button",className:"pmn-button pmn-button-secondary",onClick:()=>this.setState({error:null}),children:"重试"})]}):this.props.children}}function Ge({route:e,screen:n,navigation:o,focused:s,resultEvent:c,setFooter:x,loadingApi:g,bodyClassName:v,notifyError:f,registerDirtyGuard:C}){const w=n.component,y=t.useMemo(()=>({isFocused:s,resultEvent:c,routeKey:e.key,setFooter:x}),[s,c,e.key,x]),j=t.useMemo(()=>({isLoading:s&&g.isLoading,setLoading:(u,p)=>g.setScreenLoading(e.key,u,p),showLoading:u=>g.begin(e.key,u),hideLoading:u=>g.end(e.key,u)}),[s,g,e.key]);return m.jsx(je.Provider,{value:{routeKey:e.key,registerDirtyGuard:C},children:m.jsx(fe.Provider,{value:y,children:m.jsx(ve.Provider,{value:j,children:m.jsx("div",{hidden:!s,inert:!s,"data-modal-route-key":e.key,tabIndex:-1,className:`pmn-body ${v??""}`,children:m.jsx(He,{route:e,onError:f,children:m.jsx(w,{route:e,navigation:o})})})})})})}function Ne({config:e,initialScreen:n,params:o,options:s,translate:c,onRequestClose:x,onOpened:g,registerLoading:v,notifyError:f,error:C,clearError:w,registerDirtyGuard:y,exiting:j=!1}){const[u,p]=t.useReducer(Pe,null,()=>Se(n??e.initialRoute,o)),[O,L]=t.useState({}),[P,F]=t.useState({}),[,q]=t.useState(0),E=t.useRef(new Map),T=t.useRef(0),D=t.useRef(new Map),G=t.useRef(null),A=t.useRef(null),N=t.useRef(!1),$=t.useRef(!1),U=t.useRef(0),R=u.at(-1),h=R.key,k=t.useRef(h);k.current=h;const B=e.screens,I=t.useCallback(()=>q(r=>r+1),[]),S=t.useCallback((r,i)=>{if(r!=="global"&&k.current!==r)return null;const d=++T.current;return E.current.set(d,{owner:r,text:i}),I(),d},[I]),z=t.useCallback((r,i)=>{if(i!=null)E.current.get(i)?.owner===r&&E.current.delete(i);else for(const[d,M]of E.current)M.owner===r&&E.current.delete(d);I()},[I]),H=t.useCallback((r,i)=>{r?(z("global"),S("global",i)):z("global")},[S,z]),X=t.useCallback((r,i,d)=>{i?(z(r),S(r,d)):z(r)},[S,z]),Z=[...E.current.values()],K={visible:Z.length>0,text:Z.at(-1)?.text??null,count:Z.length},Y=t.useRef(K);Y.current=K;const ce=t.useMemo(()=>({isLoading:K.visible,setLoading:H,setScreenLoading:X,begin:S,end:z}),[K.visible,H,X,S,z]),ee=t.useRef(v);ee.current=v,t.useEffect(()=>(ee.current?.({setLoading:H}),()=>ee.current?.(null)),[H]);const ie=t.useCallback((r,i)=>F(d=>{if(i===null){if(!(r in d))return d;const M={...d};return delete M[r],M}return d[r]===i?d:{...d,[r]:i}}),[]),V=t.useCallback(()=>{const r=document.activeElement;if(!(r instanceof HTMLElement)||!G.current?.contains(r))return;const i=r.closest("[data-modal-route-key]");i&&D.current.set(i.dataset.modalRouteKey,r),G.current.focus({preventScroll:!0})},[]),te=t.useCallback(r=>{if(!Object.prototype.hasOwnProperty.call(B,r))throw new Error(`[react-modal-navigation] Unknown modal screen: ${r}`)},[B]),Q=t.useCallback((r,i)=>{try{Promise.resolve(s[r]?.(i)).catch(d=>f(d,{phase:r}))}catch(d){f(d,{phase:r})}},[s,f]),W=t.useCallback((r,i,d)=>{te(i),V(),p({type:r,name:i,params:d})},[te,V]),ne=t.useCallback((r,i)=>W("NAVIGATE",r,i),[W]),re=t.useCallback((r,i)=>W("REPLACE",r,i),[W]),oe=t.useCallback((r,i)=>{L({}),W("RESET",r,i)},[W]),ae=t.useCallback(r=>{if(!r||typeof r!="object"||Array.isArray(r))throw new TypeError("[react-modal-navigation] setParams expects an object");p({type:"SET_PARAMS",params:r})},[]),le=t.useCallback(r=>{if(u.length<=1||N.current)return!1;if(N.current=!0,r!==void 0){const i=u.at(-2);L(d=>({...d,[i.key]:{id:++U.current,data:r}}))}return V(),p({type:"BACK"}),!0},[u,V]),a=t.useCallback((r,i)=>Y.current.visible&&s.lockCloseWhileLoading!==!1?Promise.resolve(!1):x(r,i,{route:R,stack:u}),[s.lockCloseWhileLoading,x,R,u]),l=t.useCallback(r=>a("api",r),[a]),b=t.useMemo(()=>({navigate:ne,replace:re,reset:oe,setParams:ae,goBack:le,close:l,setLoading:H,canGoBack:u.length>1,stack:u,currentRoute:R}),[ne,re,oe,ae,le,l,H,u,R]);return t.useLayoutEffect(()=>{N.current=!1;const r=[...G.current?.querySelectorAll("[data-modal-route-key]")??[]].find(M=>M.dataset.modalRouteKey===h);if(!r)return;const i=D.current.get(h);(i?.isConnected&&r.contains(i)&&!i.matches(":disabled")&&!i.closest("[inert], [hidden]")?i:r).focus({preventScroll:!0})},[h]),t.useEffect(()=>{let r=!1;for(const[d,M]of E.current)M.owner!=="global"&&M.owner!==h&&(E.current.delete(d),r=!0);r&&I();const i=new Set(u.map(d=>d.key));for(const d of D.current.keys())i.has(d)||D.current.delete(d)},[h,u,I]),t.useEffect(()=>{if(A.current?.key!==h){if(A.current){const r={from:A.current,to:R,stack:u};Q("onBlur",{route:A.current,...r}),Q("onNavigate",r),Q("onRouteChange",r)}Q("onFocus",{route:R,stack:u}),A.current=R}},[R,h,u,Q]),t.useEffect(()=>()=>{E.current.clear(),D.current.clear()},[]),t.useEffect(()=>{$.current||($.current=!0,g?.({route:R,stack:u}))},[]),m.jsx(xe.Provider,{value:b,children:m.jsx(ve.Provider,{value:ce,children:m.jsxs(Ae,{dialogRef:G,options:s,requestClose:a,exiting:j,children:[m.jsx(Oe,{options:s,screen:B[R.name],route:R,navigation:b,requestClose:a,translate:c}),m.jsx("main",{className:"pmn-main",children:u.map(r=>m.jsx(Ge,{route:r,screen:B[r.name],navigation:b,focused:r.key===h,resultEvent:O[r.key]??null,bodyClassName:s.bodyClassName,setFooter:ie,loadingApi:ce,notifyError:f,registerDirtyGuard:y},r.key))}),m.jsx($e,{screen:B[R.name],options:s,override:P[h],route:R,navigation:b,requestClose:a,translate:c,onError:f},h),m.jsx(Ie,{loading:K,translate:c}),C&&m.jsxs("div",{className:"pmn-error",role:"alert",children:[m.jsx("span",{children:C.message||String(C)}),m.jsx("button",{type:"button",onClick:w,"aria-label":"Dismiss error",children:"×"})]})]})})})}const ze=".pmn-overlay,.pmn-dialog,.pmn-dialog *{box-sizing:border-box}.pmn-overlay{position:fixed;inset:0;z-index:1000;display:flex;overscroll-behavior:none;background:#00000073}.pmn-place-right{justify-content:flex-end;align-items:stretch}.pmn-place-left{justify-content:flex-start;align-items:stretch}.pmn-place-center{justify-content:center;align-items:center}.pmn-place-top{justify-content:center;align-items:flex-start}.pmn-place-bottom{justify-content:center;align-items:flex-end}.pmn-dialog{--pmn-surface:#fff;--pmn-body:#fff;--pmn-text:#0f172a;--pmn-muted:#475569;--pmn-border:#e2e8f0;--pmn-hover:#f1f5f9;--pmn-overlay-card:rgba(255,255,255,.92);color-scheme:light;position:relative;display:flex;flex-direction:column;min-height:0;width:100%;max-width:min(calc(100vw - var(--pmn-gap)*2),var(--pmn-max-width, var(--pmn-width)));max-height:min(calc(100dvh - var(--pmn-gap)*2),var(--pmn-max-height, 100dvh));height:var(--pmn-height, 100%);overflow:hidden;outline:none;box-shadow:0 25px 50px -12px #00000040;background:var(--pmn-surface);color:var(--pmn-text)}.pmn-place-left .pmn-dialog,.pmn-place-right .pmn-dialog{height:calc(100dvh - var(--pmn-gap)*2);max-height:calc(100dvh - var(--pmn-gap)*2)}.pmn-place-center .pmn-dialog{height:var(--pmn-height, auto);min-height:150px}.pmn-place-top .pmn-dialog,.pmn-place-bottom .pmn-dialog{height:var(--pmn-height, auto);min-height:0}.pmn-dialog[data-modal-theme=dark]{--pmn-surface:#334155;--pmn-body:#334155;--pmn-text:#f1f5f9;--pmn-muted:#cbd5e1;--pmn-border:#475569;--pmn-hover:#1e293b;--pmn-overlay-card:rgba(51,65,85,.94);color-scheme:dark}.pmn-main{min-height:0;flex:1;overflow:hidden;display:flex;flex-direction:column;background:transparent}.pmn-body{height:100%;min-height:0;overflow-y:auto;overscroll-behavior:contain;padding:16px;flex:1}.pmn-body[hidden]{display:none!important}.pmn-header{display:flex;flex-shrink:0;height:64px;align-items:center;gap:12px;padding:0 20px;background:transparent}.pmn-header h2{font-size:18px;font-weight:600;min-width:0;flex:1;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;margin:0}.pmn-header-spacer{flex:1}.pmn-border-bottom{border-bottom:1px solid var(--pmn-border)}.pmn-icon-button{display:inline-flex;align-items:center;justify-content:center;padding:8px;border:0;background:transparent;color:inherit;border-radius:8px;cursor:pointer}.pmn-icon-button:hover{background:var(--pmn-hover)}.pmn-footer{display:flex;flex-shrink:0;align-items:center;justify-content:flex-end;gap:12px;padding:16px 20px;background:transparent}.pmn-border-top{border-top:1px solid var(--pmn-border)}.pmn-button{padding:8px 16px;border-radius:8px;font-size:14px;font-weight:500;cursor:pointer}.pmn-button:disabled{opacity:.5;cursor:not-allowed}.pmn-button-secondary{border:1px solid var(--pmn-border);background:transparent;color:var(--pmn-text)}.pmn-button-secondary:hover:not(:disabled){background:var(--pmn-hover)}.pmn-button-primary{border:1px solid #2563eb;background:#2563eb;color:#fff}.pmn-button-primary:hover:not(:disabled){background:#1d4ed8}.pmn-loading-overlay{position:absolute;inset:0;z-index:50;display:flex;align-items:center;justify-content:center;background:var(--pmn-overlay-card);-webkit-backdrop-filter:blur(1px);backdrop-filter:blur(1px)}.pmn-loading-card{display:flex;flex-direction:column;align-items:center;gap:12px;max-width:85%;padding:20px 28px;border-radius:12px;background:var(--pmn-surface);box-shadow:0 10px 20px #00000026}.pmn-spinner{width:36px;height:36px;border:3px solid #2563eb;border-top-color:transparent;border-radius:50%;animation:pmn-spin 1s linear infinite}@keyframes pmn-spin{to{transform:rotate(360deg)}}.pmn-error{position:absolute;z-index:60;bottom:8px;left:12px;right:12px;display:flex;justify-content:space-between;gap:12px;padding:12px;border:1px solid #fca5a5;border-radius:8px;background:#fef2f2;color:#991b1b;font-size:13px}.pmn-error button{border:0;background:transparent;font-size:20px;cursor:pointer;color:inherit}.pmn-dialog input:not([type=checkbox]):not([type=radio]),.pmn-dialog textarea,.pmn-dialog select{color:var(--pmn-text);border-color:var(--pmn-border)}.pmn-error-screen{padding:24px;color:var(--pmn-text)}.pmn-error-screen p{color:#dc2626;overflow-wrap:anywhere}@media(max-width:640px){.pmn-overlay.pmn-mobile-fullscreen{padding:0!important}.pmn-mobile-fullscreen .pmn-dialog{max-width:100%;width:100%;height:100dvh;max-height:100dvh;border-radius:0!important}}@media(prefers-reduced-motion:reduce){.pmn-spinner{animation-duration:2s}}@keyframes pmn-fade-in{0%{opacity:0}to{opacity:1}}@keyframes pmn-fade-out{0%{opacity:1}to{opacity:0}}@keyframes pmn-scale-in{0%{opacity:0;transform:translateY(10px) scale(.97)}to{opacity:1;transform:none}}@keyframes pmn-scale-out{0%{opacity:1;transform:none}to{opacity:0;transform:translateY(10px) scale(.97)}}@keyframes pmn-right-in{0%{transform:translate(105%)}to{transform:none}}@keyframes pmn-right-out{0%{transform:none}to{transform:translate(105%)}}@keyframes pmn-left-in{0%{transform:translate(-105%)}to{transform:none}}@keyframes pmn-left-out{0%{transform:none}to{transform:translate(-105%)}}@keyframes pmn-top-in{0%{transform:translateY(-105%)}to{transform:none}}@keyframes pmn-top-out{0%{transform:none}to{transform:translateY(-105%)}}@keyframes pmn-bottom-in{0%{transform:translateY(105%)}to{transform:none}}@keyframes pmn-bottom-out{0%{transform:none}to{transform:translateY(105%)}}.pmn-entering{animation:pmn-fade-in var(--pmn-animation-duration) ease-out both}.pmn-exiting{animation:pmn-fade-out var(--pmn-animation-duration) ease-in both;pointer-events:none}.pmn-entering .pmn-dialog{animation:pmn-scale-in var(--pmn-animation-duration) cubic-bezier(.2,.8,.2,1) both}.pmn-exiting .pmn-dialog{animation:pmn-scale-out var(--pmn-animation-duration) ease-in both}.pmn-entering.pmn-place-right .pmn-dialog{animation-name:pmn-right-in}.pmn-exiting.pmn-place-right .pmn-dialog{animation-name:pmn-right-out}.pmn-entering.pmn-place-left .pmn-dialog{animation-name:pmn-left-in}.pmn-exiting.pmn-place-left .pmn-dialog{animation-name:pmn-left-out}.pmn-entering.pmn-place-top .pmn-dialog{animation-name:pmn-top-in}.pmn-exiting.pmn-place-top .pmn-dialog{animation-name:pmn-top-out}.pmn-entering.pmn-place-bottom .pmn-dialog{animation-name:pmn-bottom-in}.pmn-exiting.pmn-place-bottom .pmn-dialog{animation-name:pmn-bottom-out}.pmn-no-animation,.pmn-no-animation .pmn-dialog{animation:none!important}@media(prefers-reduced-motion:reduce){.pmn-entering,.pmn-exiting,.pmn-entering .pmn-dialog,.pmn-exiting .pmn-dialog{animation:none!important}}",Re="react-modal-navigation-styles",be=new WeakMap;function Ke(){t.useInsertionEffect(()=>{if(typeof document>"u")return;const e=document;let n=be.get(e);if(!n){let o=e.getElementById(Re);const s=!o;o||(o=e.createElement("style"),o.id=Re,o.setAttribute("data-react-modal-navigation",""),o.textContent=ze,e.head.appendChild(o)),n={element:o,owned:s,count:0},be.set(e,n)}return n.count+=1,()=>{n.count-=1,n.count===0&&(n.owned&&n.element.remove(),be.delete(e))}},[])}function me(e,n,o){if(typeof e=="string"){if(!e.trim())throw new TypeError("[react-modal-navigation] modal name must not be empty");return{name:e,params:n??{},options:o??{}}}if(e===null||typeof e!="object"||Array.isArray(e))throw new TypeError("[react-modal-navigation] open expects a modal name or a configuration object");const{name:s,screen:c,params:x={},options:g={},...v}=e;if(typeof s!="string"||!s.trim())throw new TypeError("[react-modal-navigation] open config requires a non-empty string name");if(g===null||typeof g!="object"||Array.isArray(g))throw new TypeError("[react-modal-navigation] open config options must be an object");if(c!==void 0&&(typeof c!="string"||!c.trim()))throw new TypeError("[react-modal-navigation] screen must be a non-empty string");return{name:s,...c===void 0?{}:{screen:c},params:x,options:{...v,...g}}}const Ue={placement:"right",showHeader:!0,showBackButton:!0,showCloseButton:!0,viewportGap:5,borderRadius:12,closeOnOverlay:!1,closeOnEsc:!0},We=e=>({"modal.loading":"加载中...","modal.processing":"处理中...","modal.back":"返回","modal.close":"关闭","modal.cancel":"取消","modal.done":"完成"})[e]??e;function Ye({registry:e,children:n,defaultOptions:o={},backgroundColor:s,theme:c,translate:x=We,onError:g,confirmDiscard:v}){Ke();const[f,C]=t.useState(null),[w,y]=t.useState(null),[j,u]=t.useState(0),p=t.useRef(null),O=t.useRef(new Map),L=t.useRef([]),P=t.useRef(null),F=t.useRef(null),q=t.useRef(0),E=t.useRef(null),T=t.useRef(!1),D=t.useRef(0),G=t.useRef(null),A=t.useRef(new Map),[N,$]=t.useState(!1),U=t.useRef(v);U.current=v;const R=t.useCallback((a,l)=>(A.current.set(a,l),()=>{A.current.get(a)===l&&A.current.delete(a)}),[]),h=t.useCallback(async a=>{for(const l of A.current.values()){if(!l.dirty)continue;if((l.onBeforeDiscard?await l.onBeforeDiscard(a):U.current?await U.current(a):typeof window<"u"&&typeof window.confirm=="function"?window.confirm(l.message||"有未保存的修改,确定放弃吗?"):!1)===!1)return!1}return!0},[]),k=t.useRef(!0),B=t.useRef(null),I=t.useRef(g);I.current=g;const S=t.useCallback((a,l)=>{k.current&&y(a);try{I.current?I.current(a,l):console.error("[react-modal-navigation]",a)}catch(b){console.error("[react-modal-navigation] onError failed",b)}},[]),z=t.useCallback((a,l)=>{const b=O.current.get(a);O.current.delete(a),b?.(l)},[]),H=t.useCallback(a=>({...Ue,...o,...c!==void 0?{theme:c}:{},...s!==void 0?{backgroundColor:s}:{},...a.config.modalOptions,...a.overrides}),[o,c,s]),X=f?H(f):null,Z=t.useRef(null);Z.current=X,B.current=X;const K=t.useCallback(async(a="api",l,b={})=>{const r=p.current;if(!r||E.current||T.current)return!1;E.current=r.id,T.current=!0;const i=B.current??H(r),d={...b,reason:a,result:l,name:r.name};try{if(!await h(d)||await i.onBeforeClose?.(d)===!1||p.current?.id!==r.id||!k.current||($(!0),await new Promise(M=>{const ue=i.animation===!1||typeof window<"u"&&window.matchMedia?.("(prefers-reduced-motion: reduce)").matches?0:Math.max(0,Number(i.animationDuration??220)||0);setTimeout(M,Math.min(ue,1e3))}),!k.current||p.current?.id!==r.id))return!1;p.current=null,G.current=null,C(null),$(!1),A.current.clear(),z(r.id,b.confirmed||l!==void 0?{status:"confirmed",value:l,reason:a}:{status:"dismissed",reason:a});try{await i.onClose?.(d)}catch(M){S(M,{phase:"onClose",name:r.name})}return!0}catch(M){return S(M,{phase:"onBeforeClose",name:r.name}),!1}finally{E.current===r.id&&(E.current=null),T.current=!1,!p.current&&k.current&&queueMicrotask(()=>F.current?.())}},[H,S,z,h]),Y=t.useCallback(async(a,l={},b={},r,i)=>{const d=e?.[a];if(!d)throw new Error(`[react-modal-navigation] Unknown modal: ${a}`);const M=i??d.initialRoute;if(!d.screens?.[M])throw new Error(`[react-modal-navigation] Unknown initial screen '${M}' in modal '${a}'`);if(!k.current||T.current||E.current)return null;T.current=!0;const ue=D.current;try{const _=p.current;if(_){const ke=B.current??H(_),pe={reason:"replace",name:_.name,nextName:a};try{if(!await h(pe)||await ke.onBeforeClose?.(pe)===!1)return null}catch(he){return S(he,{phase:"onBeforeClose",name:_.name}),null}if(!k.current||ue!==D.current||p.current?.id!==_.id)return null;p.current=null,G.current=null,C(null),A.current.clear(),z(_.id,{status:"replaced",reason:"replace"});try{await ke.onClose?.(pe)}catch(he){S(he,{phase:"onClose",name:_.name})}if(!k.current||ue!==D.current)return null}const de={id:++q.current,name:a,screen:M,params:l,config:d,overrides:b};return p.current=de,G.current=null,r&&O.current.set(de.id,r),y(null),C(de),de.id}finally{T.current=!1}},[e,H,S,z,h]);P.current=Y;const ce=t.useCallback(async()=>{if(!k.current||p.current||T.current||E.current)return;const a=L.current.shift();if(a){u(L.current.length);try{const l=await P.current(a.args.name,a.args.params,a.args.options,a.resolveResult,a.args.screen);l===null?(a.resolveResult?.({status:"dismissed",reason:"blocked"}),a.resolveId?.(null),queueMicrotask(()=>F.current?.())):a.resolveId?.(l)}catch(l){a.reject?.(l),queueMicrotask(()=>F.current?.())}}},[]);F.current=ce;const ee=t.useCallback((a,l,b)=>{const r=me(a,l,b);return e?.[r.name]?new Promise((i,d)=>{L.current.push({args:r,resolveId:i,reject:d}),u(L.current.length),queueMicrotask(()=>F.current?.())}):Promise.reject(new Error(`[react-modal-navigation] Unknown modal: ${r.name}`))},[e]),ie=t.useCallback((a,l,b)=>{const r=me(a,l,b);return e?.[r.name]?new Promise((i,d)=>{L.current.push({args:r,resolveResult:i,reject:d}),u(L.current.length),queueMicrotask(()=>F.current?.())}):Promise.reject(new Error(`[react-modal-navigation] Unknown modal: ${r.name}`))},[e]),V=t.useCallback(a=>{if(!a||typeof a!="object"||Array.isArray(a))throw new TypeError("[react-modal-navigation] update expects an options object");const l=p.current;if(!l||T.current)return!1;const b={...l,overrides:{...l.overrides,...a}};return p.current=b,C(b),B.current=H(b),!0},[H]),te=t.useCallback((a="queue-cleared")=>{const l=L.current.splice(0);u(0);for(const b of l)b.resolveId?.(null),b.resolveResult?.({status:"dismissed",reason:a});return l.length},[]),Q=t.useCallback((a,l,b)=>{const r=me(a,l,b);return Y(r.name,r.params,r.options,void 0,r.screen)},[Y]),W=t.useCallback((a,l,b)=>{const r=me(a,l,b);return new Promise((i,d)=>{Y(r.name,r.params,r.options,i,r.screen).then(M=>{M===null&&i({status:"dismissed",reason:"blocked"})},d)})},[Y]),ne=t.useCallback(a=>K("api",a),[K]),re=t.useCallback((a="api")=>K(a),[K]),oe=t.useCallback(a=>K("confirm",a,{confirmed:!0}),[K]),ae=t.useCallback((a,l)=>G.current?.setLoading(a,l),[]),le=t.useMemo(()=>({open:Q,openAsync:W,enqueue:ee,enqueueAsync:ie,update:V,clearQueue:te,close:ne,dismiss:re,confirm:oe,setLoading:ae,queueSize:j,isOpen:!!f,activeName:f?.name??null,error:w,clearError:()=>y(null)}),[Q,W,ee,ie,V,te,ne,re,oe,ae,f,w,j]);return t.useEffect(()=>(k.current=!0,()=>{k.current=!1,D.current+=1,p.current=null;for(const l of O.current.values())l({status:"dismissed",reason:"unmount"});O.current.clear(),A.current.clear();const a=L.current.splice(0);for(const l of a)l.resolveId?.(null),l.resolveResult?.({status:"dismissed",reason:"unmount"})}),[]),m.jsxs(Me.Provider,{value:le,children:[n,f&&m.jsx(Ne,{config:f.config,initialScreen:f.screen,params:f.params,options:X,translate:x,notifyError:S,error:w,clearError:()=>y(null),registerLoading:a=>{G.current=a},registerDirtyGuard:R,exiting:N,onRequestClose:K,onOpened:a=>{try{const l=Z.current?.onOpen?.(a);Promise.resolve(l).catch(b=>S(b,{phase:"onOpen"}))}catch(l){S(l,{phase:"onOpen"})}}},f.id)]})}function Qe(){const e=t.useContext(Me),n=t.useContext(xe);if(!e)throw new Error("[react-modal-navigation] useModal requires <ModalProvider>");return t.useMemo(()=>{const o=()=>{if(!n)throw new Error("[react-modal-navigation] navigation requires an active modal screen; use modal.open() outside a modal");return n};return{...e,modal:e,navigation:{navigate:(...c)=>o().navigate(...c),replace:(...c)=>o().replace(...c),reset:(...c)=>o().reset(...c),setParams:(...c)=>o().setParams(...c),goBack:(...c)=>o().goBack(...c),close:(...c)=>o().close(...c),get canGoBack(){return n?.canGoBack??!1},get stack(){return n?.stack??[]},get currentRoute(){return n?.currentRoute??null}}}},[e,n])}function Ve(){const e=t.useContext(xe);if(!e)throw new Error("useModalNavigation requires an active modal screen");return e}function we(){const e=t.useContext(fe);if(!e)throw new Error("useIsFocused must be called inside a modal screen");return e.isFocused}function _e(e){const n=we();t.useEffect(()=>{if(n)return e()},[n,e])}function Je(e){const n=t.useContext(fe);if(!n)throw new Error("useNavigationResult requires an active modal screen");const o=t.useRef(e),s=t.useRef(null);t.useEffect(()=>{o.current=e},[e]),t.useEffect(()=>{!n.isFocused||!n.resultEvent||s.current!==n.resultEvent.id&&(s.current=n.resultEvent.id,o.current(n.resultEvent.data))},[n.isFocused,n.resultEvent])}function Xe(e={}){const n=t.useContext(fe);if(!n)throw new Error("[react-modal-navigation] useModalFooter requires an active modal screen");const{setFooter:o,routeKey:s}=n,c=t.useRef(e);c.current=e;const x=t.useMemo(()=>(...q)=>c.current.done?.(...q),[]),g=t.useMemo(()=>(...q)=>(c.current.cancel??c.current.cancle)?.(...q),[]),v=t.useMemo(()=>(...q)=>c.current.renderFooter?.(...q),[]),f=typeof e.done=="function",C=typeof(e.cancel??e.cancle)=="function",w=typeof e.renderFooter=="function",{showFooter:y,showFooterBorder:j,doneDisabled:u,doneText:p,cancelText:O,loadingText:L,footerClassName:P}=e,F=t.useMemo(()=>({showFooter:y,showFooterBorder:j,doneDisabled:u,doneText:p,cancelText:O,loadingText:L,footerClassName:P,...f?{done:x}:{},...C?{cancel:g}:{},...w?{renderFooter:v}:{}}),[y,j,u,p,O,L,P,f,C,w,x,g,v]);t.useEffect(()=>{o(s,F)},[s,o,F]),t.useEffect(()=>()=>o(s,null),[s,o])}function Le(){const e=t.useContext(ve);if(!e)throw new Error("[react-modal-navigation] useModalLoading requires an active modal screen");return e}function Ze(){const e=Le(),n=we(),o=t.useRef(!0),s=t.useRef(!1),[c,x]=t.useState(!1),[g,v]=t.useState(null),f=t.useRef(e);return f.current=e,t.useEffect(()=>(o.current=!0,()=>{o.current=!1}),[]),{run:t.useCallback(async(w,y={})=>{if(s.current)return{status:"busy"};if(typeof w!="function")throw new TypeError("[react-modal-navigation] run requires a function");s.current=!0,o.current&&(x(!0),v(null));const j=f.current.showLoading(y.loadingText);try{const u=await w();return o.current&&await y.onSuccess?.(u),{status:"success",value:u}}catch(u){if(o.current){v(u);try{await y.onError?.(u)}catch(p){console.error("[react-modal-navigation] onError failed",p)}}if(y.throwOnError)throw u;return{status:"error",error:u}}finally{j!=null&&f.current.hideLoading(j),s.current=!1,o.current&&x(!1)}},[]),pending:c,error:g,clearError:()=>v(null),isFocused:n}}function et(e,n={}){const o=t.useContext(je);if(!o)throw new Error("[react-modal-navigation] useModalUnsavedChanges must be used inside a modal screen");const s=t.useRef({dirty:e,...n});s.current={dirty:e,...n};const{routeKey:c,registerDirtyGuard:x}=o;t.useEffect(()=>x(c,{get dirty(){return!!s.current.dirty},get message(){return s.current.message},get onBeforeDiscard(){return s.current.onBeforeDiscard}}),[c,x])}exports.ModalNavigator=Ne;exports.ModalProvider=Ye;exports.useFocusEffect=_e;exports.useIsFocused=we;exports.useModal=Qe;exports.useModalFooter=Xe;exports.useModalLoading=Le;exports.useModalNavigation=Ve;exports.useModalTask=Ze;exports.useModalUnsavedChanges=et;exports.useNavigationResult=Je;
package/dist/index.d.ts CHANGED
@@ -96,6 +96,9 @@ export interface ModalProviderProps {
96
96
  /** Named-object form for opening a modal without positional placeholders. */
97
97
  export interface ModalOpenConfig<P = unknown> extends ModalOptions {
98
98
  name: string;
99
+ /** Initial screen within registry[name].screens; defaults to initialRoute. */
100
+ screen?: string;
101
+ /** Params passed to the selected initial screen. */
99
102
  params?: P;
100
103
  /** Nested options override same-named top-level options. */
101
104
  options?: ModalOptions;