ui-lib.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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 | JSX.Element;
  50. children?: JSX.Element | JSX.Element[];
  51. icon?: JSX.Element;
  52. className?: string;
  53. onClick?: (e: 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(
  240. props: HTMLProps<HTMLInputElement> & { aria?: string },
  241. ) {
  242. const [visible, setVisible] = useState(false);
  243. function changeVisibility() {
  244. setVisible(!visible);
  245. }
  246. return (
  247. <div className={"password-input-container"}>
  248. <IconButton
  249. aria={props.aria}
  250. icon={visible ? <EyeIcon /> : <EyeOffIcon />}
  251. onClick={changeVisibility}
  252. className={"password-eye"}
  253. />
  254. <input
  255. {...props}
  256. type={visible ? "text" : "password"}
  257. className={"password-input"}
  258. />
  259. </div>
  260. );
  261. }
  262. export function Select(
  263. props: React.DetailedHTMLProps<
  264. React.SelectHTMLAttributes<HTMLSelectElement> & {
  265. align?: "left" | "center";
  266. },
  267. HTMLSelectElement
  268. >,
  269. ) {
  270. const { className, children, align, ...otherProps } = props;
  271. return (
  272. <div
  273. className={`${styles["select-with-icon"]} ${
  274. align === "left" ? styles["left-align-option"] : ""
  275. } ${className}`}
  276. >
  277. <select className={styles["select-with-icon-select"]} {...otherProps}>
  278. {children}
  279. </select>
  280. <DownIcon className={styles["select-with-icon-icon"]} />
  281. </div>
  282. );
  283. }
  284. export function showConfirm(content: any) {
  285. const div = document.createElement("div");
  286. div.className = "modal-mask";
  287. document.body.appendChild(div);
  288. const root = createRoot(div);
  289. const closeModal = () => {
  290. root.unmount();
  291. div.remove();
  292. };
  293. return new Promise<boolean>((resolve) => {
  294. root.render(
  295. <Modal
  296. title={Locale.UI.Confirm}
  297. actions={[
  298. <IconButton
  299. key="cancel"
  300. text={Locale.UI.Cancel}
  301. onClick={() => {
  302. resolve(false);
  303. closeModal();
  304. }}
  305. icon={<CancelIcon />}
  306. tabIndex={0}
  307. bordered
  308. shadow
  309. ></IconButton>,
  310. <IconButton
  311. key="confirm"
  312. text={Locale.UI.Confirm}
  313. type="primary"
  314. onClick={() => {
  315. resolve(true);
  316. closeModal();
  317. }}
  318. icon={<ConfirmIcon />}
  319. tabIndex={0}
  320. autoFocus
  321. bordered
  322. shadow
  323. ></IconButton>,
  324. ]}
  325. onClose={closeModal}
  326. >
  327. {content}
  328. </Modal>,
  329. );
  330. });
  331. }
  332. function PromptInput(props: {
  333. value: string;
  334. onChange: (value: string) => void;
  335. rows?: number;
  336. }) {
  337. const [input, setInput] = useState(props.value);
  338. const onInput = (value: string) => {
  339. props.onChange(value);
  340. setInput(value);
  341. };
  342. return (
  343. <textarea
  344. className={styles["modal-input"]}
  345. autoFocus
  346. value={input}
  347. onInput={(e) => onInput(e.currentTarget.value)}
  348. rows={props.rows ?? 3}
  349. ></textarea>
  350. );
  351. }
  352. export function showPrompt(content: any, value = "", rows = 3) {
  353. const div = document.createElement("div");
  354. div.className = "modal-mask";
  355. document.body.appendChild(div);
  356. const root = createRoot(div);
  357. const closeModal = () => {
  358. root.unmount();
  359. div.remove();
  360. };
  361. return new Promise<string>((resolve) => {
  362. let userInput = value;
  363. root.render(
  364. <Modal
  365. title={content}
  366. actions={[
  367. <IconButton
  368. key="cancel"
  369. text={Locale.UI.Cancel}
  370. onClick={() => {
  371. closeModal();
  372. }}
  373. icon={<CancelIcon />}
  374. bordered
  375. shadow
  376. tabIndex={0}
  377. ></IconButton>,
  378. <IconButton
  379. key="confirm"
  380. text={Locale.UI.Confirm}
  381. type="primary"
  382. onClick={() => {
  383. resolve(userInput);
  384. closeModal();
  385. }}
  386. icon={<ConfirmIcon />}
  387. bordered
  388. shadow
  389. tabIndex={0}
  390. ></IconButton>,
  391. ]}
  392. onClose={closeModal}
  393. >
  394. <PromptInput
  395. onChange={(val) => (userInput = val)}
  396. value={value}
  397. rows={rows}
  398. ></PromptInput>
  399. </Modal>,
  400. );
  401. });
  402. }
  403. export function showImageModal(
  404. img: string,
  405. defaultMax?: boolean,
  406. style?: CSSProperties,
  407. boxStyle?: CSSProperties,
  408. ) {
  409. showModal({
  410. title: Locale.Export.Image.Modal,
  411. defaultMax: defaultMax,
  412. children: (
  413. <div style={{ display: "flex", justifyContent: "center", ...boxStyle }}>
  414. <img
  415. src={img}
  416. alt="preview"
  417. style={
  418. style ?? {
  419. maxWidth: "100%",
  420. }
  421. }
  422. ></img>
  423. </div>
  424. ),
  425. });
  426. }
  427. export function Selector<T>(props: {
  428. items: Array<{
  429. title: string;
  430. subTitle?: string;
  431. value: T;
  432. disable?: boolean;
  433. }>;
  434. defaultSelectedValue?: T[] | T;
  435. onSelection?: (selection: T[]) => void;
  436. onClose?: () => void;
  437. multiple?: boolean;
  438. }) {
  439. const [selectedValues, setSelectedValues] = useState<T[]>(
  440. Array.isArray(props.defaultSelectedValue)
  441. ? props.defaultSelectedValue
  442. : props.defaultSelectedValue !== undefined
  443. ? [props.defaultSelectedValue]
  444. : [],
  445. );
  446. const handleSelection = (e: MouseEvent, value: T) => {
  447. if (props.multiple) {
  448. e.stopPropagation();
  449. const newSelectedValues = selectedValues.includes(value)
  450. ? selectedValues.filter((v) => v !== value)
  451. : [...selectedValues, value];
  452. setSelectedValues(newSelectedValues);
  453. props.onSelection?.(newSelectedValues);
  454. } else {
  455. setSelectedValues([value]);
  456. props.onSelection?.([value]);
  457. props.onClose?.();
  458. }
  459. };
  460. return (
  461. <div className={styles["selector"]} onClick={() => props.onClose?.()}>
  462. <div className={styles["selector-content"]}>
  463. <List>
  464. {props.items.map((item, i) => {
  465. const selected = selectedValues.includes(item.value);
  466. return (
  467. <ListItem
  468. className={`${styles["selector-item"]} ${
  469. item.disable && styles["selector-item-disabled"]
  470. }`}
  471. key={i}
  472. title={item.title}
  473. subTitle={item.subTitle}
  474. onClick={(e) => {
  475. if (item.disable) {
  476. e.stopPropagation();
  477. } else {
  478. handleSelection(e, item.value);
  479. }
  480. }}
  481. >
  482. {selected ? (
  483. <div
  484. style={{
  485. height: 10,
  486. width: 10,
  487. backgroundColor: "var(--primary)",
  488. borderRadius: 10,
  489. }}
  490. ></div>
  491. ) : (
  492. <></>
  493. )}
  494. </ListItem>
  495. );
  496. })}
  497. </List>
  498. </div>
  499. </div>
  500. );
  501. }
  502. export function FullScreen(props: any) {
  503. const { children, right = 10, top = 10, ...rest } = props;
  504. const ref = useRef<HTMLDivElement>();
  505. const [fullScreen, setFullScreen] = useState(false);
  506. const toggleFullscreen = useCallback(() => {
  507. if (!document.fullscreenElement) {
  508. ref.current?.requestFullscreen();
  509. } else {
  510. document.exitFullscreen();
  511. }
  512. }, []);
  513. useEffect(() => {
  514. const handleScreenChange = (e: any) => {
  515. if (e.target === ref.current) {
  516. setFullScreen(!!document.fullscreenElement);
  517. }
  518. };
  519. document.addEventListener("fullscreenchange", handleScreenChange);
  520. return () => {
  521. document.removeEventListener("fullscreenchange", handleScreenChange);
  522. };
  523. }, []);
  524. return (
  525. <div ref={ref} style={{ position: "relative" }} {...rest}>
  526. <div style={{ position: "absolute", right, top }}>
  527. <IconButton
  528. icon={fullScreen ? <MinIcon /> : <MaxIcon />}
  529. onClick={toggleFullscreen}
  530. bordered
  531. />
  532. </div>
  533. {children}
  534. </div>
  535. );
  536. }