-
Notifications
You must be signed in to change notification settings - Fork 376
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
Sharing styles across custom elements #282
Comments
Someone provided this example to me - perhaps you can adapt it :
I wonder if it will also work if you have a stylesheet link... |
FYI. There is a draft spec from @tabatkins, which will make the style sharing more efficient. |
In Blink, we calculate the hash value of textContent of The draft proposal is a way to resolve this issue officially. Users can feel that this kind of efficiency is guaranteed. |
@hayatoito you're specifically talking about issue 3 in that draft spec? It's a bit over my head, tbh, so I'm not sure how it will help the user - is it just about speed/efficiency, and the user just creates a style element and appends it, like above? |
@davidmaxwaterman Yes, creating a style element in each shadow tree is an option as a replacement, although I'm aware that that's not a direct replacement and it would be painful for users. We introduced |
@hayatoito yeah, but some people have the luxury of only targeting chrome :) Chrome apps and extensions, for example...and I guess some companies can mandate chrome as a browser (though usually it's IE, in my experience, unfortunately). |
I personally find using /deep/ (and ::shadow) as selectors very convenient, when developing extension for Chrome. It tends to at least keep both the (S)CSS and JS selects more straightforward to work with. |
Varunkumar, On Thu, Jul 16, 2015 at 1:29 PM, Varunkumar Nagarajan <
*Erik Isaksen * Google Developer Expert HTML5 The Web Platform Podcast http://thewebplatform.libsyn.com/ Show Host ReadTheSource.io http://ReadTheSource.io Co-Host nevraeka@gmail.com nevraeka@gmail.com The Web Platform Podcast Links Twitter - https://twitter.com/thewebplatform Google Plus - https://plus.google.com/u/0/106965203807974584370 Stream/Web -http://thewebplatform.libsyn.com/ Facebook - https://www.facebook.com/thewebplatform iTunes - Stitcher - http://www.stitcher.com/podcast/the-web-platform-podcast YouTube (not all episodes available) - RSS - http://thewebplatform.libsyn.com/rss Github - https://github.com/thewebplatform Read The Source: Open Source Companion Live Screencast Twitter - http://hangouts.readthesource.io/ Google Plus - https://plus.google.com/105627089988168968277/ Youtube - https://www.youtube.com/channel/UCVqD-Rd1nMmvbvf-AvQvgZw |
The shadow-piercing combinators are indeed helpful, but they're also (a) terrible for performance, and (b) footguns, as you can easily style things you didn't intend. @Nevraeka is right - using Custom Properties (soon to be released in Blink) as the theming mechanism is safer and more targeted, and more performant. We expect to add more theming abilities in the future, like perhaps a way to expose an element/subtree from your component as a pseudo-element on the host, so you can do arbitrary styling. But that's in the future, and is just a convenience measure. |
Custom properties won't help me always. For example, I want to use font-awesome in my components. Currently, I am using /deep/ combinator for all classes in fa. How can I achieve the same without that? I would end up repeating those classes in all components. I understand the complexities involved in supporting shadow-piercing combinators. However, I still feel that custom properties wouldn't be just sufficient. I will read about construct stylesheets spec. |
@varunkumar, fwiw, I hit the exact same problem with font awesome. Custom properties don't help here, but the solution is not to @import 'fa-circle' There may be a little repetition, but it's better for the component to declare exactly what it needs rather than assuming it will be there. The browser can deal with caching and optimise the problem of not fetching a duplicate stylesheet more than once. Until |
Is |
Awesome! I didn't realise |
All those at rules should work just like any other style element in regular DOM except that fonts imported inside a shadow dom, for example, isn't exposed to "light" DOM's stylesheets. |
We have started using |
Correct me if I am wrong @rniwa, but doesn't look like the discussion on @font-face in Shadow DOM has been resolved yet.. |
Correct. We've never come up with a satisfactory answer to the problem of "the 'font-face' property inherits, but could accidentally refer to completely different |
Given that we now have http://tabatkins.github.io/specs/construct-stylesheets/, this issue should be tracked form the view of custom elements. Let me add |
Expanding on @davidmaxwaterman's idea, you could use a tool like @kof's JSS (among others) to generate the repeat styles you need and add them to your roots. It seems like http://tabatkins.github.io/specs/construct-stylesheets/ might be similar to this JSS idea, with CSS strings instead of object literals, but I don't see any examples in that document (that would be nice @tabatkins!). I suppose it could be something like // shared-style.js
export default
function myStyle(color) {
return new CSSStyleSheet(`
.some-el {
background: ${color};
}
`, options)
} // other-file.js
import myStyle from '.../shared-style'
let style = myStyle('blue')
// add the style to the root |
also see cssinjs/jss#220 |
This can be rolled into #468 which has a more detailed proposal in #468 (comment) and #468 (comment). That will only allow strings for now, but once constructible stylesheets are a thing it's a straightforward extension. |
We are building a bunch of components for an internal project. These components share some common styles (like font-awesome fonts, custom css framework, etc). As of now, we are sharing these styles across components using /deep/ combinators. I understand from the recent web components meeting that /deep/ combinator and other shadow piercing combinators are going to be removed.
I understand that Shadow DOM provides style boundary and it also helps in abstracting the implementation details of the component. In my use case, I am more interested in the latter than the former. Though style boundary prevents accidental leakage of styles, I still find the ability to selectively pierce the components useful.
Please suggest alternatives for being able to share styles across components. (Custom css properties and named parts model might not be suitable for this use case. I am interesting in sharing classes across components than configuring hooks for theming).
The text was updated successfully, but these errors were encountered: