Markup for the AI (syntax)

For developersVersion 1.0.0Last updated:

Inside the item template (the menu’s first child), an element with data-bos-submenu is the container for the children. The children are rendered with the same template, so they support several levels.

<ul class="site-nav" data-bos-edit="menu" data-bos-id="nav" data-bos-name="Menú principal" data-bos-default="auto">
  <li><a href="index.html">Inicio</a><ul class="sub-menu" data-bos-submenu></ul></li>
  <li><a href="servicios.html">Servicios</a></li>
</ul>
  • Without a data-bos-submenu container, the menu only shows the first level.
  • Custom template for the children (optional): if the container has a child with data-bos-item="template", the children use that template:
<li>
  <a href="#">Servicios</a>
  <div class="mega" data-bos-submenu>
    <a class="mega__link" href="#" data-bos-item="template">Consultoría</a>
  </div>
</li>
  • Submenu button (optional): a <button data-bos-submenu-toggle> inside the template is only shown on items that have children, and receives aria-expanded="false" and aria-controls.
  • Sample links: you can write sample children inside the container. If you leave it empty, it isn’t shown while the menu displays the sample links.

What BuilderOS adds when rendering the menu:

WhereWhat
Item with childrenClasses has-submenu menu-item-has-children.
ContainerClass sub-menu and a unique id.
Link of the item with children (if there is no button)aria-haspopup="true", aria-expanded="false" and aria-controls.
data-bos-submenu-toggle buttonaria-expanded="false" and aria-controls.
Current itemClasses is-active current-menu-item and aria-current="page" on the link.
Ancestors of the current onecurrent-menu-ancestor (and current-menu-parent on the direct parent).
Any itemThe CSS classes the client sets in the menu editor.

The data-bos-* attributes are removed from the final HTML. Write the CSS and JS with classes (.sub-menu, .has-submenu), not with [data-bos-submenu]. aria-expanded is updated by the package’s JS.

Use two menu fields: one for desktop and one for the mobile panel. On mobile, set data-bos-menu-fallback="<id del de escritorio>": if the client doesn’t assign it a menu of its own, it shows the same one as desktop.

  • The field with a fallback must not carry data-bos-default.
  • The button text goes in a text field so it gets translated in i18n/<idioma>.json.
  • The button is a <button> with aria-expanded and aria-controls pointing to the panel, which starts with hidden.
  • The JS is initialized with BuilderOS.ready and tolerates running several times.
<header class="site-header" data-bos-block="Cabecera" data-bos-role="header">
  <a href="index.html" class="brand"><span data-bos-edit="text" data-bos-id="brand" data-bos-name="Marca">Demo</span></a>

  <ul class="site-nav" data-bos-edit="menu" data-bos-id="nav" data-bos-name="Menú principal" data-bos-default="auto">
    <li><a href="index.html">Inicio</a><ul class="sub-menu" data-bos-submenu></ul></li>
    <li><a href="contacto.html">Contacto</a></li>
  </ul>

  <button class="nav-toggle" type="button" aria-expanded="false" aria-controls="site-drawer">
    <span class="nav-toggle__bar" aria-hidden="true"></span>
    <span class="sr-only" data-bos-edit="text" data-bos-id="menu-label" data-bos-name="Texto del botón de menú">Menú</span>
  </button>
  <div class="site-drawer" id="site-drawer" hidden>
    <ul class="site-drawer__nav" data-bos-edit="menu" data-bos-id="nav-mobile" data-bos-name="Menú móvil" data-bos-menu-fallback="nav">
      <li><a href="index.html">Inicio</a><ul class="sub-menu" data-bos-submenu></ul></li>
      <li><a href="contacto.html">Contacto</a></li>
    </ul>
  </div>
</header>
.site-nav li { position: relative; }
.site-nav .sub-menu { display: none; position: absolute; top: 100%; left: 0; }
.site-nav li:hover > .sub-menu,
.site-nav li:focus-within > .sub-menu { display: block; }
.nav-toggle { display: none; }
.site-drawer[hidden] { display: none; }
@media (max-width: 760px) {
  .site-nav { display: none; }
  .nav-toggle { display: inline-flex; }
  .site-drawer .sub-menu { padding-left: 16px; }
}
BuilderOS.ready(function (block) {
  var toggle = block.querySelector('.nav-toggle');
  var drawer = block.querySelector('.site-drawer');
  if (!toggle || !drawer || toggle.hasAttribute('data-nav-ready')) {
    return;
  }
  toggle.setAttribute('data-nav-ready', '');
  function setOpen(open) {
    toggle.setAttribute('aria-expanded', open ? 'true' : 'false');
    drawer.hidden = !open;
  }
  toggle.addEventListener('click', function () {
    setOpen(toggle.getAttribute('aria-expanded') !== 'true');
  });
  document.addEventListener('keydown', function (e) {
    if (e.key === 'Escape' && toggle.getAttribute('aria-expanded') === 'true') {
      setOpen(false);
      toggle.focus();
    }
  });
  // Desplegables de escritorio: aria-expanded sigue al ratón y al foco.
  block.querySelectorAll('.site-nav .has-submenu').forEach(function (item) {
    var link = item.querySelector('[aria-expanded]');
    if (!link) {
      return;
    }
    item.addEventListener('mouseenter', function () { link.setAttribute('aria-expanded', 'true'); });
    item.addEventListener('mouseleave', function () { link.setAttribute('aria-expanded', 'false'); });
    item.addEventListener('focusin', function () { link.setAttribute('aria-expanded', 'true'); });
    item.addEventListener('focusout', function (e) {
      if (!item.contains(e.relatedTarget)) {
        link.setAttribute('aria-expanded', 'false');
      }
    });
  });
});
{ "header": { "brand": "Demo", "menu-label": "Menu" } }

The sample package (examples/demo-site) includes this complete header.

More menus (footer, legal links…)

Each menu field in the header or footer appears as a location in Home → Menus. Give it a clear data-bos-name, because it is the name the client will see:

<ul class="legal" data-bos-edit="menu" data-bos-id="legal" data-bos-name="Enlaces legales">
  <li><a href="aviso-legal.html">Aviso legal</a></li>
</ul>

❌ What NOT to do

  • Don’t duplicate the same menu in the HTML for desktop and mobile with JavaScript that copies the links: use two fields and data-bos-menu-fallback.
  • Don’t put the submenus outside the item template (the menu’s first child): only the container inside it is read.
  • Don’t use [data-bos-submenu] or other data-bos-* in the CSS or JS: they are removed on publishing.
  • Don’t use a <div> or an <a> as the mobile menu button: use <button> with aria-expanded and aria-controls.