Developers can use the focus management capability of the Web component to effectively manage focus gain and focus loss of the Web component, and use the W3C standard APIs on the H5 side to manage focus gain and focus loss of the only interactive element on the web page.
Application scenarios of common APIs for controlling the focus of the Web component and ArkUI component:
Application scenarios of common APIs for controlling the focus of the HTML5 element in the Web component:
For details about the focus, focus chain, and focus navigation of the Web component, see Basic Concepts of ArkUI Focus.
Focus traversal can be divided into active and passive based on how it is triggered. For details, see Focus Traversal Guidelines.
Refers to focus movement initiated by deliberate actions, such as keyboard shortcuts (Tab or Shift+Tab) and clicks or touches through (the gesture, mouse, or touchpad).
requestFocus
Moves focus to a specific component. For details, see Controlling the Focus of the Web Component and ArkUI Component.
Keyboard traversal
Click/Touch for focus
Users can use gestures, the mouse, or touchpad to click/touch a Web component to obtain the focus. Elements in the Web component can also be focused when being clicked/touched. For example, an input box in a web page can be clicked/touched to change from a non-editable state to an editable state and activate the input method.
Passive focus traversal occurs when the focus automatically shifts due to system actions or other operations without manual intervention, reflecting the default behavior of the focus system.
Passive focus traversal occurs in the following scenarios:
Component removal: When the focused Web component is removed, the system shifts focus to an adjacent sibling component following a back-to-front order. If no sibling components can gain focus, the focus is released and the parent component is notified to handle the focus.
Attribute change: If the focusable or enabled attribute of a focused component is set to false, or the visibility attribute is set to invisible, the system automatically moves focus to another focusable component, using the same method as component removal.
Invisible Web components: After the Web component gains focus, in scenarios such as app foreground and background switchover, page switchover, and navigation, the Web component loses focus and then gains focus again.
Web page loading: When the Web component loads a web page through src, loadUrl, and loadData, it gains focus by default. However, if the Web component is not focusable at this time, focus gain fails (common reasons for the non-focusable state include: the parent component is not focusable during a transition animation, or the app sets the Web component or its parent component to non-focusable). The app can call the focus request API requestFocus to try to make the Web component gain focus again. After focus is gained successfully, both the app-side onFocus event and the W3C focus event are reported.
autofocus: Elements with the autofocus style are focused by default after web pages are loaded. If the element supports text input, the cursor blinks in the text box, but the soft keyboard is not displayed. For details about how to automatically display the soft keyboard, see Automatically Displaying the Soft Keyboard.
Menu popup: ArkUI components of the overlay attribute type grab focus by default. In scenarios where the Web component is combined with such components (Menu, DatePicker, TimePicker, drop-down boxes, dialog boxes, and so on), the Web component loses focus.
Example
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
controller2: webview.WebviewController = new webview.WebviewController();
@State webBorderColor: Color = Color.Red;
@State webBorderColor2: Color = Color.Red;
build() {
Column() {
Row() {
Button('web1 requestFocus')
.onClick(() => {
try {
this.controller.requestFocus();
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
});
Button('web2 requestFocus')
.onClick(() => {
try {
this.controller2.requestFocus();
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
});
}
Web({ src: 'www.example.com', controller: this.controller })
.onFocus(() => {
this.webBorderColor = Color.Green;
})
.onBlur(() => {
this.webBorderColor = Color.Red;
})
// ···
.margin(3)
.borderWidth(10)
.borderColor(this.webBorderColor)
.height('45%')
Web({ src: 'www.example.com', controller: this.controller2 })
.onFocus(() => {
this.webBorderColor2 = Color.Green;
})
.onBlur(() => {
this.webBorderColor2 = Color.Red;
})
.margin(3)
.borderWidth(10)
.borderColor(this.webBorderColor2)
.height('45%')
}
}
} Figure 1 Component focus gain/loss events
Use requestFocus to request focus, and change the border color of the Web component by listening for the onFocus and onBlur events.

addEventListener("focus", (event) => {});
onfocus = (event) => {}; addEventListener("blur", (event) => {});
onblur = (event) => {}; <input name="q" autofocus />
In a document or dialog box, only one element can have the autofocus attribute. If you apply this attribute to multiple elements, the first element will be focused.
Example
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Web({ src: $rawfile('test.html'), controller: this.controller })
}
}
} HTML file to be loaded:
<!-- test.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<form id="form">
<input type="text" placeholder="text input" />
<input type="password" placeholder="password" />
</form>
</body>
<script>
const form = document.getElementById("form");
form.addEventListener(
"focus",
(event) => {
event.target.style.background = "pink";
},
true,
);
form.addEventListener(
"blur",
(event) => {
event.target.style.background = "";
},
true,
);
</script>
</html> Example 2 focus/blur events
Change the input background color by listening for the W3C focus and blur events.
