-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Alternative API for style modules? #4940
Comments
When moving to string templates and JS imports would we be able to get a style module
And then in your component start doing:
Not sure which I'd say is better, but, if I understand correctly, shared style modules are duplicated into all of the components where they are applied anyways, so this might reduce some boot up time? |
@Westbrook Per #4937 the static template getter now needs to return an export const mySharedStyles = html`
<style>
:host {
color: red;
border: 2px solid blue;
};
</style>` import mySharedStyles from `my-shared-styles.js`;
...
class MyElement extends PolymerElement {
static get template() {
return html`
${mySharedStyles}
<style>
h2.styled-locally {
... etc ....
}
</style>
<h2>My Element</h2>`;
}
} |
@arthurevans While I think the above will likely be what we promote for new development, we still need to decide how to migrate existing code. Had a discussion with @justinfagnani and @sorvell about this recently and we were debating the following options:
Forget what we landed on, but hopefully this will spark discussion. |
Given that we added That is,
Do you have any thoughts about whether this pattern would be preferred, versus using a full template? |
@arthurevans Can you explain a more complicated use case, for example if we want to import and external css file..maybe after a postcss processing. How that can be achievable with your example? BTW, as css developer, I personally don't a like a style definition inside a js file, mainly because if we want to pre/post process the style we need a way to import the css without manually injecting it. I think that css should remain pure css and not a string inside a js file, or maybe not at the definition level. Users should write pure css and be able to integrate others tools. Starting from a string inside a js file does not make easy integrate eg. postcss or any other css parsing. Right now, with Polymer Skeleton and webpack we just import css files a normal moduleß (using the postcss-loader obviously) and interpolate them inside the template string: my-element.js import {Element as PolymerElement} from '@polymer/polymer/polymer-element';
import './../../dumbs/sk-menu';
import cssHueRotate from '../../common-styles/keyframes/hue-rotate.pcss';
import css from './style.pcss';
import template from './template.html';
export default class SkApp extends PolymerElement {
static get template() {
return `
<style>${cssHueRotate} ${css}</style> ${template}`;
}
}
window.customElements.define('sk-app', SkApp); hue-rotate.pcss @keyframes HueRotate {
0% {
filter: none;
}
50% {
filter: hue-rotate(360deg);
}
100% {
filter: none;
}
} Is that something achievable? |
👀 Yeah, I put one of our components (from predix-ui.com) through the modulizer and got scared...! I don't have a proposal in mind but just outlining how we do things on our large polymer-based design system.
Our design system is also completely theme-able the presence of a Cheers! |
@equinusocio I think your use case is worth solving one way or another. The only reason your example wouldn't work (once updating to use the |
@mdwragg I think the idiomatic code for what you described going forward in the Polymer 3 module world would look like this:
Unfortunately, modulizer doesn't currently do this transformation because If possible I'd recommend the manual migration I described in the bullets above since it's more idiomatic and maintainable going forward. An alternative is that we could provide an |
Hi @kevinpschaaf, thanks for getting back to me. I'd have to try it, but the |
@mdwragg const $_documentContainer = document.createElement('div');
$_documentContainer.setAttribute('style', 'display: none;');
$_documentContainer.innerHTML = `<dom-module>...</dom-module>`;
document.head.appendChild($_documentContainer); So the " function upgradeHtml(html) {
const $_documentContainer = document.createElement('div');
$_documentContainer.setAttribute('style', 'display: none;');
$_documentContainer.innerHTML = html;
document.head.appendChild($_documentContainer);
} If upgradeHtml(`
<dom-module id="px-toggle-styles">
<template>
<style>...</style>
</template>
</dom-module>
`) We've discussed this internally, and the only reason we didn't just do this in modulizer is that the above code is still an extremely roundabout way of getting the shared style text content to the desired spot if you're not parsing anything natively in HTML anymore (vs. where this made more sense with HTML Imports). Rather than just importing the string and interpolating it into the template where needed (step 3 above, basically), instead you're creating a div, innerHTML'ing a custom element into it, which then boots up and registers its content in the dom-module map, and then when the custom element boots up and parses its template, it sees the style include, goes and finds the dom from the dom-module map, finds the styles in it, and clones them into the template. We're just uncertain we want to provide sugar to allow people to keep doing the roundabout thing rather than biting the bullet and doing the more idiomatic thing. OTOH the one advantage is that you don't have to touch any of the usage sites ( |
This is slightly off topic perhaps, but I’ve been wondering and I’m curious how the idiomatic way affects performance, if at all? I mean, I kind of assume that the browser will have to do the style parsing for each instance of the element when it’s concatenated as a string to the template. But I also assume this is such an obvious issue, that browsers have optimizations in place for this, and it doesn’t matter how many element instances you have on the page (1..n), the cost for the styles is always the same. With the Polymer 2 way, using dom-modules, my assumption has been that the style element, even though it’s inside a template tag, is only parsed once and under the hood, the browser just references a single style tree/document when it’s included/stamped for in an element instance. Seeing the same styles repeated over and over again inside each individual element instance in dev tools makes me a little uneasy 😅 |
@kevinpschaaf - yeah, I think I need a lie down in a darkened room and 2 days to think about it... True, we're going to be automagically generating this stuff from Polymer 2 components anyways so not sure it matters how grokkable the end result is. We'll be coding up in sass anyways and generating the style module for Polymer 2 'style js' for Polymer 3... I'll chin stroke some more... Thanks for the thoughts though, all good intel. |
@jouni Using the pattern above, the style string concatenation into the template is done once ever for the template; from there identical We are also helping push ConstructableStylesheets and an imperative API to add these to custom elements and/or shadowRoots, so that the "identicalness" heuristic is even more explicit to the browser, and would also avoid the need for the |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@kevinpschaaf I'm following up on this comment made a while back - I'm hoping to understand how to best apply common shared styles across 140 Polymer 3 components without stamping them into every Shadow DOM template with style modules - is there a recommended best practice now or a new API? It feels like this issue wasn't really addressed besides using the |
@laddng Polymer 3 doesn't support Constructible StyleSheets, so even the Polymer-specific style sharing with So in Polymer 3 the best recommendation is this pattern: const styleMod = document.createElement('dom-module');
styleMod.innerHTML = `
<template>
<style>
:host {
color: red;
border: 2px solid blue;
}
</style>
</template>
`;
styleMod.register('my-shared'); |
@justinfagnani Thank you for the fast response! This is very helpful and makes me more comfortable using the |
Thanks for the feedback. Will go ahead and close this issue, since there's an idiomatic, module-based pattern for sharing styles using |
Previously shared style modules were registered by instantiating a dom-module custom element, and adding it to the DOM, which eventually populated Polymer's internal style map. Adding the element to the DOM is unnecessary (and not free) as style modules can be registered directly using the dom-module's register() method, without adding them to the DOM, saving unnecessary operations. This is the recommendation from the Polymer team at [1]. Style modules that are still used by Polymer2 (OOBE) are not affected in this CL. [1] Polymer/polymer#4940 (comment) Bug: None Change-Id: Ib155c3bafadd7270c3eb82595b50beccfe5fa8d3 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3603224 Reviewed-by: John Lee <[email protected]> Commit-Queue: Demetrios Papadopoulos <[email protected]> Cr-Commit-Position: refs/heads/main@{#995822}
In this part fixing all occurrences under ash/webui/. Previously shared style modules were registered by instantiating a dom-module custom element, and adding it to the DOM, which eventually populated Polymer's internal style map. Adding the element to the DOM is unnecessary (and not free) as style modules can be registered directly using the dom-module's register() method, without adding them to the DOM, saving unnecessary operations. This is the recommendation from the Polymer team at [1]. Style modules that are still used by Polymer2 (OOBE) are not affected in this CL. [1] Polymer/polymer#4940 (comment) Bug: None Change-Id: If019dfc13f6c94e9932a4e53277ea14322740646 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3602650 Auto-Submit: Demetrios Papadopoulos <[email protected]> Reviewed-by: Kyle Horimoto <[email protected]> Commit-Queue: Kyle Horimoto <[email protected]> Cr-Commit-Position: refs/heads/main@{#995937}
Currently in 3.x, to create a style module (that you can then
<style include="name">
, you need to create adom-module
imperatively, inject the style, and callregister
on the style module.Kinda like here:
http://plnkr.co/edit/tfODOF?p=preview
This is not a lot of code, but it doesn't seem like a great developer experience. Compared to, say,
(Or something.)
The text was updated successfully, but these errors were encountered: