-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowerThird.tsx
More file actions
161 lines (149 loc) · 4.39 KB
/
LowerThird.tsx
File metadata and controls
161 lines (149 loc) · 4.39 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { h } from 'preact';
import { animated, useSpring } from '@react-spring/web';
import { useEffect, useState } from 'preact/hooks';
import styled from 'styled-components';
import { CGConfig } from '@/types';
const LowerThirdContainer = styled(animated.div)<{ showTicker: boolean, textColor: string }>`
z-index: 10;
display: inline-block;
width: 100%;
padding: 0.15em;
box-sizing: border-box;
background: #fff;
color: #000;
padding-left: 2em;
padding-right: 2em;
padding-top: 0.6em;
padding-bottom: 0.6em;
transition-property: padding-left, padding-right, padding-top, padding-bottom;
transition-duration: 0.3s;
transition-timing-function: ease-in-out;
--webkit-clip-path: polygon(0 0, 100% 0, calc(100% - 1.2em) 100%, 0 100%);
clip-path: polygon(0 0, 100% 0, calc(100% - 1.2em) 100%, 0 100%);
`;
const SubtitleContainer = styled(animated.div)`
width: fit-content;
background: #000;
color: #fff;
padding: 0.5em 1em 0.5em 2em;
margin-right: 1.5em;
display: inline-block;
div {
font-size: 0.7em;
}
`;
type LowerThirdInfo = {
logo: string | null;
title: string | null;
subtitle: string | null;
};
function LowerThird({
cgConfig,
}: { cgConfig: CGConfig }) {
const [shouldShow, setShouldShow] = useState<boolean>(false);
const [displayedInfo, setDisplayedInfo] = useState<LowerThirdInfo | undefined>(undefined);
const [spring, springApi] = useSpring(() => ({
from: {
x: '-100%',
subtitleY: '-101%',
borderWidth: '0em',
},
}));
const animateOut = async () => {
await Promise.all(springApi.start({
x: '-100%',
subtitleY: '-101%',
borderWidth: '0em',
config: { tension: 50, friction: 14, clamp: true },
}));
};
const animateIn = async () => {
const anim1 = springApi.start({
x: '0%',
// borderImage: 'linear-gradient(to bottom, #000 50%, transparent 50%) 100% 1',
config: { tension: 50, friction: 14, clamp: true },
});
await new Promise((res) => { setTimeout(res, 800); });
let anim3 = [] as any[];
if (cgConfig?.lowerThirdSubtitle) {
anim3 = springApi.start({
subtitleY: '0%',
config: { tension: 50, friction: 14, clamp: true },
});
await new Promise((res) => { setTimeout(res, 200); });
}
const anim2 = springApi.start({
borderWidth: '0.5em',
config: { tension: 120, friction: 14, clamp: true },
});
await Promise.all([...anim1, ...anim2, ...anim3]);
};
// Decide whether the branding should *actually* be shown
useEffect(() => {
setShouldShow(
!!displayedInfo
&& cgConfig.showLowerThird
&& (
(!!displayedInfo.title && !!displayedInfo.title.match(/\S/))
|| (!!displayedInfo.subtitle && !!displayedInfo.subtitle.match(/\S/))
),
);
}, [
cgConfig?.showLowerThird,
displayedInfo?.title,
displayedInfo?.subtitle,
]);
// Handle in/out animations
useEffect(() => {
if (!shouldShow) {
animateOut();
} else {
animateIn();
}
}, [shouldShow]);
// Animate out before updating the displayed branding
useEffect(() => {
const updateInfo = () => {
setDisplayedInfo({
logo: cgConfig.lowerThirdLogo,
title: cgConfig.lowerThirdTitle,
subtitle: cgConfig.lowerThirdSubtitle,
});
};
if (!shouldShow) {
updateInfo();
return;
}
(async () => {
await animateOut();
updateInfo();
await new Promise((res) => { setTimeout(res, 100); });
await animateIn();
})();
}, [
cgConfig.lowerThirdLogo, cgConfig.lowerThirdTitle, cgConfig.lowerThirdSubtitle,
]);
if (!displayedInfo) return null;
return (
<animated.div style={{
paddingBottom: '2em', x: spring.x, display: 'flex', flexDirection: 'column-reverse',
}}
>
{/*
Subtitle needs to go before title in the DOM tree so that layering works properly.
Otherwise, the subtitle displays on top of the title
*/}
{displayedInfo.subtitle && (
<SubtitleContainer style={{ y: spring.subtitleY }}>
<div>{displayedInfo.subtitle}</div>
</SubtitleContainer>
)}
<animated.div style={{ filter: spring.borderWidth.to((val) => `drop-shadow(${val} 0px 0px #0066b3)`), zIndex: 30 }}>
<LowerThirdContainer>
{displayedInfo.title}
</LowerThirdContainer>
</animated.div>
</animated.div>
);
}
export default LowerThird;