Commit 271f789a authored by Angelo Gulina's avatar Angelo Gulina Committed by Marcia Ramos

Docs: Refine waitForCSSLoaded example and explanation

parent eecd6dee
......@@ -20,17 +20,7 @@ const handleStartupEvents = () => {
}
};
/* Wait for.... The methods can be used:
- with a callback (preferred),
waitFor(action)
- with then (discouraged),
await waitFor().then(action);
- with await,
await waitFor;
action();
-*/
/* For `waitForCSSLoaded` methods, see docs.gitlab.com/ee/development/fe_guide/performance.html#important-considerations */
export const waitForCSSLoaded = (action = () => {}) => {
if (!gon?.features?.startupCss || allLinksLoaded()) {
return new Promise(resolve => {
......
......@@ -115,8 +115,36 @@ browser's developer console while on any page within GitLab.
import initMyWidget from './my_widget';
import { waitForCSSLoaded } from '~/helpers/startup_css_helper';
waitForCSSLoaded(initMyWidget);
```
Note that `waitForCSSLoaded()` methods supports receiving the action in different ways:
- With a callback:
```javascript
waitForCSSLoaded(action)
```
- With `then()`:
```javascript
waitForCSSLoaded().then(action);
```
- With `await` followed by `action`:
```javascript
await waitForCSSLoaded;
action();
```
For example, see how we use this in [app/assets/javascripts/pages/projects/graphs/charts/index.js](https://gitlab.com/gitlab-org/gitlab/-/commit/5e90885d6afd4497002df55bf015b338efcfc3c5#02e81de37f5b1716a3ef3222fa7f7edf22c40969_9_8):
```javascript
waitForCSSLoaded(() => {
initMyWidget();
const languagesContainer = document.getElementById('js-languages-chart');
//...
});
```
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment