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