All files / components/label Label.vue

100% Statements 33/33
79.31% Branches 23/29
100% Functions 4/4
100% Lines 33/33

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    6x 18x       6x     6x 6x 6x 6x       6x                                                                                                                                                 99x 99x 99x 99x           99x 99x 99x 99x 99x             6x 6x 6x 6x 6x 6x 6x       6x 6x 6x 6x       6x         144x 144x                                                                                                                                                               6x 6x                                                                    
<template>
	<!-- Separate <label> and description are wrapped in a <div>. -->
	<div
		v-if="!isLegend"
		class="cdx-label"
		:class="rootClasses"
		:style="rootStyle"
	>
		<label
			class="cdx-label__label"
			:for="inputId ? inputId : undefined"
			v-bind="otherAttrs"
		>
			<cdx-icon
				v-if="icon"
				:icon="icon"
				class="cdx-label__label__icon"
			/>
			<span class="cdx-label__label__text">
				<!-- @slot Label text. -->
				<slot />
			</span>
			<span v-if="optionalFlag" class="cdx-label__label__optional-flag">
				<!-- Add a space before the optional flag text. Vue strips whitespace
					between everything except plain text, so we can't rely on a newline to
					add a natural space here. -->
				<!-- eslint-disable-next-line vue/no-useless-mustaches -->
				{{ ' ' }}
				{{ optionalFlag }}
			</span>
		</label>
 
		<!-- Include an ID attribute that will be used on the input for aria-describedby. -->
		<span
			v-if="$slots.description && $slots.description().length > 0"
			:id="descriptionId || undefined"
			class="cdx-label__description"
		>
			<!-- @slot Short description text. -->
			<slot name="description" />
		</span>
	</div>
 
	<!-- <legend> must be the root element so it is a direct child of <fieldset>, and <legend>
		contains the description. Both required for assistive technology support. -->
	<legend
		v-else
		class="cdx-label"
		:class="rootClasses"
		:style="rootStyle"
		v-bind="otherAttrs"
	>
		<span class="cdx-label__label">
			<cdx-icon
				v-if="icon"
				:icon="icon"
				class="cdx-label__label__icon"
			/>
			<span class="cdx-label__label__text">
				<!-- @slot Label text. -->
				<slot />
			</span>
			<span v-if="optionalFlag" class="cdx-label__label__optional-flag">
				<!-- Add a space before the optional flag text. Vue strips whitespace
					between everything except plain text, so we can't rely on a newline to
					add a natural space here. -->
				<!-- eslint-disable-next-line vue/no-useless-mustaches -->
				{{ ' ' }}
				{{ optionalFlag }}
			</span>
		</span>
 
		<!-- For legends, the description needs to be inside the <legend> for
			assistive technology support. -->
		<span
			v-if="$slots.description && $slots.description().length > 0"
			class="cdx-label__description"
		>
			<!-- @slot Short description text. -->
			<slot name="description" />
		</span>
	</legend>
</template>
 
<script lang="ts">
import { defineComponent, PropType, computed, toRef } from 'vue';
import { Icon } from '@wikimedia/codex-icons';
import CdxIcon from '../../components/icon/Icon.vue';
import useFieldData from '../../composables/useFieldData';
import useSplitAttributes from '../../composables/useSplitAttributes';
 
/**
 * Describes the information requested by a given form field.
 */
export default defineComponent( {
	name: 'CdxLabel',
	components: { CdxIcon },
	/**
	 * We want the label or legend to inherit attributes, not the root element.
	 */
	inheritAttrs: false,
	props: {
		/**
		 * Icon before the label text.
		 *
		 * Do not use this if including a start icon within the input component.
		 */
		icon: {
			type: [ String, Object ] as PropType<Icon|null>,
			default: null
		},
		/**
		 * Text to indicate that the field is optional.
		 *
		 * For example, this might be '(optional)' in English. This text will be placed next to
		 * the label text.
		 */
		optionalFlag: {
			type: String,
			default: ''
		},
		/**
		 * Whether the label should be visually hidden.
		 */
		visuallyHidden: {
			type: Boolean,
			default: false
		},
		/**
		 * Whether this component should output a `<legend>` element.
		 *
		 * Always set this to true when this component is used inside a `<fieldset>` element. Do not
		 * set it to true otherwise.
		 */
		isLegend: {
			type: Boolean,
			default: false
		},
		/**
		 * The ID of the input/control this label is for.
		 *
		 * Will be added as the `for` attribute of the `<label>`. Not needed for `<legend>`.
		 */
		inputId: {
			type: String,
			default: ''
		},
		/**
		 * The ID of the description element.
		 *
		 * This ID can be used for the `aria-describedby` attribute of the input.
		 */
		descriptionId: {
			type: String,
			default: ''
		},
		/**
		 * Whether this label is for a disabled field or input.
		 */
		disabled: {
			type: Boolean,
			default: false
		}
	},
	setup( props, { attrs } ) {
		const { computedDisabled } = useFieldData( toRef( props, 'disabled' ) );
 
		const internalClasses = computed( () => {
			return {
				'cdx-label--visually-hidden': props.visuallyHidden,
				'cdx-label--disabled': computedDisabled.value
			};
		} );
 
		// Get helpers from useSplitAttributes.
		const {
			rootClasses,
			rootStyle,
			otherAttrs
		} = useSplitAttributes( attrs, internalClasses );
 
		return {
			rootClasses,
			rootStyle,
			otherAttrs
		};
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/common.less';
 
.cdx-label {
	display: flex;
	// Display the description on a separate line after the label text.
	flex-direction: column;
	line-height: @line-height-xx-small;
 
	// Styles for label content.
	&__label {
		&__icon.cdx-icon {
			margin-right: @spacing-25;
		}
 
		&__text {
			font-weight: @font-weight-bold;
		}
	}
 
	legend& {
		// Unset <legend> browser style.
		padding: 0;
	}
 
	// Labels of nested fields within a fieldset should not be bolded.
	fieldset label&__label &__label__text {
		font-weight: @font-weight-normal;
	}
 
	&:not( .cdx-label--disabled ) {
		.cdx-label__label__optional-flag,
		.cdx-label__description {
			color: @color-subtle;
		}
	}
 
	&--disabled {
		&,
		.cdx-label__label__icon {
			color: @color-disabled;
		}
	}
 
	&--visually-hidden {
		.screen-reader-text();
	}
 
	&:not( .cdx-label--visually-hidden ) {
		padding-bottom: @spacing-50;
 
		@media screen and ( min-width: @min-width-breakpoint-tablet ) {
			padding-bottom: @spacing-25;
		}
	}
}
</style>