This is the DivKit project repository. DivKit is a backend-driven UI framework. Its main goal is to create a platform view (iOS, Android, Web, Flutter) based on markup in JSON format.
A correct DivKit JSON should have the following structure:
{
"templates": {
// Div element templates (optional)
},
"card": {
// div-data (required)
}
}- Templates are defined in the
templatessection and used indivby specifyingtypeequal to the template name. - Template properties are marked with a
$prefix in the property name (not in the value).
{
"templates": {
"title": {
"type": "text",
"font_size": 18,
"$text": "title_text"
}
},
"card": {
"log_id": "sample_card",
"states": [
{
"state_id": 0,
"div": {
"type": "container",
"items": [
{
"type": "title",
"title_text": "Hello, DivKit!"
},
{
"type": "title",
"title_text": "This is a DivKit card example"
}
]
}
}
]
}
}It's crucial to always check the schema files to ensure correct property types and values of every element. The schema files are located in the schema/ directory. If the usage of an element is not completely clear, you should check its description in the translations.json file.
IMPORTANT: Always verify that your DivKit JSON structure matches the schema requirements at all levels of object nesting. This includes checking field types (string, number, object, array, etc.) and ensuring all required fields are present at every level of the JSON hierarchy. Each nested object must conform to its corresponding schema definition. If properties defined by objects are used in the layout, you need to check the schema of each such object. Failure to follow the schema at any nesting level will result in rendering errors or unexpected behavior across platforms. Schema validation should be your first step when troubleshooting issues.
The schema files define:
- Required properties for each element
- Correct data types for properties
- Allowed values for enum properties
- Default values for optional properties
If you encounter errors when creating a DivKit layout, always verify your element properties against the schema. Common issues include:
- Using incorrect data types (e.g., using a number instead of an object)
- Missing required properties
- Using invalid enum values
- Incorrect action formats
- Nested objects that don't conform to their respective schema definitions
Remember that some properties require complex object structures rather than simple values. Always check the schema when in doubt, and ensure that all nested objects within your JSON structure conform to their respective schema definitions.
- Always check the div-data schema to understand the field types and required fields.
- Check the div.json file to know which elements exist.
- Always verify the schema of needed elements and all objects used in them to use correct types and required properties.
- Always specify required fields for each element type.
- Use correct data types for properties (strings, numbers, objects).
- Monitor element nesting and their sizes.
- Use templates for repeating elements.
- Use variables for values that may change.
- Verify JSON correctness before use.
- Consider the specifics of platforms where the UI will be displayed.
- Ensure that all nested objects conform to their respective schema definitions at every level of the JSON hierarchy.
IMPORTANT:
- Don't wrap single elements in containers.
- Ensure that containers with
wrap_contentdon't contain elements withmatch_parentalong the main axis. - Use unique identifiers for elements and actions.
DivKit supports the following elements:
- div-image — Image.
- div-gif-image — Gif image.
- div-text — Text.
- div-separator — Separator.
- div-container — Container.
- div-grid — Grid.
- div-gallery — Gallery.
- div-pager — Pager.
- div-tabs — Tabs.
- div-state — State.
- div-custom — Host-specific element.
- div-indicator — Progress indicator for pager.
- div-slider — Slider.
- div-switch — Toggle.
- div-input — Text input field.
- div-select — List of options with only one to be selected.
- div-video — Video.
Image. Allows displaying static images with various scaling and alignment settings.
Animated GIF image. Specialized element for displaying GIF animations.
Text. Allows displaying text content with various styles, supports formatting, embedding images, and character ranges with different styles.
A separating line between elements. Used for visual content separation.
Container. It contains other elements and is responsible for their location. It is used to arrange elements vertically, horizontally, and with an overlay in a certain order, simulating the third dimension.
A grid with an option to merge cells vertically and horizontally. Allows creating tabular data structures.
Gallery. It contains a horizontal or vertical set of cards that can be scrolled. Supports various scrolling modes and element alignments.
Pager. It contains a horizontal set of cards that can be scrolled page by page. It shows the main page and the beginning of the next one.
Tabs. Allow organizing content in the form of tabs. Height of the first tab is determined by its contents, and height of the remaining depends on the platform.
State. It contains sets of states for visual elements and switches between them. Allows creating interactive elements with different visual states.
Custom element. It is delegated to a host application to create native elements depending on the platform.
Progress indicator for pager. Displays the current position in the pager and allows visualizing viewing progress.
Slider for selecting a value in the range. Allows users to select numeric values in a given range.
Two-state toggle that allows the user to control a Boolean variable. The element's look-and-feel varies by platform.
Text input element. Allows users to enter text information with various validation and formatting settings.
List of options with only one to be selected. Provides the user with the ability to choose from a predefined list of options.
Video. Allows playing video content with various playback control settings.
To make the layout interactive, DivKit uses actions. To add an action for an element, you need to use the actions property. The action property is deprecated and should not be used. An action can be described in one of two ways:
- Using a typed action:
"actions": [
{
"log_id": "typed_action",
"typed": {
"$ref": "div-action-typed.json"
}
}
]The list of action types, as well as their property types and required fields, should be checked in the schema before use.
- Using a URL:
"actions": [
{
"log_id": "some_action",
"url": "div-action://some_action?[param1=value1][¶m2=value2]"
}
]some_action corresponds to the name of typed actions, and the parameters correspond to the properties of the respective typed action.
When using the set_state action, the state_id parameter contains the full path from the root to the state element:
- For root states, it's just a number (e.g.,
state_id=0orstate_id=1) - For nested states, the path includes:
- The root state_id (numeric)
- The id of the state element
- The state_id of the target state (string)
Example for nested states:
div-action://set_state?state_id=0/nested_state/selected
For deeply nested states, the path would include all pairs of id/state_id along the way.