Skip to content

Commit ba94832

Browse files
KianNHelithrar
authored andcommitted
[Docs Site] Use Tippy.js for footnotes (#17357)
* [Docs Site] Use Tippy.js for footnotes * tabindex/hover cutoff fixes * update style guide link to lib * new fnref styling
1 parent d621320 commit ba94832

File tree

11 files changed

+103
-99
lines changed

11 files changed

+103
-99
lines changed

astro.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export default defineConfig({
169169
"./src/mermaid.css",
170170
"./src/table.css",
171171
"./src/tailwind.css",
172+
"./src/tooltips.css",
172173
],
173174
pagination: false,
174175
plugins: runLinkCheck

package-lock.json

Lines changed: 0 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"he": "^1.2.0",
5151
"instantsearch.css": "^8.5.0",
5252
"instantsearch.js": "^4.74.0",
53-
"littlefoot": "^4.1.1",
5453
"lz-string": "^1.5.0",
5554
"marked": "^14.1.1",
5655
"mermaid": "^11.3.0",

src/components/GlossaryTooltip.astro

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
import { z } from "astro:schema";
33
import { getGlossaryEntry } from "~/util/glossary";
4-
import "tippy.js/dist/tippy.css";
54
import { marked } from "marked";
65
76
type Props = z.infer<typeof props>;
@@ -31,31 +30,19 @@ definition = definition.split(/\r?\n/)[0];
3130
data-tooltip
3231
data-content={marked.parse(definition)}
3332
class="border-b-2 border-dashed border-accent-600"
33+
tabindex="0"
3434
>{
3535
link ? (
3636
<a href={link}><slot /></a>
3737
) : (
3838
<slot />
3939
)
4040
}</span><script>
41-
import tippy from "tippy.js";
41+
import { addTooltip } from "~/util/tippy";
4242

4343
const tooltips = document.querySelectorAll<HTMLSpanElement>("[data-tooltip]");
4444

4545
for (const tooltip of tooltips) {
46-
tippy(`#${CSS.escape(tooltip.id)}`, {
47-
content: tooltip.dataset.content,
48-
allowHTML: true,
49-
interactive: true,
50-
placement: "auto",
51-
arrow: false,
52-
});
46+
addTooltip(tooltip, tooltip.dataset.content as string);
5347
}
54-
</script><style is:global>
55-
.tippy-box {
56-
background-color: var(--sl-color-bg-nav);
57-
border-color: var(--sl-color-text);
58-
border: 0.1em solid;
59-
color: var(--sl-color-white);
60-
}
61-
</style>
48+
</script>

src/components/overrides/Head.astro

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import type { Props } from "@astrojs/starlight/props";
33
import Default from "@astrojs/starlight/components/Head.astro";
44
import { differenceInCalendarDays } from "date-fns";
5-
import "littlefoot/dist/littlefoot.css";
5+
import "tippy.js/dist/tippy.css";
66
77
import { getEntry } from "astro:content";
88
import { getPageDescription } from "~/util/description";
@@ -164,51 +164,7 @@ if (Astro.props.entry.data.external_link) {
164164
}
165165
---
166166

167-
<script>
168-
import littlefoot from "littlefoot";
169-
170-
littlefoot();
171-
document
172-
.querySelectorAll("span.littlefoot")
173-
.forEach((x) => x.classList.add("not-content"));
174-
</script>
175-
<script>
176-
import mermaid from "mermaid";
177-
178-
const diagrams = document.querySelectorAll<HTMLPreElement>("pre.mermaid");
179-
180-
let init = false;
181-
182-
async function render() {
183-
const theme =
184-
document.documentElement.getAttribute("data-theme") === "light"
185-
? "neutral"
186-
: "dark";
187-
188-
for (const diagram of diagrams) {
189-
if (!init) {
190-
diagram.setAttribute("data-diagram", diagram.textContent as string);
191-
}
192-
193-
const def = diagram.getAttribute("data-diagram") as string;
194-
195-
mermaid.initialize({ startOnLoad: false, theme });
196-
await mermaid
197-
.render(`mermaid-${crypto.randomUUID()}`, def)
198-
.then(({ svg }) => (diagram.innerHTML = svg));
199-
200-
diagram.setAttribute("data-processed", "true");
201-
}
202-
203-
init = true;
204-
}
205-
206-
const obs = new MutationObserver(() => render());
207-
208-
obs.observe(document.documentElement, {
209-
attributes: true,
210-
attributeFilter: ["data-theme"],
211-
});
212-
</script>
167+
<script src="src/scripts/footnotes.ts"></script>
168+
<script src="src/scripts/mermaid.ts"></script>
213169
<script src="src/scripts/analytics.ts"></script>
214170
<Default {...Astro.props}><slot /></Default>

src/components/overrides/PageSidebar.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ if (Astro.props.toc) {
4747
];
4848
4949
for (const header of headers) {
50+
if (header.id === "footnote-label") continue;
51+
5052
const depth = headerDepth(header);
5153
5254
const title = he.decode(header.innerText);

src/content/docs/style-guide/components/footnotes.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ To create footnotes, use the following syntax:
77
```mdx live
88
You can use GitHub Flavoured Markdown footnotes![^1]
99

10-
[^1]: Powered by [littlefoot.js](https://littlefoot.js.org)
10+
[^1]: Powered by [tippy.js](https://atomiks.github.io/tippyjs/)
1111
```
1212

1313
Refer to [Footnotes](/style-guide/formatting/footnotes/) to learn when to use them.

src/scripts/footnotes.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { addTooltip } from "~/util/tippy";
2+
3+
const footnotes = document.querySelector("section.footnotes");
4+
5+
if (footnotes) {
6+
const notes = footnotes.querySelectorAll("li");
7+
8+
for (const note of notes) {
9+
const content = note.querySelector("p") as HTMLParagraphElement;
10+
11+
const fnrefs = document.querySelectorAll<HTMLAnchorElement>(
12+
`#${note.id.replace("fn", "fnref")}`,
13+
);
14+
15+
for (const fnref of fnrefs) {
16+
addTooltip(fnref, content.innerHTML);
17+
18+
fnref.classList.add("footnote");
19+
20+
fnref.setAttribute("tabindex", "0");
21+
fnref.removeAttribute("href");
22+
}
23+
}
24+
25+
footnotes.remove();
26+
}

src/scripts/mermaid.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import mermaid from "mermaid";
2+
3+
const diagrams = document.querySelectorAll<HTMLPreElement>("pre.mermaid");
4+
5+
let init = false;
6+
7+
async function render() {
8+
const theme =
9+
document.documentElement.getAttribute("data-theme") === "light"
10+
? "neutral"
11+
: "dark";
12+
13+
for (const diagram of diagrams) {
14+
if (!init) {
15+
diagram.setAttribute("data-diagram", diagram.textContent as string);
16+
}
17+
18+
const def = diagram.getAttribute("data-diagram") as string;
19+
20+
mermaid.initialize({ startOnLoad: false, theme });
21+
await mermaid
22+
.render(`mermaid-${crypto.randomUUID()}`, def)
23+
.then(({ svg }) => (diagram.innerHTML = svg));
24+
25+
diagram.setAttribute("data-processed", "true");
26+
}
27+
28+
init = true;
29+
}
30+
31+
const obs = new MutationObserver(() => render());
32+
33+
obs.observe(document.documentElement, {
34+
attributes: true,
35+
attributeFilter: ["data-theme"],
36+
});

src/tooltips.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@tailwind utilities;
2+
3+
.footnote {
4+
@apply text-xs !text-[--sl-color-accent] font-semibold p-1 -m-1;
5+
}
6+
7+
.tippy-box {
8+
background-color: var(--sl-color-bg-nav);
9+
border-color: var(--sl-color-text);
10+
border: 0.1em solid;
11+
color: var(--sl-color-white);
12+
}

0 commit comments

Comments
 (0)