You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if (ctx case ServerButtonContext(:final interaction)) {
76
+
final builder = MessageComponentBuilder()
77
+
..text('# Thanks for clicking on the button');
78
+
79
+
await interaction.reply(builder);
80
+
}
81
+
});
82
+
```
83
+
84
+
As we have seen, the use of simple components quickly becomes "heavy" to manage.
85
+
86
+
That's why we're proposing a new approach: `interactive components`.
87
+
88
+
---
89
+
90
+
## Create your own interactive components
91
+
92
+
Interactive components are a turnkey solution designed to considerably simplify all these processes through a more declarative approach.
93
+
94
+
A component is nothing more or less than a class comprising 3 main elements:
95
+
96
+
1.`customId`: the unique identifier of the component
97
+
2.`build`: the method used to build the component
98
+
3.`handler`: the method used to react to the use of the component
99
+
100
+
---
101
+
102
+
### Choosing the customId
103
+
104
+
First we will declare the `customId` property of our component.
105
+
106
+
> [!important]
107
+
> This property must be unique for each component within the same application
108
+
109
+
```dart
110
+
final class MyButton implements InteractiveButton {
111
+
// [!code ++:2]
112
+
@override
113
+
String get customId => 'button::test';
114
+
}
115
+
```
116
+
117
+
---
118
+
119
+
### Component definition
120
+
121
+
We design the component to be sent to the platform.
122
+
123
+
```dart
124
+
final class MyButton implements InteractiveButton {
125
+
@override
126
+
String get customId => 'button::test';
127
+
128
+
// [!code ++:7]
129
+
@override
130
+
ButtonBuilderContract build() {
131
+
return ButtonBuilder.primary(customId,
132
+
label: 'Click me',
133
+
emoji: PartialEmoji.fromUnicode('👍'),
134
+
disabled: true,
135
+
);
136
+
}
137
+
}
138
+
```
139
+
140
+
---
141
+
142
+
### Handling the component
143
+
144
+
We apply a behaviour to the action performed by a user
145
+
146
+
> [!note]
147
+
> Within the handler, it's important to note that we don't know the context in which the button was clicked, so you'll have to check this manually
148
+
149
+
```dart
150
+
final class MyButton implements InteractiveButton {
151
+
@override
152
+
String get customId => 'button::test';
153
+
154
+
// [!code ++:9]
155
+
@override
156
+
Future<void> handle(ButtonContext ctx) async {
157
+
if (ctx case ServerButtonContext(:final interaction)) {
158
+
final builder = MessageComponentBuilder()
159
+
..text('# Hello from World');
160
+
161
+
await interaction.reply(builder);
162
+
}
163
+
}
164
+
165
+
@override
166
+
ButtonBuilderContract build() {
167
+
return ButtonBuilder.primary(customId,
168
+
label: 'Click me',
169
+
emoji: PartialEmoji.fromUnicode('👍'),
170
+
disabled: true,
171
+
);
172
+
}
173
+
}
174
+
```
175
+
176
+
---
177
+
178
+
### Registering the component
179
+
180
+
Finally, we register our component in the client.
181
+
182
+
```dart
183
+
void main() async {
184
+
final client = ClientBuilder()
185
+
.setHmrDevPort(port)
186
+
.build();
187
+
188
+
// [!code ++]
189
+
client.register(MyButton.new);
190
+
191
+
await client.init();
192
+
}
193
+
```
194
+
195
+
## Use your component
196
+
197
+
Using the example above, it would be usual to instantiate our interactive component and then call the `build()` method to retrieve the component builder.
198
+
199
+
This approach is indeed the right one, with one small exception: your component will have to be instantiated N times to be used N times.
200
+
201
+
To avoid this unnecessary re-instantiation, we provide a service that allows you to retrieve a single instance of your interactive component.
202
+
203
+
> [!note]
204
+
> We assume that no two components can have the same `customId`.
205
+
206
+
In a relatively functional approach, we provide an access point to your registered components from the `client` instance.
0 commit comments