Skip to content

Build your personal knowledge base with Trilium Notes

fix(attributes): smooth out the post-save blink

Elian Doran8d07c6166588
diff --git a/apps/client/src/widgets/ribbon/components/AttributeEditor.css b/apps/client/src/widgets/ribbon/components/AttributeEditor.css
index 30445552b4..08afda8afd 100644
--- a/apps/client/src/widgets/ribbon/components/AttributeEditor.css
+++ b/apps/client/src/widgets/ribbon/components/AttributeEditor.css
@@ -19,7 +19,19 @@
margin: 0 !important;
max-height: 100px;
overflow: auto;
- transition: opacity .1s linear;
+}
+
+/*
+ * A save dips the attribute text and brings it back, as a hint that it went through. It is one eased
+ * animation rather than an opacity toggle so that the two halves cannot land abruptly, and only the
+ * text takes part: dipping the whole wrapper would carry the buttons and the error message with it.
+ */
+@keyframes attribute-editor-save-blink {
+ 50% { opacity: .25; }
+}
+
+.attribute-list-editor-wrapper.blink .attribute-list-editor {
+ animation: attribute-editor-save-blink 300ms ease-in-out;
}
body[dir=rtl] .attribute-list-editor {
diff --git a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx
index f1030e7ae0..8a117fcd36 100644
--- a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx
+++ b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx
@@ -2,6 +2,7 @@ import "./AttributeEditor.css";
import type { AttributeEditor as CKEditorAttributeEditor, ModelElement, ModelNode, ModelPosition, TriliumMentionFeed } from "@triliumnext/ckeditor5";
import { AttributeType } from "@triliumnext/commons";
+import clsx from "clsx";
import { createPortal } from "preact/compat";
import { MutableRef, useEffect, useImperativeHandle, useMemo, useRef, useState } from "preact/hooks";
@@ -29,6 +30,12 @@ import AttributeHelp, { ATTRIBUTE_HELP_PAGE } from "./AttributeHelp";
type AttributeCommandNames = FilteredCommandNames<CommandData>;
+/**
+ * How long the post-save blink runs for. Matches the animation in the CSS, and only serves to take the
+ * class back off again — shortening it would cut the animation short.
+ */
+const BLINK_DURATION = 300;
+
// `preselectFirstItem: false` throughout: in this editor Enter means "save the attributes", so an
// open panel must not silently swallow it into committing whichever suggestion happens to be first.
const mentionSetup: TriliumMentionFeed[] = [
@@ -106,9 +113,11 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
const [ currentValue, setCurrentValue ] = useState("");
const [ error, setError ] = useState<unknown>();
const [ needsSaving, setNeedsSaving ] = useState(false);
+ const [ isBlinking, setIsBlinking ] = useState(false);
const [isMenuOpen, setIsMenuOpen] = useState(false);
const suppressNextOnHide = useRef(false);
+ const blinkTimeout = useRef<ReturnType<typeof setTimeout>>();
const lastSavedContent = useRef<string>();
const currentValueRef = useRef(currentValue);
const wrapperRef = useRef<HTMLDivElement>(null);
@@ -133,6 +142,8 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
const [ attributeDetailWidgetEl, attributeDetailWidget ] = useLegacyWidget(() => new AttributeDetailWidget());
+ useEffect(() => () => clearTimeout(blinkTimeout.current), []);
+
async function renderOwnedAttributes(ownedAttributes: FAttribute[], saved: boolean) {
// attrs are not resorted if position changes after the initial load
ownedAttributes.sort((a, b) => a.position - b.position);
@@ -171,14 +182,9 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
setNeedsSaving(false);
// blink the attribute text to give a visual hint that save has been executed
- if (wrapperRef.current) {
- wrapperRef.current.style.opacity = "0";
- setTimeout(() => {
- if (wrapperRef.current) {
- wrapperRef.current.style.opacity = "1";
- }
- }, 100);
- }
+ setIsBlinking(true);
+ clearTimeout(blinkTimeout.current);
+ blinkTimeout.current = setTimeout(() => setIsBlinking(false), BLINK_DURATION);
}
async function handleAddNewAttributeCommand(command: AttributeCommandNames | undefined) {
@@ -292,7 +298,7 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
return (
<>
{!hidden && <div
- className="attribute-list-editor-wrapper"
+ className={clsx("attribute-list-editor-wrapper", isBlinking && "blink")}
ref={wrapperRef}
style="position: relative; padding-top: 10px; padding-bottom: 10px"
onKeyDown={(e) => {