UNPKG

@chief-editor/ui

Version:

UI Component for chief editor

1 lines 1.13 kB
import{presets as CubicBezier,animate}from"@co-hooks/util";import{useRef}from"react";export function useSecondChildExpandAnimation(e,t){var i=useRef(!1),h=t.disabled,n=t.duration,r=void 0===n?400:n,o=t.expanded,d=t.ignoreInitialRender,a=void 0===d||d;return function(){if(!(a&&!i.current||h)){var t=function(i){handleAnimationTick(e.current,t,i,o)};t.bodyHeight=0,t.headHeight=0;var n=animate("linear",r,t),d=n[0],g=n[1];return d(),function(){t(1),g()}}i.current=!0}}function handleAnimationTick(e,t,i,h){if(e){if(!t.headHeight||!t.bodyHeight){if(!e.firstElementChild||!e.lastElementChild)return;t.headHeight=e.firstElementChild.getBoundingClientRect().height,t.bodyHeight=e.lastElementChild.getBoundingClientRect().height}h?i>=.5?e.style.removeProperty("height"):(i=CubicBezier.easeOut(2*i),e.style.setProperty("height",i*t.bodyHeight+t.headHeight+"px")):i>=1?e.style.removeProperty("height"):i>=.5?(i=CubicBezier.easeIn(2*(1-i)),e.style.setProperty("height",i*t.bodyHeight+t.headHeight+"px")):e.style.getPropertyValue("height")!==t.bodyHeight+t.headHeight+"px"&&e.style.setProperty("height",t.bodyHeight+t.headHeight+"px")}}