All files / components/thumbnail Thumbnail.vue

97.05% Statements 33/34
94.11% Branches 16/17
87.5% Functions 7/8
97.05% Lines 33/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147    9x 9x       9x     9x 9x 9x 9x             9x                                           27x 27x 27x 12x 12x 12x 1x     1x   12x     12x   27x   27x 12x     27x             9x 9x 9x 9x 9x 9x         28x 28x                   28x                         9x 9x                                                                                
<template>
	<span class="cdx-thumbnail">
		<span
			v-if="!thumbnailLoaded"
			class="cdx-thumbnail__placeholder"
		>
			<cdx-icon
				:icon="placeholderIcon"
				class="cdx-thumbnail__placeholder__icon--vue"
			/>
		</span>
		<Transition name="cdx-thumbnail__image">
			<span
				v-if="thumbnailLoaded"
				:style="thumbnailStyle"
				:class="NoInvertClass"
				class="cdx-thumbnail__image"
			/>
		</Transition>
	</span>
</template>
 
<script lang="ts">
import { PropType, defineComponent, ref, onMounted } from 'vue';
import { cdxIconImageLayoutFrameless, Icon } from '@wikimedia/codex-icons';
import CdxIcon from '../icon/Icon.vue';
import { Thumbnail } from '../../types';
import { NoInvertClass } from '../../constants';
 
/**
 * A small preview of an image.
 *
 * The placeholder icon will display until thumbnail image loads, or if a thumbnail image is not
 * provided.
 */
export default defineComponent( {
	name: 'CdxThumbnail',
 
	components: { CdxIcon },
 
	props: {
		/**
		 * Thumbnail data.
		 */
		thumbnail: {
			type: [ Object, null ] as PropType<Thumbnail|null>,
			default: null
		},
 
		/**
		 * Thumbnail placeholder icon.
		 */
		placeholderIcon: {
			type: [ String, Object ] as PropType<Icon>,
			default: cdxIconImageLayoutFrameless
		}
	},
 
	setup: ( props ) => {
		const thumbnailLoaded = ref( false );
		const thumbnailStyle = ref( {} );
 
		const preloadThumbnail = ( url: string ) => {
			const escapedUrl = url.replace( /([\\"\n])/g, '\\$1' );
			const image = new Image();
			image.onload = () => {
				thumbnailStyle.value = { backgroundImage: `url("${ escapedUrl }")` };
				thumbnailLoaded.value = true;
			};
			image.onerror = () => {
				thumbnailLoaded.value = false;
			};
			image.src = escapedUrl;
		};
 
		onMounted( () => {
			if ( props.thumbnail?.url ) {
				preloadThumbnail( props.thumbnail.url );
			}
		} );
 
		return {
			thumbnailStyle,
			thumbnailLoaded,
			NoInvertClass
		};
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/public/css-icon.less';
 
.cdx-thumbnail {
	// `flex` prevents a descender from being added below the image; `inline` limits width of this
	// element to the width of the image.
	display: inline-flex;
 
	&__placeholder,
	&__image {
		background-position: @background-position-base;
		background-repeat: no-repeat;
		background-size: @background-size-search-figure;
		// Thumbnail should never shrink when it's in a flex layout with other elements.
		flex-shrink: 0;
		box-sizing: @box-sizing-base;
		// Values of thumbnail as declared within the MenuItem component, f.e. in TypeaheadSearch.
		min-width: @min-size-search-figure;
		min-height: @min-size-search-figure;
		width: @size-search-figure;
		height: @size-search-figure;
		border: @border-subtle;
		border-radius: @border-radius-base;
	}
 
	&__image {
		display: inline-block;
 
		// Fade in transition applied to the thumbnail on show.
		&-enter-active {
			transition-property: @transition-property-fade;
			transition-duration: @transition-duration-base;
		}
 
		&-enter-from {
			opacity: @opacity-transparent;
		}
	}
 
	&__placeholder {
		background-color: @background-color-interactive-subtle;
		display: inline-flex;
		align-items: center;
		justify-content: center;
 
		&__icon {
			.cdx-mixin-css-icon( @cdx-icon-image-layout-frameless, @color-placeholder );
 
			&--vue.cdx-icon {
				color: @color-placeholder;
			}
		}
	}
}
</style>