From 0b241db058d143a022269213c1d8494a36eb9e32 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Tue, 12 Apr 2022 20:57:03 +0530 Subject: [PATCH 1/4] Produce a mapping of aliases to resolved colors --- scripts/postcss/css-compile-variables.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/scripts/postcss/css-compile-variables.js b/scripts/postcss/css-compile-variables.js index fa584caa..654f1193 100644 --- a/scripts/postcss/css-compile-variables.js +++ b/scripts/postcss/css-compile-variables.js @@ -153,8 +153,13 @@ module.exports = (opts = {}) => { if (opts.compiledVariables){ populateMapWithDerivedVariables(opts.compiledVariables, cssFileLocation); } - // Publish both the base-variables and derived-variables to the other postcss-plugins - const combinedMap = new Map([...baseVariables, ...resolvedMap]); + // Also produce a mapping from alias to completely resolved color + const resolvedAliasMap = new Map(); + aliasMap.forEach((value, key) => { + resolvedAliasMap.set(key, resolvedMap.get(value)); + }); + // Publish the base-variables, derived-variables and resolved aliases to the other postcss-plugins + const combinedMap = new Map([...baseVariables, ...resolvedMap, ...resolvedAliasMap]); result.messages.push({ type: "resolved-variable-map", plugin: "postcss-compile-variables", From bb9954a36c0fb0fd2c0d0427c135e79556c818a7 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Tue, 12 Apr 2022 20:57:43 +0530 Subject: [PATCH 2/4] Let derive function know if theme is dark --- scripts/postcss/css-compile-variables.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/postcss/css-compile-variables.js b/scripts/postcss/css-compile-variables.js index 654f1193..cc822517 100644 --- a/scripts/postcss/css-compile-variables.js +++ b/scripts/postcss/css-compile-variables.js @@ -33,6 +33,7 @@ const valueParser = require("postcss-value-parser"); let aliasMap; let resolvedMap; let baseVariables; +let isDark; function getValueFromAlias(alias) { const derivedVariable = aliasMap.get(alias); @@ -78,7 +79,7 @@ function resolveDerivedVariable(decl, derive) { if (!value) { throw new Error(`Cannot derive from ${baseVariable} because it is neither defined in config nor is it an alias!`); } - const derivedValue = derive(value, operation, argument); + const derivedValue = derive(value, operation, argument, isDark); resolvedMap.set(wholeVariable, derivedValue); } } @@ -134,6 +135,8 @@ module.exports = (opts = {}) => { aliasMap = new Map(); resolvedMap = new Map(); baseVariables = new Map(); + isDark = false; + return { postcssPlugin: "postcss-compile-variables", @@ -143,6 +146,7 @@ module.exports = (opts = {}) => { // If this is a runtime theme, don't derive variables. return; } + isDark = cssFileLocation.includes("dark=true"); /* Go through the CSS file once to extract all aliases and base variables. We use these when resolving derived variables later. From 39bc827aaf095145aa973289e2f79a6355f1b168 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Tue, 12 Apr 2022 20:58:14 +0530 Subject: [PATCH 3/4] Invert operation for dark theme --- scripts/postcss/color.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/scripts/postcss/color.js b/scripts/postcss/color.js index f61dac1e..b1ef7073 100644 --- a/scripts/postcss/color.js +++ b/scripts/postcss/color.js @@ -16,8 +16,17 @@ limitations under the License. const offColor = require("off-color").offColor; -module.exports.derive = function (value, operation, argument) { +module.exports.derive = function (value, operation, argument, isDark) { const argumentAsNumber = parseInt(argument); + if (isDark) { + // For dark themes, invert the operation + if (operation === 'darker') { + operation = "lighter"; + } + else if (operation === 'lighter') { + operation = "darker"; + } + } switch (operation) { case "darker": { const newColorString = offColor(value).darken(argumentAsNumber / 100).hex(); From efef7147afe50a4ce84f305bdf833481270ef129 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Tue, 12 Apr 2022 21:02:30 +0530 Subject: [PATCH 4/4] Modify jsdoc comment --- scripts/postcss/css-compile-variables.js | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/postcss/css-compile-variables.js b/scripts/postcss/css-compile-variables.js index cc822517..0f58a635 100644 --- a/scripts/postcss/css-compile-variables.js +++ b/scripts/postcss/css-compile-variables.js @@ -124,6 +124,7 @@ function populateMapWithDerivedVariables(map, cssFileLocation) { * @param {string} value - The base value on which an operation is applied * @param {string} operation - The operation to be applied (eg: darker, lighter...) * @param {string} argument - The argument for this operation + * @param {boolean} isDark - Indicates whether this theme is dark */ /** *