-
Notifications
You must be signed in to change notification settings - Fork 181
Styles declared via @CssImport should be isolated within exported component #21272
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
Comments
@mshabarov, how complex do you think this task would be? Let me know if anything needs clarification. |
Thanks for the issue @vursen , we only mention of this annotation for web component is in this article, https://vaadin.com/docs/latest/flow/integrations/embedding/theming#cssimport-in-embedded-applications, but it's the opposite - how to apply global styles to the web component. We need to investigate what's the effort. |
Prototype: main...proto-embedded-web-component-css-injection Overview
Vite plugin API?pathWhen ?include
import cssContent from 'path/to/file.css?inline';
const style = document.createElement('style');
style.textContent = cssContent.toString();
style.setAttribute('include', '{include}');
document.head.appendChild(style); ?theme-for
import cssContent from 'path/to/file.css?inline';
import { registerStyles, unsafeCSS } from '@vaadin/vaadin-themable-mixin';
registerStyles('{themeFor}', unsafeCSS(cssContent.toString()), {
moduleId: 'flow_css_mod_{AUTO_GENERATED_ID}'
}); ?module-id
import cssContent from 'path/to/file.css?inline';
import { registerStyles, unsafeCSS } from '@vaadin/vaadin-themable-mixin';
registerStyles('', unsafeCSS(cssContent.toString()), {
moduleId: '{moduleId}'
}); ?exported-web-component (new mechanism)
import 'path/to/file.css?global-css-only'; // <--- included in CSS bundle in build mode
import cssContent from 'path/to/file.css?inline'; // <--- included in JS bundle in build mode
import { injectExportedWebComponentCSS } from 'Frontend/generated/jar-resources/flow-css-import.js';
injectExportedWebComponentCSS('{AUTO_GENERATED_ID}', cssContent.toString(), {
selector: '{exportedWebComponent}'
});
import.meta.hot?.accept(); // <--- enables hot reloading
?exported-web-component +
|
Uh oh!
There was an error while loading. Please reload this page.
Let's consider this example:
In this example,
my-component.css
will be injected into both the global DOM and the Shadow DOM of all exported components. Currently, the only way to scope a stylesheet exclusively to a specific exported component is by importing it inside a theme that is applied to the exporter class:However, the current theming system based on the
@Theme
annotation is going to be deprecated in Vaadin 25. This creates a need for an alternative approach that allows developers to scope styles to an exported component without relying on themes.A solution would be to change the default behavior of
@CssImport
so that styles declared with it are injected only into the Shadow DOM of exported components that include the annotated class in their component hierarchy.For example, if a
PhoneField
component is used somewhere within the component hierarchy ofLoginFormExporter
andAccountFormExporter
, its@CssImport
should be applied only within the Shadow DOM of those two exported components, remaining fully isolated from the rest of the page and other exporters:There is an assumption that developers typically have no need to add styles to the global DOM, except for
@font-face
, which must be defined globally. This specific declaration can be automatically extracted and moved to the global DOM, as proposed in vaadin/platform#7453. This could presumably be implemented as a Vite plugin.The text was updated successfully, but these errors were encountered: