Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Last active January 10, 2017 12:51
Show Gist options
  • Select an option

  • Save ishiduca/62f78e606128122e0e02573c738c81a3 to your computer and use it in GitHub Desktop.

Select an option

Save ishiduca/62f78e606128122e0e02573c738c81a3 to your computer and use it in GitHub Desktop.
rate-star-on-yo-yo-bind example yo-yo.js yo-yo-bind
const xtend = require('xtend')
const d = require('global/document')
const domcss = require('dom-css')
const app = require('yo-yo-bind')
const WHITESTAR = 'fa fa-star-o'
const BLACKSTAR = 'fa fa-star'
const reduce = (state, action, type) => {
if (type === 'CLEAR') {
return xtend(state, {rates: state.rates.map(r => WHITESTAR)})
}
if (type === 'SELECT') {
return xtend(state, {rates: state.rates.map(
(r, i) => (action >= i) ? BLACKSTAR : WHITESTAR
)})
}
if (type === 'MOUSE') {
return xtend(state, {mouseover: action})
}
return state
}
const star = (state, dispatch, rate, i) => css(app.html `
<span
onmouseover=${ev => dispatch('MOUSE', i)}
onmouseout=${ev => dispatch('MOUSE', null)}
>
<i class=${rate}></i>
</span>
`, {
backgroundColor: (state.mouseover && state.mouseover >= i) ? '#ffaaff' : '',
borderRadius: '.25em',
cursor: 'pointer'
})
const create = (state, dispatch) => app.html `
<main>
<sapn onclick=${ev => dispatch('CLEAR')}>x</span>
${state.rates.map((rate, i) => app.html `
<span onclick=${ev => dispatch('SELECT', i)}>
${star(state, dispatch, rate, i)}
</span>
`)}
</main>
`
d.body.appendChild(app(create, {
rates: [0,0,0,0,0].map(r => WHITESTAR),
mouseover: null
}, reduce))
function css (el, style) {
domcss(el, style)
return el
}
<!doctype html>
<head>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<title>Rate</title>
</head>
<body>
<script src="./bundle.js"></script>
</body>
{
"dependencies": {
"dom-css": "^2.1.0",
"xtend": "^4.0.1",
"yo-yo-bind": "git+https://github.com/ishiduca/yo-yo-bind.git"
},
"scripts": {
"build": "browserify app.js > bundle.js"
},
"browserify": {
"transform": [
"yo-yoify"
]
},
"devDependencies": {
"yo-yoify": "^3.5.0"
}
}
var yo = require('yo-yo')
module.exports = function bindApp (create, state, reduce) {
var dom = create(state, dispatch)
return dom
function dispatch (type, action) {
var whatIsThis = reduce(state, action, type)
if (typeof whatIsThis === 'function') {
whatIsThis(dispatch)
} else {
yo.update(dom, create((state = whatIsThis), dispatch))
}
}
}
module.exports.html = yo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment