Sidecar CSS encapsulation idea #656
Replies: 3 comments 4 replies
-
That sounds like an interesting technique! Something you could try as an alternative is to use a custom element rather than a generic div class. (FYI not talking about web components per se.) So for instance:
might map to
and in CSS:
Does that provide enough specificity/encapsulation for you? |
Beta Was this translation helpful? Give feedback.
-
@bessey thanks for taking the time to write this up! We're in the process of building out a prototype internally for the kind of encapsulation you're proposing here, but it's quite experimental and not yet ready to see the light of day 😅 . I'm hoping to share more in this area in the next couple of months ❤️ |
Beta Was this translation helpful? Give feedback.
-
Ok, I extracted the proof of concept from a private repo into a clean Rails repo, the README links to the PR that adds the interesting bits, its only a couple hundred lines for those interested! |
Beta Was this translation helpful? Give feedback.
-
I have been playing with View Components for the first time the past week, mostly to see if I could get anywhere close to the good bits of the React dev experience. One part I do like about that world is styled components / emotion / in general the CSS In JS movement. However, its not the "in JS" part I like, it's just the fact there's good solutions for encapsulating the styles for a specific component.
From the docs today, the only suggestion I see for automating this is with Web Components. After playing with this for some time I dismissed this as a viable approach for a few reasons, some of which have workarounds:
Instead, what I wanted to try was automatically encapsulating the CSS associated with a template, just using convention over configuration. I succeeded! My approach is, when rendering a given View Component...
::component
selector (this is valid if useless CSS, its a pseudo-element selector)Hopefully you can see where I'm going with this. The result is "magically" encapsulated CSS, without writing any JS. I was able to achieve a pretty good proof of concept of this with nothing but a snippet of Ruby and a wee custom webpack loader. It has niceties like the ability to disable the automatic wrapping to instead allow the dev to manually choose the element in the HTML where the encapsulation class can go, without adding more HTML elements. It also allows referring to other components' encapsulation classes from within CSS with
::component("example/path/to/my_component") { ... }
which I imagine could be useful for tweaking the styles of a component in a certain context.This is all in private repo so I will need to extract to a clean Rails install and do a write up before I can share it, but I was too excited to hold off posting 😅!This is now published for all to see. Has anyone else tried something similar already? This seems like such a powerful technique to me, it might be worth considering standardising and including in the library itself.Beta Was this translation helpful? Give feedback.
All reactions