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

[TextField] Cursor jumps to beginning when clicking outside the default line-height area #44395

Open
Austin1serb opened this issue Nov 12, 2024 · 4 comments
Assignees
Labels
browser: Safari component: text field This is the name of the generic UI component, not the React module! external dependency Blocked by external dependency, we can’t do anything about it

Comments

@Austin1serb
Copy link

Austin1serb commented Nov 12, 2024

Steps to reproduce

Link to live example: (required)
Go to the TextField demo page on MUI’s website.

Steps:

  1. Type some text into any TextField component.
  2. Click above the input area, especially outside the text’s line-height area (e.g., towards the top of the field).
Screen.Recording.2024-11-12.at.9.56.12.AM.mov

3 .Observe the cursor behavior—it should ideally position itself where you clicked but instead jumps to the beginning of the text. Unless you click in the small native input line height.

Current behavior

When using the TextField component, clicking within the input area but outside the default line-height (e.g., in the top half of the input area) causes the cursor to jump to the beginning of the text instead of positioning where the click occurred. This behavior causes the input field to respond in an unexpected and inconsistent way for users.

Expected behavior

The cursor should appear at the exact location where the user clicks within the input area, aligning with standard input field behavior and enhancing user experience.

Context

This issue affects the user experience by causing unexpected cursor jumps within the input area. It occurs even with the default TextField styling, making it a general issue in applications that use MUI’s TextField component.

Your environment

npx @mui/envinfo
  System:
    OS: macOS 14.6.1
  Binaries:
    Node: 20.16.0 - /opt/homebrew/opt/node@20/bin/node
    npm: 10.8.1 - /opt/homebrew/opt/node@20/bin/npm
    pnpm: Not Found
  Browsers:
    Chrome: 130.0.6723.117
    Edge: Not Found
    Safari: 17.6
  npmPackages:
    @emotion/react: ^11.11.3 => 11.11.3 
    @emotion/styled: ^11.11.0 => 11.11.0 
    @mui/base: 5.0.0-beta.61 
    @mui/core-downloads-tracker: 6.1.6 
    @mui/icons-material: ^6.1.6 => 6.1.6 
    @mui/lab: ^6.0.0-beta.14 => 6.0.0-beta.14 
    @mui/material: ^6.1.6 => 6.1.6 
    @mui/private-theming: 6.1.6 
    @mui/styled-engine: 6.1.6 
    @mui/system: ^6.1.6 => 6.1.6 
    @mui/types: 7.2.19 
    @mui/utils: 5.15.9 
    @mui/x-charts: ^6.18.7 => 6.18.7 
    @mui/x-data-grid: ^6.18.4 => 6.18.6 
    @types/react: 18.3.12 
    react: ^18.2.0 => 18.2.0 
    react-dom: ^18.2.0 => 18.2.0 
    typescript: 4.9.5 

Search keywords: TextField cursor placement , TextField line-height issue, TextField cursor misalignment, MUI TextField cursor jump

@Austin1serb Austin1serb added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 12, 2024
@Austin1serb
Copy link
Author

Austin1serb commented Nov 12, 2024

I would love to work on fixing this issue. I’ve noticed that this behavior also occurs in native HTML when adding height to a text input without adjusting the line-height. My proposed solution is to set the line-height to match the component’s height, which should resolve the cursor alignment issue. Please let me know if I should proceed. Thank you!

@Austin1serb
Copy link
Author

Additional note: I’ve also noticed that this issue persists on iOS devices(Safari & Chrome). The cursor alignment behaves the same way on mobile, where clicking outside the text’s line-height area causes the cursor to jump to the beginning of the text input. This makes the issue relevant for both desktop and mobile users.

@siriwatknp siriwatknp added component: text field This is the name of the generic UI component, not the React module! external dependency Blocked by external dependency, we can’t do anything about it and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 13, 2024
@siriwatknp
Copy link
Member

I’ve noticed that this behavior also occurs in native HTML when adding height to a text input without adjusting the line-height

I am marking this issue as external since it is also happen with native HTML input. I think there should be a better solution than setting the line-height to match the component's height.

@Austin1serb
Copy link
Author

@siriwatknp

I’ve noticed that this behavior also occurs in native HTML when adding height to a text input without adjusting the line-height

I am marking this issue as external since it also happens with native HTML input. I think there should be a better solution than setting the line-height to match the component's height.

After extensive testing with both CSS and JavaScript solutions, none have matched the simplicity and effectiveness of the line-height workaround for managing cursor alignment in oversized input fields.

This cursor alignment issue isn’t exclusive to inputs with increased height. Even with default height, clicking just above or below the text triggers misaligned cursor behavior, as described in this Stack Overflow post: Odd cursor placement behavior in HTML input.

JavaScript alternatives often introduce flashing or prevent clicking in the middle of the text. The line-height adjustment aligns the cursor area directly with the input’s visual height, effectively resolving the issue caused by HTML's native behavior that ties cursor placement to line-height rather than height.

However, this workaround has a caveat: Highlighting the text now looks like this:

Screenshot 2024-11-13 at 1 32 42 PM

I also noticed that this workaround seems to address issue #23096 as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
browser: Safari component: text field This is the name of the generic UI component, not the React module! external dependency Blocked by external dependency, we can’t do anything about it
Projects
None yet
Development

No branches or pull requests

2 participants