Media Library Dialog Opens Off-Center

Created on 20 April 2022, about 2 years ago
Updated 6 October 2023, 8 months ago

Problem/Motivation

Under the right conditions, the "Insert from Media Library" button in the WYSIWYG editor causes the media library dialog to open off-center. This is jarring at first for the user, but then it re-centers itself if the user scrolls the page or selects a different tab in the media library viewer.

Steps to reproduce

  1. Go to Home | Simplytest
  2. Click the “Umami Demo” button
  3. Wait for the environment to generate
  4. Click the log in link in the top right (/user/login)
  5. Log into the site (admin/admin)
  6. Create a new custom block
    • Admin > Structure > Block layout > Custom block library > “Add custom block” > Basic block (/en/block/add/basic)
  7. In the WYSIWYG editor, click the “Insert from Media Library” button
    • Observe the positioning of the modal (expect it to be centered, but this issue positions it off-center toward the right of the screen)
  8. If this did not produce the issue, follow the remaining steps to attempt to reproduce it.
  9. Disable JS aggregation by navigating to Admin > Config > Development > Performance (/en/admin/config/development/performance)
  10. Uncheck the box next to “Aggregate JavaScript files”
  11. Save the form
  12. Navigate back to the block creation screen (or refresh if you kept this open in a different tab) (/en/block/add/basic)
  13. In the WYSIWYG editor, click the “Insert from Media Library” button
  14. Observe the positioning of the modal (expect it to be centered, but this issue positions it off-center toward the right of the screen)

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Media 

Last updated 1 day ago

Created by

🇺🇸United States slucero Arkansas

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.

  • 🇧🇪Belgium flyke

    I have a custom module which opens a dialog which among other things contains an image.
    The dialog is always opened off center like in the screenshot of this issue.
    But when any scroll happens, then it gets perfectly centered.
    It took me a long time to figure out the cause and a solution. I have looked at this issue, at Gin theme issue #3332854 🐛 Dialog not always positioned correctly Fixed , at Gin theme issue #3219407 and other dialog related issues I can find but I've never found a solution there.

    Then I figured out the cause and a solution for my problem myself, so I figured I'd share it here for anyone having the same problem with off-center dialogs. It might just help you on your way to a solution too.

    As said, in my case, the dialog content has an image. Without the image the dialog is actually not off center. So what happens is that the dialog loads, has a certain width without the image, it is centered based on that with, but then after that the image finishes loading and makes the dialog larger towards the right side and bottom. So thats why its off-center: because of rendering image after positioning the dialog.
    Now that I found the cause, the easiest fix is to trigger a scroll event after the image finished loading.
    In my case I did this:
    window.scrollBy(0,1);
    This scrolls the window down vertically by 1 pixel. This then automatically triggers the repositioning of the dialog.
    In my case my image in the dialog was rendered by using cropperjs which fortunatly has a ready() event. So luckily all I had to do was:

        //Initialize cropper
        cropper = new Cropper(
          image, {
            ...
            ready(){
              // Scroll the window 1 px to trigger repositioning and centering the dialog box.
              window.scrollBy(0,1);
            }
        }
        );

    So since this issue here is aimed at the media library more specifically, you should find a way to trigger window scroll after all media library preview images in the dialog have been loaded.

Production build 0.69.0 2024