Skip to content

Build your personal knowledge base with Trilium Notes

fix(attributes): stop the detail popup flickering on click in the editor

Elian Doran7d3b4588ff1d
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
index 8758ec000b..ba6fdc2108 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
@@ -16,6 +16,10 @@ export interface AttributeDetailOpts {
x: number;
y: number;
focus?: "name";
+ /**
+ * The element the popup was spawned from. Mouse presses inside it do not dismiss the
+ * popup, leaving the spawning widget free to swap the shown attribute on click.
+ */
parent?: HTMLElement;
hideMultiplicity?: boolean;
}
@@ -119,6 +123,7 @@ function AttributeDetail({ opts, parentOffset, onDismiss, onCancel }: AttributeD
// Dismiss on click outside the popup, except in floating UI logically belonging
// to it (autocomplete dropdowns and context menus are appended to the body).
// Unlike the legacy widget, the listener only exists while the popup is shown.
+ const spawner = opts?.parent;
useEffect(() => {
if (!shown) {
return;
@@ -127,6 +132,10 @@ function AttributeDetail({ opts, parentOffset, onDismiss, onCancel }: AttributeD
const onMouseDown = (e: MouseEvent) => {
if (!(e.target instanceof Element)
|| popupRef.current?.contains(e.target)
+ // The spawning widget decides for itself on click whether to show another
+ // attribute or close; dismissing here first would hide and immediately
+ // re-show the popup, which reads as a flicker.
+ || spawner?.contains(e.target)
|| e.target.closest(".algolia-autocomplete, #context-menu-container")) {
return;
}
@@ -135,7 +144,7 @@ function AttributeDetail({ opts, parentOffset, onDismiss, onCancel }: AttributeD
window.addEventListener("mousedown", onMouseDown);
return () => window.removeEventListener("mousedown", onMouseDown);
- }, [ shown, onDismiss ]);
+ }, [ shown, spawner, onDismiss ]);
if (!opts) {
return null;
diff --git a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx
index 263f3daeaa..355ec9ec2b 100644
--- a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx
+++ b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx
@@ -242,17 +242,15 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
// this.$editor.scrollTop(this.$editor[0].scrollHeight);
const rect = wrapperRef.current?.getBoundingClientRect();
- setTimeout(() => {
- // showing a little bit later because there's a conflict with outside click closing the attr detail
- attributeDetailWidget.showAttributeDetail({
- allAttributes: attrs,
- attribute: attrs[attrs.length - 1],
- isOwned: true,
- x: rect ? (rect.left + rect.right) / 2 : 0,
- y: rect?.bottom ?? 0,
- focus: "name"
- });
- }, 100);
+ attributeDetailWidget.showAttributeDetail({
+ allAttributes: attrs,
+ attribute: attrs[attrs.length - 1],
+ isOwned: true,
+ x: rect ? (rect.left + rect.right) / 2 : 0,
+ y: rect?.bottom ?? 0,
+ focus: "name",
+ parent: wrapperRef.current ?? undefined
+ });
}
// Refresh with note
@@ -370,21 +368,24 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
}
}
- setTimeout(() => {
- if (matchedAttr) {
- attributeDetailWidget.showAttributeDetail({
- allAttributes: parsedAttrs,
- attribute: matchedAttr,
- isOwned: true,
- x: e.pageX,
- y: e.pageY
- });
- setState("showAttributeDetail");
- } else {
- setState("showHelpTooltip");
- }
- }, 100);
+ if (matchedAttr) {
+ attributeDetailWidget.showAttributeDetail({
+ allAttributes: parsedAttrs,
+ attribute: matchedAttr,
+ isOwned: true,
+ x: e.pageX,
+ y: e.pageY,
+ parent: wrapperRef.current ?? undefined
+ });
+ setState("showAttributeDetail");
+ } else {
+ // Presses inside the editor no longer dismiss the popup, so
+ // clicking next to an attribute has to close it explicitly.
+ attributeDetailWidget.hide();
+ setState("showHelpTooltip");
+ }
} else {
+ attributeDetailWidget.hide();
setState("showHelpTooltip");
}
}}