Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add slide animation #204

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
type DimensionValue,
Image,
type ImageStyle,
Animated,
} from 'react-native';
import Video, {
ResizeMode,
Expand Down Expand Up @@ -89,6 +90,7 @@
onShowControls?: () => void;
onMutePress?: (isMuted: boolean) => void;
showDuration?: boolean;
animationDuration?: number;
}

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

const [isStarted, setIsStarted] = useState(autoplay);
Expand All @@ -171,6 +174,13 @@
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 _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 All @@ -222,7 +259,7 @@
}, [_hideControls, autoplay]);

const onLayout = useCallback((event: LayoutChangeEvent) => {
const { width } = event.nativeEvent.layout;

Check warning on line 262 in src/index.tsx

View workflow job for this annotation

GitHub Actions / lint

'width' is already declared in the upper scope on line 162 column 12
setWidth(width);
}, []);

Expand Down Expand Up @@ -451,7 +488,13 @@

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 @@
/>
</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 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
Loading