Skip to content

niklasfjeldberg/vue-strapi-blocks-renderer

Repository files navigation

Vue Strapi Blocks Renderer

npm version npm downloads License codecov ci

Easily render the content of Strapi's new Blocks rich text editor in your Vue frontend.

Based on @strapi/blocks-react-renderer

Features

  • No dependencies
  • Utilizes Vue futures
  • Custom block types and modifiers
  • Works with other editors that Strapi Blocks
  • Typescript support

Installation

Install the Blocks renderer and its peer dependencies:

npm install vue-strapi-blocks-renderer vue

Basic usage

After fetching your Strapi content, you can use the BlocksRenderer component to render the data from a blocks attribute. Pass the array of blocks coming from your Strapi API to the content prop:

import { StrapiBlocks, type BlocksContent } from 'vue-strapi-blocks-renderer';

// Content should come from your Strapi API
const content: BlocksContent = [
  {
    type: 'paragraph',
    children: [{ type: 'text', text: 'A simple paragraph' }],
  },
];

const VNode = StrapiBlocks({ content: content });
<template>
  <VNode />
</template>

Or

import { StrapiBlocks } from 'vue-strapi-blocks-renderer';
<template>
  <StrapiBlocks :content="content" :modifiers="modifiers" :blocks="blocks" />
</template>

Custom components

You can provide your own Vue components to the renderer, both for blocks and modifier. They will be merged with the default components, so you can override only the ones you need.

  • Blocks are full-width elements, usually at the root of the content. The available options are:
    • paragraph
    • heading (receives level)
    • list (receives format)
    • quote
    • code (receives plainText)
    • image (receives image)
    • link (receives url)
  • Modifiers are inline elements, used to change the appearance of fragments of text within a block. The available options are:
    • bold
    • italic
    • underline
    • strikethrough
    • code

To provide your own components, pass an object to the blocks and modifiers props of the renderer. For each type, the value should be a React component that will receive the props of the block or modifier. Make sure to always render the children, so that the nested blocks and modifiers are rendered as well.

import { h } from 'vue';

import {
  StrapiBlocks,
  type BlocksComponents,
  type ModifiersComponents,
} from 'vue-strapi-blocks-renderer';

const userBlocks: BlocksComponents = {
  // Will include the class "mb-4" on all paragraphs
  paragraph: (props) => h('p', { class: 'mb-4' }, props.children),
};

const userModifier: ModifiersComponents = {
  // Will include the class "text-red" on all bold text
  bold: (props) => h('strong', { class: 'text-red' }, props.children),
};

const VNode = StrapiBlocks({
  content: content,
  modifier: userModifier,
  blocks: userBlocks,
});