Skip to content

Instantly share code, notes, and snippets.

@Raynos

Raynos/x.js Secret

Last active December 21, 2015 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/a0e5175751b28da4d7cc to your computer and use it in GitHub Desktop.
Save Raynos/a0e5175751b28da4d7cc to your computer and use it in GitHub Desktop.
module.exports = row
// row("1 3", ["div", "One fourth"], ["div", "Three fourths"])
// row("1 1 1", ["div", "first column"], ["div", "second column"], ["div, "third column"])
function row(pattern) {
var args = [].slice.call(arguments, 1)
var parts = pattern.split(" ").map(function (part) {
var number = parseInt(part, 10)
return !isNaN(number) ? number : part
})
var total = parts.reduce(function (a, b) { return a + b })
return grid(parts.map(function (part, index) {
return col(part, total, {
first: index === 0,
last: index === parts.length - 1,
content: args[index]
})
}))
}
function grid(children) {
return ["div", {
style: {
background: "transparent",
margin: "0 0 15px 0"
}
}, children]
}
function col(part, total, opts) {
var width = String(100 * (part / total)) + "%"
return ["div", {
style: {
float: "left",
paddingLeft: opts.first || opts.last ? "0" : "7.5px",
paddingRight: opts.last ? "0" : "7.5px",
width: width
}
}, [ opts.content ]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment