Skip to content

Instantly share code, notes, and snippets.

@jmarsh24
Created March 1, 2022 19:19
Show Gist options
  • Save jmarsh24/0d13f68af489cb5baf00c19d3e1b4f76 to your computer and use it in GitHub Desktop.
Save jmarsh24/0d13f68af489cb5baf00c19d3e1b4f76 to your computer and use it in GitHub Desktop.
<%= turbo_frame_tag "#{dom_id(@video)}_vote" do %>
<div class="like-buttons" style="display: flex; align-items: center;">
<%= render partial: "videos/show/upvote_link" %>
<%= render partial: "videos/show/like_count" %>
<%= render partial: "videos/show/downvote_link" %>
</div>
<% end %>
import { Controller } from "@hotwired/stimulus";
import { createPopper } from "@popperjs/core";
export default class extends Controller {
static targets = ["element", "tooltip"];
static values = {
placement: String,
offset: Array,
};
initialize() {
this.placementValue = "top";
this.offsetValue = [0, 8];
}
connect() {
console.log("connected");
// Create a new Popper instance
this.popperInstance = createPopper(this.elementTarget, this.tooltipTarget, {
placement: this.placementValue,
modifiers: [
{
name: "offset",
options: {
offset: this.offsetValue,
},
},
],
});
}
show(event) {
console.log("fired");
event.preventDefault();
this.hideAllElements();
this.tooltipTarget.setAttribute("data-show", "");
// We need to tell Popper to update the tooltip position
// after we show the tooltip, otherwise it will be incorrect
this.popperInstance.update();
}
hide(event) {
this.tooltipTarget.removeAttribute("data-show");
}
hideAllElements() {
document
.querySelectorAll("#tooltip")
.forEach((element) => element.removeAttribute("data-show"));
}
// Destroy the Popper instance
disconnect() {
if (this.popperInstance) {
this.popperInstance.destroy();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment