Skip to content

Commit f1037f6

Browse files
committed
added component source for showing core uis
1 parent 2f51d67 commit f1037f6

File tree

7 files changed

+95
-11
lines changed

7 files changed

+95
-11
lines changed

components/ComponentShowcase.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { TabGroup, TabList, TabPanels, TabPanel, Tab } from "@headlessui/react";
44
import React, { HTMLAttributes } from "react";
55

66
interface IComponentShowcase extends HTMLAttributes<HTMLDivElement> {
7-
name: keyof typeof componentConfig.registry;
7+
name: keyof typeof componentConfig.examples;
88
}
99

1010
export function ComponentShowcase({ name, children }: IComponentShowcase) {
11-
const { preview: Preview } = componentConfig.registry[name];
11+
const { preview: Preview } = componentConfig.examples[name];
1212
const Code = React.Children.toArray(children)[0];
1313

1414
return (

components/ComponentSource.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { cn } from "@/lib/utils";
2+
3+
interface ComponentSourceProps extends React.HTMLAttributes<HTMLDivElement> {
4+
src: string;
5+
}
6+
7+
export function ComponentSource({ children, className }: ComponentSourceProps) {
8+
return (
9+
<div className={cn("overflow-hidden rounded-md", className)}>
10+
{children}
11+
</div>
12+
);
13+
}

components/MDX.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useMDXComponent } from "next-contentlayer/hooks";
33
import React, { HTMLAttributes } from "react";
44
import { ComponentShowcase } from "./ComponentShowcase";
55
import { cn } from "@/lib/utils";
6+
import { ComponentSource } from "./ComponentSource";
67

78
const components = {
89
h1: H1,
@@ -48,6 +49,7 @@ const components = {
4849
</code>
4950
),
5051
ComponentShowcase,
52+
ComponentSource,
5153
};
5254

5355
export default function MDX({ code }: { code: string }) {

config/components.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { lazy } from "react";
22

33
export const componentConfig = {
4-
registry: {
4+
core: {
5+
avatar: {
6+
name: "avatar",
7+
filePath: "packages/ui/Avatars/Avatar.tsx",
8+
},
9+
},
10+
examples: {
511
"accordion-style-default": {
612
name: "accordion-style-default",
713
filePath: "preview/components/accordion-style-default.tsx",

content/docs/components/avatar.mdx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,21 @@ lastUpdated: 12 Oct, 2024
1212

1313
#### 1. Install depenencies:
1414

15-
`npm install @radix-ui/react-avatar`
15+
```sh
16+
npm install @radix-ui/react-avatar
17+
```
1618

1719
<br />
1820

19-
#### 2. Add the Component:
21+
#### 2. Copy the code 👇 into your project:
22+
23+
<ComponentSource name="avatar" />
24+
25+
<br />
26+
<br />
27+
28+
## Examples
29+
30+
### Circle
31+
32+
<ComponentShowcase name="avatar-style-circle" />

contentlayer.config.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,51 @@ export default makeSource({
3131
rehypePlugins: [
3232
() => (tree) => {
3333
visit(tree, (node: UnistNode) => {
34+
if (node.name === "ComponentSource" && node.attributes) {
35+
const name = getNodeAttributeByName(node, "name")
36+
?.value as keyof typeof componentConfig.core;
37+
38+
if (!name) {
39+
return null;
40+
}
41+
42+
const component = componentConfig.core[name];
43+
const filePath = path.join(process.cwd(), component.filePath);
44+
const source = fs.readFileSync(filePath, "utf8");
45+
46+
node.children?.push(
47+
u("element", {
48+
tagName: "pre",
49+
properties: {
50+
__src__: source,
51+
},
52+
children: [
53+
u("element", {
54+
tagName: "code",
55+
properties: {
56+
className: ["language-tsx"],
57+
},
58+
children: [
59+
{
60+
type: "text",
61+
value: source,
62+
},
63+
],
64+
}),
65+
],
66+
})
67+
);
68+
}
69+
3470
if (node.name === "ComponentShowcase" && node.attributes) {
3571
const name = getNodeAttributeByName(node, "name")
36-
?.value as keyof typeof componentConfig.registry;
72+
?.value as keyof typeof componentConfig.examples;
3773

3874
if (!name) {
3975
return null;
4076
}
4177

42-
const component = componentConfig.registry[name];
78+
const component = componentConfig.examples[name];
4379
const filePath = path.join(process.cwd(), component.filePath);
4480
const source = fs.readFileSync(filePath, "utf8");
4581
// const cleanedJSX = source

preview/components/avatar-style-circle.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,23 @@ import { Avatar } from "@/packages/ui";
22

33
export default function AvatarStyleCircle() {
44
return (
5-
<Avatar>
6-
<Avatar.Image src="/images/avatar.jpeg" alt="Arif Logs" />
7-
<Avatar.Fallback>AH</Avatar.Fallback>
8-
</Avatar>
5+
<div className="flex items-center space-x-4">
6+
<Avatar className="h-10 w-10">
7+
<Avatar.Image src="/images/avatar.jpeg" alt="Arif Logs" />
8+
<Avatar.Fallback>AH</Avatar.Fallback>
9+
</Avatar>
10+
<Avatar>
11+
<Avatar.Image src="/images/avatar.jpeg" alt="Arif Logs" />
12+
<Avatar.Fallback>AH</Avatar.Fallback>
13+
</Avatar>
14+
<Avatar className="h-16 w-16">
15+
<Avatar.Image src="/images/avatar.jpeg" alt="Arif Logs" />
16+
<Avatar.Fallback>AH</Avatar.Fallback>
17+
</Avatar>
18+
<Avatar className="h-20 w-20">
19+
<Avatar.Image src="/images/avatar.jpeg" alt="Arif Logs" />
20+
<Avatar.Fallback>AH</Avatar.Fallback>
21+
</Avatar>
22+
</div>
923
);
1024
}

0 commit comments

Comments
 (0)