Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
fix(Theme Contribution): Fix generation of contributed CSS variable
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ketch committed Mar 7, 2020
1 parent 3250c8b commit d31909e
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/demo/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,36 @@ export enum keys {
* Create an object consisting only of changed values
*
* @function diff
* @param {ThemeObject} original - Source object to compare against
* @param {ThemeObject} updated - New object with partially updated values
* @return {ThemeObject} Object containing keys with changed values
* @param {Object} original - Source object to compare against
* @param {Object} updated - New object with partially updated values
* @return {Object} Object containing keys with changed values
*/
export const diff = (
original: ThemeObject,
updated: ThemeObject
): ThemeObject => {
return Object.entries(updated).reduce((_diff: ThemeObject, [name, value]) => {
export const diff = <O extends Record<string, unknown>>(
original: O,
updated: O
): O => {
return Object.entries(updated).reduce((_diff: O, [name, value]) => {
return value === original[name]
? _diff
: { ..._diff, [name]: value === '' ? original[name] : value }
}, {})
}, {} as O)
}

/**
* Convert a JS object to a stringified CSS rule, using object keys as variable names.
*
* @function objToVars
* @param {Object} obj - JS Object, where the keys are variable names (without leading `--` dashes)
* @param {boolean} [compile] - If true, translates Stencila's Custom `:--root` Selector into `[data-itemscope='root']`
* @return {string} Stringified CSS styleesheet containing variable declarations
*/
export const objToVars = (obj: ThemeObject): string => {
export const objToVars = (obj: ThemeObject, compile = false): string => {
const vars = Object.entries(obj).reduce(
(vs: string, [name, value]) => vs + `--${name}: ${value};\n`,
(vs: string, [name, value]) => vs + ` --${name}: ${value};\n`,
''
)

return `${translate(':--root')} {
return `${compile ? ':--root' : translate(':--root')} {
${vars}}`
}

Expand Down Expand Up @@ -79,7 +84,7 @@ export const submitPR = (
const customisations =
Object.keys(diffs).length === 0
? '/* No changes were made to variables in the base theme but you can set them here if you like :) */\n'
: objToVars(diffs)
: objToVars(diffs, false)

const css = `@import "../${baseName}/styles.css";\n\n${customisations}\n`
const value = encodeURIComponent(css)
Expand Down

0 comments on commit d31909e

Please sign in to comment.