Skip to main content

Overview

ButtonComponent creates interactive buttons with support for icons, tooltips, and various visual styles. It extends BaseComponent and is commonly used in settings, modals, and custom views.

Constructor

containerEl
HTMLElement
required
The parent HTML element where the button will be created
const button = new ButtonComponent(containerEl);

Properties

buttonEl
HTMLButtonElement
Direct access to the underlying HTML button element
disabled
boolean
Whether the button is currently disabled

Methods

setButtonText

Sets the text label displayed on the button.
name
string
required
The button text to display
button.setButtonText("Save Settings");
Returns: this (for method chaining)

setIcon

Sets an icon to display on the button.
icon
IconName
required
The icon ID to use. See Obsidian Icons for available icons.
button.setIcon("checkmark");
Returns: this (for method chaining)

setTooltip

Adds a hover tooltip to the button.
tooltip
string
required
The tooltip text to display
options
TooltipOptions
Optional configuration for tooltip placement, delay, and styling
button.setTooltip("Click to save", {
  placement: "top",
  delay: 500
});
Returns: this (for method chaining)

onClick

Registers a callback function to execute when the button is clicked.
callback
(evt: MouseEvent) => unknown | Promise<unknown>
required
Function to call on click. Can be synchronous or asynchronous.
button.onClick(async (evt) => {
  console.log("Button clicked!");
  await this.plugin.saveSettings();
});
Returns: this (for method chaining)

setCta

Sets the button as a Call-to-Action (CTA) with prominent styling.
button.setCta();
Returns: this (for method chaining)

removeCta

Removes the CTA styling from the button.
button.removeCta();
Returns: this (for method chaining)

setWarning

Styles the button as a warning/destructive action.
button.setWarning();
Returns: this (for method chaining)

setDisabled

Enables or disables the button.
disabled
boolean
required
Whether the button should be disabled
button.setDisabled(true);
Returns: this (for method chaining)

setClass

Adds a CSS class to the button.
cls
string
required
The CSS class name to add
button.setClass("custom-button-style");
Returns: this (for method chaining)

then

Facilitates method chaining with a callback.
cb
(component: this) => any
required
Callback function that receives the component instance
button.then((btn) => {
  btn.setButtonText("Click me");
});
Returns: this (for method chaining)

Usage Examples

Basic Button

new Setting(containerEl)
  .setName("Actions")
  .setDesc("Perform common actions")
  .addButton((button) => {
    button
      .setButtonText("Reset")
      .onClick(async () => {
        await this.plugin.resetSettings();
        new Notice("Settings reset!");
      });
  });

Button with Icon

new Setting(containerEl)
  .setName("Sync")
  .addButton((button) => {
    button
      .setButtonText("Sync Now")
      .setIcon("sync")
      .setTooltip("Synchronize with server")
      .onClick(async () => {
        await this.plugin.sync();
      });
  });

Call-to-Action Button

new Setting(containerEl)
  .setName("Save")
  .addButton((button) => {
    button
      .setButtonText("Save Settings")
      .setCta()
      .onClick(async () => {
        await this.plugin.saveSettings();
        new Notice("Settings saved!");
      });
  });

Warning/Destructive Button

new Setting(containerEl)
  .setName("Danger Zone")
  .setDesc("Permanently delete all data")
  .addButton((button) => {
    button
      .setButtonText("Delete All")
      .setWarning()
      .onClick(async () => {
        const confirmed = confirm("Are you sure?");
        if (confirmed) {
          await this.plugin.deleteAllData();
        }
      });
  });

Disabled Button

const button = new ButtonComponent(containerEl)
  .setButtonText("Submit")
  .setDisabled(true);

// Enable later when conditions are met
if (isFormValid) {
  button.setDisabled(false);
}

Icon-Only Button

new Setting(containerEl)
  .addButton((button) => {
    button
      .setIcon("trash")
      .setTooltip("Delete item")
      .setClass("icon-button")
      .onClick(async () => {
        await this.deleteItem();
      });
  });

Async Button with Loading State

const button = new ButtonComponent(containerEl)
  .setButtonText("Process")
  .onClick(async () => {
    button.setDisabled(true);
    button.setButtonText("Processing...");
    
    try {
      await this.plugin.processData();
      new Notice("Success!");
    } catch (error) {
      new Notice("Error: " + error.message);
    } finally {
      button.setDisabled(false);
      button.setButtonText("Process");
    }
  });

See Also

  • Setting - Container for form components
  • Modal - Dialog with buttons
  • Notice - Toast notifications

Build docs developers (and LLMs) love