Skip to content

Toggle Button

Toggle Button is a component that creates a group of buttons where only one can be active at a time, similar to radio buttons but with a different visual style. It’s also known as n8n radio buttons in some contexts.

See the Example by Serhii Pimenov on CodePen.

<div data-role="toggle-button">
<button class="button active" data-value="1">Editor</button>
<button class="button" data-value="2">Executions</button>
<button class="button" data-value="3">Evaluations</button>
</div>

The component automatically adds the class toggle-button to the container element. The button with the active class will be selected by default. If no button has the active class, the first button will be selected by default.

const element = Metro.makePlugin("#myToggleButton", "toggle-button");
ParameterTypeDefaultDescription
onChangefunctionMetro.noopCallback is called then active button changed
onButtonClickfunctionMetro.noopCallback is called when the button is clicked
onToggleButtonCreatefunctionMetro.noopCallback is called when the toggle button is created
  • destroy() - Removes the element from the DOM.
const toggleButton = Metro.getPlugin('#myToggleButton', 'toggle-button');
toggleButton.destroy();
EventDescription
onToggleButtonCreateTriggered when the toggle button is created
onChangeTriggered when active button changed. The event handler receives an object with a button
onButtonClickTriggered when a button is clicked. The event handler receives an object with a button
// Using data attribute
<div data-role="toggle-button" data-on-change="console.log('Button changed:', arguments[0].button.data('value'))">
<!-- buttons here -->
</div>
// Using JavaScript
const toggleButton = Metro.getPlugin('#myToggleButton', 'toggle-button');
toggleButton.element.on("change", function(e, data) {
console.log('Button changed:', data.button.data('value'));
});
VariableDefault (Light)Dark ModeDescription
--toggle-button-background#dadee9#545957Background color of the toggle button container
--toggle-button-color#7e7e77#bdc5d2Text color of inactive buttons
--toggle-button-item-background#ffffff#2c2f2eBackground color of the active button
--toggle-button-item-color#191919#ffffffText color of the active button
--toggle-button-item-hover-colorhsl(7, 100%, 68%)hsl(7, 100%, 68%)Text color of buttons on hover
#myToggleButton {
--toggle-button-background: #f0f0f0;
--toggle-button-color: #333333;
--toggle-button-item-background: #007bff;
--toggle-button-item-color: #ffffff;
--toggle-button-item-hover-color: #ff9900;
}
  • .toggle-button - The main container class (added automatically)
  • .active - Applied to the currently selected button
  • Always include at least one button with the active class to ensure a default selection
  • Use the data-value attribute to store values for each button
  • Keep button labels concise to maintain a clean appearance
  • Consider using the toggle button component for options that are mutually exclusive
  • For accessibility, ensure that the toggle button has appropriate ARIA attributes if needed