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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | 4x 26x 26x 26x 26x 4x 16x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 1x 1x 26x 1x 26x 26x 26x 26x 3x 1x 3x 3x 26x 1x 1x 2x 2x 2x 4x 4x 4x 4x 4x 26x 26x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 4x | <template> <div class="cdx-text-area" :class="rootClasses" :style="rootStyleE" >E <textarea :id="computedInputId" ref="textarea" v-bind="otherAttrsMinusId" v-model="wrappedModel" :class="textareaClasses" class="cdx-text-area__textarea" :aria-describedby="descriptionId" :disabled="computedDisabled" @input="onInput" @change="onChange" @focus="onFocus" @blur="onBlur" @invalid="( e ) => onInvalid( e, shouldPreventDefault )" /> <cdx-icon v-if="startIcon" :icon="startIcon" class="cdx-text-area__icon-vue cdx-text-area__start-icon" /> <cdx-icon v-if="endIcon" :icon="endIcon" class="cdx-text-area__icon-vue cdx-text-area__end-icon" /> </div> </template> <script lang="ts"> import { defineComponent, computed, ref, toRef, PropType, inject } from 'vue'; import CdxIcon from '../icon/Icon.vue'; import { Icon } from '@wikimedia/codex-icons'; import useSplitAttributes from '../../composables/useSplitAttributes'; import useModelWrapper from '../../composables/useModelWrapper'; import useFieldData from '../../composables/useFieldData'; import { ValidationStatusType } from '../../types'; import { ValidationStatusTypes, FieldDescriptionIdKey } from '../../constants'; import { makeStringTypeValidator } from '../../utils/stringTypeValidator'; const statusValidator = makeStringTypeValidator( ValidationStatusTypes ); /** * Multi-line text input that allows manual resizing. * * This form element is useful when you want to create a sizeable amount of free-form text, * for example comments, reviews, feedback, or short essay responses. */ export default defineComponent( { name: 'CdxTextArea', components: { CdxIcon }, inheritAttrs: false, // expose is temporarily disabled to work around a Vue / vue-tsc bug, see // https://github.com/vuejs/language-tools/issues/5069 /* expose: [ 'focus', 'blur', 'checkValidity', 'reportValidity', 'setCustomValidity' ], */ props: { /** * Current value of the textarea. * * Provided by `v-model` binding in the parent component. */ modelValue: { type: String, default: '' }, /** * `status` attribute of the textarea. */ status: { type: String as PropType<ValidationStatusType>, default: 'default', validator: statusValidator }, /** * Whether the textarea is disabled. */ disabled: { type: Boolean, default: false }, /** * Describes whether the textarea grows vertically to show all text. * * When autosize is true, the textarea automatically grows in height (vertically). * The height of the textarea expands while the user types in the textarea. * The content inside the textarea is visible and there's no scroll. */ autosize: { type: Boolean, default: false }, /** * An icon at the start of the `<textarea>` element. Similar to a `::before` pseudo-element. */ startIcon: { type: [ String, Object ] as PropType<Icon>, default: undefined }, /** * An icon at the end of the `<textarea>` element. Similar to an `::after` pseudo-element. */ endIcon: { type: [ String, Object ] as PropType<Icon>, default: undefined } }, emits: [ /** * When the textarea value changes. * * @property {string} modelValue The new model value */ 'update:modelValue', /** * When the input value changes via direct use of the input * * @property {InputEvent} event */ 'input', /** * When an input value change is committed by the user (e.g. on blur) * * @property {Event} event */ 'change', /** * When the input comes into focus * * @property {FocusEvent} event */ 'focus', /** * When the input loses focus * * @property {FocusEvent} event */ 'blur', /** * When the textarea value is invalid according to the textarea's constraint * attributes (e.g. minlength, maxlength, or required). See: * https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation#validation-related_attributes * * @property {Event} event */ 'invalid' ], setup( props, { attrs, emit } ) { const textarea = ref<HTMLTextAreaElement>(); // Take the modelValue provided by the parent component via v-model and // generate a wrapped model that we can use for the `<textarea>` element in // this component. const wrappedModel = useModelWrapper( toRef( props, 'modelValue' ), emit ); // If there is a parent Field component, it may be providing some data to this component. // Grab computed values of each relevant property. const idAttribute = attrs.id as string|undefined; const { computedDisabled, computedStatus, computedInputId } = useFieldData( toRef( props, 'disabled' ), toRef( props, 'status' ), idAttribute ); const descriptionId = inject( FieldDescriptionIdKey, undefined ); I const textareaClasses = computed( () => ( { 'cdx-text-area__textarea--has-value': !!wrappedModel.value, 'cdx-text-area__textarea--is-autosize': props.autosize } ) ); const internalClasses = computed( () => ( { 'cdx-text-area--status-default': computedStatus.value === 'default', 'cdx-text-area--status-error': computedStatus.value === 'error', 'cdx-text-area--has-start-icon': !!props.startIcon, 'cdx-text-area--has-end-icon': !!props.endIcon } ) ); // Get helpers from useSplitAttributes() composable. const { rootClasses, rootStyle, otherAttrs } = useSplitAttributes( attrs, internalClasses ); // Normally, we use v-bind to bind otherAttrs to the appropriate element. In this case, we // do not want to include the id attribute, since we're using the computed ID via the // useComputedId composable. // This removes the ID and stores all other attributes in otherAttrsMinusId. const otherAttrsMinusId = computed( () => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { id, ...everythingElse } = otherAttrs.value; return everythingElse; } ); // Allows the textarea to grow aka auto-resize while typing. function onInput( event: Event ) { if ( textarea.value && props.autosize ) { textarea.value.style.height = 'auto'; textarea.value.style.height = `${ textarea.value.scrollHeight }px`; } emit( 'input', event ); } const onChange = ( event: Event ) => { emit( 'change', event ); }; const onFocus = ( event: FocusEvent ) => { emit( 'focus', event ); }; const onBlur = ( event: FocusEvent ) => { emit( 'blur', event ); }; // We want to prevent the default behavior of the invalid event by default, to prevent the // native validation message UI from displaying unless it's explicitly called. If // `reportValidity()` is used, this ref will be set to false. const shouldPreventDefault = ref( true ); /** * Handle the `invalid` event. * * Note that we use an argument for `doPreventDefault` instead of the `shouldPreventDefault` * ref so we can unit test this. * * @param event Invalid event * @param doPreventDefault Whether default behavior should be prevented */ const onInvalid = ( event: Event, doPreventDefault: boolean ) => { if ( doPreventDefault ) { event.preventDefault(); } emit( 'invalid', event ); shouldPreventDefault.value = true; }; return { textarea, rootClasses, rootStyle, wrappedModel, computedDisabled, computedInputId, descriptionId, textareaClasses, otherAttrsMinusId, onInput, onChange, onFocus, onBlur, onInvalid, shouldPreventDefault }; }, // Public methods // These must be in the methods block, not in the setup function, otherwise their documentation // won't be picked up by vue-docgen methods: { /** * Focus the component's `<textarea>` element. * * @public */ focus(): void { const textarea = this.$refs.textarea as HTMLTextAreaElement; textarea.focus(); }, /** * Blur the component's `<textarea>` element. * * @public */ blur(): void { const textarea = this.$refs.textarea as HTMLTextAreaElement; textarea.blur(); }, /** * Check the validity of the `<textarea>` element according to its constraint attributes. * Emits an 'invalid' event if the textarea is invalid. See: * https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement/checkValidity * * @public * @return {boolean} Whether the textarea is valid */ checkValidity(): boolean { const textarea = this.$refs.textarea as HTMLTextAreaElement; return textarea.checkValidity(); }, /** * Check the validity of the `<textarea>` element and report it as a pop up on the UI. See: * https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement/reportValidity * * @public * @return {boolean} Whether the textarea is valid */ reportValidity(): boolean { // Ensure default behavior of the invalid event is not prevented, so the native UI can // pop up. this.shouldPreventDefault = false; const textarea = this.$refs.textarea as HTMLTextAreaElement; return textarea.reportValidity(); }, /** * Set custom validity and message for the `<textarea>` element. See: * https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement/setCustomValidity * * @public * @param {string} message The custom validation message to set */ setCustomValidity( message: string ): void { const textarea = this.$refs.textarea as HTMLTextAreaElement; textarea.setCustomValidity( message ); } } } ); </script> <style lang="less"> @import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less'; @import ( reference ) '../../themes/mixins/icon-alignment.less'; @import ( reference ) '../../themes/mixins/public/css-icon.less'; @import ( reference ) '../../themes/mixins/public/typography.less'; .cdx-text-area { // Added for positioning of icons. position: relative; // Set `min-width` here instead of on the `<textarea>` element so that the TextArea's width // is consistent regardless of whether the start/end icons are present. min-width: @min-width-medium; // Added double ampersand to add specificity to Vue and CSS start icons & &__start-icon { .cdx-mixin-icon( start, @param-external-padding: @spacing-50 + @border-width-base, @param-top: @spacing-25, // Set the height of the icon to the line-height of the accompanying text // to ensure centering of the icon to text. @param-height: @line-height-medium ); } // Added double ampersand to add specificity to Vue and CSS end icons & &__end-icon { .cdx-mixin-icon( end, @min-size-icon-small, @size-icon-small, @spacing-50 + @border-width-base, @spacing-25, // Set the height of the icon to the line-height of the accompanying text // to ensure centering of the icon to text. @line-height-medium ); } // Ensure background image rules for CSS start icon &__icon&__start-icon { .cdx-mixin-css-icon-background(); } // Ensure CSS end icon is size small and it's height is consistent with the Vue icon. &__icon&__end-icon { // Note: the icon-size mixin overrides the height property @size-150 (via cdx-mixin-icon) // therefore, we override height again to get the desired height of @size-150 .cdx-mixin-css-icon-size( @size-icon-small ); .cdx-mixin-css-icon-background( @size-icon-small ); } &__textarea { display: block; box-sizing: @box-sizing-base; min-height: @min-height-text-area; width: @size-full; border-width: @border-width-base; border-style: @border-style-base; border-radius: @border-radius-base; padding: @spacing-25 @spacing-50; overflow: auto; .cdx-mixin-body-text(); // TODO: Support Safari/Webkit for iOS. The grabber tool is not present in mobile safari. /* stylelint-disable-next-line plugin/no-unsupported-browser-features */ resize: vertical; &--is-autosize { /* stylelint-disable-next-line plugin/no-unsupported-browser-features */ resize: none; overflow: hidden; // Support Safari/Webkit - supports macOS only &::-webkit-resizer { display: none; } } &:enabled { background-color: @background-color-base; color: @color-base; border-color: @border-color-interactive; box-shadow: @box-shadow-inset-small @box-shadow-color-transparent; transition-property: @transition-property-base; transition-duration: @transition-duration-medium; ~ .cdx-text-area__icon-vue.cdx-icon { color: @color-placeholder; } ~ .cdx-text-area__icon { opacity: @opacity-icon-placeholder; } &:hover { border-color: @border-color-interactive--hover; } &:focus, &.cdx-text-area__textarea--has-value { ~ .cdx-text-area__icon-vue.cdx-icon { color: @color-subtle; } ~ .cdx-text-area__icon { opacity: @opacity-base; } } &:focus { border-color: @border-color-progressive--focus; box-shadow: @box-shadow-inset-small @box-shadow-color-progressive--focus; outline: @outline-base--focus; } &:read-only { background-color: @background-color-neutral-subtle; border-color: @border-color-base; } } /* stylelint-disable-next-line no-descending-specificity */ &:disabled { background-color: @background-color-disabled-subtle; color: @color-disabled; -webkit-text-fill-color: @color-disabled; border-color: @border-color-disabled; /* stylelint-disable-next-line no-descending-specificity */ ~ .cdx-text-area__icon-vue.cdx-icon { color: @color-disabled; } /* stylelint-disable-next-line no-descending-specificity */ ~ .cdx-text-area__icon { opacity: @opacity-icon-base--disabled; } } // Normalize placeholder styling, see T139034. &::placeholder { color: @color-placeholder; opacity: @opacity-base; } } // Add additional padding to textarea when the start icon exists. // Sets the start icon to 1.25em relative to the font size. &--has-start-icon { .cdx-text-area__textarea { .cdx-mixin-icon-wrapper-padding( start, @spacing-50 ); } } // Add additional padding to textarea when the end icon exists. // Sets the end icon to 1em relative to the font size. &--has-end-icon { .cdx-text-area__textarea { .cdx-mixin-icon-wrapper-padding( end, @spacing-50, @size-icon-small ); } } &--status-error { .cdx-text-area__textarea:enabled:not( :read-only ):not( :focus ) { background-color: @background-color-error-subtle; color: @color-error; border-color: @border-color-error; &::placeholder, ~ .cdx-text-area__icon-vue.cdx-icon { color: @color-error; } &:hover { background-color: @background-color-error-subtle--hover; color: @color-error--hover; border-color: @border-color-error--hover; &::placeholder, ~ .cdx-text-area__icon-vue.cdx-icon { color: @color-error--hover; } } } } } </style> |