-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,80 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { debounce } from 'lodash'; | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch } from '@wordpress/data'; | ||
import { useEffect, useState } from '@wordpress/element'; | ||
|
||
export default function useAutohide( navElement, wrappedElementsContainer ) { | ||
const [ isWrapping, setIsWrapping ] = useState( false ); | ||
export default function useAutohide( clientId, innerBlocks, ref ) { | ||
const [ state, setState ] = useState( { | ||
isWrapping: false, | ||
visibilityMap: [], | ||
} ); | ||
|
||
useEffect( () => { | ||
if ( navElement && wrappedElementsContainer ) { | ||
let timeout; | ||
const handleResize = debounce( ( nav ) => { | ||
const { bottom } = nav.getBoundingClientRect(); | ||
|
||
const { bottom } = navElement.current.getBoundingClientRect(); | ||
const items = Array.from( nav.childNodes ); | ||
const visibilityMap = items.reduce( ( result, el ) => { | ||
const isHidden = el.getBoundingClientRect().y >= bottom; | ||
const [ , blockId ] = el.id.split( 'block-' ); | ||
|
||
function handleResize() { | ||
if ( timeout ) { | ||
window.cancelAnimationFrame( timeout ); | ||
} | ||
return { | ||
...result, | ||
[ blockId ]: isHidden, | ||
}; | ||
}, {} ); | ||
const hasWrappedElements = Object.values( visibilityMap ).some( | ||
( item ) => item.isHidden | ||
); | ||
|
||
timeout = window.requestAnimationFrame( () => { | ||
const items = Array.from( navElement.current.childNodes ); // i need to work in terms of react component | ||
// Using the rendered DOM nodes does not allow me to alter props. | ||
// I would also like to use react in order to move the components around. | ||
// I can do this by storing arrays of elements. | ||
// Moving the elements from the main container to the wrapped elements container is actually easier with react. cool. | ||
setState( { | ||
isWrapping: hasWrappedElements, | ||
visibilityMap, | ||
} ); | ||
}, 100 ); | ||
|
||
const hasWrappedElements = items.some( ( el ) => { | ||
const isBelowBottom = | ||
el.getBoundingClientRect().y >= bottom; | ||
useEffect( () => { | ||
const element = ref.current; | ||
|
||
// @todo: move this side effect elsewhere. | ||
el.classList.toggle( 'wrapped', isBelowBottom ); | ||
if ( ! element ) { | ||
return; | ||
} | ||
|
||
const elementCopy = el.cloneNode( true ); | ||
const { ownerDocument } = element; | ||
|
||
if ( isBelowBottom ) { | ||
wrappedElementsContainer.append( elementCopy ); | ||
} | ||
window.addEventListener( | ||
'resize', | ||
() => handleResize( element ), | ||
false | ||
); | ||
|
||
return isBelowBottom; | ||
} ); | ||
handleResize( element ); | ||
|
||
setIsWrapping( hasWrappedElements ); | ||
} ); | ||
} | ||
return () => { | ||
window.removeEventListener( 'resize', () => | ||
handleResize( element ) | ||
); | ||
}; | ||
}, [ innerBlocks ] ); | ||
|
||
window.addEventListener( 'resize', handleResize, false ); | ||
const updatedBlocks = innerBlocks.map( ( block ) => ( { | ||
...block, | ||
isHidden: state.visibilityMap[ block.clientId ], | ||
} ) ); | ||
|
||
handleResize(); | ||
console.log( updatedBlocks ); | ||
|
||
return () => { | ||
// remove resize listener | ||
if ( navElement ) { | ||
window.removeEventListener( 'resize', handleResize ); | ||
} | ||
}; | ||
} | ||
}, [ navElement, wrappedElementsContainer ] ); | ||
useDispatch( ( dispatch ) => | ||
dispatch( 'core/block-editor' ).replaceInnerBlocks( | ||
clientId, | ||
updatedBlocks, | ||
true | ||
) | ||
); | ||
|
||
return [ isWrapping ]; | ||
return state; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default function ( navItemsElement ) { | ||
if ( navItemsElement && null !== navItemsElement.current ) { | ||
const items = Array.from( navItemsElement.current.childNodes ); | ||
|
||
return window.getComputedStyle( items[ 0 ] ).height; | ||
} | ||
|
||
return 0; | ||
} |