Skip to content

Dropdown menu in WordPress

A navigation bar with dropdown submenus that close with the Escape key, an outside click or when focus leaves the menu.

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

menu-deroulant.php
<?php
/**
 * Plugin Name: Forge, Menu déroulant
 * Description: Une barre de navigation avec sous-menus déroulants qui se ferment à la touche Échap, au clic extérieur ou quand le focus quitte le menu. Ajoute la composition « Menu déroulant » à 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-menu-deroulant
 *
pattern.html
<!-- wp:html -->
<nav class="ddnav" aria-label="Navigation principale" data-disclosure-nav>
        <ul class="ddnav__list">
          <li><a class="ddnav__link" href="#accueil" aria-current="page">Accueil</a></li>
          <li class="ddnav__item">
            <button type="button" class="ddnav__link" aria-expanded="false" aria-controls="ddnav-services" data-disclosure>Services<svg class="ddnav__chevron" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></button>
            <ul class="ddnav__panel" id="ddnav-services" hidden>
              <li><a href="#depannage"><strong>Dépannage 7 j/7</strong><span>Fuite, WC bouché : intervention sous 2 h</span></a></li>
              <li><a href="#salle-de-bain"><strong>Salle de bain</strong><span>Rénovation complète, douche à l'italienne</span></a></li>
              <li><a href="#chauffage"><strong>Chauffage</strong><span>Chaudières, pompes à chaleur, radiateurs</span></a></li>
              <li><a href="#entretien"><strong>Contrat d'entretien</strong><span>Visite annuelle et attestation incluses</span></a></li>
            </ul>
script.js
(() => {
  if (!document.querySelector(".ddnav")) return;
  document.querySelectorAll("[data-disclosure-nav]").forEach((nav) => {
    const buttons = [...nav.querySelectorAll("[data-disclosure]")];
    const panelOf = (button) => document.getElementById(button.getAttribute("aria-controls"));

    function close(button, focus) {
      button.setAttribute("aria-expanded", "false");
      panelOf(button).hidden = true;
      if (focus) button.focus();
    }
style.css
:root {
  --dd-bar: #ffffff;
  --dd-text: #1c1917;
  --dd-muted: #57534e;
  --dd-line: #e7e5e4;
  --dd-hover: #f5f5f4;
  --dd-accent: #b91c1c;
  --dd-ring: #1c1917;
  --dd-shadow: 0 18px 40px -20px rgb(28 25 23 / 0.45);
  --demo-menu-deroulant-bg: #f5f5f4;
  --demo-menu-deroulant-text: #1c1917;
}

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

Breadcrumbs and pagination components in WordPress
  • A bottom bar with Call, Directions and Free quote that tucks away while visitors read down the page and returns as soon as they scroll up. Built for tradespeople whose customers arrive on a phone.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A slim bar at the top of the page with a message, a link and a close button; once closed, it stays hidden for the period you choose.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A sticky sidebar table of contents for blog posts: it highlights the section being read, shows the reading time left and folds into a list under the title on mobile.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A column of dots on the side of the page that follows the section being read and lets visitors jump to it, with a label on hover and focus.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • An order step indicator: finished steps clickable to go back, the current step announced, upcoming steps locked.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Tooltips in four positions that open on hover, tap and keyboard focus, and close with Escape, as WCAG requires.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component