Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(dialogs): keep the classes Bootstrap wrote when the host's own change
Elian Doran22f5f1a0d6d0
diff --git a/apps/client/src/widgets/react/Modal.spec.tsx b/apps/client/src/widgets/react/Modal.spec.tsxindex 040046cf00..c0cbebe1f9 100644--- a/apps/client/src/widgets/react/Modal.spec.tsx+++ b/apps/client/src/widgets/react/Modal.spec.tsx@@ -109,6 +109,39 @@ describe("Modal", () => {expect(mocks.hide).toHaveBeenCalled();});+ /**+ * Bootstrap marks a raised dialog with `show`, and Preact writes `className` in full whenever+ * the prop changes — so a host that says something about its note in the class took its own+ * dialog off the screen. Picking a colour in the geo map's marker sheet did exactly that: the+ * note's colour class arrived, `show` left with the old attribute, and what remained was a+ * backdrop over an empty page.+ */+ it("keeps the classes Bootstrap wrote when the host's own change", async () => {+ await act(async () => {+ render(<Modal className="test-dialog" size="md" show onHidden={() => {}}>body</Modal>, container);+ });++ const dialog = container.querySelector<HTMLElement>(".test-dialog");+ expect(dialog).toBeTruthy();+ // What Bootstrap puts there once it has raised the dialog, it being mocked out here.+ dialog?.classList.add("show");++ await act(async () => {+ render(<Modal className="test-dialog with-hue" size="md" show onHidden={() => {}}>body</Modal>, container);+ });+ expect(dialog?.classList.contains("show")).toBe(true);+ expect(dialog?.classList.contains("with-hue")).toBe(true);+ expect(dialog?.classList.contains("modal")).toBe(true);++ // A class the host has stopped asking for goes, so this is not merely never writing again.+ await act(async () => {+ render(<Modal className="test-dialog" size="md" show onHidden={() => {}}>body</Modal>, container);+ });+ expect(dialog?.classList.contains("with-hue")).toBe(false);+ expect(dialog?.classList.contains("test-dialog")).toBe(true);+ expect(dialog?.classList.contains("show")).toBe(true);+ });+it("opens a dialog that starts closed only once it is asked to show", async () => {const onHidden = vi.fn();diff --git a/apps/client/src/widgets/react/Modal.tsx b/apps/client/src/widgets/react/Modal.tsxindex d7b353009a..5d1cc70b4e 100644--- a/apps/client/src/widgets/react/Modal.tsx+++ b/apps/client/src/widgets/react/Modal.tsx@@ -3,7 +3,7 @@ import "./Modal.css";import { Modal as BootstrapModal } from "bootstrap";import clsx from "clsx";import { ComponentChildren, CSSProperties, RefObject } from "preact";-import { useEffect, useMemo, useRef } from "preact/hooks";+import { useEffect, useLayoutEffect, useMemo, useRef } from "preact/hooks";import appContext from "../../components/app_context";import { openDialog } from "../../services/dialog";@@ -112,6 +112,30 @@ export default function Modal({ children, className, size, title, customTitleBarconst modalInstanceRef = useRef<BootstrapModal>();const elementToFocus = useRef<Element | null>();+ /*+ * Bootstrap writes classes of its own onto this element — `show` above all, which is what makes+ * a dialog visible — and Preact sets `className` in full whenever the prop changes, which takes+ * them off again. The rendered attribute is therefore held at what the first render gave it,+ * and the host's own classes are added and removed by hand below: a host that names what it is+ * showing in its class (the note's colour, say) must be able to change it without putting its+ * own dialog out.+ */+ const renderedClassName = useRef(`modal fade mx-auto ${className}`);+ const hostClasses = useRef(splitClasses(className));+ useLayoutEffect(() => {+ const modalElement = modalRef.current;+ if (!modalElement) return;++ const classes = splitClasses(className);+ for (const cls of hostClasses.current) {+ if (!classes.includes(cls)) {+ modalElement.classList.remove(cls);+ }+ }+ modalElement.classList.add(...classes);+ hostClasses.current = classes;+ }, [ className ]);+useEffect(() => {const modalElement = modalRef.current;if (!modalElement) return;@@ -243,7 +267,7 @@ export default function Modal({ children, className, size, title, customTitleBar}, [maxWidth, minWidth]);return (- <div className={`modal fade mx-auto ${className}`} tabIndex={-1} style={dialogStyle} role="dialog" aria-label={ariaLabel} ref={modalRef}>+ <div className={renderedClassName.current} tabIndex={-1} style={dialogStyle} role="dialog" aria-label={ariaLabel} ref={modalRef}>{(show || keepInDom) && <ContainerVisibilityContext.Provider value={show}><div className={clsx("modal-dialog", `modal-${size}`, {"modal-dialog-scrollable": scrollable, "modal-dialog-full-page-on-mobile": isFullPageOnMobile, "modal-content-with-sidebar": sidebar})} style={documentStyle} role="document"><div className={clsx("modal-content", sidebar && "modal-content-with-sidebar")}>{sidebar && <div className="modal-sidebar">@@ -299,6 +323,11 @@ export default function Modal({ children, className, size, title, customTitleBar);}+/** The classes a host asked for, as `classList` takes them. */+function splitClasses(className: string) {+ return className.split(" ").filter((cls) => cls !== "");+}+function ModalMain({ sidebar, children }: { sidebar: boolean; children: ComponentChildren }) {if (sidebar) {return <div className="modal-main">{children}</div>;