Style System

The Style System allows a template author to define style classes in the content policy of a component so that a content author is able to select them when editing the component on a page. These styles can be alternative visual variations of a component, making the component more flexible.

This eliminates the need to develop a custom component for each style or to customize the component dialog to enable such style functionality. It leads to more reusable components that can be quickly and easily adapted to the needs of content authors without any AEM back-end development.

Use Case

Template authors not only need the ability to configure how components function for the content authors, but also to configure several alternative visual variations of a component.

Likewise, content authors not only need the ability to structure and arrange their content, but also to select how it is presented visually.

The Style System provides a unified solution to both the template author’s and content author’s requirements:

  • Template authors can define style classes in the content policy of components.
  • Content authors can then select these classes from a drop-down list when editing the component on a page so they can apply the corresponding styles.

The style class is then inserted on the decoration wrapper element of the component so that the component developer does not need to be concerned with handling the styles beyond providing their CSS rules.

Overview

Using the Style System generally takes the following form.

  1. The web designer creates different visual variations of a component.

  2. The HTML developer is provided with the HTML output of the components and the desired visual variations to implement.

  3. The HTML developer defines the CSS classes that correspond to each visual variation and are to be inserted on the element wrapping the components.

  4. The HTML developer implements the corresponding CSS code (and optionally JS code) for each of the visual variations so that they look as defined.

  5. The AEM developer places the provided CSS (and optional JS) in a Client Library and deploys it.

  6. The AEM developer or template author configures the page templates and edits the policy of each styled component, adding the defined CSS classes, giving user-friendly names to each style, and indicating which styles can be combined.

  7. The AEM page author can then choose the designed styles in the page editor via the style menu of the component’s toolbar.

Only the last three steps are actually carried out in AEM. This means all development of the necessary CSS and JavaScript can be done without AEM.

Actually implementing the styles only requires deployment on AEM and selection within the components of the desired templates.

The following diagram illustrates the architecture of the Style System.

aem-style-system

Use

To demonstrate the feature, we will use WKND’s implementation of the core component’s title component as an example.

The following sections As a Content Author and As a Template Author describe how to test the functionality of the Style System using the Style System of WKND.

If you want to use the Style System for your own components do the following:

  1. Install the CSS as client libraries as discussed in the section Overview.
  2. Configure the CSS classes that you want to make available to your content authors as described in the section As a Template Author.
  3. Content authors can then use the styles as described in the section As a Content Author.

As a Content Author

  1. After installing the WKND project, navigate to WKND’s English language master home page at http://<host>:<port>/sites.html/content/wknd/language-masters/en and edit the page.

  2. Select a Title component further down the page

    Style System for the author

  3. Select the Styles button on the toolbar of the List component to open the style menu and change the appearance of the component.

    Selecting styles

    note note
    NOTE
    In this example, the Colors styles (Black, White, and Gray) are mutually exclusive, while the Style options (Underline, Align Right, and Mini Spacing) can be combined. This can be configured in the template as the template author.

As a Template Author

  1. While editing WKND’s English language master home page at http://<host>:<port>/sites.html/content/wknd/language-masters/en, edit the template of the page via Page Information > Edit Template.

    Edit Template

  2. Edit the policy of the Title component by tapping or clicking the Policy button of the component.

    Edit policy

  3. On the Styles tab of the properties, you can see how the styles have been configured.

    Edit properties

    • Group Name: Styles can be grouped together within the style menu that the content author sees when configuring the style of the component.
    • Styles can be combined: Allows for multiple styles within that group to be selected at one time.
    • Style Name: The description of the style that will display to the content author when configuring the style of the component.
    • CSS Classes: The actual name of the CSS class associated with the style.

    Use the drag handles to arrange the order of the groups and the styles within the groups. Use the add or delete icons to add or remove groups or styles within the groups.

CAUTION
The CSS classes – and any necessary JavaScript – configured as style properties of a component’s policy, must be deployed as Client Libraries to work.

Setup

Core Components version 2 and later are fully enabled to take advantage of the Style System and require no additional configuration.

The following steps are only necessary to enable the Style System for your own custom components or to enable the optional Styles tab in the Edit dialog.

Enable Style Tab in Design Dialog

For a component to work with AEM’s Style System and show the style tab in its design dialog, the component developer must include the style tab with the following settings on the component:

  • path = "/mnt/overlay/cq/gui/components/authoring/dialog/style/tab_design/styletab"
  • sling:resourceType = "granite/ui/components/coral/foundation/include"
NOTE
This uses overlays, by means of the Sling Resource Merger.

With the component configured, the styles configured by the page authors are automatically inserted by AEM on the decoration element that AEM automatically wraps around every editable component. The component itself need not do anything else to make this happen.

Enable Styles Tab in Edit Dialog

An optional Styles tab in the Edit Dialog is also available. Unlike the Design Dialog tab, the tab in the Edit Dialog is not essential for the Style System to function, but is an optional alternative interface for a content author to set styles.

The edit dialog tab can be included in a similar way to the design dialog tab:

  • path = "/mnt/overlay/cq/gui/components/authoring/dialog/style/tab_edit/styletab"
  • sling:resourceType = "granite/ui/components/coral/foundation/include"
NOTE
This uses overlays, by means of the Sling Resource Merger.
NOTE
The Styles tab on the Edit Dialog is not enabled by default.

Styles with Element Names

A developer can also configure a list of allowed element names for styles on the component with the cq:styleElements string array property. Then in the Styles tab of the policy within the design dialog, the template author can also choose an element name to be set for each style. This will set the element name of the wrapper element.

This property is set on the cq:Component node. For example:

  • /apps/<yoursite>/components/content/list@cq:styleElements=[div,section,span]
CAUTION
Avoid defining element names for styles that can be combined. When multiple element names are defined, the order of priority is:
  1. HTL takes precedence over everything: data-sly-resource="${'path/to/resource' @ decorationTagName='span'}
  2. Then among multiple active styles, the first style in the list of styles configured in the component’s policy is taken.
  3. Finally, the component’s cq:htmlTag/ cq:tagName is considered as a fallback value.

This ability to define style names is useful for generic components, like the Layout Container, or the Content Fragment component, to provide them with additional meaning.

For instance it allows a Layout Container to be given semantics like <main>, <aside>, <nav>, and so on.

fbcff2a9-b6fe-4574-b04a-21e75df764ab