-
Notifications
You must be signed in to change notification settings - Fork 11
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
[RRFC] Add ability to set dynamic styles programatically after render #13
Comments
The core of lit-html has no knowledge of classes and styles, they're just attributes, and One way to do this would be to have We'd need a name for this option. I'll call it constructor() {
super();
this.styles = {color: 'lightgreen', fontFamily: 'Roboto'};
}
render() {
return html`
<div style=${styleMap(this.styles, {directUpdate: true})}>
Some content
</div>
`;
} |
How would that integrate with the I was thinking the stylemap stuff would need evaluated here, along side Similar to how lit-html doesn't have knowledge of the event's name, callback, or options, but still adds it: if (shouldAddListener) {
this.element.addEventListener(
this.name,
this,
newListener as EventListenerWithOptions
);
} Functionality could be added to apply styles to an element: if (shouldAddStyles) {
Object.entries(styleMap).forEach((property, value) => {
this.element.style[property] = value;
}
} |
@andy-blum The only thing that needs to be done here is to put a condition around the few lines that prime the previousStyleProperties set and return the result of render(). Something like: if (this._previousStyleProperties === undefined) {
this._previousStyleProperties = new Set();
if (this.directUpdate !== false) {
for (const name in styleInfo) {
this._previousStyleProperties.add(name);
}
return this.render(styleInfo);
}
} You should be able to try this out by commenting out lines 69-72 locally and seeing that it works with CSP style-src. |
I've been poking at this again, and it seems that See this codepen: https://codepen.io/andy-blum/pen/JjZQrwj Notice how the If you remove the |
@kevinpschaaf I think this may be a reason to have |
Given that this is a pretty core directive with CSP implications, we could just bite the bullet and make a node build for |
…first render r=mtigley lit calls `element.setAttribute("style", "<our styles>")` on first render of an element so that it will have the style property set in the markup if it is being server-side rendered. We don't need that, and it causes CSP errors in about:logins so this patches out the functionality. lit/rfcs#13 Differential Revision: https://phabricator.services.mozilla.com/D165240
…first render r=mtigley lit calls `element.setAttribute("style", "<our styles>")` on first render of an element so that it will have the style property set in the markup if it is being server-side rendered. We don't need that, and it causes CSP errors in about:logins so this patches out the functionality. lit/rfcs#13 Differential Revision: https://phabricator.services.mozilla.com/D165240
Motivation
In the documentation for styles, there is a brief section dynamic styles and classes. In this section it provides the below example (modified for clarity):
The stylemap directive simply iterates over
Object.entries(this.styles)
and usesArray.prototype.reduce()
to form a string that can be rendered as part of the component's own lifecycle. Styles added this way, however, are blocked when using a CSP with thestyle-src
directive (MDN)From the MDN page above:
Example
On a page with the CSP header
Content-Security-Policy: style-src https://example.com/
This code adds the styles to the markup, but the browser does not apply those styles, blocking them per the content security policy.
How
I propose adding a new
@
expression or similar, as we would for attaching event listeners, to programatically attach stylemap-ready objects post-render:In this situation, the html tagged literal wouldn't render
style="color: lightgreen; font-family: 'Roboto';"
, but instead render the div without a style element and then post-render run:References
The text was updated successfully, but these errors were encountered: