All files / components/card Card.vue

100% Statements 28/28
95.65% Branches 22/23
100% Functions 7/7
100% Lines 28/28

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218    1x 2x       1x     1x 1x 1x       1x                                                                                                                     9x 9x   9x 9x   9x 9x   9x             1x 1x 1x 1x 1x 1x 1x       1x         11x 11x 11x               11x                                                                         1x 1x                                                                                                                              
<template>
	<component
		:is="contentTag"
		:href="cardLink"
		class="cdx-card"
		:class="{
			'cdx-card--is-link': isLink,
			// Include dynamic classes in the template so that $slots is reactive.
			'cdx-card--title-only': !$slots.description && !$slots[ 'supporting-text' ]
		}"
	>
		<cdx-thumbnail
			v-if="thumbnail || forceThumbnail"
			:thumbnail="thumbnail"
			:placeholder-icon="customPlaceholderIcon"
			class="cdx-card__thumbnail"
		/>
		<cdx-icon
			v-else-if="icon"
			:icon="icon"
			class="cdx-card__icon"
		/>
 
		<span class="cdx-card__text">
			<span class="cdx-card__text__title">
				<!-- @slot Card title -->
				<slot name="title" />
			</span>
 
			<span v-if="$slots.description" class="cdx-card__text__description">
				<!-- @slot Card description -->
				<slot name="description" />
			</span>
 
			<span v-if="$slots[ 'supporting-text' ]" class="cdx-card__text__supporting-text">
				<!-- @slot Short supporting text -->
				<slot name="supporting-text" />
			</span>
		</span>
	</component>
</template>
 
<script lang="ts">
import { PropType, defineComponent, computed } from 'vue';
import { Icon } from '@wikimedia/codex-icons';
import CdxIcon from '../icon/Icon.vue';
import CdxThumbnail from '../thumbnail/Thumbnail.vue';
import { Thumbnail } from '../../types';
 
/**
 * An element which groups various kinds of content and is optionally clickable.
 */
export default defineComponent( {
	name: 'CdxCard',
 
	components: { CdxIcon, CdxThumbnail },
 
	props: {
		/**
		 * If provided, the Card will be a link to this URL.
		 */
		url: {
			type: String,
			default: ''
		},
 
		/**
		 * Icon displayed at the start of the Card.
		 */
		icon: {
			type: [ String, Object ] as PropType<Icon>,
			default: ''
		},
 
		/**
		 * Thumbnail image data for the Card.
		 */
		thumbnail: {
			type: [ Object, null ] as PropType<Thumbnail|null>,
			default: null
		},
 
		/**
		 * Option to force a thumbnail layout.
		 *
		 * When set to `true`, the Card will display a Thumbnail. If a `thumbnail` prop was also
		 * provided, the thumbnail image will display. Otherwise, a placeholder icon will display.
		 *
		 * This is useful when displaying groups of Cards when some of the cards have thumbnail
		 * images but some do not. `forceThumbnail` will provide a consistent layout for that group.
		 *
		 * Note that this prop is not needed to display a thumbnail image: if the `thumbnail` prop
		 * is provided, it will display. This prop is only needed to enable the display of the
		 * thumbnail placeholder icon when the `thumbnail` prop is not provided.
		 */
		forceThumbnail: {
			type: Boolean,
			default: false
		},
 
		/**
		 * Optional custom icon for the placeholder shown when `forceThumbnail` is true but no
		 * thumbnail is provided.
		 *
		 * Defaults to the default placeholder icon set in the Thumbnail component.
		 */
		customPlaceholderIcon: {
			type: [ String, Object ] as PropType<Icon>,
			default: undefined
		}
	},
 
	setup( props ) {
		// If a URL is provided for this menu item, the content will be wrapped in an <a> tag with
		// the href value set to the URL. Otherwise, it'll just be wrapped in a <span>.
		const isLink = computed( () => !!props.url );
		const contentTag = computed( () => isLink.value ? 'a' : 'span' );
		const cardLink = computed( () => isLink.value ? props.url : undefined );
 
		return {
			isLink,
			contentTag,
			cardLink
		};
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/common.less';
 
.cdx-card {
	background-color: @background-color-base;
	display: flex;
	align-items: flex-start;
	position: relative;
	border: @border-base;
	border-radius: @border-radius-base;
	padding: @spacing-75;
 
	&--is-link {
		transition-property: @transition-property-base;
		transition-duration: @transition-duration-base;
 
		&,
		&:hover,
		&:focus {
			text-decoration: @text-decoration-none;
		}
 
		&:hover {
			border-color: @border-color-interactive;
		}
 
		&:focus {
			border-color: @border-color-progressive--focus;
			// Make `box-shadow` feature a `1px` White inset outline with a value combination.
			/* stylelint-disable-next-line stylistic/declaration-colon-newline-after,
				stylistic/value-list-comma-newline-after */
			box-shadow: @box-shadow-inset-small @box-shadow-color-progressive--focus,
				@box-shadow-inset-medium @box-shadow-color-inverted;
			outline: @outline-base--focus;
		}
	}
 
	&--title-only {
		align-items: center;
	}
 
	&__text {
		display: flex;
		flex-direction: column;
		line-height: @line-height-small;
		.hyphens();
 
		&__title {
			color: @color-base;
			font-weight: @font-weight-bold;
			line-height: @line-height-x-small;
		}
 
		&__description,
		&__supporting-text {
			&,
			.cdx-icon {
				color: @color-subtle;
			}
		}
 
		&__description {
			margin-top: @spacing-25;
		}
 
		&__supporting-text {
			margin-top: @spacing-50;
			font-size: @font-size-small;
		}
	}
 
	&__thumbnail.cdx-thumbnail {
		margin-right: @spacing-75;
 
		.cdx-thumbnail__placeholder,
		.cdx-thumbnail__image {
			width: @size-300;
			height: @size-300;
		}
	}
 
	.cdx-card__icon {
		// Make sure the icon inherits the content element's color.
		color: inherit;
		margin-right: @spacing-75;
	}
}
</style>