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

Improve Textarea Behavior #4845

Closed
wants to merge 13 commits into from
Closed

Improve Textarea Behavior #4845

wants to merge 13 commits into from

Conversation

Sukhendu2002
Copy link

Changes
This PR addresses issues related to the custom textarea behavior:

  • Prevents the cursor from jumping to the top of the screen when interacting with the text area.
  • Resizes the text area when content is removed, ensuring a better user experience.

Issues
Fixes #4740

@Sukhendu2002 Sukhendu2002 requested a review from a team as a code owner October 5, 2023 18:40
Comment on lines 70 to 84
textarea.addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
const currentCursorPosition = textarea.selectionStart;
const textBeforeCursor = textarea.value.substring(0, currentCursorPosition);
const textAfterCursor = textarea.value.substring(currentCursorPosition);

const newTextValue = textBeforeCursor + '\n' + textAfterCursor;
textarea.value = newTextValue;

const newCursorPosition = currentCursorPosition + 1;
textarea.setSelectionRange(newCursorPosition, newCursorPosition);

autogrowFn();
}
});
Copy link
Contributor

Choose a reason for hiding this comment

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

This doubles the new lines and seems unnecessary.
Or maybe I haven't found the steps to reproduce what it is supposed to fix. 🤷‍♂️

Copy link
Author

Choose a reason for hiding this comment

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

When we add a new line in the text area the text jumps to the top of the screen, it is supposed to fix that.

2023-10-06.17-22-22.mp4

Copy link
Contributor

@dmitrylyzo dmitrylyzo Oct 6, 2023

Choose a reason for hiding this comment

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

It doesn't jump without keydown in Chrome and Firefox. 🤷‍♂️

UPD:
Ahh, after pasting a big wall of text it starts jumping.

Copy link
Author

Choose a reason for hiding this comment

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

yea

Copy link
Author

Choose a reason for hiding this comment

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

So, it is pretty much done

Copy link
Contributor

@dmitrylyzo dmitrylyzo Oct 12, 2023

Choose a reason for hiding this comment

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

Writing by memory

        if ((textarea.scrollHeight - offset) > self.maxAllowedHeight) {
            textarea.style.overflowY = 'scroll';
            newHeight = self.maxAllowedHeight;
        } else {
            textarea.style.overflowY = 'hidden';
            stub.style.height = textarea.style.height;
            textarea.style.height = 'auto';    // to shrink the textarea
            newHeight = textarea.scrollHeight;
        }
        textarea.style.height = newHeight + 'px';
        stub.style.height = '0px';    // here is an error in the reference comment

Copy link
Author

Choose a reason for hiding this comment

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

still getting the jumping problem

Copy link
Contributor

Choose a reason for hiding this comment

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

still getting the jumping problem

On shrinking (deleting text) or on growing (adding text)?
The stub hack is for shrinking. I tried with your keydown hack.

Copy link
Author

Choose a reason for hiding this comment

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

on growing

Copy link
Contributor

Choose a reason for hiding this comment

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

I have this change with the current state of your branch.

diff --git a/src/elements/emby-textarea/emby-textarea.js b/src/elements/emby-textarea/emby-textarea.js
index 80e79e115..3189cc22f 100644
--- a/src/elements/emby-textarea/emby-textarea.js
+++ b/src/elements/emby-textarea/emby-textarea.js
@@ -19,7 +19,7 @@ function calculateOffset(textarea) {
     return offset;
 }
 
-function AutoGrow(textarea, maxLines) {
+function AutoGrow(textarea, stub, maxLines) {
     const self = this;
 
     if (maxLines === undefined) {
@@ -52,19 +52,17 @@ function AutoGrow(textarea, maxLines) {
             newHeight = self.maxAllowedHeight;
         } else {
             textarea.style.overflowY = 'hidden';
-            // textarea.style.height = 'auto';
+            stub.style.height = textarea.style.height;
+            textarea.style.height = 'auto';
             newHeight = textarea.scrollHeight/* - offset*/;
         }
         textarea.style.height = newHeight + 'px';
+        stub.style.height = '0px';
     }
 
     // Call autogrowFn() when textarea's value is changed
     textarea.addEventListener('input', autogrowFn);
     textarea.addEventListener('focus', autogrowFn);
-    textarea.addEventListener('blur', () => {
-        textarea.style.height = 'auto';
-        autogrowFn();
-    });
     textarea.addEventListener('valueset', autogrowFn);
     textarea.addEventListener('keydown', function(event) {
         if (event.key === 'Enter') {
@@ -132,6 +130,9 @@ EmbyTextAreaPrototype.attachedCallback = function () {
     label.htmlFor = this.id;
     parentNode.insertBefore(label, this);
 
+    const stub = this.ownerDocument.createElement('div');
+    parentNode.insertBefore(stub, this.nextSibling);
+
     this.addEventListener('focus', function () {
         label.classList.add('textareaLabelFocused');
         label.classList.remove('textareaLabelUnfocused');
@@ -145,7 +146,7 @@ EmbyTextAreaPrototype.attachedCallback = function () {
         label.innerText = text;
     };
 
-    new AutoGrow(this);
+    new AutoGrow(this, stub);
 };
 
 document.registerElement('emby-textarea', {

But I noticed that the focus doesn't follow the cursor on Enter, when it is at the bottom of the window.

Also, the cursor can hide under the top bar. But this is another story.

src/elements/emby-textarea/emby-textarea.js Outdated Show resolved Hide resolved
@dmitrylyzo dmitrylyzo added enhancement Improve existing functionality or small fixes ui & ux This PR or issue mainly concerns UI & UX labels Oct 6, 2023
@jellyfin-bot
Copy link
Collaborator

Cloudflare Pages deployment

Latest commit f3db787
Status ✅ Deployed!
Preview URL https://890fbd91.jellyfin-web.pages.dev
Type 🔀 Preview

View build logs
View bot logs

Copy link

sonarcloud bot commented Nov 5, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@Sukhendu2002 Sukhendu2002 closed this by deleting the head repository Dec 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve existing functionality or small fixes hacktoberfest-accepted ui & ux This PR or issue mainly concerns UI & UX
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Issue]: CSS Container Jumping when Interacting with the container
4 participants