From 554644b240280cfaac16112fc761eab432a1d832 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 11 Sep 2025 15:19:34 +0200 Subject: [PATCH] docs: add copy buttons (#3393) --- www/assets/styles.css | 14 ++++++++ www/client.ts | 26 ++++++++++++++ www/components/CopyButton.tsx | 21 +++++++++++ www/components/homepage/Hero.tsx | 14 +++++++- www/islands/CopyArea.tsx | 62 -------------------------------- www/static/markdown.css | 9 +++-- www/utils/markdown.ts | 52 ++++++++++++++++++++++++--- 7 files changed, 128 insertions(+), 70 deletions(-) create mode 100644 www/components/CopyButton.tsx delete mode 100644 www/islands/CopyArea.tsx diff --git a/www/assets/styles.css b/www/assets/styles.css index 46a7d481..2a3663d2 100644 --- a/www/assets/styles.css +++ b/www/assets/styles.css @@ -93,3 +93,17 @@ html[data-theme="dark"] ::selection { background-size: 1.5em 1.5em; background-repeat: no-repeat; } + +.group:not([data-copied]) .group-not-copied { + display: block; +} +.group[data-copied] .group-not-copied { + display: none; +} + +.group[data-copied] .group-copied { + display: block; +} +.group:not([data-copied]) .group-copied { + display: none; +} diff --git a/www/client.ts b/www/client.ts index 1a7114eb..6280baa0 100644 --- a/www/client.ts +++ b/www/client.ts @@ -1 +1,27 @@ import "./assets/styles.css"; + +document.addEventListener("click", async (ev) => { + let el = ev.target as HTMLElement | null; + if (el === null) return; + if (!(el instanceof HTMLButtonElement)) { + el = el.closest("button"); + } + if (el === null) return; + + const code = el.dataset.code; + if (!code) return; + + el.dataset.copied = "true"; + + setTimeout(() => { + delete el.dataset.copied; + }, 1000); + + try { + await navigator.clipboard.writeText(code); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + // deno-lint-ignore no-console + console.error(message || "Copy failed"); + } +}); diff --git a/www/components/CopyButton.tsx b/www/components/CopyButton.tsx new file mode 100644 index 00000000..cc83828f --- /dev/null +++ b/www/components/CopyButton.tsx @@ -0,0 +1,21 @@ +import * as Icons from "./Icons.tsx"; + +export function CopyButton(props: { code: string }) { + return ( +
+ +
+ ); +} diff --git a/www/components/homepage/Hero.tsx b/www/components/homepage/Hero.tsx index 80825c0c..73e8999f 100644 --- a/www/components/homepage/Hero.tsx +++ b/www/components/homepage/Hero.tsx @@ -1,7 +1,7 @@ -import CopyArea from "../../islands/CopyArea.tsx"; import { FancyLink } from "../../components/FancyLink.tsx"; import LemonTop from "../../islands/LemonTop.tsx"; import LemonBottom from "../../islands/LemonBottom.tsx"; +import { CopyButton } from "../CopyButton.tsx"; export function Hero() { return ( @@ -26,3 +26,15 @@ export function Hero() { ); } + +function CopyArea(props: { code: string }) { + return ( +
+
+        {props.code}
+      
+ + +
+ ); +} diff --git a/www/islands/CopyArea.tsx b/www/islands/CopyArea.tsx deleted file mode 100644 index 731838f7..00000000 --- a/www/islands/CopyArea.tsx +++ /dev/null @@ -1,62 +0,0 @@ -import { useEffect } from "preact/hooks"; -import { IS_BROWSER } from "fresh/runtime"; -import * as Icons from "../components/Icons.tsx"; -import { useSignal } from "@preact/signals"; - -export default function CopyArea(props: { code: string }) { - const copied = useSignal(false); - - async function handleClick() { - if (props.code === undefined || props.code === null) { - return; - } - try { - await navigator.clipboard.writeText(props.code.toString()); - copied.value = true; - } catch (error) { - copied.value = false; - const message = error instanceof Error ? error.message : String(error); - // deno-lint-ignore no-console - console.error(message || "Copy failed"); - } - } - - useEffect(() => { - if (!copied.value) { - return; - } - const timer = setTimeout(() => { - copied.value = false; - }, 1000); - return () => clearTimeout(timer); - }, [copied.value]); - - return ( -
-
-        {props.code}
-      
- -
-
- Copied! -
- -
-
- ); -} diff --git a/www/static/markdown.css b/www/static/markdown.css index 3c2e22d0..e7a25a06 100644 --- a/www/static/markdown.css +++ b/www/static/markdown.css @@ -801,7 +801,8 @@ ol.nested li:before { .fenced-code-header { border-top-left-radius: 0.5rem; border-top-right-radius: 0.5rem; - padding: 0.5rem; + padding-left: 0.5rem; + padding-right: 0.5rem; background: var(--color-background-tertiary); display: flex; align-items: center; @@ -812,13 +813,15 @@ ol.nested li:before { border-top-right-radius: 0 !important; } .fenced-code-title { - display: grid; + display: flex; + padding-top: 0.5rem; + padding-bottom: 0.5rem; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 0.8125rem; line-height: 1; - grid-auto-flow: column; + justify-content: space-between; align-items: center; gap: 0.625rem; } diff --git a/www/utils/markdown.ts b/www/utils/markdown.ts index 47ba0f9d..0642254d 100644 --- a/www/utils/markdown.ts +++ b/www/utils/markdown.ts @@ -176,14 +176,58 @@ class DefaultRenderer extends Marked.Renderer { if (title || icon) { const image = icon - ? `${icon.text}` + ? `${icon.text}` : ""; out += `
- - ${image} - ${title ? escapeHtml(String(title)) : " "} + + + ${image} + ${title ? escapeHtml(String(title)) : " "} + + ${ + icon && icon.text !== "Text" + ? `` + : "" + }
`; }