Popover menu doesn't work without a refresh

Created on 28 March 2023, over 1 year ago

Problem/Motivation

When using the popover menu style, the javascript is attached as a behavior that checks for the direct add buttons in the context parameter. These buttons are found, but next, there is a check for the body element in the context, which returns null as the context is the #layout-builder element. Additionally, this second check seems unnecessary as we've already established that there are 'add block' buttons present.

As a result, the buttons will not work until you have refreshed the page or triggered another behavior which has the body as context.

Steps to reproduce

  • Install lb_direct_add and configure the popover menu style
  • Create a node with layout builder enabled
  • Add a section and click 'Add block' (or whatever label you have configured)
  • There will be no popover menu, instead you are redirected to the add block route within the admin interface

Proposed resolution

Simplify the javascript and remove the unnecessary second check for the body element.

  /**
   * Attaches the JS test behavior to label links.
   */
  Drupal.behaviors.lbDirectAdd = {
    attach: function (context, settings) {
      console.log(context);
      var $directAddWidgets = $(once('lb_direct_add', '.layout-builder__add-block', context));

      if ($directAddWidgets.length) {
        var $body = $(once('lb_direct_add-click', 'body', context));

        if ($body.length) {
          $body.on('click', '.layout-builder__direct-add__toggle', directAddClickHandler);
        }

        var il = $directAddWidgets.length;

        for (var i = 0; i < il; i++) {
          DirectAddWidget.widgets.push(new DirectAddWidget($directAddWidgets[i], settings.lbDirectAdd));
        }
      }
    }
  };

Can be simplified to:

  /**
   * Attaches the JS test behavior to label links.
   */
  Drupal.behaviors.lbDirectAdd = {
    attach: function (context, settings) {
      $(once('bind-click', '.layout-builder__add-block', context)).each(function() {
        $(this).on('click', '.layout-builder__direct-add__toggle', directAddClickHandler);
        DirectAddWidget.widgets.push(new DirectAddWidget(this, settings.lbDirectAdd));
      });
    }
  };
πŸ› Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024