Made in Builder.io

Watch the biggest Figma-to-code launch of the year

Builder.io logo
Talk to Us
Platform
Developers
Talk to Us

Blog

Home

Resources

Blog

Forum

Github

Login

Signup

×

Visual CMS

Drag-and-drop visual editor and headless CMS for any tech stack

Theme Studio for Shopify

Build and optimize your Shopify-hosted storefront, no coding required

Resources

Blog

Get StartedLogin

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 users drags and drops in.

To get the most our of this tutorial, you should have the following:

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 requirewithChildren(). 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 FrameworkPrepare code with

React

Call props.children wherever the children should be rendered — just like in the React Gen 1 SDK.

Vue and Svelte

Add <slot></slot> wherever you want your children to be. For more detail, visit the Vue document on Slots and the Svelte special elements entry on <slot>.

Qwik

Add Qwik's <Slot/> component. Import { Slot } from '@builder.io/qwik';

Solid.js

Use the children helper (See SolidJS docs

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:

  1. 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.
  2. Drag and drop your custom component into the work area.
  3. 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.

Register inputs for each of your editable sections of type blocks:

// Register the component and its inputs
Builder.registerComponent(HeroWithBuilderChildren, {
  name: 'Hero',
  inputs: [
    // Input for section A editable region
    {
      name: 'sectionA',
      type: 'blocks', // Specify type of blocks
      hideFromUI: true,
      helperText: 'This is an editable region.',
      defaultValue: [
        {
          '@type': '@builder.io/sdk:Element',
            component: {
              name: 'Text',
              options: {
                text: 'Section A Editable in Builder...',
            },
          },
          responsiveStyles: {
            large: {
              // Styles for the editable section
            },
          },
        },
      ],
    },
    // Input for section B editable region
    {
      name: 'sectionB',
      type: 'blocks',
      hideFromUI: true,
      helperText: 'This is an editable region.',
      defaultValue: [
        {
          '@type': '@builder.io/sdk:Element',
          component: {
            name: 'Text',
            options: {
              text: 'Section B Editable in Builder...',
            },
          },
          responsiveStyles: {
            large: {
              // Styles for the editable section
            },
          },
        },
      ],
    },
  ],
});

Use builder-blocks-outlet component to render those blocks within your component template:

@Component({
  selector: 'my-custom-section',
  template: `
    <h2>Section A</h2>
    <!-- Render section A editable region using 
    builder-blocks-outlet component -->
    <builder-blocks-outlet
      [blocks]="sectionA"
      [builderState]="builderState"
      [builderBlock]="builderBlock"
      dataPath="component.options.sectionA"
    ></builder-blocks-outlet>
    <h2>Section B</h2>
    <!-- Render section B editable region using 
    builder-blocks-outlet component -->
    <builder-blocks-outlet
      [blocks]="sectionB"
      [builderState]="builderState"
      [builderBlock]="builderBlock"
      dataPath="component.options.sectionB"
    ></builder-blocks-outlet>
  `,
})
export class MyCustomSection implements OnChanges {
  @Input()
  sectionA = null;

  @Input()
  sectionB = null;

  @Input()
  builderState = null;

  @Input()
  builderBlock = null;
}

To use a custom component with multiple sets of children in the Visual Editor, take the following steps:

  1. In the Insert tab, expand the Custom Components section.
  2. Drag and drop your component onto the work area.
  3. With your component selected, click the Edit button and add sets of children if needed.
  4. 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:


Looking to hire a third party to help with your project?

Submit a project request and our partnerships team will reach out to connect you with an Expert from our partner ecosystem.

Connect with us

Was this article helpful?

Product

Visual CMS

Theme Studio for Shopify

Sign up

Login

Featured Integrations

React

Angular

Next.js

Gatsby

Get In Touch

Chat With Us

Twitter

Linkedin

Careers

© 2020 Builder.io, Inc.

Security

Privacy Policy

Terms of Service

Newsletter

Get the latest from Builder.io

By submitting, you agree to our Privacy Policy