All files / owid-grapher/site Footnote.tsx

17.39% Statements 8/46
100% Branches 0/0
0% Functions 0/1
17.39% Lines 8/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 811x 1x 1x 1x 1x 1x                                                                             1x 1x                                                                      
import { Tippy } from "../grapher/chart/Tippy"
import { parseIntOrUndefined } from "../clientUtils/Util"
import React from "react"
import ReactDOM from "react-dom"
 
export const Footnote = ({
    index,
    htmlContent,
    triggerTarget,
}: {
    index: number
    htmlContent?: string
    triggerTarget?: Element
}) => {
    const onEvent = (instance: any, event: Event) => {
        if (event.type === "click") event.preventDefault()
    }

    return (
        <Tippy
            appendTo={() => document.body}
            content={
                htmlContent && (
                    <div>
                        <div
                            dangerouslySetInnerHTML={{
                                __html: htmlContent,
                            }}
                        />
                    </div>
                )
            }
            interactive
            placement="auto"
            theme="owid-footnote"
            trigger="mouseenter focus click"
            triggerTarget={triggerTarget}
            onTrigger={onEvent}
            onUntrigger={onEvent}
        >
            <sup>{index}</sup>
        </Tippy>
    )
}
 
interface FootnoteContent {
    index: number
    href: string
    htmlContent: string
}
 
function getFootnoteContent(element: Element): FootnoteContent | null {
    const href = element.closest("a.ref")?.getAttribute("href")
    if (!href) return null
 
    const index = parseIntOrUndefined(href.split("-")[1])
    if (index === undefined) return null
 
    const referencedEl = document.querySelector(href)
    if (!referencedEl?.innerHTML) return null
    return { index, href, htmlContent: referencedEl.innerHTML }
}
 
export function runFootnotes() {
    const footnotes = document.querySelectorAll("a.ref")
 
    footnotes.forEach((f) => {
        const footnoteContent = getFootnoteContent(f)
        if (footnoteContent == null) return
 
        ReactDOM.hydrate(
            <Footnote
                index={footnoteContent.index}
                htmlContent={footnoteContent.htmlContent}
                triggerTarget={f}
            />,
            f
        )
    })
}