Skip to content

Commit cf2ed64

Browse files
committed
PB-480: Update content type styling concepts
Adding big picture section to the top
1 parent d5e6108 commit cf2ed64

File tree

2 files changed

+42
-32
lines changed

2 files changed

+42
-32
lines changed

docs/how-to/how-to-use-attributes-for-styling.md

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22

33
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.
44

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+
![Understanding the big picture](../images/styling-big-picture.svg)
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.
616

717
## Install the example module
818

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.
1020

1121
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:
1222

@@ -16,14 +26,12 @@ _Extended `Heading` form with color, text style, and opacity options_
1626

1727
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.
1828

19-
## Steps for styling with attributes
29+
## Steps overview
2030

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:
2232

2333
![How to style content types using attributes](../images/how-to-style-using-attributes.svg)
2434

25-
_Steps for styling content type elements with attributes_
26-
2735
## Step 1: Add fields for user input
2836

2937
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_
106114

107115
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.
108116

109-
## Step 2: Add configuration options for fields
117+
## Step 2: Add configuration options
110118

111119
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.
112120

@@ -158,7 +166,7 @@ The values shown for these attributes and inline-style properties are set by the
158166

159167
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.
160168

161-
## Step 3: Add attribute-based CSS classes
169+
## Step 3: Add CSS classes
162170

163171
The CSS classes in our `_default.less` files for both the `adminhtml` and `frontend` are attribute-based, as shown here:
164172

@@ -211,9 +219,11 @@ _Attribute-based CSS classes_
211219

212220
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.
213221

214-
## Step 4: Add Knockout bindings to templates
222+
## Step 4: Add Knockout bindings
215223

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:
217227

218228
```html
219229
<h2 attr="data.main.attributes" ko-style="data.main.style" css="data.main.css"...></h2>
@@ -239,7 +249,7 @@ These Knockout bindings are applied to the Heading's `master.html` template (as
239249
<h6 if="data.main.heading_type() == 'h6'" attr="data.main.attributes" ko-style="data.main.style" css="data.main.css" html="data.main.html"></h6>
240250
```
241251

242-
_Attribute bindings for the Heading's `data.main` config elements_
252+
_Knockout bindings for the Heading's `data.main` config elements_
243253

244254
## Discussion
245255

@@ -276,6 +286,10 @@ To aid in our discussion of these configuration nodes and how to use them, we wi
276286
{: .bs-callout .bs-callout-info }
277287
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`.
278288

289+
### The Big Picture
290+
291+
use an `<attribute>`
292+
279293
### Understanding and using `<style>` nodes
280294

281295
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
319333

320334
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.
321335

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.
325337

326338
### Understanding the `<css>` node {#understandthecssnode}
327339

@@ -343,12 +355,10 @@ Out of the box, Page Builder maps the `<css>` config node (for each content type
343355
</h2>
344356
```
345357

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.
347359

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.
349361

350362
## Final thoughts
351363

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-
![Styling big picture](../images/styling-big-picture.svg)
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!

docs/images/how-to-style-using-attributes.svg

Lines changed: 13 additions & 13 deletions
Loading

0 commit comments

Comments
 (0)