Last active
August 23, 2017 07:51
-
-
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
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
// 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