Customize Blocks and Modifiers in BlocksRenderer
mainYou can override the default rendering behavior by providing custom React components via the blocks and modifiers props. These custom components are merged with the defaults, allowing you to override only specific elements.
Blocks
Blocks are full-width, root-level elements. Supported types include:
paragraphheading(receiveslevelandplainText)list(receivesformat)quotecode(receivesplainText)image(receivesimage)link(receivesurl)
Modifiers
Modifiers are inline elements used to style text fragments within a block. Supported types include:
bolditalicunderlinestrikethroughcode
Important: When providing custom components, you must ensure that you render the children prop so that nested blocks and modifiers are correctly rendered.
import { BlocksRenderer } from '@strapi/blocks-react-renderer';
// Content should come from your Strapi API
const content = [
{
type: 'paragraph',
children: [{ type: 'text', text: 'A simple paragraph' }],
},
];
const App = () => {
return (
<BlocksRenderer
content={content}
blocks={{
// You can use the default components to set class names...
paragraph: ({ children }) => <p className="text-neutral900 max-w-prose">{children}</p>,
// ...or point to a design system
heading: ({ children, level }) => {
switch (level) {
case 1: return <Typography variant="h1">{children}</Typography>
case 2: return <Typography variant="h2">{children}</Typography>
case 3: return <Typography variant="h3">{children}</Typography>
case 4: return <Typography variant="h4">{children}</Typography>
case 5: return <Typography variant="h5">{children}</Typography>
case 6: return <Typography variant="h6">{children}</Typography>
default: return <Typography variant="h1">{children}</Typography>
}
},
// For links, you may want to use the component from your router or framework
link: ({ children, url }) => <Link to={url}>{children}</Link>,
}}
modifiers={{
bold: ({ children }) => <strong>{children}</strong>,
italic: ({ children }) => <span className="italic">{children}</span>,
}}
/>
);
};