ModalFormData Class

Used to create a fully customizable pop-up form for a player.

Examples

modalFormSimple.ts
import { Player } from '@minecraft/server';
import { ModalFormData } from '@minecraft/server-ui';

function showExampleModal(player: Player) {
    const modalForm = new ModalFormData().title('Example Modal Controls for §o§7ModalFormData§r');

    modalForm.toggle('Toggle w/o default');
    modalForm.toggle('Toggle w/ default', true);

    modalForm.slider('Slider w/o default', 0, 50, 5);
    modalForm.slider('Slider w/ default', 0, 50, 5, 30);

    modalForm.dropdown('Dropdown w/o default', ['option 1', 'option 2', 'option 3']);
    modalForm.dropdown('Dropdown w/ default', ['option 1', 'option 2', 'option 3'], 2);

    modalForm.textField('Input w/o default', 'type text here');
    modalForm.textField('Input w/ default', 'type text here', 'this is default');

    modalForm
        .show(player)
        .then(formData => {
            player.sendMessage(`Modal form results: ${JSON.stringify(formData.formValues, undefined, 2)}`);
        })
        .catch((error: Error) => {
            player.sendMessage('Failed to show form: ' + error);
            return -1;
        });
}

Methods

constructor

new ModalFormData()

Creates a new modal form builder.

Returns ModalFormData

dropdown(label: minecraftserver.RawMessage | string, options: (minecraftserver.RawMessage | string)[], defaultValueIndex?: number): ModalFormData

Adds a dropdown with choices to the form.

Parameters

Returns ModalFormData

show

show(player: minecraftserver.Player): Promise<ModalFormResponse>

Creates and shows this modal popup form. Returns asynchronously when the player confirms or cancels the dialog.

Parameters

Returns Promise<ModalFormResponse>

Important

This function can't be called in read-only mode.

Warning

This function can throw errors.

slider

slider(label: minecraftserver.RawMessage | string, minimumValue: number, maximumValue: number, valueStep: number, defaultValue?: number): ModalFormData

Adds a numeric slider to the form.

Parameters

Returns ModalFormData

submitButton

submitButton(submitButtonText: minecraftserver.RawMessage | string): ModalFormData

Parameters

Returns ModalFormData

Caution

This function is still in pre-release. Its signature may change or it may be removed in future releases.

textField

textField(label: minecraftserver.RawMessage | string, placeholderText: minecraftserver.RawMessage | string, defaultValue?: minecraftserver.RawMessage | string): ModalFormData

Adds a textbox to the form.

Parameters

Returns ModalFormData

title

title(titleText: minecraftserver.RawMessage | string): ModalFormData

This builder method sets the title for the modal dialog.

Parameters

Returns ModalFormData

toggle

toggle(label: minecraftserver.RawMessage | string, defaultValue?: boolean): ModalFormData

Adds a toggle checkbox button to the form.

Parameters

Returns ModalFormData

Examples

modalFormSimple.ts
import { Player } from '@minecraft/server';
import { ModalFormData } from '@minecraft/server-ui';

function showExampleModal(player: Player) {
    const modalForm = new ModalFormData().title('Example Modal Controls for §o§7ModalFormData§r');

    modalForm.toggle('Toggle w/o default');
    modalForm.toggle('Toggle w/ default', true);

    modalForm.slider('Slider w/o default', 0, 50, 5);
    modalForm.slider('Slider w/ default', 0, 50, 5, 30);

    modalForm.dropdown('Dropdown w/o default', ['option 1', 'option 2', 'option 3']);
    modalForm.dropdown('Dropdown w/ default', ['option 1', 'option 2', 'option 3'], 2);

    modalForm.textField('Input w/o default', 'type text here');
    modalForm.textField('Input w/ default', 'type text here', 'this is default');

    modalForm
        .show(player)
        .then(formData => {
            player.sendMessage(`Modal form results: ${JSON.stringify(formData.formValues, undefined, 2)}`);
        })
        .catch((error: Error) => {
            player.sendMessage('Failed to show form: ' + error);
            return -1;
        });
}