Skip to content

Toggle button toolbar in WordPress

A group of icon buttons with a single active choice, like text alignment.

Here is an excerpt of this component's code in WordPress. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in WordPress

barre-boutons-bascule.php
<?php
/**
 * Plugin Name: Forge, Barre de boutons bascule
 * Description: Un groupe de boutons icônes où un seul choix est actif, comme l'alignement d'un texte. Ajoute la composition « Barre de boutons bascule » à l'éditeur de blocs.
 * Version: 1.0.0
 * Requires at least: 6.6
 * Requires PHP: 8.1
 * Author: Forge
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: forge-barre-boutons-bascule
 *
pattern.html
<!-- wp:html -->
<div class="toolbar" role="toolbar" aria-label="Alignement du texte" data-toolbar>
        <button type="button" class="toolbar__button" aria-pressed="true" aria-label="Aligner à gauche">
          <svg viewBox="0 0 20 20" aria-hidden="true"><path d="M3 5h14M3 9h9M3 13h14M3 17h9"/></svg>
        </button>
        <button type="button" class="toolbar__button" aria-pressed="false" aria-label="Centrer" tabindex="-1">
          <svg viewBox="0 0 20 20" aria-hidden="true"><path d="M3 5h14M5.5 9h9M3 13h14M5.5 17h9"/></svg>
        </button>
        <button type="button" class="toolbar__button" aria-pressed="false" aria-label="Aligner à droite" tabindex="-1">
          <svg viewBox="0 0 20 20" aria-hidden="true"><path d="M3 5h14M8 9h9M3 13h14M8 17h9"/></svg>
        </button>
        <button type="button" class="toolbar__button" aria-pressed="false" aria-label="Justifier" tabindex="-1">
script.js
(() => {
  if (!document.querySelector(".toolbar")) return;
  document.querySelectorAll("[data-toolbar]").forEach((toolbar) => {
    const buttons = [...toolbar.querySelectorAll("button")];

    function select(button) {
      buttons.forEach((candidate) => {
        const active = candidate === button;
        candidate.setAttribute("aria-pressed", String(active));
        candidate.tabIndex = active ? 0 : -1;
      });
    }
style.css
:root {
  --toolbar-bg: #ffffff;
  --toolbar-border: #d4d4d8;
  --toolbar-icon: #52525b;
  --toolbar-active-bg: #18181b;
  --toolbar-active-icon: #ffffff;
  --toolbar-hover: #f4f4f5;
  --toolbar-ring: #2563eb;
  --demo-barre-boutons-bascule-bg: #f4f4f5;
  --demo-barre-boutons-bascule-text: #18181b;
}

Free component: copy it freely, including for commercial projects.

Buttons components in WordPress
  • A row of buttons to share the page on Facebook, LinkedIn, WhatsApp or by email, with a copy-link button and no third-party script.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Toggle heart buttons that count favourites, remember them between visits and announce them to screen readers.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A button that adds an event to Google Calendar, Outlook or any calendar through an .ics file, with no third-party service.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A button that copies an IBAN, a reference, an address or a promo code and confirms "Copied", on screen and to screen readers.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A WhatsApp button pinned to the bottom of the screen that opens a welcome bubble with common topics and a pre-filled message, with no third-party widget.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A bar pinned to the bottom of mobile screens with Call and Directions buttons and a live open-now status, built for trades and local shops.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component