Last active
August 5, 2016 18:07
-
-
Save gilbert/1d2f8d2389ba4e59b7c300dfb1be3ea3 to your computer and use it in GitHub Desktop.
Expand / Collapse Animation with Mithril v1.x
This file contains hidden or 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
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)', | |
} | |
} |
This file contains hidden or 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
// | |
// 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