ui-lib.tsx 14 KB

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