-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
85 lines (76 loc) · 2.09 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* node module imports */
import React, { useState, useEffect } from 'react';
/* app imports */
import VideoOverlayContext from './context';
import MainDiv from './components/main-div';
import BodyComposer from './components';
/* eslint-disable react/require-default-props -- interface */
interface FCProps {
embedLink: string;
embedTitle: string;
closeIconSrc: string;
handleOnClose?: () => void;
handleOnOpen?: () => void;
handleOnLoaded?: () => void;
}
/* eslint-enable react/require-default-props -- interface */
/* component */
function ReactYouTubeOverlay(props: FCProps): JSX.Element | null {
const {
embedLink, embedTitle, closeIconSrc, handleOnClose, handleOnOpen, handleOnLoaded,
} = props;
const [createOverlay, setCreateOverlay] = useState<boolean>(false);
const [showState, setShowState] = useState<boolean>(false);
function onCloseHandler() {
setShowState(() => false);
setTimeout(() => {
setCreateOverlay(() => false);
}, 250);
}
const bodyProps = {
embedLink,
embedTitle,
closeIconSrc,
onCloseHandler,
};
const ctxProps = {
handleOnClose,
handleOnOpen,
handleOnLoaded,
};
/* on initial load */
useEffect(() => {
setCreateOverlay(() => true);
}, [embedLink, embedTitle]);
/* on creating the element in the dom and opening starts */
useEffect(() => {
if (createOverlay) {
setTimeout(() => {
setShowState(() => true);
}, 120);
}
}, [createOverlay]);
/* eslint-disable no-else-return */
if (!createOverlay && !showState) {
return null;
} else if (createOverlay && showState) {
return (
<VideoOverlayContext.Provider value={ctxProps}>
<MainDiv show>
<BodyComposer {...bodyProps} />
</MainDiv>
</VideoOverlayContext.Provider>
);
} else {
return (
<VideoOverlayContext.Provider value={ctxProps}>
<MainDiv show={false}>
<BodyComposer {...bodyProps} />
</MainDiv>
</VideoOverlayContext.Provider>
);
}
/* eslint-enable no-else-return */
}
/* exports */
export default ReactYouTubeOverlay;