Skip to content

Commit 2ce8a18

Browse files
committed
Scroll jumping fixed, fixed maxLength
1 parent a22efd5 commit 2ce8a18

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class UUITextareaElement extends LabelMixin('input label', LitElement) {
162162
* @default undefined
163163
*/
164164
@property({ type: Number })
165-
maxLength: number | undefined = undefined;
165+
maxLength = 0;
166166

167167
/**
168168
* Enables automatic height adjustment. The height will be confined within the min and max height if defined.
@@ -206,13 +206,26 @@ export class UUITextareaElement extends LabelMixin('input label', LitElement) {
206206
}
207207

208208
private autoUpdateHeight() {
209+
const host = this.shadowRoot!.host! as HTMLElement;
209210
const input = this._textarea;
210211

212+
// Temporarily lock the height of the shadowroot host to prevent
213+
// the page scroll from moving when changing the textarea height
214+
const scrollTop = host.scrollTop;
215+
const hostHeight = getComputedStyle(host).height;
216+
host.style.display = 'block';
217+
host.style.height = hostHeight;
218+
211219
input.style.height = 'auto';
212220

213221
if (input.scrollHeight > input.clientHeight) {
214222
input.style.height = input.scrollHeight + 'px';
215223
}
224+
225+
// Reset host styles and scroll to where we were
226+
host.style.removeProperty('display');
227+
host.style.removeProperty('height');
228+
host.scrollTop = scrollTop;
216229
}
217230

218231
renderMaxLength() {
@@ -227,7 +240,7 @@ export class UUITextareaElement extends LabelMixin('input label', LitElement) {
227240
return html`
228241
${this.hideLabel === false ? this.renderLabel() : ''}
229242
<textarea
230-
maxlength=${ifDefined(this.maxLength)}
243+
maxlength=${ifDefined(this.maxLength > 0 ? this.maxLength : undefined)}
231244
style="min-height: ${this.minHeight}; max-height: ${this.maxHeight}"
232245
id="textarea"
233246
.value=${this.value}
@@ -238,7 +251,7 @@ export class UUITextareaElement extends LabelMixin('input label', LitElement) {
238251
@input=${this.onInput}
239252
@change=${this.onChange}>
240253
</textarea>
241-
${this.maxLength ? this.renderMaxLength() : ''}
254+
${this.maxLength > 0 ? this.renderMaxLength() : ''}
242255
`;
243256
}
244257
}

0 commit comments

Comments
 (0)