Skip to content

Repository files navigation

CI

@iwritec0de/blockbridge-react

Render WordPress Gutenberg blocks as React components.

Pairs with the BlockBridge WP plugin, which exposes a serializedBlocks field on the WordPress REST API. This library renders that payload as a React component tree.

Install

pnpm add @iwritec0de/blockbridge-react
# or
npm i @iwritec0de/blockbridge-react

Usage

import{BlockContent}from'@iwritec0de/blockbridge-react';import'@iwritec0de/blockbridge-react/style.css';exportdefaultfunctionPost({ post }){return<BlockContentblocks={post.serializedBlocks}/>;}

post.serializedBlocks is the array returned by the BlockBridge WP plugin's REST field.

Supported Blocks

BlockComponentNotes
core/paragraphParagraph
core/headingHeadingLevels 1–6
core/imageImageCaption, link wrapping
core/galleryGalleryGrid layout, cropped images
core/listListOrdered and unordered
core/list-itemListItemNested lists via children
core/quoteQuoteCitation support
core/buttonButtonColors, border radius, link targets
core/buttonsButtonsFlex container for button groups
core/groupGroupAny tagName, colors
core/columnsColumnsStacked on mobile
core/columnColumnVariable width
core/coverCoverBackground image, overlay, parallax, focal point
core/media-textMediaTextSide-by-side text + image/video
core/audioAudioNative <audio> with controls
core/videoVideoNative <video> with controls
core/fileFileDownload button
core/separatorSeparator
core/spacerSpacer
core/tableTableHead / body / foot
core/embedEmbedYouTube, Vimeo (responsive iframe), Twitter (blockquote), generic
Any other blockDefaultBlockFalls back to raw innerHTML

Custom serializers

Override how any block renders by passing userSerializers:

constserializers={'core/quote': ({ content, citation, children })=>(<blockquoteclassName="my-quote">{children}{citation&&<cite>{citation}</cite>}</blockquote>),};<BlockContentblocks={blocks}userSerializers={serializers}/>

The serializer receives the block's attributes spread as props, plus a children prop for blocks with nested inner blocks. The key is the Gutenberg block name (core/quote, core/heading, etc.).

Theming

All visual styles are driven by CSS custom properties prefixed with --bb-*. Override them on any wrapper to retheme:

.my-content {
--bb-color-primary:#4f46e5;
--bb-font-family:'Inter', sans-serif;
--bb-content-width:720px;
}

See src/css/tokens.css for the full token list. Key tokens:

TokenDefaultDescription
--bb-color-text#191e23Body text color
--bb-color-bg#fffBackground color
--bb-color-primary#0693e3Links, buttons
--bb-font-familySystem sans-serifBody font stack
--bb-font-size-base16pxBase font size
--bb-line-height1.8Body line height
--bb-content-width610pxMax content width
--bb-block-margin28pxVertical spacing between blocks

Demo

pnpm demo

Boots a Dockerized WordPress instance with seeded block content plus a Next.js demo site at http://localhost:3000.

pnpm demo:down # Stop and remove containers + volumes

Development

pnpm install # Install dependencies
pnpm dev # Start webpack dev server
pnpm build # Build to lib/
pnpm test# Run tests (Jest)
pnpm typecheck # TypeScript check
pnpm lint # ESLint

Project structure

praser/
├── src/
│ ├── components/ # One directory per block type
│ ├── Utils/ # blocksToElements, getSerializers, cleanName
│ ├── css/
│ │ ├── tokens.css # All CSS custom properties
│ │ ├── base.css # Global resets + base styles
│ │ └── blocks/ # Per-block styles
│ └── index.ts # Package entry — exports BlockContent + all components
├── lib/ # Build output (not committed)
└── docs/ # VitePress documentation site

License

GPL-3.0-or-later

About

Render WordPress Gutenberg blocks as React components.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages