Swap in your own React components

While designing the Payload Admin panel, we determined it should be as minimal and straightforward as possible to allow easy customization and control. There are many times where you may want to completely control how a whole view or a field works. You might even want to add in your own routes entirely. In order for Payload to support that level of customization without introducing versioning / future-proofing issues, Payload provides for a pattern to supply your own React components via your Payload config.

To swap in your own React component, first, consult the list of available component overrides below. Determine the scope that corresponds to what you are trying to accomplish, and then author your React component accordingly.

#
Base Component Overrides

You can override a set of admin panel-wide components by providing a component to your base Payload config's admin.components property. The following options are available:

PathDescription
NavContains the sidebar and mobile Nav in its entirety.
logout.ButtonA custom React component.
BeforeDashboardArray of components to inject into the built-in Dashboard, before the default dashboard contents.
AfterDashboardArray of components to inject into the built-in Dashboard, after the default dashboard contents. Demo
BeforeLoginArray of components to inject into the built-in Login, before the default login form.
AfterLoginArray of components to inject into the built-in Login, after the default login form.
BeforeNavLinksArray of components to inject into the built-in Nav, before the links themselves.
AfterNavLinksArray of components to inject into the built-in Nav, after the links.
views.AccountThe Account view is used to show the currently logged in user's Account page.
views.DashboardThe main landing page of the Admin panel.
graphics.IconUsed as a graphic within the Nav component. Often represents a condensed version of a full logo.
graphics.LogoThe full logo to be used in contexts like the Login view.
routesDefine your own routes to add to the Payload Admin UI. More
providersDefine your own provider components that will wrap the Payload Admin UI. More

Full example:

payload.config.js

import { buildConfig } from "payload/config";
import {
MyCustomNav,
MyCustomLogo,
MyCustomIcon,
MyCustomAccount,
MyCustomDashboard,
MyProvider,
} from "./customComponents";
export default buildConfig({
admin: {
components: {
Nav: MyCustomNav,
graphics: {
Icon: MyCustomIcon,
Logo: MyCustomLogo,
},
views: {
Account: MyCustomAccount,
Dashboard: MyCustomDashboard,
},
providers: [MyProvider],
},
},
});

For more examples regarding how to customize components, look at the following examples.

#
Collections

You can override components on a Collection-by-Collection basis via each Collection's admin property.

PathDescription
views.EditUsed while a document within this Collection is being edited.
views.ListThe List view is used to render a paginated, filterable table of Documents in this Collection.
edit.SaveButtonReplace the default Save button with a custom component. Drafts must be disabled
edit.SaveDraftButtonReplace the default Save Draft button with a custom component. Drafts must be enabled and autosave must be disabled.
edit.PublishButtonReplace the default Publish button with a custom component. Drafts must be enabled.
edit.PreviewButtonReplace the default Preview button with a custom component.

Examples

// Custom Buttons
import * as React from "react";
import {
CustomSaveButtonProps,
CustomSaveDraftButtonProps,
CustomPublishButtonProps,
CustomPreviewButtonProps,
} from "payload/types";
export const CustomSaveButton: CustomSaveButtonProps = ({
DefaultButton,
label,
}) => {
return <DefaultButton label={label} />;
};
export const CustomSaveDraftButton: CustomSaveDraftButtonProps = ({
DefaultButton,
disabled,
label,
saveDraft,
}) => {
return (
<DefaultButton label={label} disabled={disabled} saveDraft={saveDraft} />
);
};
export const CustomPublishButton: CustomPublishButtonProps = ({
DefaultButton,
disabled,
label,
publish,
}) => {
return <DefaultButton label={label} disabled={disabled} publish={publish} />;
};
export const CustomPreviewButton: CustomPreviewButtonProps = ({
DefaultButton,
disabled,
label,
preview,
}) => {
return <DefaultButton label={label} disabled={disabled} preview={preview} />;
};

#
Globals

As with Collections, You can override components on a global-by-global basis via their admin property.

PathDescription
views.EditUsed while this Global is being edited.
edit.SaveButtonReplace the default Save button with a custom component. Drafts must be disabled
edit.SaveDraftButtonReplace the default Save Draft button with a custom component. Drafts must be enabled and autosave must be disabled.
edit.PublishButtonReplace the default Publish button with a custom component. Drafts must be enabled.
edit.PreviewButtonReplace the default Preview button with a custom component.

#
Fields

All Payload fields support the ability to swap in your own React components. So, for example, instead of rendering a default Text input, you might need to render a color picker that provides the editor with a custom color picker interface to restrict the data entered to colors only.

Fields support the following custom components:

ComponentDescription
FilterOverride the text input that is presented in the List view when a user is filtering documents by the customized field.
CellUsed in the List view's table to represent a table-based preview of the data stored in the field. More
FieldSwap out the field itself within all Edit views. More

#
Cell Component

These are the props that will be passed to your custom Cell to use in your own components.

PropertyDescription
fieldAn object that includes the field configuration.
colIndexA unique number for the column in the list.
collectionAn object with the config of the collection that the field is in.
cellDataThe data for the field that the cell represents.
rowDataAn object with all the field values for the row.

Example

import React from "react";
import "./index.scss";
const baseClass = "custom-cell";
const CustomCell: React.FC<Props> = (props) => {
const { field, colIndex, collection, cellData, rowData } = props;
return <span className={baseClass}>{cellData}</span>;
};

#
Field Component

When writing your own custom components you can make use of a number of hooks to set data, get reactive changes to other fields, get the id of the document or interact with a context from a custom provider.

#
Sending and receiving values from the form

When swapping out the Field component, you'll be responsible for sending and receiving the field's value from the form itself. To do so, import the useField hook as follows:

import { useField } from "payload/components/forms";
type Props = { path: string };
const CustomTextField: React.FC<Props> = ({ path }) => {
const { value, setValue } = useField<Props>({ path });
return (
<input onChange={(e) => setValue(e.target.value)} value={value.path} />
);
};

#
Custom routes

You can easily add your own custom routes to the Payload Admin panel using the admin.components.routes property. Payload currently uses the extremely powerful React Router v5.x and custom routes support all the properties of the React Router <Route /> component.

Custom routes support the following properties:

PropertyDescription
Component *Pass in the component that should be rendered when a user navigates to this route.
path *React Router path. See the React Router docs for more info.
exactReact Router exact property. More
strictReact Router strict property. More
sensitiveReact Router sensitive property. More

* An asterisk denotes that a property is required.

Custom route components

Your custom route components will be given all the props that a React Router <Route /> typically would receive, as well as two props from Payload:

PropDescription
userThe currently logged in user. Will be null if no user is logged in.
canAccessAdmin *If the currently logged in user is allowed to access the admin panel or not.

Example

You can find examples of custom route views in the Payload source code /test/admin/components/views folder. There, you'll find two custom routes:

  1. A custom view that uses the DefaultTemplate, which is the built-in Payload template that displays the sidebar and "eyebrow nav"
  2. A custom view that uses the MinimalTemplate - which is just a centered template used for things like logging in or out

To see how to pass in your custom views to create custom routes of your own, take a look at the admin.components.routes property of the Payload test admin config.

#
Custom providers

As your admin customizations gets more complex you may want to share state between fields or other components. You can add custom providers to do add your own context to any Payload app for use in other custom components within the admin panel. Within your config add admin.components.providers, these can be used to share context or provide other custom functionality. Read the React context docs to learn more.

#
Styling Custom Components

Payload exports its SCSS variables and mixins for reuse in your own custom components. This is helpful in cases where you might want to style a custom input similarly to Payload's built-ini styling, so it blends more thoroughly into the existing admin UI.

To make use of Payload SCSS variables / mixins to use directly in your own components, you can import them as follows:

@import '~payload/scss';

#
Getting the current language

When developing custom components you can support multiple languages to be consistent with Payload's i18n support. The best way to do this is to add your translation resources to the i18n configuration and import useTranslation from react-i18next in your components.

For example:

import { useTranslation } from "react-i18next";
const CustomComponent: React.FC = () => {
const { t, i18n } = useTranslation("namespace1");
return (
<ul>
<li>{t("key", { variable: "value" })}</li>
<li>{t("namespace2:key", { variable: "value" })}</li>
<li>{i18n.language}</li>
</ul>
);
};

#
Getting the current locale

In any custom component you can get the selected locale with the useLocale hook. Here is a simple example:

import { useLocale } from "payload/components/utilities";
const Greeting: React.FC = () => {
const locale = useLocale();
const trans = {
en: "Hello",
es: "Hola",
};
return <span> {trans[locale]} </span>;
};
Next

React Hooks