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-use-attributes-for-styling.md
+29-19Lines changed: 29 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,21 @@
2
2
3
3
Out of the box, Page Builder provides a variety of interactive CSS styling options for your content types. For example, Page Builder's `Heading` content type provides options for setting heading types (`h1` through `h6`), text alignments, border properties, margins, and paddings. It also lets users apply one or more static CSS classes by entering them into an text input field.
4
4
5
-
But what if you want to give users even more styling options? For example, maybe you want to add Heading color options. Or Heading text-style options. This topic will show you how to use the `<attribute>`, `<style>`, and `<css>` configuration nodes to style the elements in your content types.
5
+
But what if you want to give users even more styling options? For example, maybe you want to add Heading color options. Or Heading text-style options. This topic will show you how to use the `<attribute>`, `<style>`, and `<css>` configuration options to style the elements in your content types.
6
+
7
+
## Understanding the big picture
8
+
9
+
Before we dive into the particulars of using different configuration options, let's take a moment to understand the role of the configurations within a Page Builder content type.
10
+
11
+
The following diagram shows how the configuration options for the `Heading` content type connect or map the inputs from the Heading form to the bindings in the Heading templates and finally to the rendered output in the DOM:
12
+
13
+

14
+
15
+
You may not understand all the details right away, but we hope it will help you put the pieces together as you work your way through this topic and play with the accompanying example module.
6
16
7
17
## Install the example module
8
18
9
-
To help you get started and follow along, we created an example module you can clone and install from [magento-devdocs/PageBuilderStylingWithAttributes](https://github.com/magento-devdocs/PageBuilderStylingWithAttributes). The instructions for installing the module are provided from the `README` file on the repo.
19
+
To help you get started and follow along, we created an example module you can clone and install from [magento-devdocs/PageBuilderConfigurationsForStyling](https://github.com/magento-devdocs/PageBuilderConfigurationsForStyling). The instructions for installing the module are provided from the `README` file on the repo.
10
20
11
21
For this example module, we chose to extend the `Heading` content type by adding three new Heading fields: `Heading Colors`, `Heading Styles`, and `Heading Opacity`, as shown here:
12
22
@@ -16,14 +26,12 @@ _Extended `Heading` form with color, text style, and opacity options_
16
26
17
27
The example module provides all the code used here to describe how to style elements using `<attribute>` and `<style>` nodes. It also shows the use of a custom converter for our `opacity` style to ensure that users can enter the opacity as a percentage.
18
28
19
-
## Steps for styling with attributes
29
+
## Steps overview
20
30
21
-
An overview of the steps is shown here, followed by the detailed instructions for each step:
31
+
An overview of the steps for styling content type elements is shown here, followed by the detailed instructions for each step:
22
32
23
33

24
34
25
-
_Steps for styling content type elements with attributes_
26
-
27
35
## Step 1: Add fields for user input
28
36
29
37
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:
@@ -106,7 +114,7 @@ _New fields to extend the `Heading` form_
106
114
107
115
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.
108
116
109
-
## Step 2: Add configuration options for fields
117
+
## Step 2: Add configuration options
110
118
111
119
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.
112
120
@@ -158,7 +166,7 @@ The values shown for these attributes and inline-style properties are set by the
158
166
159
167
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.
160
168
161
-
## Step 3: Add attribute-based CSS classes
169
+
## Step 3: Add CSS classes
162
170
163
171
The CSS classes in our `_default.less` files for both the `adminhtml` and `frontend` are attribute-based, as shown here:
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.
213
221
214
-
## Step 4: Add Knockout bindings to templates
222
+
## Step 4: Add Knockout bindings
215
223
216
-
In order for our attribute selectors to be rendered in the DOM as described in step 2, we must add Knockout bindings within our HTML templates. The bindings for `<attribute>`, `<style>`, and `<css>` nodes look like this:
224
+
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.
225
+
226
+
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:
_Attribute bindings for the Heading's `data.main` config elements_
252
+
_Knockout bindings for the Heading's `data.main` config elements_
243
253
244
254
## Discussion
245
255
@@ -276,6 +286,10 @@ To aid in our discussion of these configuration nodes and how to use them, we wi
276
286
{: .bs-callout .bs-callout-info }
277
287
The `<tag>` and `<html>` nodes are beyond the scope of this topic, but like the other nodes, they also map to fields in a form. In this case, the `<tag>` node maps to the Heading's `heading_type` field and the `<html>` node maps to the `heading_text` field from the `pagebuilder_heading_form.xml`.
278
288
289
+
### The Big Picture
290
+
291
+
use an `<attribute>`
292
+
279
293
### Understanding and using `<style>` nodes
280
294
281
295
By configuring `<elements>` with `<style>` nodes, you can provide end users with a form field that changes a _specific_ CSS property, such as we did with `opacity`.
@@ -319,9 +333,7 @@ Each `<style>` node defined for the element's configuration gets added to the DO
319
333
320
334
As mentioned, adding an `<attribute>` node to a config `element` gives you a way to provide end users with a form field that can use _CSS classes_ so you can change _several_ CSS properties at once. This is much more powerful that using `<style>` nodes which can only change single CSS properties.
321
335
322
-
However, `<style>` nodes have the advantage of being able to use converters, which you cannot use with `<attributes>`.
323
-
324
-
Using `<attribute>` nodes instead of `<style>` nodes is transparent to users, who are simply interacting with field options to change content styling. But for you, the developer, being able to apply a variety of different CSS classes (based on end user field selections) can provide your content types with powerful styling options.
336
+
Using `<attribute>` or `<style>` configurations is transparent to users, who are simply interacting with field options to change content styling. But for you, the developer, you have a choice that depends on whether you need to use CSS classes to style an element (use an `<attribute>`) or a specific CSS property (use an `<style>`). Either option can provide users with powerful styling options.
325
337
326
338
### Understanding the `<css>` node {#understandthecssnode}
327
339
@@ -343,12 +355,10 @@ Out of the box, Page Builder maps the `<css>` config node (for each content type
343
355
</h2>
344
356
```
345
357
346
-
As mentioned, you can override the `<css>` node to map it to a different form field name and field type. You just can't map it more than once per element.
358
+
As mentioned, you can override the `<css>` node to map it to a different field name and/or type. You just can't use it more than once per element.
347
359
348
-
Regardless of that limitation, you can still use the `<css>` node to provide creative styling options for a content type. For example, if a merchant has a set of standard CSS classes they use during the year for holidays, you could map the `<css>` node to a `select` field that allows end users to choose from sets of merchant-approved classes with descriptive names, like Halloween styling, Christmas styling, and so on.
360
+
Regardless of the limitation, you can still use the `<css>` node to provide creative styling options for a content type. For example, if a merchant has a set of standard CSS classes they use during the year for holidays, you could map the `<css>` node to a `select` field that allows end users to choose from sets of merchant-approved classes with descriptive names, like Halloween styling, Christmas styling, and so on.
349
361
350
362
## Final thoughts
351
363
352
-
Using custom attributes represents one of Page Builder's best practices for adding powerful and flexible content styling options to forms. You can add these styling attributes to both existing content types (as shown in the [example module](https://github.com/magento-devdocs/PageBuilderStylingWithAttributes)) and custom content types. The CSS styling options are only limited by the CSS specs targeted by your supported browsers. So get creative and have fun!
353
-
354
-

364
+
Using custom attributes represents one of Page Builder's best practices for adding powerful and flexible content styling options to forms. You can add these styling attributes to both existing content types (as shown in the [example module](https://github.com/magento-devdocs/PageBuilderConfigurationsForStyling)) and custom content types. The CSS styling options are only limited by the CSS specs targeted by your supported browsers. So get creative and have fun!
0 commit comments