Builder.io lets you build websites with a simple drag and drop interface. It also allows you to integrate seamlessly with existing components built by your developers. Let us explore how to create a page with a drag and drop interface powered by Builder
and Next.js
.
Before configuring a Builder
powered Next.js
project, you can also quickly bootstrap this setup in two simple commands.
npx create-next@latest
Select tailwind
in the configuration screen.
builder.io
automaticallynpm init builder.io --latest
After you run this command, you can open the webapp at localhost:3000
and everything will be automatically configured for you.
Now let us check out how to configure a Builder.io
project with Next.js/tailwind
.
app
directory)npx create-next
You can bootstrap a nextjs app with your preferred configuration. You can select tailwind
as your preferred styling library in the configuration screen.
Tailwind
manuallyLet us see how to configure Tailwind
manually in your existing nextjs project
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
tailwind.config.js
with the following content/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./app/**/*.{js,ts,jsx,tsx,mdx}",
"./pages/**/*.{js,ts,jsx,tsx,mdx}",
"./components/**/*.{js,ts,jsx,tsx,mdx}",
// Or if using `src` directory:
"./src/**/*.{js,ts,jsx,tsx,mdx}",
],
theme: {
extend: {},
},
plugins: [],
}
globals.css
@tailwind base;
@tailwind components;
@tailwind utilities;
Congrats! You have configured tailwind for your Next
app.
Builder
To get started with builder.io
, add Builder
dependencies.
npm install --save @builder.io/sdk @builder.io/react
Builder
API KeyYou need a valid api key to configure a Builder
project. You can get this easily by logging in/signing up for a builder.io
account. If you have a github account, you can easily create an account with one single click.
After logging into your builder.io
account, you can get your api keys by pressing Cmd + k
and typing api key
. Select, Copy API
key option from the command palette.
Alternatively, you can go to the account settings screen and get your API Key.
Builder
componentcomponents/builder.tsx
file and add the following contents// components/builder.tsx
"use client";
import { BuilderComponent, useIsPreviewing } from "@builder.io/react";
import { BuilderContent, builder } from '@builder.io/sdk';
import DefaultErrorPage from "next/error";
interface BuilderPageProps {
content?: BuilderContent;
}
// Replace with your Public API Key
builder.init(YOUR_API_KEY);
export function RenderBuilderContent({ content }: BuilderPageProps) {
// Call the useIsPreviewing hook to determine if
// the page is being previewed in Builder
const isPreviewing = useIsPreviewing();
// If `content` has a value or the page is being previewed in Builder,
// render the BuilderComponent with the specified content and model props.
if (content || isPreviewing) {
return <BuilderComponent content={content} model="page" />;
}
// If the `content` is falsy and the page is
// not being previewed in Builder, render the
// DefaultErrorPage with a 404.
return <DefaultErrorPage statusCode={404} />;
}
app/[...page]/page.tsx
file with following contents// Example file structure, app/[...page]/page.tsx
// You could alternatively use src/app/[...page]/page.tsx
import { builder } from "@builder.io/sdk";
import { RenderBuilderContent } from "../../components/builder";
// Replace with your Public API Key
builder.init(YOUR_API_KEY);
interface PageProps {
params: {
page: string[];
};
}
export default async function Page(props: PageProps) {
const content = await builder
// Get the page content from Builder with the specified options
.get("page", {
userAttributes: {
// Use the page path specified in the URL to fetch the content
urlPath: "/" + (props?.params?.page?.join("/") || ""),
},
// Set prerender to false to return JSON instead of HTML
prerender: false,
})
// Convert the result to a promise
.toPromise();
return (
<>
{/* Render the Builder page */}
<RenderBuilderContent content={content} />
</>
);
}
Most common way to use Builder
with your existing nextjs
app is to configure a page model. This enables Builder
to open your app in the browser visual editor.
To enable preview URL
http://localhost:3000
. Make sure to configure your port correctly if your nextjs app is using a different port.Most powerful feature of Builder
is an intuitive drag-and-drop page builder that integrates seamlessly with your existing codebase. You can use this drag-and-drop page builder without any coding knowledge.
To create Builder
Pages
+New
. Contact
. Builder
automatically generates an URL (/contact
) for you.Contact us
text block.Publish
to publish your changes. Now visit the http://localhost:3000/contact
page. You have created a Contact us
page without any coding.To update Builder
pages, you can use the builder.io
visual editor. Changes will be published instantly without any code changes. The best part about Builder
is your drag and drop pages will seamlessly integrate with custom components created by your developers.