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
refactor(dropdown): drop `isStatic`, `forceShown` and the popups' dead z-index overrides
Elian Doranc86fbe11ff0e
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex a99bc33882..d9c1440d61 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -462,15 +462,8 @@ button kbd {display: contents;}-/* A dropdown's popup, portaled to the body, stacks in the root context, so it has to clear a- modal (1055) and a stacked quick-edit popup (1100), and stay under the prompts (2000) it can- summon. */-body > .tn-dropdown-portal > .dropdown-menu {- --bs-dropdown-zindex: 1200;-}--/* Under the menu above, which is what it is drawn for; stated beside it so the two stay in step.- Dressed as a dialog's backdrop, a menu that dims the page being the same thing to the reader. */+/* Just under the popup it is drawn for, whose z-index (1200) is set in `Popup.css`. Dressed as a+ dialog's backdrop, a menu that dims the page being the same thing to the reader. */body > .tn-dropdown-portal > .tn-dropdown-backdrop {position: fixed;inset: 0;diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.css b/apps/client/src/widgets/attribute_widgets/attribute_detail.cssindex 77fc60a23d..17a3a166a5 100644--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.css+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.css@@ -20,15 +20,6 @@contain: none;}-/* The type menu is portaled to `document.body`, as every dropdown's is: the popup is a- scroll container (`overflow: auto` above), so an inline menu could only open by scrolling the form- under itself. In body it competes with the popup in the root stacking context, where the global- dropdown z-index (999) loses to the popup's 1060 — raise it above the popup, staying below the- confirmations and prompts (2000) the popup summons. */-body > div.attr-input-label-type > .dropdown-menu {- --bs-dropdown-zindex: 1100;-}-/** The same mark the name completion puts against a system name, and so the same colour: quiet, because* it tells the reader whose name this is rather than asking anything of them. Without a `--color` thediff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsxindex 073fb21271..5d3e181fba 100644--- a/apps/client/src/widgets/react/Dropdown.spec.tsx+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx@@ -119,16 +119,6 @@ describe("Dropdown", () => {await vi.waitFor(() => expect(popup()).toBeNull());});- it("shows its popup from the start when forced open, and says so once", async () => {- const onShown = vi.fn();- const { toggle } = renderDropdown({ forceShown: true, onShown });-- await vi.waitFor(() => expect(popup()?.style.visibility).toBe("visible"));- expect(popup()?.textContent).toBe("By title");- expect(toggle.getAttribute("aria-expanded")).toBe("true");- expect(onShown).toHaveBeenCalledTimes(1);- });-it("closes an open popup once it is disabled, and does not open while it is", async () => {const { toggle } = renderDropdown();click(toggle);diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsxindex f28380fa34..ba7147e860 100644--- a/apps/client/src/widgets/react/Dropdown.tsx+++ b/apps/client/src/widgets/react/Dropdown.tsx@@ -19,7 +19,6 @@ type DataAttributes = {export interface DropdownProps extends Pick<HTMLProps<HTMLDivElement>, "id" | "className"> {buttonClassName?: string;buttonProps?: Partial<HTMLAttributes<HTMLButtonElement> & DataAttributes>;- isStatic?: boolean;children: ComponentChildren;title?: string;dropdownContainerStyle?: CSSProperties;@@ -29,13 +28,9 @@ export interface DropdownProps extends Pick<HTMLProps<HTMLDivElement>, "id" | "c/** If set to true, then the dropdown button will be considered an icon action (without normal border and sized for icons only). */iconAction?: boolean;noSelectButtonStyle?: boolean;- /**- * The only supported way to disable the toggle. Bootstrap cannot close a menu whose toggle is- * disabled through `buttonProps` or a `disabled` class.- */+ /** Disables the toggle, and closes the popup if it is open. */disabled?: boolean;text?: ComponentChildren;- forceShown?: boolean;onShown?: () => void;onHidden?: () => void;/** The side of the toggle the popup prefers; it flips to the other near the viewport's edge. */@@ -156,7 +151,7 @@ interface PopupSlot {* The toggle both kinds of dropdown share: the button, its tooltip, the open state and its* callbacks, the handle, and the phone's cover. `popup` draws what opens under it.*/-function DropdownToggle({ id, className, buttonClassName, isStatic, title, text, dropdownContainerStyle, dropdownContainerClassName, dropdownContainerRef: externalContainerRef, hideToggleArrow, iconAction, disabled, noSelectButtonStyle, forceShown, onShown, onHidden, placement, autoClose = true, buttonProps, dropdownRef, titlePosition, titleOptions, mobileBackdrop: mobileBackdropProp, mobileBottomSheet, backdrop, hasPopup, onToggleArrow, popup }: Omit<DropdownProps, "children"> & {+function DropdownToggle({ id, className, buttonClassName, title, text, dropdownContainerStyle, dropdownContainerClassName, dropdownContainerRef: externalContainerRef, hideToggleArrow, iconAction, disabled, noSelectButtonStyle, onShown, onHidden, placement, autoClose = true, buttonProps, dropdownRef, titlePosition, titleOptions, mobileBackdrop: mobileBackdropProp, mobileBottomSheet, backdrop, hasPopup, onToggleArrow, popup }: Omit<DropdownProps, "children"> & {hasPopup: "menu" | "true";/** Called for Up or Down on the toggle while the popup is up. */onToggleArrow?(popup: HTMLElement, edge: "first" | "last"): void;@@ -169,7 +164,7 @@ function DropdownToggle({ id, className, buttonClassName, isStatic, title, text,const containerRef = useRef<HTMLDivElement | null>(null);const triggerRef = useRef<HTMLButtonElement | null>(null);const popupRef = useRef<HTMLDivElement | null>(null);- const [ shown, setShown ] = useState(!!forceShown && !disabled);+ const [ shown, setShown ] = useState(false);/** The item to start at once the popup shows, for a popup a key on the toggle opened. */const [ startAt, setStartAt ] = useState<"first" | "last">();@@ -233,11 +228,6 @@ function DropdownToggle({ id, className, buttonClassName, isStatic, title, text,if (shownRef.current) setTriggerMounted(true);}, []);- // One shown from the start says so once it is mounted. `shown` is the first render's here.- useEffect(() => {- if (shown) callbacks.current.onShown?.();- }, []);-// On a phone the shell's cover dims the page under the popup; a tap on it closes the popup,// being one outside it.useEffect(() => {@@ -321,7 +311,7 @@ function DropdownToggle({ id, className, buttonClassName, isStatic, title, text,"aria-labelledby": toggleId,onClick: closeOnClickInside},- className: clsx("tn-dropdown-menu", isStatic && "static", dropdownContainerClassName),+ className: clsx("tn-dropdown-menu", dropdownContainerClassName),bottomSheet,startAt,isWanted: () => shownRef.current,diff --git a/packages/commons/src/lib/script_api_preact.ts b/packages/commons/src/lib/script_api_preact.tsindex bde4f9c9c0..502ead3963 100644--- a/packages/commons/src/lib/script_api_preact.ts+++ b/packages/commons/src/lib/script_api_preact.ts@@ -138,7 +138,6 @@ interface DropdownProps {className?: string;buttonClassName?: string;buttonProps?: Record<string, unknown>;- isStatic?: boolean;children?: ComponentChildren;title?: string;dropdownContainerStyle?: Css;@@ -149,7 +148,6 @@ interface DropdownProps {noSelectButtonStyle?: boolean;disabled?: boolean;text?: ComponentChildren;- forceShown?: boolean;onShown?: () => void;onHidden?: () => void;/** The side of the toggle the popup prefers, as Floating UI names it: `"top"`, `"bottom-start"`, … */