Skip to content

AnchoredSuggest<T>

A suggest popover opened at an anchor, for a picker that has no text input: a toolbar button, a context-menu action, a decoration in the editor.

Obsidian’s AbstractInputSuggest opens only from an input and EditorSuggest only from the editor. This one is opened by AnchoredSuggest.openAt and closes on Escape, on a pointer-down outside it, and when its anchor element leaves the DOM. The keyboard works as in Obsidian’s own popovers: arrows move, Enter picks.

class ColorSuggest extends AnchoredSuggest<string> {
protected getSuggestions(): string[] {
return ['red', 'green', 'blue'];
}
public renderSuggestion(color: string, el: HTMLElement): void {
el.setText(color);
}
protected onChooseSuggestion(color: string): void {
console.log(color);
}
}
await new ColorSuggest(this.app).openAt({ anchor: buttonEl });

It relies on PopoverSuggest members that obsidian.d.ts does not declare (suggestions, reposition, setAutoDestroy, suggestEl), read from Obsidian 1.15.0.

Import:

import { AnchoredSuggest } from '@mnaoumov/obsidian-lib/anchored-suggest';

Signature:

export class AnchoredSuggest<T> extends PopoverSuggest<T>

Extends: PopoverSuggest<T>

Constructor

new AnchoredSuggest(app: App, scope?: Scope | undefined)

Creates a new anchored suggest.

Properties

PropertyTypeDescription
limitnumberThe maximum number of suggestions shown. 0 disables the limit. Defaults to 100.

Methods

MethodReturnsDescription
close()voidCloses the popover.
getSuggestions(query)Promise<T[]> | T[]Gets the suggestions for a query.
onChooseSuggestion(value, $event)voidCalled with the suggestion the user picked, after the popover closed.
openAt(params)Promise<boolean>Opens the popover at an anchor, or moves it there when it is already open.
selectSuggestion(value, $event)voidPicks a suggestion: closes the popover, then hands the suggestion to AnchoredSuggest.onChooseSuggestion.
setQuery(query)Promise<boolean>Recomputes the suggestions for a new query at the current anchor.