-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(front): add loading state to posts grid (#1187)
Closes: #1187
- Loading branch information
Showing
5 changed files
with
58 additions
and
11 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
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
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 |
---|---|---|
@@ -1,17 +1,12 @@ | ||
<script> | ||
import PostsGridItem from './PostsGridItem.svelte'; | ||
import '@styles/components/posts-grid.css'; | ||
export let gridItems; | ||
</script> | ||
|
||
<ul class="posts-grid"> | ||
{#each gridItems as { lazyImage, slug, title }} | ||
<li | ||
class="posts-grid-item" | ||
style={lazyImage && `background-image: url('${lazyImage}');`} | ||
> | ||
<a href="/post/{slug}/" class="posts-grid-link"> | ||
<h2 class="link-bg">{title}</h2> | ||
</a> | ||
</li> | ||
{#each gridItems as gridItem} | ||
<PostsGridItem {...gridItem} /> | ||
{/each} | ||
</ul> |
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,49 @@ | ||
<script> | ||
import { onMount } from 'svelte'; | ||
import '@styles/components/posts-grid.css'; | ||
export let lazyImage, slug, title; | ||
let loaded = false; | ||
/** | ||
* Lifecycle function executed after the component is mounted. | ||
* Asynchronously loads the image and updates the component state accordingly. | ||
* | ||
* @function onMount | ||
* @async | ||
* @throws {Error} - Throws an error if image loading fails. | ||
*/ | ||
onMount(async function () { | ||
/** | ||
* Image object for loading the SVG icon. | ||
* @type {HTMLImageElement} | ||
*/ | ||
const img = new Image(); | ||
img.src = lazyImage; | ||
img.onload = function () { | ||
loaded = true; | ||
}; | ||
}); | ||
</script> | ||
|
||
<li | ||
class:loaded | ||
class="posts-grid-item" | ||
style={lazyImage && `background-image: url('${lazyImage}');`} | ||
> | ||
<a href="/post/{slug}/" class="posts-grid-link" class:loaded> | ||
<h2 class="link-bg">{title}</h2> | ||
</a> | ||
</li> | ||
|
||
<style lang="postcss"> | ||
.posts-grid-item { | ||
opacity: 0; | ||
transition-duration: var(--transition-duration); | ||
transition-property: opacity; | ||
transition-timing-function: var(--transition-timing-function); | ||
&.loaded { | ||
opacity: 1; | ||
} | ||
} | ||
</style> |
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