DEV Community

Vishang
Vishang

Posted on • Updated on

Develop a component with Style System in AEM

🐣 Creating a heading component with style system

Creating a heading component from title component.

Create a component called heading inside /apps/your-project-flder/components/content/~create component

create heading.html

<div class="cmp_heading_textbox" data-sly-use.headingObject="heading.js">
    <h1 class="cmp_heading_text" data-sly-element=${currentStyle.type}>${headingObject.title}</h1>
</div>
Enter fullscreen mode Exit fullscreen mode

create heading.js

"use strict";
use(function() {
    var titleByAuthor = granite.resource.properties["titleProperty"];
    var jcrtitle = currentPage.title;
    var pageName = currentPage.name;
    var title = titleByAuthor || jcrtitle || pageName;

    return {
        title:title
    };
});
Enter fullscreen mode Exit fullscreen mode

Next thing we need is the dialog boxes. So copy cq:dialog and cq:design_dialog from title component.

Now in order to have style tab. Copy styletab node from any core component and paste it inside items node in cq:design_dialog

Add a styles in your clientlib-components folder. Create a clientlib folder for your component. create css.txt and styles.less files inside css folder as per the standard practice for adding styles to the component.
Image description

We are using BEM notation to create the styling.
style.less

.headingTextBox-dark {
    .cmp_heading_textbox {
    padding: 2px;
        background-color:#555555;
    }
    .cmp_heading_text {
    color: #ffffff;
    }
}
.headingTextBox-light {
    .cmp_heading_textbox {
    padding: 2px;
        background-color:#FFFFFF;
    }
    .cmp_heading_text {
    color: #555555;
    }
}
.headingTextBox-bordered {
    .cmp_heading_textbox {
    border: 2px solid #00b2b2;
    }
}
.headingText-center {
    .cmp_heading_text {
    text-align: center;
    }
}
Enter fullscreen mode Exit fullscreen mode

Once done content author can create styling by adding styles to the component via component policy.
Something like this 👇
Image description

Once done you can see pencil ✎ symbol in your component which shows different styles assigned.

Top comments (2)

Collapse
 
mattmoranjava profile image
Matt Moran

@vish448 - is there any way to make a style selected by default in the policy editor, or does it just default to whatever's top of the drop-down lists?

Collapse
 
vish448 profile image
Vishang

yes you're right. It's just default to whatever top of the drop-down