react-native-video-player
Version:
A <VideoPlayer /> component for React Native with controls
148 lines (147 loc) • 5.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Seekbar = void 0;
var _reactNative = require("react-native");
var _react = require("react");
var _jsxRuntime = require("react/jsx-runtime");
const parsePadding = (value, layoutWidth) => {
if (typeof value === 'number') return value;
if (typeof value === 'string' && value.endsWith('%')) {
return parseFloat(value) / 100 * layoutWidth;
}
return 0;
};
const Seekbar = exports.Seekbar = /*#__PURE__*/(0, _react.memo)(/*#__PURE__*/(0, _react.forwardRef)(({
fullWidth,
autoplay,
isPlaying,
setIsPlaying,
disableSeek,
showControls,
controlsTimeoutId,
onSeek,
onSeeking,
duration,
seekBarCustomStyles,
seekBarFullWidthCustomStyles,
seekBarKnobCustomStyles,
seekBarKnobSeekingCustomStyles,
seekBarProgressCustomStyles,
seekBarBackgroundCustomStyles
}, ref) => {
const [progress, setProgress] = (0, _react.useState)(0);
const [isSeeking, setIsSeeking] = (0, _react.useState)(false);
(0, _react.useImperativeHandle)(ref, () => ({
onProgress: setProgress
}));
const seekBarWidth = (0, _react.useRef)(200);
const seekTouchStart = (0, _react.useRef)(0);
const seekProgressStart = (0, _react.useRef)(0);
const wasPlayingBeforeSeek = (0, _react.useRef)(autoplay || isPlaying);
const _onSeekBarLayout = (0, _react.useCallback)(({
nativeEvent
}) => {
const layoutWidth = nativeEvent.layout.width;
const customStyle = seekBarCustomStyles;
const paddingHorizontal = customStyle?.paddingHorizontal ? parsePadding(customStyle.paddingHorizontal, layoutWidth) * 2 : 0;
const paddingLeft = customStyle?.paddingLeft ? parsePadding(customStyle.paddingLeft, layoutWidth) : 0;
const paddingRight = customStyle?.paddingRight ? parsePadding(customStyle.paddingRight, layoutWidth) : 0;
const totalPadding = paddingHorizontal || paddingLeft + paddingRight;
seekBarWidth.current = layoutWidth - totalPadding;
}, [seekBarCustomStyles]);
const _onSeekGrant = (0, _react.useCallback)(e => {
seekTouchStart.current = e.nativeEvent.pageX;
seekProgressStart.current = progress ?? 0;
wasPlayingBeforeSeek.current = isPlaying;
setIsSeeking(true);
setIsPlaying(false);
if (controlsTimeoutId) clearTimeout(controlsTimeoutId);
}, [controlsTimeoutId, isPlaying, progress, setIsPlaying]);
const _onSeekRelease = (0, _react.useCallback)(() => {
setIsSeeking(false);
setIsPlaying(wasPlayingBeforeSeek.current);
onSeek(progress * duration);
showControls();
}, [duration, onSeek, progress, setIsPlaying, showControls]);
const _onSeek = (0, _react.useCallback)(e => {
const diff = e.nativeEvent.pageX - seekTouchStart.current;
const ratio = 100 / seekBarWidth.current;
const newProgress = seekProgressStart.current + ratio * diff / 100;
const fixedProgress = Math.min(Math.max(newProgress, 0), 1);
onSeeking(fixedProgress);
setProgress(fixedProgress);
}, [onSeeking]);
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
style: [styles.seekBar, seekBarCustomStyles, fullWidth && styles.seekBarFullWidth, fullWidth && seekBarFullWidthCustomStyles],
onLayout: _onSeekBarLayout,
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
style: [!isNaN(progress ?? 0) ? {
flexGrow: progress ?? 0
} : null, styles.seekBarProgress, seekBarProgressCustomStyles]
}), !fullWidth && !disableSeek && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
style: [styles.seekBarKnob, seekBarKnobCustomStyles, isSeeking && styles.seekBarKnobSeeking, isSeeking && seekBarKnobSeekingCustomStyles],
hitSlop: {
top: 20,
bottom: 20,
left: 10,
right: 20
},
onStartShouldSetResponder: () => true,
onMoveShouldSetResponder: () => true,
onResponderGrant: _onSeekGrant,
onResponderMove: _onSeek,
onResponderRelease: _onSeekRelease,
onResponderTerminate: _onSeekRelease
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
style: [styles.seekBarBackground, !isNaN(progress) ? {
flexGrow: 1 - progress
} : null, seekBarBackgroundCustomStyles]
})]
});
}));
const styles = _reactNative.StyleSheet.create({
seekBar: {
alignItems: 'center',
height: 30,
flexGrow: 1,
flexDirection: 'row',
paddingHorizontal: 10,
marginLeft: -10,
marginRight: -5
},
seekBarFullWidth: {
marginLeft: 0,
marginRight: 0,
paddingHorizontal: 0,
marginTop: -3,
height: 3
},
seekBarProgress: {
height: 3,
backgroundColor: '#F00'
},
seekBarKnob: {
width: 20,
height: 20,
marginHorizontal: -8,
marginVertical: -10,
borderRadius: 10,
backgroundColor: '#F00',
transform: [{
scale: 0.8
}],
zIndex: 1
},
seekBarKnobSeeking: {
transform: [{
scale: 1
}]
},
seekBarBackground: {
backgroundColor: 'rgba(255, 255, 255, 0.5)',
height: 3
}
});
//# sourceMappingURL=Seekbar.js.map