Skip to content

Commit 5359a98

Browse files
authored
feat: offer standalone friendly API (#160)
1 parent dec96ad commit 5359a98

File tree

4 files changed

+60
-21
lines changed

4 files changed

+60
-21
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ import { MaterialCssVarsModule } from "angular-material-css-vars";
3737
export class AppModule {}
3838
```
3939

40+
In standalone workspaces, add the following to your `app.config.ts`:
41+
42+
```typescript
43+
import { ApplicationConfig } from "@angular/core";
44+
import { provideMaterialCssVars } from "angular-material-css-vars";
45+
46+
export const appConfig: ApplicationConfig = {
47+
providers: [
48+
provideMaterialCssVars({
49+
// all optional
50+
isAutoContrast: true,
51+
primary: "#3f51b5",
52+
// ...
53+
}),
54+
],
55+
};
56+
```
57+
4058
5. If you want to adjust the theme at runtime, you can use `MaterialCssVarsService`:
4159

4260
```typescript

projects/material-css-vars/src/lib/material-css-vars.module.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { ModuleWithProviders, NgModule } from "@angular/core";
1+
import {
2+
ENVIRONMENT_INITIALIZER,
3+
EnvironmentProviders,
4+
inject,
5+
makeEnvironmentProviders,
6+
ModuleWithProviders,
7+
NgModule,
8+
} from "@angular/core";
29
import { CommonModule } from "@angular/common";
310
import { MaterialCssVariablesConfig } from "./model";
411
import { MATERIAL_CSS_VARS_CFG } from "../mat-css-config-token.const";
512
import { MaterialCssVarsService } from "./material-css-vars.service";
613

714
@NgModule({
8-
declarations: [],
915
imports: [CommonModule],
1016
})
1117
export class MaterialCssVarsModule {
@@ -18,6 +24,20 @@ export class MaterialCssVarsModule {
1824
};
1925
}
2026

21-
// This is necessary so the service is constructed, even if the service is never injected
27+
// This is necessary, so the service is constructed, even if the service is never injected
28+
// ToDo: change to environment initializer, like in the provideMaterialCssVars() function below
2229
constructor(private materialCssVarsService: MaterialCssVarsService) {}
2330
}
31+
32+
export function provideMaterialCssVars(
33+
config?: Partial<MaterialCssVariablesConfig>,
34+
): EnvironmentProviders {
35+
return makeEnvironmentProviders([
36+
{ provide: MATERIAL_CSS_VARS_CFG, useValue: config },
37+
{
38+
provide: ENVIRONMENT_INITIALIZER,
39+
useValue: () => inject(MaterialCssVarsService),
40+
multi: true,
41+
},
42+
]);
43+
}

src/app/app.config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ApplicationConfig } from "@angular/core";
2+
import { provideMaterialCssVars } from "../../projects/material-css-vars/src/lib/material-css-vars.module";
3+
import { APP_BASE_HREF } from "@angular/common";
4+
import { provideAnimations } from "@angular/platform-browser/animations";
5+
6+
export const appConfig: ApplicationConfig = {
7+
providers: [
8+
provideMaterialCssVars({
9+
primary: "#3f51b5",
10+
accent: "#e91e63",
11+
warn: "#f44336",
12+
}),
13+
{ provide: APP_BASE_HREF, useValue: window._app_base ?? "/" },
14+
provideAnimations(),
15+
],
16+
};

src/main.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
1-
import { enableProdMode, importProvidersFrom } from "@angular/core";
2-
1+
import { enableProdMode } from "@angular/core";
32
import { environment } from "./environments/environment";
43
import { AppComponent } from "./app/app.component";
5-
import { MaterialCssVarsModule } from "../projects/material-css-vars/src/lib/material-css-vars.module";
6-
import { provideAnimations } from "@angular/platform-browser/animations";
74
import { bootstrapApplication } from "@angular/platform-browser";
8-
import { APP_BASE_HREF } from "@angular/common";
5+
import { appConfig } from "./app/app.config";
96

107
if (environment.production) {
118
enableProdMode();
129
}
1310

14-
bootstrapApplication(AppComponent, {
15-
providers: [
16-
importProvidersFrom(
17-
MaterialCssVarsModule.forRoot({
18-
primary: "#3f51b5",
19-
accent: "#e91e63",
20-
warn: "#f44336",
21-
}),
22-
),
23-
{ provide: APP_BASE_HREF, useValue: window._app_base ?? "/" },
24-
provideAnimations(),
25-
],
26-
}).catch((err) => {
11+
bootstrapApplication(AppComponent, appConfig).catch((err) => {
2712
console.error(err);
2813
});
2914

0 commit comments

Comments
 (0)