Skip to content

Commit c02db98

Browse files
Kapil GowruKapil Gowru
authored andcommitted
feat: moved content for eusable snippets
1 parent d676e49 commit c02db98

File tree

3 files changed

+116
-6
lines changed

3 files changed

+116
-6
lines changed
Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,78 @@
11
---
22
title: Custom React Components
3-
description: How to create and use custom React components.
3+
subtitle: Add your own React components to enhance your docs
44
---
55

6-
Learn how to integrate custom React components into your documentation.
6+
You can extend Fern's built-in component library by adding your own custom React components.
7+
This allows you to create unique, interactive elements that match your documentation needs.
78

8-
<Warning>This page is a WIP, please refer to our previous [documentation](https://buildwithfern.com/learn/docs/getting-started/overview).</Warning>
9+
<Note>Setting up custom react components is part of the pro plan.</Note>
10+
11+
## How does it work
12+
13+
<Steps>
14+
### Create a React component
15+
16+
Let's start by creating a `components` folder where you can define your react components. Note
17+
that the react components can be defined in `.ts`, `.tsx`, `.js` or `.mdx` files.
18+
19+
```ts components/CustomCard.tsx
20+
export const CustomCard = ({ title, text, link, sparkle = false }) => {
21+
return (
22+
<a href={link} className="block p-6 rounded-lg border border-gray-200 hover:shadow-lg transition-shadow">
23+
<h2 className="text-xl font-semibold mb-2">
24+
{title} {sparkle && "✨"}
25+
</h2>
26+
<p className="text-gray-600">{text}</p>
27+
</a>
28+
);
29+
};
30+
```
31+
32+
### Use the component in your docs
33+
34+
Once you've written the component, you can start leveraging it in your Markdown guides.
35+
36+
```jsx guide.mdx
37+
import { CustomCard } from "../components/CustomCard"
38+
39+
<CustomCard
40+
title="MyTitle"
41+
text="Hello"
42+
href="https://github.com/fern-api/fern/tree/main/generators/python"
43+
/>
44+
```
45+
46+
### Specify your components directory in `docs.yml`
47+
48+
Add your components directory to `docs.yml` so that the Fern CLI can scan your components directory
49+
and upload them to the server.
50+
51+
```yml docs.yml
52+
experimental:
53+
mdx-components:
54+
- ./components
55+
```
56+
</Steps>
57+
58+
## Why not just use custom CSS and JS instead?
59+
60+
While you can bundle React components as custom JavaScript, using Fern's built-in React component support provides several key advantages:
61+
62+
<AccordionGroup>
63+
<Accordion title="No layout shifts or flashes">
64+
When adding React components via custom JavaScript, you can't control when components are rendered relative to the rest of the page content. This often leads to glitchy behavior where components flash or
65+
jump as they load asynchronously after the main content.
66+
</Accordion>
67+
68+
<Accordion title="Faster page load">
69+
Custom JavaScript bundles typically include their own copy of the React library, which:
70+
- Increases page load time by duplicating React code that's already included
71+
- Reduces performance as multiple React instances run on the same page
72+
- Creates larger bundle sizes that users have to download
73+
</Accordion>
74+
75+
<Accordion title="Improved SEO">
76+
Components added via custom JavaScript aren't server-side rendered, which means search engines can't index content.
77+
</Accordion>
78+
</AccordionGroup>
44.4 KB
Loading
Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
11
---
22
title: Reusable Snippets
3-
description: Creating and managing reusable content snippets.
3+
description: Reusable, custom snippets to keep content in sync. Edit once, update everywhere.
44
---
55

6-
Learn how to create and manage reusable content snippets for your documentation.
6+
Keep your documentation DRY (Don't Repeat Yourself) by defining a reusable snippet once, and then referencing it in multiple places. This way, you only need to update the snippet in one place to keep all references in sync.
77

8-
<Warning>This page is a WIP, please refer to our previous [documentation](https://buildwithfern.com/learn/docs/getting-started/overview).</Warning>
8+
## Create a reusable snippet
9+
10+
To use reusable snippets, start by creating a new folder in your `fern` project called `snippets`. Inside the `snippets` folder, create a new file for each snippet you want to define.
11+
12+
For example:
13+
14+
```bash
15+
fern
16+
└─ pages
17+
└─ my-tutorial.mdx
18+
└─ assets
19+
└─ snippets
20+
├─ herbs.mdx
21+
├─ peace-lily.mdx
22+
└─ trees.mdx
23+
```
24+
25+
In each snippet file, define the content you want to reuse. For example, `peace-lily.mdx` might contain:
26+
27+
```mdx title="snippets/peace-lily.mdx"
28+
<Warning> Remember to water your plant at least twice a week. </Warning>
29+
```
30+
31+
## Use a reusable snippet
32+
33+
To use a snippet in your documentation, reference it by its file path (including the `.mdx` extension) in your content. For example, to include the `peace-lily` snippet in your content, use:
34+
35+
<Tabs>
36+
<Tab title="Markdown">
37+
```mdx
38+
Peace lilies are easy to grow and relatively trouble-free.
39+
40+
<Markdown src="/snippets/peace-lily.mdx" />
41+
```
42+
</Tab>
43+
<Tab title="Preview">
44+
Peace lilies are easy to grow and relatively trouble-free.
45+
46+
<Markdown src="/snippets/peace-lily.mdx" />
47+
</Tab>
48+
</Tabs>

0 commit comments

Comments
 (0)