You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/how-to/how-to-configure-styling-options.md
+58-43Lines changed: 58 additions & 43 deletions
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,7 @@ An overview of the steps for styling content type elements is shown here, follow
38
38
39
39

40
40
41
-
## Step 1: Add fields for user input
41
+
## Step 1: Add form fields for user input
42
42
43
43
First, you need to add fields to your content type's form so that users have a way of selecting or entering styling options. In our Heading extension, we add three new fields:
44
44
@@ -118,19 +118,19 @@ The UI component form for these fields is shown here:
118
118
119
119
_New fields to extend the `Heading` form_
120
120
121
-
The names of these fields, `heading_color`, `heading_style`, and `heading_opacity` are particularly important. They are the same names you must assign to the attributes in your configuration file. We'll do that next.
121
+
The names of these fields, `heading_color`, `heading_style`, and `heading_opacity` are particularly important. They are the same names you must assign to their corresponding `<attribute>` and `<style>` nodes for the `<element>` in your configuration file. We'll do that next.
122
122
123
-
## Step 2: Add configuration options
123
+
## Step 2: Add element configuration options
124
124
125
-
Adding `<attribute>` and `<style>` nodes to an `element` configuration is how you add custom attributes and inline styles to the DOM, respectively. Both `<attributes>` and `<style>` nodes have a `name` property and a `source` property.
125
+
Adding `<attribute>` and `<style>` nodes to an `<element>` configuration is how you add custom attributes and inline styles to the DOM, respectively. Both `<attributes>` and `<style>` nodes have a `name` property and a `source` property.
126
126
127
127
The `name` properties of these nodes **must** match the corresponding field's name in the form. Using the same names (between config and field) is what allows Page Builder to map the field's input values to the template's output values.
128
128
129
-
The `source` property for `<attribute>` nodes defines the name of the custom attribute added to the element's DOM so you can use it for targeting the element with your attribute-based CSS classes.
129
+
The `source` property for `<attribute>` nodes defines the name of the custom attribute added to the element's DOM so you can use it for targeting the element with your attribute-based CSS classes (step 4).
130
130
131
131
The `source` property for `<style>` nodes specifies the CSS property (in `snake_case`) added to the element's `style` attribute in the DOM.
132
132
133
-
In our extended Heading configuration, we added two `<attribute>` nodes and one `<style>` node, with names corresponding to the previously defined `Heading` fields: `heading_color`, `heading_style`, and `heading_opacity`:
133
+
In our extended Heading configuration, we added two `<attribute>` nodes and one `<style>` node, with names corresponding to the previously defined `Heading`form fields: `heading_color`, `heading_style`, and `heading_opacity`:
134
134
135
135
```xml
136
136
<!-- heading.xml config extension -->
@@ -155,7 +155,7 @@ In our extended Heading configuration, we added two `<attribute>` nodes and one
155
155
156
156
_Configuration attributes for the `Heading` element_
157
157
158
-
In this example, the `source` values for the nodes (`data-heading-color`, `data-heading-style`, and `opacity`) are rendered in the DOM for the Heading's main element (`h2`), as shown here:
158
+
In this example, the `source` values for the nodes (`data-heading-color`, `data-heading-style`, and `opacity`) will be rendered in the DOM for the Heading's `main` element (which is `h2` by default), as shown here:
159
159
160
160
```html
161
161
<h2data-content-type="heading"
@@ -170,11 +170,58 @@ In this example, the `source` values for the nodes (`data-heading-color`, `data-
170
170
171
171
The values shown for these attributes and inline-style properties are set by the user from the form fields. In this example, the user selected `brand-green` from the `heading_color` field, `style-italic` from the `heading_style` field, and entered `100` (in percent) in the `heading_opacity` field (converted to the decimal form you see here using a custom converter you will find in the example).
172
172
173
-
Knowing how the attributes and their user-selected values are written to the DOM, we can now target our content type element using attribute-based CSS classes from our `_default.less` files. These CSS classes are described next.
173
+
But before these `attributes` and `styles` can be rendered to the DOM as shown, we need to add the necessary Knockout bindings to our HTML templates. We'll do that next.
174
174
175
-
## Step 3: Add CSS classes
175
+
## Step 3: Add template Knockout bindings
176
176
177
-
The CSS classes in our `_default.less` files for both the `adminhtml` and `frontend` are attribute-based, as shown here:
177
+
In our example module, we are using the Heading's native `master.html` and `preview.html` templates, which already have all the Knockout bindings needed to render our new `<attribute>` and `<style>` configurations to the DOM. But its critical that you understand what these bindings are and what they do. Because without them, nothing will be rendered to the screen.
178
+
179
+
In order for our configuration options to be rendered in the DOM (as described in step 2), we must add or ensure that Knockout bindings for our three configuration styling options are within our HTML templates. The three Knockout bindings for the `<attribute>`, `<style>`, and `<css>` configuration nodes are `attr`, `ko-style`, and `css`, respectively:
For all three Knockout bindings, `data.main` references the `main` element in the configuration file (`heading.xml`). The other binding references are as follows:
185
+
186
+
- For the `attr` binding, the `attributes` property references the collection of `<attribute>` nodes defined for the `main` element.
187
+
188
+
- For the `ko-style` binding, the `style` property references the collection of `<style>` nodes defined for the `main` element.
189
+
190
+
- For the `css` binding, the `css` property references the `<css>` node defined for the `main` element.
191
+
192
+
These Knockout bindings are applied to the Heading's `master.html` template (as well as the `preview.html` template), as shown here:
_Knockout bindings for the Heading's `data.main` config elements_
206
+
207
+
With these bindings in place, Page Builder will rendered them to the DOM, using the values from the form fields (as noted in step 2), to look something like this:
208
+
209
+
```html
210
+
<h2data-content-type="heading"
211
+
data-appearance="default"
212
+
data-heading-color="brand-green"
213
+
data-heading-style="style-italic"
214
+
data-element="main"
215
+
style="...; opacity: 1;">
216
+
My Heading Text
217
+
</h2>
218
+
```
219
+
220
+
Knowing how the attributes and their user-selected values are written to the DOM, we can now target our content type's `main` element (`<h2>` in this example) using attribute-based CSS classes from our `_default.less` files. We'll do this next.
221
+
222
+
## Step 4: Add CSS classes
223
+
224
+
To target our DOM output, we will use attribute-based CSS classes in our `_default.less` files for both the `adminhtml` and `frontend`, as shown here from our module's adminhtml area:
@@ -223,39 +270,7 @@ The CSS classes in our `_default.less` files for both the `adminhtml` and `front
223
270
224
271
_Attribute-based CSS classes_
225
272
226
-
The values for these attributes are set by the user from the form field that corresponds to the attribute. Using <attribute> configurations in this way makes it easy to target your content type's elements from your `_default.less` files.
227
-
228
-
## Step 4: Add Knockout bindings
229
-
230
-
In our example module, we are using the Heading's native `master.html` and `preview.html` templates, which already have all the Knockout bindings we need to render our new `<attribute>` and `<style>` configurations. Nonetheless, we will describe how these bindings map to our configuration options to render them to the DOM.
231
-
232
-
In order for our configuration options to be rendered in the DOM as described in step 2, we must add or ensure that Knockout bindings for our three configuration styling options are within our HTML templates. The three Knockout bindings for the `<attribute>`, `<style>`, and `<css>` configuration nodes are `attr`, `ko-style`, and `css`, respectively:
For all three Knockout bindings, `data.main` references the `main` element in the configuration file (`heading.xml`). The other binding references are as follows:
238
-
239
-
- For the `attr` binding, the `attributes` property references the collection of `<attribute>` nodes defined for the `main` element.
240
-
241
-
- For the `ko-style` binding, the `style` property references the collection of `<style>` nodes defined for the `main` element.
242
-
243
-
- For the `css` binding, the `css` property references the `<css>` node defined for the `main` element.
244
-
245
-
These Knockout bindings are applied to the Heading's `master.html` template (as well as the `preview.html` template), as shown here:
_Knockout bindings for the Heading's `data.main` config elements_
273
+
Since the values for these attributes are set by the user from the form field, we can add a variety of different CSS properties for each available value. This makes it easy to target and style your content type elements in both small and large ways, depending on your use cases.
0 commit comments