|
| 1 | +<template lang="pug"> |
| 2 | +div( |
| 3 | + :class="styles.Thumbnail", |
| 4 | + :style="{ backgroundImage: `url(${thumbnailUrl})` }", |
| 5 | +) |
| 6 | + button( |
| 7 | + type="button", |
| 8 | + :class="styles.PlayButton", |
| 9 | + :aria-label="buttonLabel", |
| 10 | + @click="$emit('click')", |
| 11 | + @mouseenter="$emit('before-start-playing')", |
| 12 | + @focus="$emit('before-start-playing')", |
| 13 | + @touchstart="$emit('before-start-playing')", |
| 14 | + ) |
| 15 | + Icon(:class="styles.PlayIcon", :source="PlayIcon") |
| 16 | + p(v-if="videoLength", :class="className") |
| 17 | + | {{ secondsToTimestamp(videoLength) }} |
| 18 | + div(v-if="showVideoProgress", :class="styles.Progress") |
| 19 | + progress( |
| 20 | + :class="styles.ProgressBar", |
| 21 | + :value="progressValuePercents", |
| 22 | + max="100", |
| 23 | + ) |
| 24 | + div( |
| 25 | + :class="styles.Indicator", |
| 26 | + :style="{ transform: `scaleX(${progressValue})`}", |
| 27 | + ) |
| 28 | + span(:class="styles.Label") {{ progressValuePercents }}% |
| 29 | +</template> |
| 30 | + |
| 31 | +<script setup lang="ts"> |
| 32 | +import { ref, computed, inject } from 'vue'; |
| 33 | +import { classNames } from 'polaris/polaris-react/src/utilities/css'; |
| 34 | +import { secondsToTimeComponents, secondsToTimestamp, secondsToDurationTranslationKey } from 'polaris/polaris-react/src/utilities/duration'; |
| 35 | +import PlayIcon from 'polaris/polaris-react/src/components/VideoThumbnail/illustrations/play.svg'; |
| 36 | +import { UseI18n } from '@/use'; |
| 37 | +import { Icon } from '@/components'; |
| 38 | +import styles from '@/classes/VideoThumbnail.json'; |
| 39 | +
|
| 40 | +interface VideoThumbnailProps { |
| 41 | + /** URL source for thumbnail image. */ |
| 42 | + thumbnailUrl: string; |
| 43 | + /** |
| 44 | + * Length of video in seconds. |
| 45 | + * @default 0 |
| 46 | + */ |
| 47 | + videoLength?: number; |
| 48 | + /** |
| 49 | + * Video progress in seconds. Displays a progress bar at the bottom of the thumbnail. Only renders when videoLength is also set. |
| 50 | + * @default 0 |
| 51 | + */ |
| 52 | + videoProgress?: number; |
| 53 | + /** |
| 54 | + * Indicate whether to allow video progress to be displayed |
| 55 | + * @default false |
| 56 | + */ |
| 57 | + showVideoProgress?: boolean; |
| 58 | + /** Custom ARIA label for play button. |
| 59 | + * @default 'Play video of length {human readable duration}' |
| 60 | + */ |
| 61 | + accessibilityLabel?: string; |
| 62 | +} |
| 63 | +
|
| 64 | +const props = withDefaults(defineProps<VideoThumbnailProps>(), { |
| 65 | + videoLength: 0, |
| 66 | + videoProgress: 0, |
| 67 | + showVideoProgress: false, |
| 68 | +}); |
| 69 | +
|
| 70 | +const i18n = UseI18n(); |
| 71 | +
|
| 72 | +const buttonLabel = computed(() => { |
| 73 | + if (props.accessibilityLabel) { |
| 74 | + return props.accessibilityLabel; |
| 75 | + } |
| 76 | +
|
| 77 | + if (props.videoLength) { |
| 78 | + const { hours, minutes, seconds } = secondsToTimeComponents(props.videoLength); |
| 79 | +
|
| 80 | + const translationKey = i18n.translate(secondsToDurationTranslationKey(props.videoLength), { |
| 81 | + hourCount: hours, |
| 82 | + minuteCount: minutes, |
| 83 | + secondCount: seconds, |
| 84 | + }); |
| 85 | +
|
| 86 | + return i18n.translate( |
| 87 | + 'Polaris.VideoThumbnail.playButtonA11yLabel.defaultWithDuration', |
| 88 | + { duration: translationKey }, |
| 89 | + ); |
| 90 | + } |
| 91 | +
|
| 92 | + return i18n.translate('Polaris.VideoThumbnail.playButtonA11yLabel.default'); |
| 93 | +}); |
| 94 | +
|
| 95 | +const progressValue = computed(() => calculateProgress(props.videoLength, props.videoProgress)); |
| 96 | +const progressValuePercents = computed(() => Math.round(progressValue.value * 100)); |
| 97 | +
|
| 98 | +const className = computed(() => { |
| 99 | + return classNames( |
| 100 | + styles.Timestamp, |
| 101 | + props.showVideoProgress && styles.withProgress, |
| 102 | + ); |
| 103 | +}); |
| 104 | +
|
| 105 | +function calculateProgress(videoLength: number, videoProgress: number) { |
| 106 | + // if (videoProgress > videoLength && process.env.NODE_ENV === 'development') { |
| 107 | + // // eslint-disable-next-line no-console |
| 108 | + // console.warn( |
| 109 | + // 'Value passed to the video progress should not exceed video length. Resetting progress to 100%.', |
| 110 | + // ); |
| 111 | + // } |
| 112 | +
|
| 113 | + if (videoProgress > 0 && videoLength > 0) { |
| 114 | + const progress = parseFloat((videoProgress / videoLength).toFixed(2)); |
| 115 | + return progress > 1 ? 1 : progress; |
| 116 | + } |
| 117 | +
|
| 118 | + return 0; |
| 119 | +} |
| 120 | +</script> |
| 121 | + |
| 122 | +<style lang="scss"> |
| 123 | +@import 'polaris/polaris-react/src/components/VideoThumbnail/VideoThumbnail.scss'; |
| 124 | +</style> |
0 commit comments