Skip to content

Instantly share code, notes, and snippets.

@gilbert
Last active August 5, 2016 18:07
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 gilbert/1d2f8d2389ba4e59b7c300dfb1be3ea3 to your computer and use it in GitHub Desktop.
Save gilbert/1d2f8d2389ba4e59b7c300dfb1be3ea3 to your computer and use it in GitHub Desktop.
Expand / Collapse Animation with Mithril v1.x
var animateHeight = {
oncreate: function (vnode) {
var height = vnode.dom.clientHeight
vnode.dom.style.maxHeight = 0
vnode.dom.offsetHeight // Trick to recalc layout
window.requestAnimationFrame( ()=> (vnode.dom.style.maxHeight = height+1+"px") )
},
onbeforeremove: function (vnode, done) {
vnode.dom.addEventListener('transitionend', done)
vnode.dom.style.maxHeight = 0
},
style: {
overflow: 'hidden',
transition: 'max-height 0.5s cubic-bezier(0.215, 0.610, 0.355, 1.000)',
}
}
//
// See it in action here: https://jsbin.com/juxixotake/1/edit?js,output
//
var flag = false;
var Example = {
view : function( component ){
return m('.example-component', [
flag
? m('div', animateHeight, sampleView())
: m('p', "Click the toggle button.")
,
m('button', { onclick: () => flag = !flag }, "Toggle")
]);
}
};
m.mount( document.body, Example);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment