Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Swap in your own React components

The Payload Admin Panel is designed to be as minimal and straightforward as possible to allow for easy customization and full control over the UI. In order for Payload to support this level of customization, Payload provides a pattern for you to supply your own React components through your Payload Config.

All Custom Components in Payload are React Server Components by default. This enables the use of the Local API directly on the front-end. Custom Components are available for nearly every part of the Admin Panel for extreme granularity and control.

There are four main types of Custom Components in Payload:

To swap in your own Custom Component, first determine the scope that corresponds to what you are trying to accomplish, consult the list of available components, then author your React component(s) accordingly.

Defining Custom Components

As Payload compiles the Admin Panel, it checks your config for Custom Components. When detected, Payload either replaces its own default component with yours, or if none exists by default, renders yours outright. While there are many places where Custom Components are supported in Payload, each is defined in the same way using Component Paths.

To add a Custom Component, point to its file path in your Payload Config:

1
import { buildConfig } from 'payload'
2
3
const config = buildConfig({
4
// ...
5
admin: {
6
components: {
7
logout: {
8
Button: '/src/components/Logout#MyComponent'
9
}
10
}
11
},
12
})

Component Paths

In order to ensure the Payload Config is fully Node.js compatible and as lightweight as possible, components are not directly imported into your config. Instead, they are identified by their file path for the Admin Panel to resolve on its own.

Component Paths, by default, are relative to your project's base directory. This is either your current working directory, or the directory specified in config.admin.importMap.baseDir.

Components using named exports are identified either by appending # followed by the export name, or using the exportName property. If the component is the default export, this can be omitted.

1
import { buildConfig } from 'payload'
2
import { fileURLToPath } from 'node:url'
3
import path from 'path'
4
const filename = fileURLToPath(import.meta.url)
5
const dirname = path.dirname(filename)
6
7
const config = buildConfig({
8
// ...
9
admin: {
10
importMap: {
11
baseDir: path.resolve(dirname, 'src'),
12
},
13
components: {
14
logout: {
15
Button: '/components/Logout#MyComponent'
16
}
17
}
18
},
19
})

In this example, we set the base directory to the src directory, and omit the /src/ part of our component path string.

Component Config

While Custom Components are usually defined as a string, you can also pass in an object with additional options:

1
import { buildConfig } from 'payload'
2
3
const config = buildConfig({
4
// ...
5
admin: {
6
components: {
7
logout: {
8
Button: {
9
path: '/src/components/Logout',
10
exportName: 'MyComponent',
11
}
12
}
13
}
14
},
15
})

The following options are available:

Property

Description

clientProps

Props to be passed to the Custom Components if it's a Client Component. More details.

exportName

Instead of declaring named exports using # in the component path, you can also omit them from path and pass them in here.

path

File path to the Custom Component. Named exports can be appended to the end of the path, separated by a #.

serverProps

Props to be passed to the Custom Component if it's a Server Component. More details.

For details on how to build Custom Components, see Building Custom Components.

Import Map

In order for Payload to make use of Component Paths, an "Import Map" is automatically generated at app/(payload)/admin/importMap.js. This file contains every Custom Component in your config, keyed to their respective paths. When Payload needs to lookup a component, it uses this file to find the correct import.

The Import Map is automatically regenerated at startup and whenever Hot Module Replacement (HMR) runs, or you can run payload generate:importmap to manually regenerate it.

Custom Imports

If needed, custom items can be appended onto the Import Map. This is mostly only relevant for plugin authors who need to add a custom import that is not referenced in a known location.

To add a custom import to the Import Map, use the admin.dependencies property in your Payload Config:

1
import { buildConfig } from 'payload'
2
3
export default buildConfig({
4
// ...
5
admin: {
6
// ...
7
dependencies: {
8
myTestComponent: { // myTestComponent is the key - can be anything
9
path: '/components/TestComponent.js#TestComponent',
10
type: 'component',
11
clientProps: {
12
test: 'hello',
13
},
14
},
15
},
16
}
17
}

Building Custom Components

All Custom Components in Payload are React Server Components by default. This enables the use of the Local API directly on the front-end, among other things.

Default Props

To make building Custom Components as easy as possible, Payload automatically provides common props, such as the payload class and the i18n object. This means that when building Custom Components within the Admin Panel, you do not have to get these yourself.

Here is an example:

1
import React from 'react'
2
import type { Payload } from 'payload'
3
4
async function MyServerComponent({
5
payload
6
}: {
7
payload: Payload
8
}) {
9
const page = await payload.findByID({
10
collection: 'pages',
11
id: '123',
12
})
13
14
return (
15
<p>{page.title}</p>
16
)
17
}

Each Custom Component receives the following props by default:

Prop

Description

payload

The Payload class.

i18n

The i18n object.

Custom Props

It is also possible to pass custom props to your Custom Components. To do this, you can use either the clientProps or serverProps properties depending on whether your prop is serializable, and whether your component is a Server or Client Component.

1
import { buildConfig } from 'payload'
2
3
const config = buildConfig({
4
// ...
5
admin: {
6
components: {
7
logout: {
8
Button: {
9
path: '/src/components/Logout#MyComponent',
10
clientProps: {
11
myCustomProp: 'Hello, World!'
12
},
13
}
14
}
15
}
16
},
17
})

Here is how your component might receive this prop:

1
import React from 'react'
2
import { Link } from '@payloadcms/ui'
3
4
export function MyComponent({ myCustomProp }: { myCustomProp: string }) {
5
return (
6
<Link href="/admin/logout">{myCustomProp}</Link>
7
)
8
}

Client Components

All Custom Components in Payload are React Server Components by default, however, it is possible to use Client Components by simply adding the 'use client' directive at the top of your file. Payload will automatically detect and remove all non-serializable default props before rendering your component.

1
'use client'
2
import React, { useState } from 'react'
3
4
export function MyClientComponent() {
5
const [count, setCount] = useState(0)
6
7
return (
8
<button onClick={() => setCount(count + 1)}>
9
Clicked {count} times
10
</button>
11
)
12
}

Accessing the Payload Config

From any Server Component, the Payload Config can be accessed directly from the payload prop:

1
import React from 'react'
2
3
export default async function MyServerComponent({
4
payload: {
5
config
6
}
7
}) {
8
return (
9
<Link href={config.serverURL}>
10
Go Home
11
</Link>
12
)
13
}

But, the Payload Config is non-serializable by design. It is full of custom validation functions and more. This means that the Payload Config, in its entirety, cannot be passed directly to Client Components.

For this reason, Payload creates a Client Config and passes it into the Config Provider. This is a serializable version of the Payload Config that can be accessed from any Client Component via the useConfig hook:

1
'use client'
2
import React from 'react'
3
import { useConfig } from '@payloadcms/ui'
4
5
export function MyClientComponent() {
6
const { config: { serverURL } } = useConfig()
7
8
return (
9
<Link href={serverURL}>
10
Go Home
11
</Link>
12
)
13
}

Similarly, all Field Components automatically receive their respective Field Config through props.

Within Server Components, this prop is named field:

1
import React from 'react'
2
import type { TextFieldServerComponent } from 'payload'
3
4
export const MyClientFieldComponent: TextFieldServerComponent = ({ field: { name } }) => {
5
return (
6
<p>
7
{`This field's name is ${name}`}
8
</p>
9
)
10
}

Within Client Components, this prop is named clientField because its non-serializable props have been removed:

1
'use client'
2
import React from 'react'
3
import type { TextFieldClientComponent } from 'payload'
4
5
export const MyClientFieldComponent: TextFieldClientComponent = ({ clientField: { name } }) => {
6
return (
7
<p>
8
{`This field's name is ${name}`}
9
</p>
10
)
11
}

Getting the Current Language

All Custom Components can support language translations to be consistent with Payload's I18n. This will allow your Custom Components to display the correct language based on the user's preferences.

To do this, first add your translation resources to the I18n Config. Then from any Server Component, you can translate resources using the getTranslation function from @payloadcms/translations.

All Server Components automatically receive the i18n object as a prop by default:

1
import React from 'react'
2
import { getTranslation } from '@payloadcms/translations'
3
4
export default async function MyServerComponent({ i18n }) {
5
const translatedTitle = getTranslation(myTranslation, i18n)
6
7
return (
8
<p>{translatedTitle}</p>
9
)
10
}

The best way to do this within a Client Component is to import the useTranslation hook from @payloadcms/ui:

1
'use client'
2
import React from 'react'
3
import { useTranslation } from '@payloadcms/ui'
4
5
export function MyClientComponent() {
6
const { t, i18n } = useTranslation()
7
8
return (
9
<ul>
10
<li>{t('namespace1:key', { variable: 'value' })}</li>
11
<li>{t('namespace2:key', { variable: 'value' })}</li>
12
<li>{i18n.language}</li>
13
</ul>
14
)
15
}

Getting the Current Locale

All Custom Views can support multiple locales to be consistent with Payload's Localization feature. This can be used to scope API requests, etc.

All Server Components automatically receive the locale object as a prop by default:

1
import React from 'react'
2
3
export default async function MyServerComponent({ payload, locale }) {
4
const localizedPage = await payload.findByID({
5
collection: 'pages',
6
id: '123',
7
locale,
8
})
9
10
return (
11
<p>{localizedPage.title}</p>
12
)
13
}

The best way to do this within a Client Component is to import the useLocale hook from @payloadcms/ui:

1
'use client'
2
import React from 'react'
3
import { useLocale } from '@payloadcms/ui'
4
5
function Greeting() {
6
const locale = useLocale()
7
8
const trans = {
9
en: 'Hello',
10
es: 'Hola',
11
}
12
13
return (
14
<span>{trans[locale.code]}</span>
15
)
16
}

Using Hooks

To make it easier to build your Custom Components, you can use Payload's built-in React Hooks in any Client Component. For example, you might want to interact with one of Payload's many React Contexts. To do this, you can use one of the many hooks available depending on your needs.

1
'use client'
2
import React from 'react'
3
import { useDocumentInfo } from '@payloadcms/ui'
4
5
export function MyClientComponent() {
6
const { slug } = useDocumentInfo()
7
8
return (
9
<p>{`Entity slug: ${slug}`}</p>
10
)
11
}

Adding Styles

Payload has a robust CSS Library that you can use to style your Custom Components to match to Payload's built-in styling. This will ensure that your Custom Components integrate well into the existing design system. This will make it so they automatically adapt to any theme changes that might occur.

To apply custom styles, simply import your own .css or .scss file into your Custom Component:

1
import './index.scss'
2
3
export function MyComponent() {
4
return (
5
<div className="my-component">
6
My Custom Component
7
</div>
8
)
9
}

Then to colorize your Custom Component's background, for example, you can use the following CSS:

1
.my-component {
2
background-color: var(--theme-elevation-500);
3
}

Payload also exports its SCSS library for reuse which includes mixins, etc. To use this, simply import it as follows into your .scss file:

1
@import '~@payloadcms/ui/scss';
2
3
.my-component {
4
@include mid-break {
5
background-color: var(--theme-elevation-900);
6
}
7
}
Next

Root Components