The Gutenberg editor empowers marketers to build content and layouts, but it is incompatible with React and front-end design systems. We're here to fix that!
For this tutorial, you'll need a website. If you have that already, just skip to the next section, or follow these instructions to build a new Next.js-based test site.
cd
into a directory that you like for programming, and then create a generic Next.js Typescript app like this (it will create a new sub-directory called "atlas-tester
"). (Consult Next.js's documentation if you run into trouble.)
npxcreate- next- app - - example with- typescript atlas- tester cdatlas- tester npmrun dev # run the auto- updating development server on port 3000 open 'http:// localhost:3000' # or just open this in your web browser
To edit the root page of the site, open up pages/index.tsx
. In the next section, we'll be replacing this entire page.
Start by installing our npm package:
npm install@asmartbear/ gutenberg- bridge
Now create (or reuse) a page for testing Gutenberg. Here is a complete example (so e.g. if you use Next.js, you can completely replace a page with this code).
import {AtlasContainer, AtlasGutenberg, StandardThemeGenerator } from '@asmartbear/ gutenberg- bridge'; import '@asmartbear/gutenberg- bridge/ dist/ registerAll'; const Page: React.FC = () => {constgutenbergHtml = ` < h2> Hello, World!< / h2> <p>This is Gutenberg content without blocks, for example if you use the Classic Editor. </p>`;const themeGen = new StandardThemeGenerator();const theme = themeGen.getTheme();return <><h1>Hello Atlas</h1><AtlasContainer theme={theme}><AtlasGutenberg htmlRaw={gutenbergHtml} /></AtlasContainer></>;}export default Page
Restart the dev server (e.g. npm run dev
for Next.js) to see the results. It's not doing much yet, but let's explain the code:
import '@asmartbear/ gutenberg- bridge/ dist/ registerAll'
const themeGen = new StandardThemeGenerator( )
true
to create a dark-mode theme.< AtlasContainer theme={ theme}>
_app.tsx
, as explained here.) In this tutorial, we'll just keep it here in the page.< AtlasGutenberg htmlRaw={ gutenbergHtml} / >
That was just plain HTML. To prove that this system converts full Gutenberg blocks to React components, replace the hard-coded input with the Atlas "Paper" block, with an inner Gutenberg Core "Paragraph" block:
constgutenbergHtml = ` <!- - wp:atlas- material/ paper { "elevation":8, "rounded":true, "rotation":"- 2"} - - > <div class="atlas- paper"> < !- - wp:paragraph - - > <p> This is demonstrating one of the Atlas components, sourcedfrom Gutenberg data. < / p> <!- - / wp:paragraph - - > < / div> < !- - / wp:atlas- material/ paper - - > <!- - wp:paragraph - - > < p> < / p> < !- - / wp:paragraph - - > `;
When you run this, you'll discover that, while it mostly works, some CSS styles appear to be missing. You can tell, because here is how this renders in our example app:
But here is how it renders in the Gutenberg editor:
So, we need to add CSS styles. There are a few global stylesheets to add. They're bundled with the npm package, so you already have them. The trick is, every React application framework has a different way of including global stylesheets! So, you'll have to use whatever technique is appropriate for your system.
For example, if you're using Next.js, global stylesheets are imported in pages/_app.tsx
. Here's a full example you can use if you're following along with the app from above:
// Atlas CSS, detailed below. import '@asmartbear/gutenberg- bridge/ dist/ css/ full. css'; import '@asmartbear/gutenberg- bridge/ dist/ css/ core- blocks. css'; import '@asmartbear/gutenberg- bridge/ dist/ css/ atlas- blocks. css'; import '@asmartbear/gutenberg- bridge/ dist/ css/ atlas- material- blocks. css'; // Everything from here- down is Next. js stuff, not Atlas stuff. import React from 'react';import Head from 'next/head';interface AppProps {Component: any; pageProps: any; };export default function App({ Component, pageProps }: AppProps): JSX.Element {return (<React.Fragment> <Head> <meta name="viewport" content="width=device- width, initial- scale=1, shrink- to- fit=no" /> </Head> <Component {...pageProps} /> </React.Fragment> );}
Not all of these stylesheets are required. Here's what they do:
full.css
core-blocks.css
atlas-blocks.css
atlas-material-blocks.css
With stylesheets in place, the blocks are rendering correctly:
Now that we can display arbitrary Gutenberg blocks, we need to grab that data from WordPress.
You can use either the WordPress REST API or WP GraphQL. The key piece of data you need from a page or a post is called the "raw content." This is different from the "content," a.k.a. the rendered content. Only the raw content contains the Gutenberg block data.
Both the REST API and WP GraphQL require authentication in order to query the raw content. You can use any type of WordPress authentication to do this. If you want something simple and already built into Core, just use a user account (possibly creating one specifically for this purpose) and generate an "Application Password," which you can do at the bottom of your User Profile page (where you edit your name and avatar). You can then authenticate with WordPress using that username, and that generated application password.
Note for WP GraphQL users: The way you get raw content is by specifying the "raw
" format parameter to post content. Here's a simple example:
queryMyQuery { pageBy(uri: "/my-page") {title content(format: RAW)}}
The Atlas Theme System provides a variety of tools for encoding typefaces (including support for Google Fonts), type sizing and spacing, color palettes, layout grids, and viewport width breakpoints. Getting this to match your website theme (or just building your theme using our system directly!), creates an integrated WYSIWYG experience in the Gutenberg editor, where marketers and content authors are "locked down" to the specific options of your site design, while still supporting great features like automated "dark mode."
The StandardThemeGenerator
object from our example is a good place to start. Everything is documented and Typescript-safe, so it's easiest to just look through your options there and play with settings.
Here's an example of some things you can do with the generator:
const themeGen = new StandardThemeGenerator();themeGen.baseFontScale= 1.2; / / increase all font sizes by 20% themeGen.baseHeadingWeight= 600; / / CSS weight for headings themeGen.baseTypefaces.unshift("RobotoSlab"); / / Insert a preferred font themeGen.googleFontFamilies.push("RobotoSlab"); / / Load font from themeGen.addBrandColor("tiffany", "#0ecad4"); // Named brand colors themeGen.addBrandColor("mirage", "#002838");themeGen.addBrandColor("seafoam", "#50e3c2");themeGen.addBrandColor("royal", "#7e5cef");themeGen.addBrandColor("sunset", "#ff6c29");const theme = themeGen.getTheme();
After generating the theme
object, there are even more settings you can tweak! Everything from letter-spacing in headings to named font pre-sets to auto-scaling fonts for narrow devices.
The more you add to the font typefaces, the presets, and the palette colors, the more you will also be empowering marketers, because all of that appears in the WordPress Gutenberg editor, and automatically supports "dark mode."
To get the Atlas blocks, plus your custom blocks, plus your website's styles, all appearing in the Gutenberg Editor, you'll need your own, specially-built theme for WordPress. Atlas will create that for you.
First, install the utility that builds the WordPress theme. This is packaged separately from the run-time library. You can install it into your project as a development dependency, or install it globally:
npminstall --global @asmartbear/gutenberg-bridge-theme-builder
Next, create a JSON configuration file containg all your custom theme data (which you've tweaked with code), as well as information about the Gutenberg blocks which are supported in React (including your custom blocks, which we haven't created yet). Because this configuration is established from code, you'll need to export it from the running website.
A simple way to do this in development is to use a special React component Atlas supplies for this purpose:
importCopyBlockConfig from '@asmartbear/ gutenberg- bridge/ dist/ react- util/ CopyBlockConfig' ...return <><h1>Hello Atlas</h1><AtlasContainer theme={theme}><AtlasGutenberg htmlRaw={gutenbergHtml} /><CopyBlockConfig /> {/* < - - right here */ } </AtlasContainer></>;
The component looks like this:
Just click on the text box and the content is copied to the clipboard. Place that content in a file wherever is convenient; for the tutorial, we'll put it in a file named atlas-block-config.json
in the root of the project.
Next, edit package.json
to add two things: A script that builds the WordPress theme, and configuration commands to the theme-builder utility. Add the script and below it (often just before the end of the file), this other configuration:
{"scripts": {..."wp-theme": "shnode_modules/ @asmartbear/ gutenberg- bridge- theme- builder/ build- wp- theme. sh" },..."atlas": {"config-json": ". / atlas- block- config. json", "theme-dest": ". / wp- theme", "block-css- output- file": "css/ block. css", "block-dirs": [ "node_modules/ @asmartbear/ gutenberg- bridge/ dist" ]}}
Here's what that configuration means:
.../build-wp-theme.sh
/ usr/ local/ lib/ node_modules/ @asmartbear/ gutenberg- bridge- theme- builder/ build- wp- theme. sh
."config-json"
"theme-dest"
"block-css-output-file"
"block-dirs"
Finally, we can generate the theme!
npmrun wp- theme
Note that it generated both a directory called wp-theme/
as well as a ZIP file called wp-theme-vX.X.X.zip
. Both should be excluded from git using the pattern wp-theme*
as a line inside .gitignore
.
You can install this theme in any of the usual ways, including from wp-cli
or from the web interface. For the latter, go to the menu "Appearence > Themes
", then click the "Add New
" button at the top, then click the "Upload Theme
" button at the top, and supply the ZIP file. Finally, activate the theme by clicking the "Activate
" button in the list of themes.
That's it! The Gutenberg editor will have all the Atlas blocks, and your website's theme.
Now that you're generating the theme yourself, you should use your own, generated CSS for blocks instead of the CSS that is bundled with the core npm package. This is because the Atlas blocks are included in the generated CSS, and any custom blocks you create will also be included. To do this, just include the generated CSS instead of the built-ins. Using the earlier example, and assuming the package.json
configuration from our example above, the CSS includes would become:
import '@asmartbear/gutenberg- bridge/ dist/ css/ full. css'; import '@asmartbear/gutenberg- bridge/ dist/ css/ core- blocks. css'; import '.. / css/ block. css';
Now that you have the bridge working, let's take a look at the Atlas blocks that are already at your disposal, both in React and in Gutenberg.