Skip to content

Commit 5a107ce

Browse files
committed
PB-480: Update content type styling concepts
1 parent 1605bd0 commit 5a107ce

File tree

2 files changed

+75
-31
lines changed

2 files changed

+75
-31
lines changed

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

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# How to use attributes to apply CSS styling
1+
# How to use attributes for content styling
22

33
Attributes in Page Builder give you a way to add a variety of interactive CSS styling options for your content types. For example, Page Builder's Heading content type provides a number of built-in styling options from its form. The Heading form gives users options to set different heading types, text alignments, border properties, margins, and paddings. It also lets users apply multiple static CSS classes.
44

5-
But what if you want to give users more interactive styling options? For example, maybe you want to add Heading color options. Or Heading text style options. Using attributes is the way to do it.
5+
But what if you want to give users more interactive styling options? For example, maybe you want to add Heading color options. Or Heading text-style options. This topic will explain how to user attributes to provide more content type styling options.
66

77
## Install the example module
88

@@ -20,7 +20,7 @@ The example module provides all the code used here to describe the attribute-bas
2020

2121
## Steps for styling with attributes
2222

23-
This general steps to follow when using attributes to apply CSS styling are shown here, followed by detailed instructions for each step:
23+
An overview of the steps is shown here, followed by the detailed instructions for each step:
2424

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

@@ -79,9 +79,9 @@ First, you need to add fields to your content type's form so that users have a w
7979
</form>
8080
```
8181

82-
The names of these fields (`heading_color` and `heading_style`) are particularly important because you will use them as the names of your attributes in your content type configuration file in step 2.
82+
_Two `select` fields added to the native Heading form_
8383

84-
_Select fields added to the native Heading form_
84+
The names of these fields, `heading_color` and `heading_style`, are particularly important. They are the same names you must assign to the attributes in your configuration file. You will do that next in step 2.
8585

8686
## Step 2: Add configuration attributes for field names
8787

@@ -107,19 +107,26 @@ Attributes have a `name` and a `source`. The `name` must correspond to the name
107107
</config>
108108
```
109109

110-
_Element attributes for main mapped to the extended Heading form fields_
110+
_Element attributes for `main` mapped to the extended Heading form fields_
111111

112112
In this example, the `source` values (`data-heading-color` and `data-heading-style`) are rendered in the DOM for the Heading's main element, as shown here:
113113

114114
```html
115-
<h2 data-content-type="heading" data-appearance="default" data-heading-color="brand-green" data-heading-style="style-italic" data-element="main" style="border-style: none; border-width: 1px; border-radius: 0px; opacity: 1;">My Heading Text</h2>
115+
<h2 data-content-type="heading"
116+
data-appearance="default"
117+
data-heading-color="brand-green"
118+
data-heading-style="style-italic"
119+
data-element="main"
120+
style="border-style: none; border-width: 1px; border-radius: 0px; opacity: 1;">
121+
My Heading Text
122+
</h2>
116123
```
117124

118-
The values for these attributes are set by the user from the form fields. In this example, the user selected `brand-green` from the `heading_color` selector field and `style-italic` from the `heading_style` selector field. This adds the attributes and their values to the DOM where they can be targeted with CSS classes from our content type's `_default.less` files (from the adminhtml and frontend areas). These classes are created in step 3.
125+
The values for these attributes are set by the user from the form fields. In this example, the user selected `brand-green` from the `heading_color` selector field and `style-italic` from the `heading_style` selector field. Page Builder adds the attributes and their values to the DOM where they can be targeted with CSS classes from our content type's `_default.less` files (from the adminhtml and frontend areas). You will create these CSS classes in step 3.
119126

120127
## Step 3: Add attribute-based CSS classes
121128

122-
The CSS styles in your `_default.less` files for both your `adminhtml` and `frontend` areas of your module should be attribute-based as shown here in our extended Heading example:
129+
The CSS classes in your `_default.less` files for both the `adminhtml` and `frontend` should be attribute-based, as shown here in our extended Heading example:
123130

124131
```scss
125132
/*-- adminhtml _default.less attribute-based classes */
@@ -168,21 +175,19 @@ The CSS styles in your `_default.less` files for both your `adminhtml` and `fron
168175

169176
_Attribute-based styles for the config attributes (source)_
170177

171-
Again, the values for these attributes are set by the user in the content type's form fields. This makes it easy to target your content type's elements in the DOM based on what the user selected in the form.
178+
The values for these attributes are set by the user from the form field that corresponds to the attribute. This makes it easy to target your content type's elements from your `_default.less` files.
172179

173180
## Step 4: Add Knockout attribute bindings to HTML templates
174181

175-
In order for your attribute selectors to be rendered in the DOM as described in step 2, you must ensure that you add Knockout attributes to your content type's HTML templates. Knockout attribute bindings look like this:
182+
In order for your attribute selectors to be rendered in the DOM as described in step 2, you must add Knockout attribute bindings to your content type's HTML templates. The Knockout attribute bindings look like this:
176183

177184
```json
178185
attr="data.main.attributes"
179186
```
180187

181-
The `main` node refers to the element defined in the configuration file and `attributes` refer to all the `<attributes>` defined for the `main` element.
188+
The Knockout `attr` binding ensures that all the attributes defined for an element in your content type's configuration are rendered in the DOM for that element. In the previous example binding, the `main` node refers to the element defined in the configuration file. And `attributes` refers to all the `<attribute>` nodes defined for the `main` element.
182189

183-
The Knockout `attr` binding ensures that all the attributes defined for an element in your content type's configuration are rendered in the DOM for that element as described in step 2.
184-
185-
These Knockout bindings are applied to the Heading's `master.html` template (as well as the `preview.html` template) as shown here:
190+
These Knockout bindings are applied to the Heading's `master.html` template (as well as the `preview.html` template). The Heading `master.html` template is here:
186191

187192
```html
188193
<!-- Heading master.html -->
@@ -195,19 +200,19 @@ These Knockout bindings are applied to the Heading's `master.html` template (as
195200
<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>
196201
```
197202

198-
_Attribute bindings for Heading's data.main config element_
203+
_Attribute bindings for the Heading's `data.main` config elements_
199204

200205
## Discussion
201206

202-
Page Builder provides three XML configuration nodes you can use to to provide users with styling options for content type elements:
207+
Styling with `<attributes>` is only one of three options that you can use to give end users a way to style content from a form. The other two options are using the `<style>` and `<css>` nodes. The differences between these three options are summarized here:
203208

204-
- `<attribute>` - styles a content type element using an attribute-based CSS class.
205-
- `<style>` - styles content type elements using specific CSS properties.
206-
- `<css>` - styles content types using one or more static CSS classes.
209+
- `<attribute>` nodes - used to style a content-type element using an attribute-based CSS class.
210+
- `<style>` nodes - used to style content-type elements using specific CSS properties.
211+
- `<css>` nodes - used to style content-type elements with static CSS classes entered by the end user.
207212

208-
The `<attribute>` and `<style>` nodes can be used multiple times within a content type element. But the `<css>` node can only be used once per element. The `<css>` node was designed to be used only once per element because its purpose is to map user-entered CSS classes from the form to the template, where it adds those classes to the `class` attribute in the DOM.
213+
The `<attribute>` and `<style>` nodes can be added multiple times to a content-type element. But the `<css>` node can only be added once per element. Refer to [Understanding the <css> node](#understadingthe<css>node).
209214

210-
The following code snippet is from Page Builder's native `heading.xml` configuration file (Magento/PageBuilder/view/adminhtml/pagebuilder/content_type/heading.xml), showing multiple `<style>` nodes, two `<attribute>` nodes and one `<css>` node for the `main` element:
215+
We will use the following snippent from the `heading.xml` configuration file (`Magento/PageBuilder/view/adminhtml/pagebuilder/content_type/heading.xml`) as a reference to our discussion of these nodes and how to use them.
211216

212217
```xml
213218
<!-- snippet from heading.xml -->
@@ -229,21 +234,60 @@ The following code snippet is from Page Builder's native `heading.xml` configura
229234
</element>
230235
```
231236

232-
### `<style>` nodes
237+
{: .bs-callout .bs-callout-info }
238+
The `<tag>` and `<html>` nodes are beyond the scope of this topic, but like the other nodes, they also map to form fields. In this case, the `<tag>` node maps to the Heading's `heading_type` selector field and the `<html>` node maps to the `heading_text` input field from the (`pagebuilder_heading_form.xml`) form.
233239

234-
The `<style>` nodes map to the same-named fields defined in the `pagebuilder_base_form.xml`. These nodes define the inline `style` properties added to the Heading's DOM element. Adding a `<style>` node to a config element provides a way to style your content type using a specific CSS property (such as `opacity`) from within the content type's form.
240+
### Understanding and using `<style>` nodes
235241

236-
### `<attribute>` nodes
242+
In contrast to using attributes for content styling, adding a `<style>` node to a content type config `element` gives you a way to provide end users with a form field that changes a _specific_ CSS property, such as `opacity`.
237243

238-
The `<attribute>` nodes map to the same-named fields defined in the `pagebuilder_heading_form.xml`. These nodes define the custom attributes added to the Heading's DOM element. Adding an `<attribute>` node to a config element provides a way to style your content type using attribute-based CSS classes from within the content type's form.
244+
In the `heading.xml` config example, the `<style>` nodes map to the same-named fields defined in the `pagebuilder_base_form.xml`, which gives users the form fields needed to change each of the CSS properties. When rendered to the DOM, Page Builder adds these CSS properties (and the user-entered values) to the inline `style` attribute for the Heading's DOM element (which could be `<h1>` to `<h6>` depending on the Heading Type selected in the Heading's form).
239245

240-
### `<css>` nodes
246+
For example, if a user selected a value for each of the styles defined in the `heading.xml` config shown above, the inline `style` attribute in the storefront DOM would have entries and values for all the `<style>` nodes defined, as shown here:
241247

242-
The `<css>` node maps to the `css_classes` field from the `pagebuilder_base_form.xml` and adds its values (CSS classes) to the `class` attribute of the Heading's DOM element (h1 through h6).
248+
```html
249+
<h2 data-content-type="heading"
250+
data-appearance="default"
251+
data-heading-color="brand-blue"
252+
data-heading-style="style-default"
253+
data-element="main"
254+
style="text-align: left; border-style: solid; border-color: rgb(240, 240, 240); border-width: 1px; border-radius: 3px; margin: 5px; padding: 2px;">
255+
My Heading Text
256+
</h2>
257+
```
243258

244-
{: .bs-callout .bs-callout-info }
245-
The `<tag>` and `<html>` nodes are beyond the scope of this topic, but they map to the Heading's `heading_type` selector field and `heading_text` input field from the (`pagebuilder_heading_form.xml`) form.
259+
Each `<style>` node defined gets added to the DOM as another CSS property in the inline `style` attribute. In the example module for this topic, we added `<style name="heading_opacity" source="opacity" />` to the `heading.xml` config. So when Page Builder renders the extended Heading content type in the DOM, it adds opacity to all the existing inline styles: `style="... opacity: 1; ..."`.
260+
261+
### Using `<attribute>` nodes instead of `<style>` nodes
262+
263+
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 to change _several_ CSS properties at once. This is much more powerful that using `<style>` nodes which can only change single CSS properties.
264+
265+
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.
266+
267+
### Understanding the `<css>` node
268+
269+
The `<css>` config node is a bit different from the `<attribute>` and `<style>` nodes. The `<css>` node wasn't designed to be added to an element more than once like the other two nodes. Instead, it was designed to capture multiple CSS classes from a single form field and render those classes to the DOM within the `class` attribute. For this reason, it is currently not possible to assign CSS classes to an element's `class` attribute from more than the one field mapped to the `<css>` node.
270+
271+
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.
272+
273+
Out of the box, Page Builder maps the `<css>` config node (for each content type) to the `css_classes` input field from the `pagebuilder_base_form.xml`. For example, if you were to enter two CSS classes into this field within the Heading form, Page Builder would add the `class` attribute to the Heading's `main` element (`<h2>`) and populate it with the CSS classes entered, as shown on the first line here:
274+
275+
![CSS Classes input field](../images/css-class-input-output.png)
276+
277+
```html
278+
<h2 class="fall-heading-style halloween-heading-style"
279+
data-content-type="heading"
280+
data-appearance="default"
281+
data-heading-color="brand-blue"
282+
data-heading-style="style-default"
283+
data-element="main"
284+
style="text-align: left; border-style: solid; border-color: rgb(240, 240, 240); border-width: 1px; border-radius: 3px; margin: 5px; padding: 2px; opacity: 1;">
285+
My Heading Text
286+
</h2>
287+
```
288+
289+
As mentioned, you can override the `<css>` node to map it to a different form field name and field type.
246290

247291
## Final thoughts
248292

249-
Using custom attributes represents one of Page Builder's best practices for adding powerful content styling options to forms. You can add attributes to both existing content types (as shown with the Heading extension) and custom content types. The CSS styling options are only limited by the CSS specs your targeted browsers support. Have fun!
293+
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 Heading extension module) 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!
11.9 KB
Loading

0 commit comments

Comments
 (0)