CSS Variable Namespacing - #68846
Conversation
6c27311 to
302644d
Compare
302644d to
e95f942
Compare
| // Validate that the whole `--foo` variable is passed in. | ||
| if (typeof ngDevMode === 'undefined' || ngDevMode) { | ||
| if (!name.startsWith('--')) { | ||
| throw new Error( |
There was a problem hiding this comment.
I think this should be a RuntimeError ?
There was a problem hiding this comment.
My understanding is that RuntimeError is more for errors from framework internals that might not present clean stack traces and warrant additional documentation. The precedent I see is using Error in cases like this.
e95f942 to
5dd7961
Compare
dgp1130
left a comment
There was a problem hiding this comment.
Looks great, no major concerns on my end. Thanks for taking this on @mattrbeck!
3c2ce20 to
af7d747
Compare
af7d747 to
73bad82
Compare
Adds logic to inject symbols into CSS variables for runtime namespacing. The runtime now replaces instances of `%NS%` with a namespacing variable, limiting reach of CSS variables to the current app. An opt-out syntax of a `--global` prefix allows users to avoid this behavior.
Using `--global-foo` is now prohibited. We suspect these cases will likely be typos of `--global--foo` in the future, so we blanket ban them and direct users to the expected syntax.
Adds support for namespacing css variables in style properties. Behaves as you'd expect following the implementation for stylesheets generally. This change also moves the error message into a util function since we now need to produce the same error in three places.
... for now. Should be enabled in the next major.
73bad82 to
9c1a257
Compare
|
Caretaker: TGP is green as of July 1. Possibility for backslide if a new CSS variable beginning with |
|
This PR was merged into the repository. The changes were merged into the following branches:
|
Using `--global-foo` is now prohibited. We suspect these cases will likely be typos of `--global--foo` in the future, so we blanket ban them and direct users to the expected syntax. PR Close #68846
Adds support for namespacing css variables in style properties. Behaves as you'd expect following the implementation for stylesheets generally. This change also moves the error message into a util function since we now need to produce the same error in three places. PR Close #68846
... for now. Should be enabled in the next major. PR Close #68846
|
This pull request has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Superseds #67362. Primary differences are:
--globalprefix, e.g.--global--foo: blue[style.--foo]="'blue'"--global-fooThis 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
Closes #67362 via supersession.