{"id":59442,"date":"2023-12-04T15:58:40","date_gmt":"2023-12-04T10:28:40","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=59442"},"modified":"2024-06-10T15:35:26","modified_gmt":"2024-06-10T10:05:26","slug":"dynamic-css-using-aem-style-system-and-custom-tool","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/dynamic-css-using-aem-style-system-and-custom-tool\/","title":{"rendered":"Dynamic CSS using AEM style system and custom tool"},"content":{"rendered":"<h2><b>Summary<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">A business requirement in my project wanted specific users to reskin or restyle AEM components without needing deployment. We know that with the help of OOTB style systems, we can configure styles for the component, but it would still require code deployment. So, the solution was to build a tool allowing authorized users to upload CSS to a specific location in AEM and restyle or reskin AEM components with the help of OOTB-style systems.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let me guide you through the steps that I undertook to develop the same.<\/span><\/p>\n<h2><b>Prerequisites<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">For this, you should<\/span><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\"> Have basic knowledge of AEM-style systems<\/span><\/li>\n<li><span style=\"font-weight: 400;\"> Know how to create a custom tool in AEM<\/span><\/li>\n<li><span style=\"font-weight: 400;\"> Know how to write BEM-specific CSS<\/span><\/li>\n<\/ol>\n<h2><b>Steps taken to achieve the task<\/b><\/h2>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Create a custom tool<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Write CSS with BEM notations<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Configure styles in the component<\/span><\/li>\n<\/ol>\n<h2><b>Create a custom tool<\/b><\/h2>\n<ol>\n<li><span style=\"font-weight: 400;\"> First, we need to design a custom tool with the following features:<\/span><\/li>\n<\/ol>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Upload a CSS file to a specific location inside AEM. Example: &#8211; under \/var\/dynamic-css<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Delete a file under a specific location<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">\u00a0 2. To create a custom tool, please refer to this <\/span><a href=\"https:\/\/experienceleaguecommunities.adobe.com\/t5\/adobe-experience-manager\/create-custom-aem-menu-tools-with-granite-ui-shell-aem-community\/m-p\/378396\"><span style=\"font-weight: 400;\">link<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h2><b>Write CSS with BEM notations<\/b><\/h2>\n<ol>\n<li><span style=\"font-weight: 400;\"> The\u00a0<\/span><b>Block, Element, Modifier <\/b>methodology<span style=\"font-weight: 400;\">\u00a0(commonly referred to as\u00a0<\/span><a href=\"https:\/\/en.bem.info\/method\"><span style=\"font-weight: 400;\">BEM<\/span><\/a><span style=\"font-weight: 400;\">) is a popular\u00a0<\/span><i><span style=\"font-weight: 400;\">naming convention for<\/span><\/i><span style=\"font-weight: 400;\">\u00a0classes in HTML and CSS.\u00a0<\/span><a href=\"https:\/\/en.bem.info\/\"><span style=\"font-weight: 400;\">Developed by<\/span><\/a><span style=\"font-weight: 400;\"> the team at Yandex, its goal is to help developers better understand the relationship between HTML and CSS in a given project.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Here\u2019s an example of what a CSS developer writing in the BEM style might write:<\/span><\/p>\n<pre><i><span style=\"font-weight: 400;\">\/* Block component *\/<\/span><\/i>\r\n<span style=\"font-weight: 400;\">.cmp-title <\/span><span style=\"font-weight: 400;\">{}<\/span>\r\n\r\n<i><span style=\"font-weight: 400;\">\/* Element that depends upon the block *\/<\/span><\/i> \r\n<span style=\"font-weight: 400;\">.cmp-title__text <\/span><span style=\"font-weight: 400;\">{}<\/span>\r\n\r\n<i><span style=\"font-weight: 400;\">\/* Modifier that changes the style of the block *\/<\/span><\/i>\r\n<span style=\"font-weight: 400;\">. cmp-title__text --orange <\/span><span style=\"font-weight: 400;\">{}<\/span> \r\n<span style=\"font-weight: 400;\">. cmp-title__text --bold <\/span><span style=\"font-weight: 400;\">{}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">In this CSS methodology, a <\/span><b>block<\/b><span style=\"font-weight: 400;\"> is a top-level abstraction of a new component, for example, a cmp-title:\u00a0.cmp-title { }. This block should be thought of as a parent. Child items, or\u00a0<\/span><b>elements<\/b><span style=\"font-weight: 400;\">, can be placed inside, denoted by two underscores following the block&#8217;s name like .__text { }. Finally, <\/span><b>modifiers<\/b><span style=\"font-weight: 400;\"> can manipulate the block to theme or style that particular component without inflicting changes on a completely unrelated module. This is done by appending two hyphens to the block&#8217;s name, like cmp-title__text&#8211;orange.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here is the example markup of the title core component.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">&lt;div data-sly-use.title=\"com.adobe.cq.wcm.core.components.models.Title\"<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0data-sly-use.template=\"core\/wcm\/components\/commons\/v1\/templates.html\"<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0data-sly-test.text=\"${title.text}\"<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0data-cmp-data-layer=\"${title.data.json}\"<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0id=\"${title.id}\"<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0class=\"<\/span><b>cmp-title<\/b><span style=\"font-weight: 400;\">\"&gt;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0&lt;h1 class=\"<\/span><b>cmp-title__text<\/b><span style=\"font-weight: 400;\">\" data-sly-element=\"${title.type}\"&gt;&lt;a<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0data-sly-unwrap=\"${!title.link.valid || title.linkDisabled}\"<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0class=\"<\/span><b>cmp-title__link<\/b><span style=\"font-weight: 400;\">\"<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0data-sly-attribute=\"${title.link.htmlAttributes}\"<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0data-cmp-clickable=\"${title.data ? true : false}\"&gt;${text}&lt;\/a&gt;&lt;\/h1&gt;<\/span>\r\n<span style=\"font-weight: 400;\">&lt;\/div&gt;<\/span><\/pre>\n<h2><b>Configure styles in the component<\/b><\/h2>\n<ol>\n<li><span style=\"font-weight: 400;\"> In AEM, navigate to <\/span><b>Templates <\/b><span style=\"font-weight: 400;\">from <\/span><b>Tools<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"> Edit the template and go to <\/span><b>component policy settings<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Edit the policy of the\u00a0<\/span><b>Title <\/b><span style=\"font-weight: 400;\">component by tapping or clicking the\u00a0<\/span><b>Policy <\/b><span style=\"font-weight: 400;\">button.<\/span><\/li>\n<\/ol>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59436 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/12\/1-1024x487.jpg\" alt=\"\" width=\"625\" height=\"297\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/12\/1-1024x487.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/1-300x143.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/1-768x365.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/1-624x297.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/1.jpg 1312w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0 \u00a04. <\/span><span style=\"font-weight: 400;\">On the <\/span><b>Styles tab<\/b><span style=\"font-weight: 400;\"> of the properties, you can see how the styles have been configured.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59437 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/12\/Configure-Styles.jpg\" alt=\"\" width=\"528\" height=\"1005\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/12\/Configure-Styles.jpg 528w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Configure-Styles-158x300.jpg 158w\" sizes=\"(max-width: 528px) 100vw, 528px\" \/><\/p>\n<ol start=\"5\">\n<li>Once the styles have been configured, click on <strong>Done<\/strong>.<\/li>\n<li>Now you need to create a client lib folder at the component level with a CSS file that has the below import statement (path pointing to CSS upload folder).<\/li>\n<\/ol>\n<pre><em>@import url(\"\/var\/..\/.. \/title.css\")<\/em><\/pre>\n<ol start=\"7\">\n<li>Give a category name to the clientlib folder, say <em>core.dynamic.title.styles.<\/em><\/li>\n<li>Now go to the template page policy.<\/li>\n<\/ol>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59438 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/12\/Page-Policy.jpg\" alt=\"\" width=\"759\" height=\"423\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/12\/Page-Policy.jpg 759w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Page-Policy-300x167.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Page-Policy-624x348.jpg 624w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/p>\n<ol start=\"9\">\n<li>Add the category of the component clientlib folder in the properties tab.<\/li>\n<\/ol>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59439 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/12\/Page-Policy-Properties.jpg\" alt=\"\" width=\"805\" height=\"451\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/12\/Page-Policy-Properties.jpg 805w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Page-Policy-Properties-300x168.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Page-Policy-Properties-768x430.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Page-Policy-Properties-624x350.jpg 624w\" sizes=\"(max-width: 805px) 100vw, 805px\" \/><\/p>\n<p>10. Now go to your page, click the<strong> paint brush<\/strong> icon, select the style, and apply.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59440 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/12\/Apply-Styles-1024x344.jpg\" alt=\"\" width=\"625\" height=\"210\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/12\/Apply-Styles-1024x344.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Apply-Styles-300x101.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Apply-Styles-768x258.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Apply-Styles-624x210.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Apply-Styles.jpg 1027w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<ol start=\"11\">\n<li>Once the style is applied, you can upload another CSS via the tool and reskin the component accordingly.<\/li>\n<\/ol>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59441 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/12\/Title-Component-1024x308.jpg\" alt=\"\" width=\"625\" height=\"188\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/12\/Title-Component-1024x308.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Title-Component-300x90.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Title-Component-768x231.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Title-Component-624x188.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/12\/Title-Component.jpg 1240w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Summary\u00a0 A business requirement in my project wanted specific users to reskin or restyle AEM components without needing deployment. We know that with the help of OOTB style systems, we can configure styles for the component, but it would still require code deployment. So, the solution was to build a tool allowing authorized users to [&hellip;]<\/p>\n","protected":false},"author":1345,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":237},"categories":[5868],"tags":[4847,5558,5560,5561,5559],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59442"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1345"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=59442"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59442\/revisions"}],"predecessor-version":[{"id":59462,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59442\/revisions\/59462"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=59442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=59442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=59442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}