Skip to content

Instantly share code, notes, and snippets.

@KiaraGrouwstra
Last active August 23, 2017 07:51
Show Gist options
  • Save KiaraGrouwstra/dc20136eb6ac3c1cc5560ddde6dfa5bd to your computer and use it in GitHub Desktop.
Save KiaraGrouwstra/dc20136eb6ac3c1cc5560ddde6dfa5bd to your computer and use it in GitHub Desktop.
Patch Style: get the CSS snippet needed to give one element the style of another
// get the CSS snippet needed to give one element the style of another
var R = require('ramda');
var getPx = (str) => /(\d+)\s*px/g.test(str) ? Number(str.replace(/[^\d]/g, '')) : null;
var px2rem = R.curry((base, str) => str.replace(/(\d+)\s*px/g, (match, px) => Number(px)/base + 'rem'));
var base = getPx(getComputedStyle(document.documentElement).fontSize);
var toRem = px2rem(base);
var patchStyle = (fromEl, toEl, asRem = false) => {
let [styles, stylesTo] = [fromEl, toEl].map(getComputedStyle);
let css = R.pipe(
R.values,
R.filter(k => styles[k] != stylesTo[k]),
R.map(k => [k, styles[k]]),
R.fromPairs,
R.map(v => asRem ? toRem(v) : v),
R.toPairs,
R.map(([k, v]) => ` ${k}: ${v};`),
R.join('\n') //,
)(styles);
return css; //`{\n${css}\n}`;
}
// var btns = Array.from(document.getElementsByTagName('button'));
// patchStyle(btns[11], btns[10]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment