Skip to content
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

Add support for CSS variables using the PostCSS CSS Variables package (issue 11462) #11567

Merged
merged 2 commits into from
Feb 6, 2020

Conversation

Snuffleupagus
Copy link
Collaborator

@Snuffleupagus Snuffleupagus commented Feb 4, 2020

Having thought briefly about using css-vars-ponyfill, I'm no longer convinced that it'd be a good idea. The reason is that if we actually want to properly support CSS variables, then that functionality should be available in all of our CSS files.
Note in particular the pdf_viewer.css file that's built as part of the COMPONENTS target, in which case I really cannot see how a rewrite-at-the-client solution would ever be guaranteed to always work correctly and without accidentally touching other CSS in the surrounding application.

All-in-all, simply re-writing the CSS variables at build-time seems much easier and is thus the approach taken in this patch; courtesy of /~https://github.com/MadLittleMods/postcss-css-variables
By using its preserve option, the built files will thus include both a fallback and a modern var(...) format[1]. As a proof-of-concept this patch removes a couple of manually added fallback values, and converts an additional sidebar related property to use a CSS variable.

Fixes #11462


[1] Comparing the master branch with this patch, when using gulp generic, produces the following diff for the built web/viewer.css file:

@@ -408,6 +408,7 @@

 :root {
   --sidebar-width: 200px;
+  --sidebar-transition-duration: 200ms;
 }

 * {
@@ -550,27 +551,28 @@
   position: absolute;
   top: 32px;
   bottom: 0;
-  width: 200px; /* Here, and elsewhere below, keep the constant value for compatibility
-                   with older browsers that lack support for CSS variables. */
+  width: 200px;
   width: var(--sidebar-width);
   visibility: hidden;
   z-index: 100;
   border-top: 1px solid rgba(51, 51, 51, 1);
   -webkit-transition-duration: 200ms;
           transition-duration: 200ms;
+  -webkit-transition-duration: var(--sidebar-transition-duration);
+          transition-duration: var(--sidebar-transition-duration);
   -webkit-transition-timing-function: ease;
           transition-timing-function: ease;
 }
 html[dir='ltr'] #sidebarContainer {
   -webkit-transition-property: left;
   transition-property: left;
-  left: -200px;
+  left: calc(-1 * 200px);
   left: calc(-1 * var(--sidebar-width));
 }
 html[dir='rtl'] #sidebarContainer {
   -webkit-transition-property: right;
   transition-property: right;
-  right: -200px;
+  right: calc(-1 * 200px);
   right: calc(-1 * var(--sidebar-width));
 }

@@ -640,6 +642,8 @@
 #viewerContainer:not(.pdfPresentationMode) {
   -webkit-transition-duration: 200ms;
           transition-duration: 200ms;
+  -webkit-transition-duration: var(--sidebar-transition-duration);
+          transition-duration: var(--sidebar-transition-duration);
   -webkit-transition-timing-function: ease;
           transition-timing-function: ease;
 }

@pdfjsbot
Copy link

pdfjsbot commented Feb 4, 2020

From: Bot.io (Linux m4)


Received

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

Live output at: http://54.67.70.0:8877/f5070d8c5daf49d/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Feb 4, 2020

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/f5070d8c5daf49d/output.txt

Total script time: 1.77 mins

Published

@Snuffleupagus
Copy link
Collaborator Author

Sign, it seems that IE11 cannot handle calc(-1 * 200px); correctly or specifically the -1 part here...

We could possibly use another PostCSS package to solve that, but then again maybe not since I just found postcss/postcss-calc#87 :-(

Snuffleupagus added a commit to Snuffleupagus/pdf.js that referenced this pull request Feb 5, 2020
… element to enable IE11 compatibility

As gross as this hack is, it nonetheless seem necessary to allow using CSS variables; see also mozilla#11567 (comment)
@pdfjsbot
Copy link

pdfjsbot commented Feb 5, 2020

From: Bot.io (Linux m4)


Received

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

Live output at: http://54.67.70.0:8877/c176f1ec14d0963/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Feb 5, 2020

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/c176f1ec14d0963/output.txt

Total script time: 1.77 mins

Published

Snuffleupagus added a commit to Snuffleupagus/pdf.js that referenced this pull request Feb 5, 2020
… element to enable IE11 compatibility

As gross as this hack is, it nonetheless seem necessary to allow using CSS variables; see also mozilla#11567 (comment)
@pdfjsbot
Copy link

pdfjsbot commented Feb 5, 2020

From: Bot.io (Linux m4)


Received

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

Live output at: http://54.67.70.0:8877/ab9cdcfbd5d2c0b/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Feb 5, 2020

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/ab9cdcfbd5d2c0b/output.txt

Total script time: 1.68 mins

Published

@Snuffleupagus Snuffleupagus marked this pull request as ready for review February 5, 2020 08:48
…ge (issue 11462)

Having thought *briefly* about using `css-vars-ponyfill`, I'm no longer convinced that it'd be a good idea. The reason is that if we actually want to properly support CSS variables, then that functionality should be available in *all* of our CSS files.
Note in particular the `pdf_viewer.css` file that's built as part of the `COMPONENTS` target, in which case I really cannot see how a rewrite-at-the-client solution would ever be guaranteed to always work correctly and without accidentally touching other CSS in the surrounding application.

All-in-all, simply re-writing the CSS variables at build-time seems much easier and is thus the approach taken in this patch; courtesy of /~https://github.com/MadLittleMods/postcss-css-variables
By using its `preserve` option, the built files will thus include *both* a fallback and a modern `var(...)` format[1]. As a proof-of-concept this patch removes a couple of manually added fallback values, and converts an additional sidebar related property to use a CSS variable.

---
[1] Comparing the `master` branch with this patch, when using `gulp generic`, produces the following diff for the built `web/viewer.css` file:
```diff
@@ -408,6 +408,7 @@

 :root {
   --sidebar-width: 200px;
+  --sidebar-transition-duration: 200ms;
 }

 * {
@@ -550,27 +551,28 @@
   position: absolute;
   top: 32px;
   bottom: 0;
-  width: 200px; /* Here, and elsewhere below, keep the constant value for compatibility
-                   with older browsers that lack support for CSS variables. */
+  width: 200px;
   width: var(--sidebar-width);
   visibility: hidden;
   z-index: 100;
   border-top: 1px solid rgba(51, 51, 51, 1);
   -webkit-transition-duration: 200ms;
           transition-duration: 200ms;
+  -webkit-transition-duration: var(--sidebar-transition-duration);
+          transition-duration: var(--sidebar-transition-duration);
   -webkit-transition-timing-function: ease;
           transition-timing-function: ease;
 }
 html[dir='ltr'] #sidebarContainer {
   -webkit-transition-property: left;
   transition-property: left;
-  left: -200px;
+  left: calc(-1 * 200px);
   left: calc(-1 * var(--sidebar-width));
 }
 html[dir='rtl'] #sidebarContainer {
   -webkit-transition-property: right;
   transition-property: right;
-  right: -200px;
+  right: calc(-1 * 200px);
   right: calc(-1 * var(--sidebar-width));
 }

@@ -640,6 +642,8 @@
 #viewerContainer:not(.pdfPresentationMode) {
   -webkit-transition-duration: 200ms;
           transition-duration: 200ms;
+  -webkit-transition-duration: var(--sidebar-transition-duration);
+          transition-duration: var(--sidebar-transition-duration);
   -webkit-transition-timing-function: ease;
           transition-timing-function: ease;
 }
```
… element to enable IE11 compatibility

As gross as this hack is, it nonetheless seem necessary to allow using CSS variables; see also mozilla#11567 (comment)
@pdfjsbot
Copy link

pdfjsbot commented Feb 5, 2020

From: Bot.io (Linux m4)


Received

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

Live output at: http://54.67.70.0:8877/7e46a92fbb7f6fb/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Feb 5, 2020

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/7e46a92fbb7f6fb/output.txt

Total script time: 1.70 mins

Published

@Snuffleupagus
Copy link
Collaborator Author

This seems to work now, even in IE11, without anything being obviously broken :-)

Once this's landed we should thus be free to use CSS variables to simplify/re-factor the existing rules (and obviously when adding new rules), however I don't intend to convert more rules in this PR since the changes are mostly for proof-of-concept reasons and to allow validation of the overall approach.

@timvandermeij timvandermeij merged commit a5fec29 into mozilla:master Feb 6, 2020
@timvandermeij
Copy link
Contributor

Looks good! I'm glad that we can start using CSS variables properly now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use CSS variables in viewer.css
3 participants