@prismicio/svelte v0
Overview
@prismicio/svelte
is the official Prismic package for creating web apps with Prismic and Svelte.
To learn to develop with Prismic and Svelte, see our user-friendly Svelte guide.
Dependencies and requirements
This package can only be used in a Svelte project.
Installation
Install this package in your Svelte project:
npm install --save-dev @prismicio/svelte
Example
Here is an example in a Svelte project. The App.svelte
file will import the SliceZone
and use it to render slices as Svelte components.
<script>
import * as prismic from "@prismicio/client";
import { SliceZone } from "@prismicio/svelte";
// Import Slices components and map them to API IDs
import ExampleSlice from "./slices/ExampleSlice.svelte";
const components = {
example_slice_a: ExampleSlice,
};
// Query a document from Prismic
const client = prismic.createClient(
"example-prismic-repo",
);
const promise = client.getFirst();
</script>
{#await promise}
<p>Loading Slices</p>
{:then document}
<SliceZone slices={document.data.body} {components} />
{:catch error}
<pre>{JSON.stringify(error.message, null, 2)}</pre>
{/await}
Each slice component receives a slice
object as a prop.
<script>
export let slice;
</script>
<h1>{slice.primary.example_key_text_in_slice}</h1>
Usage
@prismicio/svelte
exports the following components to render content from the Prismic API.
<SliceZone>
<script>
import { SliceZone } from "@prismicio/svelte";
import {
ExampleSliceA,
ExampleSliceB,
} from "$lib/slices";
export let data;
</script>
<SliceZone
slices={data.document.data.body}
components={{
example_slice_a: ExampleSliceA,
example_slice_b: ExampleSliceB,
}}
context={{ foo: "bar" }}
/>
The SliceZone
is a Svelte component that renders an array of Prismic slices with corresponding slice components.
The SliceZone
accepts five props:
slices array | An array of slices from the Prismic API. |
components object | An object mapping Svelte slice components to API IDs. |
context object | Arbitrary data to pass to slice components. |
defaultComponent object | The component that the |
dev boolean | Should specify whether the application is in
development ( |
<PrismicLink>
<script>
import { PrismicLink } from "@prismicio/svelte";
export let data;
</script>
<!-- With default text property -->
<PrismicLink field={document.data.example_link} />;
<!-- Overriding with custom children -->
<PrismicLink field={document.data.example_link}>
Example Link
</PrismicLink>
PrismicLink
is a Svelte component that renders a link field, content relationship field, or document from the Prismic API as an a
element. PrismicLink
accepts all standard a
attributes and SvelteKit link options as props. It also accepts the following Prismic-specific props:
field object (required) | A link field, content relationship field, or document from the Prismic API. |
linkResolver function | A function that accepts a document object and returns a URL path, which will be used to resolve the URL for internal links. This option is unnecessary if your client is configured with a Route Resolver. More info. |
target string | An HTML target attribute. If left |
rel string | An HTML rel attribute. If undefined and target is
|
<PrismicRichText>
<script>
import { PrismicRichText } from "@prismicio/svelte";
import Title from "../components/Title.svelte";
export let data;
const components = {
h1: Title,
};
</script>
<PrismicRichText
field={data.document.data.example_rich_text}
{components}
/>
PrismicRichText
is a Svelte component that renders a title or rich text field from the Prismic API. PrismicRichText
accepts the following props:
field object (required) | A rich text or title field from the Prismic API. |
components object | An object to render elements with Svelte components.
The key for each property should be a rich text
element type (e.g. |
<PrismicText>
<script>
import { PrismicText } from "@prismicio/svelte";
export let data;
</script>
<PrismicText field={data.document.data.example_rich_text} />
PrismicText
is a Svelte component that renders a title or rich text field from the Prismic API as plain text. PrismicText
accepts the following props:
field object (required) | A rich text or title field from the Prismic API. |
separator string | An optional separator used to split blocks. Defaults
to |
<PrismicImage>
<script>
import { PrismicImage } from "@prismicio/svelte";
export let data;
</script>
<PrismicImage field={data.document.data.example_image} />
PrismicImage
is a Svelte component that renders an image field from the Prismic API as an optimized image with alt text and an automatically-generated srcset
attribute.
| An image field from the Prismic API. |
| An object of Imgix URL API parameters to transform the image, e.g. |
| Widths to be used for the |
| Pixel densities to be used for the srcset attribute, e.g. |
| An empty string to declare an image as decorative (such as a patterned background). Any other value is invalid. |
| An empty string to declare an image as decorative (such as a patterned background) only if no alt text has been defined in Prismic. Any other value is invalid. |
<PrismicEmbed>
<script>
import { PrismicEmbed } from "@prismicio/svelte";
export let data;
</script>
<PrismicEmbed field={data.document.data.example_embed} />
PrismicEmbed
is a Svelte component that renders an embed field from the Prismic API as an iframe
element. PrismicEmbed
accepts one prop:
field object (required) | An embed field from the Prismic API. |