Dropbutton quickly shows/hides its menu on pageload causing layout shift

Created on 18 May 2023, about 1 year ago
Updated 26 May 2023, about 1 year ago

The dropbutton component shows its menu when JavaScript is disabled, but then quickly hides it when JS loads.

This results in a layout shift of the page, which provides a suboptimal user experience. IMHO, it makes it seem fairly janky.

This is easily viewable on the /admin/content page:

Google also highly prioritizes elimination of layout shifts, and includes a "Cumulative Layout Shift" metric within its Core Web Vitals. See

What can be done

We want to still support non-JS admin users (at least for the time being).

We fix this issue by adding a <noscript> tag to the document head, and then adding a <link> tag pointing to non-JS dropbutton styles that we migrate to a separate CSS file.

🐛 Bug report
Status

Fixed

Version

10.1

Component
Claro 

Last updated about 9 hours ago

Created by

🇺🇸United States mherchel Gainesville, FL, US

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Issue created by @mherchel
  • 🇺🇸United States mherchel Gainesville, FL, US
  • 🇺🇸United States mherchel Gainesville, FL, US

    We fixed a similar issue with the mobile-friendly menus within Olivero by adding a noscript_styles variable to the html.html.twig.

    This patch follows the same pattern.

    That being said, it would be nice if libraries or themes/modules had an easier way to add noscript styles to a page. Maybe something like this in a *.libraries.yml file could be a followup:

    drupal.dropbutton:
      css:
        component:
          misc/dropbutton/dropbutton.css: { noscript: true }
    
  • Status changed to Needs review about 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    Custom Commands Failed
  • 🇺🇸United States mherchel Gainesville, FL, US
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    29,388 pass
  • 🇺🇸United States mherchel Gainesville, FL, US
  • Status changed to RTBC about 1 year ago
  • 🇺🇸United States smustgrave

    Was able to confirm the shift as seen in the gif.

    Applied the patch and no longer seeing it.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    29,388 pass
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    29,388 pass
  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States bnjmnm Ann Arbor, MI

    I've been wanting this addressed for a long time! Pretty minor stuff

    1. +++ b/core/themes/claro/claro.theme
      @@ -39,6 +39,28 @@ function claro_theme_suggestions_details_alter(&$suggestions, $variables) {
      +function claro_preprocess_html(&$variables) {
      +  $theme_path = \Drupal::request()->getBasePath() . '/' . \Drupal::service('extension.list.theme')->getPath('claro');
      +  $query_string = \Drupal::state()->get('system.css_js_query_string') ?: '0';
      

      If this goes in a page_attachments_alter there isn't the need to change html.html.twig, and its leveraging an existing API to add this

       function claro_page_attachments_alter(array &$attachments) {
        $theme_path = \Drupal::request()->getBasePath() . '/' . \Drupal::service('extension.list.theme')->getPath('claro');
        $query_string = \Drupal::state()->get('system.css_js_query_string') ?: '0';
      
        $attachments['#attached']['html_head'][] = [
          [
            '#tag' => 'link',
            '#noscript' => TRUE,
            '#attributes' => [
              'rel' => 'stylesheet',
              'href' => $theme_path . '/css/components/dropbutton-noscript.css?' . $query_string,
            ],
          ],
          'dropbutton_noscript',
        ];
      }

      It's possible this was tried and ruled out in Olivero but couldn't quickly find the relevant issue as it was implemented when Olivero was contrib. If the reasoning is already in the issue and there's a good reason to use the existing approach, link to the issue with the explanation, address item 2 (which is only needed if you keep preprocess_html) and fine to re-rtbc.

    2. +++ b/core/themes/claro/templates/classy/layout/html.html.twig
      @@ -38,6 +38,7 @@
      +    {{ noscript_styles }}
      

      If my attachments_alter suggestion isn't implemented,and this change still happens, then this template file should be moved to a new directory. Any theme that used to have (the now removed) Classy as a subtheme had the classy templates it used copied to a /classy subdirectory to help distinguish formerly-classy from templates specific to the theme. Once we change it, it's effectively overridden and belongs to Claro, etc.

      This isn't particularly apparent in Drupal 10 as the test coverage that would let you know the move was needed got removed with Classy

  • Status changed to Needs review about 1 year ago
  • 43:53
    38:19
    Running
  • 🇺🇸United States mherchel Gainesville, FL, US

    To be honest, I'm not sure why we implemented it the way we did in Olivero. I think it was a situation where the code morphed a bunch and then it ended up there (which works well). That being said, I like this solution better because it doesn't need to modify the HTML template.

    New patch and interdiff attached. Thank you :)

  • Status changed to RTBC about 1 year ago
  • 🇪🇸Spain ckrina Barcelona

    I just tested this and it work perfect. Also, since @bnjmnm's feedback has been addressed, I'm moving this back to RTBC.

    • lauriii committed c5604c9d on 11.x
      Issue #3361315 by mherchel, smustgrave, bnjmnm, ckrina: Dropbutton...
    • lauriii committed fa2193ff on 10.1.x
      Issue #3361315 by mherchel, smustgrave, bnjmnm, ckrina: Dropbutton...
  • Status changed to Fixed about 1 year ago
  • 🇫🇮Finland lauriii Finland

    Woah! I've wanted to work on this so many times since we first implemented the Claro dropbuttons. Thank you @mherchel & all! 💯

    Committed c5604c9 and pushed to 11.x. Also cherry-picked to 10.1.x. Thanks! 🎉

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

Production build 0.69.0 2024