Creating Custom Content Blocks: Wordpress Gutenberg vs. Sanity.io
Tags: javascript, webdev, react, beginners
Published: 2018-12-07 07:38:19

Wordpress 5.0 comes with a brand new rich text editor called [Gutenberg](https://wordpress.org/gutenberg/). It is highly anticipated and has created both buzz and [controversy](https://rianrietveld.com/2018/10/09/i-have-resigned-the-wordpress-accessibility-team/). The promise of Gutenberg is customizability, especially when it comes to adding custom content blocks. If you want to get started with Gutenberg, you can read this [excellent introduction in Smashing Magazine](https://www.smashingmagazine.com/2018/10/gutenberg-testimonials-sliderblock/). It is built in React and allow developers to extend the editors basic functionality.

![Custom testimonial block type in Wordpress Gutenberg](https://cdn.sanity.io/images/3do82whm/production/07e5821d81408fde490467f1cffd69613fa301cc-2000x1081.png) Custom testimonial block type in Wordpress Gutenberg. Published on smashingmagazine.com

We couldn't resist though. Since having custom content blocks is in the DNA of [Sanity](https://www.github.com/sanity-io/sanity), we wanted to show how easy it is to recreate the testimonials slider that the 12 min read, and 10 part article introduces you to. Like an eleventh of the time easier. 🐇

Of course, the comparison only goes so far. Wordpress is a classic CMS with a template engine, whereas you can use Sanity as a headless CMS with the frontend framework you would prefer, be it Vue, React or something else. 

If you haven't tried Sanity yet, it only takes two minutes to get started. To create a project and get an instant real-time hosted content API, and the connected open sourced editor locally, run this line in your terminal:
    
```js    
    npm install -g @sanity/cli && sanity init
```

If you choose the blog template in the CLI tool, you're pretty much set to go!

## The testimonial slider content model

In the tutorial on Smashing Magazine, you learn to make a "testimonial slider" – typically used on marketing pages as a way to visualize social proof for your product.

To re-create the Wordpress testimonial slider for Sanity you only need to define its content model. We'll take care of the input fields, and the real-time syncing to the datastore. 

The content model is pretty straightforward: First, we make the type for a `testimonialSlider`. It's an object, with an array (which also holds the order of the testimonials) of testimonial objects with the fields `author`, `image`, `content`, and `link` to the source. I made the content field be clean text, but we could also have used `blockContent` if we wanted to have rich text (and a slider within a slider, in case you are into recursive content patterns). If we add `options: { hotspot: true }` to the image field, your editor can even set custom hotspots and crops for the image, which may be useful for image art direction.
    
 ```js   
 const testimonialSlider = {
  name: 'testimonialSlider',
  title: 'Testimonial slider',
  type: 'object',
  fields: [
    {
      name: 'slider',
      title: 'Slider',
      type: 'array',
      of: [
        {
          name: 'testimonial',
          title: 'Testimonial',
          type: 'object',
          fields: [
            {
              name: 'author',
              title: 'Author',
              type: 'string'
            },
            {
              name: 'image',
              title: 'Image',
              type: 'image'
            },
            {
              name: 'content',
              title: 'Content',
              type: 'text'
            },
            {
              name: 'link',
              title: 'Link',
              type: 'url'
            }
          ]
        }
      ]
    }
  ]
}

const blockContent = {
  name: 'blockContent',
  type: 'array',
  of: [
    {
      type: 'block'
    },
    {
      type: 'testimonialSlider'
    }
  ]
}

export default {
  testimonialSlider,
  blockContent
}

```

That's pretty much it. The user interface will be clean and can be used right away.

![Example of the slider user interface](https://thepracticaldev.s3.amazonaws.com/i/a2jp512e4wxj10b2w09e.gif)

To get that nice prepared JSON-structure for your frontend, you can use this GROQ query, assuming that your rich text is named `content` and used in the document type `post`:
    
```js    
*[_type == "post"]{
  ...,
  content[]{
    ...,
    _type == "testimonialSlider" => {
      slider[]{
        ...,
        image{
          ...,
          asset->
        }
      }
    }
  }
}
```

## Custom preview component

You can also add a custom preview component with some React code:
    
```js    
import React from 'react'
import client from 'part:@sanity/base/client'
import urlBuilder from '@sanity/image-url'
const urlFor = source => urlBuilder(client).image(source)

const sliderPreview = ({ value = {} }) => {
  return (
    <marquee>
      {value &&
        value.slider.map(slide => (
          <div
            key={slide._key}
            style={{ display: 'inline-block', marginRight: '1em' }}
          >
            <figure>
              <img
                src={urlFor(slide.image)
                  .width(50)
                  .url()}
                style={{ marginRight: '0.5em' }}
              />
              <span className="legend">
                “<em>{slide.content}</em>”<br />
                {slide.author} – {slide.link}
              </span>
            </figure>
          </div>
        ))}
    </marquee>
  )
}

export default sliderPreview
```

Include it in your schema by adding this to your `testimonialSlider` content type schema:

    
```js
import React from 'react'
import sliderPreview from './sliderPreview.js'
 
const testimonialSlider = {
  name: 'testimonialSlider',
  title: 'Testimonial slider',
  type: 'object',
  preview: {
    select: {
      slider: 'slider'
    },
    component: sliderPreview
  },
  fields: [
    /* the fields */
  ]
}
```    

Here I've used the good old HTML element `<marquee>` to get a scrolling effect; you probably shouldn't do the same:

![Preview of the testimonial slider block](https://thepracticaldev.s3.amazonaws.com/i/2adq0mnxnz4cn6espygz.gif)

## Beyond appearances: The advantage of deeply typed rich text content

This testimonial slider example tightly ties to a specific presentation on a webpage, which makes sense in WordPress because it's built to render and manage a website. WordPress saves the input in Gutenberg as HTML, which is what you eventually also get out of the APIs. HTML in content APIs is generally not what you want if you want to use it in your favorite frontend framework, or in something that should render in something other than a web browser.

[Sanity](https://www.github.com/sanity-io/sanity) saves the content in the editor as [Portable Text](https://www.portabletext.org), which makes it portable across any markup, but also makes rich text queryable. It should be easy to create custom editorial experiences, with the custom types and inputs that makes sense in your project or organization, and it should be easy to take that content and fit it to whatever form of presentation.