Problem/Motivation

The admin toolbar applies a padding to the top of the body element that should be equal to its height.

However, with the following scenarios, the height calculation is incorrect.

  1. Settings Tray is enabled and is accessible by the user, the page does not contain any contextual links (?) and the front-end (?) theme uses Bootstrap CSS
  2. The displayed toolbar tabs break into multiple lines.

Proposed resolution

Refactor Drupal.toolbar.ToolbarVisualView.updateToolbarHeight() to fix the height calculation issues.

Remaining tasks

* Test
* Patch
* Get a review

User interface changes

Toolbar does not overlap page content anymore.

API changes

Nothing.

Data model changes

Nothing.

Original report

The admin toolbar applies a padding to the top of the body element that should be equal to its height.

However in the following scenario the height calculation is incorrect.

Settings Tray module adds a tab to the toolbar with a class of hidden. It's the first element inside #toolbar-bar.

Bootstrap css defines hidden as display: none !important;

In ToolbarVisualView.js the updateToolbarHeight method sets toolbarTabOuterHeight by getting the outerHeight of the first .toolbar-tab

Because the first tab is hidden (and crucially, because of bootstrap it has !important), the outerHeight is 0px and thus the body receives a top padding of 0px.

I think the height calculation needs to do something like check the height of tallest tab, not just first one. Or perhaps just ensure the tab it selects to get the height isn't hidden/

Here is an example of calculating the height on an element that is display: none!important.
https://jsfiddle.net/marmite/d9oma2e3/7/

The jQuery documentation for outerHeight says:
The value reported by .outerHeight() is not guaranteed to be accurate when the element or its parent is hidden. To get an accurate value, ensure the element is visible before using .outerHeight(). jQuery will attempt to temporarily show and then re-hide an element in order to measure its dimensions, but this is unreliable and (even when accurate) can significantly impact page performance. This show-and-rehide measurement feature may be removed in a future version of jQuery.

Issue fork drupal-2958478

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

jessebaker created an issue. See original summary.

adriancid’s picture

Hi @jessebaker thanks for reporting, would be great if the next time you use the Issue Summary Template to report an issue.

Do you have some strange behavior with the module? Can you show some screens?

jessebaker’s picture

StatusFileSize
new48.91 KB

My apologies, I'd not seen the template and will use it next time.

Attached is a screenshot of the issue caused by the incorrect height calculation. Note the page title is under the admin toolbar.

adriancid’s picture

I see, can you help us providing a patch for this?

jessebaker’s picture

Oh! I have just looked into this more closely and I'm afraid I made a mistake. My report is correct but It looks like the code which is causing the issue is in a file in the Drupal Core Toolbar module, not Admin Toolbar. I'm sorry!

adriancid’s picture

@jessebaker no problem, this is normal, ;-) you can move this issue to the Drupal core issue queue and select the correct component.

adriancid’s picture

Project: Admin Toolbar » Drupal core
Version: 8.x-1.23 » 8.6.x-dev
Component: User interface » toolbar.module
chanderbhushan’s picture

Status: Active » Needs review
StatusFileSize
new964 bytes

Hi,

https://www.drupal.org/project/drupal/issues/2959062 same issue in drupal 8.4.x-dev. I have applied patch. thanks.

borisson_’s picture

wrd’s picture

Christopher Riley’s picture

Yes it does thank you so much for this patch.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dsrikanth’s picture

Status: Needs review » Reviewed & tested by the community

Confirming this patch works.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs subsystem maintainer review

Tagging for subsystem maintainer review.

The latest patch also changes the compiled JavaScript file directly. Please update the patch to change the ES6 file and then compile it to take effect. More info: https://www.drupal.org/node/2815083.

huzooka’s picture

What if we just use the toolbar-bar's height if it contains at least one visible item (instead of the first visible item's height)?

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

rj’s picture

StatusFileSize
new1.36 KB

The attached patch changes both the compiled JS file directly and the ES6 file.

shubham.prakash’s picture

Status: Needs work » Needs review
afeijo’s picture

Status: Needs review » Reviewed & tested by the community

Confirming this patch works.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

huzooka’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs review
StatusFileSize
new161.05 KB
new12.77 KB
new17.08 KB

Added a test-only patch and a test+fix patch with an improved toolbar height calculation.

huzooka’s picture

Title: Height calculation error when used with Settings Tray and Bootstrap css » Toolbar height calculation is faulty in multiple cases
huzooka’s picture

Assigned: Unassigned » huzooka

huzooka’s picture

Summary about the test extensions:

Toolbar height test theme (toolbar_test_themeTest):

  • Extends stark.
  • Contains a library extension for the system/base library's hidden.module.css that hides .hidden elements with display: none !important;. This mimics the attribute of Bootstrap that is crucial in the reported issue.

Toolbar items for toolbar testing module (toolbar_test_toolbar_items):

  • Adds a link to the home page with hook_page_top().

    The test tries to click on this element. Without the proper fix, this is impossible, because the toolbar overlaps this link.

  • Provides a toolbar tab item with .hidden CSS class and -1000 weight. This mimics the Edit toolbar item (provided by the contextual module and altered by settings_tray) thats get hidden if no contextual links are present on the current page.
  • Optionally provides additional toolbar (tab) items that can be set with Drupal State API. These items are used to make the toolbar bar occupy more lines (and make it much taller than expected). This is needed for underlining why #17 is not the best solution.

The test (contains four test cases):

  • The first test case reproduces the originally reported error (Settings Tray enabled, .hidden elements are removed with display: none !important;, current user has permission to access contextual links, but these are removed from the page).
  • The second test case tests uses three additional toolbar tab that follow the Manage item. This items are big enough to make the toolbar bar be multi-line.
  • The third test case tests uses the same extra toolbar tabs that case #2, but here those follow the first, hidden tab and precede the Manage item.
  • The fourth test uses a multi-line toolbar tray for testing the toolbar height calculation.
huzooka’s picture

Status: Needs review » Needs work
huzooka’s picture

Status: Needs work » Needs review
ajits’s picture

Status: Needs review » Needs work
StatusFileSize
new112.34 KB
new1 KB

I am currently working on a project which encountered a similar height issue of the toolbar causing the toolbar items overlapping the tray items. The cause was too many toolbar items (we use the contenta cms installation profile). To reproduce on vanilla Drupal, I have created a toolbar_test module which I am am attaching to this issue.

To reproduce:

  • Download
  • Un compress in modules/ directory
  • Install the module via drush or from /admin/modules
  • Zoom in if required. I had to zoom in to 120% to reproduce locally, which my colleague was able to see it at 100%

Adding a screenshot below (after clicking the manage item):

huzooka’s picture

Status: Needs work » Needs review

Re #29, @AjitS:

I'd fix the bug you described in a separate issue. This is about the toolbar height calculation.

Back to NR.

martijn.cuppens’s picture

It is possible to switch to position: sticky instead of position: fixed. This way, we can completely ditch the ugly padding hacks we have use now. The effect will be the same, only sticky doesn't work in IE (see https://caniuse.com/#feat=css-sticky). The toolbar will still functionally work in IE though.

I just tested the concept and succeeded to get rid of the padding hacks. There's some rewriting to be done for it (the navbar will need to use flexbox instead of floats and I'll need to finetune everything for mobile.

Let me know if I should continue on this. As well template, CSS and JS changes will be needed, it's gonna be quite a large patch.

denisev’s picture

Was there an issue made for #29 @huzooka? We have the same problem on a project and I am searching for a solution.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

porchlight’s picture

Status: Needs review » Needs work
StatusFileSize
new40.39 KB
new39.59 KB
new33.45 KB

I'm still running into an issue where the height is being calculated after orientation change. If I have 1 extra menu tab, the menu drops to 2 lines, and then if I stretch the screen a little wider the extra menu tab goes up onto the 1st line, but the toolbar height does not get calculated again so I end up with too much padding and a gap of space. Anyway we can calculate as the viewport width changes?

nod_’s picture

Issue tags: +Usability

To be honest at this point i would try to use position:fixed and let IE have a non-sticky toolbar, I was never comfortable doing this kind of page offsets. There were a few attempts a few years ago but support was too low. Need signoff from Usability folks though.

I can see we're not updating the drupal-offset-top attribute, any idea when se stopped using that?

nod_’s picture

Related check out @droplet module: https://www.drupal.org/project/toolbar_anti_flicker

huzooka’s picture

Status: Needs work » Needs review

@nod_, @porchlight you only have to apply https://www.drupal.org/project/drupal/issues/3035577 (listed as a related issue)

porchlight’s picture

@huzooka I tried the patch in that issue as well but still no luck. I'm not sure where to put my feedback at this point. I still think this needs work, but I'll just follow whichever one has more action.

martijn.cuppens’s picture

@nod_, position: fixed works in IE, position: sticky doesn't

FWIW, the CSS only solution would look something like this: https://codepen.io/MartijnCuppens/pen/eYpabGZ
In IE, the toolbar just isn't sticky.

nod_’s picture

Right I meant sticky! Sorry

huzooka’s picture

@porchlight, I thought you have to apply both patches at once.

The if I stretch the screen a little wider case is fixed by #3035577: Re-calculate toolbar's height every time when the viewport changes.

porchlight’s picture

@huzooka - I tried applying both this patch and the patch from #3035577 but they won't apply at the same time. Tried ordering this patch first then that one, didn't apply. Tried applying that patch then this one, didn't apply... Maybe they can be combined?

nod_’s picture

Priority: Minor » Normal
Status: Needs review » Needs work

Can we make a meta out of this issue please? too many different patches for slightly different issues. It's very hard to follow and review I could use some help aggregating everything. Thanks :)

huzooka’s picture

Status: Needs work » Needs review

@nod_, If you think that a meta would be useful, please create a new meta issue instead of changing this to a meta.

Maybe you missed, but I we have a patch here that also provides test coverage. Meta issues shouldn't have patches.

nod_’s picture

Status: Needs review » Needs work
Issue tags: -Needs subsystem maintainer review +JavaScript

Seems like there is an issue with offcanvas. Using the module that adds a link and a menu item, on the offcanvas test page, the link is hidden: /off-canvas-test-links and when opening/closing a tray, the margin is a bit too extreme probably not entirely toolbar fault but still a regression.

The code is very similar to Drupal.displace, in fact doing

      this.model.set('height', Drupal.displace.calculateOffset('top'));
      $('body').css({
        'padding-top': this.model.get('height')
      });

has the same effect as the code in the patch and is broken in the same way with off-canvas.

nod_ credited gokulnk.

nod_’s picture

Seems like the issue was caught before we changed the code to remove the flicker.

droplet’s picture

MULTIPLE LINES is illegal usage IMO. To me, I'd say many items also illegal usage! (If you do, MAX of 7 I think, which can fit into a small 375px screen)

If the CORE accepts multiple lines:
- It should be scrollable or extra dropdown (also Bad UX)
- You should consider what happens when the items covered half or 90% of the screens, or even 100% :s

In another word, if you used a lot of items and can't fit into a single line, make it responsive (to icons, even on a wider screen)

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.

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.

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.

andrerb’s picture

Issue tags: -JavaScript +JavaScript
StatusFileSize
new15.16 KB
new17.23 KB
new21.29 KB

Still a problem with Drupal 10.1.2 and Seven 1.0.0 (contrib)

Before:
before

After:
after

re-rolled patch

gauravvvv’s picture

Status: Needs work » Needs review
StatusFileSize
new15.31 KB

Tried fixing CCF in #55, 55 not applied on D11, so not attaching the interdiff.

gauravvvv’s picture

StatusFileSize
new15.35 KB
new764 bytes

Fixed CCF failure

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new5.04 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

kostyashupenko’s picture

Status: Needs work » Needs review
StatusFileSize
new15.47 KB
new692 bytes

Trying to fix errors reported by bot.

smustgrave’s picture

Status: Needs review » Needs work

Seems to have caused nightwatch failures.

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

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

mstrelan’s picture

Converted 59 to an MR, hiding patches.

mstrelan’s picture

Not just nightwatch that's failing now:

Functional JavaScript:

  • Drupal\Tests\settings_tray\FunctionalJavascript\SettingsTrayBlockFormTest::testBlocks
  • Drupal\Tests\layout_builder\FunctionalJavascript\LayoutBuilderToolbarTest::testBackToSiteLink
  • Drupal\Tests\contextual\FunctionalJavascript\ContextualLinksTest::testContextualLinksClick
  • Drupal\Tests\toolbar\FunctionalJavascript\ToolbarHeightCalculationTest::testToolbarHeightCalculation

Nightwatch:

  • Check toolbar overlap with page content
rohit sankhla’s picture

I was able to fix this issue by adding one line of css.

#toolbar-item-administration-tray.toolbar-tray-horizontal {
  height: 40px;
}

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

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

twod’s picture

Status: Needs work » Needs review

Summary of my changes:

  • Merged in the main branch. (Removed two files which were deleted by main, the MR just added a declare line to them anyway.)
  • Added px units to the body paddings, it would not work correctly without it.
  • Added an explicit top style to the vertical toolbar tray so it follows --drupal-displace-offset-top when displacement is recalculated.
  • Increased the z-index of the horizontal tray so that it renders on top of any extra wrapped toolbar rows. Otherwise clicks would got to the wrapped toolbar tabs instead of the tray.
  • Added a resize observer to recalculate the toolbar height (and body padding) when the main toolbar element changes its size, fixes issues with resizing making the toolbar and tray overlapping or leave big gaps. Added a small debounce to this, which makes it less smooth, but may help lower-end devices.
  • Fixed various linting errors.
  • Fixed toolbarApiTest.js - Not sure why the size increased to 80px, maybe rounding error?
  • Fixed AssetAggregationAcrossPagesTest.php - Looks like the changed scripts were part of the asset aggregation size test and the change was large enough to fail it.

Some test runs also failed CKEditor5MarkupTest.php, but that test seems really unreliable even main and it looks like a race condition. Running it multiple times locally makes different parts of it fail. If I insert a sleep(1) on line 346 before it checks if the response contains the expected scripts it will always pass. I did not include that change here though as it seemed too unrelated.

The last run failed testEditorFileReferenceIntegration::testEditorFileReferenceIntegration(), again it is also failing randomly for me on main.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new6.84 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.