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

Commit b294cf0

Browse files
committed
fix(theme, colors): md-colors demo was broken. improve JSDoc and fix lint issues
- 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 5891eff commit b294cf0

File tree

7 files changed

+60
-40
lines changed

7 files changed

+60
-40
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: 50 additions & 30 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,
@@ -361,7 +379,8 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
361379
/**
362380
* @ngdoc method
363381
* @name $mdThemingProvider#setDefaultTheme
364-
* @param {string} theme Default theme name to be applied to elements. Default value is `default`.
382+
* @param {string} theme Default theme name to be applied to elements.
383+
* Default value is `default`.
365384
*/
366385
setDefaultTheme: function(theme) {
367386
defaultTheme = theme;
@@ -565,7 +584,7 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
565584
Object.keys(color.hues).map(function(key) {
566585
return color.hues[key];
567586
}).forEach(function(hueValue) {
568-
if (VALID_HUE_VALUES.indexOf(hueValue) == -1) {
587+
if (VALID_HUE_VALUES.indexOf(hueValue) === -1) {
569588
throw new Error("Invalid hue value '%1' in theme %2's %3 color %4. Available hue values: %5"
570589
.replace('%1', hueValue)
571590
.replace('%2', self.name)
@@ -993,9 +1012,8 @@ function parseRules(theme, colorType, rules) {
9931012
rules = rules.replace(/THEME_NAME/g, theme.name);
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;
998-
var palette = PALETTES[color.name];
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;
9991017
var defaultBgHue = theme.colors['background'].hues['default'];
10001018
var defaultBgContrastType = PALETTES[theme.colors['background'].name][defaultBgHue].contrastType;
10011019

@@ -1005,20 +1023,20 @@ function parseRules(theme, colorType, rules) {
10051023
rules = rules.replace(simpleVariableRegex, function(match, colorType, hue, contrast, opacity) {
10061024
var regexColorType = colorType;
10071025
if (colorType === 'foreground') {
1008-
if (hue == 'shadow') {
1026+
if (hue === 'shadow') {
10091027
return theme.foregroundShadow;
10101028
} else if (theme.foregroundPalette[hue]) {
10111029
// Use user defined palette number (ie: foreground-2)
1012-
return rgba( colorToRgbaArray( theme.foregroundPalette[hue] ) );
1030+
return rgba(colorToRgbaArray(theme.foregroundPalette[hue]));
10131031
} else if (theme.foregroundPalette['1']){
1014-
return rgba( colorToRgbaArray( theme.foregroundPalette['1'] ) );
1032+
return rgba(colorToRgbaArray(theme.foregroundPalette['1']));
10151033
}
10161034
// Default to background-default-contrast-{opacity}
10171035
colorType = 'background';
10181036
contrast = 'contrast';
10191037
if (!opacity && hue) {
1020-
// Convert references to legacy hues to opacities (ie: foreground-4 to *-divider)
1021-
switch(hue) {
1038+
// Convert references to legacy hues to opacities (i.e. foreground-4 to *-divider)
1039+
switch (hue) {
10221040
// hue-1 uses default opacity
10231041
case '2':
10241042
opacity = 'secondary';
@@ -1042,7 +1060,8 @@ function parseRules(theme, colorType, rules) {
10421060
var colorDetails = (PALETTES[ theme.colors[colorType].name ][hue] || '');
10431061

10441062
// If user has specified a foreground color, use those
1045-
if (colorType === 'background' && contrast && regexColorType !== 'foreground' && colorDetails.contrastType == defaultBgContrastType) {
1063+
if (colorType === 'background' && contrast && regexColorType !== 'foreground' &&
1064+
colorDetails.contrastType === defaultBgContrastType) {
10461065
// Don't process if colorType was changed
10471066
switch (opacity) {
10481067
case 'secondary':
@@ -1077,8 +1096,6 @@ function parseRules(theme, colorType, rules) {
10771096
return rgba(colorDetails[contrast ? 'contrast' : 'value'], opacity);
10781097
});
10791098

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

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

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

11051122
newRule = newRule.replace(themeRuleRegex, function(match, start, end) {
11061123
return match + ', ' + start + end;
@@ -1134,7 +1151,7 @@ function generateAllThemes($injector, $mdTheming) {
11341151

11351152
// Break the CSS into individual rules
11361153
var rules = themeCss
1137-
.split(/\}(?!(\}|'|"|;))/)
1154+
.split(/}(?!([}'";]))/)
11381155
.filter(function(rule) { return rule && rule.trim().length; })
11391156
.map(function(rule) { return rule.trim() + '}'; });
11401157

@@ -1178,9 +1195,12 @@ function generateAllThemes($injector, $mdTheming) {
11781195
// Internal functions
11791196
// *************************
11801197

1181-
// The user specifies a 'default' contrast color as either light or dark,
1182-
// then explicitly lists which hues are the opposite contrast (eg. A100 has dark, A200 has light)
1183-
function sanitizePalette(palette, name) {
1198+
/**
1199+
* The user specifies a 'default' contrast color as either light or dark, then explicitly lists
1200+
* which hues are the opposite contrast (eg. A100 has dark, A200 has light).
1201+
* @param {!object} palette to sanitize
1202+
*/
1203+
function sanitizePalette(palette) {
11841204
var defaultContrast = palette.contrastDefaultColor;
11851205
var lightColors = palette.contrastLightColors || [];
11861206
var strongLightColors = palette.contrastStrongLightColors || [];
@@ -1207,7 +1227,7 @@ function generateAllThemes($injector, $mdTheming) {
12071227
return 'light';
12081228
}
12091229
function getContrastColor(contrastType) {
1210-
switch(contrastType) {
1230+
switch (contrastType) {
12111231
default:
12121232
case 'strongLight':
12131233
return STRONG_LIGHT_CONTRAST_COLOR;
@@ -1218,7 +1238,7 @@ function generateAllThemes($injector, $mdTheming) {
12181238
}
12191239
}
12201240
function getOpacityValues(contrastType) {
1221-
switch(contrastType) {
1241+
switch (contrastType) {
12221242
default:
12231243
case 'strongLight':
12241244
return STRONG_LIGHT_CONTRAST_OPACITY;
@@ -1294,13 +1314,13 @@ function checkValidPalette(theme, colorType) {
12941314
}
12951315

12961316
function colorToRgbaArray(clr) {
1297-
if (angular.isArray(clr) && clr.length == 3) return clr;
1317+
if (angular.isArray(clr) && clr.length === 3) return clr;
12981318
if (/^rgb/.test(clr)) {
12991319
return clr.replace(/(^\s*rgba?\(|\)\s*$)/g, '').split(',').map(function(value, i) {
1300-
return i == 3 ? parseFloat(value, 10) : parseInt(value, 10);
1320+
return i === 3 ? parseFloat(value) : parseInt(value, 10);
13011321
});
13021322
}
1303-
if (clr.charAt(0) == '#') clr = clr.substring(1);
1323+
if (clr.charAt(0) === '#') clr = clr.substring(1);
13041324
if (!/^([a-fA-F0-9]{3}){1,2}$/g.test(clr)) return;
13051325

13061326
var dig = clr.length / 3;
@@ -1318,7 +1338,7 @@ function colorToRgbaArray(clr) {
13181338
function rgba(rgbArray, opacity) {
13191339
if (!rgbArray) return "rgb('0,0,0')";
13201340

1321-
if (rgbArray.length == 4) {
1341+
if (rgbArray.length === 4) {
13221342
rgbArray = angular.copy(rgbArray);
13231343
opacity ? rgbArray.pop() : opacity = rgbArray.pop();
13241344
}

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)