Show HN: Moos.app – Interactive animated experiences for the web
moos.app102 pointsby thomasikzelf30 comments
badge.textContent = count > 99? '99+' : count
badge.classList.toggle('show', count > 0)
paper.classList.toggle('show', count > 0)
fire.classList.toggle('show', count > 99)
The declarative example also misses the 99+ case. I don't think this example describes the difference between imperative and declarative well. <select id=dropdown>
<option value=a>a</option>
<option value=b>b</option>
</select>
<select id=a style="display: none">...</select>
<select id=b style="display: none">...</select>
<script>
const $ = name => document.querySelector(name)
$('#dropdown').addEventListener('change', ev => {
$('#a').style.display = ev.target.value == "a"? "block" : "none"
$('#b').style.display = ev.target.value == "b"? "block" : "none"
}
</script>
vs const [showing, setShowing] = useState(null)
const handleChange = ev => setShowing(ev.target.value)
let other
if(showing == "a") other = <select>...</select>
if(showing == "b") other = <select>...</select>
return <>
<select
<option value=a>a</option>
<option value=b>b</option>
</select>
{other}
</>
some notes: const tag = (tagName, props, ...children) => {
const el = document.createElement(tagName)
for(let k in props) el.setAttribute(k, props[k])
for(let child of children)
el.appendChild(typeof child == 'string'? document.createTextNode(child) : child)
return el
}
This makes the construction of new elments a bit more concise. The rest is just functions, manually adding or removing classes and adding or removing nodes via the direct DOM API. const createSpecialButton = text => tag("button", {style: "background: purple"}, text)
When something becomes a framework is a bit blurry. I consider this more of an utility function. It is only 7 lines long. You call it, it does not call you. It gives you back a concrete element, not some abstract intermediate value. It is completely optional. The amount of these utilities you need in a big project is still tiny.