# Modals

> Open a form from a command or a button, and read what the member submitted.

A modal is a form the client shows over the conversation. Open one in answer to a command or a
component click. A modal has a custom id, a title (≤ 45 characters) and 1–5 top-level items:
**labels**, each wrapping one input (text input, select, radio group, checkbox group, checkbox or
file upload), and text displays.

**TypeScript:**

```ts
import {
  LabelBuilder, ModalBuilder, StringSelectMenuBuilder, TextDisplayBuilder, TextInputBuilder,
  TextInputStyle,
} from '@zaun/bot';

interaction.showModal(new ModalBuilder().setCustomId('report').setTitle('Report a problem')
  .addTextDisplayComponents(new TextDisplayBuilder().setContent('Tell us what happened. Moderators will read it.'))
  .addTextInput('Summary', new TextInputBuilder().setCustomId('summary').setStyle(TextInputStyle.Short).setMinLength(5).setMaxLength(80))
  .addTextInput('Details', new TextInputBuilder().setCustomId('details').setStyle(TextInputStyle.Paragraph).setRequired(false))
  .addLabelComponents(new LabelBuilder().setLabel('Area').setComponent(
    new StringSelectMenuBuilder().setCustomId('area').addOptions({ label: 'Chat', value: 'chat' }, { label: 'Voice', value: 'voice' }))));
```

**Rust:**

```rust
let modal = Modal::new("report", "Report a problem")
    .text("Tell us what happened. Moderators will read it.")
    .input("Summary", TextInput::short("summary").length(5, 80))
    .input("Details", TextInput::paragraph("details").required(false))
    .select("Area", Select::strings("area", &[("Chat", "chat"), ("Voice", "voice")]));
ctx.show_modal(modal)?;
```

## Reading the submission

**TypeScript:**

```ts
bot.modal('report', (interaction) => {
  const summary = interaction.fields.getTextInputValue('summary');
  const details = interaction.fields.getTextInputValue('details') || '(none)';
  const area = interaction.fields.getStringSelectValues('area')[0];
  interaction.reply({ content: `Thanks! We got: ${summary} (${area})`, ephemeral: true });
});
```

Also `getRadioValue`, `getCheckbox`, `getSelectedMembers` and `getUploadedFiles`.

**Rust:**

```rust
fn on_modal(&mut self, ctx: Context, modal: ModalData) {
    let summary = modal.text("summary").unwrap_or_default();
    let details = modal.text("details").unwrap_or("(none)");
    let _ = ctx.reply_ephemeral(format!("Thanks! We got: {summary}"));
}
```

A text input gives its text; a checkbox gives `"true"` or `"false"`; selects, radio and checkbox
groups give their chosen values; a file upload gives attachment ids.

## What the host checks

The host checks every submission against the modal it showed: required inputs present, lengths and
counts in bounds, choices among the options. A modal can be submitted **once**, by the member it was
shown to, within 15 minutes.

## Resulting code

See the [feedback form example](/docs/examples/feedback-modal/).
