All files / components/message Message.vue

100% Statements 52/52
97.61% Branches 41/42
100% Functions 14/14
100% Lines 52/52

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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395    2x 4x       2x     2x 2x 2x 2x 2x 2x                 2x                                                                                                                                   17x                           17x 17x 17x 17x   17x 16x 13x 3x   2x     1x   17x   17x             17x 17x     17x             5x       1x       4x   4x   4x   17x 17x 1x 16x     3x 3x       17x                     2x 2x 2x 2x 2x 2x   21x 21x 21x         21x                                             2x   6x                             2x 2x                                                                                                                                                                                                                                                                                                                                        
<template>
	<Transition
		name="cdx-message"
		:appear="fadeIn"
		:leave-active-class="leaveActiveClass"
	>
		<div
			v-if="!dismissed"
			class="cdx-message"
			:class="rootClasses"
			:aria-live="type !== 'error' ? 'polite' : undefined"
			:role="type === 'error' ? 'alert' : undefined"
		>
			<cdx-icon class="cdx-message__icon--vue" :icon="computedIcon" />
 
			<div class="cdx-message__content">
				<!-- @slot Message content. -->
				<slot />
			</div>
 
			<cdx-button
				v-if="userDismissable"
				class="cdx-message__dismiss-button"
				weight="quiet"
				type="button"
				:aria-label="dismissButtonLabel"
				@click="onDismiss( 'user-dismissed' )"
			>
				<cdx-icon :icon="cdxIconClose" :icon-label="dismissButtonLabel" />
			</cdx-button>
		</div>
	</Transition>
</template>
 
<script lang="ts">
import { defineComponent, PropType, ref, computed, onMounted, warn } from 'vue';
import {
	cdxIconInfoFilled,
	cdxIconError,
	cdxIconAlert,
	cdxIconSuccess,
	cdxIconClose,
	Icon
} from '@wikimedia/codex-icons';
import CdxButton from '../button/Button.vue';
import CdxIcon from '../icon/Icon.vue';
import { statusTypeValidator } from '../../constants';
import { StatusType, StatusIconMap } from '../../types';
 
const iconMap: StatusIconMap = {
	notice: cdxIconInfoFilled,
	error: cdxIconError,
	warning: cdxIconAlert,
	success: cdxIconSuccess
};
 
/**
 * A message that provides system feedback to the user.
 */
export default defineComponent( {
	name: 'CdxMessage',
	components: { CdxButton, CdxIcon },
	props: {
		/**
		 * Status type of Message.
		 *
		 * @values 'notice', 'warning', 'error', 'success'
		 */
		type: {
			type: String as PropType<StatusType>,
			default: 'notice',
			validator: statusTypeValidator
		},
 
		/**
		 * Whether this message follows the inline design (no padding, background color, or border).
		 */
		inline: {
			type: Boolean,
			default: false
		},
 
		/**
		 * Custom message icon. Only allowed for notice messages.
		 */
		icon: {
			type: [ String, Object ] as PropType<Icon>,
			default: null
		},
 
		/**
		 * Whether the message should fade in. Should be used for messages that are dynamically
		 * displayed, not present on page load.
		 */
		fadeIn: {
			type: Boolean,
			default: false
		},
 
		/**
		 * Label text for the dismiss button for user-dismissable messages.
		 *
		 * An icon-only button will be displayed that will hide the message on click. This prop is
		 * for the hidden button label required for accessibility purposes and tooltip text.
		 */
		dismissButtonLabel: {
			type: String,
			default: ''
		},
 
		/**
		 * Enable automatic dismissal of message after a period of time.
		 *
		 * This prop can be set to `true` to use the default display time of 4000 milliseconds. To
		 * customize the display time, set this prop to a number of milliseconds.
		 *
		 * Error messages cannot be automatically dismissed. If the `type` prop is set to `error`,
		 * this prop will be ignored.
		 *
		 * TODO: consider adding a stricter validator to set limits on this. If the time is too
		 * short, the message may not be readable. If the time is too long, the message probably
		 * shouldn't be auto-dismissed.
		 */
		autoDismiss: {
			type: [ Boolean, Number ],
			default: false,
			validator: ( value: unknown ) =>
				typeof value === 'boolean' ||
				( typeof value === 'number' && value > 0 )
		}
	},
	emits: [
		/**
		 * Emitted when the message is dismissed by the user via the dismiss button.
		 */
		'user-dismissed',
		/**
		 * Emitted when the message is automatically dismissed after the display time.
		 */
		'auto-dismissed'
	],
	setup( props, { emit } ) {
		const dismissed = ref( false );
		const userDismissable = computed( () =>
			props.inline === false &&
			props.dismissButtonLabel.length > 0
		);
 
		const displayTime = computed( () => {
			if ( props.autoDismiss === false || props.type === 'error' ) {
				return false;
			} else if ( props.autoDismiss === true ) {
				// Return default delay time of 4000 milliseconds.
				return 4000;
			}
			// Return custom delay time.
			return props.autoDismiss;
		} );
 
		const rootClasses = computed( (): Record<string, boolean> => {
			return {
				'cdx-message--inline': props.inline,
				'cdx-message--block': !props.inline,
				'cdx-message--user-dismissable': userDismissable.value,
				[ `cdx-message--${ props.type }` ]: true
			};
		} );
 
		// For notice messages, use a custom icon if provided. Otherwise, use the default icon.
		const computedIcon = computed( () =>
			props.icon && props.type === 'notice' ? props.icon : iconMap[ props.type ]
		);
 
		// Custom class for the leave-active transition. Will be set on dismissal.
		const leaveActiveClass = ref( '' );
 
		/**
		 * Dismiss the message and emit an event.
		 *
		 * @param eventName Name of the event to be emitted.
		 */
		function onDismiss( eventName: 'user-dismissed' | 'auto-dismissed' ) {
			if ( dismissed.value ) {
				// Message has already been dismissed, so do not emit another event. This prevents
				// a race condition when the user dismisses an auto-dismissable message before the
				// display time elapses.
				return;
			}
 
			// Set the leave-active class name so we can customize the transition depending on how
			// the message was dismissed.
			leaveActiveClass.value = eventName === 'user-dismissed' ?
				'cdx-message-leave-active-user' :
				'cdx-message-leave-active-system';
 
			// Remove the component from the DOM via the v-if statement on the root element.
			dismissed.value = true;
 
			// Emit an event in case the parent component needs to take action on dismissal.
			emit( eventName );
		}
 
		onMounted( () => {
			if ( props.type === 'error' && props.autoDismiss !== false ) {
				warn( 'CdxMessage: Message with type="error" cannot use auto-dismiss' );
			} else if ( displayTime.value ) {
				// If auto-dismiss is enabled, set a timer to remove the message after the
				// display time.
				setTimeout( () => onDismiss( 'auto-dismissed' ), displayTime.value );
			}
		} );
 
		return {
			dismissed,
			userDismissable,
			rootClasses,
			leaveActiveClass,
			computedIcon,
			onDismiss,
			cdxIconClose
		};
	}
} );
</script>
 
<!-- eslint-disable max-len -->
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/common.less';
@import ( reference ) '../../themes/mixins/public/css-icon.less';
 
.cdx-message {
	// Background color and color are for notice type messages.
	// These will be overridden for other message types.
	background-color: @background-color-notice-subtle;
	color: @color-notice;
	display: flex;
	align-items: flex-start;
	position: relative;
	border: @border-width-base @border-style-base @border-color-notice;
	padding: @spacing-100 @spacing-100;
 
	@media screen and ( min-width: @min-width-breakpoint-tablet ) {
		padding-right: @spacing-150;
		padding-left: @spacing-150;
	}
 
	// Set the default CSS icon to the icon for notice messages, which is the default message type.
	// The icon and icon color will be overridden for other message types below.
	// The color of the Vue icon for notice messages is inherited from the color rule set on
	// .cdx-message.
	.cdx-message__icon {
		.cdx-mixin-css-icon( @cdx-icon-info-filled );
	}
 
	&--warning {
		background-color: @background-color-warning-subtle;
		border-color: @border-color-warning;
 
		.cdx-message__icon {
			.cdx-mixin-css-icon( @cdx-icon-alert, @color-warning );
		}
 
		.cdx-message__icon--vue {
			color: @color-warning;
		}
	}
 
	&--error {
		background-color: @background-color-error-subtle;
		border-color: @border-color-error;
 
		.cdx-message__icon {
			.cdx-mixin-css-icon( @cdx-icon-error, @color-error );
		}
 
		.cdx-message__icon--vue {
			color: @color-error;
		}
	}
 
	&--success {
		background-color: @background-color-success-subtle;
		border-color: @border-color-success;
 
		.cdx-message__icon {
			.cdx-mixin-css-icon( @cdx-icon-success, @color-success );
		}
 
		.cdx-message__icon--vue {
			color: @color-success;
		}
	}
 
	&--user-dismissable {
		padding-right: calc( @min-size-interactive-pointer + @spacing-100 );
 
		@media screen and ( min-width: @min-width-breakpoint-tablet ) {
			padding-right: calc( @min-size-interactive-pointer + @spacing-150 );
		}
	}
 
	&--inline {
		background-color: @background-color-transparent;
		border: 0;
		padding: 0;
		font-weight: @font-weight-bold;
 
		&.cdx-message--error {
			color: @color-error;
		}
 
		&.cdx-message--success {
			color: @color-success;
		}
	}
 
	// Note that the height is set above for the CSS icons via the mixin, which is why the selector
	// used here is `.cdx-message__icon` instead of `&__icon`. This allows the styles included here
	// to override those set above.
	// The second selector is written the same way for consistency's sake.
	.cdx-message__icon,
	.cdx-message__icon--vue {
		// Vertically align icon with the text. Flexbox on its own is not enough here.
		// Use close enough height to `&__content`'s `line-height`.
		height: @size-150;
	}
 
	&__content {
		.hyphens();
		// Vertically center message with icon.
		align-self: center;
		flex-grow: 1;
		// Add space between icon and message content.
		margin-left: @spacing-50;
	}
 
	&__content,
	&__content > * {
		line-height: @line-height-medium;
	}
 
	&__content > *:first-child {
		margin-top: 0;
		padding-top: 0;
	}
 
	&__content > *:last-child {
		margin-bottom: 0;
		padding-bottom: 0;
	}
 
	&__dismiss-button.cdx-button {
		position: absolute;
		// Use `spacing` tokens as the top/right axis orientation can always be in pixels,
		// similar to paddings
		top: @spacing-75;
		right: @spacing-100;
		padding: @spacing-30;
		// Remove `line-height` to not overgrow button.
		line-height: 0;
 
		@media screen and ( min-width: @min-width-breakpoint-tablet ) {
			right: @spacing-50;
		}
	}
 
	// Add space between stacked messages.
	& + & {
		margin-top: @spacing-50;
	}
 
	// Fade-in and auto-dismissal use the system transition timing function.
	&-enter-active,
	&-leave-active-system {
		transition-property: @transition-property-fade;
		transition-duration: @transition-duration-medium;
		transition-timing-function: @transition-timing-function-system;
	}
 
	// User dismissal uses the human initiated transition timing function.
	&-leave-active-user {
		transition-property: @transition-property-fade;
		transition-duration: @transition-duration-medium;
		transition-timing-function: @transition-timing-function-user;
	}
 
	&-enter-from,
	&-leave-to {
		opacity: @opacity-transparent;
	}
}
</style>
<!-- eslint-enable max-len -->