Skip to content

Commit

Permalink
Make rendering elements declarative
Browse files Browse the repository at this point in the history
All items will be on the page, and hidden/visible based on loading state.
  • Loading branch information
ryelle committed Jan 31, 2024
1 parent 2a80ec3 commit ef9a51b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,22 @@
<div
class="wporg-site-screenshot__mshot-container"
data-wp-init="callbacks.init"
data-wp-watch="callbacks.update"
data-wp-class--has-loaded="state.hasLoaded"
>
<div class="wporg-site-screenshot__loader"></div>
<div
data-wp-class--wporg-site-screenshot__loader="!state.hasLoaded"
data-wp-class--wporg-site-screenshot__error="state.hasError"
>
<img
data-wp-bind--hidden="!state.base64Image"
data-wp-bind--alt="context.alt"
data-wp-bind--src="state.base64Image"
/>
<span
data-wp-bind--hidden="state.base64Image"
data-wp-text="context.alt"
></span>
</div>
</div>
<?php else : ?>
<img
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
justify-content: center;
height: 100%;

img,
span {
display: none;
}

&::after {
content: "";
display: inline-block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* WordPress dependencies
*/
import { getContext, getElement, store } from '@wordpress/interactivity';
import { getContext, store } from '@wordpress/interactivity';

/**
* Module constants
Expand All @@ -21,6 +21,13 @@ const { actions, state } = store( 'wporg/showcase/screenshot', {
get base64Image() {
return getContext().base64Image;
},
get hasError() {
return getContext().hasError;
},
get hasLoaded() {
console.log( state.base64Image, state.hasError );

Check failure on line 28 in source/wp-content/themes/wporg-showcase-2022/src/site-screenshot/view.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
return state.base64Image || state.hasError;
},
},
actions: {
increaseAttempts() {
Expand Down Expand Up @@ -68,6 +75,7 @@ const { actions, state } = store( 'wporg/showcase/screenshot', {
}
},
},

callbacks: {
// Run on init, starts the image fetch process.
*init() {
Expand All @@ -90,29 +98,5 @@ const { actions, state } = store( 'wporg/showcase/screenshot', {
}
}
},

update() {
const context = getContext();
const { ref } = getElement();
const { alt, hasError, isMShots } = context;

if ( ! isMShots ) {
return;
}

if ( hasError ) {
const spinner = ref.querySelector( 'div' );
spinner.classList.remove( 'wporg-site-screenshot__loader' );
spinner.classList.add( 'wporg-site-screenshot__error' );
spinner.innerText = alt;
ref.parentElement.classList.remove( 'has-loaded' );
} else if ( state.base64Image ) {
const img = document.createElement( 'img' );
img.src = state.base64Image;
img.alt = alt;
ref.replaceChildren( img );
ref.parentElement.classList.add( 'has-loaded' );
}
},
},
} );

0 comments on commit ef9a51b

Please sign in to comment.