Vue components
Render Mojimoto rich text, images and links in Vue 3, and fetch entries with composables, using @mojimoto/vue.
@mojimoto/vue turns the JSON from the Delivery API into Vue elements. It has components for rich text, plain text, images and links, and composables for fetching in the browser. It needs Vue 3.3 or newer.
In Nuxt, install @mojimoto/nuxt instead. It registers these components for you and adds composables that render on the server.
This page describes version 0.3.2.
Install
npm i @mojimoto/vue @mojimoto/clientSet it up
The components work with no setup. Install the plugin when you want the composables, or one link resolver for the whole app.
import { createApp } from 'vue'
import { createClient } from '@mojimoto/client'
import { createMojimoto } from '@mojimoto/vue'
import App from './App.vue'
const cms = createClient({
endpoint: 'https://mojimoto.com/api/v1',
project: 'acme',
token: 'mjmt_your_read_token',
})
createApp(App)
.use(createMojimoto({ client: cms, linkResolver: (link) => `/${link.uid}` }))
.mount('#app')| Option | Type | Description |
|---|---|---|
client |
MojimotoClient |
The client the composables use. |
linkResolver |
(link) => string |
Turns a link to another entry into an href, for every component. |
richTextComponents |
MojiRichTextComponents |
Your own output for rich text nodes, for every MojiRichText. |
To set the same values for one part of the app, call provideMojimoto({ ... }) in a component's setup.
Warning: This client runs in the browser, so its token is public. Give it a token with only the
readability.
A first example
<script setup lang="ts">
import { MojiImage, MojiRichText, type MojimotoDocument, type MojiRichTextValue } from '@mojimoto/vue'
type Media = { url: string; alt: string | null; width: number | null; height: number | null }
type Post = MojimotoDocument<'post', { title: string; cover: Media | null; body: MojiRichTextValue }>
defineProps<{ post: Post }>()
</script>
<template>
<article>
<h1>{{ post.data.title }}</h1>
<MojiImage :field="post.data.cover?.url" :alt="post.data.cover?.alt ?? ''" />
<MojiRichText :field="post.data.body" />
</article>
</template>The types here are written by hand. Generated types give you the same for your whole model.
Render rich text
MojiRichText renders a rich_text field as plain HTML elements: p, h1 to h6, ul, ol, pre, table, img, strong, em and a. It has no wrapper element of its own.
| Prop | Type | Description |
|---|---|---|
field |
MojiRichTextValue or null |
The field value. |
components |
MojiRichTextComponents |
Your own output for any block or span. |
linkResolver |
(link) => string |
Turns a link to another entry into an href. |
The fallback slot is rendered when the field is empty.
Override only the nodes you want to change. Each entry returns a VNode or a string, and you do not need to set key.
<script setup lang="ts">
import { h, type VNode } from 'vue'
import { MojiRichText, type MojiRichTextComponents } from '@mojimoto/vue'
const components: MojiRichTextComponents<VNode | string> = {
heading2: ({ children }) => h('h2', { class: 'section-title' }, children),
image: ({ node }) => h('img', { src: node.url, alt: node.alt ?? '', loading: 'lazy' }),
}
</script>
<template>
<MojiRichText :field="post.data.body" :components="components" :link-resolver="(link) => `/${link.uid}`">
<template #fallback>
<p>Nothing here yet.</p>
</template>
</MojiRichText>
</template>The entries and what each receives are listed under Change the HTML for one node. A component's own props win over the plugin's values, and those win over the built-in output.
Two things to know:
- A soft line break is a
\nin the text. It only shows if the element haswhite-space: pre-linein your CSS. - An
embedblock is rendered withinnerHTML, using the HTML stored in the field. Overrideembedif that HTML can come from somewhere you do not trust.
Render plain text
MojiText renders a rich text field with the formatting removed, for a heading or a summary.
<p class="summary"><MojiText :field="post.data.summary" fallback="No summary yet" /></p>| Prop | Type | Description |
|---|---|---|
field |
MojiRichTextValue or null |
The field value. |
separator |
string |
Placed between blocks. A space by default. |
fallback |
string |
Used when the field is empty. |
as |
tag name or component | Wraps the text in an element. Without it the text is rendered bare. |
Note:
asworks when the app runs, but its type is wrong today and type checking rejects any value. In TypeScript, wrapMojiTextin your own element as above.
Render an image
MojiImage renders an img. Its field is a URL string, or an image block from a rich text field. A media field is delivered as an object, so pass its url:
<MojiImage
:field="post.data.cover?.url"
:alt="post.data.cover?.alt ?? ''"
:width="post.data.cover?.width ?? undefined"
:height="post.data.cover?.height ?? undefined"
loading="lazy"
/>| Prop | Type | Description |
|---|---|---|
field |
string, an image block or null |
What to show. Nothing is rendered when it is empty. |
alt |
string |
The alt text. For an image block it falls back to the block's own alt, then to an empty string. |
Any other attribute, such as class, loading, width and height, lands on the img.
Render a link
MojiLink renders a link value as an a, with its content in the default slot. MojiRichText uses it for every link in the text. There is no link field type, so use it directly only when you hold a link value yourself.
<MojiLink :field="{ link_type: 'Web', url: 'https://example.com', target: '_blank' }" class="button">
Visit the site
</MojiLink>| Prop | Type | Description |
|---|---|---|
field |
MojiLinkValue or null |
The link. |
linkResolver |
(link) => string |
Turns a link to another entry into an href. |
as |
tag name or component | Render through this in place of a. It receives href, target and rel. |
A link with target: '_blank' gets rel="noopener noreferrer". Other attributes, such as class, land on the element.
A component passed to as must take href, as NuxtLink does. Vue Router's RouterLink takes to, so give it a wrapper that maps one to the other. The prop is typed as a string today, so TypeScript accepts a tag name and rejects a component, although a component works when the app runs.
resolveHref(link, linkResolver) is exported if you only want the URL.
Render blocks
There is no slice zone component for Vue. A blocks field is an array of { id, type, data }, so match each type to a component yourself:
<script setup lang="ts">
import type { Component } from 'vue'
import type { MojimotoBlock } from '@mojimoto/client'
import HeroBlock from './HeroBlock.vue'
import QuoteBlock from './QuoteBlock.vue'
defineProps<{ sections: MojimotoBlock[] }>()
const components: Record<string, Component> = { hero: HeroBlock, quote: QuoteBlock }
</script>
<template>
<template v-for="block in sections" :key="block.id">
<component :is="components[block.type]" v-if="components[block.type]" :block="block" />
</template>
</template>Fetch with composables
The composables use the plugin's client. They return refs straight away and fill them when the response arrives, so they suit an app rendered in the browser.
<script setup lang="ts">
import { useMojiQuery } from '@mojimoto/vue'
const { data: posts, error, loading } = useMojiQuery({ type: 'post', sort: '-updated_at', perPage: 5 })
</script>
<template>
<p v-if="loading">Loading</p>
<p v-else-if="error">{{ error.message }}</p>
<ul v-else>
<li v-for="post in posts" :key="post.id">{{ post.data.title }}</li>
</ul>
</template>| Composable | Returns |
|---|---|
useMojiQuery(options) |
{ data, error, loading }, each a ref. data is the array of entries. The options are those of the client's query. |
useMojiDocument(type, uid, options) |
The same refs. data is the entry, or null when there is none. The options are lang, preview and depth. |
useMojimotoClient() |
The plugin's client. Throws if there is none. |
useMojimotoContext() |
Everything the plugin was given. |
Pass useMojiQuery a function to fetch again when something reactive changes. The request in flight is cancelled.
const { data } = useMojiQuery(() => ({ type: 'post', uid: String(route.params.uid) }))useMojiDocument takes plain values and fetches once.
Other exports
asText and asHTML are re-exported from @mojimoto/richtext. MojimotoInjectionKey is the key the plugin provides under. The types are MojiRichTextValue, MojiLinkValue, MojiLinkResolver, MojiRichTextComponents, MojimotoDocument, MojimotoClient, MojimotoContextValue and AsyncState.
Something wrong or missing on this page? Tell us and we will fix it.