Module Attributes
Objective
The goal of this tutorial is to help you understand how module attributes are arranged in Divi 5.
This understanding will be put in practice in next tutorials.
Prerequisites
This tutorial assumes that you have read, followed, and completed:
Divi 4's Module Attributes
You can read in-depth explanation on Divi 4 attributes and extensive explanation on why we need to replace it here. The simplified explanation:
- Divi 4 uses shortcode and its attributes mechanism
- Therefore, it is being saved in one level attributes
Here's how it looks like:
[et_pb_text
// Responsive Options
width="40px"
width_tablet="30px"
width_phone="20px"
width_last_edited="on|desktop"
// Hover Options
width__hover_enabled="on"
width__hover="42px"
// Sticky Options
width__sticky_enabled="on|desktop"
width__sticky="44px"
]
As Divi has more features (for example: one attribute can have 5 different variants: desktop, tablet, phone, hover, and sticky) and become more complex, it is hard to scale up with this format.
Divi 5's Module Attributes
Divi 5's module attribute is saved as object. Technically you can arrange your attribute anyway you like, but all built-in modules uses the same convention that are about to be explained below. To harness simplicity and extendability of Divi 5, you will need to follow the same convention. Otherwise, you are swimming against the currents. This convention gives consistency across module; once you get the hang of it a glance against module attribute path gives clear understanding of what is being rendered and how it will be processed.
In a nutshell, module attributes are expected to use the following structure:
| Level | Terminology | Key | Value |
|---|---|---|---|
moduleAttributes | object | ||
| 1 | elementAttributes | string | object |
| 2 | elementPart | 'innerContent' / 'decoration' / 'advanced' / 'meta' | object |
| 3 | optionAttributes | see below | object |
| 4 | breakpointAttributes | 'desktop' / 'tablet' / 'phone' | object |
| 5 | stateAttributes | 'value' / 'hover' / 'sticky' | object |
| 6 | attributeValue | string / object |
Level 4 is the breakpoint layer of a leaf option. Nested option groups (see Nested option groups) add named keys before that layer, so desktop may be deeper than level 4.
First level: elementAttributes
With the exception of css property that handles multiple elements, the first level of attribute should represent an element that is being rendered on the module. If we are using Simple Quick Module that we made as a reference, technically it has four attributes (plus one css):
// moduleAttributes
const moduleAttributes = {
// First level: elementAttributes
module: {},
title: {},
content: {},
recentPosts: {},
// CSS is exception to the rule, we'll discuss it later.
css: {},
};
Second level: elementPart
Every elementAttributes (except for css) contains maximum four possible elementPart: innerContent, decoration, meta, and advanced. How every elementPart differs from each other:
| Name | Description |
|---|---|
innerContent | What is being rendered as content of the element goes here. IMPORTANT: unlike other elementPart, innerContent's direct value is breakpointAttributes instead of optionAttributes. In a nutshell, innerContent doesn't have optionAttributes. |
decoration | The value of this property will always being passed into <ElementStyle /> component that handles commonly used Module Options. Therefore, the format of this property have to follow the exact interface that <ElementStyle /> demands. |
meta | Attribute of an element that will not be rendered or used on the layout (eg. admin label) goes here. |
advanced | Any attribute that can't be categorized as innerContent, decoration, or meta is declared here. |
In form of mock code, elementPart looks like this:
const moduleAttributes = {
// First level: elementAttributes.
module: {
// Second level: elementPart.
innerContent: {},
decoration: {},
meta: {},
advanced: {},
},
title: {},
content: {},
recentPosts: {},
};
Third level: optionAttributes
optionAttributes can be defined as the option (like Background Options / Spacing Options / Scroll Effect Options) of an elementPart. Interestingly, optionAttributes of every elementPart differs:
innerContent has no optionAttributes
elementPart's innerContent doesn't have third level attribute / optionAttributes and directly have the fourth level attribute / breakpointAttributes inside of it. In another word, you can consider innerContent elementPart is its own optionAttributes. The other three elementParts (decoration, advanced, meta) have optionAttributes.
decoration's optionAttributes keys are fixed
The key of optionAttributes of decoration is fixed. It cannot be other the following:
{
animation: {},
background: {},
bodyFont: {},
border: {},
boxShadow: {},
button: {},
disabledOn: {},
filters: {},
interactions: {},
font: {},
icon: {},
headingFont: {},
overflow: {},
order: {},
position: {},
scroll: {},
sizing: {},
spacing: {},
sticky: {},
transform: {},
transition: {},
zIndex: {},
}
The reason for this is decoration elementPart's value are always processed by built-in Element* component and utilities. This simplify things: all decoration elementPart is being processed the same way; as long as you follow the same structure and have object with the same interface / object shape, it'll be processed exactly the same. Case in point:
const a = moduleAttributes?.title?.decoration?.background;
const b = moduleAttributes?.content?.decoration?.bodyFont;
Just by looking at this, you can immediately infer that a is optionAttributes of title's background and b is optionAttributes of content's bodyFont.
advanced's and meta's optionAttributes
Both advanced and meta's optionAttributes are named option keys. Each leaf key is breakpointAttributes. Nested option groups (for example a divi/font / divi/font-chart-js group under advanced) insert extra named keys first; desktop lives inside the nested group, not as a sibling of font. See Nested option groups. meta is for attributes that will not be rendered on the layout.
If we illustrate it as mock code, optionAttributes will look like this:
const moduleAttributes = {
// First level: elementAttributes.
module: {
// Second level: elementPart.
innerContent: {
// NOTE: `innerContent` doesn't have `optionAttributes`. Its value is always directly `breakpointAttributes`.
desktop: {
value: 'Hello',
},
},
decoration: {
// Third level: `optionAttributes`.
// NOTE: The interface / object shape of `decoration`'s' `optionAttributes` is fixed.
animation: {},
background: {},
bodyFont: {},
border: {},
boxShadow: {},
button: {},
disabledOn: {},
filters: {},
interactions: {},
font: {},
icon: {},
headingFont: {},
overflow: {},
order: {},
position: {},
scroll: {},
sizing: {},
spacing: {},
sticky: {},
transform: {},
transition: {},
zIndex: {},
},
meta: {
// Third level: `optionAttributes`.
adminLabel: {},
},
advanced: {
// Third level: `optionAttributes`.
text: {},
htmlAttributes: {},
loop: {},
},
},
title: {},
content: {},
recentPosts: {},
};
Fourth level: breakpointAttributes
breakpointAttributes is object where each property key represents breakpoint that Divi is supported which is desktop, tablet, and phone. If we illustrate it as mock code, it'll look like this:
const moduleAttributes = {
// First level: elementAttributes.
module: {},
title: {
// Second level: `elementPart`.
decoration: {
// Third level: `optionAttributes`.
spacing: {
// Fourth level: `breakpointAttributes`.
desktop: {},
tablet: {},
phone: {},
}
}
},
content: {},
recentPosts: {},
};
Fifth level: stateAttributes
stateAttributes is object where each property key represets state that Divi is supported which is value, hover, and sticky. If we illustrate it as mock code, it'll look like this:
const moduleAttributes = {
// First level: elementAttributes.
module: {},
title: {
// Second level: `elementPart`.
decoration: {
// Third level: `optionAttributes`.
spacing: {
// Fourth level: `breakpointAttributes`.
desktop: {
// Fifth level: `stateAttributes`.
value: {},
hover: {},
sticky: {},
},
tablet: {},
phone: {},
}
}
},
content: {},
recentPosts: {},
};
Sixth level: attributeValue
attributeValue represents the value of an attribute under the structure mentioned above. It is declared either as string or object (which is more convenient when an attribute has multiple values). If we illustrate it as mock code, it will look like this:
const moduleAttributes = {
// First level: elementAttributes.
module: {},
title: {
// Second level: `elementPart`.
decoration: {
// Third level: `optionAttributes`.
spacing: {
// Fourth level: `breakpointAttributes`.
desktop: {
// Fifth level: `stateAttributes`.
value:
// Sixth level: `attributeValue`.
{
top: '10px',
right: '10px',
bottom: '10px',
left: '10px',
},
},
tablet: {},
phone: {},
}
}
},
content: {},
recentPosts: {},
};
Nested option groups
The six-level table above describes a leaf option: desktop is the first key of that option (title.decoration.spacing.desktop.value). Nested option groups insert extra named keys, so desktop is not always at level 4.
A nested group (for example divi/font) is a container. Breakpoints live inside each child, not on the container:
// Leaf attr: desktop is the first key
chart.advanced.legend.layout = {
desktop: { value: { position: 'left', align: 'end' } },
};
// Nested font group: desktop lives under font
chart.advanced.legend.title = {
font: {
desktop: { value: { family: 'Roboto', size: '21px' } },
},
};
Sibling keys of desktop may only be other breakpoints (tablet, phone, …). font cannot sit next to desktop. This is illegal:
// ❌ font is a sibling of desktop
chart.advanced.legend.title = {
desktop: { value: { color: '#008000' } },
font: { desktop: { value: { family: 'Roboto' } } },
};
Subnames (position, family, color) live inside desktop.value. Nested option groups are named keys above the breakpoint layer. Do not model a font group as a subname of a breakpoint object. See specs/module-attribute/attribute-format.md.
Got a question or feedback? Ask away!