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

ENH: Simplify theme variable code and support Sphinx Design #746

Merged
merged 18 commits into from
Jun 23, 2022

Conversation

drammock
Copy link
Collaborator

closes #733

@12rambau note that even though these override rules ought to be "more specific" because they're nested inside html data themes, I still needed to add !important to get them to actually override the sphinx-design rules.

LMK if anything looks weird / anything got missed.

Copy link
Collaborator

@choldgraf choldgraf left a comment

Choose a reason for hiding this comment

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

This looks good in general! I have a quick comments in there about how to structure the implementation etc, but the end-result looks very nice!


html[data-theme="light"],
html[data-theme="dark"] {
@each $name in $sd-semantic-color-names {
Copy link
Collaborator

Choose a reason for hiding this comment

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

the SCSS magic is strong with you ✨

Can you add a link to the SCSS docs that explain how @each works? I think many developers won't be familiar with it so it will be helpful to have breadcrumbs to help them learn quickly if they hit this code

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can you add a link to the SCSS docs that explain how @each works?

done

--pst-color-#{$name}-text: #{text-color($value)};
--pst-color-#{$name}-highlight: #{mix(#000, $value, 15%)};
}
/* ↓↓↓ these are not sphinx-design overrides ↓↓↓ */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I am worried about intermixing "special casing" CSS with "default CSS" in one place, I feel like this could be confusing given that Sphinx Design is not bundled with the theme.

Is there a way that we can cleanly separate the two so that one section is entirely "just this theme" and another section is clearly "sphinx design", or even better if we can put the sphinx design stuff just in the extensions/ folder?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am worried about intermixing "special casing" CSS with "default CSS"

I tried and failed to keep them separate yesterday. Today I did better; this is now fixed.

--pst-color-text-base: rgb(201, 209, 217);
--pst-color-text-muted: rgb(192, 192, 192);
--pst-color-border: rgb(192, 192, 192);
$pst-semantic-colors: (
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we somehow make this a dictionary-like object at the top of the file, with [light] and [dark] keys? Then we can access the colors via those keys in our SCSS logic below? Might make it easier to keep everything in one place.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could we somehow make this a dictionary-like object at the top of the file, with [light] and [dark] keys?

done

* main colors
* main colors. the $pst-semantic-colors follow the naming convention in
* sphinx-design, to allow for color overrides in
* ../extensions/_sphinx_design.scss. NOTE: some of these colors (most notably
Copy link
Collaborator

Choose a reason for hiding this comment

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

See my other point about intermixing sphinx-design and theme-specific colors, but if we're going to do it this way, can we be remove the uncertainty here? E.g., not "some of these colors", but instead list the specific colors this statement applies to. And remove the "probably", we should be precise and explicit because others without context will have a hard time reasoning what is a sphinx-design thing and what's a theme thing, as well as the implications of using those variables in the theme in general.

Sorry for being a stickler on this, but we recently spent a lot of effort trying to reduce our surface area of variables that are exposed to devs and to users, so just trying to make sure we stick with those goals :-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

can we remove the uncertainty here?

This point is now moot; all colors needed for sphinx-design overrides are now only in extensions/_sphinx_design.scss

Copy link
Collaborator Author

@drammock drammock left a comment

Choose a reason for hiding this comment

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

I think I've addressed all your comments @choldgraf, and the local nox -s docs-live looks good to me. See what you think!

--pst-color-text-base: rgb(201, 209, 217);
--pst-color-text-muted: rgb(192, 192, 192);
--pst-color-border: rgb(192, 192, 192);
$pst-semantic-colors: (
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could we somehow make this a dictionary-like object at the top of the file, with [light] and [dark] keys?

done

--pst-color-#{$name}-text: #{text-color($value)};
--pst-color-#{$name}-highlight: #{mix(#000, $value, 15%)};
}
/* ↓↓↓ these are not sphinx-design overrides ↓↓↓ */
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am worried about intermixing "special casing" CSS with "default CSS"

I tried and failed to keep them separate yesterday. Today I did better; this is now fixed.

* main colors
* main colors. the $pst-semantic-colors follow the naming convention in
* sphinx-design, to allow for color overrides in
* ../extensions/_sphinx_design.scss. NOTE: some of these colors (most notably
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

can we remove the uncertainty here?

This point is now moot; all colors needed for sphinx-design overrides are now only in extensions/_sphinx_design.scss


html[data-theme="light"],
html[data-theme="dark"] {
@each $name in $sd-semantic-color-names {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can you add a link to the SCSS docs that explain how @each works?

done

@drammock
Copy link
Collaborator Author

The failing test is "no unexpected sphinx warnings", and it's complaining because sphinx/pygments can't lex the SCSS file. This is apparently a known bug

I pasted the content of _colors.scss into https://pygments.org/demo/?lexer=scss and indeed it is choking on the $ characters (but only in certain expressions)

@drammock
Copy link
Collaborator Author

One easy solution is to not embed the entire _color.scss file in the documentation, and instead simply point users to the file using the GitHub URL. @12rambau @choldgraf WDYT?

@drammock
Copy link
Collaborator Author

here are the relevant sections from the RTD build:

light:
light

dark:
dark

Copy link
Collaborator

@choldgraf choldgraf left a comment

Choose a reason for hiding this comment

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

I had no idea that SCSS could do all of this stuff, it is basically like python! :-)

The implementation looks good to me (a few quick comments in there). The tests seem to be failing because Sphinx doesn't know how to parse the SCSS, I think in the docs here:

https://github.com/pydata/pydata-sphinx-theme/blob/main/docs/user_guide/customizing.rst#color-variables

Those docs are actually probably out of date now anyway, because the color variables are structured differently than they used to be...can you think of a way to have the same effect with this new structure? Doesn't need to be the exact same, but should be useful at helping others understand the variable structure and how to change them.

Here's the rendered page:

https://pydata-sphinx-theme.readthedocs.io/en/latest/user_guide/customizing.html#color-variables

@choldgraf choldgraf changed the title color overrides for sphinx-design ENH: Color variables for Sphinx Design and unify theming infrastructure Jun 22, 2022
@choldgraf
Copy link
Collaborator

oops just saw your extra comments - I'd be +1 on:

  • Just pointing users to the file instead of embedding it
  • Trying to add whatever useful contextual information we can think of so others know what variables they can/cannot modify themselves
  • Add a comment about the SCSS bug and link to the issue you shared above, saying this is why we're not including the colors file

@drammock
Copy link
Collaborator Author

Just pointing users to the file instead of embedding it

done

Trying to add whatever useful contextual information we can think of so others know what variables they can/cannot modify themselves

I think this is pretty clear now because we point them to _color.scss and that has all and only the variables they might want to mess with. Everything else is hidden in extensions/_sphinx_design.scss

Add a comment about the SCSS bug and link to the issue you shared above, saying this is why we're not including the colors file

done.

@choldgraf
Copy link
Collaborator

Ahh I bet what's going on with sphinx design is that our variables are defined first, and later the sphinx design variables are defined, effectively overriding them.

Can we think of a quick way to make our variable definitions load after all the extensions, but before the user supplied custom CSS file? If that'd be complicated i agree we should just go with the current implementation

@drammock
Copy link
Collaborator Author

Ahh I bet what's going on with sphinx design is that our variables are defined first, and later the sphinx design variables are defined, effectively overriding them.

I'm not certain but I think it's the opposite: sphinx-design's style sheet is evaluated first, in a context in which our re-definition of their variables hasn't happened yet, and the value of those variables gets "frozen" into the rules. I think this SO answer backs that up, though the use case is a little different: https://stackoverflow.com/a/52015817

it's a really long answer, so I'll excerpt here two things that sum it up:

The 3 variables (--r, --g, --b) are evaluated inside :root so we have already substituted them with their values. [...] Considering this, we should always keep the evaluation at the lowest possible point in the DOM tree and especially after the variable changes (or at the same level)

If that's applicable here, I think there is actually no way to override CSS variables in file A with new values in file B and have that change affect rules defined in file A. You just have to rewrite those rules (or provide different rules with higher specificity).

I would love to be wrong about this, but I'm really bumping up against the limits of my understanding of the CSS cascade. Maybe someone else will recognize what I've done as an anti-pattern an fix it in a later PR :)

@drammock
Copy link
Collaborator Author

...aaand, I'm totally wrong. Pushing a fix that eliminates the rule-rewriting, and everything seems to still look correct. 🤷🏻

@drammock
Copy link
Collaborator Author

rendered docs: https://pydata-sphinx-theme--746.org.readthedocs.build/en/746/demo/kitchen-sink/web-components.html?highlight=web%20components#badges-and-buttons

CI failure looks like a fluke:

/opt/hostedtoolcache/Python/3.10.5/x64/bin/pip cache dir
/home/runner/.cache/pip
Error: getCacheEntry failed: connect ETIMEDOUT 13.107.43.16:443

can someone with permissions re-run that one job?

@drammock
Copy link
Collaborator Author

ok, all green except for the macOS job which was cancelled (?) so I think this one is ready!

Copy link
Collaborator

@12rambau 12rambau left a comment

Choose a reason for hiding this comment

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

Thanks @drammock for this PR!

I didn't had the time to check it but that was nice reading your exchange. Appart from the few comments I made I'm very satisfied with this final version. it works as expected and ensure that the theme remains a nice neighbour.

@choldgraf I started to play with sass in the PR on sidebar display and I think there's a lot of refactoring to make in our files (but that will be for another PR)

docs/demo/kitchen-sink/web-components.rst Show resolved Hide resolved
docs/demo/kitchen-sink/web-components.rst Outdated Show resolved Hide resolved
@drammock
Copy link
Collaborator Author

@12rambau I think I've addressed your comments. It helped catch something I had missed, which was that previously downstream users overriding --pst-color-whatever would not have affected badges and buttons! Now it will, though at the cost of creating more CSS variables in our --pst-color-* namespace. However, those extra variables are only appearing in the extensions/_sphinx_design.scss file, so theme contributors will probably not even see them in most cases. Hopefully that's good enough?

Here's the doc section, still looking good:

https://pydata-sphinx-theme--746.org.readthedocs.build/en/746/demo/kitchen-sink/web-components.html#badges-and-buttons

and CIs are all green.

Copy link
Collaborator

@12rambau 12rambau left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@choldgraf choldgraf left a comment

Choose a reason for hiding this comment

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

This looks good to me as well, I really like the conciseness of the SCSS rules, many thanks for stepping through all this work @drammock 🚀

@choldgraf choldgraf changed the title ENH: Color variables for Sphinx Design and unify theming infrastructure ENH: Simplify theme variable code and support Sphinx Design Jun 23, 2022
@choldgraf choldgraf merged commit 7829e4a into pydata:main Jun 23, 2022
@drammock drammock deleted the sd-color-overrides branch June 23, 2022 10:07
@jarrodmillman jarrodmillman added this to the 0.10 milestone Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

override more sphinx-design colors
4 participants