-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
only make order summary sticky when it's not longer than view (#47680)
* only sticky summary when it's not longer than view * Add changefile(s) from automation for the following project(s): woocommerce-blocks * fix tests * correctly check for height * switch how to get top * remove from editor --------- Co-authored-by: github-actions <github-actions@github.com>
- Loading branch information
Showing
8 changed files
with
129 additions
and
5 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
93 changes: 93 additions & 0 deletions
93
plugins/woocommerce-blocks/assets/js/base/hooks/use-observed-viewport.ts
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,93 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useState, useRef, useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Returns a ref, its dimensions, and its visible viewport dimensions. Useful to know if an element should be sticky or not. This hook only runs when an element changes its intersection or dimensions. | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* const App = () => { | ||
* const [ observedRef, observedElement, viewWindow ] = useObservedViewport(); | ||
* | ||
* return ( | ||
* <MyElement ref={ observedRef } className={ observedElement.height < viewWindow.height ? 'is-sticky': '' } /> | ||
* ); | ||
* }; | ||
* ``` | ||
*/ | ||
export function useObservedViewport< T extends HTMLElement >(): [ | ||
React.Ref< T >, | ||
{ height: number; width: number }, | ||
{ height: number; width: number } | ||
] { | ||
const [ observedElement, setObservedElement ] = useState( { | ||
height: 0, | ||
width: 0, | ||
} ); | ||
|
||
const [ viewWindow, setViewWindow ] = useState( { | ||
height: 0, | ||
width: 0, | ||
} ); | ||
|
||
const observedRef = useRef< T >( null ); | ||
|
||
useEffect( () => { | ||
if ( ! observedRef.current ) { | ||
return; | ||
} | ||
const element = observedRef.current; | ||
const resizeObserver = new ResizeObserver( ( entries ) => { | ||
entries.forEach( ( entry ) => { | ||
if ( entry.target === element ) { | ||
const { height, width } = entry.contentRect; | ||
const elementTop = | ||
element.computedStyleMap().get( 'top' )?.toString() || | ||
'0'; | ||
setObservedElement( { | ||
height: height + parseInt( elementTop, 10 ), | ||
width, | ||
} ); | ||
} | ||
} ); | ||
} ); | ||
|
||
const intersectionObserver = new IntersectionObserver( | ||
( entries ) => { | ||
entries.forEach( ( entry ) => { | ||
const { height, width } = entry.boundingClientRect; | ||
setObservedElement( { height, width } ); | ||
if ( entry.target.ownerDocument.defaultView ) { | ||
setViewWindow( { | ||
height: entry.target.ownerDocument.defaultView | ||
?.innerHeight, | ||
width: entry.target.ownerDocument.defaultView | ||
?.innerWidth, | ||
} ); | ||
} | ||
} ); | ||
}, | ||
{ | ||
root: null, | ||
rootMargin: '0px', | ||
threshold: 1, | ||
} | ||
); | ||
|
||
resizeObserver.observe( element ); | ||
intersectionObserver.observe( element ); | ||
|
||
return () => { | ||
if ( ! element ) { | ||
return; | ||
} | ||
|
||
resizeObserver.unobserve( element ); | ||
intersectionObserver.unobserve( element ); | ||
}; | ||
}, [] ); | ||
return [ observedRef, observedElement, viewWindow ]; | ||
} |
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
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
4 changes: 4 additions & 0 deletions
4
plugins/woocommerce/changelog/47680-fix-only-sticky-summary-if-smaller-than-screen
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,4 @@ | ||
Significance: patch | ||
Type: fix | ||
Comment: This is a follow up to another PR not yet shipped. | ||
|