host.tsx 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use client";
  2. import clsx from "clsx";
  3. import { AnimatePresence, motion } from "framer-motion";
  4. import { confirmStore } from "./store";
  5. import { useConfirmQueue } from "./useConfirm";
  6. export function ConfirmHost() {
  7. const {queue} = useConfirmQueue();
  8. const current = queue[0];
  9. // if (!current) return null;
  10. const close = (result: boolean) => {
  11. current.resolve(result);
  12. confirmStore.setState({
  13. queue: queue.slice(1),
  14. });
  15. };
  16. return (
  17. <AnimatePresence>
  18. {current && <motion.div
  19. layout
  20. initial={{ opacity: 0, }}
  21. animate={{ opacity: 1 }}
  22. exit={{ opacity: 0 }}
  23. className="fixed z-1000 flex justify-center items-center bg-black/50 inset-0"
  24. onClick={(e) => e.stopPropagation()}
  25. >
  26. <div className="relative w-80 px-3 pb-6 pt-4 bg-white rounded-xl">
  27. <div className={clsx("text-center text-ly-16 font-bold mb-3", !current.title && 'text-white')}>
  28. {current.title ? current.title : 'notice'}
  29. </div>
  30. {Array.isArray(current.content) ?
  31. current.content.map((txt,i) => {
  32. return <p key={i} className="text-ly-16 leading-5 text-center">
  33. {txt}
  34. </p>
  35. })
  36. :
  37. <p className="text-ly-16 leading-5 text-center">{current.content}</p>
  38. }
  39. <div className="w-full flex justify-center flex-wrap gap-4 mt-4">
  40. {!current.noOk && <button type="button"
  41. className="grow shrink basis-auto min-w-[calc(50%_-_0.5rem)] box-border h-12 border-1 flex justify-center items-center rounded-3xl text-ly-16"
  42. onClick={() => close(true)}
  43. >
  44. {current.okBtnTxt ? current.okBtnTxt : 'Yes'}
  45. </button>}
  46. {!current.noCancel && <button type="button"
  47. className="grow shrink basis-auto min-w-[calc(50%_-_0.5rem)] h-12 flex justify-center items-center rounded-3xl bg-ly-middlegreen text-ly-16 text-white"
  48. onClick={() => close(false)}
  49. >
  50. {current.cancelBtnTxt ? current.cancelBtnTxt : 'Cancel'}
  51. </button>}
  52. </div>
  53. {!current.noCancel &&
  54. <button type="button" className="absolute top-4 right-4 h-6 w-6" onClick={() => close(false)}>
  55. <svg className="block w-full h-full" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24" fill="none"><path stroke="rgba(0, 0, 0, 1)" strokeWidth="1.2" strokeLinejoin="round" strokeLinecap="round" d="M7 7L17 17"></path><path stroke="rgba(0, 0, 0, 1)" strokeWidth="1.2" strokeLinejoin="round" strokeLinecap="round" d="M7 17L17 7"></path></svg>
  56. </button>
  57. }
  58. </div>
  59. </motion.div>}
  60. </AnimatePresence>
  61. );
  62. }