stitches icon indicating copy to clipboard operation
stitches copied to clipboard

CSS Specificity issue with Reach UI

Open kysley opened this issue 5 years ago • 11 comments

Bug report

Describe the bug

When extending the @reach/listbox component, the css that is generated by stitches holds a lower priority than the base css that is required by the library. This means that much of the styling you define with styled will be overridden without !important everywhere.

To Reproduce

https://codesandbox.io/s/musing-nobel-tuyif?file=/src/App.js

The sandbox contains a minimum repro :) When you inspect the dropdown (popover) element, you will see that the priority of stitches styles are lower.

The top dropdown, built with stitches, will have a white background when open. The bottom dropdown, built with SC, has a green background as expected.

Expected behavior

This seems to work as expected using styled-components, which I was using before testing this out (love what you guys have here!).

Screenshots

Stitches behavior

image

S-C behavior

image

System information

  • Version of Stitches: canary-7
  • Version of Styled Components: 5.2.1

kysley avatar Feb 18 '21 04:02 kysley

Hmm, yeah. It seems that the style tag added by stitches quite high up in the head. Whereas as styled-components tries to put it as lower down as possible.

peduarte avatar Feb 18 '21 05:02 peduarte

@peduarte I could work on this if you could provide some insight on how we want to implement it? Not sure if we want to follow how styled-components does it (https://github.com/styled-components/styled-components/blob/08390f25fa7e353107f0a30080075104cec6426f/packages/styled-components/src/sheet/dom.js#L10).

kysley avatar Feb 18 '21 16:02 kysley

/cc @jonathantneal

peduarte avatar Feb 18 '21 21:02 peduarte

If you would like to put the stitches style tag (<style id="stitches" />) lower in the <head />, then it will take whichever priority you would like to give it.

jonathantneal avatar Feb 18 '21 21:02 jonathantneal

@jonathantneal, Using snowpack to bundle, I don't have the liberty of inserting a style tag lower in the <head />. I forked this and am working on a branch where stitches inserts its style at the bottom of the <head />- assuming this is something that should be fixed.

Image

kysley avatar Feb 19 '21 14:02 kysley

FWIW, the @reach styles are not required. They're heavily emphasized, but if you - in good faith - replicate things with higher precedence... that's a totally valid workaround.

kylemh avatar Feb 19 '21 18:02 kylemh

There are a few ways we can provide this feature. @kysley, what would be your ideal API?

We could let you specify a node. We could let you specify a selector. We could give you a react element. We could fire an "update" event or something like that with all the necessary CSS. We could give you a prepend/append switch and ... well, hopefully you see that you have options, and we want the best DX for you.

I’m glad y’all need this feature. I was surprised that nearly-if-not-all CSS-in-JS libraries get away with auto-injecting the <style> tag. I’m good with the feature, because so many designers & developers prefer how quickly they can build things this way, but I would personally never let a script perform any kind of implicit DOM manipulation on my site, because I prefer everything explicit like how things work with css() or styled().

Anyway — it may be wise to not add features for their own sake, but your request shows me that Stitches ought to give control of <style> to authors (like me, and maybe you, too! 😸). We’ll add this, your feedback is appreciated, and thanks for bringing it up.

jonathantneal avatar Feb 19 '21 21:02 jonathantneal

Thanks for understanding @jonathantneal, expressing my frustration (and intent) with such grace.

Thinking about an ideal API, prepend/append switch sounds good- perhaps the best? As a consumer I might worry about placing the react element too close to the root (so that stitches styles are processed/added too soon, if that makes sense?). A switch would be expressive enough to know, that if the order is append, its going at the end of the <head/>.

I don't see an issue with specifying a selector/node, but that might be more useful for core users- or users who don't have all their styles bundled for them.

kysley avatar Feb 19 '21 23:02 kysley

@jonathantneal Possibly related: https://github.com/modulz/stitches/issues/357

peduarte avatar Feb 20 '21 15:02 peduarte

Checking in to say that the api works as expected. Thanks a lot @jonathantneal!

kysley avatar Mar 10 '21 20:03 kysley

@kysley Great to hear.

peduarte avatar Mar 10 '21 22:03 peduarte