dotnet / dotnet/maui

WebView.Focus() & Unfocus() behaves inconsistently across platforms

Open
#36,201 1 comment 2 reactions 0 assignees View on GitHub
area-controls-webview partner/syncfusion platform/android platform/ios platform/macos platform/windows s/triaged s/verified t/bug
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

## Description
The behavior of WebView.Focus() and Unfocus() differs across platforms.

On Android and Windows, calling Focus() on a WebView often results in focus being applied to the web content (for example, previously active elements may regain focus and editable areas may show a cursor).

On iOS and MacCatalyst, calling Focus() does not produce the same observable effect in the web content.

This leads to inconsistent behavior when managing focus in cross-platform applications using WebView.

## Expected Behavior

WebView.Focus() should behave consistently across platforms when interacting with web content, including editable elements such as contenteditable regions.

## Actual Behavior

- Android:
Focus commonly affects the WebView content. Editable elements such as contenteditable divs may regain focus and display a cursor.

- Windows:
Similar behavior to Android. Focus may be reflected within the page and editable elements can become active.

- iOS / MacCatalyst:
The same effect is not observed in the web content after calling Focus(), including for contenteditable elements.

## Additional Information / Workaround

As a workaround, we can explicitly focus the editable element using JavaScript:
await webView.EvaluateJavaScriptAsync("document.getElementById('editor').focus();");
await webView.EvaluateJavaScriptAsync("document.getElementById('editor').blur();");

This works on iOS and MacCatalyst.

However, this approach does not align with the behavior observed on Android and Windows, where calling WebView.Focus() alone is sufficient for the web content to regain focus. Because of this difference, additional platform-specific handling is required to achieve consistent behavior.

https://github.com/user-attachments/assets/fe593feb-ea15-4c42-b503-4342ac346804

https://github.com/user-attachments/assets/9c39c1bb-29ec-4e6e-bb08-96342c8e9af0

https://github.com/user-attachments/assets/eae7eb7e-0efc-42b0-94b9-3acaaeecdb92

https://github.com/user-attachments/assets/e1507638-c907-430e-9e2d-f50ec635a72d

### Steps to Reproduce

1. Run the attached project file.
2. Press the buttons provided to programmatic focus and unfocus methods.
3. You can observe that the programmatic focus works in Windows and Android once the editable Dom content is manually focused before triggering focus method.
4. You can observe that the programmatic focus won't work on mac and iOS even we focus the editable content manually before triggering focus method.

[SimpleSample (4).zip](https://github.com/user-attachments/files/29456195/SimpleSample.4.zip)

### Link to public reproduction project repository

_No response_

### Version with bug

10.0.70

### Is this a regression from previous behavior?

Not sure, did not test other versions

### Last version that worked well

Unknown/Other

### Affected platforms

I was *not* able test on other platforms

### Affected platform versions

_No response_

### Did you find any workaround?

_No response_

### Relevant log output

```shell

```

Contributor guide

Open the contributing guide

Research direction

Start with the WebView.Focus() and Unfocus() entry points across Android, Windows, iOS, and MacCatalyst, then reproduce the behavior using the attached SimpleSample project. Compare the platform-specific focus behavior with the contenteditable case and the EvaluateJavaScriptAsync workaround. Done means programmatic WebView focus and unfocus produce consistent observable results in the web content across the affected platforms.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop, mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.