niklasfjeldberg / vue-strapi-blocks-renderer

A Vue renderer for the Strapi's Blocks rich text editor
https://www.npmjs.com/package/vue-strapi-blocks-renderer
MIT License
43 stars 1 forks source link
blocks renderer strapi vue

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

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.

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,
});