# Config the gadget dark mode

In the [global configuration item](https://open.feishu.cn/document/uYjL24iN/uEDNuEDNuEDN#c011c4d1) of the gadget, the dark mode (DarkMode) of the gadget can be configured through the darkmode attribute. This article introduces the configuration process of dark mode, as well as configuration instructions related to dark mode.

## Version requirements

- Since version v5.3.0, Feishu supports the dark mode of gadgets.
- Feishu Developer Tools supports the dark mode of gadgets since version v2.5.3.

## Step 1: Turn on the dark mode of the gadget

Open the app.json file in the root directory of the gadget project, and configure the `darkmode` attribute in the file to `true`, which means that the current gadget has turned on the dark mode.

After it is enabled, the basic components of the gadget will display different default styles according to the system theme, but it should be noted that some custom components may have abnormal default styles (for example, the background color and font are both black in dark mode). Therefore, you need to proceed to the next step to configure the relevant variables of the dark mode.

## Step 2: Configure dark mode related variables

After you configure the `darkmode` attribute to `true` in the app.json file in the project root directory, you can specify a theme.json file, through which you can define UI colors and icons under different themes in the form of variables.
**Default Style Description**:

- If you configure the `darkmode` attribute to `true` in the app.json file, but do not define the UI style in dark mode through the app.json and theme.json files, the UI style will keep the original light color by default The style in the mode.

- If you call the API of the gadget (for example, [setNavigationBarColor](https://open.feishu.cn/document/uYjL24iN/ucjMyUjL3IjM14yNyITN), [setTabBarStyle](https://open.feishu.cn/document/uYjL24iN/uITN04iM1QjLyUDN) etc.) It will respond to the styles in the original DarkMode configuration (that is, the styles defined by the app.json and theme.json files). Therefore, you need to configure the business code reasonably according to the actual development situation, so as to avoid the failure of the original DarkMode configuration due to calling the gadget API. The difference between defining a theme style through a configuration file and modifying a theme style through an API is as follows:

- If you define the UI style through the app.json and theme.json files, the gadget framework will automatically adjust the UI style under different themes according to the configuration of the theme.json file.

- The gadget API will not automatically adapt when switching the theme. If you need to modify the style of different themes through the gadget API, you need to [listen to the theme switching event](https://open.feishu.cn/document/uYjL24iN/uQTM5UjL0ETO14CNxkTN/darkmode#1c4089e9) , after receiving the specified theme switching event, call the API to modify the style to adapt to the current theme.

1. Configure the `themeLocation` attribute in the app.json file, and the attribute value is the path of the variable configuration file theme.json relative to the project root directory.

For example, if you add a variable configuration file theme.json in the project root directory, the property to be configured is `"themeLocation": "theme.json"`.

2. Define variables in the theme.json file.

The variables that support definition are as follows, and the description information of each variable can be found in [Configuration gadget](https://open.feishu.cn/document/uYjL24iN/uEDNuEDNuEDN).

Global configuration window properties and properties under page configuration | Globally configure properties under window.tabBar
---|---
- navigationBarBackgroundColor<br>- navigationBarTextStyle<br>- backgroundColor<br>- backgroundTextStyle<br>- backgroundColorTop<br>- backgroundColorBottom | - color<br>- selectedColor<br>- backgroundColor<br>- borderStyle<br>- list<br>- iconPath<br>- selectedIconPath

The theme.json file defines variables through the properties shown in the table below.

Attribute | Type | Required | Description
---|---|---|---
light | object | is | Variable definition in light mode.
dark | object | is | Variable definition in dark mode.

The `light` and `dark` attributes define the variable name and variable value in the format of `key: value`, and the configuration example is shown below.

```json
{
   "light": {
     "navBgColor": "#f6f6f6",
     "navTxtStyle": "black"
   },
   "dark": {
     "navBgColor": "#191919",
     "navTxtStyle": "white"
   }
}
```

3. After completing the variable definition in the theme.json file, return to the app.json file in the root directory, and refer to the variable at the beginning of `@` in the relevant properties of the corresponding global configuration or page configuration.

An example configuration that references variables in the app.json file is shown below. Since the variable values of light mode (light) and dark mode (dark) are respectively defined in the theme.json file, after the configuration here is completed, the gadget framework will automatically switch the corresponding gadget theme according to the system theme color.

```json
     // global configuration
     {
       "window": {
         "navigationBarBackgroundColor": "@navBgColor",
         "navigationBarTextStyle": "@navTxtStyle"
       }
     }

// page configuration

{
       "navigationBarBackgroundColor": "@navBgColor",
       "navigationBarTextStyle": "@navTxtStyle"
     }
     ```

### app.json file configuration example
In the following configuration examples, only theme color configuration items are provided, and other configuration items are omitted.

```json
{
     "window": {
         "navigationBarBackgroundColor": "@navBgColor",
         "navigationBarTextStyle": "@navTxtStyle",
         "backgroundColor": "@bgColor",
         "backgroundTextStyle": "@bgTxtStyle",
         "backgroundColorTop": "@bgColorTop",
         "backgroundColorBottom": "@bgColorBottom"
     },
     "tabBar": {
         "color": "@tabFontColor",
         "selectedColor": "@tabSelectedColor",
         "backgroundColor": "@tabBgColor",
         "borderStyle": "@tabBorderStyle",
         "list": [{
             "iconPath": "@iconPath1",
             "selectedIconPath": "@selectedIconPath1"
         }, {
             "iconPath": "@iconPath2",
             "selectedIconPath": "@selectedIconPath2"
         }]
     }
}
```

### theme.json file configuration example

```json
{
     "light": {
         "navBgColor": "#f6f6f6",
         "navTxtStyle": "black",
         "bgColor": "#ffffff",
         "bgTxtStyle": "light",
         "bgColorTop": "#eeeeee",
         "bgColorBottom": "#efefef",
         "tabFontColor": "#000000",
         "tabSelectedColor": "#3cc51f",
         "tabBgColor": "#ffffff",
         "tabBorderStyle": "black",
         "iconPath1": "image/icon1_light.png",
         "selectedIconPath1": "image/selected_icon1_light.png",
         "iconPath2": "image/icon2_light.png",
         "selectedIconPath2": "image/selected_icon2_light.png"
     },
     "dark": {
         "navBgColor": "#191919",
         "navTxtStyle": "white",
         "bgColor": "#1f1f1f",
         "bgTxtStyle": "dark",
         "bgColorTop": "#191919",
         "bgColorBottom": "#1f1f1f",
         "tabFontColor": "#ffffff",
         "tabSelectedColor": "#51a937",
         "tabBgColor": "#191919",
         "tabBorderStyle": "white",
         "iconPath1": "image/icon1_dark.png",
         "selectedIconPath1": "image/selected_icon1_dark.png",
         "iconPath2": "image/icon2_dark.png",
         "selectedIconPath2": "image/selected_icon2_dark.png"
     }
}
```

## Dark mode related configuration instructions

### Get the current system theme

- [tt.getSystemInfo](https://open.feishu.cn/document/uYjL24iN/uQjNx4CN2EjL0YTM) or [tt.getSystemInfoSync](https://open.feishu.cn/document/uYjL24iN/uUjNx4SN2EjL1YTM) interface will contain `theme` attribute in the returned result, the attribute value is `light` or `dark`.

- If `"darkmode": true` is not declared in the app.json file of the project root directory, the tt.getSystemInfo or tt.getSystemInfoSync interface cannot get the `theme` attribute, and the attribute value is `undefined` at this time.

### Listen to theme switching events

The following monitoring methods are supported:

- Method 1: Pass in the `onThemeChange` callback method in the `App()` function of the project code, and this callback method will be triggered when the gadget switches themes.

- Method 2: Monitor theme switching through the [tt.onThemeChange](https://open.feishu.cn/document/uYjL24iN/uUTOuUTOuUTO/darkmode/onthemechange) interface. Subsequent monitoring can be canceled through the [tt.offThemeChange](https://open.feishu.cn/document/uYjL24iN/uUTOuUTOuUTO/darkmode/offthemechange) interface.

### Adapt to TTSS

In TTSS, it is supported to adapt different themes through media query [prefers-color-scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme), which The method is consistent with the adaptation method in the Web. For example:

```css
/* Normal style start */
.some-background {
     background: white;
}
.some-text {
     color: black;
}
/* Normal style end */
@media (prefers-color-scheme: dark) {
     /* Style start in DarkMode */
     .some-background {
         background: #1b1b1b;
     }
     .some-text {
         color: #ffffff;
     }
     /* DarkMode style end */
}
```

### The dark mode debugging function of Feishu Developer Tools

During the development of gadgets in [Feishu Developer Tools](https://open.feishu.cn/document/uYjL24iN/ucDOzYjL3gzM24yN4MjN), you can enable the dark mode debugging switch to simulate the style and effect of gadgets in this mode.

![](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/47ebe35cb3671815e73f752e3df32acc_HDmaLPQ4mB.png?height=706&lazyload=true&maxWidth=600&width=1764)