Skip to content

feat: added live preview svelte package #12250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

PeteJStewart
Copy link

Live Preview Package | Svelte

This is the live preview package for Svelte. Similar to the Vue & React Live Preview packages.
There are details about implementing the package within the README file. In general I have tried to keep it as close to the Vue implementation as possible.

The simplest example of how it can be used is:

<script lang="ts">
  import type { PageProps } from './$types';
  import { useLivePreview } from '@payloadcms/live-preview-svelte';
  import { readable } from 'svelte/store';
  import { PUBLIC_PAYLOAD_URL } from '$env/static/public';

  let { data }: PageProps = $props();
  let article = useLivePreview(data, {
    serverURL: PUBLIC_PAYLOAD_URL,
  });
</script>

<article>
	<h1>{$article.title}</h1>
	<div>{@html $article.content}</div>
</article>

I've tried to keep the DX as clean as possible and to replicate all feature found in the Vue package.
useLivePreview also returns loading as a readable rune, but in most cases it's not needed.
There is further information about that in the README.

There have been some discussions about this on Discord: https://discord.com/channels/967097582721572934/967097582721572937/1366094097890279515

What?

New Svelte Live Preview package

Why?

Because there wasn't one, and many people use Svelte and want this package.

How?

The package uses the @payloadcms/live-preview package and Svelte writable/readable store to extract the logic to a library package.

@jacobsfletch
Copy link
Member

Thanks for this @PeteJStewart! I made a few changes here, namely:

  1. Added the live-preview-svelte package to the list of acceptable commit scopes in .github/workflows/pr-title.yml
  2. Added it to the scripts property in the root package.json
  3. Aliased the path to the package in the paths property in the root tsconfig.base.json
  4. Added a reference to the package in the root tsconfig.json
  5. Added the package to the publish list in tools/releaser/src/lib/publishList.ts

Another change that is needed here is that docs need to go into the /docs/live-preview directory instead of the README. Can you do this?

Beyond that I'm hoping we can have a few Svelte developers test out a canary before it is released, but unfortunately I cannot publish a pre-release from your fork. I'll look into this, but we may have to open a PR from a direct branch instead, or mark as beta.

@PeteJStewart
Copy link
Author

Thanks @jacobsfletch, I'll add the docs tomorrow and remove the README. 👍

@PeteJStewart
Copy link
Author

PeteJStewart commented May 1, 2025

@jacobsfletch I believe this PR is ready now. I've added the Svelte documentation to /docs/live-preview.
I haven't removed the README, as I felt the extra info in there was useful, however, feel free to remove it if you want to keep the directory clean.
I personally like to see README files with the code, as I'm often looking at the code rather than the documentation, so it's nice to find some docs there, too. But I appreciate that's not the normal setup on Payload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants