Skip to content

Commit 316fad1

Browse files
authored
docs: refactor button and modal builder examples (#5)
1 parent 3637fa4 commit 316fad1

File tree

1 file changed

+55
-52
lines changed

1 file changed

+55
-52
lines changed

src/content/docs/api/components.mdx

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,35 @@ Action buttons are buttons that allow you to interact with the bot.
5353
:::code-group labels=[Primary, Secondary, Success, Danger]
5454

5555
```dart
56-
final primary = ButtonBuilder.primary('customId',
56+
final primary = ButtonBuilder.primary(
57+
'customId',
5758
label: 'Click me',
5859
emoji: PartialEmoji.fromUnicode('👍'),
5960
disabled: true,
6061
);
6162
```
6263

6364
```dart
64-
final secondary = ButtonBuilder.secondary('customId',
65+
final secondary = ButtonBuilder.secondary(
66+
'customId',
6567
label: 'Click me',
6668
emoji: PartialEmoji.fromUnicode('🚀'),
6769
disabled: true,
6870
);
6971
```
7072

7173
```dart
72-
final success = ButtonBuilder.success('customId',
74+
final success = ButtonBuilder.success(
75+
'customId',
7376
label: 'Click me',
7477
emoji: PartialEmoji.fromUnicode('✅'),
7578
disabled: true,
7679
);
7780
```
7881

7982
```dart
80-
final danger = ButtonBuilder.danger('customId',
83+
final danger = ButtonBuilder.danger(
84+
'customId',
8185
label: 'Click me',
8286
emoji: PartialEmoji.fromUnicode('❌'),
8387
disabled: true,
@@ -125,7 +129,7 @@ final buttons = [
125129
MessageButton.link('https://google.com', label: 'label'),
126130
];
127131
128-
final builder = MessageComponentBuilder()
132+
final builder = MessageBuilder()
129133
..text('# Hello from World')
130134
..buttons(buttons);
131135
@@ -134,75 +138,74 @@ await channel.send(builder);
134138

135139
---
136140

137-
## Dialogs
141+
## Modals
138142

139143
Modals in Discord are interactive pop-up windows that allow users to input information or make specific choices.
140144

141145
They are used for more complex interactions, such as filling out forms or confirming important actions, enhancing the user experience by providing a richer and more intuitive interface.
142146

143147
> [!note]
144-
> The `customId` property is mandatory for dialogs.
148+
> The `customId` property is mandatory for modals.
145149
146-
:::code-group labels=[Dialog builder]
150+
:::code-group labels=[Modal builder]
147151

148152
```dart
149-
final text = DialogBuilder(customId)
150-
..setTitle('Test Dialog')
151-
..text(
152-
title: 'This is a test dialog',
153-
customId: 'title',
154-
constraint: DialogFieldConstraint(maxLength: 20))
155-
..paragraph(
156-
title: 'This is a test paragraph',
157-
customId: 'paragraph',
158-
constraint: DialogFieldConstraint(maxLength: 20));
153+
final modal = ModalBuilder(customId)
154+
..setTitle("Modal title")
155+
..text("# Some markdown compatible text")
156+
..label(
157+
label: "A label wrapping a component",
158+
description: "Some optional description",
159+
component: ModalTextInput(
160+
"textInputID",
161+
style: ModalTextInputStyle.short,
162+
isRequired: false,
163+
minLength: 10,
164+
maxLength: 100,
165+
placeholder: "Some default value",
166+
),
167+
)
168+
..label(
169+
label: "Another label",
170+
description: "Some optional description",
171+
component: SelectMenu.role("selectMenuId", maxValues: 3),
172+
);
159173
```
160174

161175
:::
162176

163-
### Sending dialogs
177+
### Sending modals
164178

165179
> [!note]
166-
> Dialogs in Discord can only be used in response to a user interaction.
180+
> Modals in Discord can only be used in response to a user interaction.
167181
168182
This means they must be triggered by a specific user action, such as clicking a button or using a command.
169183

170-
This restriction ensures that dialogs are always relevant and contextual, thereby enhancing the user experience by providing information or choices at the appropriate time.
184+
This restriction ensures that modals are always relevant and contextual, thereby enhancing the user experience by providing information or choices at the appropriate time.
171185

172186
```dart
173-
final text = DialogBuilder(customId)
174-
..setTitle('Test Dialog')
175-
..text(
176-
title: 'This is a test dialog',
177-
customId: 'title',
178-
constraint: DialogFieldConstraint(maxLength: 20))
179-
..paragraph(
180-
title: 'This is a test paragraph',
181-
customId: 'paragraph',
182-
constraint: DialogFieldConstraint(maxLength: 20));
183-
184187
// [!code ++]
185-
await ctx.interaction.dialog(dialog);
188+
await ctx.interaction.modal(modal);
186189
```
187190

188-
### Handing dialogs
191+
### Handing modals
189192

190-
Basiquement, vous pouvez écouter l'évènement de deux manières différentes.
191-
Par défaut, l'évènement catch l'ensemble des interactions de type `ServerDialogSubmitEvent`.
193+
Basically, you can listen to the event in two different ways.
194+
By default, the event catches all interactions of type `ServerModalSubmitEvent`.
192195

193196
:::code-group labels=[Class, Function]
194197

195198
```dart
196-
final class MyDialogEvent extends ServerDialogSubmitEvent {
199+
final class MyModalEvent extends ServerModalSubmitEvent {
197200
FutureOr<void> handle(ctx, options) {
198-
// Handle dialog submit
201+
// Handle modal submit
199202
}
200203
}
201204
```
202205

203206
```dart
204-
client.events.server.dialogSubmit((ctx, options) {
205-
// Handle dialog submit
207+
client.events.server.modalSubmit((ctx, options) {
208+
// Handle modal submit
206209
});
207210
```
208211

@@ -213,21 +216,21 @@ However, it is possible to choose just one component to listen to, using its `cu
213216
:::code-group labels=[Class, Function]
214217

215218
```dart
216-
final class MyDialogEvent implements ServerDialogSubmitEvent {
219+
final class MyModalEvent implements ServerModalSubmitEvent {
217220
// [!code ++:2]
218221
@override
219222
String? get customId => 'customId';
220223
221224
@override
222225
FutureOr<void> handle(ctx, options) {
223-
// Handle dialog submit when customId is 'customId'
226+
// Handle modal submit when customId is 'customId'
224227
}
225228
}
226229
```
227230

228231
```dart
229-
client.events.server.dialogSubmit((ctx, options) {
230-
// Handle dialog submit when customId is 'customId'
232+
client.events.server.modalSubmit((ctx, options) {
233+
// Handle modal submit when customId is 'customId'
231234
// [!code --]
232235
});
233236
// [!code ++]
@@ -262,12 +265,12 @@ final helloEmoji = PartialEmoji.fromUnicode('👋');
262265
final worldEmoji = PartialEmoji.fromUnicode('🌍');
263266
264267
// [!code ++:4]
265-
final selectMenu = MessageMenu.text('text', [
268+
final selectMenu = SelectMenu.text('text', [
266269
SelectMenuOption(label: 'label 1', value: 'value 1'),
267270
SelectMenuOption(label: 'label 2', value: 'value 2'),
268271
]);
269272
270-
final builder = MessageComponentBuilder()
273+
final builder = MessageBuilder()
271274
..text('# Hello from World')
272275
// [!code ++:1]
273276
..selectMenu(selectMenu);
@@ -278,7 +281,7 @@ await channel.send(builder);
278281

279282
```dart
280283
client.events.server.selectText((ctx, values) async {
281-
final builder = MessageComponentBuilder()
284+
final builder = MessageBuilder()
282285
..text('Find ${values.length} channels ($channelNames)');
283286
284287
await ctx.interaction.reply(builder: builder, ephemeral: true);
@@ -293,7 +296,7 @@ final class MyTextSelect extends ServerTextSelectEvent {
293296
294297
@override
295298
FutureOr<void> handle(ServerSelectContext ctx, List<String> values) async {
296-
final builder = MessageComponentBuilder()
299+
final builder = MessageBuilder()
297300
..text('Find ${values.length} channels ($channelNames)');
298301
299302
await ctx.interaction.reply(builder: builder, ephemeral: true);
@@ -309,11 +312,11 @@ final class MyTextSelect extends ServerTextSelectEvent {
309312

310313
```dart
311314
// [!code ++:3]
312-
final channelSelectMenu = MessageMenu.channel('channel',
315+
final channelSelectMenu = SelectMenu.channel('channel',
313316
channelTypes: [ChannelType.guildText],
314317
defaultValues: [Snowflake.parse('1322554770057068636')]);
315318
316-
final builder = MessageComponentBuilder()
319+
final builder = MessageBuilder()
317320
..text('# Hello from World')
318321
// [!code ++]
319322
..selectMenu(channelSelectMenu));
@@ -326,7 +329,7 @@ await channel.send(builder);
326329
client.events.server.selectText((ctx, channels) async {
327330
final channelNames = channels.map((channel) => channel.name);
328331
329-
final builder = MessageComponentBuilder()
332+
final builder = MessageBuilder()
330333
..text('${values.length} channels ($channelNames)');
331334
332335
await ctx.interaction.reply(builder: builder, ephemeral: true);
@@ -341,7 +344,7 @@ final class MyChannelSelect extends ServerChannelSelectEvent {
341344
342345
@override
343346
FutureOr<void> handle(ServerSelectContext ctx, List<ServerChannel> channels) async {
344-
final builder = MessageComponentBuilder()
347+
final builder = MessageBuilder()
345348
..text('Find ${values.length} channels ($channelNames)');
346349
347350
await ctx.interaction.reply(builder: builder, ephemeral: true);

0 commit comments

Comments
 (0)