When you want your custom component to accept other blocks, configure your component with children. The following video demonstrates a custom tabs component that accepts Builder blocks as content. Each tab receives a unique block that the user drags and drops in.
To get the most our of this tutorial, you should have the following:
- An app you've integrated with Builder
- Familiarity with using custom components in Builder
For more details on child-related options when working with child components, visit the canHaveChildren, childRequirements, and defaultChildren sections of the registerComponent() documentation.
Tip: Builder's Gen 2 SDKs don't require withChildren()
. For more information on the SDKs, visit SDK Comparision.
First, configure the component to receive and render children, based on your framework:
Gen 2 SDK by Framework | Prepare code with |
---|---|
React | Call |
Vue and Svelte | Add |
Qwik | |
Solid.js | Use the |
Then, make sure to add the children helper canHaveChildren: true
component option when registering.
For Gen 1 or Gen 2, now your component should show up in the Visual Editor with no further configuration in the code.
The following video shows the Hero block in the Custom Components section of the Insert tab. When the Hero block is on the page in the Visual Editor, you can drag and drop more blocks and drop them into the Hero block, along with the other children defined in code.
To use a registered component with children, do the following:
- If you're still developing your component, change the Preview URL to localhost with the port you're using for your local app and the page name. In this example, the URL is
http://localhost:3000/cc-children
. - Drag and drop your custom component into the work area.
- Drag other blocks onto your custom component.
In the following video, after the user drops a Columns block into the custom component, they open the Layers tab to show the Columns block nested within the Hero block.
For React, use this technique if you want to manually specify details about your custom component's children. This section shows you how to to create a a Tabs component that renders multiple sets of children. Refer to the corresponding comments in the code for each step.
- Import
React
,useState
,BuilderBlocks
, andBuilder
along with your other JavaScript imports. - Create your component. This example creates a
Tabs
component where each tab has a label as well as content that displays when that tab is active. - Configure
BuilderBlocks
so that Builder knows what makes up your component. - Register your component using
registerComponent()
. Specify the required values ofname
andtype
as well assubFields
. ThesubFields
are label and content and they get their values from defaultValue. When you create a new tab in the Visual Editor, the default label isNew Tab 1
while the content is empty, because of the empty array.
// 1. Add imports
import React, { useState } from 'react';
import { BuilderBlocks, Builder } from '@builder.io/react';
// 2. Create your component.
// This is a tabs component with some basic styles. When the user clicks
// on a tab, that content becomes active.
function Tabs(props) {
const [activeTab, setActiveTab] = useState(0);
return (
<>
<div
style={{
display: 'flex',
overflow: 'auto',
}}
>
{props.tabs?.map((item, index) => (
<span
key={index}
style={{
padding: 20,
color: activeTab === index ? 'blue' : '#000',
}}
onClick={() => {
setActiveTab(index);
}}
>
{item.label}
</span>
))}
</div>
{props.tabs?.length && (
<BuilderBlocks
parentElementId={props.builderBlock.id}
dataPath={`component.options.tabs.${activeTab}.content`}
blocks={props.tabs[activeTab].content}
/>
)}
</>
);
}
// 3. Configure BuilderBlocks. The above section with BuilderBlocks
// tells Builder what to expect:
// the parent id, the path to the child component,
// and what the blocks should be made of.
// Here, the blocks are made of the content of the active tab
// 4. Register your component. This component is called Tabs, is of type
// list and contains two subFields: a label and the content.
// As a best practice, provide a defaultValue. The default label is
// "Tab 1" and the content is an empty array.
Builder.registerComponent(Tabs, {
name: 'Tabs',
inputs: [
{
name: 'tabs',
type: 'list',
subFields: [
{
name: 'label',
type: 'text',
defaultValue: 'New tab',
},
{
name: 'content',
type: 'uiBlocks',
defaultValue: [],
},
],
defaultValue: [
{
label: 'Tab 1',
content: [],
},
],
},
],
});
This section explains how to configure custom components to accept children with the Gen 1 and Gen 2 SDKs. By implementing editable regions within custom components, users can dynamically add and customize content within their Builder projects.
To use a custom component with multiple sets of children in the Visual Editor, take the following steps:
- In the Insert tab, expand the Custom Components section.
- Drag and drop your component onto the work area.
- With your component selected, click the Edit button and add sets of children if needed.
- Drag other blocks, for example Text or Image blocks, into the Add Block region of your component.
To customize your components even further, you can leverage Builder's Input Types.
For more examples of custom components with multiple sets of children, check out: