Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/712 fix scrolling #791

Merged
merged 6 commits into from
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/ui-library/src/components/forms/textarea/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,28 @@ export class BlrTextarea extends LitElement {
}

protected updateCounter() {
const scrollTop = this.textareaElement?.scrollTop;
const isFocused = this.textareaElement === document.activeElement;

const length = this.textareaElement?.value?.length;
if (length !== undefined) {
this.count = length;
}

const shouldUpdateTextarea = this.textareaElement && scrollTop !== undefined;

if (shouldUpdateTextarea) {
requestAnimationFrame(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
this.textareaElement.scrollTop = scrollTop;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i quite dont understand why this is needed...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"requestAnimationFrame" ensures that the update to the scroll position is synchronized with the browser's rendering cycle. This method waits for the browser to be ready to render the next frame before making changes to the DOM.

"this.textareaElement.scrollTop = scrollTop"; is to preserve the current scroll position of the <textarea>. The scrollTop variable captures the scroll position of the textarea before any update

In summary, this code effectively resolves the Firefox-specific issue of the scrollbar jumping to the top on user input

if (isFocused) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
this.textareaElement.focus();
}
});
}
}

protected determinateCounterVariant(): CounterVariantType {
Expand Down
Loading