# Custom Components in Payload CMS Custom Components allow you to fully customize the Admin Panel by swapping in your own React components. You can replace nearly every part of the interface or add entirely new functionality. ## Component Types There are four main types of Custom Components: 1. **Root Components** - Affect the Admin Panel globally (logo, nav, header) 2. **Collection Components** - Specific to collection views 3. **Global Components** - Specific to global document views 4. **Field Components** - Custom field UI and cells ## Defining Custom Components ### Component Paths Components are defined using file paths (not direct imports) to keep the config lightweight and Node.js compatible. ```typescript import { buildConfig } from 'payload' export default buildConfig({ admin: { components: { logout: { Button: '/src/components/Logout#MyComponent', // Named export }, Nav: '/src/components/Nav', // Default export }, }, }) ``` **Component Path Rules:** 1. Paths are relative to project root (or `config.admin.importMap.baseDir`) 2. For **named exports**: append `#ExportName` or use `exportName` property 3. For **default exports**: no suffix needed 4. File extensions can be omitted ### Component Config Object Instead of a string path, you can pass a config object: ```typescript { logout: { Button: { path: '/src/components/Logout', exportName: 'MyComponent', clientProps: { customProp: 'value' }, serverProps: { asyncData: someData }, }, }, } ``` **Config Properties:** | Property | Description | | ------------- | ----------------------------------------------------- | | `path` | File path to component (named exports via `#`) | | `exportName` | Named export (alternative to `#` in path) | | `clientProps` | Props for Client Components (must be serializable) | | `serverProps` | Props for Server Components (can be non-serializable) | ### Setting Base Directory ```typescript import path from 'path' import { fileURLToPath } from 'node:url' const filename = fileURLToPath(import.meta.url) const dirname = path.dirname(filename) export default buildConfig({ admin: { importMap: { baseDir: path.resolve(dirname, 'src'), // Set base directory }, components: { Nav: '/components/Nav', // Now relative to src/ }, }, }) ``` ## Server vs Client Components **All components are React Server Components by default.** ### Server Components (Default) Can use Local API directly, perform async operations, and access full Payload instance. ```tsx import React from 'react' import type { Payload } from 'payload' async function MyServerComponent({ payload }: { payload: Payload }) { const page = await payload.findByID({ collection: 'pages', id: '123', }) return
{page.title}
} export default MyServerComponent ``` ### Client Components Use the `'use client'` directive for interactivity, hooks, state, etc. ```tsx 'use client' import React, { useState } from 'react' export function MyClientComponent() { const [count, setCount] = useState(0) return } ``` **Important:** Client Components cannot receive non-serializable props (functions, class instances, etc.). Payload automatically strips these when passing to client components. ## Default Props All Custom Components receive these props by default: | Prop | Description | Type | | --------- | ---------------------------------------- | --------- | | `payload` | Payload instance (Local API access) | `Payload` | | `i18n` | Internationalization object | `I18n` | | `locale` | Current locale (if localization enabled) | `string` | **Server Component Example:** ```tsx async function MyComponent({ payload, i18n, locale }) { const data = await payload.find({ collection: 'posts', locale, }) return
}
```
### Example: Header Actions
```typescript
export default buildConfig({
admin: {
components: {
actions: ['/components/ClearCacheButton', '/components/PreviewButton'],
},
},
})
```
```tsx
// components/ClearCacheButton.tsx
'use client'
export default function ClearCacheButton() {
return (
)
}
```
## Collection Components
Collection Components are specific to a collection's views.
```typescript
import type { CollectionConfig } from 'payload'
export const Posts: CollectionConfig = {
slug: 'posts',
admin: {
components: {
// Edit view components
edit: {
PreviewButton: '/components/PostPreview',
SaveButton: '/components/CustomSave',
SaveDraftButton: '/components/CustomSaveDraft',
PublishButton: '/components/CustomPublish',
},
// List view components
list: {
Header: '/components/PostsListHeader',
beforeList: ['/components/ListFilters'],
afterList: ['/components/ListFooter'],
},
},
},
fields: [
// ...
],
}
```
## Global Components
Similar to Collection Components but for Global documents.
```typescript
import type { GlobalConfig } from 'payload'
export const Settings: GlobalConfig = {
slug: 'settings',
admin: {
components: {
edit: {
PreviewButton: '/components/SettingsPreview',
SaveButton: '/components/SettingsSave',
},
},
},
fields: [
// ...
],
}
```
## Field Components
Customize how fields render in Edit and List views.
### Field Component (Edit View)
```typescript
{
name: 'status',
type: 'select',
options: ['draft', 'published'],
admin: {
components: {
Field: '/components/StatusField',
},
},
}
```
```tsx
// components/StatusField.tsx
'use client'
import { useField } from '@payloadcms/ui'
import type { SelectFieldClientComponent } from 'payload'
export const StatusField: SelectFieldClientComponent = ({ path, field }) => {
const { value, setValue } = useField({ path })
return (
{translatedTitle}
} ``` **Client Component:** ```tsx 'use client' import { useTranslation } from '@payloadcms/ui' export function MyClientComponent() { const { t, i18n } = useTranslation() return ({t('namespace:key', { variable: 'value' })}
Language: {i18n.language}
{totalDocs} posts
} // Only use client components when you need: // - State (useState, useReducer) // - Effects (useEffect) // - Event handlers (onClick, onChange) // - Browser APIs (localStorage, window) ``` ### 4. React Best Practices - Use React.memo() for expensive components - Implement proper key props in lists - Avoid inline function definitions in renders - Use Suspense boundaries for async operations ## Import Map Payload generates an import map at `app/(payload)/admin/importMap.js` that resolves all component paths. **Regenerate manually:** ```bash payload generate:importmap ``` **Override location:** ```typescript export default buildConfig({ admin: { importMap: { baseDir: path.resolve(dirname, 'src'), importMapFile: path.resolve(dirname, 'app', 'custom-import-map.js'), }, }, }) ``` ## Type Safety Use Payload's TypeScript types for components: ```tsx import type { TextFieldServerComponent, TextFieldClientComponent, TextFieldCellComponent, } from 'payload' export const MyFieldComponent: TextFieldServerComponent = (props) => { // Fully typed props } ``` ## Troubleshooting ### "useConfig is undefined" or similar hook errors **Cause:** Dependency version mismatch between Payload packages. **Solution:** Pin all `@payloadcms/*` packages to the exact same version: ```json { "dependencies": { "payload": "3.0.0", "@payloadcms/ui": "3.0.0", "@payloadcms/richtext-lexical": "3.0.0" } } ``` ### Component not loading 1. Check file path is correct (relative to baseDir) 2. Verify named export syntax: `/path/to/file#ExportName` 3. Run `payload generate:importmap` to regenerate 4. Check for TypeScript errors in component file ## Resources - [Custom Components Docs](https://payloadcms.com/docs/custom-components/overview) - [Root Components](https://payloadcms.com/docs/custom-components/root-components) - [Custom Views](https://payloadcms.com/docs/custom-components/custom-views) - [React Hooks](https://payloadcms.com/docs/admin/react-hooks) - [Custom CSS](https://payloadcms.com/docs/admin/customizing-css)