Skip to content

Commit

Permalink
fix(front): add loading state to posts grid (#1187)
Browse files Browse the repository at this point in the history
Closes: #1187
  • Loading branch information
dgrebb committed Jan 26, 2024
1 parent 7716081 commit 923084c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-backstop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ jobs:
if: steps.backstop.outcome != 'success'
env:
REPORT_PATH: ${{ inputs.env }}
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: backstop-report
retention-days: 3
Expand Down
2 changes: 1 addition & 1 deletion front/src/lib/components/posts/Code/Code.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json from 'svelte-highlight/languages/json';
import plaintext from 'svelte-highlight/languages/plaintext';
import xml from 'svelte-highlight/languages/xml';
import CodeCopy from '@components/posts/Code/CodeCopy.svelte';
import CodeCopy from './CodeCopy.svelte';
import '@styles/components/Code/Code.css';
export let pageTitle, slug, syntax, code, showLineNumbers;
Expand Down
13 changes: 4 additions & 9 deletions front/src/lib/components/posts/PostsGrid.svelte
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>
49 changes: 49 additions & 0 deletions front/src/lib/components/posts/PostsGridItem.svelte
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>
3 changes: 3 additions & 0 deletions front/static/styles/noscript.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ body .footer {
.post .hero-wrap noscript .hero.noscript {
display: block;
}
.posts-grid .posts-grid-item {
opacity: 1 !important;
}

0 comments on commit 923084c

Please sign in to comment.