Skip to content

Commit

Permalink
Fix textarea resizing bug (#2182)
Browse files Browse the repository at this point in the history
* fix textarea resizing issue

* add changelog entry

* prettier

* remove unnecessary aria-hidden

* prettier
  • Loading branch information
KonnorRogers authored Oct 4, 2024
1 parent 1fb72ad commit 61f2fdb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/pages/resources/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/components/textarea/textarea.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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>
Expand Down
14 changes: 12 additions & 2 deletions src/components/textarea/textarea.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default css`
}
.textarea {
display: flex;
display: grid;
align-items: center;
position: relative;
width: 100%;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -87,7 +98,6 @@ export default css`
}
.textarea__control {
flex: 1 1 auto;
font-family: inherit;
font-size: inherit;
font-weight: inherit;
Expand Down

0 comments on commit 61f2fdb

Please sign in to comment.