react-modal-navigation 1.1.4 → 1.1.5
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 +2 -0
- package/README.md +38 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +15 -0
- package/dist/index.js +433 -400
- package/dist/style.css +12 -0
- package/docs/API.md +38 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -519,3 +519,41 @@ modal.setLoading(true, { text: t("common.loading") });
|
|
|
519
519
|
modal.setLoading(false);
|
|
520
520
|
```
|
|
521
521
|
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
## 弹窗内部 Toast 消息提醒
|
|
525
|
+
|
|
526
|
+
`useToast()` 只在当前弹窗的范围内显示消息,不会向全局 `document.body` 添加通知。支持成功、错误、警告、信息四种状态,最多同时显示 5 条;默认 3000ms 后自动消失。
|
|
527
|
+
|
|
528
|
+
```jsx
|
|
529
|
+
import { useToast } from 'react-modal-navigation';
|
|
530
|
+
import { useTranslation } from 'react-i18next';
|
|
531
|
+
|
|
532
|
+
function EditDevice() {
|
|
533
|
+
const toast = useToast();
|
|
534
|
+
const { t } = useTranslation();
|
|
535
|
+
const save = async () => {
|
|
536
|
+
try {
|
|
537
|
+
await saveDevice();
|
|
538
|
+
toast.success(t('device.saveSuccess'));
|
|
539
|
+
} catch (error) {
|
|
540
|
+
toast.error(t('device.saveFailed'), { duration: 5000 });
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
return <button onClick={save}>{t('common.save')}</button>;
|
|
544
|
+
}
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
```jsx
|
|
548
|
+
const toast = useToast();
|
|
549
|
+
toast('操作提示'); // 默认 info
|
|
550
|
+
toast.info('请先填写名称');
|
|
551
|
+
toast.success('保存成功');
|
|
552
|
+
toast.warning('设备暂时离线');
|
|
553
|
+
toast.error('保存失败', { duration: 5000 });
|
|
554
|
+
const id = toast('持续提醒', { duration: 0 }); // 不自动关闭
|
|
555
|
+
toast.dismiss(id); // 关闭指定消息
|
|
556
|
+
toast.clear(); // 清空当前弹窗消息
|
|
557
|
+
```
|
|
558
|
+
|
|
559
|
+
`duration` 单位毫秒;设为 `0` 则不自动消失。`dismissible: false` 隐藏关闭按钮。消息区域始终限制在弹窗内,弹窗关闭时通知自动销毁。国际化文案由业务侧调用 `t()` 后传入。
|
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"),Le=require("react-dom"),Me=t.createContext(null),ve=t.createContext(null),fe=t.createContext(null),xe=t.createContext(null),je=t.createContext(null);let Pe=0;const ye=(e,n)=>({key:`modal-route-${++Pe}`,name:e,params:n??{}}),Se=(e,n)=>[ye(e,n)];function Be(e,n){switch(n.type){case"RESET":return Se(n.name,n.params);case"NAVIGATE":{const o=e.findIndex(a=>a.name===n.name);return o<0?[...e,ye(n.name,n.params)]:e.slice(0,o+1).map((a,c)=>c===o?{...a,params:n.params??{}}:a)}case"SET_OPTIONS":return e.map((o,a)=>a===e.length-1?{...o,options:{...o.options??{},...n.options??{}}}:o);case"SET_PARAMS":return e.map((o,a)=>a===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 Ee=()=>{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 X=[];let ge=null;function Fe(){if(ie++,ie===1){const n=document.documentElement,o=document.body,a={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),y=parseFloat(getComputedStyle(o).paddingRight)||0;c&&(o.style.paddingRight=`${y+c}px`),o.style.setProperty("--modal-scrollbar-width",`${c}px`),n.style.overflow="hidden",o.style.overflow="hidden",ge=()=>{n.style.overflow=a.html,o.style.overflow=a.body,o.style.paddingRight=a.padding,a.width?o.style.setProperty("--modal-scrollbar-width",a.width):o.style.removeProperty("--modal-scrollbar-width")}}let e=!1;return()=>{e||(e=!0,ie=Math.max(0,ie-1),ie||(ge?.(),ge=null))}}const qe='a[href],button:not([disabled]),input:not([disabled]),textarea:not([disabled]),select:not([disabled]),[tabindex]:not([tabindex="-1"])',Oe=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:a,exiting:c=!1}){const y=t.useRef(null),b=t.useRef(null),E=t.useRef(o);E.current=o;const p=t.useRef(n.closeOnEsc);p.current=n.closeOnEsc;const[k,j]=t.useState(Ee),x=n.theme==="light"||n.theme==="dark"?n.theme:k;t.useEffect(()=>{if(n.theme==="light"||n.theme==="dark")return;const T=()=>j(Ee()),I=new MutationObserver(T);for(const C of[document.documentElement,document.body])I.observe(C,{attributes:!0,attributeFilter:["class","data-theme"]});const W=window.matchMedia?.("(prefers-color-scheme: dark)");return W?.addEventListener?.("change",T),T(),()=>{I.disconnect(),W?.removeEventListener?.("change",T)}},[n.theme]),t.useEffect(()=>{b.current=document.activeElement;const T=Fe(),I=y.current;X.push(I),e.current?.focus({preventScroll:!0});const W=g=>{const R=e.current;if(!R||X.at(-1)!==I)return;if(g.key==="Escape"&&p.current!==!1){g.preventDefault(),E.current("escape");return}if(g.key!=="Tab")return;const P=[...R.querySelectorAll(qe)].filter(Oe);if(!P.length){g.preventDefault(),R.focus();return}const H=P[0],S=P.at(-1);R.contains(document.activeElement)?g.shiftKey&&(document.activeElement===H||document.activeElement===R)?(g.preventDefault(),S.focus()):!g.shiftKey&&(document.activeElement===S||document.activeElement===R)&&(g.preventDefault(),H.focus()):(g.preventDefault(),(g.shiftKey?S:H).focus())},C=g=>{X.at(-1)===I&&e.current&&!e.current.contains(g.target)&&e.current.focus({preventScroll:!0})};return document.addEventListener("keydown",W),document.addEventListener("focusin",C),()=>{document.removeEventListener("keydown",W),document.removeEventListener("focusin",C);const g=X.at(-1)===I,R=X.indexOf(I);if(R!==-1&&X.splice(R,1),T(),g){const P=b.current;P?.isConnected&&!P.closest("[inert], [hidden]")?P.focus?.({preventScroll:!0}):X.at(-1)?.querySelector("[role=dialog]")?.focus?.({preventScroll:!0})}}},[e]);const w=n.backgroundColor??n.backgroudColor,u=typeof w=="string"?w:w?.[x],h=typeof u=="string"&&/^(#|rgb\(|rgba\(|hsl\(|hsla\(|oklch\(|color\(|var\()/.test(u.trim())?u:null,q={sm:400,md:560,lg:720,xl:960,full:"100%"},N=["center","left","right","top","bottom"].includes(n.placement)?n.placement:"right",$=T=>typeof T=="number"?`${T}px`:T,B=$(n.width??q[n.size]??460),A=$(n.height),M=$(n.maxHeight),O=$(n.maxWidth),L=Number(n.viewportGap??5),D=Number.isFinite(L)?Math.max(0,L):5,F=typeof n.borderRadius=="number"?`${n.borderRadius}px`:n.borderRadius??"12px";return Le.createPortal(d.jsx("div",{ref:y,className:`pmn-overlay ${c?"pmn-exiting":"pmn-entering"} ${n.animation===!1?"pmn-no-animation":""} pmn-place-${N} ${n.mobileFullscreen?"pmn-mobile-fullscreen":""}`,style:{padding:D,"--pmn-animation-duration":`${Math.min(1e3,Math.max(0,Number(n.animationDuration??220)||0))}ms`},onMouseDown:T=>{T.target===T.currentTarget&&n.closeOnOverlay===!0&&E.current("overlay")},children:d.jsx("section",{ref:e,tabIndex:-1,role:"dialog","aria-modal":"true","aria-label":n.ariaLabel??"Dialog","data-modal-theme":x,className:`pmn-dialog ${x==="dark"?"dark":""} ${!h&&u?u:""} ${n.className??""}`,style:{"--pmn-width":B,"--pmn-gap":`${D}px`,...A!==void 0?{"--pmn-height":A}:{},...M!==void 0?{"--pmn-max-height":M}:{},...O!==void 0?{"--pmn-max-width":O}:{},borderRadius:F,...h?{backgroundColor:h}:{}},children:a})}),document.body)}function Ce({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:a,requestClose:c,translate:y}){if(e.showHeader===!1)return null;const b=y??(w=>w),E=n.showTitle??e.showTitle??!0,p=typeof n.title=="function"?n.title({t:b,route:o,navigation:a}):n.title??o.name,k={route:o,navigation:a,close:c,canGoBack:a.canGoBack,title:p,t:b},j=n.renderHeader??e.renderHeader;if(j)return j(k);const x=n.renderHeaderRight??e.renderHeaderRight;return d.jsxs("header",{className:`pmn-header ${n.showHeaderBorder??e.showHeaderBorder?"pmn-border-bottom":""} ${e.headerClassName??""}`,children:[e.showBackButton!==!1&&a.canGoBack&&d.jsx("button",{type:"button",className:"pmn-icon-button","aria-label":b("modal.back"),onClick:()=>a.goBack(),children:d.jsx(Ce,{back:!0})}),E?d.jsx("h2",{children:p}):d.jsx("div",{className:"pmn-header-spacer"}),x?.(k),e.showCloseButton!==!1&&d.jsx("button",{type:"button",className:"pmn-icon-button","aria-label":b("modal.close"),onClick:()=>c("header"),children:d.jsx(Ce,{})})]})}function $e({screen:e,options:n,override:o,route:a,navigation:c,requestClose:y,translate:b,onError:E,loadingApi:p}){const[k,j]=t.useState(!1),x=b??(L=>L),w={...n,...e,...o},{showFooter:u,renderFooter:h,done:q,cancel:N,cancle:$}=w,B=N??$,A={route:a,navigation:c,close:y,t:x};if(u===!1)return null;const M=`pmn-footer ${w.showFooterBorder?"pmn-border-top":""} ${w.footerClassName??""}`;if(typeof h=="function")return d.jsx("footer",{className:M,children:h({...A,pending:k})});if(typeof q!="function"&&typeof B!="function")return null;const O=async L=>{if(k||typeof L!="function")return;j(!0);const D=p?.begin(a.key,w.loadingText??x("modal.processing"));try{await L(A)}catch(F){E?.(F,{phase:"footer",route:a})}finally{p?.end(a.key,D),j(!1)}};return d.jsxs("footer",{className:M,children:[typeof B=="function"&&d.jsx("button",{type:"button",disabled:k,onClick:()=>O(B),className:"pmn-button pmn-button-secondary",children:w.cancelText??x("modal.cancel")}),typeof q=="function"&&d.jsx("button",{type:"button",disabled:k||w.doneDisabled===!0,onClick:()=>O(q),className:"pmn-button pmn-button-primary",children:k?w.loadingText??x("modal.processing"):w.doneText??x("modal.done")})]})}function Ie({loading:e,translate:n}){if(!e.visible)return null;const a=(n??(y=>y))("modal.loading"),c=e.text??(a==="modal.loading"?"加载中...":a);return d.jsx("div",{className:"pmn-loading-overlay",role:"status","aria-live":"polite","aria-busy":"true",children:d.jsxs("div",{className:"pmn-loading-content",children:[d.jsx("span",{className:"pmn-spinner","aria-hidden":"true"}),c&&d.jsx("span",{className:"pmn-loading-text",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?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 ze({route:e,screen:n,navigation:o,focused:a,resultEvent:c,setFooter:y,loadingApi:b,bodyClassName:E,notifyError:p,registerDirtyGuard:k}){const j=n.component,x=t.useMemo(()=>({isFocused:a,resultEvent:c,routeKey:e.key,setFooter:y}),[a,c,e.key,y]),w=t.useMemo(()=>({isLoading:a&&b.isLoading,setLoading:(u,h)=>b.setScreenLoading(e.key,u,h),showLoading:u=>b.begin(e.key,u),hideLoading:u=>b.end(e.key,u)}),[a,b,e.key]);return d.jsx(je.Provider,{value:{routeKey:e.key,registerDirtyGuard:k},children:d.jsx(fe.Provider,{value:x,children:d.jsx(xe.Provider,{value:w,children:d.jsx("div",{hidden:!a,inert:!a,"data-modal-route-key":e.key,tabIndex:-1,className:`pmn-body ${E??""}`,children:d.jsx(He,{route:e,onError:p,children:d.jsx(j,{route:e,navigation:o})})})})})})}function Te({config:e,initialScreen:n,params:o,options:a,translate:c,onRequestClose:y,onOpened:b,registerLoading:E,notifyError:p,error:k,clearError:j,registerDirtyGuard:x,exiting:w=!1}){const[u,h]=t.useReducer(Be,null,()=>Se(n??e.initialRoute,o)),[q,N]=t.useState({}),[$,B]=t.useState({}),[,A]=t.useState(0),M=t.useRef(new Map),O=t.useRef(0),L=t.useRef(new Map),D=t.useRef(null),F=t.useRef(null),T=t.useRef(!1),I=t.useRef(!1),W=t.useRef(0),C=u.at(-1),g=C.key,R=t.useRef(g);R.current=g;const P=e.screens,H=t.useCallback(()=>A(r=>r+1),[]),S=t.useCallback((r,l)=>{if(r!=="global"&&R.current!==r)return null;const m=++O.current;return M.current.set(m,{owner:r,text:l}),H(),m},[H]),K=t.useCallback((r,l)=>{if(l!=null)M.current.get(l)?.owner===r&&M.current.delete(l);else for(const[m,G]of M.current)G.owner===r&&M.current.delete(m);H()},[H]),z=t.useCallback((r,l)=>{r?(K("global"),S("global",l)):K("global")},[S,K]),Z=t.useCallback((r,l,m)=>{l?(K(r),S(r,m)):K(r)},[S,K]),ee=[...M.current.values()],U={visible:ee.length>0,text:ee.at(-1)?.text??null,count:ee.length},_=t.useRef(U);_.current=U;const ne=t.useMemo(()=>({isLoading:U.visible,setLoading:z,setScreenLoading:Z,begin:S,end:K}),[U.visible,z,Z,S,K]),te=t.useRef(E);te.current=E,t.useEffect(()=>(te.current?.({setLoading:z}),()=>te.current?.(null)),[z]);const le=t.useCallback((r,l)=>B(m=>{if(l===null){if(!(r in m))return m;const G={...m};return delete G[r],G}return m[r]===l?m:{...m,[r]:l}}),[]),V=t.useCallback(()=>{const r=document.activeElement;if(!(r instanceof HTMLElement)||!D.current?.contains(r))return;const l=r.closest("[data-modal-route-key]");l&&L.current.set(l.dataset.modalRouteKey,r),D.current.focus({preventScroll:!0})},[]),re=t.useCallback(r=>{if(!Object.prototype.hasOwnProperty.call(P,r))throw new Error(`[react-modal-navigation] Unknown modal screen: ${r}`)},[P]),Q=t.useCallback((r,l)=>{try{Promise.resolve(a[r]?.(l)).catch(m=>p(m,{phase:r}))}catch(m){p(m,{phase:r})}},[a,p]),Y=t.useCallback((r,l,m)=>{re(l),V(),h({type:r,name:l,params:m})},[re,V]),oe=t.useCallback((r,l)=>Y("NAVIGATE",r,l),[Y]),ae=t.useCallback((r,l)=>Y("REPLACE",r,l),[Y]),se=t.useCallback((r,l)=>{N({}),Y("RESET",r,l)},[Y]),ce=t.useCallback(r=>{if(!r||typeof r!="object"||Array.isArray(r))throw new TypeError("[react-modal-navigation] setParams expects an object");h({type:"SET_PARAMS",params:r})},[]),ue=t.useCallback(r=>{if(!r||typeof r!="object"||Array.isArray(r))throw new TypeError("[react-modal-navigation] setOptions expects an object");h({type:"SET_OPTIONS",options:r})},[]),s=t.useCallback(r=>{if(u.length<=1||T.current)return!1;if(T.current=!0,r!==void 0){const l=u.at(-2);N(m=>({...m,[l.key]:{id:++W.current,data:r}}))}return V(),h({type:"BACK"}),!0},[u,V]),i=t.useCallback((r,l)=>_.current.visible&&a.lockCloseWhileLoading!==!1?Promise.resolve(!1):y(r,l,{route:C,stack:u}),[a.lockCloseWhileLoading,y,C,u]),v=t.useCallback(r=>i("api",r),[i]),f=t.useMemo(()=>({navigate:oe,replace:ae,reset:se,setParams:ce,setOptions:ue,goBack:s,close:v,setLoading:z,canGoBack:u.length>1,stack:u,currentRoute:C}),[oe,ae,se,ce,ue,s,v,z,u,C]);return t.useLayoutEffect(()=>{T.current=!1;const r=[...D.current?.querySelectorAll("[data-modal-route-key]")??[]].find(G=>G.dataset.modalRouteKey===g);if(!r)return;const l=L.current.get(g);(l?.isConnected&&r.contains(l)&&!l.matches(":disabled")&&!l.closest("[inert], [hidden]")?l:r).focus({preventScroll:!0})},[g]),t.useEffect(()=>{let r=!1;for(const[m,G]of M.current)G.owner!=="global"&&G.owner!==g&&(M.current.delete(m),r=!0);r&&H();const l=new Set(u.map(m=>m.key));for(const m of L.current.keys())l.has(m)||L.current.delete(m)},[g,u,H]),t.useEffect(()=>{if(F.current?.key!==g){if(F.current){const r={from:F.current,to:C,stack:u};Q("onBlur",{route:F.current,...r}),Q("onNavigate",r),Q("onRouteChange",r)}Q("onFocus",{route:C,stack:u}),F.current=C}},[C,g,u,Q]),t.useEffect(()=>()=>{M.current.clear(),L.current.clear()},[]),t.useEffect(()=>{I.current||(I.current=!0,b?.({route:C,stack:u}))},[]),d.jsx(ve.Provider,{value:f,children:d.jsx(xe.Provider,{value:ne,children:d.jsxs(Ae,{dialogRef:D,options:a,requestClose:i,exiting:w,children:[d.jsx(De,{options:a,screen:{...P[C.name],...C.options??{}},route:C,navigation:f,requestClose:i,translate:c}),d.jsx("main",{className:"pmn-main",children:u.map(r=>d.jsx(ze,{route:r,screen:P[r.name],navigation:f,focused:r.key===g,resultEvent:q[r.key]??null,bodyClassName:a.bodyClassName,setFooter:le,loadingApi:ne,notifyError:p,registerDirtyGuard:x},r.key))}),d.jsx($e,{screen:{...P[C.name],...C.options??{}},options:a,override:$[g],route:C,navigation:f,requestClose:i,translate:c,onError:p,loadingApi:ne},g),d.jsx(Ie,{loading:U,translate:c}),k&&d.jsxs("div",{className:"pmn-error",role:"alert",children:[d.jsx("span",{children:k.message||String(k)}),d.jsx("button",{type:"button",onClick:j,"aria-label":"Dismiss error",children:"×"})]})]})})})}const Ge=".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:#ffffffb8;backdrop-filter:blur(2px);-webkit-backdrop-filter:blur(2px);cursor:progress}.pmn-dialog[data-modal-theme=dark] .pmn-loading-overlay{background:#0f172ab8}.pmn-loading-content{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:14px;max-width:85%;text-align:center;color:var(--pmn-text);pointer-events:none}.pmn-loading-text{font-size:14px;font-weight:500;line-height:1.5;overflow-wrap:anywhere}.pmn-spinner{width:38px;height:38px;border:3px solid rgba(37,99,235,.2);border-top-color:#2563eb;border-radius:50%;animation:pmn-spin .8s linear infinite}@keyframes pmn-spin{to{transform:rotate(360deg)}}@media(prefers-reduced-motion:reduce){.pmn-spinner{animation-duration:1.8s}}.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 a=!o;o||(o=e.createElement("style"),o.id=Re,o.setAttribute("data-react-modal-navigation",""),o.textContent=Ge,e.head.appendChild(o)),n={element:o,owned:a,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:a,screen:c,params:y={},options:b={},...E}=e;if(typeof a!="string"||!a.trim())throw new TypeError("[react-modal-navigation] open config requires a non-empty string name");if(b===null||typeof b!="object"||Array.isArray(b))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:a,...c===void 0?{}:{screen:c},params:y,options:{...E,...b}}}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:a,theme:c,translate:y=We,onError:b,confirmDiscard:E}){Ke();const[p,k]=t.useState(null),[j,x]=t.useState(null),[w,u]=t.useState(0),h=t.useRef(null),q=t.useRef(new Map),N=t.useRef([]),$=t.useRef(null),B=t.useRef(null),A=t.useRef(0),M=t.useRef(null),O=t.useRef(!1),L=t.useRef(0),D=t.useRef(null),F=t.useRef(new Map),[T,I]=t.useState(!1),W=t.useRef(E);W.current=E;const C=t.useCallback((s,i)=>(F.current.set(s,i),()=>{F.current.get(s)===i&&F.current.delete(s)}),[]),g=t.useCallback(async s=>{for(const i of F.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},[]),R=t.useRef(!0),P=t.useRef(null),H=t.useRef(b);H.current=b;const S=t.useCallback((s,i)=>{R.current&&x(s);try{H.current?H.current(s,i):console.error("[react-modal-navigation]",s)}catch(v){console.error("[react-modal-navigation] onError failed",v)}},[]),K=t.useCallback((s,i)=>{const v=q.current.get(s);q.current.delete(s),v?.(i)},[]),z=t.useCallback(s=>({...Ue,...o,...c!==void 0?{theme:c}:{},...a!==void 0?{backgroundColor:a}:{},...s.config.modalOptions,...s.overrides}),[o,c,a]),Z=p?z(p):null,ee=t.useRef(null);ee.current=Z,P.current=Z;const U=t.useCallback(async(s="api",i,v={})=>{const f=h.current;if(!f||M.current||O.current)return!1;M.current=f.id,O.current=!0;const r=P.current??z(f),l={...v,reason:s,result:i,name:f.name};try{if(!await g(l)||await r.onBeforeClose?.(l)===!1||h.current?.id!==f.id||!R.current||(I(!0),await new Promise(m=>{const G=r.animation===!1||typeof window<"u"&&window.matchMedia?.("(prefers-reduced-motion: reduce)").matches?0:Math.max(0,Number(r.animationDuration??220)||0);setTimeout(m,Math.min(G,1e3))}),!R.current||h.current?.id!==f.id))return!1;h.current=null,D.current=null,k(null),I(!1),F.current.clear(),K(f.id,v.confirmed||i!==void 0?{status:"confirmed",value:i,reason:s}:{status:"dismissed",reason:s});try{await r.onClose?.(l)}catch(m){S(m,{phase:"onClose",name:f.name})}return!0}catch(m){return S(m,{phase:"onBeforeClose",name:f.name}),!1}finally{M.current===f.id&&(M.current=null),O.current=!1,!h.current&&R.current&&queueMicrotask(()=>B.current?.())}},[z,S,K,g]),_=t.useCallback(async(s,i={},v={},f,r)=>{const l=e?.[s];if(!l)throw new Error(`[react-modal-navigation] Unknown modal: ${s}`);const m=r??l.initialRoute;if(!l.screens?.[m])throw new Error(`[react-modal-navigation] Unknown initial screen '${m}' in modal '${s}'`);if(!R.current||O.current||M.current)return null;O.current=!0;const G=L.current;try{const J=h.current;if(J){const ke=P.current??z(J),pe={reason:"replace",name:J.name,nextName:s};try{if(!await g(pe)||await ke.onBeforeClose?.(pe)===!1)return null}catch(he){return S(he,{phase:"onBeforeClose",name:J.name}),null}if(!R.current||G!==L.current||h.current?.id!==J.id)return null;h.current=null,D.current=null,k(null),F.current.clear(),K(J.id,{status:"replaced",reason:"replace"});try{await ke.onClose?.(pe)}catch(he){S(he,{phase:"onClose",name:J.name})}if(!R.current||G!==L.current)return null}const de={id:++A.current,name:s,screen:m,params:i,config:l,overrides:v};return h.current=de,D.current=null,f&&q.current.set(de.id,f),x(null),k(de),de.id}finally{O.current=!1}},[e,z,S,K,g]);$.current=_;const ne=t.useCallback(async()=>{if(!R.current||h.current||O.current||M.current)return;const s=N.current.shift();if(s){u(N.current.length);try{const i=await $.current(s.args.name,s.args.params,s.args.options,s.resolveResult,s.args.screen);i===null?(s.resolveResult?.({status:"dismissed",reason:"blocked"}),s.resolveId?.(null),queueMicrotask(()=>B.current?.())):s.resolveId?.(i)}catch(i){s.reject?.(i),queueMicrotask(()=>B.current?.())}}},[]);B.current=ne;const te=t.useCallback((s,i,v)=>{const f=me(s,i,v);return e?.[f.name]?new Promise((r,l)=>{N.current.push({args:f,resolveId:r,reject:l}),u(N.current.length),queueMicrotask(()=>B.current?.())}):Promise.reject(new Error(`[react-modal-navigation] Unknown modal: ${f.name}`))},[e]),le=t.useCallback((s,i,v)=>{const f=me(s,i,v);return e?.[f.name]?new Promise((r,l)=>{N.current.push({args:f,resolveResult:r,reject:l}),u(N.current.length),queueMicrotask(()=>B.current?.())}):Promise.reject(new Error(`[react-modal-navigation] Unknown modal: ${f.name}`))},[e]),V=t.useCallback(s=>{if(!s||typeof s!="object"||Array.isArray(s))throw new TypeError("[react-modal-navigation] update expects an options object");const i=h.current;if(!i||O.current)return!1;const v={...i,overrides:{...i.overrides,...s}};return h.current=v,k(v),P.current=z(v),!0},[z]),re=t.useCallback((s="queue-cleared")=>{const i=N.current.splice(0);u(0);for(const v of i)v.resolveId?.(null),v.resolveResult?.({status:"dismissed",reason:s});return i.length},[]),Q=t.useCallback((s,i,v)=>{const f=me(s,i,v);return _(f.name,f.params,f.options,void 0,f.screen)},[_]),Y=t.useCallback((s,i,v)=>{const f=me(s,i,v);return new Promise((r,l)=>{_(f.name,f.params,f.options,r,f.screen).then(m=>{m===null&&r({status:"dismissed",reason:"blocked"})},l)})},[_]),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)=>D.current?.setLoading(s,i),[]),ue=t.useMemo(()=>({open:Q,openAsync:Y,enqueue:te,enqueueAsync:le,update:V,clearQueue:re,close:oe,dismiss:ae,confirm:se,setLoading:ce,queueSize:w,isOpen:!!p,activeName:p?.name??null,error:j,clearError:()=>x(null)}),[Q,Y,te,le,V,re,oe,ae,se,ce,p,j,w]);return t.useEffect(()=>(R.current=!0,()=>{R.current=!1,L.current+=1,h.current=null;for(const i of q.current.values())i({status:"dismissed",reason:"unmount"});q.current.clear(),F.current.clear();const s=N.current.splice(0);for(const i of s)i.resolveId?.(null),i.resolveResult?.({status:"dismissed",reason:"unmount"})}),[]),d.jsxs(Me.Provider,{value:ue,children:[n,p&&d.jsx(Te,{config:p.config,initialScreen:p.screen,params:p.params,options:Z,translate:y,notifyError:S,error:j,clearError:()=>x(null),registerLoading:s=>{D.current=s},registerDirtyGuard:C,exiting:T,onRequestClose:U,onOpened:s=>{try{const i=ee.current?.onOpen?.(s);Promise.resolve(i).catch(v=>S(v,{phase:"onOpen"}))}catch(i){S(i,{phase:"onOpen"})}}},p.id)]})}function _e(){const e=t.useContext(Me),n=t.useContext(ve);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),setOptions:(...c)=>o().setOptions(...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 Qe(){const e=t.useContext(ve);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 Ve(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),a=t.useRef(null);t.useEffect(()=>{o.current=e},[e]),t.useEffect(()=>{!n.isFocused||!n.resultEvent||a.current!==n.resultEvent.id&&(a.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:a}=n,c=t.useRef(e);c.current=e;const y=t.useMemo(()=>(...A)=>c.current.done?.(...A),[]),b=t.useMemo(()=>(...A)=>(c.current.cancel??c.current.cancle)?.(...A),[]),E=t.useMemo(()=>(...A)=>c.current.renderFooter?.(...A),[]),p=typeof e.done=="function",k=typeof(e.cancel??e.cancle)=="function",j=typeof e.renderFooter=="function",{showFooter:x,showFooterBorder:w,doneDisabled:u,doneText:h,cancelText:q,loadingText:N,footerClassName:$}=e,B=t.useMemo(()=>({showFooter:x,showFooterBorder:w,doneDisabled:u,doneText:h,cancelText:q,loadingText:N,footerClassName:$,...p?{done:y}:{},...k?{cancel:b}:{},...j?{renderFooter:E}:{}}),[x,w,u,h,q,N,$,p,k,j,y,b,E]);t.useEffect(()=>{o(a,B)},[a,o,B]),t.useEffect(()=>()=>o(a,null),[a,o])}function Ne(){const e=t.useContext(xe);if(!e)throw new Error("[react-modal-navigation] useModalLoading requires an active modal screen");return e}function Ze(){const e=Ne(),n=we(),o=t.useRef(!0),a=t.useRef(!1),[c,y]=t.useState(!1),[b,E]=t.useState(null),p=t.useRef(e);return p.current=e,t.useEffect(()=>(o.current=!0,()=>{o.current=!1}),[]),{run:t.useCallback(async(j,x={})=>{if(a.current)return{status:"busy"};if(typeof j!="function")throw new TypeError("[react-modal-navigation] run requires a function");a.current=!0,o.current&&(y(!0),E(null));const w=p.current.showLoading(x.loadingText);try{const u=await j();return o.current&&await x.onSuccess?.(u),{status:"success",value:u}}catch(u){if(o.current){E(u);try{await x.onError?.(u)}catch(h){console.error("[react-modal-navigation] onError failed",h)}}if(x.throwOnError)throw u;return{status:"error",error:u}}finally{w!=null&&p.current.hideLoading(w),a.current=!1,o.current&&y(!1)}},[]),pending:c,error:b,clearError:()=>E(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 a=t.useRef({dirty:e,...n});a.current={dirty:e,...n};const{routeKey:c,registerDirtyGuard:y}=o;t.useEffect(()=>y(c,{get dirty(){return!!a.current.dirty},get message(){return a.current.message},get onBeforeDiscard(){return a.current.onBeforeDiscard}}),[c,y])}exports.ModalNavigator=Te;exports.ModalProvider=Ye;exports.useFocusEffect=Ve;exports.useIsFocused=we;exports.useModal=_e;exports.useModalFooter=Xe;exports.useModalLoading=Ne;exports.useModalNavigation=Qe;exports.useModalTask=Ze;exports.useModalUnsavedChanges=et;exports.useNavigationResult=Je;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("react/jsx-runtime"),e=require("react"),De=require("react-dom"),Ne=e.createContext(null),Ce=e.createContext(null),xe=e.createContext(null),Ee=e.createContext(null),Le=e.createContext(null),Pe=e.createContext(null);let Ae=0;const ke=(t,n)=>({key:`modal-route-${++Ae}`,name:t,params:n??{}}),Be=(t,n)=>[ke(t,n)];function $e(t,n){switch(n.type){case"RESET":return Be(n.name,n.params);case"NAVIGATE":{const o=t.findIndex(a=>a.name===n.name);return o<0?[...t,ke(n.name,n.params)]:t.slice(0,o+1).map((a,c)=>c===o?{...a,params:n.params??{}}:a)}case"SET_OPTIONS":return t.map((o,a)=>a===t.length-1?{...o,options:{...o.options??{},...n.options??{}}}:o);case"SET_PARAMS":return t.map((o,a)=>a===t.length-1?{...o,params:{...o.params??{},...n.params??{}}}:o);case"BACK":return t.length>1?t.slice(0,-1):t;case"REPLACE":return t.length?[...t.slice(0,-1),ke(n.name,n.params)]:t;default:return t}}const Me=()=>{if(typeof document>"u")return"light";const t=document.documentElement,n=document.body;return t.classList.contains("dark")||n.classList.contains("dark")||t.dataset.theme==="dark"||n.dataset.theme==="dark"?"dark":t.classList.contains("light")||n.classList.contains("light")||t.dataset.theme==="light"||n.dataset.theme==="light"?"light":window.matchMedia?.("(prefers-color-scheme: dark)").matches?"dark":"light"};let fe=0;const se=[];let ve=null;function Ie(){if(fe++,fe===1){const n=document.documentElement,o=document.body,a={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),b=parseFloat(getComputedStyle(o).paddingRight)||0;c&&(o.style.paddingRight=`${b+c}px`),o.style.setProperty("--modal-scrollbar-width",`${c}px`),n.style.overflow="hidden",o.style.overflow="hidden",ve=()=>{n.style.overflow=a.html,o.style.overflow=a.body,o.style.paddingRight=a.padding,a.width?o.style.setProperty("--modal-scrollbar-width",a.width):o.style.removeProperty("--modal-scrollbar-width")}}let t=!1;return()=>{t||(t=!0,fe=Math.max(0,fe-1),fe||(ve?.(),ve=null))}}const ze='a[href],button:not([disabled]),input:not([disabled]),textarea:not([disabled]),select:not([disabled]),[tabindex]:not([tabindex="-1"])',He=t=>!t.closest('[inert], [hidden], [aria-hidden="true"]')&&t.getClientRects().length>0&&getComputedStyle(t).visibility!=="hidden"&&getComputedStyle(t).display!=="none";function Ge({dialogRef:t,options:n,requestClose:o,children:a,exiting:c=!1}){const b=e.useRef(null),g=e.useRef(null),E=e.useRef(o);E.current=o;const p=e.useRef(n.closeOnEsc);p.current=n.closeOnEsc;const[w,M]=e.useState(Me),x=n.theme==="light"||n.theme==="dark"?n.theme:w;e.useEffect(()=>{if(n.theme==="light"||n.theme==="dark")return;const P=()=>M(Me()),U=new MutationObserver(P);for(const Z of[document.documentElement,document.body])U.observe(Z,{attributes:!0,attributeFilter:["class","data-theme"]});const N=window.matchMedia?.("(prefers-color-scheme: dark)");return N?.addEventListener?.("change",P),P(),()=>{U.disconnect(),N?.removeEventListener?.("change",P)}},[n.theme]),e.useEffect(()=>{g.current=document.activeElement;const P=Ie(),U=b.current;se.push(U),t.current?.focus({preventScroll:!0});const N=k=>{const C=t.current;if(!C||se.at(-1)!==U)return;if(k.key==="Escape"&&p.current!==!1){k.preventDefault(),E.current("escape");return}if(k.key!=="Tab")return;const L=[...C.querySelectorAll(ze)].filter(He);if(!L.length){k.preventDefault(),C.focus();return}const V=L[0],D=L.at(-1);C.contains(document.activeElement)?k.shiftKey&&(document.activeElement===V||document.activeElement===C)?(k.preventDefault(),D.focus()):!k.shiftKey&&(document.activeElement===D||document.activeElement===C)&&(k.preventDefault(),V.focus()):(k.preventDefault(),(k.shiftKey?D:V).focus())},Z=k=>{se.at(-1)===U&&t.current&&!t.current.contains(k.target)&&t.current.focus({preventScroll:!0})};return document.addEventListener("keydown",N),document.addEventListener("focusin",Z),()=>{document.removeEventListener("keydown",N),document.removeEventListener("focusin",Z);const k=se.at(-1)===U,C=se.indexOf(U);if(C!==-1&&se.splice(C,1),P(),k){const L=g.current;L?.isConnected&&!L.closest("[inert], [hidden]")?L.focus?.({preventScroll:!0}):se.at(-1)?.querySelector("[role=dialog]")?.focus?.({preventScroll:!0})}}},[t]);const v=n.backgroundColor??n.backgroudColor,d=typeof v=="string"?v:v?.[x],h=typeof d=="string"&&/^(#|rgb\(|rgba\(|hsl\(|hsla\(|oklch\(|color\(|var\()/.test(d.trim())?d:null,F={sm:400,md:560,lg:720,xl:960,full:"100%"},S=["center","left","right","top","bottom"].includes(n.placement)?n.placement:"right",I=P=>typeof P=="number"?`${P}px`:P,j=I(n.width??F[n.size]??460),T=I(n.height),q=I(n.maxHeight),O=I(n.maxWidth),z=Number(n.viewportGap??5),G=Number.isFinite(z)?Math.max(0,z):5,K=typeof n.borderRadius=="number"?`${n.borderRadius}px`:n.borderRadius??"12px";return De.createPortal(u.jsx("div",{ref:b,className:`pmn-overlay ${c?"pmn-exiting":"pmn-entering"} ${n.animation===!1?"pmn-no-animation":""} pmn-place-${S} ${n.mobileFullscreen?"pmn-mobile-fullscreen":""}`,style:{padding:G,"--pmn-animation-duration":`${Math.min(1e3,Math.max(0,Number(n.animationDuration??220)||0))}ms`},onMouseDown:P=>{P.target===P.currentTarget&&n.closeOnOverlay===!0&&E.current("overlay")},children:u.jsx("section",{ref:t,tabIndex:-1,role:"dialog","aria-modal":"true","aria-label":n.ariaLabel??"Dialog","data-modal-theme":x,className:`pmn-dialog ${x==="dark"?"dark":""} ${!h&&d?d:""} ${n.className??""}`,style:{"--pmn-width":j,"--pmn-gap":`${G}px`,...T!==void 0?{"--pmn-height":T}:{},...q!==void 0?{"--pmn-max-height":q}:{},...O!==void 0?{"--pmn-max-width":O}:{},borderRadius:K,...h?{backgroundColor:h}:{}},children:a})}),document.body)}function Se({back:t}){return u.jsx("svg",{"aria-hidden":"true",viewBox:"0 0 24 24",width:"20",height:"20",fill:"none",stroke:"currentColor",strokeWidth:"2",children:t?u.jsx("path",{d:"m15 18-6-6 6-6"}):u.jsx("path",{d:"M18 6 6 18M6 6l12 12"})})}function Ke({options:t,screen:n,route:o,navigation:a,requestClose:c,translate:b}){if(t.showHeader===!1)return null;const g=b??(v=>v),E=n.showTitle??t.showTitle??!0,p=typeof n.title=="function"?n.title({t:g,route:o,navigation:a}):n.title??o.name,w={route:o,navigation:a,close:c,canGoBack:a.canGoBack,title:p,t:g},M=n.renderHeader??t.renderHeader;if(M)return M(w);const x=n.renderHeaderRight??t.renderHeaderRight;return u.jsxs("header",{className:`pmn-header ${n.showHeaderBorder??t.showHeaderBorder?"pmn-border-bottom":""} ${t.headerClassName??""}`,children:[t.showBackButton!==!1&&a.canGoBack&&u.jsx("button",{type:"button",className:"pmn-icon-button","aria-label":g("modal.back"),onClick:()=>a.goBack(),children:u.jsx(Se,{back:!0})}),E?u.jsx("h2",{children:p}):u.jsx("div",{className:"pmn-header-spacer"}),x?.(w),t.showCloseButton!==!1&&u.jsx("button",{type:"button",className:"pmn-icon-button","aria-label":g("modal.close"),onClick:()=>c("header"),children:u.jsx(Se,{})})]})}function Ue({screen:t,options:n,override:o,route:a,navigation:c,requestClose:b,translate:g,onError:E,loadingApi:p}){const[w,M]=e.useState(!1),x=g??(z=>z),v={...n,...t,...o},{showFooter:d,renderFooter:h,done:F,cancel:S,cancle:I}=v,j=S??I,T={route:a,navigation:c,close:b,t:x};if(d===!1)return null;const q=`pmn-footer ${v.showFooterBorder?"pmn-border-top":""} ${v.footerClassName??""}`;if(typeof h=="function")return u.jsx("footer",{className:q,children:h({...T,pending:w})});if(typeof F!="function"&&typeof j!="function")return null;const O=async z=>{if(w||typeof z!="function")return;M(!0);const G=p?.begin(a.key,v.loadingText??x("modal.processing"));try{await z(T)}catch(K){E?.(K,{phase:"footer",route:a})}finally{p?.end(a.key,G),M(!1)}};return u.jsxs("footer",{className:q,children:[typeof j=="function"&&u.jsx("button",{type:"button",disabled:w,onClick:()=>O(j),className:"pmn-button pmn-button-secondary",children:v.cancelText??x("modal.cancel")}),typeof F=="function"&&u.jsx("button",{type:"button",disabled:w||v.doneDisabled===!0,onClick:()=>O(F),className:"pmn-button pmn-button-primary",children:w?v.loadingText??x("modal.processing"):v.doneText??x("modal.done")})]})}function We({loading:t,translate:n}){if(!t.visible)return null;const a=(n??(b=>b))("modal.loading"),c=t.text??(a==="modal.loading"?"加载中...":a);return u.jsx("div",{className:"pmn-loading-overlay",role:"status","aria-live":"polite","aria-busy":"true",children:u.jsxs("div",{className:"pmn-loading-content",children:[u.jsx("span",{className:"pmn-spinner","aria-hidden":"true"}),c&&u.jsx("span",{className:"pmn-loading-text",children:c})]})})}class Ye extends e.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?u.jsxs("div",{role:"alert",className:"pmn-error-screen",children:[u.jsx("strong",{children:"页面加载失败"}),u.jsx("p",{children:this.state.error.message}),u.jsx("button",{type:"button",className:"pmn-button pmn-button-secondary",onClick:()=>this.setState({error:null}),children:"重试"})]}):this.props.children}}const _e={success:"✓",error:"×",warning:"!",info:"i"};function Qe({items:t,dismiss:n}){return t.length?u.jsx("div",{className:"pmn-toast-region","aria-label":"Notifications",children:t.map(o=>u.jsxs("div",{className:`pmn-toast pmn-toast-${o.type}`,role:o.type==="error"||o.type==="warning"?"alert":"status",children:[u.jsx("span",{className:"pmn-toast-symbol","aria-hidden":"true",children:_e[o.type]}),u.jsx("span",{className:"pmn-toast-message",children:o.message}),o.dismissible&&u.jsx("button",{type:"button",className:"pmn-toast-close","aria-label":"Dismiss notification",onClick:()=>n(o.id),children:"×"})]},o.id))}):null}function Ve({route:t,screen:n,navigation:o,focused:a,resultEvent:c,setFooter:b,loadingApi:g,bodyClassName:E,notifyError:p,registerDirtyGuard:w}){const M=n.component,x=e.useMemo(()=>({isFocused:a,resultEvent:c,routeKey:t.key,setFooter:b}),[a,c,t.key,b]),v=e.useMemo(()=>({isLoading:a&&g.isLoading,setLoading:(d,h)=>g.setScreenLoading(t.key,d,h),showLoading:d=>g.begin(t.key,d),hideLoading:d=>g.end(t.key,d)}),[a,g,t.key]);return u.jsx(Le.Provider,{value:{routeKey:t.key,registerDirtyGuard:w},children:u.jsx(xe.Provider,{value:x,children:u.jsx(Ee.Provider,{value:v,children:u.jsx("div",{hidden:!a,inert:!a,"data-modal-route-key":t.key,tabIndex:-1,className:`pmn-body ${E??""}`,children:u.jsx(Ye,{route:t,onError:p,children:u.jsx(M,{route:t,navigation:o})})})})})})}function Fe({config:t,initialScreen:n,params:o,options:a,translate:c,onRequestClose:b,onOpened:g,registerLoading:E,notifyError:p,error:w,clearError:M,registerDirtyGuard:x,exiting:v=!1}){const[d,h]=e.useReducer($e,null,()=>Be(n??t.initialRoute,o)),[F,S]=e.useState([]),I=e.useRef(0),j=e.useRef(new Map),T=e.useCallback(r=>{clearTimeout(j.current.get(r)),j.current.delete(r),S(i=>i.filter(m=>m.id!==r))},[]),q=e.useCallback((r,i={})=>{if(typeof r!="string"||!r.trim())throw new TypeError("[react-modal-navigation] toast message must be a non-empty string");const m=typeof i=="string"?{type:i}:i??{},_=["success","error","warning","info"].includes(m.type)?m.type:"info",be=++I.current,je=Number.isFinite(m.duration)?Math.max(0,m.duration):3e3;return S(Oe=>[...Oe,{id:be,message:r,type:_,dismissible:m.dismissible!==!1}].slice(-5)),je>0&&j.current.set(be,setTimeout(()=>T(be),je)),be},[T]),O=e.useMemo(()=>{const r=(i,m)=>q(i,m);for(const i of["success","error","warning","info"])r[i]=(m,_={})=>q(m,{..._,type:i});return r.dismiss=T,r.clear=()=>{for(const i of j.current.values())clearTimeout(i);j.current.clear(),S([])},r},[q,T]);e.useEffect(()=>()=>{for(const r of j.current.values())clearTimeout(r);j.current.clear()},[]);const[z,G]=e.useState({}),[K,P]=e.useState({}),[,U]=e.useState(0),N=e.useRef(new Map),Z=e.useRef(0),k=e.useRef(new Map),C=e.useRef(null),L=e.useRef(null),V=e.useRef(!1),D=e.useRef(!1),ce=e.useRef(0),R=d.at(-1),A=R.key,de=e.useRef(A);de.current=A;const W=t.screens,Q=e.useCallback(()=>U(r=>r+1),[]),ee=e.useCallback((r,i)=>{if(r!=="global"&&de.current!==r)return null;const m=++Z.current;return N.current.set(m,{owner:r,text:i}),Q(),m},[Q]),J=e.useCallback((r,i)=>{if(i!=null)N.current.get(i)?.owner===r&&N.current.delete(i);else for(const[m,_]of N.current)_.owner===r&&N.current.delete(m);Q()},[Q]),X=e.useCallback((r,i)=>{r?(J("global"),ee("global",i)):J("global")},[ee,J]),me=e.useCallback((r,i,m)=>{i?(J(r),ee(r,m)):J(r)},[ee,J]),ie=[...N.current.values()],te={visible:ie.length>0,text:ie.at(-1)?.text??null,count:ie.length},pe=e.useRef(te);pe.current=te;const le=e.useMemo(()=>({isLoading:te.visible,setLoading:X,setScreenLoading:me,begin:ee,end:J}),[te.visible,X,me,ee,J]),ue=e.useRef(E);ue.current=E,e.useEffect(()=>(ue.current?.({setLoading:X}),()=>ue.current?.(null)),[X]);const he=e.useCallback((r,i)=>P(m=>{if(i===null){if(!(r in m))return m;const _={...m};return delete _[r],_}return m[r]===i?m:{...m,[r]:i}}),[]),ne=e.useCallback(()=>{const r=document.activeElement;if(!(r instanceof HTMLElement)||!C.current?.contains(r))return;const i=r.closest("[data-modal-route-key]");i&&k.current.set(i.dataset.modalRouteKey,r),C.current.focus({preventScroll:!0})},[]),ge=e.useCallback(r=>{if(!Object.prototype.hasOwnProperty.call(W,r))throw new Error(`[react-modal-navigation] Unknown modal screen: ${r}`)},[W]),s=e.useCallback((r,i)=>{try{Promise.resolve(a[r]?.(i)).catch(m=>p(m,{phase:r}))}catch(m){p(m,{phase:r})}},[a,p]),l=e.useCallback((r,i,m)=>{ge(i),ne(),h({type:r,name:i,params:m})},[ge,ne]),y=e.useCallback((r,i)=>l("NAVIGATE",r,i),[l]),f=e.useCallback((r,i)=>l("REPLACE",r,i),[l]),$=e.useCallback((r,i)=>{G({}),l("RESET",r,i)},[l]),B=e.useCallback(r=>{if(!r||typeof r!="object"||Array.isArray(r))throw new TypeError("[react-modal-navigation] setParams expects an object");h({type:"SET_PARAMS",params:r})},[]),H=e.useCallback(r=>{if(!r||typeof r!="object"||Array.isArray(r))throw new TypeError("[react-modal-navigation] setOptions expects an object");h({type:"SET_OPTIONS",options:r})},[]),re=e.useCallback(r=>{if(d.length<=1||V.current)return!1;if(V.current=!0,r!==void 0){const i=d.at(-2);G(m=>({...m,[i.key]:{id:++ce.current,data:r}}))}return ne(),h({type:"BACK"}),!0},[d,ne]),Y=e.useCallback((r,i)=>pe.current.visible&&a.lockCloseWhileLoading!==!1?Promise.resolve(!1):b(r,i,{route:R,stack:d}),[a.lockCloseWhileLoading,b,R,d]),oe=e.useCallback(r=>Y("api",r),[Y]),ae=e.useMemo(()=>({navigate:y,replace:f,reset:$,setParams:B,setOptions:H,goBack:re,close:oe,setLoading:X,canGoBack:d.length>1,stack:d,currentRoute:R}),[y,f,$,B,H,re,oe,X,d,R]);return e.useLayoutEffect(()=>{V.current=!1;const r=[...C.current?.querySelectorAll("[data-modal-route-key]")??[]].find(_=>_.dataset.modalRouteKey===A);if(!r)return;const i=k.current.get(A);(i?.isConnected&&r.contains(i)&&!i.matches(":disabled")&&!i.closest("[inert], [hidden]")?i:r).focus({preventScroll:!0})},[A]),e.useEffect(()=>{let r=!1;for(const[m,_]of N.current)_.owner!=="global"&&_.owner!==A&&(N.current.delete(m),r=!0);r&&Q();const i=new Set(d.map(m=>m.key));for(const m of k.current.keys())i.has(m)||k.current.delete(m)},[A,d,Q]),e.useEffect(()=>{if(L.current?.key!==A){if(L.current){const r={from:L.current,to:R,stack:d};s("onBlur",{route:L.current,...r}),s("onNavigate",r),s("onRouteChange",r)}s("onFocus",{route:R,stack:d}),L.current=R}},[R,A,d,s]),e.useEffect(()=>()=>{N.current.clear(),k.current.clear()},[]),e.useEffect(()=>{D.current||(D.current=!0,g?.({route:R,stack:d}))},[]),u.jsx(Pe.Provider,{value:O,children:u.jsx(Ce.Provider,{value:ae,children:u.jsx(Ee.Provider,{value:le,children:u.jsxs(Ge,{dialogRef:C,options:a,requestClose:Y,exiting:v,children:[u.jsx(Ke,{options:a,screen:{...W[R.name],...R.options??{}},route:R,navigation:ae,requestClose:Y,translate:c}),u.jsx("main",{className:"pmn-main",children:d.map(r=>u.jsx(Ve,{route:r,screen:W[r.name],navigation:ae,focused:r.key===A,resultEvent:z[r.key]??null,bodyClassName:a.bodyClassName,setFooter:he,loadingApi:le,notifyError:p,registerDirtyGuard:x},r.key))}),u.jsx(Ue,{screen:{...W[R.name],...R.options??{}},options:a,override:K[A],route:R,navigation:ae,requestClose:Y,translate:c,onError:p,loadingApi:le},A),u.jsx(We,{loading:te,translate:c}),u.jsx(Qe,{items:F,dismiss:T}),w&&u.jsxs("div",{className:"pmn-error",role:"alert",children:[u.jsx("span",{children:w.message||String(w)}),u.jsx("button",{type:"button",onClick:M,"aria-label":"Dismiss error",children:"×"})]})]})})})})}const Je=".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:#ffffffb8;backdrop-filter:blur(2px);-webkit-backdrop-filter:blur(2px);cursor:progress}.pmn-dialog[data-modal-theme=dark] .pmn-loading-overlay{background:#0f172ab8}.pmn-loading-content{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:14px;max-width:85%;text-align:center;color:var(--pmn-text);pointer-events:none}.pmn-loading-text{font-size:14px;font-weight:500;line-height:1.5;overflow-wrap:anywhere}.pmn-spinner{width:38px;height:38px;border:3px solid rgba(37,99,235,.2);border-top-color:#2563eb;border-radius:50%;animation:pmn-spin .8s linear infinite}@keyframes pmn-spin{to{transform:rotate(360deg)}}@media(prefers-reduced-motion:reduce){.pmn-spinner{animation-duration:1.8s}}.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}}.pmn-toast-region{position:absolute;top:56px;left:12px;right:12px;z-index:80;display:flex;flex-direction:column;align-items:center;gap:8px;pointer-events:none;max-height:calc(100% - 72px);overflow:hidden}.pmn-toast{pointer-events:auto;display:flex;align-items:flex-start;gap:10px;width:max-content;max-width:100%;min-width:min(260px,100%);padding:11px 13px;border:1px solid var(--pmn-border);border-radius:9px;background:var(--pmn-surface);color:var(--pmn-text);box-shadow:0 5px 18px #0000001a;font-size:13px;line-height:1.5;overflow-wrap:anywhere}.pmn-toast-symbol{flex:0 0 20px;display:grid;place-items:center;width:20px;height:20px;border-radius:50%;font-size:13px;font-weight:700;background:var(--pmn-hover);color:var(--pmn-muted)}.pmn-toast-success .pmn-toast-symbol{background:#dcfce7;color:#15803d}.pmn-toast-error .pmn-toast-symbol{background:#fee2e2;color:#b91c1c}.pmn-toast-warning .pmn-toast-symbol{background:#fef3c7;color:#b45309}.pmn-toast-info .pmn-toast-symbol{background:#dbeafe;color:#1d4ed8}.pmn-toast-message{flex:1;min-width:0;white-space:pre-wrap}.pmn-toast-close{border:0;background:transparent;color:var(--pmn-muted);cursor:pointer;font-size:19px;line-height:18px;padding:0 1px}.pmn-toast-close:hover{color:var(--pmn-text)}",Te="react-modal-navigation-styles",we=new WeakMap;function Xe(){e.useInsertionEffect(()=>{if(typeof document>"u")return;const t=document;let n=we.get(t);if(!n){let o=t.getElementById(Te);const a=!o;o||(o=t.createElement("style"),o.id=Te,o.setAttribute("data-react-modal-navigation",""),o.textContent=Je,t.head.appendChild(o)),n={element:o,owned:a,count:0},we.set(t,n)}return n.count+=1,()=>{n.count-=1,n.count===0&&(n.owned&&n.element.remove(),we.delete(t))}},[])}function ye(t,n,o){if(typeof t=="string"){if(!t.trim())throw new TypeError("[react-modal-navigation] modal name must not be empty");return{name:t,params:n??{},options:o??{}}}if(t===null||typeof t!="object"||Array.isArray(t))throw new TypeError("[react-modal-navigation] open expects a modal name or a configuration object");const{name:a,screen:c,params:b={},options:g={},...E}=t;if(typeof a!="string"||!a.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:a,...c===void 0?{}:{screen:c},params:b,options:{...E,...g}}}const Ze={placement:"right",showHeader:!0,showBackButton:!0,showCloseButton:!0,viewportGap:5,borderRadius:12,closeOnOverlay:!1,closeOnEsc:!0},et=t=>({"modal.loading":"加载中...","modal.processing":"处理中...","modal.back":"返回","modal.close":"关闭","modal.cancel":"取消","modal.done":"完成"})[t]??t;function tt({registry:t,children:n,defaultOptions:o={},backgroundColor:a,theme:c,translate:b=et,onError:g,confirmDiscard:E}){Xe();const[p,w]=e.useState(null),[M,x]=e.useState(null),[v,d]=e.useState(0),h=e.useRef(null),F=e.useRef(new Map),S=e.useRef([]),I=e.useRef(null),j=e.useRef(null),T=e.useRef(0),q=e.useRef(null),O=e.useRef(!1),z=e.useRef(0),G=e.useRef(null),K=e.useRef(new Map),[P,U]=e.useState(!1),N=e.useRef(E);N.current=E;const Z=e.useCallback((s,l)=>(K.current.set(s,l),()=>{K.current.get(s)===l&&K.current.delete(s)}),[]),k=e.useCallback(async s=>{for(const l of K.current.values()){if(!l.dirty)continue;if((l.onBeforeDiscard?await l.onBeforeDiscard(s):N.current?await N.current(s):typeof window<"u"&&typeof window.confirm=="function"?window.confirm(l.message||"有未保存的修改,确定放弃吗?"):!1)===!1)return!1}return!0},[]),C=e.useRef(!0),L=e.useRef(null),V=e.useRef(g);V.current=g;const D=e.useCallback((s,l)=>{C.current&&x(s);try{V.current?V.current(s,l):console.error("[react-modal-navigation]",s)}catch(y){console.error("[react-modal-navigation] onError failed",y)}},[]),ce=e.useCallback((s,l)=>{const y=F.current.get(s);F.current.delete(s),y?.(l)},[]),R=e.useCallback(s=>({...Ze,...o,...c!==void 0?{theme:c}:{},...a!==void 0?{backgroundColor:a}:{},...s.config.modalOptions,...s.overrides}),[o,c,a]),A=p?R(p):null,de=e.useRef(null);de.current=A,L.current=A;const W=e.useCallback(async(s="api",l,y={})=>{const f=h.current;if(!f||q.current||O.current)return!1;q.current=f.id,O.current=!0;const $=L.current??R(f),B={...y,reason:s,result:l,name:f.name};try{if(!await k(B)||await $.onBeforeClose?.(B)===!1||h.current?.id!==f.id||!C.current||(U(!0),await new Promise(H=>{const re=$.animation===!1||typeof window<"u"&&window.matchMedia?.("(prefers-reduced-motion: reduce)").matches?0:Math.max(0,Number($.animationDuration??220)||0);setTimeout(H,Math.min(re,1e3))}),!C.current||h.current?.id!==f.id))return!1;h.current=null,G.current=null,w(null),U(!1),K.current.clear(),ce(f.id,y.confirmed||l!==void 0?{status:"confirmed",value:l,reason:s}:{status:"dismissed",reason:s});try{await $.onClose?.(B)}catch(H){D(H,{phase:"onClose",name:f.name})}return!0}catch(H){return D(H,{phase:"onBeforeClose",name:f.name}),!1}finally{q.current===f.id&&(q.current=null),O.current=!1,!h.current&&C.current&&queueMicrotask(()=>j.current?.())}},[R,D,ce,k]),Q=e.useCallback(async(s,l={},y={},f,$)=>{const B=t?.[s];if(!B)throw new Error(`[react-modal-navigation] Unknown modal: ${s}`);const H=$??B.initialRoute;if(!B.screens?.[H])throw new Error(`[react-modal-navigation] Unknown initial screen '${H}' in modal '${s}'`);if(!C.current||O.current||q.current)return null;O.current=!0;const re=z.current;try{const Y=h.current;if(Y){const ae=L.current??R(Y),r={reason:"replace",name:Y.name,nextName:s};try{if(!await k(r)||await ae.onBeforeClose?.(r)===!1)return null}catch(i){return D(i,{phase:"onBeforeClose",name:Y.name}),null}if(!C.current||re!==z.current||h.current?.id!==Y.id)return null;h.current=null,G.current=null,w(null),K.current.clear(),ce(Y.id,{status:"replaced",reason:"replace"});try{await ae.onClose?.(r)}catch(i){D(i,{phase:"onClose",name:Y.name})}if(!C.current||re!==z.current)return null}const oe={id:++T.current,name:s,screen:H,params:l,config:B,overrides:y};return h.current=oe,G.current=null,f&&F.current.set(oe.id,f),x(null),w(oe),oe.id}finally{O.current=!1}},[t,R,D,ce,k]);I.current=Q;const ee=e.useCallback(async()=>{if(!C.current||h.current||O.current||q.current)return;const s=S.current.shift();if(s){d(S.current.length);try{const l=await I.current(s.args.name,s.args.params,s.args.options,s.resolveResult,s.args.screen);l===null?(s.resolveResult?.({status:"dismissed",reason:"blocked"}),s.resolveId?.(null),queueMicrotask(()=>j.current?.())):s.resolveId?.(l)}catch(l){s.reject?.(l),queueMicrotask(()=>j.current?.())}}},[]);j.current=ee;const J=e.useCallback((s,l,y)=>{const f=ye(s,l,y);return t?.[f.name]?new Promise(($,B)=>{S.current.push({args:f,resolveId:$,reject:B}),d(S.current.length),queueMicrotask(()=>j.current?.())}):Promise.reject(new Error(`[react-modal-navigation] Unknown modal: ${f.name}`))},[t]),X=e.useCallback((s,l,y)=>{const f=ye(s,l,y);return t?.[f.name]?new Promise(($,B)=>{S.current.push({args:f,resolveResult:$,reject:B}),d(S.current.length),queueMicrotask(()=>j.current?.())}):Promise.reject(new Error(`[react-modal-navigation] Unknown modal: ${f.name}`))},[t]),me=e.useCallback(s=>{if(!s||typeof s!="object"||Array.isArray(s))throw new TypeError("[react-modal-navigation] update expects an options object");const l=h.current;if(!l||O.current)return!1;const y={...l,overrides:{...l.overrides,...s}};return h.current=y,w(y),L.current=R(y),!0},[R]),ie=e.useCallback((s="queue-cleared")=>{const l=S.current.splice(0);d(0);for(const y of l)y.resolveId?.(null),y.resolveResult?.({status:"dismissed",reason:s});return l.length},[]),te=e.useCallback((s,l,y)=>{const f=ye(s,l,y);return Q(f.name,f.params,f.options,void 0,f.screen)},[Q]),pe=e.useCallback((s,l,y)=>{const f=ye(s,l,y);return new Promise(($,B)=>{Q(f.name,f.params,f.options,$,f.screen).then(H=>{H===null&&$({status:"dismissed",reason:"blocked"})},B)})},[Q]),le=e.useCallback(s=>W("api",s),[W]),ue=e.useCallback((s="api")=>W(s),[W]),he=e.useCallback(s=>W("confirm",s,{confirmed:!0}),[W]),ne=e.useCallback((s,l)=>G.current?.setLoading(s,l),[]),ge=e.useMemo(()=>({open:te,openAsync:pe,enqueue:J,enqueueAsync:X,update:me,clearQueue:ie,close:le,dismiss:ue,confirm:he,setLoading:ne,queueSize:v,isOpen:!!p,activeName:p?.name??null,error:M,clearError:()=>x(null)}),[te,pe,J,X,me,ie,le,ue,he,ne,p,M,v]);return e.useEffect(()=>(C.current=!0,()=>{C.current=!1,z.current+=1,h.current=null;for(const l of F.current.values())l({status:"dismissed",reason:"unmount"});F.current.clear(),K.current.clear();const s=S.current.splice(0);for(const l of s)l.resolveId?.(null),l.resolveResult?.({status:"dismissed",reason:"unmount"})}),[]),u.jsxs(Ne.Provider,{value:ge,children:[n,p&&u.jsx(Fe,{config:p.config,initialScreen:p.screen,params:p.params,options:A,translate:b,notifyError:D,error:M,clearError:()=>x(null),registerLoading:s=>{G.current=s},registerDirtyGuard:Z,exiting:P,onRequestClose:W,onOpened:s=>{try{const l=de.current?.onOpen?.(s);Promise.resolve(l).catch(y=>D(y,{phase:"onOpen"}))}catch(l){D(l,{phase:"onOpen"})}}},p.id)]})}function nt(){const t=e.useContext(Ne),n=e.useContext(Ce);if(!t)throw new Error("[react-modal-navigation] useModal requires <ModalProvider>");return e.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{...t,modal:t,navigation:{navigate:(...c)=>o().navigate(...c),replace:(...c)=>o().replace(...c),reset:(...c)=>o().reset(...c),setParams:(...c)=>o().setParams(...c),setOptions:(...c)=>o().setOptions(...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}}}},[t,n])}function rt(){const t=e.useContext(Ce);if(!t)throw new Error("useModalNavigation requires an active modal screen");return t}function Re(){const t=e.useContext(xe);if(!t)throw new Error("useIsFocused must be called inside a modal screen");return t.isFocused}function ot(t){const n=Re();e.useEffect(()=>{if(n)return t()},[n,t])}function at(t){const n=e.useContext(xe);if(!n)throw new Error("useNavigationResult requires an active modal screen");const o=e.useRef(t),a=e.useRef(null);e.useEffect(()=>{o.current=t},[t]),e.useEffect(()=>{!n.isFocused||!n.resultEvent||a.current!==n.resultEvent.id&&(a.current=n.resultEvent.id,o.current(n.resultEvent.data))},[n.isFocused,n.resultEvent])}function st(t={}){const n=e.useContext(xe);if(!n)throw new Error("[react-modal-navigation] useModalFooter requires an active modal screen");const{setFooter:o,routeKey:a}=n,c=e.useRef(t);c.current=t;const b=e.useMemo(()=>(...T)=>c.current.done?.(...T),[]),g=e.useMemo(()=>(...T)=>(c.current.cancel??c.current.cancle)?.(...T),[]),E=e.useMemo(()=>(...T)=>c.current.renderFooter?.(...T),[]),p=typeof t.done=="function",w=typeof(t.cancel??t.cancle)=="function",M=typeof t.renderFooter=="function",{showFooter:x,showFooterBorder:v,doneDisabled:d,doneText:h,cancelText:F,loadingText:S,footerClassName:I}=t,j=e.useMemo(()=>({showFooter:x,showFooterBorder:v,doneDisabled:d,doneText:h,cancelText:F,loadingText:S,footerClassName:I,...p?{done:b}:{},...w?{cancel:g}:{},...M?{renderFooter:E}:{}}),[x,v,d,h,F,S,I,p,w,M,b,g,E]);e.useEffect(()=>{o(a,j)},[a,o,j]),e.useEffect(()=>()=>o(a,null),[a,o])}function qe(){const t=e.useContext(Ee);if(!t)throw new Error("[react-modal-navigation] useModalLoading requires an active modal screen");return t}function ct(){const t=qe(),n=Re(),o=e.useRef(!0),a=e.useRef(!1),[c,b]=e.useState(!1),[g,E]=e.useState(null),p=e.useRef(t);return p.current=t,e.useEffect(()=>(o.current=!0,()=>{o.current=!1}),[]),{run:e.useCallback(async(M,x={})=>{if(a.current)return{status:"busy"};if(typeof M!="function")throw new TypeError("[react-modal-navigation] run requires a function");a.current=!0,o.current&&(b(!0),E(null));const v=p.current.showLoading(x.loadingText);try{const d=await M();return o.current&&await x.onSuccess?.(d),{status:"success",value:d}}catch(d){if(o.current){E(d);try{await x.onError?.(d)}catch(h){console.error("[react-modal-navigation] onError failed",h)}}if(x.throwOnError)throw d;return{status:"error",error:d}}finally{v!=null&&p.current.hideLoading(v),a.current=!1,o.current&&b(!1)}},[]),pending:c,error:g,clearError:()=>E(null),isFocused:n}}function it(t,n={}){const o=e.useContext(Le);if(!o)throw new Error("[react-modal-navigation] useModalUnsavedChanges must be used inside a modal screen");const a=e.useRef({dirty:t,...n});a.current={dirty:t,...n};const{routeKey:c,registerDirtyGuard:b}=o;e.useEffect(()=>b(c,{get dirty(){return!!a.current.dirty},get message(){return a.current.message},get onBeforeDiscard(){return a.current.onBeforeDiscard}}),[c,b])}const lt=()=>{const t=e.useContext(Pe);if(!t)throw new Error("[react-modal-navigation] useToast requires an active modal");return t};exports.ModalNavigator=Fe;exports.ModalProvider=tt;exports.useFocusEffect=ot;exports.useIsFocused=Re;exports.useModal=nt;exports.useModalFooter=st;exports.useModalLoading=qe;exports.useModalNavigation=rt;exports.useModalTask=ct;exports.useModalUnsavedChanges=it;exports.useNavigationResult=at;exports.useToast=lt;
|
package/dist/index.d.ts
CHANGED
|
@@ -175,3 +175,18 @@ export function useModalUnsavedChanges(dirty: boolean, options?: {
|
|
|
175
175
|
message?: string;
|
|
176
176
|
onBeforeDiscard?: (context: ModalEventContext) => boolean | Promise<boolean>;
|
|
177
177
|
}): void;
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
/** Notification displayed inside the current modal only. */
|
|
181
|
+
export type ToastType = 'success' | 'error' | 'warning' | 'info';
|
|
182
|
+
export interface ToastOptions { type?: ToastType; duration?: number; dismissible?: boolean }
|
|
183
|
+
export interface ToastActions {
|
|
184
|
+
(message: string, options?: ToastOptions | ToastType): number;
|
|
185
|
+
success(message: string, options?: Omit<ToastOptions, 'type'>): number;
|
|
186
|
+
error(message: string, options?: Omit<ToastOptions, 'type'>): number;
|
|
187
|
+
warning(message: string, options?: Omit<ToastOptions, 'type'>): number;
|
|
188
|
+
info(message: string, options?: Omit<ToastOptions, 'type'>): number;
|
|
189
|
+
dismiss(id: number): void;
|
|
190
|
+
clear(): void;
|
|
191
|
+
}
|
|
192
|
+
export function useToast(): ToastActions;
|