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, with the exception of Custom Providers. 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 consult the list of available components, determine the scope that corresponds to what you are trying to accomplish, 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 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:
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.baseDir
. To simplify Component Paths, you can also configure the base directory using the admin.importMap.baseDir
property.
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.
In this example, we set the base directory to the src
directory, and omit the /src/
part of our component path string.
Config Options
While Custom Components are usually defined as a string, you can also pass in an object with additional options:
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 more 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:
Building Custom Components
All Custom Components in Payload are React Server Components by default, with the exception of Custom Providers. 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:
Each Custom Component receives the following props by default:
Custom Props
To pass in custom props from the config, 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.
Client Components
When Building Custom Components, it's still possible to use client-side code such as useState
or the window
object. To do this, simply add the use client
directive at the top of your file. Payload will automatically detect and remove all default, non-serializable props before rendering your component.
Accessing the Payload Config
From any Server Component, the Payload Config can be accessed directly from the payload
prop:
But, the Payload Config is non-serializable by design. It is full of custom validation functions, React components, etc. 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:
All Field Components automatically receive their respective Field Config through props.
Getting the Current Language
All Custom Components can support multiple languages to be consistent with Payload's Internationalization. To do this, first add your translation resources to the I18n Config.
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.
The best way to do this within a Client Component is to import the useTranslation
hook from @payloadcms/ui
:
Getting the Current Locale
All Custom Views can support multiple locales to be consistent with Payload's Localization. They automatically receive the locale
object as a prop by default. This can be used to scope API requests, etc.:
The best way to do this within a Client Component is to import the useLocale
hook from @payloadcms/ui
:
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 one of the many hooks available depending on your needs.
Adding Styles
Payload has a robust CSS Library that you can use to style your Custom Components similarly to Payload's built-in styling. This will ensure that your Custom Components match the existing design system, and so that 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:
Then to colorize your Custom Component's background, for example, you can use the following CSS:
Payload also exports its SCSS library for reuse which includes mixins, etc. To use this, simply import it as follows into your .scss
file:
Root Components
Root Components are those that effect the Admin Panel generally, such as the logo or the main nav.
To override Root Components, use the admin.components
property in your Payload Config:
For details on how to build Custom Components, see Building Custom Components.
The following options are available:
Path | Description |
---|---|
Nav | Contains the sidebar / mobile menu in its entirety. |
beforeNavLinks | An array of Custom Components to inject into the built-in Nav, before the links themselves. |
afterNavLinks | An array of Custom Components to inject into the built-in Nav, after the links. |
beforeDashboard | An array of Custom Components to inject into the built-in Dashboard, before the default dashboard contents. |
afterDashboard | An array of Custom Components to inject into the built-in Dashboard, after the default dashboard contents. |
beforeLogin | An array of Custom Components to inject into the built-in Login, before the default login form. |
afterLogin | An array of Custom Components to inject into the built-in Login, after the default login form. |
logout.Button | The button displayed in the sidebar that logs the user out. |
graphics.Icon | The simplified logo used in contexts like the the Nav component. |
graphics.Logo | The full logo used in contexts like the Login view. |
providers | Custom React Context providers that will wrap the entire Admin Panel. More details. |
actions | An array of Custom Components to be rendered within the header of the Admin Panel, providing additional interactivity and functionality. |
header | An array of Custom Components to be injected above the Payload header. |
views | Override or create new views within the Admin Panel. More details. |
Custom Providers
As you add more and more Custom Components to your Admin Panel, you may find it helpful to add additional React Context(s). Payload allows you to inject your own context providers in your app so you can export your own custom hooks, etc.
To add a Custom Provider, use the admin.components.providers
property in your Payload Config:
Then build your Custom Provider as follows: