Skip to content

Use the light-dark CSS function in the viewer (issue 17780) #19819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025

Conversation

Snuffleupagus
Copy link
Collaborator

@Snuffleupagus Snuffleupagus commented Apr 16, 2025

This removes the need for (most) separate @media (prefers-color-scheme: dark) blocks when defining colors values, and also provides a simple way of forcing use of either the light or dark theme.

Please refer to https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/light-dark and https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme

NOTE: To support this in older browsers, we utilize a PostCSS plugin.


This patch reduces the size of the gulp mozcentral target by 5302 bytes.

@Snuffleupagus Snuffleupagus linked an issue Apr 16, 2025 that may be closed by this pull request
@Snuffleupagus Snuffleupagus force-pushed the CSS-light-dark branch 4 times, most recently from 89da7be to fa29abc Compare April 16, 2025 11:24
@Snuffleupagus Snuffleupagus marked this pull request as ready for review April 16, 2025 11:24
@Snuffleupagus
Copy link
Collaborator Author

/botio integrationtest

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.241.84.105:8877/0ec1e5b6873b486/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.193.163.58:8877/ff0cf034ced8020/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/0ec1e5b6873b486/output.txt

Total script time: 12.64 mins

  • Integration Tests: Passed

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/ff0cf034ced8020/output.txt

Total script time: 31.06 mins

  • Integration Tests: FAILED

@mozilla mozilla deleted a comment from moz-tools-bot Apr 18, 2025
@mozilla mozilla deleted a comment from moz-tools-bot Apr 18, 2025
@Snuffleupagus Snuffleupagus marked this pull request as draft April 18, 2025 12:46
Copy link
Contributor

@calixteman calixteman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you.

Copy link
Contributor

@calixteman calixteman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you.

@mozilla mozilla deleted a comment from moz-tools-bot Apr 22, 2025
@mozilla mozilla deleted a comment from moz-tools-bot Apr 22, 2025
@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_preview from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.241.84.105:8877/c44e3ab003f2fad/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/c44e3ab003f2fad/output.txt

Total script time: 0.86 mins

Published

@Snuffleupagus
Copy link
Collaborator Author

Snuffleupagus commented Apr 22, 2025

Unfortunately there's one remaining issue that I've not been able to understand, and much less fix. @calixteman Any ideas here?

The color-inputs, for the FreeText and Ink editors, are now affected by the global color-scheme setting which means that they look "wrong" and are initialized with an incorrect color (white) when the browser is set to dark mode.

I thought that adding a color-scheme: only light; override to the following block would fix that, but it doesn't seem to help.

pdf.js/web/viewer.css

Lines 1144 to 1149 in 213d51d

.editorParamsColor {
width: 32px;
height: 32px;
flex: none;
padding: 0;
}

Please see the following screen-shots, from the latest preview, that show the lack of effect from the override.

light

dark

@Snuffleupagus
Copy link
Collaborator Author

Oh, it seems that the initial color comes from

static get _defaultLineColor() {
return shadow(
this,
"_defaultLineColor",
this._colorManager.getHexCode("CanvasText")
);
}
and
function getColorValues(colors) {
const span = document.createElement("span");
span.style.visibility = "hidden";
document.body.append(span);
for (const name of colors.keys()) {
span.style.color = name;
const computedColor = window.getComputedStyle(span).color;
colors.set(name, getRGB(computedColor));
}
span.remove();
}
which (perhaps not unsurprisingly) is now affected by the color-scheme setting.

@calixteman Is the new behaviour perhaps then correct/expected here, or do we want to keep using a black default color and if so how best to implement that?

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_preview from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.241.84.105:8877/5f5382e3600a080/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/5f5382e3600a080/output.txt

Total script time: 0.87 mins

Published

@Snuffleupagus Snuffleupagus marked this pull request as ready for review April 22, 2025 16:41
@Snuffleupagus
Copy link
Collaborator Author

The latest version of the patch keeps the old behaviour for these input-color elements, by updating the getColorValues helper function slightly.
As far as I understand that function is primarily intended for forced-colors: active mode, and setting a preferred color-scheme there does not (in my testing) affect that mode.

Copy link
Contributor

@calixteman calixteman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The color of an added signature in dark mode is not correct:
https://github.com/mozilla/pdf.js/blob/master/src/display/editor/signature.js#L29-L46
We should probably use AnnotationEditor._defaultLineColor instead of CanvasText.

@Snuffleupagus Snuffleupagus force-pushed the CSS-light-dark branch 2 times, most recently from 3e48440 to b03cd96 Compare April 23, 2025 08:36
@Snuffleupagus
Copy link
Collaborator Author

Snuffleupagus commented Apr 23, 2025

The color of an added signature in dark mode is not correct:
https://github.com/mozilla/pdf.js/blob/master/src/display/editor/signature.js#L29-L46
We should probably use AnnotationEditor._defaultLineColor instead of CanvasText.

That seemed to work, thank you.

/botio-linux preview

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_preview from @Snuffleupagus received. Current queue size: 1

Live output at: http://54.241.84.105:8877/8c374cd3dea6e56/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/8c374cd3dea6e56/output.txt

Total script time: 0.94 mins

Published

@calixteman
Copy link
Contributor

Would it be possible to add an integration test in order to test that the color of an added signature is correct whatever the mode is ?

This removes the need for (most) separate `@media (prefers-color-scheme: dark)` blocks when defining colors values, and also provides a simple way of forcing use of either the light or dark theme.

Please refer to https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/light-dark and https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme

*NOTE:* To support this in older browsers, we utilize a [PostCSS plugin](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-light-dark-function).
Copy link
Contributor

@calixteman calixteman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you.

@Snuffleupagus
Copy link
Collaborator Author

Would it be possible to add an integration test in order to test that the color of an added signature is correct whatever the mode is ?

That should be possible, and I've done my best to add a couple of new tests for this (however I'm not great at writing integration-tests).

/botio integrationtest

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 1

Live output at: http://54.241.84.105:8877/acd26b2ea58cc34/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 1

Live output at: http://54.193.163.58:8877/77105792e03b5f4/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Failed

Full output at http://54.241.84.105:8877/acd26b2ea58cc34/output.txt

Total script time: 13.76 mins

  • Integration Tests: FAILED

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/77105792e03b5f4/output.txt

Total script time: 30.57 mins

  • Integration Tests: FAILED

@Snuffleupagus Snuffleupagus merged commit 01c1c6e into mozilla:master Apr 23, 2025
9 checks passed
@Snuffleupagus Snuffleupagus deleted the CSS-light-dark branch April 23, 2025 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use light-dark css function
3 participants