From ed2c47aab2490692508dfe62c528fa79fb860da5 Mon Sep 17 00:00:00 2001 From: Robin Schanbacher Date: Wed, 5 Aug 2026 12:21:40 +0200 Subject: [PATCH] feat: copy button --- app/globals.css | 27 +++++++-------------- app/wiki/[slug]/page.tsx | 4 ++-- components/CopyButton.tsx | 45 +++++++++++++++++++++++++++++++++++ components/mdx-components.tsx | 40 +++++++++++++++++++++++++++---- 4 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 components/CopyButton.tsx diff --git a/app/globals.css b/app/globals.css index c529701..018908e 100644 --- a/app/globals.css +++ b/app/globals.css @@ -33,18 +33,6 @@ h1 { font-size: 2rem; } -.steps-container { - counter-reset: step; -} - -.steps-container .relative { - counter-increment: step; -} - -.steps-container .relative div:first-child::before { - content: counter(step); -} - /* Shiki Code-Blöcke stylen */ pre { padding: 1rem; @@ -94,6 +82,7 @@ figure[data-rehype-pretty-code-figure] code { font-family: var(--font-mono, monospace); } +/* Container für den Code-Block */ /* Container für den Code-Block */ figure[data-rehype-pretty-code-figure] { position: relative; @@ -101,22 +90,22 @@ figure[data-rehype-pretty-code-figure] { margin-bottom: 1.5rem; border-radius: 0.75rem; overflow: hidden; - border: 1px solid #27272a; /* Tailwind zinc-800 */ + border: 1px solid #27272a; background-color: #0d1117; } -/* Eventuelle leere Pseudo-Elemente der Figure deaktivieren */ +/* Leeres Pseudo-Element der Figure deaktivieren */ figure[data-rehype-pretty-code-figure]::before { display: none !important; } -/* Das Sprach-Badge ganz nach links rücken */ +/* Sprach-Badge links oben */ figure[data-rehype-pretty-code-figure] pre[data-language]::before { content: attr(data-language); display: block !important; position: absolute; top: 0.6rem; - left: 0.6rem; /* <-- Auf 0.5rem (oder 0.4rem) verringert für noch weiter links */ + left: 0.5rem; font-family: var(--font-mono, ui-monospace, monospace); font-size: 0.68rem; font-weight: 600; @@ -128,14 +117,14 @@ figure[data-rehype-pretty-code-figure] pre[data-language]::before { padding: 0.15rem 0.5rem; border-radius: 0.375rem; pointer-events: none; - z-index: 20; + z-index: 10; } -/* Platz oben für das Badge schaffen */ +/* Platz oben im Code-Block */ figure[data-rehype-pretty-code-figure] pre { margin: 0 !important; padding: 1rem !important; - padding-top: 2.4rem !important; /* Genügend Abstand nach oben */ + padding-top: 2.6rem !important; /* Platz für Badge & Button */ background-color: transparent !important; overflow-x: auto; font-size: 0.875rem; diff --git a/app/wiki/[slug]/page.tsx b/app/wiki/[slug]/page.tsx index 6bad611..619bada 100644 --- a/app/wiki/[slug]/page.tsx +++ b/app/wiki/[slug]/page.tsx @@ -2,7 +2,7 @@ import { getWikiPageBySlug } from '@/lib/wiki'; import { notFound } from 'next/navigation'; import { MDXRemote } from 'next-mdx-remote/rsc'; import rehypePrettyCode from 'rehype-pretty-code'; -import { Callout, Grid, Card, Steps, Step, InlineCode } from '@/components/mdx-components'; +import { Callout, Grid, Card, Steps, Step, InlineCode, CodeBlockContainer } from '@/components/mdx-components'; const mdxOptions = { mdxOptions: { @@ -25,9 +25,9 @@ const mdxComponents = { Card, Steps, Step, - // Custom Typography Überschreibungen code: InlineCode, + pre: CodeBlockContainer, h1: (props: any) =>

, h2: (props: any) =>

, h3: (props: any) =>

, diff --git a/components/CopyButton.tsx b/components/CopyButton.tsx new file mode 100644 index 0000000..6f88bd8 --- /dev/null +++ b/components/CopyButton.tsx @@ -0,0 +1,45 @@ +'use client'; + +import { useState } from 'react'; + +export function CopyButton({ code }: { code?: string }) { + const [copied, setCopied] = useState(false); + + const handleCopy = async (e: React.MouseEvent) => { + e.preventDefault(); + + // Falls kein expliziter Code übergeben wird, suchen wir den Text im Elternelement (Codeblock) + let textToCopy = code; + if (!textToCopy) { + const parent = e.currentTarget.closest('figure'); + const codeElement = parent?.querySelector('code'); + textToCopy = codeElement?.textContent || ''; + } + + if (textToCopy) { + await navigator.clipboard.writeText(textToCopy); + setCopied(true); + setTimeout(() => setCopied(false), 2000); + } + }; + + return ( + + ); +} \ No newline at end of file diff --git a/components/mdx-components.tsx b/components/mdx-components.tsx index 653b4ad..654d488 100644 --- a/components/mdx-components.tsx +++ b/components/mdx-components.tsx @@ -1,4 +1,5 @@ import React from 'react'; +import { CopyButton } from './CopyButton'; // 1. Callouts (Info, Warning, Error, Success) export function Callout({ @@ -48,17 +49,39 @@ export function Card({ title, href, children }: { title: string; href: string; c // 3. Steps export function Steps({ children }: { children: React.ReactNode }) { + // Wir gehen durch alle Kinder und fügen automatisch die 'stepNumber' als Prop hinzu + const steps = React.Children.map(children, (child, index) => { + if (React.isValidElement(child)) { + return React.cloneElement(child as React.ReactElement, { + stepNumber: index + 1, + }); + } + return child; + }); + return ( -
- {children} +
+ {steps}
); } -export function Step({ title, children }: { title: string; children: React.ReactNode }) { +export function Step({ + title, + children, + stepNumber +}: { + title: string; + children: React.ReactNode; + stepNumber?: number; +}) { return (
-
+ {/* Die Nummer wird direkt als Text aus React gerendert */} +
+ {stepNumber} +
+

{title}

{children}
@@ -84,4 +107,13 @@ export function InlineCode({ children, className, ...props }: React.HTMLAttribut {children} ); +} + +export function CodeBlockContainer({ children, ...props }: React.HTMLAttributes) { + return ( +
+ +
{children}
+
+ ); } \ No newline at end of file