PluginProbe
Extendify / 0.10.0
Extendify v0.10.0
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Onboarding / hooks / useAnimatedHeight.js

useAnimatedHeight.js in Extendify 0.10.0, at src/Onboarding/hooks/useAnimatedHeight.js

44 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useEffect } from '@wordpress/element'
2
3 export const useAnimatedHeight = (preview, blockHeight, ready) => {
4 useEffect(() => {
5 let raf1, raf2
6 if (!preview.current) return
7 const p = preview.current
8 const iframe = p?.querySelector('iframe[title]')
9 if (!iframe) return
10 const content = iframe.contentWindow.document.body
11 const scale = preview.offsetWidth / 1400
12 iframe.style.maxHeight = `${blockHeight / scale}px`
13
14 const handleIn = () => {
15 // The live component changes over time so easier to query on demand
16 const height = content.offsetHeight
17 content.style.transitionDuration = Math.max(height * 3, 3000) + 'ms'
18 raf1 = window.requestAnimationFrame(() => {
19 content.style.top = Math.abs(height - blockHeight) * -1 + 'px'
20 })
21 }
22 const handleOut = () => {
23 const height = content.offsetHeight
24 content.style.transitionDuration = height / 1.5 + 'ms'
25 raf2 = window.requestAnimationFrame(() => {
26 content.style.top = 0
27 })
28 }
29
30 p.addEventListener('focus', handleIn)
31 p.addEventListener('mouseenter', handleIn)
32 p.addEventListener('blur', handleOut)
33 p.addEventListener('mouseleave', handleOut)
34 return () => {
35 window.cancelAnimationFrame(raf1)
36 window.cancelAnimationFrame(raf2)
37 p.removeEventListener('focus', handleIn)
38 p.removeEventListener('mouseenter', handleIn)
39 p.removeEventListener('blur', handleOut)
40 p.removeEventListener('mouseleave', handleOut)
41 }
42 }, [blockHeight, preview, ready])
43 }
44