ui-lib.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /* eslint-disable @next/next/no-img-element */
  2. import styles from "./ui-lib.module.scss";
  3. import LoadingIcon from "../icons/three-dots.svg";
  4. import CloseIcon from "../icons/close.svg";
  5. import EyeIcon from "../icons/eye.svg";
  6. import EyeOffIcon from "../icons/eye-off.svg";
  7. import DownIcon from "../icons/down.svg";
  8. import ConfirmIcon from "../icons/confirm.svg";
  9. import CancelIcon from "../icons/cancel.svg";
  10. import MaxIcon from "../icons/max.svg";
  11. import MinIcon from "../icons/min.svg";
  12. import Locale from "../locales";
  13. import { createRoot } from "react-dom/client";
  14. import React, {
  15. CSSProperties,
  16. HTMLProps,
  17. MouseEvent,
  18. useEffect,
  19. useState,
  20. useCallback,
  21. useRef,
  22. } from "react";
  23. import { IconButton } from "./button";
  24. export function Popover(props: {
  25. children: JSX.Element;
  26. content: JSX.Element;
  27. open?: boolean;
  28. onClose?: () => void;
  29. }) {
  30. return (
  31. <div className={styles.popover}>
  32. {props.children}
  33. {props.open && (
  34. <div className={styles["popover-mask"]} onClick={props.onClose}></div>
  35. )}
  36. {props.open && (
  37. <div className={styles["popover-content"]}>{props.content}</div>
  38. )}
  39. </div>
  40. );
  41. }
  42. export function Card(props: { children: JSX.Element[]; className?: string }) {
  43. return (
  44. <div className={styles.card + " " + props.className}>{props.children}</div>
  45. );
  46. }
  47. export function ListItem(props: {
  48. title: string;
  49. subTitle?: string;
  50. children?: JSX.Element | JSX.Element[];
  51. icon?: JSX.Element;
  52. className?: string;
  53. onClick?: (event: MouseEvent) => void;
  54. vertical?: boolean;
  55. }) {
  56. return (
  57. <div
  58. className={
  59. styles["list-item"] +
  60. ` ${props.vertical ? styles["vertical"] : ""} ` +
  61. ` ${props.className || ""}`
  62. }
  63. onClick={props.onClick}
  64. >
  65. <div className={styles["list-header"]}>
  66. {props.icon && <div className={styles["list-icon"]}>{props.icon}</div>}
  67. <div className={styles["list-item-title"]}>
  68. <div>{props.title}</div>
  69. {props.subTitle && (
  70. <div className={styles["list-item-sub-title"]}>
  71. {props.subTitle}
  72. </div>
  73. )}
  74. </div>
  75. </div>
  76. {props.children}
  77. </div>
  78. );
  79. }
  80. export function List(props: { children: React.ReactNode; id?: string }) {
  81. return (
  82. <div className={styles.list} id={props.id}>
  83. {props.children}
  84. </div>
  85. );
  86. }
  87. export function Loading() {
  88. return (
  89. <div
  90. style={{
  91. height: "100vh",
  92. width: "100vw",
  93. display: "flex",
  94. alignItems: "center",
  95. justifyContent: "center",
  96. }}
  97. >
  98. <LoadingIcon />
  99. </div>
  100. );
  101. }
  102. interface ModalProps {
  103. title: string;
  104. children?: any;
  105. actions?: React.ReactNode[];
  106. defaultMax?: boolean;
  107. footer?: React.ReactNode;
  108. onClose?: () => void;
  109. }
  110. export function Modal(props: ModalProps) {
  111. useEffect(() => {
  112. const onKeyDown = (e: KeyboardEvent) => {
  113. if (e.key === "Escape") {
  114. props.onClose?.();
  115. }
  116. };
  117. window.addEventListener("keydown", onKeyDown);
  118. return () => {
  119. window.removeEventListener("keydown", onKeyDown);
  120. };
  121. // eslint-disable-next-line react-hooks/exhaustive-deps
  122. }, []);
  123. const [isMax, setMax] = useState(!!props.defaultMax);
  124. return (
  125. <div
  126. className={
  127. styles["modal-container"] + ` ${isMax && styles["modal-container-max"]}`
  128. }
  129. >
  130. <div className={styles["modal-header"]}>
  131. <div className={styles["modal-title"]}>{props.title}</div>
  132. <div className={styles["modal-header-actions"]}>
  133. <div
  134. className={styles["modal-header-action"]}
  135. onClick={() => setMax(!isMax)}
  136. >
  137. {isMax ? <MinIcon /> : <MaxIcon />}
  138. </div>
  139. <div
  140. className={styles["modal-header-action"]}
  141. onClick={props.onClose}
  142. >
  143. <CloseIcon />
  144. </div>
  145. </div>
  146. </div>
  147. <div className={styles["modal-content"]}>{props.children}</div>
  148. <div className={styles["modal-footer"]}>
  149. {props.footer}
  150. <div className={styles["modal-actions"]}>
  151. {props.actions?.map((action, i) => (
  152. <div key={i} className={styles["modal-action"]}>
  153. {action}
  154. </div>
  155. ))}
  156. </div>
  157. </div>
  158. </div>
  159. );
  160. }
  161. export function showModal(props: ModalProps) {
  162. const div = document.createElement("div");
  163. div.className = "modal-mask";
  164. document.body.appendChild(div);
  165. const root = createRoot(div);
  166. const closeModal = () => {
  167. props.onClose?.();
  168. root.unmount();
  169. div.remove();
  170. };
  171. div.onclick = (e) => {
  172. if (e.target === div) {
  173. closeModal();
  174. }
  175. };
  176. root.render(<Modal {...props} onClose={closeModal}></Modal>);
  177. }
  178. export type ToastProps = {
  179. content: string;
  180. action?: {
  181. text: string;
  182. onClick: () => void;
  183. };
  184. onClose?: () => void;
  185. };
  186. export function Toast(props: ToastProps) {
  187. return (
  188. <div className={styles["toast-container"]}>
  189. <div className={styles["toast-content"]}>
  190. <span>{props.content}</span>
  191. {props.action && (
  192. <button
  193. onClick={() => {
  194. props.action?.onClick?.();
  195. props.onClose?.();
  196. }}
  197. className={styles["toast-action"]}
  198. >
  199. {props.action.text}
  200. </button>
  201. )}
  202. </div>
  203. </div>
  204. );
  205. }
  206. export function showToast(
  207. content: string,
  208. action?: ToastProps["action"],
  209. delay = 3000,
  210. ) {
  211. const div = document.createElement("div");
  212. div.className = styles.show;
  213. document.body.appendChild(div);
  214. const root = createRoot(div);
  215. const close = () => {
  216. div.classList.add(styles.hide);
  217. setTimeout(() => {
  218. root.unmount();
  219. div.remove();
  220. }, 300);
  221. };
  222. setTimeout(() => {
  223. close();
  224. }, delay);
  225. root.render(<Toast content={content} action={action} onClose={close} />);
  226. }
  227. export type InputProps = React.HTMLProps<HTMLTextAreaElement> & {
  228. autoHeight?: boolean;
  229. rows?: number;
  230. };
  231. export function Input(props: InputProps) {
  232. return (
  233. <textarea
  234. {...props}
  235. className={`${styles["input"]} ${props.className}`}
  236. ></textarea>
  237. );
  238. }
  239. export function PasswordInput(props: HTMLProps<HTMLInputElement>) {
  240. const [visible, setVisible] = useState(false);
  241. function changeVisibility() {
  242. setVisible(!visible);
  243. }
  244. return (
  245. <div className={"password-input-container"}>
  246. <IconButton
  247. icon={visible ? <EyeIcon /> : <EyeOffIcon />}
  248. onClick={changeVisibility}
  249. className={"password-eye"}
  250. />
  251. <input
  252. {...props}
  253. type={visible ? "text" : "password"}
  254. className={"password-input"}
  255. />
  256. </div>
  257. );
  258. }
  259. export function Select(
  260. props: React.DetailedHTMLProps<
  261. React.SelectHTMLAttributes<HTMLSelectElement>,
  262. HTMLSelectElement
  263. >,
  264. ) {
  265. const { className, children, ...otherProps } = props;
  266. return (
  267. <div className={`${styles["select-with-icon"]} ${className}`}>
  268. <select className={styles["select-with-icon-select"]} {...otherProps}>
  269. {children}
  270. </select>
  271. <DownIcon className={styles["select-with-icon-icon"]} />
  272. </div>
  273. );
  274. }
  275. export function showConfirm(content: any) {
  276. const div = document.createElement("div");
  277. div.className = "modal-mask";
  278. document.body.appendChild(div);
  279. const root = createRoot(div);
  280. const closeModal = () => {
  281. root.unmount();
  282. div.remove();
  283. };
  284. return new Promise<boolean>((resolve) => {
  285. root.render(
  286. <Modal
  287. title={Locale.UI.Confirm}
  288. actions={[
  289. <IconButton
  290. key="cancel"
  291. text={Locale.UI.Cancel}
  292. onClick={() => {
  293. resolve(false);
  294. closeModal();
  295. }}
  296. icon={<CancelIcon />}
  297. tabIndex={0}
  298. bordered
  299. shadow
  300. ></IconButton>,
  301. <IconButton
  302. key="confirm"
  303. text={Locale.UI.Confirm}
  304. type="primary"
  305. onClick={() => {
  306. resolve(true);
  307. closeModal();
  308. }}
  309. icon={<ConfirmIcon />}
  310. tabIndex={0}
  311. autoFocus
  312. bordered
  313. shadow
  314. ></IconButton>,
  315. ]}
  316. onClose={closeModal}
  317. >
  318. {content}
  319. </Modal>,
  320. );
  321. });
  322. }
  323. function PromptInput(props: {
  324. value: string;
  325. onChange: (value: string) => void;
  326. rows?: number;
  327. }) {
  328. const [input, setInput] = useState(props.value);
  329. const onInput = (value: string) => {
  330. props.onChange(value);
  331. setInput(value);
  332. };
  333. return (
  334. <textarea
  335. className={styles["modal-input"]}
  336. autoFocus
  337. value={input}
  338. onInput={(e) => onInput(e.currentTarget.value)}
  339. rows={props.rows ?? 3}
  340. ></textarea>
  341. );
  342. }
  343. export function showPrompt(content: any, value = "", rows = 3) {
  344. const div = document.createElement("div");
  345. div.className = "modal-mask";
  346. document.body.appendChild(div);
  347. const root = createRoot(div);
  348. const closeModal = () => {
  349. root.unmount();
  350. div.remove();
  351. };
  352. return new Promise<string>((resolve) => {
  353. let userInput = value;
  354. root.render(
  355. <Modal
  356. title={content}
  357. actions={[
  358. <IconButton
  359. key="cancel"
  360. text={Locale.UI.Cancel}
  361. onClick={() => {
  362. closeModal();
  363. }}
  364. icon={<CancelIcon />}
  365. bordered
  366. shadow
  367. tabIndex={0}
  368. ></IconButton>,
  369. <IconButton
  370. key="confirm"
  371. text={Locale.UI.Confirm}
  372. type="primary"
  373. onClick={() => {
  374. resolve(userInput);
  375. closeModal();
  376. }}
  377. icon={<ConfirmIcon />}
  378. bordered
  379. shadow
  380. tabIndex={0}
  381. ></IconButton>,
  382. ]}
  383. onClose={closeModal}
  384. >
  385. <PromptInput
  386. onChange={(val) => (userInput = val)}
  387. value={value}
  388. rows={rows}
  389. ></PromptInput>
  390. </Modal>,
  391. );
  392. });
  393. }
  394. export function showImageModal(
  395. img: string,
  396. defaultMax?: boolean,
  397. style?: CSSProperties,
  398. boxStyle?: CSSProperties,
  399. ) {
  400. showModal({
  401. title: Locale.Export.Image.Modal,
  402. defaultMax: defaultMax,
  403. children: (
  404. <div style={{ display: "flex", justifyContent: "center", ...boxStyle }}>
  405. <img
  406. src={img}
  407. alt="preview"
  408. style={
  409. style ?? {
  410. maxWidth: "100%",
  411. }
  412. }
  413. ></img>
  414. </div>
  415. ),
  416. });
  417. }
  418. export function Selector<T>(props: {
  419. items: Array<{
  420. title: string;
  421. subTitle?: string;
  422. value: T;
  423. disable?: boolean;
  424. }>;
  425. defaultSelectedValue?: T[] | T;
  426. onSelection?: (selection: T[]) => void;
  427. onClose?: () => void;
  428. multiple?: boolean;
  429. }) {
  430. return (
  431. <div className={styles["selector"]} onClick={() => props.onClose?.()}>
  432. <div className={styles["selector-content"]}>
  433. <List>
  434. {props.items.map((item, i) => {
  435. const selected = props.multiple
  436. ? // @ts-ignore
  437. props.defaultSelectedValue?.includes(item.value)
  438. : props.defaultSelectedValue === item.value;
  439. return (
  440. <ListItem
  441. className={`${styles["selector-item"]} ${
  442. item.disable && styles["selector-item-disabled"]
  443. }`}
  444. key={i}
  445. title={item.title}
  446. subTitle={item.subTitle}
  447. onClick={(event) => {
  448. event.stopPropagation();
  449. if (!item.disable) {
  450. props.onSelection?.([item.value]);
  451. props.onClose?.();
  452. }
  453. }}
  454. >
  455. {selected ? (
  456. <div
  457. style={{
  458. height: 10,
  459. width: 10,
  460. backgroundColor: "var(--primary)",
  461. borderRadius: 10,
  462. }}
  463. ></div>
  464. ) : (
  465. <></>
  466. )}
  467. </ListItem>
  468. );
  469. })}
  470. </List>
  471. </div>
  472. </div>
  473. );
  474. }
  475. export function FullScreen(props: any) {
  476. const { children, right = 10, top = 10, ...rest } = props;
  477. const ref = useRef<HTMLDivElement>();
  478. const [fullScreen, setFullScreen] = useState(false);
  479. const toggleFullscreen = useCallback(() => {
  480. if (!document.fullscreenElement) {
  481. ref.current?.requestFullscreen();
  482. } else {
  483. document.exitFullscreen();
  484. }
  485. }, []);
  486. useEffect(() => {
  487. document.addEventListener("fullscreenchange", (e) => {
  488. if (e.target === ref.current) {
  489. setFullScreen(!!document.fullscreenElement);
  490. }
  491. });
  492. }, []);
  493. return (
  494. <div ref={ref} style={{ position: "relative" }} {...rest}>
  495. <div style={{ position: "absolute", right, top }}>
  496. <IconButton
  497. icon={fullScreen ? <MinIcon /> : <MaxIcon />}
  498. onClick={toggleFullscreen}
  499. bordered
  500. />
  501. </div>
  502. {children}
  503. </div>
  504. );
  505. }