Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
moskalakamil committed Dec 10, 2024
2 parents 3fd0c13 + 52ad2d8 commit 88c7826
Showing 1 changed file with 51 additions and 7 deletions.
58 changes: 51 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
type DimensionValue,
Image,
type ImageStyle,
Animated,
} from 'react-native';
import Video, {
ResizeMode,
Expand Down Expand Up @@ -89,6 +90,7 @@ interface VideoPlayerComponentProps extends ReactVideoProps {
onShowControls?: () => void;
onMutePress?: (isMuted: boolean) => void;
showDuration?: boolean;
animationDuration?: number;
}

const getDurationTime = (duration: number): string => {
Expand Down Expand Up @@ -151,6 +153,7 @@ const VideoPlayerComponent = forwardRef(
onShowControls,
onMutePress,
showDuration = false,
animationDuration = 100,
} = props;

const [isStarted, setIsStarted] = useState(autoplay);
Expand All @@ -171,6 +174,13 @@ const VideoPlayerComponent = forwardRef(
const seekProgressStart = useRef<number>(0);
const wasPlayingBeforeSeek = useRef<boolean>(autoplay);

const animationValue = useRef(new Animated.Value(0)).current;

const controlsTranslateY = animationValue.interpolate({
inputRange: [0, 1],
outputRange: [48, 0],
});

const getSizeStyles = useCallback(() => {
const ratio = videoHeight / videoWidth;
return { height: width * ratio, width: width };
Expand All @@ -197,18 +207,45 @@ const VideoPlayerComponent = forwardRef(

const _hideControls = useCallback(() => {
if (controlsTimeoutRef.current) clearTimeout(controlsTimeoutRef.current);

controlsTimeoutRef.current = setTimeout(() => {
if (onHideControls) onHideControls();
if (disableControlsAutoHide) return;
setIsControlsVisible(false);

Animated.timing(animationValue, {
toValue: 0.1,
duration: animationDuration,
useNativeDriver: true,
}).start(() => {
setIsControlsVisible(false);
});
}, controlsTimeout);
}, [onHideControls, disableControlsAutoHide, controlsTimeout]);
}, [
controlsTimeout,
onHideControls,
disableControlsAutoHide,
animationValue,
animationDuration,
]);

const _showControls = useCallback(() => {
if (onShowControls && !isControlsVisible) onShowControls();
setIsControlsVisible(true);

Animated.timing(animationValue, {
toValue: 1,
duration: animationDuration,
useNativeDriver: true,
}).start();

_hideControls();
}, [_hideControls, isControlsVisible, onShowControls]);
}, [
onShowControls,
isControlsVisible,
animationValue,
animationDuration,
_hideControls,
]);

useEffect(() => {
if (autoplay) _hideControls();
Expand Down Expand Up @@ -451,7 +488,13 @@ const VideoPlayerComponent = forwardRef(

const renderControls = useCallback(
() => (
<View style={[styles.controls, customStyles.controls]}>
<Animated.View
style={[
styles.controls,
customStyles.controls,
{ transform: [{ translateY: controlsTranslateY }] },
]}
>
<TouchableOpacity
onPress={_onPlayPress}
style={[
Expand Down Expand Up @@ -505,14 +548,15 @@ const VideoPlayerComponent = forwardRef(
/>
</TouchableOpacity>
)}
</View>
</Animated.View>
),
[
customStyles.controls,
customStyles.controlButton,
customStyles.playControl,
customStyles.controlIcon,
customStyles.durationText,
customStyles.controlIcon,
controlsTranslateY,
_onPlayPress,
isPlaying,
renderSeekBar,
Expand All @@ -529,7 +573,7 @@ const VideoPlayerComponent = forwardRef(

const renderVideo = useCallback(
() => (
<View style={customStyles.videoWrapper}>
<View style={[{ overflow: 'hidden' }, customStyles.videoWrapper]}>

Check warning on line 576 in src/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline style: { overflow: 'hidden' }
<Video
{...props}
ref={videoRef}
Expand Down

0 comments on commit 88c7826

Please sign in to comment.