Skip to content

A platform for community discussion. Free, open, simple.

DEV: Close footnote tooltip when trigger is removed (#34182)

Sérgio Saquetim168c4b75fd70
diff --git a/plugins/footnote/assets/javascripts/api-initializers/inline-footnotes.gjs b/plugins/footnote/assets/javascripts/api-initializers/inline-footnotes.gjs
index d7e6cb49668..6730ba07db0 100644
--- a/plugins/footnote/assets/javascripts/api-initializers/inline-footnotes.gjs
+++ b/plugins/footnote/assets/javascripts/api-initializers/inline-footnotes.gjs
@@ -1,34 +1,45 @@
+import Component from "@glimmer/component";
+import { array } from "@ember/helper";
+import { on } from "@ember/modifier";
+import { action } from "@ember/object";
import { htmlSafe } from "@ember/template";
import { apiInitializer } from "discourse/lib/api";
-import DTooltipInstance from "float-kit/lib/d-tooltip-instance";
+import DTooltip from "float-kit/components/d-tooltip";
-const TooltipContentComponent = <template>
- {{htmlSafe @data.contentHtml}}
-</template>;
-
-export default apiInitializer((api) => {
- function onFootnoteClick(event) {
+class InlineFootnote extends Component {
+ @action
+ preventDefault(event) {
event.preventDefault();
-
- const tooltipService = api.container.lookup("service:tooltip");
-
- const instance = new DTooltipInstance(api.container, {
- identifier: "inline-footnote",
- interactive: true,
- closeOnScroll: false,
- closeOnClickOutside: true,
- component: TooltipContentComponent,
- data: {
- contentHtml: event.target.dataset.footnoteContent,
- },
- });
- instance.trigger = event.target;
- instance.detachedTrigger = true;
-
- tooltipService.show(instance);
}
- api.decorateCookedElement((elem) => {
+ <template>
+ <DTooltip
+ @identifier="inline-footnote"
+ @interactive={{true}}
+ @closeOnScroll={{false}}
+ @closeOnClickOutside={{true}}
+ @triggers={{array "click"}}
+ >
+ <:trigger>
+ {{! template-lint-disable no-invalid-link-text }}
+ <a
+ class="expand-footnote"
+ href
+ role="button"
+ data-footnote-id={{@data.footnoteId}}
+ data-footnote-content={{@data.footnoteContent}}
+ {{on "click" this.preventDefault}}
+ ></a>
+ </:trigger>
+ <:content>
+ {{htmlSafe @data.footnoteContent}}
+ </:content>
+ </DTooltip>
+ </template>
+}
+
+export default apiInitializer((api) => {
+ api.decorateCookedElement((elem, helper) => {
if (
!api.container.lookup("service:site-settings").display_footnotes_inline
) {
@@ -46,15 +57,13 @@ export default apiInitializer((api) => {
const footnoteId = refLink.getAttribute("href");
const footnoteContent = elem.querySelector(footnoteId)?.innerHTML;
- const expandableFootnote = document.createElement("a");
- expandableFootnote.classList.add("expand-footnote");
- expandableFootnote.href = "";
- expandableFootnote.role = "button";
- expandableFootnote.dataset.footnoteId = footnoteId;
- expandableFootnote.dataset.footnoteContent = footnoteContent;
- expandableFootnote.addEventListener("click", onFootnoteClick);
+ const expandableFootnote = document.createElement("span");
+ footnoteRef.replaceWith(expandableFootnote);
- footnoteRef.after(expandableFootnote);
+ helper.renderGlimmer(expandableFootnote, InlineFootnote, {
+ footnoteId,
+ footnoteContent,
+ });
});
if (footnoteRefs.length) {
diff --git a/plugins/footnote/test/javascripts/acceptance/footnote-test.js b/plugins/footnote/test/javascripts/acceptance/footnote-test.js
index 8867b7083e6..70e5aac5788 100644
--- a/plugins/footnote/test/javascripts/acceptance/footnote-test.js
+++ b/plugins/footnote/test/javascripts/acceptance/footnote-test.js
@@ -18,11 +18,15 @@ acceptance("Discourse Footnote Plugin", function (needs) {
topic["post_stream"]["posts"][0]["cooked"] = `
<p>Lorem ipsum dolor sit amet<sup class="footnote-ref"><a href="#footnote-17-1" id="footnote-ref-17-1">[1]</a></sup></p>
<p class="second">Second reference should also work. <sup class="footnote-ref"><a href="#footnote-17-1" id="footnote-ref-17-0">[1]</a></sup></p>
+ <p class="other">Other page should close<sup class="footnote-ref"><a href="#footnote-17-2" id="footnote-ref-17-2">[2]</a></sup></p>
<hr class="footnotes-sep">
<ol class="footnotes-list">
<li id="footnote-17-1" class="footnote-item">
<p>consectetur adipiscing elit <a href="#footnote-ref-17-1" class="footnote-backref">↩︎</a></p>
</li>
+ <li id="footnote-17-2" class="footnote-item">
+ <p><a class="link-in-tooltip" href="/">Index ↩︎</a></p>
+ </li>
</ol>
`;
return helper.response(topic);
@@ -52,4 +56,15 @@ acceptance("Discourse Footnote Plugin", function (needs) {
await click(".second .expand-footnote");
assert.dom(TOOLTIP_SELECTOR).hasText("consectetur adipiscing elit ↩︎");
});
+
+ test("closes tooltip when clicking link within tooltip content", async function (assert) {
+ await visit("/t/-/45");
+
+ // open
+ await click(".other .expand-footnote");
+ assert.dom(TOOLTIP_SELECTOR).hasText("Index ↩︎");
+
+ await click(".link-in-tooltip");
+ assert.dom(TOOLTIP_SELECTOR).doesNotExist();
+ });
});