Skip to content

Commit 3917c08

Browse files
authored
Textarea font size and auto-height changes (#597)
* font size * auto-height * styles moved back to top. font-size is inherit instead * height
1 parent 1e4dac6 commit 3917c08

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

packages/uui-textarea/lib/uui-textarea.element.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import { ifDefined } from 'lit/directives/if-defined.js';
1313
* @cssprop --uui-textarea-min-height - Sets the minimum height of the textarea
1414
* @cssprop --uui-textarea-max-height - Sets the maximum height of the textarea
1515
* @cssprop {color} --uui-textarea-background-color - Sets the background color of the textarea
16+
* @cssprop --uui-textarea-font-size - Overwrites the default font size
1617
*/
18+
1719
@defineElement('uui-textarea')
1820
export class UUITextareaElement extends FormControlMixin(LitElement) {
1921
/**
@@ -71,10 +73,10 @@ export class UUITextareaElement extends FormControlMixin(LitElement) {
7173
box-sizing: border-box;
7274
min-width: 100%;
7375
max-width: 100%;
74-
font-size: var(--uui-size-5);
76+
font-size: inherit;
7577
padding: var(--uui-size-2);
7678
border: 1px solid
77-
var(--uui-textarea-border-color, var(--uui-color-border));
79+
var(--uui-textarea-border-color, var(--uui-color-border)); /** Note: Specified border size is needed and hardcoded in autoUpdateHeight() */
7880
border-radius: 0;
7981
outline: none;
8082
min-height: var(--uui-textarea-min-height);
@@ -262,6 +264,12 @@ export class UUITextareaElement extends FormControlMixin(LitElement) {
262264
if (!this.label) {
263265
console.warn(this.tagName + ' needs a `label`', this);
264266
}
267+
268+
if (this.autoHeight) {
269+
requestAnimationFrame(() => {
270+
this.autoUpdateHeight();
271+
});
272+
}
265273
}
266274

267275
/**
@@ -305,8 +313,11 @@ export class UUITextareaElement extends FormControlMixin(LitElement) {
305313

306314
input.style.height = 'auto';
307315

308-
if (input.scrollHeight > input.clientHeight) {
309-
input.style.height = input.scrollHeight + 'px';
316+
// Note: Add + 2 because of the border top+bottom 1px each
317+
if (input.scrollHeight + 2 > input.clientHeight) {
318+
input.style.height = input.scrollHeight + 2 + 'px';
319+
} else if (input.scrollHeight + 2 < input.clientHeight) {
320+
input.style.removeProperty('height');
310321
}
311322

312323
// Reset host styles and scroll to where we were
@@ -319,8 +330,8 @@ export class UUITextareaElement extends FormControlMixin(LitElement) {
319330
return html`
320331
<textarea
321332
id="textarea"
322-
.rows=${this.rows}
323-
.cols=${this.cols}
333+
rows=${ifDefined(this.rows)}
334+
cols=${ifDefined(this.cols)}
324335
.value=${this.value as string}
325336
.name=${this.name}
326337
wrap=${ifDefined(this.wrap)}

0 commit comments

Comments
 (0)