在此影片中,我們將審視CSS(或 LESS)和JavaScript,以及這些樣式如何套用至HTML和DOM,來設定使用樣式系統來設定Experience Manager核心標題元件的樣式。
提供的AEM套件(technical-review.sites.style-system-1.0.0.zip)會安裝範例標題樣式、We.Retail配置容器和標題元件的範例原則,以及範例頁面。
technical-review.sites.style-system-1.0.0.zip
以下是 LESS 在以下位置找到的示例樣式的定義:
/apps/demo/sites/style-system/clientlib-example/components/titles/styles/example.less
對於偏好CSS的使用者,此程式碼片段下方為此的CSS LESS 編譯為。
/* LESS */
.cmp-title--example {
.cmp-title {
text-align: center;
.cmp-title__text {
color: #EB212E;
font-weight: 600;
font-size: 5rem;
border-bottom: solid 1px #ddd;
padding-bottom: 0;
margin-bottom: .25rem
}
// Last Modified At element injected via JS
.cmp-title__last-modified-at {
color: #999;
font-size: 1.5rem;
font-style: italic;
font-weight: 200;
}
}
}
上述 LESS 由Experience Manager以原生方式編譯為下列CSS。
/* CSS */
.cmp-title--example .cmp-title {
text-align: center;
}
.cmp-title--example .cmp-title .cmp-title__text {
color: #EB212E;
font-weight: 600;
font-size: 5rem;
border-bottom: solid 1px #ddd;
padding-bottom: 0;
margin-bottom: 0.25rem;
}
.cmp-title--example .cmp-title .cmp-title__last-modified-at {
color: #999;
font-size: 1.5rem;
font-style: italic;
font-weight: 200;
}
將範例樣式套用至標題元件時,下列JavaScript會收集並插入目前頁面的上次修改日期和時間。
jQuery的使用是可選的,也是使用的命名慣例。
以下是 LESS 在以下位置找到的示例樣式的定義:
/apps/demo/sites/style-system/clientlib-example/components/titles/styles/js/title.js
/**
* JavaScript supporting Styles may be re-used across multi Component Styles.
*
* For example:
* Many styles may require the Components Image (provided via an <img> element) to be set as the background-image.
* A single JavaScript function may be used to adjust the DOM for all styles that required this effect.
*
* JavaScript must react to the DOMNodeInserted event to handle style-switching in the Page Editor Authoring experience.
* JavaScript must also run on DOM ready to handle the initial page load rendering (AEM Publish).
* JavaScript must mark and check for elements as processed to avoid cyclic processing (ie. if the JavaScript inserts a DOM node of its own).
*/
jQuery(function ($) {
"use strict;"
moment.locale("en");
/**
* Method that injects p element, containing the current pages last modified date/time, under the title text.
*
* Similar to the CSS Style application, component HTML elements should be targeted via the BEM class names (as they define the stable API)
* and targeting "raw" elements (ex. "li", "a") should be avoided.
*/
function applyComponentStyles() {
$(".cmp-title--example").not("[data-styles-title-last-modified-processed]").each(function () {
var title = $(this).attr("data-styles-title-last-modified-processed", true),
url = Granite.HTTP.getPath() + ".model.json";
$.getJSON(url, function(data) {
var dateObject = moment(data['lastModifiedDate']),
titleText = title.find('.cmp-title__text');
titleText.after($("<p>").addClass("cmp-title__last-modified-at").text("Last modified " + dateObject.fromNow()));
});
});
}
// Handle DOM Ready event
applyComponentStyles();
// Apply Styles when a component is inserted into the DOM (ie. during Authoring)
$(".responsivegrid").bind("DOMNodeInserted", applyComponentStyles);
});
好 — 元件中的所有元素都可透過BEM記號定址:
<!-- Good practice -->
<div class="cmp-list">
<ul class="cmp-list__item-group">
<li class="cmp-list__item">...</li>
</ul>
</div>
壞 — 清單和清單元素只能依元素名稱定址:
<!-- Bad practice -->
<div class="cmp-list">
<ul>
<li>...</li>
</ul>
</div>
公開更多資料並隱藏它比公開太少的資料要好,這些資料需要未來的後端開發才能公開它。
實作可供作者切換的內容有助於保持此HTML精簡,讓作者能夠選取要寫入HTML的內容元素。 將影像寫入HTML時,可能不會用於所有樣式時,可能特別重要。
此規則的例外情況是,昂貴的資源(例如影像)預設會公開,因為CSS隱藏的事件影像在此情況下會不必要地擷取。
保持選擇器重量/特異性一致;這有助於避免和解決難以識別的CSS階層衝突。
將每個樣式組織為離散檔案。
@imports
或者,如果需要原始CSS,可透過「HTML用戶端程式庫」檔案包含,或自訂前端資產建置系統。避免混合許多複雜的樣式。
請一律使用CSS類別(遵循BEM標籤法)來定義CSS規則。
避免使用樣式 BLOCK--MODIFIER
直接在回應式格線上。 更改此元素的顯示可能會影響響應網格的呈現和功能,因此,當更改響應網格的行為時,只有在此級別上的樣式。
使用應用樣式範圍 BLOCK--MODIFIER
. 此 BLOCK__ELEMENT--MODIFIERS
可用於元件,但由於 BLOCK
代表元件,元件是已設定樣式的元件,樣式是「已定義」且範圍是透過 BLOCK--MODIFIER
.
範例CSS選取器結構應如下:
第一級選擇器 塊 — 修飾符 |
第2級選擇器 區塊 |
第3級選擇器 BLOCK__ELEMENT |
有效的CSS選取器 | |
.cmp-list-dark | .cmp-list | .cmp-list__item | → | .cmp-list-dark .cmp-list .cmp-list__item { 顏色:藍色; } |
.cmp-image—hero | .cmp-image | .cmp-image__caption | → | .cmp-image—hero .cmp-image .cmp-image__caption { 顏色:紅色; } |
如果是巢狀元件,這些巢狀元件元素的CSS選取器深度將超過第3級選取器。 對巢狀元件重複相同的模式,但由父元件限定範圍 BLOCK
. 換句話說,啟動巢狀元件的 BLOCK
在第3層,且巢狀元件的 ELEMENT
位於第4個選取器層級。
本節中定義的最佳實務涉及「style-JavaScript」,或專門用於以風格而非功能目的操控元件的JavaScript。
BLOCK--MODIFIERs
.BLOCK--MODIFIER BLOCK
,並在JavaScript中的所有DOM操作完成時顯示。