Modals
@mnaoumov/obsidian-lib provides some modals that you can use in your plugin.
In order for modals to look properly, their styles have to be initialized. See Styling for more details.
To answer alert, confirm, prompt, selectItem and selectOption from a script in tests, show them through a DialogContext. See Scripted dialogs.
import { alert } from '@mnaoumov/obsidian-lib/modals/alert';
await alert({ app, message: 'Sample alert message', title: 'Sample alert title'});
Confirm
Section titled “Confirm”import { confirm } from '@mnaoumov/obsidian-lib/modals/confirm';
await confirm({ app, message: 'Sample confirm message', title: 'Sample confirm title'});
Prompt
Section titled “Prompt”import { prompt } from '@mnaoumov/obsidian-lib/modals/prompt';import type { MaybeReturn } from '@mnaoumov/typescript-lib/type';
await prompt({ app, defaultValue: 'Sample prompt default value', title: 'Sample prompt title', valueValidator(value: string): MaybeReturn<string> { if (value.length < 30) { return 'Value must be at least 30 characters long'; } }});
Asking for a password or an API key
Section titled “Asking for a password or an API key”Pass isMasked: true to ask for a secret. The input becomes a password input with autocomplete and spell-checking turned off, so the value never reaches a spell-checking service, and a toggle button beside it shows and hides the value so the user can check what they typed. spellcheckMode is ignored while the input is masked.
import { prompt } from '@mnaoumov/obsidian-lib/modals/prompt';
const apiKey = await prompt({ app, isMasked: true, placeholder: 'sk-...', title: 'Enter your API key'});Select Item
Section titled “Select Item”import { selectItem } from '@mnaoumov/obsidian-lib/modals/select-item';
await selectItem({ app, items: ['Item 1', 'Item 2', 'Item 3'], itemTextFunction(item: string): string { return item; }, placeholder: 'Sample select item placeholder'});
Select Option
Section titled “Select Option”Unlike Select Item (a fuzzy-search picker over an arbitrary list), selectOption renders one button per option — useful for a small, fixed set of mutually-exclusive choices where each choice has its own label and resolved value. One option can be marked as the call-to-action (primary) button. It resolves with the chosen value, or null if the modal is dismissed without a choice.
import { selectOption } from '@mnaoumov/obsidian-lib/modals/select-option';
const chosen = await selectOption({ app, message: 'Sample select option message', options: [ { isCta: true, text: 'Yes', value: 'yes' }, { text: 'No', value: 'no' }, { text: 'Cancel', value: null } ], title: 'Sample select option title'});Code Block Builder
Section titled “Code Block Builder”showCodeBlockBuilder builds a plugin’s code block from a declarative list of fields: toggles, color pickers, text with an optional validation pattern, sliders, single and multiple-choice dropdowns, cascading dropdowns, vault folders, and collapsible groups of fields. A field whose value differs from its default writes one key: value line into the block, and a required field always does. The modal shows the block live, lets your plugin draw a preview of it, and offers Copy and Insert into the note. It resolves with the inserted block, or null if the user closed it without inserting.
import { showCodeBlockBuilder } from '@mnaoumov/obsidian-lib/modals/code-block-builder';
const codeBlock = await showCodeBlockBuilder({ app, fields: [ { defaultValue: 'chart', isRequired: true, key: 'type', name: 'Type', type: 'text' }, { defaultValue: false, key: 'grid', name: 'Show grid', type: 'boolean' }, { fields: [ { defaultValue: 400, key: 'width', max: 1000, min: 100, name: 'Width', step: 50, type: 'slider' }, { defaultValue: 'blue', key: 'palette', name: 'Palette', options: ['blue', 'green'], type: 'dropdown' } ], name: 'Layout', type: 'group' } ], language: 'my-chart', onUpdatePreview({ codeBlock, previewEl }): void { previewEl.setText(codeBlock); }});The fence is one backtick longer than the longest backtick run in the values, so a value holding a fence never ends the block early. The modal was adapted, with credit, from the code block creator in altarok’s obsidian-dev-utils.