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
fix(relation_map): focus the relation name once the popover shows
Elian Dorand3609deb7b96
diff --git a/apps/client/src/widgets/react/Popover.tsx b/apps/client/src/widgets/react/Popover.tsxindex d1624a9b3c..9c6db392bb 100644--- a/apps/client/src/widgets/react/Popover.tsx+++ b/apps/client/src/widgets/react/Popover.tsx@@ -49,6 +49,11 @@ export interface PopoverProps {* and `data-placement` (see the effect below), so only the stylesheet positions it.*/maximized?: boolean;+ /**+ * Called once the card is first placed and shown. A field inside the card can only take the focus+ * from then on, since the card stays `visibility: hidden` until `placeFloating()` resolves.+ */+ onPlaced?(): void;children: ComponentChildren;}@@ -59,7 +64,7 @@ export interface PopoverProps {* CLAUDE.md). `placeFloating()` positions it, and `autoUpdate()` places it again as ancestors* scroll.*/-export default function Popover({ getAnchorRect, placement, updateKey, className, keepOpenSelector, onDismiss, maximized, children }: PopoverProps) {+export default function Popover({ getAnchorRect, placement, updateKey, className, keepOpenSelector, onDismiss, maximized, onPlaced, children }: PopoverProps) {const elRef = useRef<HTMLDivElement>(null);const arrowRef = useRef<HTMLDivElement>(null);const updateRef = useRef<() => void>();@@ -68,6 +73,9 @@ export default function Popover({ getAnchorRect, placement, updateKey, className// on every render would reset the position mid-interaction.const getRectRef = useRef(getAnchorRect);getRectRef.current = getAnchorRect;+ const onPlacedRef = useRef(onPlaced);+ onPlacedRef.current = onPlaced;+ const hasBeenPlaced = useRef(false);useEffect(() => {const el = elRef.current;@@ -97,8 +105,16 @@ export default function Popover({ getAnchorRect, placement, updateKey, className};// `placeFloating()` resolves later, by which time the card can have been maximized.const update = () => void placeFloating(el, anchor, options).then((placed) => {- if (released) release();- else el.dataset.placement = placed;+ if (released) {+ release();+ return;+ }++ el.dataset.placement = placed;+ if (!hasBeenPlaced.current) {+ hasBeenPlaced.current = true;+ onPlacedRef.current?.();+ }});updateRef.current = update;// Places the card again when an ancestor scrolls, the viewport resizes, or the card'sdiff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationNamePopover.spec.tsx b/apps/client/src/widgets/type_widgets/relation_map/RelationNamePopover.spec.tsxindex 45d3d7dc55..4c54e869cc 100644--- a/apps/client/src/widgets/type_widgets/relation_map/RelationNamePopover.spec.tsx+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationNamePopover.spec.tsx@@ -67,10 +67,11 @@ describe("RelationNamePopover", () => {expect(outside.onAnswer).toHaveBeenCalledWith(null);});- it("opens on the current name when renaming", async () => {+ it("opens on the current name when renaming, with the field focused", async () => {const { input } = await mount("author");expect(input.value).toBe("author");+ expect(document.activeElement).toBe(input);expect(document.querySelector(".relation-name-heading")?.textContent).toBe("relation_map.rename_relation");});});@@ -103,6 +104,8 @@ async function mount(defaultValue = "") {renderInto(<RelationNamePopover connection={fakeConnection()} defaultValue={defaultValue} onAnswer={onAnswer} />);});+ // Lets `placeFloating()` resolve, which is when the popover focuses its field.+ await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });const input = document.querySelector<HTMLInputElement>(".relation-name-popover input");if (!input) throw new Error("the popover rendered no field");diff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationNamePopover.tsx b/apps/client/src/widgets/type_widgets/relation_map/RelationNamePopover.tsxindex f7c4f5017c..17c341fe83 100644--- a/apps/client/src/widgets/type_widgets/relation_map/RelationNamePopover.tsx+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationNamePopover.tsx@@ -32,6 +32,7 @@ export default function RelationNamePopover({ connection, defaultValue, onAnswer}) {const [ name, setName ] = useState(defaultValue);const isComposing = useRef(false);+ const inputRef = useRef<HTMLInputElement>(null);const isRename = !!defaultValue;const suggestRelationNames = useCallback((query: string) => fetchAttributeNames("relation", query), []);@@ -55,6 +56,7 @@ export default function RelationNamePopover({ connection, defaultValue, onAnswerplacement="bottom"getAnchorRect={() => connectionAnchorRect(connection)}onDismiss={cancel}+ onPlaced={() => inputRef.current?.focus()}><div className="relation-name-header"><span className="relation-name-heading">@@ -65,7 +67,7 @@ export default function RelationNamePopover({ connection, defaultValue, onAnswer<div className="relation-name-body"><FormAutocomplete- autoFocus+ inputRef={inputRef}currentValue={name}placeholder={t("relation_map.relation_name_placeholder")}source={suggestRelationNames}