@shopify/polaris
Version:
Shopify’s product component library
98 lines (88 loc) • 3.18 kB
JavaScript
import React$1 from 'react';
import { useI18n } from '../../utilities/i18n/hooks.js';
import { classNames } from '../../utilities/css.js';
import { secondsToDurationTranslationKey, secondsToTimestamp, secondsToTimeComponents } from '../../utilities/duration.js';
import img from './illustrations/play.svg.js';
import styles from './VideoThumbnail.scss.js';
function VideoThumbnail({
thumbnailUrl,
videoLength = 0,
videoProgress = 0,
showVideoProgress = false,
accessibilityLabel,
onClick,
onBeforeStartPlaying
}) {
var i18n = useI18n();
var buttonLabel;
if (accessibilityLabel) {
buttonLabel = accessibilityLabel;
} else if (videoLength) {
var {
hours,
minutes,
seconds
} = secondsToTimeComponents(videoLength);
buttonLabel = i18n.translate('Polaris.VideoThumbnail.playButtonA11yLabel.defaultWithDuration', {
duration: i18n.translate(secondsToDurationTranslationKey(videoLength), {
hourCount: hours,
minuteCount: minutes,
secondCount: seconds
})
});
} else {
buttonLabel = i18n.translate('Polaris.VideoThumbnail.playButtonA11yLabel.default');
}
var timeStampMarkup = videoLength ? /*#__PURE__*/React$1.createElement("p", {
className: classNames(styles.Timestamp, showVideoProgress && styles.withProgress)
}, secondsToTimestamp(videoLength)) : null;
var progressMarkup = null;
if (showVideoProgress) {
var progressValue = calculateProgress(videoLength, videoProgress);
var progressValuePercents = Math.round(progressValue * 100);
progressMarkup = /*#__PURE__*/React$1.createElement("div", {
className: styles.Progress
}, /*#__PURE__*/React$1.createElement("progress", {
className: styles.ProgressBar,
value: progressValuePercents,
max: "100"
}), /*#__PURE__*/React$1.createElement("div", {
className: styles.Indicator,
style: {
transform: "scaleX(".concat(progressValue, ")")
}
}, /*#__PURE__*/React$1.createElement("span", {
className: styles.Label
}, progressValuePercents, "%")));
}
return /*#__PURE__*/React$1.createElement("div", {
className: styles.Thumbnail,
style: {
backgroundImage: "url(".concat(thumbnailUrl, ")")
}
}, /*#__PURE__*/React$1.createElement("button", {
type: "button",
className: styles.PlayButton,
"aria-label": buttonLabel,
onClick: onClick,
onMouseEnter: onBeforeStartPlaying,
onFocus: onBeforeStartPlaying,
onTouchStart: onBeforeStartPlaying
}, /*#__PURE__*/React$1.createElement("img", {
className: styles.PlayIcon,
src: img,
alt: ""
})), timeStampMarkup, progressMarkup);
}
function calculateProgress(videoLength, videoProgress) {
if (videoProgress > videoLength && process.env.NODE_ENV === 'development') {
// eslint-disable-next-line no-console
console.warn('Value passed to the video progress should not exceed video length. Resetting progress to 100%.');
}
if (videoProgress > 0 && videoLength > 0) {
var progress = parseFloat((videoProgress / videoLength).toFixed(2));
return progress > 1 ? 1 : progress;
}
return 0;
}
export { VideoThumbnail };