Skip to content

Build your personal knowledge base with Trilium Notes

fix(calendar): focus the new event's title once the popover shows

Elian Doran5072a239edf6
diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
index 3fd3ad9fa5..d42ecdc8ff 100644
--- a/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
@@ -73,6 +73,24 @@ describe("GhostPopover", () => {
expect(onCommit).toHaveBeenLastCalledWith("Fair");
});
+ it("focuses the title field once the popover is shown", async () => {
+ // `Popover` keeps the card hidden until `placeFloating()` resolves, and a browser ignores
+ // `focus()` on a hidden element, so the field must be focused only once the card shows.
+ const visibilityAtFocus: string[] = [];
+ const focus = vi.spyOn(HTMLElement.prototype, "focus").mockImplementation(function (this: HTMLElement) {
+ visibilityAtFocus.push(this.closest<HTMLElement>(".tn-popover")?.style.visibility ?? "");
+ });
+ try {
+ mount(vi.fn());
+ await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
+ } finally {
+ focus.mockRestore();
+ }
+
+ expect(visibilityAtFocus).not.toHaveLength(0);
+ expect(visibilityAtFocus.every((visibility) => visibility === "visible")).toBe(true);
+ });
+
it("hands over the title as typed, an empty one standing for the calendar's own naming", async () => {
const onCommit = vi.fn().mockResolvedValue(undefined);
mount(onCommit);
diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
index c86dda7d18..cc1e0b63a5 100644
--- a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
@@ -53,12 +53,10 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCan
const titleRef = useRef<HTMLInputElement>(null);
/**
- * The field asks for the caret on mount, which a dialog is in no position to grant: it is still
- * rising, and Bootstrap lays its own focus trap on the dialog as it lands — taking back
- * whatever the field had claimed. Asked for again once the dialog is up, which is after that.
+ * Focuses the field once its container shows: `Popover` keeps the card hidden until it is
+ * placed, and Bootstrap's focus trap takes the focus back while the `Modal` opens.
*
- * Held still between renders, or the dialog would unbind and rebind what it listens for on
- * every letter typed into the very field this focuses.
+ * Stable across renders, so `Modal` does not rebind its listeners on every keystroke.
*/
const focusTitle = useCallback(() => titleRef.current?.focus(), []);
@@ -98,7 +96,6 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCan
<div className="calendar-ghost-body">
<FormTextBox
inputRef={titleRef}
- autoFocus
currentValue={title}
placeholder={t("calendar_view.draft_title_placeholder")}
onChange={setTitle}
@@ -148,6 +145,7 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCan
placement={glob.isRtl ? "left-start" : "right-start"}
getAnchorRect={() => ghostAnchorRect(container, anchor)}
onDismiss={onDismiss}
+ onPlaced={focusTitle}
>
<div className="calendar-ghost-header">
<span className="calendar-ghost-heading">{t("calendar_view.new_event")}</span>