-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Theming Parameters via CSS Variables
- Loading branch information
Showing
9 changed files
with
248 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
let engineInfo; | ||
module.exports.getInfo = function() { | ||
if (!engineInfo) { | ||
const {name, version} = require("../package.json"); | ||
engineInfo = {name, version}; | ||
} | ||
return engineInfo; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const engine = require("../engine"); | ||
|
||
// match a CSS url | ||
// (taken from sap/ui/core/theming/Parameters.js) | ||
const rCssUrl = /url[\s]*\('?"?([^\'")]*)'?"?\)/; | ||
|
||
module.exports = { | ||
addInlineParameters({result, options}) { | ||
// CSS Variables can only be added when the library name is known | ||
if (typeof options.library !== "object" || typeof options.library.name !== "string") { | ||
return; | ||
} | ||
|
||
const libraryNameDashed = options.library.name.replace(/\./g, "-"); | ||
const themeMetadata = this.getThemeMetadata({result, options}); | ||
|
||
const themeMetadataVariable = ` | ||
:root { | ||
--sapThemeMetaData-UI5-${libraryNameDashed}: ${JSON.stringify(themeMetadata)}; | ||
} | ||
`; | ||
|
||
const urlVariables = this.getUrlVariables({result}); | ||
const themingCssVariables = this.getThemingCssVariables({result}); | ||
|
||
const themingParameters = ` | ||
/* Inline theming parameters (CSS Variables) */ | ||
:root { | ||
--sapUiTheme-${libraryNameDashed}: ${JSON.stringify(urlVariables)}; | ||
${themingCssVariables} | ||
} | ||
`; | ||
|
||
result.css += themeMetadataVariable + themingParameters; | ||
if (options.rtl) { | ||
result.cssRtl += themeMetadataVariable + themingParameters; | ||
} | ||
}, | ||
getThemeMetadata({result, options}) { | ||
let scopes; | ||
if (typeof result.variables.scopes === "object") { | ||
scopes = Object.keys(result.variables.scopes); | ||
} else { | ||
scopes = []; | ||
} | ||
|
||
const libraryNameSlashed = options.library.name.replace(/\./g, "/"); | ||
|
||
// TODO: How to get theme name? .theming "sId"? parse from file path? new parameter? | ||
const themeId = "<theme-name>"; | ||
// TODO: How to get base theme name(s)? Read from .theming? | ||
const Extends = ["<base-theme>"]; | ||
|
||
const {version, name} = engine.getInfo(); | ||
return { | ||
Path: `UI5.${libraryNameSlashed}.${themeId}.library`, | ||
PathPattern: "/%frameworkId%/%libId%/themes/%themeId%/%fileId%.css", | ||
Extends, | ||
Scopes: scopes, | ||
Engine: { | ||
Version: version, | ||
Name: name | ||
}, | ||
Version: { | ||
Build: "<TODO>", // TOOD: add new property options.library.version | ||
Source: "<TODO>" // TOOD: add new property options.library.version | ||
} | ||
}; | ||
}, | ||
getUrlVariables({result}) { | ||
const urlVariables = {}; | ||
|
||
// TODO: support scopes (default/scopes top-level properties, see runtime code) | ||
Object.entries(result.variables).map(([name, value]) => { | ||
if (rCssUrl.test(value)) { | ||
urlVariables[name] = value; | ||
} | ||
}); | ||
|
||
return urlVariables; | ||
}, | ||
getThemingCssVariables({result}) { | ||
return Object.entries(result.variables).map(([name, value]) => { | ||
return ` --${name}: ${value};`; | ||
}).join("\n"); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const themingParameters = { | ||
addInlineParameters({result, options}) { | ||
switch (options.themingParameters) { | ||
case themingParameters.STRATEGY.DATA_URI: | ||
require("./dataUri").addInlineParameters({result, options}); | ||
break; | ||
case themingParameters.STRATEGY.CSS_VARIABLES: | ||
require("./cssVariables").addInlineParameters({result, options}); | ||
break; | ||
} | ||
return result; | ||
}, | ||
STRATEGY: { | ||
DATA_URI: "DATA_URI", | ||
CSS_VARIABLES: "CSS_VARIABLES" | ||
} | ||
}; | ||
|
||
module.exports = themingParameters; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* eslint-env mocha */ | ||
|
||
const assert = require("assert"); | ||
const sinon = require("sinon"); | ||
const mock = require("mock-require"); | ||
|
||
describe("themingParameters/cssVariables", function() { | ||
let themingParametersCssVariables; | ||
|
||
before(() => { | ||
mock("../../../lib/engine", { | ||
getInfo: sinon.stub().returns({ | ||
name: "less-openui5", | ||
version: "1.0.0-test" | ||
}) | ||
}); | ||
themingParametersCssVariables = require("../../../lib/themingParameters/cssVariables"); | ||
}); | ||
after(() => { | ||
mock.stopAll(); | ||
}); | ||
|
||
it("should not add theming parameters when library name is missing", function() { | ||
const result = {}; | ||
const options = {}; | ||
const returnValue = themingParametersCssVariables.addInlineParameters({result, options}); | ||
|
||
assert.equal(returnValue, undefined, "nothing should be returned"); | ||
assert.deepEqual(result, {}, "result object should not be modified"); | ||
}); | ||
|
||
it("should add theming parameters to css", function() { | ||
const result = { | ||
css: "/* css */", | ||
variables: {foo: "bar"} | ||
}; | ||
const options = { | ||
library: { | ||
name: "sap.ui.test" | ||
} | ||
}; | ||
const expectedMetadata = { | ||
Path: `UI5.sap/ui/test.<theme-name>.library`, // TODO: theme name placeholder | ||
PathPattern: "/%frameworkId%/%libId%/themes/%themeId%/%fileId%.css", | ||
Extends: ["<base-theme>"], // TODO: base theme placeholder | ||
Scopes: [], | ||
Engine: { | ||
Version: "1.0.0-test", | ||
Name: "less-openui5" | ||
}, | ||
Version: { | ||
Build: "<TODO>", | ||
Source: "<TODO>" | ||
} | ||
}; | ||
|
||
const returnValue = themingParametersCssVariables.addInlineParameters({result, options}); | ||
|
||
assert.equal(returnValue, undefined, "nothing should be returned"); | ||
assert.deepEqual(result, { | ||
variables: {foo: "bar"}, | ||
css: `/* css */ | ||
:root { | ||
--sapThemeMetaData-UI5-sap-ui-test: ${JSON.stringify(expectedMetadata)}; | ||
} | ||
/* Inline theming parameters (CSS Variables) */ | ||
:root { | ||
--sapUiTheme-sap-ui-test: {}; | ||
--foo: bar; | ||
} | ||
` | ||
}, "result.css should be enhanced"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters