Active
Project:
Drupal core
Version:
main
Component:
media system
Priority:
Major
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
24 Apr 2019 at 15:11 UTC
Updated:
19 Aug 2022 at 16:48 UTC
Jump to comment: Most recent
The media library view looks correct when invoked from an admin page. But when brought up from the settings tray (via layout builder), it loads the front-end theme, and loses its custom styling.
I expected it to retain the admin theme's styling.

| Comment | File | Size | Author |
|---|---|---|---|
| Edit_layout_for_Demo_Landing_Page___NAMM_org.png | 144.05 KB | mherchel |
Comments
Comment #2
phenaproximaI think this might be a potential stable blocker for Media Library.
A few options were discussed in Slack. @mherchel said this:
#2 is, in my opinion, a non-starter; iframes interact with the browser in a fundamentally different way from a normal document, and Media Library is heavily reliant on document-bound JavaScript. Trying to sandbox it in an iframe for styling reasons would likely break a lot of our implementation assumptions and lead to a huge amount of refactoring, not to mention potential security and accessibility issues. So I vote that we avoid iframe usage like the plague.
I would greatly prefer #1, because it seems like the most attainable option. Our CSS selectors are currently fairly "meek" from a cascading point of view, in the sense that they mostly rely on class composition to achieve the desired look. We could likely fix a lot of the styling issues @mherchel uncovered here by targeting some of the CSS to an ID, which has very high precedence. This is a similar approach to what Settings Tray did; we wouldn't necessarily need a full reset, but we could probably take advantage of IDs to impose the styling opinions we feel most strongly about.
Comment #3
mherchelYeah, I agree w @phenaproxima. A reset would be easy to implement, and get 95% of the job done. Styles that end up leaking through can then be cleaned up by the themer.
I do want to say that it appears that the default styles from the library are also not being loaded. I removed the CSS files from my front-end theme, cleared cache, and then reloaded, and the view still was not styled.
Comment #4
phenaproximaI'm tagging this issue for tests of that specific problem. That sounds like a straight-up bug.
Comment #5
wim leersFYI: the same is true for the "link" and "image" dialogs (from the Text Editor module) when doing in-place editing of a body field using CKEditor.
That's because we haven't done the work to make dialogs in the front-end theme match the back-end theme.
IOW: this problem is not specific to Media, but with Media Library + Layout Builder in Drupal 8, it'll be encountered far more often.
Comment #6
mark_fullmerOur team has tested doing a JS-style inline CSS reset modeled after #2952390: Off-canvas styles override CKEditor's reset and theme for this. Though it does capture 95% of the styling, we're concerned about that last 5% where certain elements would either need to be massaged in a more idiosyncratic way. To give one example of this, the
detailsCSS fromcore/themes/seven/css/components/details.cssis needed, but this includes a.seven-detailsclass that wouldn't be present in active themes, so that CSS needs to be reworked (a couple ways to do that).A solution we're toying with that would faithfully render 100% of the admin theme is shown in a sandbox module at Media Library Theme Reset.
While this is unpleasantly duplicative of core CSS, it does provide a solution that would be plug-and-play for all themes thus avoiding the need for themers to do a round of cleanup.
Any thoughts on whether an approach like this could be added in the core Media Library?
Comment #7
xjmPromoting to major as per #2834729: [META] Roadmap to stabilize Media Library.
Comment #8
lauriiiComment #10
bkosborneThis is the issue that's blocking me from switching my sites to using media library for all media form widgets. We use layout builder heavily and are currently using the entity browser widget for selecting media items. It works well because the entity browser widget loads in an iframe, so it has complete "fencing" of the CSS.
Isn't the best solution here to render the media library dialog in an iframe? Even with the module Mark Fullmer contributed, the media library looks very bad/broken when used on our front end themes.
Comment #11
maskedjellybean+1 for using an iframe. Until this gets resolved I'm using Mark Fullmer's module as well, but even though it makes the Media Library usable it still looks pretty bad. You're never going to be able to predict all the styling that someone will put into their front-end theme that you will need to override.
Comment #12
mark_fullmerJust chiming in here to agree that https://drupal.org/project/media_library_theme_reset is an imperfect solution here; our team also has some front-end CSS that takes precedence over the reset, due to how it was implemented.
So, +1 to a solution such as an iFrame.
Comment #13
phenaproximaI'd like @seanB to confirm this, but to reiterate what I said in #2: I'm fairly sure that, due to the way the media library fundamentally works, it is impossible for us to have it load in an iframe. Its interactions with the AJAX system, and with the rest of the page, preclude that. I believe that moving it into an iframe would likely require a lot of deep rewrites in the media library module, which are no longer possible (it's a stable module, after all). Unless there's some contrib proof-of-concept demonstrating that the media library can be in an iframe without enormous pain, then I don't think that is a viable option.
Besides, that would sidestep the real issue, which is that admin-side stuff is often incompatible with frontend themes. It is a difficult, pre-existing problem in core which the media library exposes in an exacerbated fashion. Doing crazy hacks and interesting workarounds for the media library will fix a symptom, but not cure the disease.
Comment #14
bkosborneOkay, thanks for providing that explanation. Also, I apologize, I didn't read your first comment that explained why pursuing the iframe option was not practical. So then it seems like trying to apply a super aggressive CSS reset to the media library may be the only path forward.
Comment #15
phenaproximaNo apology necessary! I know how frustrating this problem can be, and I appreciate the feedback that proves how urgent of an issue this is. Ideally we'd fix it in a way that worked for other admin-facing frontend things in core (*cough*Layout Builder*cough*). So although a CSS reset isn't ideal, it might be the best we can do for the time being. It does look like a plan to address this core-wide is materializing over in #2195695: Admin UIs on the front-end are difficult to theme, though, so there's hope!
Comment #16
seanb+1 to the CSS reset (as a contrib solution) because of what phenaproxima already explained. Changing this to an iframe would require major changes in how the module works at the moment and provides it's own set of challenges.
I've struggled with the frontend theming myself as well. Some sites want the backend theme to be applied in layout builder, others theme the media library in the look and feel of the frontend (which is not as easy as I would like it to be). We should probably provide a documentation page to point people to the contrib module and provide some explanation on how to theme this in frontends as short term solutions. Fixing #2195695: Admin UIs on the front-end are difficult to theme is the real solution, but that will probably take a while unfortunately.
Comment #18
phenaproximaComment #19
mherchelI'm dealing with this within Olivero now in #3172957: Olivero: Media library within Layout Builder modal is unstyled.
Once again, I'm thinking that the styling should be part of core. I'm currently looking at the way that Claro/Gin style the library, and there's a significant amount of styling/templates that go into it.
Also note that the CSS reset just got easier with the addition of #3170864: Add postcss-preset-env. We can now do a
#Selector * { all: initial; }and it will compile down to the following:Comment #20
mglamanThere is another problem, beyond styling. If the frontend them replaces
input[type="submit"]withbutton[type="submit"], the AJAX filtering for Media Library breaks. See #2998908: Views exposed form Ajax selector doesn't work on buttons.Comment #24
jayhuskinsI'm curious if we could flip this issue on its head by rendering layout builder in the admin theme as this module does:
https://www.drupal.org/project/layout_builder_admin_theme
then inject the front-end styles and rendering for the block previews.
Comment #25
datawebHi there, i created a module that takes the Claro Media Library templates, hooks and preprocess functions to whatever theme.
It would be nice to review the module in the context of this issue.
https://www.drupal.org/project/claro_media_library_theme
Comment #26
mark_fullmerThis is an approach that does indeed work, if requiring a fair amount of CSS duplication and template file re-registering. It takes the same approach as the contrib module media_library_theme_reset, which has an open issue for supporting Claro: #3100124: Add support for Claro.
I am a maintainer of that module. Maybe instead of creating a separate module, we could collaborate and integrate the Claro theme option that you've architected into Media Library Theme Reset? I'd be happy to work on the integration with you, if that sounds good. Let me know!