Skip to content

Prompt modals

Falcion edited this page Apr 22, 2025 · 3 revisions

Prompt modal is a generalized modal which only returns embedded value of user's input. It's functionality is primitive: read and return input, where his contrustor accepts from unique only an locale item and module to correctly name and translate it's elements.

Showcase:
showcase

Codebase of given prompt:

import { App, Modal, Setting } from 'obsidian';
import LocalesModule from './../../locales/core';

export class PromptUserInput extends Modal {
    constructor(app: App, title: string, locale: LocalesModule, onSubmit: (result: string) => void) {
        super(app);
        this.setTitle(title);

        let name = '';
        new Setting(this.contentEl)
            .setName(locale.getLocaleItem('PROMPT_LOCALES')[0]!)
            .addText((text) =>
                text.onChange((value) => {
                    name = value;
                }));

        new Setting(this.contentEl)
            .addButton((btn) =>
                btn
                    .setButtonText(locale.getLocaleItem('PROMPT_LOCALES')[1]!)
                    .setCta()
                    .onClick(() => {
                        this.close();
                        onSubmit(name);
                    }));
    }
}

Clone this wiki locally