2020-11-06 17:53:03 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
useCallback,
|
|
|
|
useState,
|
|
|
|
useRef,
|
|
|
|
useLayoutEffect,
|
|
|
|
} from '@wordpress/element';
|
|
|
|
|
|
|
|
export const Column = ( { children, shouldStick = false } ) => {
|
|
|
|
const [ isContentStuck, setIsContentStuck ] = useState( false );
|
|
|
|
const content = useRef( null );
|
|
|
|
const initialTop = useRef( null );
|
|
|
|
|
|
|
|
const maybeStickContent = useCallback( () => {
|
|
|
|
if ( ! content.current ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { bottom, top } = content.current.getBoundingClientRect();
|
|
|
|
|
|
|
|
if ( initialTop.current === null ) {
|
|
|
|
initialTop.current = top;
|
|
|
|
}
|
|
|
|
|
|
|
|
const shouldBeSticky = bottom < window.innerHeight;
|
|
|
|
|
|
|
|
if ( top === initialTop.current ) {
|
|
|
|
setIsContentStuck( shouldBeSticky );
|
|
|
|
}
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
useLayoutEffect( () => {
|
|
|
|
if ( ! shouldStick ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
maybeStickContent();
|
|
|
|
window.addEventListener( 'resize', maybeStickContent );
|
|
|
|
window.addEventListener( 'scroll', maybeStickContent );
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener( 'resize', maybeStickContent );
|
|
|
|
window.removeEventListener( 'scroll', maybeStickContent );
|
|
|
|
};
|
|
|
|
}, [ maybeStickContent, shouldStick ] );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="woocommerce-homescreen-column"
|
|
|
|
ref={ content }
|
|
|
|
style={ {
|
2020-11-11 17:19:24 +00:00
|
|
|
position: shouldStick && isContentStuck ? 'sticky' : 'static',
|
2020-11-06 17:53:03 +00:00
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ children }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|