use-video-interval
Version:
A React hook for executing a callback at customizable intervals during video playback.
68 lines (65 loc) • 1.66 kB
TypeScript
/**
* IntervalCallbacks
*
* A record of interval numbers as keys and callback functions as values.
*
* @type {Record<number, () => void>}
*/
type IntervalCallbacks = Record<number, () => void>;
/**
* Options for the useVideoInterval hook.
*/
interface Options {
/**
* The threshold in seconds to check the current time against.
*
* @default 0.5
*/
threshold?: number;
/**
* Whether to execute the callback only once.
*
* @default true
*/
triggerOnce?: boolean;
}
type useVideoIntervalProps = {
videoRef: React.RefObject<HTMLVideoElement | null>;
intervalCallbacks: IntervalCallbacks;
options?: Options;
};
/**
* useVideoInterval
*
* @example
* ```tsx
* import { useRef, useState } from 'react';
*
* function App() {
* const videoRef = useRef<HTMLVideoElement>( null );
* const [ showOverlay, setShowOverlay ] = useState( false );
*
* useVideoInterval( videoRef, {
* 3: () => setShowOverlay( true ),
* 8: () => alert( 'This is 8 seconds!' ),
* 15: () => console.log( 'Reached 15s mark' )
* });
*
* return (
* <>
* <video ref={ videoRef } src="" />
* { showOverlay && <Overlay /> }
* </>
* );
* }
* ```
*
* @param videoRef - A reference to the video element.
* @param intervalCallbacks - An object containing interval numbers as keys and callback functions as values.
* @param options(optional) - An object containing options for the hook.
* @return {void}
*
*/
declare function useVideoInterval(props: useVideoIntervalProps): void;
export { useVideoInterval };
export type { IntervalCallbacks, Options, useVideoIntervalProps };