Skip to content

Commit

Permalink
Add hovercard for private/public saved sceanrios
Browse files Browse the repository at this point in the history
  • Loading branch information
noracato committed Oct 23, 2024
1 parent 4931050 commit b5b9dd4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 15 deletions.
20 changes: 20 additions & 0 deletions app/components/hovercard/component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

<div
data-controller="hovercard"
data-hovercard-url-value="<%= @path %>"
data-action="mouseenter->hovercard#show mouseleave->hovercard#hide"
>
<%= content %>

<%# static card %>
<%# should make text optional and setup the @path with turbo see: https://boringrails.com/articles/hovercards-stimulus/ %>
<div class="relative hidden" data-hovercard-target="card">
<div data-tooltip-arrow class="absolute top-0 left-0 z-50 shadow-lg rounded-lg p-3 min-w-max-content bg-midnight-300 text-midnight-800">
<div class="flex space-x-3 items-center w-64 text-sm">
<%= @text %>
</div>
</div>
</div>
</div>


8 changes: 8 additions & 0 deletions app/components/hovercard/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Hovercard
class Component < ApplicationComponent
option :path
option :text, default: proc { "" }
end
end
7 changes: 0 additions & 7 deletions app/javascript/controllers/hello_controller.js

This file was deleted.

35 changes: 35 additions & 0 deletions app/javascript/controllers/hovercard_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = ["card"];
static values = { url: String };

show() {
if (this.hasCardTarget) {
this.cardTarget.classList.remove("hidden");
} else {
fetch(this.urlValue)
.then((r) => r.text())
.then((html) => {
const fragment = document
.createRange()
.createContextualFragment(html);

this.element.appendChild(fragment);
});
}
}

hide() {
if (this.hasCardTarget) {
this.cardTarget.classList.add("hidden");
}
}

disconnect() {
if (this.hasCardTarget) {
this.cardTarget.remove();
}
}
}

18 changes: 10 additions & 8 deletions app/views/saved_scenarios/_block_right_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

<div class="border-t border-solid border-gray-200 my-10"></div>

<%= render(PublishSavedScenario::Component.new(
path_on: publish_saved_scenario_path(saved_scenario),
path_off: unpublish_saved_scenario_path(saved_scenario),
status: saved_scenario.private,
title: t("scenario_bar.private.#{saved_scenario.private}"),
icon_on:'eye-slash',
icon_off: 'eye',
))%>
<%= render(Hovercard::Component.new(path: '', text: t("scenario_bar.private.description.#{saved_scenario.private}"))) do %>
<%= render(PublishSavedScenario::Component.new(
path_on: publish_saved_scenario_path(saved_scenario),
path_off: unpublish_saved_scenario_path(saved_scenario),
status: saved_scenario.private,
title: t("scenario_bar.private.#{saved_scenario.private}"),
icon_on:'eye-slash',
icon_off: 'eye',
))%>
<% end %>
<%= render(SavedScenarioNavItem::Component.new(
path: saved_scenario_path(saved_scenario),
title: t("scenario_bar.featured.#{saved_scenario.featured?}"),
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ en:
private:
true: Private
false: Public
description:
true: Only you can view or copy this scenario
false: |
Anyone can view or copy this scenario, but only you can make changes
featured:
true: Featured
false: Make this featured

0 comments on commit b5b9dd4

Please sign in to comment.