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

Ensure proper and stable CSS insertion ordering #3678

Open
lex111 opened this issue Nov 3, 2020 · 19 comments
Open

Ensure proper and stable CSS insertion ordering #3678

lex111 opened this issue Nov 3, 2020 · 19 comments
Labels
bug An error in the Docusaurus core causing instability or issues with its execution
Milestone

Comments

@lex111
Copy link
Contributor

lex111 commented Nov 3, 2020

Edit: this issue is now about ensuring that we have a properly specified CSS insertion ordering with the ability to prevent regressions.

For Docusaurus 2.0 (#6113), CSS insertion ordering should become stable and documented. Upgrading Docusaurus should not lead to a different CSS ordering in particular for user-provided custom CSS.

See also #5987 (comment)


Original bug report below:

🐛 Bug Report

During a little refactoring, I realized that the custom CSS files content is not at the end of final CSS file styles.css (as it expected). This makes it impossible to override the Docusaurus component styles without applying the !important rule (which in turn is bad practice).

@slorber could you please investigate this issue? Can we "force" the custom CSS content to be at the end of all other styles?

By the example of Docusaurus website you can see that custom CSS is somewhere in the middle of bundled CSS file.

image

@lex111 lex111 added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Nov 3, 2020
@slorber
Copy link
Collaborator

slorber commented Nov 3, 2020

I don't find an easy way to do that.

We need client modules to be added in CSS first because Infima is a client module, and the theme css modules should be able to override Infima.

Looks like we need another system to be able to have 3 kind of css:

  • client modules (infima, admonition etc...)
  • theme imports (ie css modules etc...)
  • custom css

I'm not sure how we can make this but it's unlikely we can do that using the clientModules API, otherwise Infima would end up overriding theme css modules

@lex111
Copy link
Contributor Author

lex111 commented Nov 4, 2020

Maybe custom CSS files will include after main CSS file as separate link element? That is, we will exclude custom CSS content from main CSS bundle (styles.css). Can this be done?

@slorber
Copy link
Collaborator

slorber commented Nov 4, 2020

this can probably be done but I don't know how yet

@lex111
Copy link
Contributor Author

lex111 commented Nov 4, 2020

Hm.. what about "lazy" load custom CSS, something like as in Algolia's doc search?

@slorber
Copy link
Collaborator

slorber commented Nov 5, 2020

this only works if that CSS will only be displayed on HTML that is not immediately visible, otherwise it will produce FOUC.
I'd rather make Webpack output another css file, that seems the proper solution.

@lex111
Copy link
Contributor Author

lex111 commented Nov 5, 2020

I'd rather make Webpack output another css file, that seems the proper solution.

Agree, so we need to create new webpack entry specially for the custom CSS files?

@slorber
Copy link
Collaborator

slorber commented Nov 5, 2020

I think yes, and find a good api to express that for users, maybe something like siteConfig.customCSS = string | string[]
Maybe we should even deprecate theme.customCSS as it could be confusing?
We should document the difference in term of CSS ordering between this new API and the clientModules

@lex111
Copy link
Contributor Author

lex111 commented Nov 5, 2020

I'd rather leave the customCSS in the theme config, but just put them at the end of final CSS by default (i.e. after the CSS modules). If this can be achieved, it will be very very good.

@slorber
Copy link
Collaborator

slorber commented Nov 6, 2020

I don't think it's a good idea, because the theme would need a new plugin lifecycle to be able to add this css at the bottom of the stylesheet.
I don't think as of now it would be great to introduce a new plugin lifecycle just for this usecase, we'd rather make this a core config feature instead?

@lex111
Copy link
Contributor Author

lex111 commented Nov 6, 2020

All right then, I just wanted to avoid another deprecated filed (and use new filed instead), as this is a manual process by the users.

@slorber
Copy link
Collaborator

slorber commented Nov 6, 2020

Yes. I guess we could keep the theme customCss field, but maybe deprecate it in the doc (or at least recommend to use site. customCss field) and add a Joi warning? (afaik it's possible to have Joi validation warnings instead of errors, but I don't think our code can handle warnings already)

@lex111
Copy link
Contributor Author

lex111 commented Nov 6, 2020

Sounds good, but now we need to figure out how to do what we want 🤔

@sebdanielsson
Copy link

I don't know why but I added "chokidar": "^3.5.1" to the packages.json and now all entries in my custom.css is loaded.

@lex111 lex111 changed the title Custom CSS content should be append to the end of bundled CSS file Unable override built-in components styles Jun 10, 2021
@lex111
Copy link
Contributor Author

lex111 commented Jun 10, 2021

Clarify the issue: currently, the styles (specified in CSS modules file) of Docusaurus built-in components cannot be overridden, except by applying !important rule, since they are included after the custom CSS file.

@platformlead
Copy link

Hello there I think I run into this issue.  I  (as a non developer/devops engineer ) am using the \src\css\custom.css with more than less success to customize few properties of the site. 

My current goal is to change the width of the document - TOC rows (inside the docMainContainer) from the default 75% -25% to 85%-15% using css properties.  I looked at #3955 , #3879 and more but was not able to generate the proper styling because the element has the 75% !important property which overrides anything I set for it. 
 

My workaround for the time is doing a build, then search for the "75% !important" string and replacing the !important part of string with "". That and custom css property at the end of the post solves the issue for me. 
 
Though this works, it is not an elegant solution. Am I missing something? There should be a better way, right? 
 

Thank you for reading this and your efforts for creating this tool. Cheers. 

.row .col.col--3 {
  --ifm-col-width: 15%;
}

@Josh-Cena
Copy link
Collaborator

Josh-Cena commented Nov 22, 2021

Revisiting this issue. I'm not sure how we are to fix it, and the more I think about it the more I believe we can just put the CSS file in static and then use stylesheets to add them to the HTML head...

I would agree on a siteConfig.customCss field, and minimize and output each file separately as a <link> tag, as opposed to squashing them with the rest of the CSS, to make sure they always come out on top.

@slorber
Copy link
Collaborator

slorber commented Dec 1, 2021

I would agree on a siteConfig.customCss field, and minimize and output each file separately as a tag, as opposed to squashing them with the rest of the CSS, to make sure they always come out on top.

That also makes sense to me.

  • this API is not very intuitive IMHO. Users do not expect this but you can add a js file or a X.module.css file there even if it doesn't really make sense 🤷‍♂️

But we might still want to have some processing/minification being run on that CSS file

@slorber slorber added this to the 2.0.0 milestone Dec 16, 2021
@slorber
Copy link
Collaborator

slorber commented Dec 16, 2021

Added this isssue for the 2.0 milestone (see #6113), as it looks important that we commit to maintaining backward compatibility on CSS ordering

@slorber slorber changed the title Unable override built-in components styles [2.0] Ensure proper and stable CSS insertion ordering Dec 16, 2021
@slorber
Copy link
Collaborator

slorber commented Jan 5, 2022

The problem is not so easy to solve.
See also: #6227

For v2.0 we'll keep the existing behavior, at least we now have tests to ensure CSS order does not change by mistake.

@slorber slorber changed the title [2.0] Ensure proper and stable CSS insertion ordering Ensure proper and stable CSS insertion ordering Jan 5, 2022
@slorber slorber modified the milestones: 2.0.0, 3.0.0 Jan 5, 2022
KallynGowdy added a commit to casual-simulation/casualos that referenced this issue Jan 10, 2022
…red on build

- rc-tooltip: https://www.npmjs.com/package/rc-tooltip
- The issue was that two CSS declarations (.rc-tooltip & .rc-tooltip-hidden) appear with one order in development (.rc-tooltip then .rc-tooltip-hidden) and the reverse order in a build (.rc-tooltip-hidden then .rc-tooltip).
- See facebook/docusaurus#3678
- This patch fixes the issue by ensuring that the .rc-tooltip-hidden class takes precedence over the .rc-tooltip class.
nomisRev pushed a commit to arrow-kt/arrow-website that referenced this issue Apr 5, 2023
### Description

This PR make some changes on some selectors' specificity. Due to the way
Docusaurus build production styles, this could sometimes create a
problem through the CSS injection order, making some Infima styles
override custom ones. To overcome this, we are using a subtle trick by
duplicating the class in our CSS, to increase the specificity of our
selectors.

It doesn't seem like an easy task to solve in general for Docusaurus.
More info in: facebook/docusaurus#3678

It also adds proper styling for the Table of Contents on mobile.

This closes #147

### Preview


![imagen](https://user-images.githubusercontent.com/7753447/230122746-a8eb2d3d-f70b-4bb1-92b2-1ea06ca65563.png)

![imagen](https://user-images.githubusercontent.com/7753447/230122431-ddd3dd06-5b06-4e8b-a91e-1aae1723e5f2.png)
calvellido added a commit to arrow-kt/arrow-website that referenced this issue Apr 6, 2023
### Description

This PR adds a small section with syndication links to the blog posts
listing page.

It also removes some `TODO` code that has deemed to be not successful
for what we wanted to. Basically, after submitting an issue to one of
our library dependencies, I thought that by the uses of the CSS Modules
`composes` feature, we could avoid the CSS injection order problem
present in:

#147
#158
facebook/docusaurus#3678

But it doesn't seem to be the case, so we are in the need of the use of
the double selector trick to successfully override some Infima selectors
styling with our classes.

This closes #153

### Preview


![imagen](https://user-images.githubusercontent.com/7753447/230139114-f5ef7faa-5988-4871-8dd9-60b91caf2352.png)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants