-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: update react demo example * fix: react jsx wrapper * revert: demo changes * fix: add missing debug prop --------- Co-authored-by: Anonymous <[email protected]>
- Loading branch information
1 parent
02fc09e
commit 747833d
Showing
1 changed file
with
66 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,82 @@ | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import React, { useEffect, useRef } from 'react'; | ||
import ScrollyVideo from './ScrollyVideo'; | ||
|
||
function ScrollyVideoComponent(props) { | ||
|
||
// variable to hold the DOM element | ||
function ScrollyVideoComponent({ | ||
src, | ||
transitionSpeed, | ||
frameThreshold, | ||
cover, | ||
sticky, | ||
full, | ||
trackScroll, | ||
useWebCodecs, | ||
videoPercentage, | ||
debug, | ||
}) { | ||
const containerElement = useRef(null); | ||
|
||
// ref to hold the scrollyVideo object | ||
const scrollyVideoRef = useRef(null); | ||
|
||
// Store the props so we know when things change | ||
const [lastPropsString, setLastPropsString] = useState(''); | ||
const videoPercentageRef = useRef(videoPercentage); | ||
videoPercentageRef.current = videoPercentage; | ||
|
||
// effect for destroy and recreate on props change (except video percentage) | ||
useEffect(() => { | ||
if (containerElement) { | ||
// separate out the videoPercentage prop | ||
const { videoPercentage, ...restProps } = props; | ||
if (!containerElement.current) return; | ||
|
||
if (JSON.stringify(restProps) !== lastPropsString) { | ||
// if scrollyvideo already exists and any parameter is updated, destroy and recreate. | ||
if (scrollyVideoRef.current && scrollyVideoRef.current.destroy) scrollyVideoRef.current.destroy(); | ||
scrollyVideoRef.current = new ScrollyVideo({ scrollyVideoContainer: containerElement.current, ...props }); | ||
// if scrollyVideo already exists and any parameter is updated, destroy and recreate. | ||
if (scrollyVideoRef.current && scrollyVideoRef.current.destroy) { | ||
scrollyVideoRef.current.destroy(); | ||
} | ||
|
||
// Save the new props | ||
setLastPropsString(JSON.stringify(restProps)); | ||
} | ||
scrollyVideoRef.current = new ScrollyVideo({ | ||
scrollyVideoContainer: containerElement.current, | ||
src, | ||
transitionSpeed, | ||
frameThreshold, | ||
cover, | ||
sticky, | ||
full, | ||
trackScroll, | ||
useWebCodecs, | ||
debug, | ||
videoPercentage: videoPercentageRef.current, | ||
}); | ||
}, [ | ||
src, | ||
transitionSpeed, | ||
frameThreshold, | ||
cover, | ||
sticky, | ||
full, | ||
trackScroll, | ||
useWebCodecs, | ||
debug, | ||
]); | ||
|
||
// If we need to update the target time percent | ||
if (scrollyVideoRef.current && typeof videoPercentage === 'number' && videoPercentage >= 0 && videoPercentage <= 1) { | ||
scrollyVideoRef.current.setTargetTimePercent(videoPercentage); | ||
} | ||
// effect for video percentage change | ||
useEffect(() => { | ||
// If we need to update the target time percent | ||
if ( | ||
scrollyVideoRef.current && | ||
typeof videoPercentage === 'number' && | ||
videoPercentage >= 0 && | ||
videoPercentage <= 1 | ||
) { | ||
scrollyVideoRef.current.setTargetTimePercent(videoPercentage); | ||
} | ||
}, [videoPercentage]); | ||
|
||
// Cleanup the component on unmount | ||
return () => { | ||
if (scrollyVideoRef.current && scrollyVideoRef.current.destroy) scrollyVideoRef.current.destroy(); | ||
} | ||
}, [containerElement, props]); | ||
// effect for unmount | ||
useEffect( | ||
() => () => { | ||
if (scrollyVideoRef.current && scrollyVideoRef.current.destroy) { | ||
scrollyVideoRef.current.destroy(); | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
return (<div ref={containerElement} />); | ||
return <div ref={containerElement} />; | ||
} | ||
|
||
export default ScrollyVideoComponent; |