feat: copy button
This commit is contained in:
1 parent
da99471ca5
commit
ed2c47aab2
4 files changed
+91
-25
No files matched your search
+8
-19
@@ -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;
|
||||
|
||||
@@ -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) => <h1 className="text-3xl font-bold text-white mt-8 mb-4 border-b border-zinc-800 pb-3" {...props} />,
|
||||
h2: (props: any) => <h2 className="text-2xl font-bold text-white mt-10 mb-4" {...props} />,
|
||||
h3: (props: any) => <h3 className="text-xl font-semibold text-white mt-6 mb-3" {...props} />,
|
||||
|
||||
@@ -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<HTMLButtonElement>) => {
|
||||
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 (
|
||||
<button
|
||||
onClick={handleCopy}
|
||||
className="absolute top-2.5 right-3 p-1.5 rounded-md bg-zinc-900 border border-zinc-800 text-zinc-400 hover:text-white hover:bg-zinc-800 transition-colors z-20"
|
||||
aria-label="Code kopieren"
|
||||
>
|
||||
{copied ? (
|
||||
// Check Icon (Erfolg)
|
||||
<svg className="w-4 h-4 text-emerald-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
|
||||
</svg>
|
||||
) : (
|
||||
// Clipboard Icon
|
||||
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
|
||||
</svg>
|
||||
)}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
@@ -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<any>, {
|
||||
stepNumber: index + 1,
|
||||
});
|
||||
}
|
||||
return child;
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="steps-container ml-4 border-l border-zinc-800 pl-8 my-8 space-y-8">
|
||||
{children}
|
||||
<div className="ml-4 border-l border-zinc-800 pl-8 my-8 space-y-8">
|
||||
{steps}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function Step({ title, children }: { title: string; children: React.ReactNode }) {
|
||||
export function Step({
|
||||
title,
|
||||
children,
|
||||
stepNumber
|
||||
}: {
|
||||
title: string;
|
||||
children: React.ReactNode;
|
||||
stepNumber?: number;
|
||||
}) {
|
||||
return (
|
||||
<div className="relative">
|
||||
<div className="absolute -left-[41px] flex h-5 w-5 items-center justify-center rounded-full bg-zinc-900 border border-zinc-800 text-[10px] font-bold text-white" />
|
||||
{/* Die Nummer wird direkt als Text aus React gerendert */}
|
||||
<div className="absolute -left-[41px] flex h-5 w-5 items-center justify-center rounded-full bg-zinc-900 border border-zinc-800 text-[10px] font-bold text-white">
|
||||
{stepNumber}
|
||||
</div>
|
||||
|
||||
<h3 className="text-white font-bold mb-2 mt-0 text-base">{title}</h3>
|
||||
<div className="text-zinc-400 text-sm [&>p]:m-0">{children}</div>
|
||||
</div>
|
||||
@@ -85,3 +108,12 @@ export function InlineCode({ children, className, ...props }: React.HTMLAttribut
|
||||
</code>
|
||||
);
|
||||
}
|
||||
|
||||
export function CodeBlockContainer({ children, ...props }: React.HTMLAttributes<HTMLPreElement>) {
|
||||
return (
|
||||
<div className="relative group">
|
||||
<CopyButton />
|
||||
<pre {...props}>{children}</pre>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user