Use data-id attribute to select loading message, instead of CSS class… (#9271)

Backports PR #9248

**Commit 1:**
Use data-id attribute to select loading message, instead of CSS class since that creates a brittle coupling between JS and CSS.
- Formalize this rule in the CSS style guide.

* Original sha: d5747639cc
* Authored by CJ Cenizal <cj@cenizal.com> on 2016-11-29T17:28:48Z

**Commit 2:**
Use uniquely and semantically named data attribute instead of data-id.

* Original sha: 0b426286f5
* Authored by CJ Cenizal <cj@cenizal.com> on 2016-11-29T20:24:54Z
This commit is contained in:
jasper 2016-11-30 10:13:04 -05:00 committed by CJ Cenizal
parent c36e636e9b
commit 76175e6b30
2 changed files with 25 additions and 5 deletions

View file

@ -116,17 +116,19 @@ block content
.kibanaWelcomeLogo
.kibanaLoader__content
| Loading Kibana
.kibanaLoadingMessage.
Give me a moment here. I&rsquo;m loading a whole bunch of code. Don&rsquo;t worry, all this
good stuff will be cached up for next time!
.kibanaLoadingMessage(
data-remove-message-when-embedded
)
| Give me a moment here. I&rsquo;m loading a whole bunch of code. Don&rsquo;t worry, all this
| good stuff will be cached up for next time!
script.
window.onload = function () {
var hideLoadingMessage = /#.*[?&]embed(&|$|=true)/.test(window.location.href);
if (hideLoadingMessage) {
var loading = document.querySelector('.kibanaWelcomeView');
loading.removeChild(loading.lastChild);
var loadingMessage = document.querySelector('[data-remove-message-when-embedded]');
loadingMessage.parentNode.removeChild(loadingMessage);
}
var buildNum = #{kibanaPayload.buildNum};

View file

@ -2,6 +2,7 @@
# CSS Style Guide
- [CSS Style Guide](#css-style-guide)
- [Selecting elements](#selecting-elements)
- [Using the preprocessor](#using-the-preprocessor)
- [Don't build concatenated selector names](#dont-build-concatenated-selector-names)
- [Avoid nested selectors](#avoid-nested-selectors)
@ -15,6 +16,23 @@
- [How to apply DRY](#how-to-apply-dry)
- [Compelling reasons for using mixins](#compelling-reasons-for-using-mixins)
## Selecting elements
References to CSS selectors within JavaScript are difficult to discover, making it easy to accidentally
break the UI when refactoring markup or CSS.
Instead, add a `data` attribute with a unique and descriptive name and select the element using that.
```html
<div data-welcome-message>Hello, world</div>
```
```javascript
const welcomeMessage = document.querySelector('[data-welcome-message]');
```
This uncouples our CSS from our JavaScript, making it easy to change each independently of the other.
## Using the preprocessor
### Don't build concatenated selector names