Skip to content

Commit

Permalink
Favorite button: Add a loading animation during API request
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle committed Jul 22, 2024
1 parent 74b3424 commit e60f04a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mu-plugins/blocks/favorite-button/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
'count' => $settings['count'],
'isFavorite' => $is_favorite,
'label' => $labels,
'isLoading' => false,
];
$encoded_state = wp_json_encode( $init_state );

Expand All @@ -63,6 +64,7 @@
data-wp-context="<?php echo esc_attr( $encoded_state ); ?>"
data-wp-class--is-favorite="context.isFavorite"
data-wp-class--is-loaded="context.id"
data-wp-class--is-loading="context.isLoading"
>
<?php if ( $user_id ) : ?>
<button
Expand Down
23 changes: 23 additions & 0 deletions mu-plugins/blocks/favorite-button/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,33 @@
display: none;
}

/* The interactivity API hasn't kicked in yet, so hide one of the hearts. */
&.is-loading {
svg {
animation-name: favoriteFade;
animation-duration: 750ms;
animation-iteration-count: infinite;
}
}

&.is-variant-small {
.wporg-favorite-button__button {
border: none;
padding: 2px 4px;
}
}
}

@keyframes favoriteFade {
0% {
opacity: 1;
}

50% {
opacity: 0.5;
}

100% {
opacity: 1;
}
}
4 changes: 4 additions & 0 deletions mu-plugins/blocks/favorite-button/src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ store( 'wporg/favorite-button', {
actions: {
*triggerAction() {
const context = getContext();
context.isLoading = true;

if ( context.isFavorite ) {
try {
const result = yield wp.apiFetch( {
Expand Down Expand Up @@ -48,6 +50,8 @@ store( 'wporg/favorite-button', {
wp.a11y.speak( context.label.favorited, 'polite' );
} catch ( error ) {}
}

context.isLoading = false;
},
},
} );

0 comments on commit e60f04a

Please sign in to comment.