Skip to content

Commit 3f03315

Browse files
committedAug 24, 2016
Generate theme classes for parent theme in sub theme's mixin
1 parent 2fd7ac9 commit 3f03315

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed
 

‎index.js

+4
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ module.exports = {
88

99
postprocessTree: function(type, tree) {
1010
return type === 'css' ? csso(tree) : tree;
11+
},
12+
13+
isDevelopingAddon: function() {
14+
return true;
1115
}
1216
};

‎lib/scss-preprocessor/broccoli-import-theme-components.js

+48-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ function Import(inputTree, options) {
1212
this.include = options.include;
1313
this.main = options.main;
1414
this.themeName = options.themeName || 'ui-base';
15-
this.themePrefix = options.themePrefix || 'ui-base';
15+
this.themePrefix = options.themePrefix || 'base';
16+
this.parentTheme = options.parentTheme;
1617
}
1718

1819
Import.prototype = Object.create(Filter.prototype);
@@ -53,19 +54,57 @@ Import.prototype.addThemeComponentClasses = function(content) {
5354
var inputPath = this.inputPaths[0];
5455
var importFiles = walkSync(inputPath, { globs: this.include });
5556
var themePrefix = this.themePrefix;
57+
var parentThemeComponentFiles = this.parentTheme ?
58+
this.parentTheme.toScssComponentFilesArray() : [];
5659

57-
var componentClasses = importFiles.reduce(function(string, filePath) {
58-
var name = path.basename(filePath).replace(themePrefix + '--', '').replace('.scss', '');
60+
var themeComponents = importFiles.map(function(filePath) {
61+
return path.basename(filePath).replace(themePrefix + '--', '').replace('.scss', '');
62+
});
5963

60-
return string += ' &--' + name + ' {\n @include ' + name + '($theme);\n }\n\n';
61-
}, '');
64+
var parentThemeComponents = parentThemeComponentFiles.map(function(filePath) {
65+
return filePath.replace('components\/', '').replace('.scss', '');
66+
});
6267

63-
var themeMixin = '@mixin ' + this.themeName + '($theme...) {\n' +
64-
' $theme: keywords($theme);\n\n' +
65-
componentClasses +
66-
'}';
68+
var themeClasses = parentThemeComponents.reduce(function(array, component) {
69+
if (themeComponents.indexOf(component) === -1) {
70+
var importStatement = ' &--' + component + ' {\n @include ' + component + '($theme);\n }\n';
71+
array = array.concat(importStatement);
72+
}
73+
74+
return array;
75+
}, []);
76+
77+
themeClasses = themeComponents.reduce(function(array, component) {
78+
var importStatement;
79+
80+
if (themePrefix === 'base') {
81+
importStatement = ' .' + component + ' {\n @include ' + component + '($theme);\n }\n';
82+
} else {
83+
importStatement = ' &--' + component + ' {\n @include ' + themePrefix + '--' + component + '($theme);\n }\n';
84+
}
85+
86+
return array.concat(importStatement);
87+
}, themeClasses);
88+
89+
var themeMixin = this.buildThemeMixin(themeClasses);
6790

6891
return themeMixin;
6992
};
7093

94+
Import.prototype.buildThemeMixin = function(themeClasses) {
95+
if (this.themePrefix === 'base') {
96+
return '@mixin ' + this.themeName + '($theme...) {\n' +
97+
' $theme: keywords($theme);\n\n' +
98+
' @at-root {\n' +
99+
themeClasses.join('\n') +
100+
' }\n' +
101+
'}';
102+
} else {
103+
return '@mixin ' + this.themeName + '($theme...) {\n' +
104+
' $theme: keywords($theme);\n\n' +
105+
themeClasses.join('\n') +
106+
'}';
107+
}
108+
};
109+
71110
module.exports = Import;

‎lib/scss-preprocessor/index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = function(tree, options) {
3131
main: themeMain,
3232
themeName: theme.name,
3333
themePrefix: theme.prefix,
34+
parentTheme: theme.parentTheme,
3435
include: [path.join(theme.name, componentsNamespace, '*.scss')]
3536
});
3637

@@ -62,8 +63,5 @@ module.exports = function(tree, options) {
6263
var scssTree = funnel(tree, { include: ['**/*.scss'] });
6364
scssTree = mergeTrees([scssTree].concat(themeTrees), { overwrite: true });
6465

65-
var stew = require('broccoli-stew');
66-
var loggedTree = stew.debug(scssTree, { name: 'derp' });
67-
68-
return loggedTree;
66+
return scssTree;
6967
};

0 commit comments

Comments
 (0)
Please sign in to comment.