Open
Conversation
This allows Angular to apply a namespace to any CSS variables based on runtime configuration. In most situations, the `APP_ID` followed by a separator (`-` or `_`) is likely sufficient as a namespace.
```typescript
bootstrapApplication(Root, {
providers: [
{
provide: APP_ID,
useValue: 'my-app',
},
// Variables like `--foo` will become `--my-app_foo`.
provideCssVarNamespacing('my-app_'),
],
});
```
This is implemented by having the compiler unconditionally transform `--foo` to `--%NS%foo` to ensure a consistent output format, and then replace the `%NS%` at runtime with the namespace given to `provideCssVarNamespacing` (or an empty string for a no-op).
Adds the CssVarNamespacer utility service for prefixing CSS variables with MicA namespace dynamically.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds CSS variable namespacing support to Angular.
This allows multiple apps to coexist on the same page with isolated CSS variables, meaning one can use
color: var(--primary-color);without worrying about accidentally inheriting the primary color of a different app which happens to set it on an ancestor element.To enable this feature, call
provideCssVarNamespacingin yourapp.config.ts. Typically you want to configure this with the same value asAPP_ID, but with an additional separator at the end (a-or_):This only namespaces styles in Angular components (the
stylesorstyleUrlsproperties in@Component). It does not namespace global styles, which are out of scope for this effort.Namespacing does naturally break any JavaScript references to CSS variables, therefore this PR also introduces
CssVarNamespacerwhich allows you to automatically namespace variables based on what is configured in the application.Libraries should consider always using the namespacer when referring to CSS variables, as they may be consumed by applications which enable namespacing.
Namespacing works by having the compiler unconditionally prepend
%NS%to CSS variables (--foo->--%NS%foo) and then at runtime replaces%NS%with a namespace specified byprovideCssVarNamespacing('my-app_')(--%NS%foo->--my-app_foo).Internal bug: b/485672083