REFACTOR: Use CSS animations for likes (#14809)

This commit is contained in:
Penar Musaraj 2021-11-04 09:13:34 -04:00 committed by GitHub
parent ccd259e2b5
commit 89a2cec7be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 35 deletions

View File

@ -1,39 +1,14 @@
import { applyDecorators, createWidget } from "discourse/widgets/widget"; import { applyDecorators, createWidget } from "discourse/widgets/widget";
import { next, run } from "@ember/runloop"; import { later, next } from "@ember/runloop";
import { Promise } from "rsvp"; import { Promise } from "rsvp";
import { formattedReminderTime } from "discourse/lib/bookmark"; import { formattedReminderTime } from "discourse/lib/bookmark";
import { h } from "virtual-dom"; import { h } from "virtual-dom";
import { isTesting } from "discourse-common/config/environment";
import showModal from "discourse/lib/show-modal"; import showModal from "discourse/lib/show-modal";
import { smallUserAtts } from "discourse/widgets/actions-summary"; import { smallUserAtts } from "discourse/widgets/actions-summary";
const LIKE_ACTION = 2; const LIKE_ACTION = 2;
const VIBRATE_DURATION = 5; const VIBRATE_DURATION = 5;
function animateHeart($elem, start, end, complete) {
if (isTesting()) {
return run(this, complete);
}
$elem
.stop()
.css("textIndent", start)
.animate(
{ textIndent: end },
{
complete,
step(now) {
$(this)
.css("transform", "scale(" + now + ")")
.addClass("d-liked")
.removeClass("d-unliked");
},
duration: 150,
},
"linear"
);
}
const _builders = {}; const _builders = {};
const _extraButtons = {}; const _extraButtons = {};
export let apiExtraButtons = {}; export let apiExtraButtons = {};
@ -151,6 +126,9 @@ registerButton("like", (attrs) => {
icon: attrs.liked ? "d-liked" : "d-unliked", icon: attrs.liked ? "d-liked" : "d-unliked",
className, className,
before: "like-count", before: "like-count",
data: {
"post-id": attrs.id,
},
}; };
// If the user has already liked the post and doesn't have permission // If the user has already liked the post and doesn't have permission
@ -695,16 +673,16 @@ export default createWidget("post-menu", {
return this.sendWidgetAction("toggleLike"); return this.sendWidgetAction("toggleLike");
} }
const $heart = $(`[data-post-id=${attrs.id}] .toggle-like .d-icon`); const heart = document.querySelector(
$heart.closest("button").addClass("has-like"); `.toggle-like[data-post-id="${attrs.id}"] .d-icon`
);
heart.closest(".toggle-like").classList.add("has-like");
heart.classList.add("has-animation");
const scale = [1.0, 1.5];
return new Promise((resolve) => { return new Promise((resolve) => {
animateHeart($heart, scale[0], scale[1], () => { later(() => {
animateHeart($heart, scale[1], scale[0], () => { this.sendWidgetAction("toggleLike").then(() => resolve());
this.sendWidgetAction("toggleLike").then(() => resolve()); }, 400);
});
});
}); });
}, },

View File

@ -241,7 +241,7 @@ discourseModule("Integration | Component | Widget | post", function (hooks) {
{{mount-widget widget="post-menu" args=args toggleLike=toggleLike}} {{mount-widget widget="post-menu" args=args toggleLike=toggleLike}}
`, `,
beforeEach() { beforeEach() {
const args = { showLike: true, canToggleLike: true }; const args = { showLike: true, canToggleLike: true, id: 5 };
this.set("args", args); this.set("args", args);
this.set("toggleLike", () => { this.set("toggleLike", () => {
args.liked = !args.liked; args.liked = !args.liked;

View File

@ -1,3 +1,15 @@
@keyframes heartBump {
0% {
transform: scale(1);
}
50% {
transform: scale(1.5);
}
100% {
transform: scale(1);
}
}
.wrap { .wrap {
max-width: $large-width; max-width: $large-width;
} }
@ -103,6 +115,9 @@ nav.post-controls {
// Like button after I've liked // Like button after I've liked
.d-icon { .d-icon {
color: var(--love); color: var(--love);
&.has-animation {
animation: heartBump 0.4s;
}
} }
&.d-hover { &.d-hover {
background: var(--primary-low); background: var(--primary-low);