|
| 1 | +# Composify Cloud Tutorial |
| 2 | + |
| 3 | +This guide will help you integrate the Composify Cloud editor and renderer into your existing project. |
| 4 | + |
| 5 | +## Install Composify |
| 6 | + |
| 7 | +Add Composify to your project with your preferred package manager: |
| 8 | + |
| 9 | +:::code-group |
| 10 | +```bash [npm] |
| 11 | +npm install @composify/react --save |
| 12 | +``` |
| 13 | + |
| 14 | +```bash [pnpm] |
| 15 | +pnpm add @composify/react |
| 16 | +``` |
| 17 | + |
| 18 | +```bash [yarn] |
| 19 | +yarn add @composify/react |
| 20 | +``` |
| 21 | +::: |
| 22 | + |
| 23 | +:::info[Vue support is on the way 🚀] |
| 24 | +Right now, Composify works with React only. Vue support is in the works and will be coming soon. |
| 25 | +::: |
| 26 | + |
| 27 | +## Register your components |
| 28 | + |
| 29 | +You _can_ use plain HTML elements, but Composify really shines when you use your own components. |
| 30 | + |
| 31 | +To make them available inside Composify, register them as described in the [component registration guide](/docs/catalog#registering-a-component). |
| 32 | + |
| 33 | +:::note[Tip] |
| 34 | +Keep your `Catalog.register(...)` calls in a central file that imports all your component registrations. This way, you only need to import one file in your app. |
| 35 | +::: |
| 36 | + |
| 37 | +For example, see our [components index file](https://github.com/composify-js/composify/blob/main/examples/nextjs/components/index.ts) and how it's imported in the [page component](https://github.com/composify-js/composify/blob/main/examples/nextjs/app/%5Bslug%5D/page.tsx#L1). |
| 38 | + |
| 39 | +## Render a page |
| 40 | + |
| 41 | +With our components registered, let's render a page. The `Renderer` takes the saved JSX and renders it using your components. |
| 42 | + |
| 43 | +:::code-group |
| 44 | +```jsx showLineNumbers [Next.js] |
| 45 | +/* app/pages/[slug]/page.tsx */ |
| 46 | +import '@/components'; |
| 47 | + |
| 48 | +import { Renderer } from '@composify/react/renderer'; |
| 49 | +import { notFound } from 'next/navigation'; |
| 50 | + |
| 51 | +export default async function Page({ |
| 52 | + params |
| 53 | +}: { |
| 54 | + params: Promise<{ slug: string }>; |
| 55 | +}) { |
| 56 | + const { slug } = await params; |
| 57 | + |
| 58 | + const url = `https://composify.cloud/[organization id]/${slug}`; |
| 59 | + const res = await fetch(url, { |
| 60 | + cache: 'no-store', |
| 61 | + }); |
| 62 | + |
| 63 | + const { content } = await res.json().catch(() => ({})); |
| 64 | + |
| 65 | + if (!content) { |
| 66 | + return notFound(); |
| 67 | + } |
| 68 | + |
| 69 | + return ( |
| 70 | + <Renderer source={content} /> |
| 71 | + ); |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +```tsx showLineNumbers [React Router] |
| 76 | +/* app/routes/pages/page.tsx */ |
| 77 | +import '~/components'; |
| 78 | + |
| 79 | +import { Renderer } from '@composify/react/renderer'; |
| 80 | +import { type LoaderFunctionArgs } from 'react-router'; |
| 81 | +import { useLoaderData } from 'react-router'; |
| 82 | + |
| 83 | +export async function loader({ params }: LoaderFunctionArgs) { |
| 84 | + const slug = params.slug ?? ''; |
| 85 | + |
| 86 | + const url = `https://composify.cloud/[organization id]/${slug}`; |
| 87 | + const res = await fetch(url, { |
| 88 | + cache: 'no-store', |
| 89 | + }); |
| 90 | + |
| 91 | + const { content } = await res.json().catch(() => ({})); |
| 92 | + |
| 93 | + if (!content) { |
| 94 | + throw new Response('', { status: 404 }); |
| 95 | + } |
| 96 | + |
| 97 | + return { slug, content }; |
| 98 | +} |
| 99 | + |
| 100 | +export default function Page() { |
| 101 | + const { slug, content } = useLoaderData<typeof loader>(); |
| 102 | + |
| 103 | + return ( |
| 104 | + <Renderer source={content} /> |
| 105 | + ); |
| 106 | +} |
| 107 | +``` |
| 108 | +::: |
| 109 | + |
| 110 | +## Set up the Editor |
| 111 | + |
| 112 | +Now for the fun part: setting up the visual editor. Use the `CloudEditor` component to create or update content directly in Composify Cloud. |
| 113 | + |
| 114 | +:::code-group |
| 115 | +```jsx showLineNumbers [Next.js] |
| 116 | +/* app/pages/composify-editor.tsx */ |
| 117 | +'use client'; |
| 118 | + |
| 119 | +import '@composify/react/style.css'; |
| 120 | +import '@/components'; |
| 121 | + |
| 122 | +import { CloudEditor } from '@composify/react/editor'; |
| 123 | + |
| 124 | +export default function Page() { |
| 125 | + return <CloudEditor />; |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +```jsx showLineNumbers [React Router] |
| 130 | +/* app/routes/composify-editor.tsx */ |
| 131 | +import '@composify/react/style.css'; |
| 132 | +import '~/components'; |
| 133 | + |
| 134 | +import { CloudEditor } from '@composify/react/editor'; |
| 135 | + |
| 136 | +export default function Page() { |
| 137 | + return <CloudEditor />; |
| 138 | +} |
| 139 | +``` |
| 140 | +::: |
| 141 | + |
| 142 | +## Wrap up |
| 143 | + |
| 144 | +That's it! 🎉 |
| 145 | + |
| 146 | +1. Go to your organization settings in Composify Cloud. |
| 147 | +2. Enter the editor URL you just set up. |
| 148 | +3. Start editing visually, and watch your changes sync into your app. |
0 commit comments