Problem/Motivation

Layout builder's interface is always jumping (scrolling) to the top of the page after removing a section or a block.
This is especially annoying when dealing with a page with lots of contents as we need to re-scroll back to where we were each time.

Steps to reproduce:

1. Install default site profile. Theme Oliviero will be configured on Front.
2. Enable module "Layout Builder"
3. On page "Block Layout" /admin/structure/block disable blocks "Search form (narrow)" and "Search form (wide)"
4. Enable Layout Builder for a content type, for example Basic Page "/admin/structure/types/manage/page/display"
5. Go to Layout Builder configuration for a Content type /admin/structure/types/manage/page/display/default/layout
6. Try to add a new Section/Block
7. Remove a Section
8. After submit, the page focuses on a "Save" button which is on top.

The issue is related to Drupal AJAX behavior on success defined in core/misc/ajax.js, see $(target).trigger('focus');.
The AJAX system automatically moves focus to the first tabbable element after closing a dialog, sometimes scrolling to a page top.

Proposed resolution

Disable refocus after a dialog close for Layout Rebuild forms (Remove Section/Block).

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3152561

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

EricRondo created an issue. See original summary.

tim.plunkett’s picture

Version: 8.9.x-dev » 9.1.x-dev
Issue summary: View changes
Status: Active » Needs review
Issue tags: -Layout Builder +Blocks-Layouts
StatusFileSize
new1.11 KB

Oh wow, that is very annoying indeed.

It looks like this code was added in #2994909: Highlight active element while working with dialogs in Layout Builder.
It makes sense for the cases where something is being added or configured. But in the case of removal, I agree it should stay put.

Please try this patch (don't worry about the 9.1 vs 8.9, it should be the same for both)

Status: Needs review » Needs work

The last submitted patch, 2: 3152561-highlight-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

EricRondo’s picture

Sorry this is not working for me, same behavior (applied patch, flushed cache, added block, then removed : back to top :-))

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

twiik’s picture

Priority: Normal » Major

Not working for me either. Testing on 8.9.15.

I also tried removing all the "scroll"-code I could find in the layout builder javascript files, but that made no difference to this behavior.

The only way I was able to stop it from happening was by removing the AjaxResponse in the LayoutRebuildTrait, but then nothing happens obviously, but just creating a new AjaxResponse in rebuildLayout function with none of the other code cause it to happen. Like it still happens if my LayoutRebuildTrait looks like this:

trait LayoutRebuildTrait {

  /**
   * Rebuilds the layout.
   *
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   An AJAX response to either rebuild the layout and close the dialog, or
   *   reload the page.
   */
  protected function rebuildAndClose(SectionStorageInterface $section_storage) {
    $response = $this->rebuildLayout($section_storage);
//    $response->addCommand(new CloseDialogCommand('#drupal-off-canvas'));
    return $response;
  }

  /**
   * Rebuilds the layout.
   *
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   An AJAX response to either rebuild the layout and close the dialog, or
   *   reload the page.
   */
  protected function rebuildLayout(SectionStorageInterface $section_storage) {
    $response = new AjaxResponse(); // <-- If the entire AjaxResponse is removed then we no longer scroll to the top of the page when removing a block or layout section.
//    $layout = [
//      '#type' => 'layout_builder',
//      '#section_storage' => $section_storage,
//    ];
//    $response->addCommand(new ReplaceCommand('#layout-builder', $layout));
    return $response;
  }

}

Maybe you can alter the AjaxResponse and something else is altering in some behavior which causes the browser to scroll to the top of the page if the response is there? I dunno, I'm not very knowledgable about how anything fits together in Drupal, especially not if involves javascript. :p

I'm changing the priority to "Major" as I feel this is currently a huge UX issue, albeit one which hopefully has a very simple fix. I'm just not able to find it.

james hawthorn-byng’s picture

Just tested this patch on 9.1.10 and also doesn't work :(
I agree that this is quite a major UX issue especially as pages can get quite long.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pyrello’s picture

I just tested this against 9.3.x and the patch still applies, but the behavior that it causes does not seem correct. What I would expect to see is that the page does not scroll/jump to the top before scrolling/jumping to the location of the block or section being removed. Instead what the patch does is preserves the initial jump to the top, but then the jump back to the element being removed never happens.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

lexbritvin made their first commit to this issue’s fork.

lexbritvin’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new1.44 KB

After investigation, the issue doesn't always reproduce. I added steps to reproduce to the issue summary. And updated the proposed resolution block.

I tested the solution on 9, 10 and 11-dev.
I opened a MR with the proposed solution. Likewise, I also attach a patch for history and usage in a project.

I'm not sure if we can add tests for this change.

smustgrave’s picture

Priority: Major » Normal
Status: Needs review » Reviewed & tested by the community
Issue tags: +Bug Smash Initiative, +Needs Review Queue Initiative

Verified the issue following the steps in the IS
Patch #16 did solve the issue.

Lowering to normal though as annoying as this is doesn't seem to break anything.

Thanks!

  • larowlan committed 928467a9 on 10.1.x
    Issue #3152561 by lexbritvin, tim.plunkett, EricRondo, TwiiK: Layout...

  • larowlan committed 57366cba on 11.x
    Issue #3152561 by lexbritvin, tim.plunkett, EricRondo, TwiiK: Layout...
larowlan’s picture

Version: 11.x-dev » 10.1.x-dev
Status: Reviewed & tested by the community » Fixed

I agree this would be difficult to test, and it can be easily manually tested. On the basis of our new proposed heuristics I think this can go in without tests.

I checked in gitlab for any other classes in contrib that extend this class that may not want this behaviour but could not find any.

Committed to 11.x and backported to 10.1.x

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.