Layer style conditions determine when layer styles are applied. They allow layouts to adapt to factors such as the device type or system settings, such as dark mode.
Quick guide
A layer style condition is defined in a JSON file in the ./layer_style_condition directory.
./layer_style_condition/
└── darkMode.json
For example, darkMode.json defines a condition that checks whether dark mode is enabled:
{
"name": "darkMode",
"content": {
"mode": "equals",
"left": {
"context": "device",
"field": "darkModeEnabled"
},
"right": "true"
}
}
To apply the condition, include its name in parentheses in the layer style name, for example, text(darkMode).
Layer style condition files
./layer_style_condition directory
Layer style condition files are stored in the ./layer_style_condition directory.
./layer_style_condition/
├── darkMode.json
├── isAndroid.json
└── isiOS.json
Layer style condition file structure
Each file must contain a name and a content object. The name must match the file name, and content must define the condition.
{
"name": "darkMode",
"content": {
"mode": "equals",
"left": {
"context": "device",
"field": "darkModeEnabled"
},
"right": "true"
}
}
This example checks whether the dark mode is enabled.
Using layer style conditions
Layer style conditions are used together with layer styles. To apply a condition to a layer style, include the condition name in parentheses in the layer style's name.
For example, a condition named darkMode can be applied by naming the layer style text(darkMode).
If the condition is not met and no fallback layer style is defined, no layer style will be applied.
Conditions for dark and light color themes
The following layer style condition checks whether dark mode is enabled on the device.
{
"name": "darkMode",
"content": {
"mode": "equals",
"left": {
"context": "device",
"field": "darkModeEnabled"
},
"right": "true"
}
}
The accentColor(darkMode).json layer style defines the accent color used when dark mode is enabled.
{
"name": ".accentColor(darkMode)",
"content": {
"value": "#bf4797"
}
}
The accentColor.json layer style defines the accent color used when the condition is not met.
{
"name": ".accentColor",
"content": {
"value": "#2f96b4"
}
}
The following layout applies the .accentColor layer style to a color layer in dark and light mode respectively.
{
"type": "color",
"classes": [
"accentColor"
]
}
Conditions based on the device operating system
The following layer style condition identifies an Android device:
{
"name": "isAndroid",
"content": {
"mode": "equals",
"left": {
"context": "device",
"field": "os"
},
"right": {
"value": "android"
}
}
}
The following layer style condition identifies an iOS device:
{
"name": "isiOS",
"content": {
"mode": "equals",
"left": {
"context": "device",
"field": "os"
},
"right": {
"value": "iOS"
}
}
}
Troubleshooting
If a layer style condition is not applied as expected, check the following:
- Is the file stored in the
./layer_style_condition directory?
- Does the
name in the JSON file match the file name?
- Does the layer reference the intended layer style?
- Is the condition specified in the layer style condition met?
- If the condition is not met, is a fallback layer style defined?