Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(front): add loading state to posts grid (#1187) #1198

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading