Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit a3cf860

Browse files
committed
fix(theme, colors): fix lint errors and improve docs
- fix lint warning of unnecessary escape in regex and missing colon - resolve test failures after merge - fix URLs and icons in `md-colors` demos - clarify how to map the old foreground expressions in theming docs - update links to opacity requirements for icons
1 parent 9ec03ee commit a3cf860

File tree

7 files changed

+48
-30
lines changed

7 files changed

+48
-30
lines changed

src/components/colors/demoBasicUsage/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
<!-- Footnote -->
1818
<p class="footnote">
1919
Note: The <code>md-colors</code> directive allows pre-defined theme colors to be easily applied
20-
as CSS style properties. <code>md-colors</code> uses the Palettes defined at
21-
<a class="md-accent" href="https://material.io/archive/guidelines/style/color.html#">Material Design Colors</a>
22-
and the Themes defined using <code>$mdThemingProvider</code>. This feature is simply an
23-
extension of the <code>$mdTheming</code> features.
20+
as CSS style properties. <code>md-colors</code> uses the Palettes defined in the
21+
<a class="md-accent" href="https://material.io/archive/guidelines/style/color.html#color-color-palette">
22+
Material Design Colors</a> and the Themes defined using <code>$mdThemingProvider</code>.
23+
This feature is simply an extension of the <code>$mdTheming</code> features.
2424
</p>
2525
</div>

src/components/colors/demoBasicUsage/regularCard.tmpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<md-card-title>
33
<md-card-title-media>
44
<div class="md-media-sm card-media" layout>
5-
<md-icon md-svg-icon="person" style="color:grey"></md-icon>
5+
<md-icon md-svg-icon="social:person" style="color:grey"></md-icon>
66
</div>
77
</md-card-title-media>
88
<md-card-title-text>

src/components/colors/demoBasicUsage/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ angular.module('colorsDemo', ['ngMaterial'])
55
.accentPalette('green');
66

77
$mdIconProvider
8-
.defaultIconSet('img/icons/sets/social-icons.svg', 24);
8+
.iconSet('social', 'img/icons/sets/social-icons.svg', 24);
99
})
1010
.directive('regularCard', function () {
1111
return {

src/components/colors/demoBasicUsage/userCard.tmpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<md-card-title>
33
<md-card-title-media>
44
<div class="md-media-sm card-media" layout md-colors="::{background: '{{theme}}-accent'}">
5-
<md-icon md-svg-icon="person"></md-icon>
5+
<md-icon md-svg-icon="social:person"></md-icon>
66
</div>
77
</md-card-title-media>
88
<md-card-title-text>

src/components/colors/demoThemePicker/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ angular
33
.controller('ThemeDemoCtrl', function ($scope, $mdColorPalette) {
44
$scope.colors = Object.keys($mdColorPalette);
55

6-
$scope.mdURL = 'https://material.google.com/style/color.html#color-color-palette';
6+
$scope.mdURL = 'https://material.io/archive/guidelines/style/color.html#color-color-palette';
77
$scope.primary = 'purple';
88
$scope.accent = 'green';
99

src/core/services/theming/theming.js

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,26 @@ function detectDisabledThemes($mdThemingProvider) {
134134
* {{primary-contrast}} - Generates .md-hue-1, .md-hue-2, .md-hue-3 with configured contrast (ie. text) color shades set for each hue
135135
* {{primary-contrast-0.7}} - Apply 0.7 opacity to each of the above rules
136136
* {{primary-contrast-divider}} - Apply divider opacity to contrast color
137-
* {{background-default-contrast}} - Apply primary text color for contrasting with default background
138-
* {{background-50-contrast-icon}} - Apply contrast color for icon on background's shade 50 hue
139137
*
138+
* Foreground expansion: Applies rgba to black/white foreground text
139+
*
140+
* Old Foreground Expressions:
141+
* {{foreground-1}} - used for primary text
142+
* {{foreground-2}} - used for secondary text/divider
143+
* {{foreground-3}} - used for disabled text
144+
* {{foreground-4}} - used for dividers
145+
*
146+
* New Foreground Expressions:
147+
*
148+
* Apply primary text color for contrasting with default background
149+
* {{background-default-contrast}} - default opacity
150+
* {{background-default-contrast-secondary}} - opacity for secondary text
151+
* {{background-default-contrast-hint}} - opacity for hints and placeholders
152+
* {{background-default-contrast-disabled}} - opacity for disabled text
153+
* {{background-default-contrast-divider}} - opacity for dividers
154+
*
155+
* Apply contrast color for specific shades
156+
* {{background-50-contrast-icon}} - Apply contrast color for icon on background's shade 50 hue
140157
*/
141158

142159
// In memory generated CSS rules; registered by theme.name
@@ -190,9 +207,8 @@ var DARK_DEFAULT_HUES = {
190207
}
191208
};
192209

193-
// use inactive icon opacity from https://material.google.com/style/color.html#color-text-background-colors
194-
// not inactive icon opacity from https://material.google.com/style/icons.html#icons-system-icons
195-
210+
// Icon opacity values (active/inactive) from
211+
// https://material.io/archive/guidelines/style/icons.html#icons-system-icons
196212
var DARK_CONTRAST_OPACITY = {
197213
'icon': 0.54,
198214
'secondary': 0.54,
@@ -209,6 +225,8 @@ var LIGHT_CONTRAST_OPACITY = {
209225
'divider': 0.12
210226
};
211227

228+
// Icon opacity values (active/inactive) from
229+
// https://material.io/archive/guidelines/style/icons.html#icons-system-icons
212230
var STRONG_LIGHT_CONTRAST_OPACITY = {
213231
'icon': 1.0,
214232
'secondary': 0.7,
@@ -991,10 +1009,11 @@ function parseRules(theme, colorType, rules) {
9911009
checkValidPalette(theme, colorType);
9921010

9931011
rules = rules.replace(/THEME_NAME/g, theme.name);
1012+
var color = theme.colors[colorType];
9941013
var themeNameRegex = new RegExp('\\.md-' + theme.name + '-theme', 'g');
9951014
// Matches '{{ primary-color }}', etc
996-
var hueRegex = new RegExp('(?:\'|")?{{\\s*(' + colorType + ')-?(color|default)?-?(contrast)?-?((?:\\d\\.?\\d*)|(?:[a-zA-Z]+))?\\s*}}(\"|\')?','g');
997-
var simpleVariableRegex = /'?"?\{\{\s*([a-zA-Z]+)-(A?\d+|hue-[0-3]|shadow|default)-?(contrast)?-?((?:\d\.?\d*)|(?:[a-zA-Z]+))?\s*\}\}'?"?/g;
1015+
var hueRegex = new RegExp('(\'|")?{{\\s*([a-zA-Z]+)-?(color|default)?-?(contrast)?-?((?:\\d\\.?\\d*)|(?:[a-zA-Z]+))?\\s*}}("|\')?','g');
1016+
var simpleVariableRegex = /'?"?{{\s*([a-zA-Z]+)-(A?\d+|hue-[0-3]|shadow|default)-?(contrast)?-?((?:\d\.?\d*)|(?:[a-zA-Z]+))?\s*}}'?"?/g;
9981017
var palette = PALETTES[color.name];
9991018
var defaultBgHue = theme.colors['background'].hues['default'];
10001019
var defaultBgContrastType = PALETTES[theme.colors['background'].name][defaultBgHue].contrastType;
@@ -1005,20 +1024,20 @@ function parseRules(theme, colorType, rules) {
10051024
rules = rules.replace(simpleVariableRegex, function(match, colorType, hue, contrast, opacity) {
10061025
var regexColorType = colorType;
10071026
if (colorType === 'foreground') {
1008-
if (hue == 'shadow') {
1027+
if (hue === 'shadow') {
10091028
return theme.foregroundShadow;
10101029
} else if (theme.foregroundPalette[hue]) {
10111030
// Use user defined palette number (ie: foreground-2)
1012-
return rgba( colorToRgbaArray( theme.foregroundPalette[hue] ) );
1031+
return rgba(colorToRgbaArray(theme.foregroundPalette[hue]));
10131032
} else if (theme.foregroundPalette['1']){
1014-
return rgba( colorToRgbaArray( theme.foregroundPalette['1'] ) );
1033+
return rgba(colorToRgbaArray(theme.foregroundPalette['1']));
10151034
}
10161035
// Default to background-default-contrast-{opacity}
10171036
colorType = 'background';
10181037
contrast = 'contrast';
10191038
if (!opacity && hue) {
1020-
// Convert references to legacy hues to opacities (ie: foreground-4 to *-divider)
1021-
switch(hue) {
1039+
// Convert references to legacy hues to opacities (i.e. foreground-4 to *-divider)
1040+
switch (hue) {
10221041
// hue-1 uses default opacity
10231042
case '2':
10241043
opacity = 'secondary';
@@ -1042,7 +1061,8 @@ function parseRules(theme, colorType, rules) {
10421061
var colorDetails = (PALETTES[ theme.colors[colorType].name ][hue] || '');
10431062

10441063
// If user has specified a foreground color, use those
1045-
if (colorType === 'background' && contrast && regexColorType !== 'foreground' && colorDetails.contrastType == defaultBgContrastType) {
1064+
if (colorType === 'background' && contrast && regexColorType !== 'foreground' &&
1065+
colorDetails.contrastType === defaultBgContrastType) {
10461066
// Don't process if colorType was changed
10471067
switch (opacity) {
10481068
case 'secondary':
@@ -1077,8 +1097,6 @@ function parseRules(theme, colorType, rules) {
10771097
return rgba(colorDetails[contrast ? 'contrast' : 'value'], opacity);
10781098
});
10791099

1080-
// Matches '{{ primary-color }}', etc
1081-
var hueRegex = new RegExp('(\'|")?{{\\s*([a-zA-Z]+)-(color|contrast)-?(\\d\\.?\\d*)?\\s*}}("|\')?','g');
10821100
var generatedRules = [];
10831101

10841102
// For each type, generate rules for each hue (ie. default, md-hue-1, md-hue-2, md-hue-3)
@@ -1099,8 +1117,8 @@ function parseRules(theme, colorType, rules) {
10991117

11001118
// Don't apply a selector rule to the default theme, making it easier to override
11011119
// styles of the base-component
1102-
if (theme.name == 'default') {
1103-
var themeRuleRegex = /((?:\s|>|\.|\w|-|:|\(|\)|\[|\]|"|'|=)*)\.md-default-theme((?:\s|>|\.|\w|-|:|\(|\)|\[|\]|"|'|=)*)/g;
1120+
if (theme.name === 'default') {
1121+
var themeRuleRegex = /((?:\s|>|\.|\w|-|:|\(|\)|\[|]|"|'|=)*)\.md-default-theme((?:\s|>|\.|\w|-|:|\(|\)|\[|]|"|'|=)*)/g;
11041122

11051123
newRule = newRule.replace(themeRuleRegex, function(match, start, end) {
11061124
return match + ', ' + start + end;
@@ -1134,7 +1152,7 @@ function generateAllThemes($injector, $mdTheming) {
11341152

11351153
// Break the CSS into individual rules
11361154
var rules = themeCss
1137-
.split(/\}(?!(\}|'|"|;))/)
1155+
.split(/}(?!(}|'|"|;))/)
11381156
.filter(function(rule) { return rule && rule.trim().length; })
11391157
.map(function(rule) { return rule.trim() + '}'; });
11401158

@@ -1207,7 +1225,7 @@ function generateAllThemes($injector, $mdTheming) {
12071225
return 'light';
12081226
}
12091227
function getContrastColor(contrastType) {
1210-
switch(contrastType) {
1228+
switch (contrastType) {
12111229
default:
12121230
case 'strongLight':
12131231
return STRONG_LIGHT_CONTRAST_COLOR;
@@ -1218,7 +1236,7 @@ function generateAllThemes($injector, $mdTheming) {
12181236
}
12191237
}
12201238
function getOpacityValues(contrastType) {
1221-
switch(contrastType) {
1239+
switch (contrastType) {
12221240
default:
12231241
case 'strongLight':
12241242
return STRONG_LIGHT_CONTRAST_OPACITY;

src/core/services/theming/theming.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ describe('$mdThemingProvider', function() {
171171
function parse(str) {
172172
return themingProvider._parseRules(testTheme, 'primary', str)
173173
.join('')
174-
.split(/\}(?!(\}|'|"|;))/)
174+
.split(/}(?!(}|'|"|;))/)
175175
.filter(function(val) { return !!val; })
176176
.map(function(rule) {
177177
rule += '}';
178178
return {
179-
content: (rule.match(/\{\s*(.*?)\s*\}/) || [])[1] || null,
179+
content: (rule.match(/{\s*(.*?)\s*}/) || [])[1] || null,
180180
hue: (rule.match(/md-(hue-\d)/) || [])[1] || null,
181181
type: (rule.match(/(primary)/) || [])[1] || null
182182
};

0 commit comments

Comments
 (0)