-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(esl-lazy-template): introducing
esl-lazy-template
mixin to loa…
…d HTML parts lazy and asynchronous (#2535)
- Loading branch information
Showing
17 changed files
with
505 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
site/src/esl-lazy-template-demo/distance-to-viewport-alert.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {ESLMixinElement} from '@exadel/esl/modules/esl-mixin-element/core'; | ||
import {ready} from '@exadel/esl/modules/esl-utils/decorators'; | ||
import {ESLEventUtils} from '@exadel/esl/modules/esl-utils/dom/events'; | ||
import {Rect} from '@exadel/esl/modules/esl-utils/dom/rect'; | ||
import {getWindowRect} from '@exadel/esl/modules/esl-utils/dom/window'; | ||
import {getViewportForEl} from '@exadel/esl/modules/esl-utils/dom/scroll'; | ||
|
||
export class ESLDemoDistanceToViewportAlert extends ESLMixinElement { | ||
public static override is = 'distance-to-viewport-alert'; | ||
|
||
@ready | ||
protected override connectedCallback(): void { | ||
super.connectedCallback(); | ||
this.showAlert(); | ||
} | ||
|
||
protected calculateDistance(): number { | ||
let topDistance; | ||
let bottomDistance; | ||
const elementRect = Rect.from(this.$host.getBoundingClientRect()); | ||
const $root = getViewportForEl(this.$host) as HTMLElement; | ||
if (!$root) { // window | ||
const windowRect = getWindowRect(); | ||
topDistance = elementRect.top - windowRect.height; | ||
bottomDistance = -elementRect.bottom; | ||
} else { | ||
const windowRect = Rect.from($root.getBoundingClientRect()); | ||
topDistance = elementRect.top - windowRect.bottom; | ||
bottomDistance = -elementRect.bottom; | ||
} | ||
return Math.max(topDistance, bottomDistance); | ||
} | ||
|
||
protected showAlert(): void { | ||
const distance = this.calculateDistance(); | ||
const text = distance < 0 | ||
? `The element with id="${this.$host.id}" was connected to DOM when it was in viewport` | ||
: `The element with id="${this.$host.id}" was connected to DOM at the distance ${distance}px from the viewport`; | ||
|
||
const detail = { | ||
text, | ||
cls: 'alert-info', | ||
hideDelay: 5000 | ||
}; | ||
ESLEventUtils.dispatch(document.body, 'esl:alert:show', {detail}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions
5
site/static/assets/lazy-templates/animated-geometric-shapes-background.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div id="template-from-url-1" | ||
class="text-center" | ||
distance-to-viewport-alert | ||
style="background: lightyellow; border: 3px dashed darkorange;"> | ||
<p>...</p> | ||
<p>I am content No.1 which was added to DOM by the lazy template and loaded from the specified URL</p> | ||
<p>I am inside DIV with id="template-from-url-1".</p> | ||
<p>...</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div id="template-from-url-2" | ||
class="text-center" | ||
distance-to-viewport-alert | ||
style="background: lightyellow; border: 3px dashed darkorange;"> | ||
<p>...</p> | ||
<p>I am content No.2 which was added to DOM by the lazy template and loaded from the specified URL</p> | ||
<p>I am inside DIV with id="template-from-url-2".</p> | ||
<p>...</p> | ||
</div> |
5 changes: 5 additions & 0 deletions
5
site/static/assets/lazy-templates/wind-blowing-in-may-svg-animation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
layout: content | ||
title: ESL Lazy Template | ||
seoTitle: ESL Lazy Template is a custom mixin that can make parts of the page markup lazy, which will have a good effect on the performance of pages | ||
name: ESL Lazy Template | ||
tags: [components, beta] | ||
aside: | ||
source: src/modules/esl-lazy-template | ||
examples: | ||
- lazy-template | ||
--- | ||
|
||
{% mdRender 'src/modules/esl-lazy-template/README.md', 'intro' %} |
Oops, something went wrong.