-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathcomponent-example.tsx
86 lines (80 loc) · 3.34 KB
/
component-example.tsx
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
81
82
83
84
85
86
"use client";
import * as React from "react";
import { CopyButton, CopyWithClassNames } from "@/components/copy-button";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { cn } from "@/lib/utils";
interface ComponentExampleProps extends React.HTMLAttributes<HTMLDivElement> {
extractClassname?: boolean;
extractedClassNames?: string;
align?: "center" | "start" | "end";
src?: string;
}
export function ComponentExample({
children,
className,
extractedClassNames,
align = "center",
...props
}: ComponentExampleProps) {
const [Example, Code, ...Children] = React.Children.toArray(children) as React.ReactElement[];
const codeString = React.useMemo(() => {
if (typeof Code?.props["data-rehype-pretty-code-fragment"] !== "undefined") {
const [, Button] = React.Children.toArray(Code.props.children) as React.ReactElement[];
return Button?.props?.value || Button?.props?.__rawString__ || null;
}
}, [Code]);
return (
<div className={cn("group relative my-4 flex flex-col space-y-2", className)} {...props}>
<Tabs defaultValue="preview" className="relative mr-auto w-full">
<div className="flex items-center justify-between pb-3">
<TabsList className="w-full justify-start rounded-none border-b bg-transparent p-0">
<TabsTrigger
value="preview"
className="relative rounded-none border-b-2 border-b-transparent bg-transparent px-4 pb-3 pt-2 font-semibold text-muted-foreground shadow-none transition-none data-[state=active]:border-b-primary data-[state=active]:text-foreground data-[state=active]:shadow-none"
>
Preview
</TabsTrigger>
<TabsTrigger
value="code"
className="relative rounded-none border-b-2 border-b-transparent bg-transparent px-4 pb-3 pt-2 font-semibold text-muted-foreground shadow-none transition-none data-[state=active]:border-b-primary data-[state=active]:text-foreground data-[state=active]:shadow-none"
>
Code
</TabsTrigger>
</TabsList>
{extractedClassNames ? (
<CopyWithClassNames
value={codeString}
classNames={extractedClassNames}
className="absolute right-4 top-20"
/>
) : (
codeString && <CopyButton value={codeString} className="absolute right-4 top-20" />
)}
</div>
<TabsContent value="preview" className="rounded-md border">
<div
className={cn("flex min-h-[350px] justify-center p-10", {
"items-center": align === "center",
"items-start": align === "start",
"items-end": align === "end",
})}
>
{Example}
</div>
</TabsContent>
<TabsContent value="code">
<div className="flex flex-col space-y-4">
<div className="w-full rounded-md [&_button]:hidden [&_pre]:my-0 [&_pre]:max-h-[350px] [&_pre]:overflow-auto">
{Code}
</div>
{Children?.length ? (
<div className="rounded-md [&_button]:hidden [&_pre]:my-0 [&_pre]:max-h-[350px] [&_pre]:overflow-auto">
{Children}
</div>
) : null}
</div>
</TabsContent>
</Tabs>
</div>
);
}