|
1 |
| -# sanity-blocks-vue-component |
| 1 | +<div align="center"> |
| 2 | + <h1>Sanity Blocks Vue Component</h1> |
| 3 | + <p> |
| 4 | + <img alt="NPM version" src="https://img.shields.io/npm/v/sanity-blocks-vue-component?color=000&style=flat-square"> |
| 5 | + <img alt="NPM downloads" src="https://img.shields.io/npm/dm/sanity-blocks-vue-component?color=000&style=flat-square"> |
| 6 | + <img alt="GitHub Release Date" src="https://img.shields.io/github/release-date/rdunk/sanity-blocks-vue-component?color=000&style=flat-square"> |
| 7 | + <img alt="Vue version" src="https://img.shields.io/npm/dependency-version/sanity-blocks-vue-component/vue?color=000&style=flat-square"> |
| 8 | + <img alt="License" src="https://img.shields.io/npm/l/sanity-blocks-vue-component.svg?color=000&style=flat-square"> |
| 9 | + </p> |
| 10 | + </p> |
| 11 | + <p> |
| 12 | + <h3>A Vue component for rendering <a href="https://www.sanity.io/docs/block-content" _target="blank">block content</a> from Sanity.</h3> |
| 13 | + <br> |
| 14 | + <br> |
| 15 | + <br> |
| 16 | +</div> |
2 | 17 |
|
3 |
| -This branch is in beta. It is a complete rewrite supporting Vue 3 only. |
| 18 | +> **Notice**: This version is a complete rewrite for Vue 3. For Vue 2, see versions <1.0.0. |
4 | 19 |
|
5 |
| -A Vue component for rendering [block text](https://www.sanity.io/docs/schema-types/block-type) from [Sanity](https://www.sanity.io/). Allows you to pass other Vue components as custom serializers. |
| 20 | +A Vue component for easily rendering Sanity block content or [Portable Text](https://github.com/portabletext/portabletext). Allows you to use Vue components as serializers. |
| 21 | + |
| 22 | +## Install |
| 23 | + |
| 24 | +```bash |
| 25 | +$ npm i sanity-blocks-vue-component # or yarn add sanity-blocks-vue-component |
| 26 | +``` |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +Import directly into your component or view: |
| 31 | + |
| 32 | +```vue |
| 33 | +<template> |
| 34 | + <SanityBlocks :blocks="blocks" :serializers="serializers" /> |
| 35 | +</template> |
| 36 | +
|
| 37 | +<script> |
| 38 | +import { SanityBlocks } from 'sanity-blocks-vue-component'; |
| 39 | +import CustomComponent from 'CustomComponent.vue'; |
| 40 | +
|
| 41 | +export default { |
| 42 | + components: { SanityBlocks }, |
| 43 | + setup() { |
| 44 | + const blocks: [...]; // Sanity block text |
| 45 | + const serializers: { |
| 46 | + types: { |
| 47 | + custom: CustomComponent, |
| 48 | + }, |
| 49 | + }; |
| 50 | + return { blocks, serializers }; |
| 51 | + } |
| 52 | +} |
| 53 | +</script> |
| 54 | +``` |
| 55 | + |
| 56 | +Or install globally: |
| 57 | + |
| 58 | +```ts |
| 59 | +import { SanityBlocks } from 'sanity-blocks-vue-component'; |
| 60 | +Vue.component(SanityBlocks); |
| 61 | +``` |
| 62 | + |
| 63 | +## Props |
| 64 | + |
| 65 | +The following props can be passed to the component. |
| 66 | + |
| 67 | +| Prop | Required | Description | Type | |
| 68 | +| ------------- | -------- | -------------------------------------------------- | ------ | |
| 69 | +| `blocks` | Yes | Block content retrieved from Sanity. | Array | |
| 70 | +| `serializers` | No | Any custom serializers you want to use. See below. | Object | |
| 71 | + |
| 72 | +## Serializer Prop |
| 73 | + |
| 74 | +Serializers are the functions used for rendering block content. They can be defined either as a string (e.g. `div`) or as a Vue Component (see below for more detail). This package comes with default serializers that will work for rendering basic block content, but you may pass a `serializer` prop to override or extend the default serializers. Any object passed as a prop will be merged with the default serializers object. |
| 75 | + |
| 76 | +| Property | Description | |
| 77 | +| ----------- | -------------------------------------- | |
| 78 | +| `types` | Object of serializers for block types. | |
| 79 | +| `marks` | Object of serializers for marks. | |
| 80 | +| `styles` | Object of serializers for styles. | |
| 81 | +| `list` | Serializer for list containers. | |
| 82 | +| `listItem` | Serializer for list items. | |
| 83 | +| `hardBreak` | Serializer for hard breaks. | |
| 84 | + |
| 85 | +## Using Component Serializers |
| 86 | + |
| 87 | +The most common use case is defining serializers for custom block types and marks, using the `types` and `marks` serializer properties respectively. For example, if you have a block of `_type` `custom`, you can add a property to the `serializers.types` object with the key `custom` and a value of the Vue component that should serialize this block. |
| 88 | + |
| 89 | +When using a custom Vue component as a serializer, all properties of the block or mark object (excluding `_key` and `_type`) will be passed as [props](https://v3.vuejs.org/guide/component-props.html). **To access the data, you should define the correpsonding props in your component.** For mark serializers, you can also use [slots](https://v3.vuejs.org/guide/component-slots.html) to access the mark text or content. |
6 | 90 |
|
7 | 91 | ## License
|
8 | 92 |
|
|
0 commit comments