2026-10-28 17:30:35 +08:00
|
|
|
import {Text} from './typography';
|
2026-10-18 08:49:30 +08:00
|
|
|
import React,{useEffect,useState} from 'react';
|
2026-10-28 17:30:35 +08:00
|
|
|
import {Alert,Modal,Platform,View,type AlertButton} from 'react-native';
|
2026-10-18 08:49:30 +08:00
|
|
|
import {Button,s,useTheme} from './ui';
|
|
|
|
|
type Prompt={title:string;message?:string;buttons:AlertButton[]};
|
|
|
|
|
let present:((prompt:Prompt)=>void)|undefined;
|
|
|
|
|
// React Native Web has no Alert implementation. Keep preview confirmations functional,
|
|
|
|
|
// while Android uses its familiar accessible native confirmation dialog.
|
|
|
|
|
export const Dialog={alert:(title:string,message?:string,buttons:AlertButton[]=[{text:'知道了'}])=>{
|
|
|
|
|
if(Platform.OS!=='web')Alert.alert(title,message,buttons);else present?.({title,message,buttons});
|
|
|
|
|
}};
|
|
|
|
|
export function PreviewDialogs(){
|
|
|
|
|
const t=useTheme(),[prompt,setPrompt]=useState<Prompt|null>(null);
|
|
|
|
|
useEffect(()=>{present=p=>setPrompt(old=>old||p);return()=>{present=undefined;};},[]);
|
2026-10-20 10:04:17 +08:00
|
|
|
// Mount the portal only when needed. A permanently mounted hidden Modal keeps
|
|
|
|
|
// its original DOM position and can sit underneath a later stock-operation Modal.
|
|
|
|
|
if(Platform.OS!=='web'||!prompt)return null;
|
2026-10-18 08:49:30 +08:00
|
|
|
function dismiss(){const cancel=prompt?.buttons.find(b=>b.style==='cancel');if(cancel){setPrompt(null);cancel.onPress?.();}}
|
2026-10-28 17:30:35 +08:00
|
|
|
return <Modal transparent visible={!!prompt} animationType="fade" onRequestClose={dismiss}><View style={{flex:1,backgroundColor:'rgba(10,20,35,.45)',justifyContent:'center',alignItems:'center',padding:24}}><View accessibilityRole="alert" style={{backgroundColor:t.card,borderRadius:22,padding:24,gap:18,maxWidth:380,width:'100%'}}><Text style={{color:t.text,fontSize:18,fontWeight:'700'}}>{prompt?.title}</Text><Text style={{color:t.muted,lineHeight:24}}>{prompt?.message}</Text>{prompt?.buttons.map((button,i)=><Button key={i} title={button.text||'确定'} secondary={button.style==='cancel'} onPress={()=>{setPrompt(null);button.onPress?.();}}/>)}</View></View></Modal>;
|
2026-10-18 08:49:30 +08:00
|
|
|
}
|