Skip to content

feat(next): export default edit and live preview views #12772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jacobsfletch
Copy link
Member

@jacobsfletch jacobsfletch commented Jun 11, 2025

Exports the default EditView and LivePreviewView from @payloadcms/next/views. This will allow you to import these components into your own project and render them within your custom view.

For example, if you wanted to swap live preview with the default edit view, you could do something like this:

import type { CollectionConfig } from 'payload'

export const MyCollectionConfig: CollectionConfig = {
  // ...
  admin: {
    components: {
      views: {
        edit: {
          default: {
            Component: '/path-to-my-custom-live-preview-view',
            tab: {
              label: 'Live Preview',
            },
          },
          livePreview: {
            Component: '/path-to-a-404-view,
          },
          newEdit: {
            path: '/edit',
            Component: '/path-to-my-custom-edit-view,
            tab: {
              label: 'Edit',
              href: '/edit',
            },
          },
        },
      },
    },
  },
}

Your custom live preview view would look something like this:

import type { DocumentViewServerProps } from 'payload'

import { LivePreviewView } from '@payloadcms/next/views'
import React from 'react'

export const MyLivePreviewView = (props: DocumentViewServerProps) => {
  return <LivePreviewView {...props} />
}

Followed by your custom edit view:

'use client'
import type { DocumentViewClientProps } from 'payload'

import { EditView } from '@payloadcms/next/views'
import React from 'react'

export const MyEditView = (props: DocumentViewClientProps) => {
  return <EditView {...props} />
}

Bigger Picture

In the future we plan to make this even easier, as this is such a frequent request. Ideally, the /preview route doesn't exist at all, and the default edit view can switch between the two without re-routing to a different page entirely. This would mean that there is no need to wire up custom views to achieve the same functionality, and your selection would save to preferences.

This would also completely avoid the LeaveWithoutSaving modal from popping up when you attempt to navigate to live preview with unsaved changes, which would be a huge win.

jacobsfletch added a commit that referenced this pull request Jun 12, 2025
…igs (#12774)

Customizing the `path` property on default document views is currently
not supported, but the types suggest that it is. You can only provide a
path to custom views. This PR ensures that `path` cannot be set on
default views as expected.

For example:

```ts
import type { CollectionConfig } from 'payload'

export const MyCollectionConfig: CollectionConfig = {
  // ...
  admin: {
    components: {
      views: {
        edit: {
          default: {
            path: '/' // THIS IS NOT ALLOWED!
          },
          myCustomView: {
            path: '/edit', // THIS IS ALLOWED!
            Component: '/collections/CustomViews3/MyEditView.js#MyEditView',
          },
        },
      },
    },
  },
}
```

For background context, this was deeply explored in #12701. This is not
planned, however, due to [performance and maintainability
concerns](#12701 (comment)),
plus [there are alternatives to achieve
this](#12772).

This PR also fixes and improves various jsdocs, and fixes a typo found
in the docs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants