Skip to content

Commit dfa0974

Browse files
fix(ui): live-preview-tab should show beforeDocumentControls (#12568)
1 parent f2b6c4a commit dfa0974

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

packages/next/src/views/LivePreview/index.client.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const getAbsoluteUrl = (url) => {
7171
}
7272

7373
const PreviewView: React.FC<Props> = ({
74+
BeforeDocumentControls,
7475
collectionConfig,
7576
config,
7677
Description,
@@ -494,6 +495,7 @@ const PreviewView: React.FC<Props> = ({
494495
/>
495496
<DocumentControls
496497
apiURL={apiURL}
498+
BeforeDocumentControls={BeforeDocumentControls}
497499
customComponents={{
498500
PreviewButton,
499501
PublishButton,
@@ -604,6 +606,7 @@ export const LivePreviewClient: React.FC<
604606
>
605607
<PreviewView
606608
apiRoute={apiRoute}
609+
BeforeDocumentControls={props.BeforeDocumentControls}
607610
collectionConfig={collectionConfig}
608611
config={config}
609612
Description={props.Description}

packages/next/src/views/LivePreview/index.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import type { DocumentViewServerProps, LivePreviewConfig } from 'payload'
1+
import type {
2+
BeforeDocumentControlsServerPropsOnly,
3+
DocumentViewServerProps,
4+
LivePreviewConfig,
5+
ServerProps,
6+
} from 'payload'
27

8+
import { RenderServerComponent } from '@payloadcms/ui/elements/RenderServerComponent'
39
import React from 'react'
410

511
import './index.scss'
@@ -12,6 +18,13 @@ export async function LivePreviewView(props: DocumentViewServerProps) {
1218

1319
let livePreviewConfig: LivePreviewConfig = req.payload.config?.admin?.livePreview
1420

21+
const serverProps: ServerProps = {
22+
i18n: req.i18n,
23+
payload: req.payload,
24+
user: req.user,
25+
// TODO: Add remaining serverProps
26+
}
27+
1528
if (collectionConfig) {
1629
livePreviewConfig = {
1730
...(livePreviewConfig || {}),
@@ -26,6 +39,10 @@ export async function LivePreviewView(props: DocumentViewServerProps) {
2639
}
2740
}
2841

42+
const BeforeDocumentControls =
43+
collectionConfig?.admin?.components?.edit?.beforeDocumentControls ||
44+
globalConfig?.admin?.components?.elements?.beforeDocumentControls
45+
2946
const breakpoints: LivePreviewConfig['breakpoints'] = [
3047
...(livePreviewConfig?.breakpoints || []),
3148
{
@@ -54,6 +71,15 @@ export async function LivePreviewView(props: DocumentViewServerProps) {
5471

5572
return (
5673
<LivePreviewClient
74+
BeforeDocumentControls={
75+
BeforeDocumentControls
76+
? RenderServerComponent({
77+
Component: BeforeDocumentControls,
78+
importMap: req.payload.importMap,
79+
serverProps: serverProps satisfies BeforeDocumentControlsServerPropsOnly,
80+
})
81+
: null
82+
}
5783
breakpoints={breakpoints}
5884
Description={props.Description}
5985
initialData={doc}

test/live-preview/collections/Pages.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export const Pages: CollectionConfig = {
2222
useAsTitle: 'title',
2323
defaultColumns: ['id', 'title', 'slug', 'createdAt'],
2424
components: {
25+
edit: {
26+
beforeDocumentControls: [
27+
'/components/BeforeDocumentControls/index.js#BeforeDocumentControlsTest',
28+
],
29+
},
2530
views: {
2631
edit: {
2732
livePreview: {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const BeforeDocumentControlsTest = () => {
2+
return (
3+
<button aria-label="before-document-controls" type="button">
4+
Test
5+
</button>
6+
)
7+
}

test/live-preview/e2e.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ describe('Live Preview', () => {
8383
})
8484
})
8585

86+
test('collection - should render BeforeDocumentControls', async () => {
87+
await goToCollectionLivePreview(page, pagesURLUtil)
88+
// locate using aria label "before-document-controls"
89+
const beforeDocumentControls = page.locator('button[aria-label="before-document-controls"]')
90+
await expect(beforeDocumentControls).toBeVisible()
91+
})
92+
8693
test('collection — has route', async () => {
8794
await goToCollectionLivePreview(page, pagesURLUtil)
8895
await expect(page.locator('.live-preview')).toBeVisible()

0 commit comments

Comments
 (0)