Skip to content

Commit 9d7e856

Browse files
committed
fix(material/schematics): add schematic to rename tokens
1 parent d6b6bce commit 9d7e856

File tree

1 file changed

+66
-8
lines changed
  • src/material/schematics/ng-update

1 file changed

+66
-8
lines changed

src/material/schematics/ng-update/index.ts

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {Rule, SchematicContext} from '@angular-devkit/schematics';
9+
import {chain, Rule, SchematicContext} from '@angular-devkit/schematics';
1010
import {
1111
createMigrationSchematicRule,
1212
NullableDevkitMigration,
@@ -22,14 +22,72 @@ const materialMigrations: NullableDevkitMigration[] = [
2222
ExplicitSystemVariablePrefixMigration,
2323
];
2424

25-
/** Entry point for the migration schematics with target of Angular Material v19 */
25+
/** Entry point for the migration schematics with target of Angular Material v20 */
2626
export function updateToV20(): Rule {
27-
return createMigrationSchematicRule(
28-
TargetVersion.V20,
29-
materialMigrations,
30-
materialUpgradeData,
31-
onMigrationComplete,
32-
);
27+
return chain([
28+
createMigrationSchematicRule(
29+
TargetVersion.V20,
30+
materialMigrations,
31+
materialUpgradeData,
32+
onMigrationComplete,
33+
),
34+
renameMdcTokens(),
35+
renameComponentTokens(),
36+
]);
37+
}
38+
39+
// Renames any CSS variables beginning with "--mdc-" to be "--mat-". These CSS variables
40+
// refer to tokens that used to be derived from a mix of MDC and Angular. Now all the tokens
41+
// are converged on being prefixed "--mat-".
42+
function renameMdcTokens(): Rule {
43+
return tree => {
44+
tree.visit(path => {
45+
const content = tree.readText(path);
46+
const updatedContent = content.replace('--mdc-', '--mat-');
47+
tree.overwrite(path, updatedContent);
48+
});
49+
};
50+
}
51+
52+
// Renames Angular Material component token CSS variables that were renamed so that the base
53+
// component's name came first or otherwise renamed to match our terminology instead of MDC's.
54+
function renameComponentTokens(): Rule {
55+
const replacements = [
56+
{oldStr: '--mat-circular-progress', newStr: '--mat-progress-spinner'},
57+
{oldStr: '--mat-elevated-card', newStr: '--mat-card-elevated'},
58+
{oldStr: '--mat-extended-fab', newStr: '--mat-fab-extended'},
59+
{oldStr: '--mat-filled-button', newStr: '--mat-button-filled'},
60+
{oldStr: '--mat-filled-text-field', newStr: '--mat-form-field-filled'},
61+
{oldStr: '--mat-full-pseudo-checkbox', newStr: '--mat-pseudo-checkbox-full'},
62+
{oldStr: '--mat-legacy-button-toggle', newStr: '--mat-button-toggle-legacy'},
63+
{oldStr: '--mat-linear-progress', newStr: '--mat-progress-bar'},
64+
{oldStr: '--mat-minimal-pseudo-checkbox', newStr: '--mat-pseudo-checkbox-minimal'},
65+
{oldStr: '--mat-outlined-button', newStr: '--mat-button-outlined'},
66+
{oldStr: '--mat-outlined-card', newStr: '--mat-card-outlined'},
67+
{oldStr: '--mat-outlined-text-field', newStr: '--mat-form-field-outlined'},
68+
{oldStr: '--mat-plain-tooltip', newStr: '--mat-tooltip'},
69+
{oldStr: '--mat-protected-button', newStr: '--mat-button-protected'},
70+
{oldStr: '--mat-secondary-navigation-tab', newStr: '--mat-tab'},
71+
{oldStr: '--mat-standard-button-toggle', newStr: '--mat-button-toggle'},
72+
{oldStr: '--mat-switch', newStr: '--mat-slide-toggle'},
73+
{oldStr: '--mat-tab-header', newStr: '--mat-tab'},
74+
{oldStr: '--mat-tab-header-with-background', newStr: '--mat-tab'},
75+
{oldStr: '--mat-tab-indicator', newStr: '--mat-tab'},
76+
{oldStr: '--mat-text-button', newStr: '--mat-button-text'},
77+
{oldStr: '--mat-tonal-button', newStr: '--mat-button-tonal'},
78+
];
79+
return tree => {
80+
tree.visit(path => {
81+
const content = tree.readText(path);
82+
let updatedContent = content;
83+
for (const replacement of replacements) {
84+
updatedContent = updatedContent.replace(replacement.oldStr, replacement.newStr);
85+
}
86+
if (content !== updatedContent) {
87+
tree.overwrite(path, updatedContent);
88+
}
89+
});
90+
};
3391
}
3492

3593
/** Function that will be called when the migration completed. */

0 commit comments

Comments
 (0)