Skip to content

Commit 09c3a6d

Browse files
devin-ai-integration[bot]fern-api[bot]devalog
authored
docs: add custom header and footer component documentation (#3478)
Co-authored-by: fern-api[bot] <115122769+fern-api[bot]@users.noreply.github.com> Co-authored-by: Devin Logan <devinannlogan@gmail.com>
1 parent 04d06b8 commit 09c3a6d

4 files changed

Lines changed: 62 additions & 82 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Server-side rendered custom header and footer
2+
3+
Replace Fern's default header or footer with your own React components using the new `header` and `footer` properties in `docs.yml`.
4+
5+
```yaml docs.yml
6+
header: ./components/CustomHeader.tsx
7+
footer: ./components/CustomFooter.tsx
8+
```
9+
10+
Components are server-side rendered for better SEO and performance, with no layout shifts during page load.
11+
12+
Learn more about [custom header and footer components](/learn/docs/writing-content/custom-react-components#custom-header-and-footer).

fern/products/docs/pages/component-library/custom-components/custom-css-js.mdx

Lines changed: 9 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ subtitle: Add custom CSS, global JavaScript, and UI components.
44
description: Learn how to add custom CSS, JavaScript, and UI components to your Fern documentation. Style your docs with custom classes and scripts.
55
---
66

7-
This page covers how to customize your docs with CSS, JavaScript, and custom components. However, you can [customize many things directly in your `docs.yml` file](/docs/configuration/site-level-settings), including colors, typography, navbar links, layout, analytics, and metadata. Try these built-in options first before adding custom code.
7+
This page covers CSS and JavaScript customization:
8+
9+
- **CSS** for styling, visual changes, and hiding elements
10+
- **JavaScript** for client-side behavior, third-party integrations, and widgets
11+
12+
For server-rendered components like custom headers, footers, or reusable elements in your MDX content, see [Custom React components](/learn/docs/writing-content/custom-react-components).
13+
14+
You can also [customize many things directly in your `docs.yml` file](/docs/configuration/site-level-settings), including colors, typography, navbar links, layout, analytics, and metadata. Try these built-in options first before adding custom code.
815

916
## Custom CSS
1017

@@ -230,79 +237,4 @@ js:
230237
- Implement custom search (also requires [your Algolia credentials](/docs/customization/search))
231238
- Insert scripts and widgets
232239

233-
## Custom components
234-
235-
<Markdown src="/snippets/pro-plan.mdx"/>
236-
237-
You can use custom CSS and JS to replace Fern's default UI components with your own. The `header` and `footer`
238-
are the most commonly replaced components. You can replace any component in the docs,
239-
including the sidebar, tabs, search bar, and more.
240-
241-
To implement your own components in Fern Docs, write JavaScript to render your
242-
custom components in the DOM. Build to CSS and JavaScript files that
243-
are stored in `fern/` and referenced in `docs.yml`:
244-
245-
<CodeBlocks>
246-
<CodeBlock title="fern/">
247-
```bash {5-7}
248-
fern/
249-
├─ openapi/
250-
├─ pages/
251-
├─ images/
252-
├─ dist/
253-
└─ output.css
254-
└─ output.js
255-
├─ docs.yml
256-
└─ fern.config.json
257-
```
258-
</CodeBlock>
259-
<CodeBlock title="docs.yml">
260-
```yaml
261-
css: ./dist/output.css
262-
js: ./dist/output.js
263-
```
264-
</CodeBlock>
265-
</CodeBlocks>
266-
267-
### Example custom components
268-
269-
See this [GitHub repo](https://github.com/fern-api/docs-custom-js-example)
270-
and its [generated docs page](https://custom-js-example.docs.buildwithfern.com/get-started/welcome)
271-
for an example of how to replace the Fern `header` and `footer` with custom React components.
272-
273-
#### Example custom header
274-
275-
<Frame>
276-
<img alt="Custom header" src="./custom-header.png" />
277-
</Frame>
278-
279-
```JavaScript
280-
ReactDOM.render(
281-
React.createElement(NavHeader),
282-
document.getElementById('fern-header'),
283-
)
284-
```
285-
286-
#### Example custom footer
287-
288-
<Frame>
289-
<img alt="Custom footer" src="./custom-footer.png" />
290-
</Frame>
291-
292-
```JavaScript
293-
ReactDOM.render(
294-
React.createElement(NavFooter),
295-
document.getElementById('fern-footer'),
296-
)
297-
```
298-
299-
### Important notes
300-
301-
- `ReactDOM.render()` may need to be called multiple times to prevent it from unmounting (this side-effect will be removed in the future).
302-
- `yarn build` or `npm build` must generate files with deterministic names to be referenced in `docs.yml`. The above example uses a [`vite` config](https://github.com/fern-api/docs-custom-js-example/blob/main/custom-app/vite.config.ts) to accomplish this.
303-
- For your hosted Docs site, you may need to update your CD steps to include building the react-app.
304-
- Custom components aren't supported in local development, but are supported in preview links.
305-
306-
<Info>
307-
This approach is subject to change, with notice, as we make improvements to the plugin architecture.
308-
</Info>
240+

fern/products/docs/pages/component-library/custom-components/custom-react-components.mdx

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ description: Add custom React components to your Fern docs for interactive, serv
55

66
<Markdown src="/snippets/pro-plan.mdx"/>
77

8-
You can extend Fern's built-in component library by adding your own custom React components.
9-
This allows you to create unique, interactive elements that match your documentation needs.
8+
You can extend Fern's built-in component library by adding your own custom React components. This allows you to create unique, interactive elements that match your documentation needs. Components are server-side rendered for better SEO and performance, with no layout shifts.
109

1110
<Note title="Defining a constant">
1211
Don't use a React component to define a constant. Instead, consider using [reusable snippets](/docs/writing-content/reusable-snippets).
1312
</Note>
1413

15-
## How it works
14+
## Custom components in MDX
1615

1716
<Steps>
1817
### Create a React component
1918

20-
Let's start by creating a `components` folder where you can define your react components. Note
21-
that the react components can be defined in `.ts`, `.tsx`, `.js` or `.mdx` files.
19+
Let's start by creating a `components` folder where you can define your React components. Note
20+
that the React components can be defined in `.ts`, `.tsx`, `.js` or `.mdx` files.
2221

2322
```ts components/CustomCard.tsx
2423
export const CustomCard = ({ title, text, link, sparkle = false }) => {
@@ -65,6 +64,35 @@ import { CustomCard } from "../components/CustomCard"
6564
```
6665
</Steps>
6766
67+
## Custom header and footer
68+
69+
Replace Fern's default header or footer with your own React components.
70+
71+
<Steps>
72+
### Create your component
73+
74+
Your component file must have a default export returning a React component. Tailwind CSS classes are available, including the `dark:` prefix for dark mode styles (e.g., `dark:bg-gray-900`, `dark:text-gray-400`):
75+
76+
```tsx components/CustomFooter.tsx
77+
export default function CustomFooter() {
78+
return (
79+
<footer className="w-full py-8 px-6 bg-gray-100 dark:bg-gray-900">
80+
<div className="max-w-7xl mx-auto">
81+
Plant Store API Documentation
82+
</div>
83+
</footer>
84+
);
85+
}
86+
```
87+
88+
### Add the component paths to `docs.yml`
89+
90+
```yaml docs.yml
91+
header: ./components/CustomHeader.tsx
92+
footer: ./components/CustomFooter.tsx
93+
```
94+
</Steps>
95+
6896
## Why not just use custom CSS and JS instead?
6997

7098
While you can bundle React components as custom JavaScript, using Fern's built-in React component support provides several key advantages:

fern/products/docs/pages/customization/site-level-settings.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ navbar-links:
111111
[`metadata` configuration](/learn/docs/getting-started/global-configuration#seo-metadata-configuration).
112112
</ParamField>
113113

114+
<ParamField path="header" type="string" required={false} toc={true}>
115+
Path to a custom React component file (TSX or JSX) to replace Fern's default header. The component must have a default export. Learn more about [custom header and footer components](/learn/docs/writing-content/custom-react-components#custom-header-and-footer).
116+
</ParamField>
117+
118+
<ParamField path="footer" type="string" required={false} toc={true}>
119+
Path to a custom React component file (TSX or JSX) to replace Fern's default footer. The component must have a default export. Learn more about [custom header and footer components](/learn/docs/writing-content/custom-react-components#custom-header-and-footer).
120+
</ParamField>
121+
114122
## Instances configuration
115123

116124
An `instance` is the backend of a distinct docs website. Each instance is published to a unique domain using the `--instance` flag. It is most common to use instances to configure staging and production docs which publish to separate URLs.

0 commit comments

Comments
 (0)