Setting the Dark Mode

The Web component allows you to set the dark mode for frontend pages.

  • Call darkMode() to configure an expected dark mode. WebDarkMode.Off indicates that the dark mode is disabled.
    WebDarkMode.On indicates that the dark mode is enabled and its setting follows the frontend page. WebDarkMode.Auto indicates that the dark mode is enabled and its setting follows the system. In the following example, the dark mode setting is configured to follow the system by using darkMode().

    // xxx.ets
    import web_webview from '@ohos.web.webview';
      
    @Entry
    @Component
    struct WebComponent {
      controller: web_webview.WebviewController = new web_webview.WebviewController();
      @State mode: WebDarkMode = WebDarkMode.Auto;
      build() {
        Column() {
          Web({ src: 'www.example.com', controller: this.controller })
            .darkMode(this.mode)
        }
      }
    }
    
  • Call forceDarkAccess() to forcibly set the dark mode for the frontend page and configure its setting not to follow the frontend page or system. In this mode, you need to specify WebDarkMode.On when calling darkMode(). In the following example, forceDarkAccess() is used to forcibly set the dark mode for the frontend page.

    // xxx.ets
    import web_webview from '@ohos.web.webview';
    
    @Entry		
    @Component
    struct WebComponent {
      controller: web_webview.WebviewController = new web_webview.WebviewController();
      @State mode: WebDarkMode = WebDarkMode.On;
      @State access: boolean = true;
      build() {
        Column() {
          Web({ src: 'www.example.com', controller: this.controller })
            .darkMode(this.mode)
            .forceDarkAccess(this.access)
        }
      }
    }