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
feat(menus): reveal menus and dropdown panels as they open
Elian Doran60bf7016515c
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsxindex 5335fe1a74..3c406c7b07 100644--- a/apps/client/src/widgets/react/Dropdown.tsx+++ b/apps/client/src/widgets/react/Dropdown.tsx@@ -140,6 +140,7 @@ export function DropdownPanel({ scrollable, ...props }: DropdownPanelProps) {// Otherwise it grows with its content, as Bootstrap's dropdowns did.capHeight={!!scrollable}placedByStylesheet={popupProps.placedByStylesheet}+ revealclassName={clsx("dropdown-menu show", className, bottomSheet && "mobile-bottom-menu")}startAt={startAt}onDismiss={dismiss}diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsxindex 6afa424fa2..e79e7b9e17 100644--- a/apps/client/src/widgets/react/Menu.tsx+++ b/apps/client/src/widgets/react/Menu.tsx@@ -389,6 +389,7 @@ export default function Menu<T>({ id, className, anchor, placement, bottomSheet,anchor={anchor}placement={placement ?? (anchor instanceof HTMLElement ? "bottom-start" : "right-start")}placedByStylesheet={popupProps.placedByStylesheet || bottomSheet || inline}+ reveal={!inline}onPlaced={() => {const menu = menuRef.current;if (menu) {diff --git a/apps/client/src/widgets/react/Popup.css b/apps/client/src/widgets/react/Popup.cssindex 33f6576eeb..ade8f3745e 100644--- a/apps/client/src/widgets/react/Popup.css+++ b/apps/client/src/widgets/react/Popup.css@@ -13,6 +13,91 @@ body .tn-popup {visibility: hidden;}+/* `translate` and `scale` rather than `transform`, which the mobile sheet rules in style.css reset+ with `!important`. */+:root .tn-popup[data-reveal] {+ --tn-reveal-x: 0px;+ --tn-reveal-y: 0px;+ --tn-reveal-origin-x: center;+ --tn-reveal-origin-y: center;+ transform-origin: var(--tn-reveal-origin-x) var(--tn-reveal-origin-y);+ animation: tn-popup-reveal 120ms cubic-bezier(0.2, 0, 0, 1);++ &[data-reveal^="bottom"] {+ --tn-reveal-y: -4px;+ --tn-reveal-origin-y: top;+ }++ &[data-reveal^="top"] {+ --tn-reveal-y: 4px;+ --tn-reveal-origin-y: bottom;+ }++ &[data-reveal^="right"] {+ --tn-reveal-x: -4px;+ --tn-reveal-origin-x: left;+ }++ &[data-reveal^="left"] {+ --tn-reveal-x: 4px;+ --tn-reveal-origin-x: right;+ }++ &:is([data-reveal="bottom-start"], [data-reveal="top-start"]) {+ --tn-reveal-origin-x: left;+ }++ &:is([data-reveal="bottom-end"], [data-reveal="top-end"]) {+ --tn-reveal-origin-x: right;+ }++ &:is([data-reveal="right-start"], [data-reveal="left-start"]) {+ --tn-reveal-origin-y: top;+ }++ &:is([data-reveal="right-end"], [data-reveal="left-end"]) {+ --tn-reveal-origin-y: bottom;+ }++ &[data-reveal="stylesheet"] {+ animation: tn-popup-rise 160ms cubic-bezier(0.2, 0, 0, 1);+ }++ &.mobile-bottom-menu[data-reveal="stylesheet"] {+ animation: tn-popup-sheet 200ms cubic-bezier(0.2, 0, 0, 1);+ }+}++/* Floating UI lines a start or end alignment up with the other edge in a right-to-left page. */+body[dir=rtl] .tn-popup[data-reveal]:is([data-reveal="bottom-start"], [data-reveal="top-start"]) {+ --tn-reveal-origin-x: right;+}++body[dir=rtl] .tn-popup[data-reveal]:is([data-reveal="bottom-end"], [data-reveal="top-end"]) {+ --tn-reveal-origin-x: left;+}++@keyframes tn-popup-reveal {+ from {+ opacity: 0;+ translate: var(--tn-reveal-x) var(--tn-reveal-y);+ scale: 0.96;+ }+}++@keyframes tn-popup-rise {+ from {+ opacity: 0;+ translate: 0 16px;+ }+}++@keyframes tn-popup-sheet {+ from {+ translate: 0 100%;+ }+}+/** A menu's or a scrollable panel's frame does not scroll, so the blur the theme puts on its* `::before` covers it whatever its content scrolls to. The content scrolls inside, in the heightdiff --git a/apps/client/src/widgets/react/Popup.spec.tsx b/apps/client/src/widgets/react/Popup.spec.tsxindex 6cca797d11..f62e94264b 100644--- a/apps/client/src/widgets/react/Popup.spec.tsx+++ b/apps/client/src/widgets/react/Popup.spec.tsx@@ -158,6 +158,21 @@ describe("Popup", () => {expect(onPlaced).toHaveBeenCalledTimes(1);});+ it("names the side it reveals itself from once placed, when it is asked to reveal itself", async () => {+ anchorAt(100, 50);+ expect((await open({ reveal: true })).dataset.reveal).toBe("bottom-start");++ render(null, host);+ anchorAt(100, 740);+ expect((await open({ reveal: true })).dataset.reveal).toBe("top-start");++ render(null, host);+ expect((await open({ reveal: true, placedByStylesheet: true })).dataset.reveal).toBe("stylesheet");++ render(null, host);+ expect((await open()).dataset.reveal).toBeUndefined();+ });+it("says nothing of a placement that settles once it has closed", async () => {anchorAt(100, 50);const onPlaced = vi.fn();diff --git a/apps/client/src/widgets/react/Popup.tsx b/apps/client/src/widgets/react/Popup.tsxindex a2c1f7927f..bb81002084 100644--- a/apps/client/src/widgets/react/Popup.tsx+++ b/apps/client/src/widgets/react/Popup.tsx@@ -49,6 +49,12 @@ export interface PopupProps extends Pick<HTMLAttributes<HTMLDivElement>, "id" |* the screen fullscreen, or else the page's body.*/container?: HTMLElement;+ /**+ * Animates it in once it is placed: from the side facing its anchor, or out of the bottom of the+ * screen where the stylesheet places it (see Popup.css). For a menu or a panel, not for a list+ * of suggestions that changes as the user types.+ */+ reveal?: boolean;/** Called once it is first placed and shown: a browser focuses nothing inside it before. */onPlaced?(): void;/**@@ -70,7 +76,7 @@ export interface PopupProps extends Pick<HTMLAttributes<HTMLDivElement>, "id" |/** How many pixels it keeps from the edges of the viewport. */const VIEWPORT_PADDING = 5;-export default function Popup({ anchor, placement = "bottom-start", offset: gap = 0, capHeight = true, portalClassName, placedByStylesheet, backdropClassName, elementRef, container, onPlaced, onDismiss, escapeDismisses = true, keepOpenSelector, className, children, ...elementProps }: PopupProps) {+export default function Popup({ anchor, placement = "bottom-start", offset: gap = 0, capHeight = true, portalClassName, placedByStylesheet, reveal, backdropClassName, elementRef, container, onPlaced, onDismiss, escapeDismisses = true, keepOpenSelector, className, children, ...elementProps }: PopupProps) {const popupRef = useRef<HTMLDivElement | null>(null);// Stable, so the element is handed on once rather than taken back and given again each render.const setElement = useCallback((element: HTMLDivElement | null) => {@@ -82,9 +88,13 @@ export default function Popup({ anchor, placement = "bottom-start", offset: gapplaced.current = onPlaced;/** Whether it has said it is placed, which it says once, however often it is placed again. */const hasPlaced = useRef(false);- const reportPlaced = () => {+ const reveals = useRef(reveal);+ reveals.current = reveal;+ /** Says it is placed, and names the side it reveals itself from, which CSS animates once. */+ const reportPlaced = (side: string) => {if (hasPlaced.current) return;hasPlaced.current = true;+ if (reveals.current && popupRef.current) popupRef.current.dataset.reveal = side;placed.current?.();};// A popup opened inside this one stands in the page's body too, so it registers here to@@ -114,7 +124,7 @@ export default function Popup({ anchor, placement = "bottom-start", offset: gapif (placedByStylesheet) {clearPlacement(popup);popup.style.visibility = "visible";- reportPlaced();+ reportPlaced("stylesheet");return;}const reference = "x" in anchor ? pointAt(anchor.x, anchor.y) : anchor;@@ -124,8 +134,8 @@ export default function Popup({ anchor, placement = "bottom-start", offset: gap// Only a menu at a point moves over it. A rect, such as a caret, marks text the popup must not// cover, so the popup keeps to one side and its height is capped to the room there.const options = { placement, offset: gap, capHeight, shiftAcross: "x" in anchor };- const stopUpdating = autoUpdate(reference, popup, () => void placeFloating(popup, reference, options).then(() => {- if (!closed) reportPlaced();+ const stopUpdating = autoUpdate(reference, popup, () => void placeFloating(popup, reference, options).then((side) => {+ if (!closed) reportPlaced(side);// Resolved after the stylesheet took over, so what it wrote goes again.else if (stylesheetPlaces.current) clearPlacement(popup);}));