This is split off from #3404214: META: Reduce / eliminate “jank” (layout shifts) within Drupal’s admin UI but originates as a discussion on #3441124: Views UI action buttons create janky layout shift on page load where @nod_ suggested trying to abstract out the method of reducing jank into something more reusable (such as a CSS class). We've also had discussions in Slack at https://drupal.slack.com/archives/C1BMUQ9U6/p1714158882541689

I'm not entirely sure this is possible or worth it, as the cause of the jank issues are many. But, there may be opportunity here if the same cause affects multiple places.

I'm leaving this here as a placeholder to see if once we get some of these fixes into core, patterns can emerge and we can re-evaluate.

Comments

mherchel created an issue. See original summary.

nod_’s picture

Maybe for the js-hide class we could use the media query to make sure we hide things asap. Would be good to try it out see if that helps some UI

mherchel’s picture

Maybe for the js-hide class we could use the media query to make sure we hide things asap.

We already got that committed in #3404218: Table filter creates jank (layout shift) on page load. See https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/syste...

saurav-drupal-dev’s picture

Here's a quick approach to tackle page jank:
1. CSS Fixes:
Reserve Space for Content: Use min-width, min-height, or aspect-ratio for images and iframes to prevent layout shifts.

img, iframe {
  min-width: 100px;
  min-height: 100px;
  aspect-ratio: 16/9;
}


Font Loading: Use font-display: swap to avoid layout shifts caused by late-loading fonts.
@font-face {
  font-display: swap;
}

Smooth Animations: Apply fade-in animations for dynamically loaded content to avoid sudden jumps.

.dynamic-content {
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
}
.dynamic-content.loaded {
  opacity: 1;
}

2. JS Fix:
Add a loaded class to dynamically added elements after the page loads to trigger smooth transitions.

document.addEventListener('DOMContentLoaded', () => {
  document.querySelectorAll('.dynamic-content').forEach(el => el.classList.add('loaded'));
});

3. Drupal Setup:
Add the CSS/JS to a custom module or theme to apply across pages.

4. Testing:
Monitor metrics like Cumulative Layout Shift (CLS) to ensure improvements.
Let me know what you think!

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.