Skip to content

templates: adds Reusable Content Block to website templates #13263

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 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 72 additions & 41 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions templates/website/src/blocks/RenderBlocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { CallToActionBlock } from '@/blocks/CallToAction/Component'
import { ContentBlock } from '@/blocks/Content/Component'
import { FormBlock } from '@/blocks/Form/Component'
import { MediaBlock } from '@/blocks/MediaBlock/Component'
import { ReusableContentBlock } from '@/blocks/ReusableContent/Component'

const blockComponents = {
archive: ArchiveBlock,
content: ContentBlock,
cta: CallToActionBlock,
formBlock: FormBlock,
mediaBlock: MediaBlock,
reusableContentBlock: ReusableContentBlock,
}

export const RenderBlocks: React.FC<{
Expand Down
28 changes: 28 additions & 0 deletions templates/website/src/blocks/ReusableContent/Component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Page } from '@/payload-types'
import { RenderBlocks } from '@/blocks/RenderBlocks'
import React from 'react'

export type Props = Extract<Page['layout'][0], { blockType: 'reusableContentBlock' }>

export const ReusableContentBlock: React.FC<Props> = ({ reusableContentBlockFields }) => {
const { customId, reusableContent } = reusableContentBlockFields

if (
typeof reusableContent === 'object' &&
reusableContent !== null &&
'layout' in reusableContent &&
Array.isArray((reusableContent as { layout: unknown }).layout)
) {
const layout = (reusableContent as { layout: Page['layout'] }).layout

console.log('Resolved reusable layout:', layout)

return (
<section id={customId ?? undefined}>
<RenderBlocks blocks={layout} />
</section>
)
}

return null
}
26 changes: 26 additions & 0 deletions templates/website/src/blocks/ReusableContent/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Block } from 'payload'

export const ReusableContent: Block = {
slug: 'reusableContentBlock',
fields: [
{
name: 'reusableContentBlockFields',
type: 'group',
fields: [
{
name: 'reusableContent',
type: 'relationship',
relationTo: 'reusable-content',
required: true,
},
{
name: 'customId',
type: 'text',
admin: {
description: 'Custom ID for targeting with CSS/JS',
},
},
],
},
],
}
11 changes: 10 additions & 1 deletion templates/website/src/collections/Pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CallToAction } from '../../blocks/CallToAction/config'
import { Content } from '../../blocks/Content/config'
import { FormBlock } from '../../blocks/Form/config'
import { MediaBlock } from '../../blocks/MediaBlock/config'
import { ReusableContent } from '@/blocks/ReusableContent/config'
import { hero } from '@/heros/config'
import { slugField } from '@/fields/slug'
import { populatePublishedAt } from '../../hooks/populatePublishedAt'
Expand Down Expand Up @@ -75,7 +76,15 @@ export const Pages: CollectionConfig<'pages'> = {
{
name: 'layout',
type: 'blocks',
blocks: [CallToAction, Content, MediaBlock, Archive, FormBlock],
blockReferences: [
CallToAction,
Content,
Archive,
MediaBlock,
FormBlock,
ReusableContent,
],
blocks: [],
required: true,
admin: {
initCollapsed: true,
Expand Down
37 changes: 37 additions & 0 deletions templates/website/src/collections/ReusableContents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { CollectionConfig } from 'payload'

import { Archive } from '@/blocks/ArchiveBlock/config'
import { CallToAction } from '@/blocks/CallToAction/config'
import { Content } from '@/blocks/Content/config'
import { MediaBlock } from '@/blocks/MediaBlock/config'
import { FormBlock } from '@/blocks/Form/config'

import { anyone } from '../access/anyone'
import { authenticated } from '../access/authenticated'

export const ReusableContent: CollectionConfig = {
slug: 'reusable-content',
access: {
create: authenticated,
delete: authenticated,
read: anyone,
update: authenticated,
},
admin: {
useAsTitle: 'title',
},
fields: [
{
name: 'title',
type: 'text',
required: true,
},
{
name: 'layout',
type: 'blocks',
blockReferences: [CallToAction, Content, Archive, MediaBlock, FormBlock],
blocks: [],
required: true,
},
],
}
Loading
Loading