1
0

Dialog.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. // hoist the class declaration because either rollup or babel is being a hoe
  2. import { addGlobalStyle } from "./dom.js";
  3. import { NanoEmitter } from "./NanoEmitter.js";
  4. export const defaultDialogCss = `\
  5. .uu-no-select {
  6. user-select: none;
  7. }
  8. .uu-dialog-bg {
  9. --uu-dialog-bg: #333333;
  10. --uu-dialog-bg-highlight: #252525;
  11. --uu-scroll-indicator-bg: rgba(10, 10, 10, 0.7);
  12. --uu-dialog-separator-color: #797979;
  13. --uu-dialog-border-radius: 10px;
  14. }
  15. .uu-dialog-bg {
  16. display: block;
  17. position: fixed;
  18. width: 100%;
  19. height: 100%;
  20. top: 0;
  21. left: 0;
  22. z-index: 5;
  23. background-color: rgba(0, 0, 0, 0.6);
  24. }
  25. .uu-dialog {
  26. --uu-calc-dialog-height: calc(min(100vh - 40px, var(--uu-dialog-height-max)));
  27. position: absolute;
  28. display: flex;
  29. flex-direction: column;
  30. width: calc(min(100% - 60px, var(--uu-dialog-width-max)));
  31. border-radius: var(--uu-dialog-border-radius);
  32. height: auto;
  33. max-height: var(--uu-calc-dialog-height);
  34. left: 50%;
  35. top: 50%;
  36. transform: translate(-50%, -50%);
  37. z-index: 6;
  38. color: #fff;
  39. background-color: var(--uu-dialog-bg);
  40. }
  41. .uu-dialog.align-top {
  42. top: 0;
  43. transform: translate(-50%, 40px);
  44. }
  45. .uu-dialog.align-bottom {
  46. top: 100%;
  47. transform: translate(-50%, -100%);
  48. }
  49. .uu-dialog-body {
  50. font-size: 1.5rem;
  51. padding: 20px;
  52. }
  53. .uu-dialog-body.small {
  54. padding: 15px;
  55. }
  56. #uu-dialog-opts {
  57. display: flex;
  58. flex-direction: column;
  59. position: relative;
  60. padding: 30px 0px;
  61. overflow-y: auto;
  62. }
  63. .uu-dialog-header {
  64. display: flex;
  65. justify-content: space-between;
  66. align-items: center;
  67. margin-bottom: 6px;
  68. padding: 15px 20px 15px 20px;
  69. background-color: var(--uu-dialog-bg);
  70. border: 2px solid var(--uu-dialog-separator-color);
  71. border-style: none none solid none !important;
  72. border-radius: var(--uu-dialog-border-radius) var(--uu-dialog-border-radius) 0px 0px;
  73. }
  74. .uu-dialog-header.small {
  75. padding: 10px 15px;
  76. border-style: none none solid none !important;
  77. }
  78. .uu-dialog-header-pad {
  79. content: " ";
  80. min-height: 32px;
  81. }
  82. .uu-dialog-header-pad.small {
  83. min-height: 24px;
  84. }
  85. .uu-dialog-titlecont {
  86. display: flex;
  87. align-items: center;
  88. }
  89. .uu-dialog-titlecont-no-title {
  90. display: flex;
  91. justify-content: flex-end;
  92. align-items: center;
  93. }
  94. .uu-dialog-title {
  95. position: relative;
  96. display: inline-block;
  97. font-size: 22px;
  98. }
  99. .uu-dialog-close {
  100. cursor: pointer;
  101. }
  102. .uu-dialog-header-img,
  103. .uu-dialog-close
  104. {
  105. width: 32px;
  106. height: 32px;
  107. }
  108. .uu-dialog-header-img.small,
  109. .uu-dialog-close.small
  110. {
  111. width: 24px;
  112. height: 24px;
  113. }
  114. .uu-dialog-footer {
  115. font-size: 17px;
  116. text-decoration: underline;
  117. }
  118. .uu-dialog-footer.hidden {
  119. display: none;
  120. }
  121. .uu-dialog-footer-cont {
  122. margin-top: 6px;
  123. padding: 15px 20px;
  124. background: var(--uu-dialog-bg);
  125. background: linear-gradient(to bottom, rgba(0, 0, 0, 0) 0%, var(--uu-dialog-bg) 30%, var(--uu-dialog-bg) 100%);
  126. border: 2px solid var(--uu-dialog-separator-color);
  127. border-style: solid none none none !important;
  128. border-radius: 0px 0px var(--uu-dialog-border-radius) var(--uu-dialog-border-radius);
  129. }
  130. .uu-dialog-footer-buttons-cont button:not(:last-of-type) {
  131. margin-right: 15px;
  132. }`;
  133. /** ID of the last opened (top-most) dialog */
  134. export let currentDialogId: string | null = null;
  135. /** IDs of all currently open dialogs, top-most first */
  136. export const openDialogs: string[] = [];
  137. export const defaultStrings = {
  138. closeDialogTooltip: "Click to close the dialog",
  139. };
  140. /** Options passed to the Dialog constructor */
  141. export interface DialogOptions {
  142. /** ID that gets added to child element IDs - has to be unique and conform to HTML ID naming rules! */
  143. id: string;
  144. /** Target and max width of the dialog in pixels */
  145. width: number;
  146. /** Target and max height of the dialog in pixels */
  147. height: number;
  148. /** Whether the dialog should close when the background is clicked - defaults to true */
  149. closeOnBgClick?: boolean;
  150. /** Whether the dialog should close when the escape key is pressed - defaults to true */
  151. closeOnEscPress?: boolean;
  152. /** Whether the dialog should be destroyed when it's closed - defaults to false */
  153. destroyOnClose?: boolean;
  154. /** Whether the dialog should be unmounted when it's closed - defaults to true - superseded by destroyOnClose */
  155. unmountOnClose?: boolean;
  156. /** Whether all listeners should be removed when the dialog is destroyed - defaults to true */
  157. removeListenersOnDestroy?: boolean;
  158. /** Whether the dialog should have a smaller overall appearance - defaults to false */
  159. small?: boolean;
  160. /** Where to align or anchor the dialog vertically - defaults to "center" */
  161. verticalAlign?: "top" | "center" | "bottom";
  162. /** Strings used in the dialog (used for translations) - defaults to the default English strings */
  163. strings?: Partial<typeof defaultStrings>;
  164. /** CSS to apply to the dialog - defaults to the {@linkcode defaultDialogCss} */
  165. dialogCss?: string;
  166. /** Called to render the body of the dialog */
  167. renderBody: () => HTMLElement | Promise<HTMLElement>;
  168. /** Called to render the header of the dialog - leave undefined for a blank header */
  169. renderHeader?: () => HTMLElement | Promise<HTMLElement>;
  170. /** Called to render the footer of the dialog - leave undefined for no footer */
  171. renderFooter?: () => HTMLElement | Promise<HTMLElement>;
  172. /** Called to render the close button of the dialog - leave undefined for no close button */
  173. renderCloseBtn?: () => HTMLElement | Promise<HTMLElement>;
  174. }
  175. /** Creates and manages a modal dialog element */
  176. export class Dialog extends NanoEmitter<{
  177. /** Emitted just **after** the dialog is closed */
  178. close: () => void;
  179. /** Emitted just **after** the dialog is opened */
  180. open: () => void;
  181. /** Emitted just **after** the dialog contents are rendered */
  182. render: () => void;
  183. /** Emitted just **after** the dialog contents are cleared */
  184. clear: () => void;
  185. /** Emitted just **after** the dialog is destroyed and **before** all listeners are removed */
  186. destroy: () => void;
  187. }> {
  188. /** Options passed to the dialog in the constructor */
  189. public readonly options;
  190. /** ID that gets added to child element IDs - has to be unique and conform to HTML ID naming rules! */
  191. public readonly id;
  192. /** Strings used in the dialog (used for translations) */
  193. public strings;
  194. protected dialogOpen = false;
  195. protected dialogMounted = false;
  196. constructor(options: DialogOptions) {
  197. super();
  198. const { strings, ...opts } = options;
  199. this.strings = {
  200. ...defaultStrings,
  201. ...(strings ?? {}),
  202. };
  203. this.options = {
  204. closeOnBgClick: true,
  205. closeOnEscPress: true,
  206. destroyOnClose: false,
  207. unmountOnClose: true,
  208. removeListenersOnDestroy: true,
  209. smallHeader: false,
  210. verticalAlign: "center",
  211. dialogCss: defaultDialogCss,
  212. ...opts,
  213. };
  214. this.id = opts.id;
  215. }
  216. //#region public
  217. /** Call after DOMContentLoaded to pre-render the dialog and invisibly mount it in the DOM */
  218. public async mount() {
  219. if(this.dialogMounted)
  220. return;
  221. this.dialogMounted = true;
  222. if(!document.querySelector("style.uu-dialog-css"))
  223. addGlobalStyle(this.options.dialogCss).classList.add("uu-dialog-css");
  224. const bgElem = document.createElement("div");
  225. bgElem.id = `uu-${this.id}-dialog-bg`;
  226. bgElem.classList.add("uu-dialog-bg");
  227. if(this.options.closeOnBgClick)
  228. bgElem.ariaLabel = bgElem.title = this.getString("closeDialogTooltip");
  229. bgElem.style.setProperty("--uu-dialog-width-max", `${this.options.width}px`);
  230. bgElem.style.setProperty("--uu-dialog-height-max", `${this.options.height}px`);
  231. bgElem.style.visibility = "hidden";
  232. bgElem.style.display = "none";
  233. bgElem.inert = true;
  234. bgElem.appendChild(await this.getDialogContent());
  235. document.body.appendChild(bgElem);
  236. this.attachListeners(bgElem);
  237. this.events.emit("render");
  238. return bgElem;
  239. }
  240. /** Closes the dialog and clears all its contents (unmounts elements from the DOM) in preparation for a new rendering call */
  241. public unmount() {
  242. this.close();
  243. this.dialogMounted = false;
  244. const clearSelectors = [
  245. `#uu-${this.id}-dialog-bg`,
  246. `#uu-style-dialog-${this.id}`,
  247. ];
  248. for(const sel of clearSelectors)
  249. document.querySelector(sel)?.remove();
  250. this.events.emit("clear");
  251. }
  252. /** Clears the DOM of the dialog and then renders it again */
  253. public async remount() {
  254. this.unmount();
  255. await this.mount();
  256. }
  257. /**
  258. * Opens the dialog - also mounts it if it hasn't been mounted yet
  259. * Prevents default action and immediate propagation of the passed event
  260. */
  261. public async open(e?: MouseEvent | KeyboardEvent) {
  262. e?.preventDefault();
  263. e?.stopImmediatePropagation();
  264. if(this.isOpen())
  265. return;
  266. this.dialogOpen = true;
  267. if(openDialogs.includes(this.id))
  268. throw new Error(`A dialog with the same ID of '${this.id}' already exists and is open!`);
  269. if(!this.isMounted())
  270. await this.mount();
  271. const dialogBg = document.querySelector<HTMLElement>(`#uu-${this.id}-dialog-bg`);
  272. if(!dialogBg)
  273. return console.warn(`Couldn't find background element for dialog with ID '${this.id}'`);
  274. dialogBg.style.visibility = "visible";
  275. dialogBg.style.display = "block";
  276. dialogBg.inert = false;
  277. currentDialogId = this.id;
  278. openDialogs.unshift(this.id);
  279. // make sure all other dialogs are inert
  280. for(const dialogId of openDialogs)
  281. if(dialogId !== this.id)
  282. document.querySelector(`#uu-${dialogId}-dialog-bg`)?.setAttribute("inert", "true");
  283. // make sure body is inert and scroll is locked
  284. document.body.classList.remove("uu-no-select");
  285. document.body.setAttribute("inert", "true");
  286. this.events.emit("open");
  287. return dialogBg;
  288. }
  289. /** Closes the dialog - prevents default action and immediate propagation of the passed event */
  290. public close(e?: MouseEvent | KeyboardEvent) {
  291. e?.preventDefault();
  292. e?.stopImmediatePropagation();
  293. if(!this.isOpen())
  294. return;
  295. this.dialogOpen = false;
  296. const dialogBg = document.querySelector<HTMLElement>(`#uu-${this.id}-dialog-bg`);
  297. if(!dialogBg)
  298. return console.warn(`Couldn't find background element for dialog with ID '${this.id}'`);
  299. dialogBg.style.visibility = "hidden";
  300. dialogBg.style.display = "none";
  301. dialogBg.inert = true;
  302. openDialogs.splice(openDialogs.indexOf(this.id), 1);
  303. currentDialogId = openDialogs[0] ?? null;
  304. // make sure the new top-most dialog is not inert
  305. if(currentDialogId)
  306. document.querySelector(`#uu-${currentDialogId}-dialog-bg`)?.removeAttribute("inert");
  307. // remove the scroll lock and inert attribute on the body if no dialogs are open
  308. if(openDialogs.length === 0) {
  309. document.body.classList.add("uu-no-select");
  310. document.body.removeAttribute("inert");
  311. }
  312. this.events.emit("close");
  313. if(this.options.destroyOnClose)
  314. this.destroy();
  315. // don't destroy *and* unmount at the same time
  316. else if(this.options.unmountOnClose)
  317. this.unmount();
  318. }
  319. /** Returns true if the dialog is currently open */
  320. public isOpen() {
  321. return this.dialogOpen;
  322. }
  323. /** Returns true if the dialog is currently mounted */
  324. public isMounted() {
  325. return this.dialogMounted;
  326. }
  327. /** Clears the DOM of the dialog and removes all event listeners */
  328. public destroy() {
  329. this.unmount();
  330. this.events.emit("destroy");
  331. this.options.removeListenersOnDestroy && this.unsubscribeAll();
  332. }
  333. //#region static
  334. /** Returns the ID of the top-most dialog (the dialog that has been opened last) */
  335. public static getCurrentDialogId() {
  336. return currentDialogId;
  337. }
  338. /** Returns the IDs of all currently open dialogs, top-most first */
  339. public static getOpenDialogs() {
  340. return openDialogs;
  341. }
  342. //#region protected
  343. protected getString(key: keyof typeof defaultStrings) {
  344. return this.strings[key] ?? defaultStrings[key];
  345. }
  346. /** Called once to attach all generic event listeners */
  347. protected attachListeners(bgElem: HTMLElement) {
  348. if(this.options.closeOnBgClick) {
  349. bgElem.addEventListener("click", (e) => {
  350. if(this.isOpen() && (e.target as HTMLElement)?.id === `uu-${this.id}-dialog-bg`)
  351. this.close(e);
  352. });
  353. }
  354. if(this.options.closeOnEscPress) {
  355. document.body.addEventListener("keydown", (e) => {
  356. if(e.key === "Escape" && this.isOpen() && Dialog.getCurrentDialogId() === this.id)
  357. this.close(e);
  358. });
  359. }
  360. }
  361. //#region protected
  362. /**
  363. * Adds generic, accessible interaction listeners to the passed element.
  364. * All listeners have the default behavior prevented and stop propagation (for keyboard events only as long as the captured key is valid).
  365. * @param listenerOptions Provide a {@linkcode listenerOptions} object to configure the listeners
  366. */
  367. protected onInteraction<
  368. TElem extends HTMLElement
  369. > (
  370. elem: TElem,
  371. listener: (evt: MouseEvent | KeyboardEvent) => void,
  372. listenerOptions?: AddEventListenerOptions & {
  373. preventDefault?: boolean;
  374. stopPropagation?: boolean;
  375. },
  376. ) {
  377. const { preventDefault = true, stopPropagation = true, ...listenerOpts } = listenerOptions ?? {};
  378. const interactionKeys = ["Enter", " ", "Space"];
  379. const proxListener = (e: MouseEvent | KeyboardEvent) => {
  380. if(e instanceof KeyboardEvent) {
  381. if(interactionKeys.includes(e.key)) {
  382. preventDefault && e.preventDefault();
  383. stopPropagation && e.stopPropagation();
  384. }
  385. else return;
  386. }
  387. else if(e instanceof MouseEvent) {
  388. preventDefault && e.preventDefault();
  389. stopPropagation && e.stopPropagation();
  390. }
  391. // clean up the other listener that isn't automatically removed if `once` is set
  392. listenerOpts?.once && e.type === "keydown" && elem.removeEventListener("click", proxListener, listenerOpts);
  393. listenerOpts?.once && e.type === "click" && elem.removeEventListener("keydown", proxListener, listenerOpts);
  394. listener(e);
  395. };
  396. elem.addEventListener("click", proxListener, listenerOpts);
  397. elem.addEventListener("keydown", proxListener, listenerOpts);
  398. }
  399. /** Returns the dialog content element and all its children */
  400. protected async getDialogContent() {
  401. const header = this.options.renderHeader?.();
  402. const footer = this.options.renderFooter?.();
  403. const dialogWrapperEl = document.createElement("div");
  404. dialogWrapperEl.id = `uu-${this.id}-dialog`;
  405. dialogWrapperEl.classList.add("uu-dialog");
  406. dialogWrapperEl.ariaLabel = dialogWrapperEl.title = "";
  407. dialogWrapperEl.role = "dialog";
  408. dialogWrapperEl.setAttribute("aria-labelledby", `uu-${this.id}-dialog-title`);
  409. dialogWrapperEl.setAttribute("aria-describedby", `uu-${this.id}-dialog-body`);
  410. if(this.options.verticalAlign !== "center")
  411. dialogWrapperEl.classList.add(`align-${this.options.verticalAlign}`);
  412. //#region header
  413. const headerWrapperEl = document.createElement("div");
  414. headerWrapperEl.classList.add("uu-dialog-header");
  415. this.options.small && headerWrapperEl.classList.add("small");
  416. if(header) {
  417. const headerTitleWrapperEl = document.createElement("div");
  418. headerTitleWrapperEl.id = `uu-${this.id}-dialog-title`;
  419. headerTitleWrapperEl.classList.add("uu-dialog-title-wrapper");
  420. headerTitleWrapperEl.role = "heading";
  421. headerTitleWrapperEl.ariaLevel = "1";
  422. headerTitleWrapperEl.appendChild(header instanceof Promise ? await header : header);
  423. headerWrapperEl.appendChild(headerTitleWrapperEl);
  424. }
  425. else {
  426. // insert element to pad the header height
  427. const padEl = document.createElement("div");
  428. padEl.classList.add("uu-dialog-header-pad", this.options.small ? "small" : "");
  429. headerWrapperEl.appendChild(padEl);
  430. }
  431. if(this.options.renderCloseBtn) {
  432. const closeBtnEl = await this.options.renderCloseBtn();
  433. closeBtnEl.classList.add("uu-dialog-close");
  434. this.options.small && closeBtnEl.classList.add("small");
  435. closeBtnEl.tabIndex = 0;
  436. if(closeBtnEl.hasAttribute("alt"))
  437. closeBtnEl.setAttribute("alt", this.getString("closeDialogTooltip"));
  438. closeBtnEl.title = closeBtnEl.ariaLabel = this.getString("closeDialogTooltip");
  439. this.onInteraction(closeBtnEl, () => this.close());
  440. headerWrapperEl.appendChild(closeBtnEl);
  441. }
  442. dialogWrapperEl.appendChild(headerWrapperEl);
  443. //#region body
  444. const dialogBodyElem = document.createElement("div");
  445. dialogBodyElem.id = `uu-${this.id}-dialog-body`;
  446. dialogBodyElem.classList.add("uu-dialog-body");
  447. this.options.small && dialogBodyElem.classList.add("small");
  448. const body = this.options.renderBody();
  449. dialogBodyElem.appendChild(body instanceof Promise ? await body : body);
  450. dialogWrapperEl.appendChild(dialogBodyElem);
  451. //#region footer
  452. if(footer) {
  453. const footerWrapper = document.createElement("div");
  454. footerWrapper.classList.add("uu-dialog-footer-cont");
  455. dialogWrapperEl.appendChild(footerWrapper);
  456. footerWrapper.appendChild(footer instanceof Promise ? await footer : footer);
  457. }
  458. return dialogWrapperEl;
  459. }
  460. }