-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add callback function to AdvertisingSlot (#131)
## Description * Add a `onAdLoaded` callback function that gets triggered once an ad has been successfully loaded * Refactor `useIntersectionObserver` to its own hook * Remove `@storybook/addon-docs/blocks` that was causing issues with more recent version of node This PR will be merge to beta for further tests on production.
- Loading branch information
1 parent
5f023ff
commit fbeff4e
Showing
7 changed files
with
125 additions
and
70 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
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,71 @@ | ||
import { useRef } from 'react'; | ||
import calculateRootMargin from '../components/utils/calculateRootMargin'; | ||
import getLazyLoadConfig from '../components/utils/getLazyLoadConfig'; | ||
import { useIsomorphicLayoutEffect } from '../hooks/useIsomorphicLayoutEffect'; | ||
|
||
/** | ||
* Custom hook that creates an Intersection Observer to lazy load an element when it appears within the viewport. | ||
* | ||
* @param {Function} activate - Function to activate the advertisement when the intersection is observed. | ||
* @param {Object} config - Configuration object for the advertising context. | ||
* @param {string} id - ID of the advertising slot. | ||
* @param {Object} customEventHandlers - An object containing custom event handlers. | ||
* @param {Object} containerDivRef - Ref object to the element to be observed. | ||
* @param {boolean} isLazyLoadEnabled - Boolean to indicate if lazy loading is enabled. | ||
* | ||
* @example | ||
* | ||
* const containerDivRef = useRef(); | ||
* const { activate, config } = useContext(AdvertisingContext); | ||
* const isLazyLoadEnabled = isLazyLoading(getLazyLoadConfig(config, id)); | ||
* | ||
* // Usage of useIntersectionObserver | ||
* useIntersectionObserver(activate, config, id, customEventHandlers, containerDivRef, isLazyLoadEnabled); | ||
* | ||
* @returns {void} | ||
*/ | ||
export const useIntersectionObserver = ( | ||
activate, | ||
config, | ||
id, | ||
customEventHandlers, | ||
onAdLoaded, | ||
containerDivRef, | ||
isLazyLoadEnabled | ||
) => { | ||
const observerRef = useRef(null); | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
if (!config || !isLazyLoadEnabled) { | ||
return () => {}; | ||
} | ||
|
||
const rootMargin = calculateRootMargin(getLazyLoadConfig(config, id)); | ||
observerRef.current = new IntersectionObserver( | ||
([{ isIntersecting }]) => { | ||
if (isIntersecting) { | ||
activate(id, customEventHandlers, onAdLoaded); | ||
if (containerDivRef.current) { | ||
observerRef.current.unobserve(containerDivRef.current); | ||
} | ||
} | ||
}, | ||
{ rootMargin } | ||
); | ||
observerRef.current.observe(containerDivRef.current); | ||
|
||
return () => { | ||
if (containerDivRef.current) { | ||
observerRef.current.unobserve(containerDivRef.current); | ||
} | ||
}; | ||
}, [ | ||
activate, | ||
config, | ||
id, | ||
customEventHandlers, | ||
onAdLoaded, | ||
containerDivRef, | ||
isLazyLoadEnabled, | ||
]); | ||
}; |
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