WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.34k stars 4.13k forks source link

The CSS class `is-dark-theme` is not set in the post editor #60299

Open afercia opened 5 months ago

afercia commented 5 months ago

Description

Splitting this out from https://github.com/WordPress/gutenberg/issues/60275

To be more accurate, in the Post editor the CSS class is-dark-theme is initially applied but than removed. In the Site editor, it works as expected.

It seems to be something missed with the refactorings of the editor irame, editor styles scoping and such.

When a theme has a dark background color, useDarkThemeBodyClassName is responsible for adding the is-dark-theme CSS class to the editor iframe body element.

It does that, but then the body gets updated and gets new CSS classes when the iframe is fully loaed, see the Iframe component internal handling of the classes in the onload callback and then the classes are set to the body.

I'm not sure whether that means the body gets entirely replaced. Regardless the new CSS classes replace the initial ones thus removing is-dark-theme it it was set.

The is-dark-theme CSS class is necessaty to make some editor features and styles work correctly.

Step-by-step reproduction instructions

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

afercia commented 5 months ago

The commit to fix this issue was reverted to fix a problem with Safari, see https://github.com/WordPress/gutenberg/pull/60616.

I'm reopening this issue as it needs to be solved, but with a different approach.

ellatrix commented 5 months ago

Not sure if that would break anything, but maybe the best wait to fix the issue is to move is-dark-theme to the html element, which is not managed in React?

afercia commented 5 months ago

Based on this comment, looks like the problem with Safari is broader because also the CSS classes admin-color-{$color_scheme} and wp-embed-responsive seems to not be set correctly in Safari. However, I can't reproduce that issue on trunk.

Regarding the bug reported at https://github.com/WordPress/gutenberg/pull/60550 I'm not sure the root cause was the change in https://github.com/WordPress/gutenberg/pull/60300 as that change followed an already existing pattern used for the admin-color-{$color_scheme} and wp-embed-responsive classes.

Anyways, to illustrate a couple of the issues triggered by the missing is-dark-theme CSS classe, please see the screenshot below. It's just two examples. there are probably more issues.

A block in 'select mode' now shows a blue outline (left) but it is supposed to show a white outline (dark):

Screenshot 2024-04-11 at 15 57 25

In addition to the outline, the Spacer block, when selected, shows a full dark background (left) but it is supposed to show a lighter background (right):

Screenshot 2024-04-11 at 16 58 41

ellatrix commented 5 months ago

My suggestion would be to move the class to the html element so that it's not overridden later on

afercia commented 5 months ago

My suggestion would be to move the class to the html element so that it's not overridden later on

I've been testing pretty exensively. While moving the class to the thml element seems trivial, it doesn't work well anyways with themes that may handle the body background dynamically. For example, Twenty Twenty-One does have its won 'dark mode toggle' feature that changes the body background after the editor useDarkThemeBodyClassName checks the background color luminance.

That's a pre-existing issue and I suspect it never worked well. However, it seems there's a need to a better mechanism to get the actual body background color even when it gets changed by themes or plugins features.

afercia commented 2 days ago

A first attempt to fix this issue was reverted in https://github.com/WordPress/gutenberg/pull/60616. I can still reproduce the incorrect behavior in the Post editor. @glendaviesnz @ellatrix any change to explore an alternative approach to fix this issue> RIght now, the implementation of the is-dark-theme class in the Post editor is just broken.