Fix textarea resizing bug (#2182)

* fix textarea resizing issue

* add changelog entry

* prettier

* remove unnecessary aria-hidden

* prettier
pull/2209/head
Konnor Rogers 2024-10-04 16:21:56 -04:00 zatwierdzone przez GitHub
rodzic 1fb72ad4eb
commit 61f2fdbca1
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
3 zmienionych plików z 18 dodań i 2 usunięć

Wyświetl plik

@ -14,6 +14,7 @@ New versions of Shoelace are released as-needed and generally occur when a criti
## Next
- Fixed a bug in `<sl-textarea>` causing scroll jumping when using `resize="auto"` [#2182]
- Fixed a bug in `<sl-relative-time>` where the title attribute would show with redundant info [#2184]
## 2.17.1

Wyświetl plik

@ -46,6 +46,7 @@ export default class SlTextarea extends ShoelaceElement implements ShoelaceFormC
private resizeObserver: ResizeObserver;
@query('.textarea__control') input: HTMLTextAreaElement;
@query('.textarea__size-adjuster') sizeAdjuster: HTMLTextAreaElement;
@state() private hasFocus = false;
@property() title = ''; // make reactive to pass through
@ -196,6 +197,8 @@ export default class SlTextarea extends ShoelaceElement implements ShoelaceFormC
private setTextareaHeight() {
if (this.resize === 'auto') {
// This prevents layout shifts. We use `clientHeight` instead of `scrollHeight` to account for if the `<textarea>` has a max-height set on it. In my tests, this has worked fine. Im not aware of any edge cases. [Konnor]
this.sizeAdjuster.style.height = `${this.input.clientHeight}px`;
this.input.style.height = 'auto';
this.input.style.height = `${this.input.scrollHeight}px`;
} else {
@ -370,6 +373,8 @@ export default class SlTextarea extends ShoelaceElement implements ShoelaceFormC
@focus=${this.handleFocus}
@blur=${this.handleBlur}
></textarea>
<!-- This "adjuster" exists to prevent layout shifting. https://github.com/shoelace-style/shoelace/issues/2180 -->
<div part="textarea-adjuster" class="textarea__size-adjuster" ?hidden=${this.resize !== 'auto'}></div>
</div>
</div>

Wyświetl plik

@ -6,7 +6,7 @@ export default css`
}
.textarea {
display: flex;
display: grid;
align-items: center;
position: relative;
width: 100%;
@ -55,6 +55,17 @@ export default css`
cursor: not-allowed;
}
.textarea__control,
.textarea__size-adjuster {
grid-area: 1 / 1 / 2 / 2;
}
.textarea__size-adjuster {
visibility: hidden;
pointer-events: none;
opacity: 0;
}
.textarea--standard.textarea--disabled .textarea__control {
color: var(--sl-input-color-disabled);
}
@ -87,7 +98,6 @@ export default css`
}
.textarea__control {
flex: 1 1 auto;
font-family: inherit;
font-size: inherit;
font-weight: inherit;