diff --git a/apps/guide/content/docs/legacy/interactions/images/modal-example.png b/apps/guide/content/docs/legacy/interactions/images/modal-example.png index 9cee3d67d630..e63c9b3074be 100644 Binary files a/apps/guide/content/docs/legacy/interactions/images/modal-example.png and b/apps/guide/content/docs/legacy/interactions/images/modal-example.png differ diff --git a/apps/guide/content/docs/legacy/interactions/images/modal-text-display-example.png b/apps/guide/content/docs/legacy/interactions/images/modal-text-display-example.png new file mode 100644 index 000000000000..db81e63372ed Binary files /dev/null and b/apps/guide/content/docs/legacy/interactions/images/modal-text-display-example.png differ diff --git a/apps/guide/content/docs/legacy/interactions/modals.mdx b/apps/guide/content/docs/legacy/interactions/modals.mdx index 4bdc32bc4a6f..5c09b7f37512 100644 --- a/apps/guide/content/docs/legacy/interactions/modals.mdx +++ b/apps/guide/content/docs/legacy/interactions/modals.mdx @@ -2,7 +2,7 @@ title: Modals --- -With modals you can create pop-up forms that allow users to provide you with formatted inputs through submissions. We'll cover how to create, show, and receive modal forms using discord.js! +With modals you can create pop-up forms that allow users to provide you with formatted inputs through submissions. We'll cover how to create, show, and receive modals This page is a follow-up to the [interactions (slash commands) page](../slash-commands/advanced-creation). Please @@ -14,8 +14,8 @@ With modals you can create pop-up forms that allow users to provide you with for Unlike message components, modals aren't strictly components themselves. They're a callback structure used to respond to interactions. - You can have a maximum of five `ActionRowBuilder`s per modal builder, and one `TextInputBuilder` within an - `ActionRowBuilder`. Currently, you can only use `TextInputBuilder`s in modal action rows builders. + You can have a maximum of five `Label` or `Text Display` components per modal. Similarly a `Label` must only contain + one component. To create a modal you construct a new `ModalBuilder`. You can then use the setters to add the custom id and title. @@ -36,25 +36,15 @@ client.on(Events.InteractionCreate, async (interaction) => { The custom id is a developer-defined string of up to 100 characters. Use this field to ensure you can uniquely define - all incoming interactions from your modals! + all incoming interactions from your modals. -The next step is to add the input fields in which users responding can enter free-text. Adding inputs is similar to adding components to messages. +The next step is to add a Modal components to the `modalBuilder`. Which users responding can enter free-text. Adding inputs is similar to adding components to messages. At the end, we then call `ChatInputCommandInteraction#showModal` to display the modal to the user. - -If you're using typescript you'll need to specify the type of components your action row holds. This can be done by specifying the generic parameter in `ActionRowBuilder`: - -```diff -- new ActionRowBuilder() -+ new ActionRowBuilder() -``` - - - ```js -const { ActionRowBuilder, Events, ModalBuilder, TextInputBuilder, TextInputStyle } = require('discord.js'); +const { Events, LabelBuilder, ModalBuilder, TextInputBuilder, TextInputStyle } = require('discord.js'); client.on(Events.InteractionCreate, async (interaction) => { if (!interaction.isChatInputCommand()) return; @@ -63,29 +53,35 @@ client.on(Events.InteractionCreate, async (interaction) => { // Create the modal const modal = new ModalBuilder().setCustomId('myModal').setTitle('My Modal'); - // Add components to modal - + // [!code focus:31] // Create the text input components const favoriteColorInput = new TextInputBuilder() .setCustomId('favoriteColorInput') - // The label is the prompt the user sees for this input - .setLabel("What's your favorite color?") // Short means only a single line of text .setStyle(TextInputStyle.Short); const hobbiesInput = new TextInputBuilder() .setCustomId('hobbiesInput') - .setLabel("What's some of your favorite hobbies?") // Paragraph means multiple lines of text. - .setStyle(TextInputStyle.Paragraph); + .setStyle(TextInputStyle.Paragraph) + // Uninteractable text inside of the text input + .setPlaceholder('card games, films, books, etc.'); - // An action row only holds one text input, - // so you need one action row per text input. - const firstActionRow = new ActionRowBuilder().addComponents(favoriteColorInput); - const secondActionRow = new ActionRowBuilder().addComponents(hobbiesInput); + // Creating labels for the text input components + const favoriteColorLabel = new LabelBuilder() + // The label is the prompt the user sees for this component + .setLabel("What's your favorite color?") + // Add the text input to the label + .setTextInputComponent(favoriteColorInput); + + const hobbiesLabel = new LabelBuilder() + .setLabel("What's some of your favorite hobbies?") + // The description is a small text under the label and above the interactive component + .setDescription('Activities you like to participate in') + .setTextInputComponent(hobbiesInput); - // Add inputs to the modal - modal.addComponents(firstActionRow, secondActionRow); + // Add labels to the modal + modal.addLabelComponents(favoriteColorLabel, hobbiesLabel); // Show the modal to the user await interaction.showModal(modal); // [!code word:showModal] @@ -93,40 +89,15 @@ client.on(Events.InteractionCreate, async (interaction) => { }); ``` -Restart your bot and invoke the `/ping` command again. You should see a popup form resembling the image below: +Restart your bot and invoke the `/ping` command again. You should see the modal as imaged below: ![Modal Example](./images/modal-example.png) - Showing a modal must be the first response to an interaction. You cannot `defer()` or `deferUpdate()` then show a + Showing a modal must be the first response to an interaction. You cannot `deferReply()` or `deferUpdate()` then show a modal later. -### Input styles - -Currently there are two different input styles available: - -- `Short`, a single-line text entry; -- `Paragraph`, a multi-line text entry similar to the HTML `