|
1 | 1 | --- |
2 | 2 | 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 |
4 | 4 | --- |
5 | 5 |
|
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. |
7 | 8 |
|
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> |
0 commit comments