Skip to content

Commit 45f220d

Browse files
authored
Merge pull request johannesjo#114 from ratatoeskr666/use_scss_support
added @use sass support to all style sheets
2 parents ded72b8 + c2e6ef2 commit 45f220d

File tree

10 files changed

+316
-296
lines changed

10 files changed

+316
-296
lines changed

README.md

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ You want to style your angular material dynamically with all the colors in the r
1212
2. If @angular/material is already configured remove `@import '@angular/material/theming';` from your main stylesheet file if present.
1313
3. Add this to your main stylesheet instead:
1414
```scss
15-
@import 'angular-material-css-vars/main';
15+
@use 'angular-material-css-vars/main' as mat-css;
1616
1717
// optional
1818
$mat-css-dark-theme-selector: '.isDarkTheme';
1919
$mat-css-light-theme-selector: '.isLightTheme';
2020
2121
// init theme
22-
@include init-material-css-vars() {
22+
@include mat-css.init-material-css-vars() using($mat-css-theme) {
2323
// If your app has any theme mixins, call them here.
2424
// $mat-css-theme gets set to an appropriate value before this content is called.
2525
// @include your-custom-component-theme($mat-css-theme);
@@ -69,18 +69,18 @@ export class AppModule {
6969
## Utility
7070
There are also several [utility functions and mixins](https://github.com/johannesjo/angular-material-css-vars/blob/master/projects/material-css-vars/src/lib/_public-util.scss).
7171
```scss
72-
@import 'angular-material-css-vars/public-util';
72+
@use 'angular-material-css-vars/public-util' as mat-css-utilities;
7373
7474
.with-color {
75-
border-color: mat-css-color-primary(300);
75+
border-color: mat-css-utilities.mat-css-color-primary(300);
7676
}
7777
7878
.color-and-contrast {
79-
@include mat-css-color-and-contrast(300);
79+
@include mat-css-utilities.mat-css-color-and-contrast(300);
8080
}
8181
8282
.with-bg {
83-
@include mat-css-bg(300);
83+
@include mat-css-utilities.mat-css-bg(300);
8484
}
8585
```
8686

@@ -89,23 +89,27 @@ There are also [some additional hacks](additional-hacks.md) (e.g. adding a color
8989
## Initialization Options
9090
You can provide different options before initialization to change the body class used for the dark theme and to provide different default styles:
9191
```scss
92+
...
93+
@use 'angular-material-css-vars/main' as mat-css;
94+
...
95+
9296
// $mat-css-default-light-theme: ... ;
9397
// $mat-css-text: ... ;
9498
$mat-css-dark-theme-selector: '.isDarkTheme';
9599
$mat-css-light-theme-selector: '.isLightTheme';
96100
97-
@include init-material-css-vars();
101+
@include mat-css.init-material-css-vars();
98102
99103
```
100104
To make those variables take effect with your mixins, you need to make sure that they are also defined before using them. E.g.:
101105
```scss
106+
@use 'angular-material-css-vars/public-util' as mat-css-utilities;
107+
102108
// probably best put in a common variables file and imported before the mixins
103109
$mat-css-dark-theme-selector: '.isDarkThemeCUSTOM';
104110
105-
@import 'angular-material-css-vars/public-util';
106-
107111
.my-component {
108-
@include mat-css-dark-theme {
112+
@include mat-css-utilities.mat-css-dark-theme {
109113
// dark theme styles ...
110114
}
111115
}
@@ -118,34 +122,37 @@ A full list of the theme map [can be found here](https://github.com/johannesjo/a
118122
### Set default (fallback palettes)
119123
There are two ways to set the default fallback theme. One is using the `mat-css-palette-defaults` mixin.
120124
```scss
121-
@import 'angular-material-css-vars/public-util';
122-
@import 'angular-material-css-vars/main';
125+
@use 'angular-material-css-vars/public-util' as mat-css-utilities;
126+
@use 'angular-material-css-vars/main' as mat-css-main;
127+
@use '@angular/material/theming' as mat-theming;
123128
124-
@include init-material-css-vars();
129+
@include mat-css-main.init-material-css-vars();
125130
126-
@include mat-css-set-palette-defaults($mat-light-blue, 'primary');
127-
@include mat-css-set-palette-defaults($mat-pink, 'accent');
128-
@include mat-css-set-palette-defaults($mat-red, 'warn');
131+
@include mat-css-utilities.mat-css-set-palette-defaults(mat-theming.$mat-light-blue, 'primary');
132+
@include mat-css-utilities.mat-css-set-palette-defaults(mat-theming.$mat-pink, 'accent');
133+
@include mat-css-utilities.mat-css-set-palette-defaults(mat-theming.$mat-red, 'warn');
129134
```
130135
The other is to include your own variables for [$mat-css-default-light-theme](https://github.com/johannesjo/angular-material-css-vars/blob/master/projects/material-css-vars/src/lib/_variables.scss).
131136
```scss
132-
@import 'angular-material-css-vars/main';
137+
@use 'angular-material-css-vars/main' as mat-css-main;
138+
@use 'angular-material-css-vars/variables' as mat-css-vars;
139+
@use 'angular-material-css-vars/public-util' as mat-css-utilities;
133140
134141
$mat-css-default-light-theme: map-merge(
135142
// if you don't want to enter ALL the properties
136-
$mat-css-default-light-theme,
143+
mat-css-vars.$default-light-theme,
137144
(
138-
--palette-primary-50: _mat-css-hex-to-rgb(#e1f5fe),
139-
--palette-primary-100: _mat-css-hex-to-rgb(#b3e5fc),
140-
--palette-primary-200: _mat-css-hex-to-rgb(#81d4fa),
141-
--palette-primary-300: _mat-css-hex-to-rgb(#4fc3f7),
142-
--palette-primary-400: _mat-css-hex-to-rgb(#29b6f6),
143-
--palette-primary-500: _mat-css-hex-to-rgb(#03a9f4),
145+
--palette-primary-50: mat-css-utilities.hex-to-rgb(#e1f5fe),
146+
--palette-primary-100: mat-css-utilities.hex-to-rgb(#b3e5fc),
147+
--palette-primary-200: mat-css-utilities.hex-to-rgb(#81d4fa),
148+
--palette-primary-300: mat-css-utilities.hex-to-rgb(#4fc3f7),
149+
--palette-primary-400: mat-css-utilities.hex-to-rgb(#29b6f6),
150+
--palette-primary-500: mat-css-utilities.hex-to-rgb(#03a9f4),
144151
// ...
145152
)
146153
);
147154
148-
@include init-material-css-vars();
155+
@include mat-css-main.init-material-css-vars();
149156
150157
```
151158
@@ -160,14 +167,18 @@ See the Material guide on [Theming your custom component](https://material.angul
160167
## Font config
161168
If needed the typography can be adjusted as well.
162169
```scss
170+
@use 'angular-material-css-vars/main' as mat-css-main;
171+
@use '@angular/material' as mat;
172+
@use '@angular/material/theming' as mat-theming;
173+
163174
// example
164-
$custom-typography: mat-typography-config(
175+
$custom-typography: mat.mat-typography-config(
165176
$font-family: 'Roboto, monospace',
166-
$headline: mat-typography-level(32px, 48px, 700),
167-
$body-1: mat-typography-level(16px, 24px, 500)
177+
$headline: mat-theming.mat-typography-level(32px, 48px, 700),
178+
$body-1: mat-theming.mat-typography-level(16px, 24px, 500)
168179
);
169180
170-
@include init-material-css-vars($typography-config: $custom-typography) {
181+
@include mat-css-main.init-material-css-vars($typography-config: $custom-typography) using($mat-css-theme) {
171182
@include app-theme($mat-css-theme);
172183
};
173184
```
@@ -182,19 +193,19 @@ With Angular v15, you can decide whether to keep using the legacy components, or
182193
No further action is required, just import the `init-material-css-vars` mixin like before:
183194
184195
```scss
185-
@import "angular-material-css-vars/main";
196+
@use 'angular-material-css-vars/main' as mat-css-main;
186197
187-
@include init-material-css-vars;
198+
@include mat-css-main.init-material-css-vars;
188199
```
189200
190201
### Keep using the legacy components
191202
192203
Please pass the following configuration to the mixin:
193204
194205
```scss
195-
@import "angular-material-css-vars/main";
206+
@use 'angular-material-css-vars/main' as mat-css-main;
196207
197-
@include init-material-css-vars($load-legacy-components: true, $load-mdc-components: false);
208+
@include mat-css-main.init-material-css-vars($load-legacy-components: true, $load-mdc-components: false);
198209
```
199210
200211
### Use legacy and MDC based components in parallel
@@ -204,9 +215,9 @@ Please pass the following configuration to the mixin:
204215
Please pass the following configuration to the mixin:
205216
206217
```scss
207-
@import "angular-material-css-vars/main";
218+
@use 'angular-material-css-vars/main' as mat-css-main;
208219
209-
@include init-material-css-vars($load-legacy-components: true, $load-mdc-components: true);
220+
@include mat-css-main.init-material-css-vars($load-legacy-components: true, $load-mdc-components: true);
210221
```
211222
212223
## upgrading to angular v12
Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// used to circumvent node sass issue: https://github.com/sass/node-sass/issues/2251
2-
@function _mat-css-rgb($r, $g: null, $b: null ) {
2+
@function rgb($r, $g: null, $b: null ) {
33
@if ($g == null) {
44
@return unquote('#{$r}');
55
}
@@ -12,7 +12,7 @@
1212
}
1313

1414
// used to circumvent node sass issue: https://github.com/sass/node-sass/issues/2251
15-
@function _mat-css-rgba($r, $g, $b: null, $a: null ) {
15+
@function rgba($r, $g, $b: null, $a: null ) {
1616
@if ($b == null) {
1717
@return unquote('#{$r}, #{$g}');
1818
}
@@ -24,31 +24,17 @@
2424
@error "wrong number of arguments";
2525
}
2626

27-
@function _mat-css-str-replace($string, $search, $replace: '') {
27+
@function str-replace($string, $search, $replace: '') {
2828
$index: str-index($string, $search);
2929

3030
@if $index {
31-
@return str-slice($string, 1, $index - 1) + $replace + _mat-css-str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
31+
@return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
3232
}
3333

3434
@return $string;
3535
}
3636

37-
@function _mat-css-hex-to-rgb($color) {
38-
@if ($color == null) {
39-
@return null;
40-
}
41-
@return red($color), green($color), blue($color);
42-
}
43-
44-
@function _mat-css-hex-to-rgba($color) {
45-
@if ($color == null) {
46-
@return null;
47-
}
48-
@return red($color), green($color), blue($color), alpha($color);
49-
}
50-
51-
@mixin _mat-css-root($varMap: null) {
37+
@mixin root($varMap: null) {
5238
@at-root :root {
5339
@each $varName, $varValue in $varMap {
5440
@if (type_of($varValue)==string) {
@@ -60,17 +46,17 @@
6046
}
6147
}
6248

63-
@function _mat-css-strip-variable($color) {
49+
@function strip-variable($color) {
6450
$var: $color;
6551
@if (str-index($var, 'rgba(')) {
66-
$var: _mat-css-str-replace($var, 'rgba(', '');
52+
$var: str-replace($var, 'rgba(', '');
6753
}
6854
@if (str-index($var, 'rgb(')) {
69-
$var: _mat-css-str-replace($var, 'rgb(', '');
55+
$var: str-replace($var, 'rgb(', '');
7056
}
7157
@if (str-index($var, 'var(')) {
72-
$var: _mat-css-str-replace($var, ')', ''); // remove excess ")"
58+
$var: str-replace($var, ')', ''); // remove excess ")"
7359
$var: $var + ')';
7460
}
7561
@return $var;
76-
}
62+
}
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
@use '@angular/material'as mat;
2-
@import '@angular/material/theming';
3-
@import 'variables';
4-
@import 'internal-helper';
5-
@import 'public-util';
2+
@use 'internal-helper' as helper;
3+
@use '@angular/material/theming' as theming;
4+
@use 'variables' as variables;
5+
@use 'public-util';
66
// contains main overwrite of `mat-color` to make it all work
77
// needs to come after '~@angular/material/theming'
8-
@import 'mat-lib-overwrites';
8+
@use 'mat-lib-overwrites' as overwrites;
99

1010
@mixin init-css-vars($default-theme, $text) {
1111
// init css variables
12-
@include _mat-css-root($text);
13-
@include _mat-css-root($default-theme);
12+
@include helper.root($text);
13+
@include helper.root($default-theme);
1414
}
1515

1616
@mixin init-mat-theme($dark-theme-selector, $typography-config: mat.define-typography-config(), $load-mdc-components, $load-legacy-components) {
@@ -21,17 +21,17 @@
2121
@include mat.legacy-core();
2222
}
2323

24-
$primary: mat.define-palette($mat-css-palette-primary);
25-
$accent: mat.define-palette($mat-css-palette-accent);
26-
$warn: mat.define-palette($mat-css-palette-warn);
24+
$primary: mat.define-palette(variables.$palette-primary);
25+
$accent: mat.define-palette(variables.$palette-accent);
26+
$warn: mat.define-palette(variables.$palette-warn);
2727

2828
$theme: mat.define-light-theme((
2929
color: (
3030
primary: $primary,
3131
accent: $accent,
3232
warn: $warn,
33-
foreground: $mat-css-palette-foreground,
34-
background: $mat-css-palette-background
33+
foreground: variables.$palette-foreground,
34+
background: variables.$palette-background
3535
),
3636
typography: $typography-config,
3737
density: 0
@@ -42,15 +42,15 @@
4242
primary: $primary,
4343
accent: $accent,
4444
warn: $warn,
45-
foreground: $mat-css-palette-foreground-dark,
46-
background: $mat-css-palette-background-dark
45+
foreground: variables.$palette-foreground-dark,
46+
background: variables.$palette-background-dark
4747
),
4848
typography: $typography-config,
4949
density: 0
5050
));
5151

5252
// set global variable so passed-in content can use the theme
53-
$mat-css-theme: $theme !global;
53+
$mat-css-theme: $theme !global; // stays for backwards-compatibility
5454

5555
// NOTE: light theme is the default theme
5656
@if $load-mdc-components {
@@ -59,11 +59,11 @@
5959
@if $load-legacy-components {
6060
@include mat.all-legacy-component-themes($theme);
6161
}
62-
62+
6363
// Fix mat-typography class, see https://github.com/angular/components/issues/26184
6464
@include mat.typography-hierarchy($theme);
6565

66-
@content;
66+
@content($mat-css-theme);
6767

6868
@if $dark-theme-selector {
6969
$mat-css-theme: $dark-theme !global;
@@ -76,25 +76,25 @@
7676
}
7777
// add content passed in, which can use the $theme variable to apply the dark theme to
7878
// other theme mixins needed by the app
79-
@content;
79+
@content($mat-css-theme);
8080
}
8181
}
8282

83-
@include mat-css-other-overwrites;
83+
@include overwrites.other-overwrites;
8484

8585
$mat-css-theme: null !global;
8686
}
8787

8888
@mixin init-material-css-vars(
89-
$default-theme: $mat-css-default-light-theme,
90-
$dark-theme-selector: $mat-css-dark-theme-selector,
91-
$default-theme-text: $mat-css-text,
89+
$default-theme: variables.$default-light-theme,
90+
$dark-theme-selector: variables.$dark-theme-selector,
91+
$default-theme-text: variables.$text,
9292
$typography-config: mat.define-typography-config(),
9393
$load-mdc-components: true,
9494
$load-legacy-components: false,
9595
) {
9696
@include init-css-vars($default-theme, $default-theme-text);
97-
@include init-mat-theme($dark-theme-selector, $typography-config, $load-mdc-components, $load-legacy-components) {
98-
@content;
97+
@include init-mat-theme($dark-theme-selector, $typography-config, $load-mdc-components, $load-legacy-components) using ($mat-css-theme) {
98+
@content($mat-css-theme);
9999
}
100100
}

0 commit comments

Comments
 (0)