• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Setting the Dark Mode
2
3The 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.
4
5ArkWeb allows you to flexibly control the dark mode of web components. The dark mode can be set independently of the system. In addition, ArkWeb can enforce dark mode on different web pages to ensure compatibility with various system themes.
6
7## Adapting the Web Page to the Dark Mode
8You can adapt the web page to the dark mode using the **color-scheme** and **prefers-color-scheme** attributes.
9
10- **color-scheme** is a CSS attribute that indicates the color scheme supported by a web page. It can affect the form, scroll bar, and CSS system color. The CSS system color refers to the built-in color of the **Web** component, which is the default color used when the style of some elements is not defined.
11
12  When the dark color scheme is supported, the **Web** component applies dark mode to forms, scroll bars, and elements that use the CSS system color. If an element uses a custom color style, the custom color style is not affected by **color-scheme**.
13
14  If **color-scheme** is not set, the default value is **normal**, indicating that no color scheme is specified. The default color scheme of the **Web** component is used, which is the same as that of **light**. Example:
15
16  ```html
17  /* Method 1: Use the meta tag for global settings */
18  <meta name="color-scheme" content="light"> /* Only the light mode is supported */
19
20  /* Method 2: Use style for global settings */
21  :root {
22    color-scheme: light dark; /* Light and dark modes are supported. The mode is switched based on the system. */
23  }
24
25  /* Method 3: Use style to set a specific element */
26  div {
27    color-scheme: light; /* Only the light mode is supported */
28  }
29  ```
30
31  The following figure shows the rendering effect of the **color-scheme.html** page when dark mode is enabled and disabled. When dark mode is disabled, light color scheme is used for web pages, and a custom background style is used for **input2**. When the dark mode is enabled, the web page uses dark color scheme, **input2** retains the custom style, and the colors of the web page background, font, form, progress bar, and buttons are automatically switched to the dark color.
32
33  ```html
34  <!-- color-scheme.html -->
35  <!DOCTYPE html>
36  <html>
37  <head>
38      <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
39      <meta name="color-scheme" content="light dark">
40  </head>
41  <body>
42    <h1>Example page</h1>
43    <input name="input1" type="text" placeholder="please enter text">
44    <br><br>
45    <input name="input2" type="text" placeholder="please enter text" style="background-color: Lightgray;">
46    <br><br>
47    <progress value="50" max="100"></progress>
48    <br><br>
49    <input type="checkbox">
50    <input type="checkbox" checked>
51    <br><br>
52    <button>submit</button>
53  </body>
54  </html>
55  ```
56
57  **Figure 1** Effect of color-scheme page
58
59  ![web-color-scheme](figures/arkweb_color_scheme.png)
60
61- **prefers-color-scheme** is a CSS media query feature that can detect the system theme color. You can use this feature to define different CSS styles for different system theme colors to adapt to users' theme preferences. Example:
62
63  ```html
64  <style>
65    /* Default style */
66    body { background-color: White; }
67
68    /* Light style, which overwrites the default style when the dark style is disabled on the web page */
69    @media (prefers-color-scheme: light) {
70      body { background-color: Gray; }
71    }
72
73    /* Dark style, which overwrites the default style when the dark style is enabled on the web page */
74    @media (prefers-color-scheme: dark) {
75      body { background-color: Black; }
76    }
77  </style>
78  ```
79
80  You can use **color-scheme** to declare a web page color scheme and change the default style of web page elements. However, the application scope of **color-scheme** is limited. You can use **prefers-color-scheme** to define the dark style of web pages more flexibly. Moreover, **prefers-color-scheme** can be used together with **color-scheme**.
81
82  For example, when the following style definition is added to **color-scheme.html** and the web dark mode is enabled, the web page uses the dark color scheme and the style defined in **@media (prefers-color-scheme: dark)**. Figure 2 shows the rendering effect.
83
84  ```html
85  <style>
86    @media (prefers-color-scheme: dark) {
87      body { background-color: Gray; color: LightYellow; }
88      input { background-color: Lightgray; }
89    }
90  </style>
91  ```
92
93  **Figure 2** Effect of prefers-color-scheme
94
95  ![web-prefers-color-scheme](figures/arkweb_prefers_color_scheme.png)
96
97
98## Setting the Web Dark Mode
99
100You can use the [darkMode()](../reference/apis-arkweb/arkts-basic-components-web-attributes.md#darkmode9) API to configure the web dark mode, which is disabled by default. [WebDarkMode.Auto](../reference/apis-arkweb/arkts-basic-components-web-e.md#webdarkmode9) can be set for applications to follow the system dark mode. You can also set [WebDarkMode.On](../reference/apis-arkweb/arkts-basic-components-web-e.md#webdarkmode9) or [WebDarkMode.Off](../reference/apis-arkweb/arkts-basic-components-web-e.md#webdarkmode9) to enable or disable the dark mode.
101
102If you set [WebDarkMode.On](../reference/apis-arkweb/arkts-basic-components-web-e.md#webdarkmode9) or [WebDarkMode.Auto](../reference/apis-arkweb/arkts-basic-components-web-e.md#webdarkmode9) and enable the system dark mode, the web page enters the dark mode. When the dark mode is enabled, the web page uses the dark mode defined in **@media(prefers-color-scheme: dark)**. If the web page does not define the dark mode, the original mode is retained.
103
104To forcibly convert a web page that is not adapted to the dark mode to the dark style, you can use the [forceDarkAccess()](../reference/apis-arkweb/arkts-basic-components-web-attributes.md#forcedarkaccess9) 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 the dark mode. The forcible dark mode cannot ensure that all color conversions meet the expectation.
105
106In 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.
107
108> **NOTE**
109>
110> If the dark mode 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 mode, as shown in Table 1.
111
112**Table 1** Relationship between the dark mode, forcible dark mode, and color-scheme
113
114| Dark Mode| Forcible Dark Mode| color-scheme | Expected Result|
115| - | - | - | - |
116| Disabled| No impact| - | The web page uses the color scheme supported by **color-scheme**.|
117| Enabled| Disabled| - | The web page uses the color scheme supported by **color-scheme** and the style defined in **@media (prefers-color-scheme: dark)**.|
118| Enabled| Enabled| Dark mode is supported| The web page uses the dark color scheme and the style defined in **@media (prefers-color-scheme: dark)**.|
119| Enabled| Enabled| Dark color is not supported| The web page converts the color value of the hight-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.|
120
121The [forceDarkAccess()](../reference/apis-arkweb/arkts-basic-components-web-attributes.md#forcedarkaccess9) 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.
122
123```ts
124// xxx.ets
125import { webview } from '@kit.ArkWeb';
126
127@Entry
128@Component
129struct WebComponent {
130  controller: webview.WebviewController = new webview.WebviewController();
131  @State mode: WebDarkMode = WebDarkMode.Auto;
132  @State access: boolean = true;
133
134  build() {
135    Column() {
136      Web({ src: $rawfile('index.html'), controller: this.controller })
137        .darkMode(this.mode)
138        .forceDarkAccess(this.access)
139    }
140  }
141}
142```
143
144The code for the **index.html** page is as follows:
145
146```html
147<!-- index.html -->
148<!DOCTYPE html>
149<html>
150<head>
151    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
152    <style type="text/css">
153        body { background: LightBlue; color: Black; }
154        @media (prefers-color-scheme: dark) {
155            body { background: LightGray; color: Brown; }
156        }
157    </style>
158</head>
159<body class="contentCss">
160  <p>Dark mode debug page</p>
161  <input name="input1" placeholder="please enter text" style="color-scheme: light dark;">
162  <br><br>
163  <input name="input2" placeholder="please enter text">
164</body>
165</html>
166```
167
168Figure 3 shows the style of the **index.html** page when the dark mode is disabled, the dark mode is enabled, and the forcible dark mode is enabled. When the dark mode is disabled, the web page uses the default style. After the dark mode is enabled, the color scheme of **input1** is switched to dark, and the gray background and brown text style defined in **@media (prefers-color-scheme: dark)** are used. When the forcible dark mode is enabled, the color scheme of **input1** is dark and is not converted by the web page. The web page background color, text color, and background color of **input2** are converted to those shown in (3) based on the color values in (2).
169
170**Figure 3** Effects of the dark color mode and forcible dark color mode on the web page
171
172![web-dark-mode](figures/arkweb_dark_mode.png)
173
174## FAQs
175
176### What should I do if the web page is not switched to the dark mode?
177
178**Symptom**
179
180The web page is not switched to the dark mode.
181
182**Solution**
183
184There are multiple reasons for the switch failure. Perform the following steps to locate the fault:
185
1861. Check whether the dark mode is enabled on the web page. By default, [darkMode()](../reference/apis-arkweb/arkts-basic-components-web-attributes.md#darkmode9) is disabled. You need to explicitly declare [WebDarkMode.On](../reference/apis-arkweb/arkts-basic-components-web-e.md#webdarkmode9) or [WebDarkMode.Auto](../reference/apis-arkweb/arkts-basic-components-web-e.md#webdarkmode9) to enable the dark mode.
187
1882. If the 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 the dark mode is enabled on the web page. To enable forcible adaptation, use [forceDarkAccess()](../reference/apis-arkweb/arkts-basic-components-web-attributes.md#forcedarkaccess9) to enable the forcible dark mode.
189
1903. 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.
191
192### What should I do if the web page style is abnormal after the forcible dark mode is enabled?
193
194**Symptom**
195
196When 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.
197
198**Solution**
199
200In 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.
201
202### What should I do if the web page switches to a dark background when the dark mode is not enabled?
203
204**Symptom**
205
206The dark mode is not enabled for the **Web** component, but the web page background becomes dark.
207
208**Solution**
209
210If 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()](../reference/apis-arkui/arkui-ts/ts-universal-attributes-background.md#backgroundcolor) is set to a dark color for the **Web** component.
211