# Buttons, selects and layouts

> Message components: buttons, selects, and Components V2 containers, sections and media.

Only bots may send components, and **pictures and files are attachments of the same message, never
URLs**: clients never load anything remote to show a component.

## Buttons and selects (classic layout)

Up to 5 **action rows**; each holds up to 5 buttons or one select.

**TypeScript:**

```ts

interaction.reply({
  content: 'Pick one',
  components: [
    new ActionRowBuilder().addComponents(
      new ButtonBuilder().setCustomId('a').setLabel('Option A').setStyle(ButtonStyle.Primary),
      new ButtonBuilder().setCustomId('b').setLabel('Option B').setStyle(ButtonStyle.Secondary),
      new ButtonBuilder().setURL('https://zaun.example/help').setLabel('Help'),
    ),
    new ActionRowBuilder().addComponents(
      new StringSelectMenuBuilder().setCustomId('colour').setPlaceholder('Colour')
        .addOptions({ label: 'Red', value: 'red' }, { label: 'Blue', value: 'blue' }),
    ),
  ],
});
```

Clicks go to `bot.component(customId, handler)`. Answer with `interaction.update(..)` to change the
message in place, `interaction.reply(..)` for a new message, or `interaction.deferUpdate()`.

```ts
bot.component('a', (interaction) => interaction.update({ content: 'You picked A', components: [] }));
bot.component('colour', (interaction) =>
  interaction.reply({ content: `Colour: ${interaction.values.join(', ')}`, ephemeral: true }));
```

**Rust:**

```rust
Reply::new()
    .content("Pick one")
    .row(ActionRow::buttons([
        Button::primary("a", "Option A"),
        Button::secondary("b", "Option B"),
        Button::link("https://zaun.example/help", "Help"),
    ]))
    .row(ActionRow::select(
        Select::strings("colour", &[("Red", "red"), ("Blue", "blue")]).placeholder("Colour"),
    ));
```

Clicks arrive in `on_component` with the component's `custom_id` (and `values` for selects).
Answer with `ctx.update_message(..)` to change the message in place, `ctx.reply(..)` for a new
message, or `ctx.defer_update()`.

```rust
fn on_component(&mut self, ctx: Context, c: ComponentData) {
    match c.custom_id.as_str() {
        "a" => { let _ = ctx.update_message(Reply::new().content("You picked A")); }
        "colour" => { let _ = ctx.reply_ephemeral(format!("Colour: {}", c.values.join(", "))); }
        _ => {}
    }
}
```

Link buttons take `https://` only, and readers confirm before they open.

## Components V2

A Components V2 message has no content or embeds: components carry everything. The flag can't be
removed later. In TypeScript, a reply whose components are layout builders (a container, section,
text display, …) is sent as V2; in Rust, `Reply::v2()` or `Reply::container(..)` turns it on.

**TypeScript:**

```ts
import {
  ActionRowBuilder, ButtonBuilder, ButtonStyle, ContainerBuilder, SectionBuilder,
  SeparatorBuilder, TextDisplayBuilder,
} from '@zaun/bot';

interaction.reply({
  components: [
    new ContainerBuilder().setAccentColor(0x5865f2)
      .addTextDisplayComponents(new TextDisplayBuilder().setContent('## Release 1.2\nEverything new this week.'))
      .addSeparatorComponents(new SeparatorBuilder())
      .addSectionComponents(new SectionBuilder()
        .addTextDisplayComponents(new TextDisplayBuilder().setContent('Read the notes'))
        .setButtonAccessory(new ButtonBuilder().setCustomId('notes').setLabel('Open').setStyle(ButtonStyle.Primary)))
      .addActionRowComponents(new ActionRowBuilder().addComponents(
        new ButtonBuilder().setCustomId('ok').setLabel('Got it').setStyle(ButtonStyle.Success))),
  ],
});
```

**Rust:**

```rust
Reply::new().container(
    Container::new()
        .accent(0x5865F2)
        .text("## Release 1.2\nEverything new this week.")
        .separator()
        .section(Section::with_button(&["Read the notes"], Button::primary("notes", "Open")))
        .row(ActionRow::buttons([Button::success("ok", "Got it")])),
);
```

## Reference

| `type` | Component | TypeScript builder | Where | Key fields and limits |
|---|---|---|---|---|
| 1 | Action Row | `ActionRowBuilder` | message, container | 1–5 buttons or 1 select |
| 2 | Button | `ButtonBuilder` | action row, section accessory | styles 1 primary, 2 secondary, 3 success, 4 danger, 5 link; label ≤ 80; emoji; custom id for 1–4 |
| 3 | String Select | `StringSelectMenuBuilder` | action row, modal label | ≤ 25 options; placeholder ≤ 150; min/max values 0–25 |
| 4 | Text Input | `TextInputBuilder` | modal label | short or paragraph; length 0–4 000 |
| 5–8 | User / Role / Mentionable / Channel Select | `UserSelectMenuBuilder`, `RoleSelectMenuBuilder`, `MentionableSelectMenuBuilder`, `ChannelSelectMenuBuilder` | action row, modal label | default values; channel types (8) |
| 9 | Section | `SectionBuilder` | V2 message, container | 1–3 text displays + a button or thumbnail |
| 10 | Text Display | `TextDisplayBuilder` | V2 message, container, section, modal | Markdown; ≤ 4 000 characters per message in all |
| 11 | Thumbnail | `ThumbnailBuilder` | section accessory | an image attachment |
| 12 | Media Gallery | `MediaGalleryBuilder` | V2 message, container | 1–10 attachments |
| 13 | File | `FileBuilder` | V2 message, container | one attachment |
| 14 | Separator | `SeparatorBuilder` | V2 message, container | divider; small or large spacing |
| 17 | Container | `ContainerBuilder` | V2 message | children 1, 9, 10, 12, 13, 14; accent colour; spoiler |
| 18 | Label | `LabelBuilder` | modal | label ≤ 45, description ≤ 100, one input |
| 19 | File Upload | `FileUploadBuilder` | modal label | 0–10 files |
| 21 | Radio Group | `RadioGroupBuilder` | modal label | 2–10 options |
| 22 | Checkbox Group | `CheckboxGroupBuilder` | modal label | 1–10 options |
| 23 | Checkbox | `CheckboxBuilder` | modal label | never required |

Totals: classic messages up to 5 action rows; V2 messages up to 40 components counting nested ones.
Custom ids are 1–100 bytes and unique per message or modal. Premium buttons are not supported.
