目录

Setting Dark Mode

The system provides the light and dark theme modes for users. Dark mode reduces the screen brightness and visual stimulus in low-light environments, improving reading experience. The Web component is rendered based on the web page style. If the web page is not adapted to dark mode, it will be separated from the system theme. To ensure a consistent user experience, you need to consider users' theme preferences and adapt the web page to dark mode.

ArkWeb allows you to flexibly set dark mode for Web components without affecting the system. In addition, ArkWeb can enforce dark mode on different web pages to ensure compatibility with various system themes.

Adapting Web Pages to Dark Mode

You can adapt web pages to dark mode using the color-scheme and prefers-color-scheme attributes.

Setting the Web Dark Mode

You can use the darkMode() API to configure the web dark mode, which is disabled by default. WebDarkMode.Auto can be set for applications to follow the system dark mode. You can also set WebDarkMode.On or WebDarkMode.Off to enable or disable dark mode.

If you set WebDarkMode.On or WebDarkMode.Auto and enable the system dark mode, the web page enters dark mode. When dark mode is enabled, the web page uses the dark style defined in @media(prefers-color-scheme: dark). If the web page does not define a dark style, the original mode is retained.

To forcibly convert a web page that is not adapted to dark mode to the dark style, you can use the forceDarkAccess() API. The forcible dark mode can overwrite the default style of the web page and convert the background and text colors of the web page to dark mode. The forcible dark mode cannot ensure that all color conversions meet the expectation.

In forcible dark mode, the high-brightness color value is converted to a color value suitable for a low-light environment, and the low-brightness color value remains unchanged. The specific color value conversion algorithm adheres to the Chromium kernel standard and is updated in tandem with the Chromium kernel. Color conversion applies only to elements that do not support dark color schemes. If the global declaration of a web page supports the dark color scheme, the color values of the entire web page will not be converted.

If the dark style of an element is defined in @media(prefers-color-scheme: dark) but the dark color scheme is not declared by using color-scheme, the web page converts the color value based on the dark style, as shown in Table 1.

Table 1 Relationship between dark mode, forcible dark mode, and color-scheme

Dark Mode Forcible Dark Mode color-scheme Expected Result
Disabled No impact - The web page uses the color scheme supported by color-scheme.
Enabled Disabled - The web page uses the color scheme supported by color-scheme and the style defined in @media (prefers-color-scheme: dark).
Enabled Enabled Dark mode is supported The web page uses the dark color scheme and the style defined in @media (prefers-color-scheme: dark).
Enabled Enabled Dark mode is not supported The web page converts the color value of the high-brightness elements based on the algorithm. If a style is defined in @media (prefers-color-scheme: dark), the color value is converted based on the defined style.

The forceDarkAccess() API takes effect only when the web dark mode is enabled. In the following example, the application sets the web dark mode to follow the system. When the system dark mode is enabled, the web page enters the forcible dark mode.

import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();
  @State mode: WebDarkMode = WebDarkMode.Auto;
  @State access: boolean = true;

  build() {
    Column() {
      Web({ src: $rawfile('darkModePage.html'), controller: this.controller })
        .darkMode(this.mode)
        .forceDarkAccess(this.access)
    }
  }
}

The code of the resources/rawfile/darkModePage.html page is as follows:

<!-- darkModePage.html -->
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <style type="text/css">
        body { background: LightBlue; color: Black; }
        @media (prefers-color-scheme: dark) {
            body { background: LightGray; color: Brown; }
        }
    </style>
</head>
<body class="contentCss">
  <p>Dark mode debug page</p>
  <input name="input1" placeholder="please enter text" style="color-scheme: light dark;">
  <br><br>
  <input name="input2" placeholder="please enter text">
</body>
</html>

The styles of the darkModePage.html page when dark mode is disabled, when dark mode is enabled, and when forced dark mode is enabled are shown in Figure 3. When dark mode is disabled, the web page adopts the default style. When dark mode is enabled, the color scheme of input1 switches to dark, and the web page applies the gray background and brown text styles defined in @media(prefers-color-scheme: dark). When forced dark mode is enabled, the color scheme of input1 is dark and is not converted by Web, while the web page background color, text color, and input2 background color are all converted from the color values in (2) to those shown in (3).

Figure 3 Effects of the dark color mode and forcible dark color mode on the web page

Adapting the Web Component Background Color to Dark Mode

When the Web component is rotated or its size changes, the web page size changes and the background color of the Web component may be displayed. In dark mode, you are advised to set the background color of the Web component to black to ensure a consistent experience with the web page background.

You can use backgroundColor() to set the background color of Web components. If no background color is set, the default background color white is used. In forcible dark mode, the default background color is black. If forcible dark mode is not enabled, you can use the following methods for adaptation.

FAQs

[h2]What should I do if the web page is not switched to dark mode?

Symptom

The web page is not switched to dark mode.

Solution

There are multiple reasons for the switch failure. Perform the following steps to locate the fault:

  1. Check whether dark mode is enabled on the web page. By default, darkMode() is disabled. You need to explicitly declare WebDarkMode.On or WebDarkMode.Auto to enable dark mode.

  2. If dark mode is enabled on the web page, check whether the dark style is defined for the web page. If the dark style is not defined, the web page style remains unchanged even if dark mode is enabled on the web page. To enable forcible adaptation, use forceDarkAccess() to enable the forcible dark mode.

  3. If the forcible dark mode is enabled on the web page, check whether the web page supports the dark color scheme. If a web page supports the dark color scheme by using color-scheme, the color value will not be converted by the web page in forcible dark mode. In addition, color-scheme does not affect the custom color style of elements on the web page. Therefore, the web page is not switched to the dark style. In this case, you need to modify the dark mode configuration.

[h2]What should I do if the web page style is abnormal after the forcible dark mode is enabled?

Symptom

When the forcible dark mode is enabled, the web page style conversion is abnormal. For example, the font is unclear, the style is not user-friendly, or the color is improper.

Solution

In forcible dark mode, the web page uses the Chromium color value conversion algorithm to automatically adjust the element color style. The layout and style of different web pages are different. The algorithm cannot ensure that all conversions meet the expectation. You are advised to customize the dark style.

[h2]What should I do if the web page switches to a dark background when dark mode is not enabled?

Symptom

The dark mode is not enabled for the Web component, but the web page background becomes dark.

Solution

If no background color is set or the background color is set to transparent on the web page, the background color of the Web component is displayed. In this case, check whether backgroundColor() is set to a dark color for the Web component.