Client CraftClient Craft HubStart free

Make a custom block editable in the Visual Builder

On this page
Updated 24 Nov 20263 min read

Take your own Block Grid block from content-only to fully styled in the Visual Builder: element type, opt-in, wrapped view and a hint.

Plan: Free Visual Builder package

Goal: a "Quote" block of your own that editors can place, edit in place and style (spacing, background, border, per device) in the Visual Builder, with its accent toggle clickable on the canvas. You write no styling code: the Visual Builder's wrapper applies the styles.

Before you start #

  • Visual Builder installed and turned on.
  • A Block Grid data type for your pages, here called "Page Grid".
  • ModelsBuilder in SourceCodeManual or SourceCodeAuto mode, so models compile with the site. (With InMemoryAuto, skip the model step and use the generated QuoteBlock model the same way.)

Step by step #

  1. 1
    Create the element typeIn Settings › Document Types, create an element type Quote block (alias quoteBlock) with:
    • Quote (quote, Textarea)
    • Author (author, Textstring)
    • Accent (accent, Toggle)

    In Settings › Data types › Page Grid, add Quote block to the allowed blocks. Don't give it a settings type; the opt-in adds one.

  2. 2
    Opt the grid in to stylesAdd a composer to the website project:
    csharp
    1using ClientCraft.VisualBuilder.Umbraco.Schema;
    2using Umbraco.Cms.Core.Composing;
    3using Umbraco.Cms.Core.DependencyInjection;
    4
    5namespace MySite.VisualBuilder;
    6
    7public sealed class VisualBuilderSetup : IComposer
    8{
    9 public void Compose(IUmbracoBuilder builder) => builder.AddVisualBuilderSite(site => site.Styles("Page Grid"));
    10}

    Start the site once. The Quote block now has the shared Element appearance settings type, and every other block of Page Grid gets its style field too. Look for "Visual Builder site setup" in the log if something is off.

  3. 3
    Generate the modelsGenerate ModelsBuilder models (Settings › Models Builder › Generate models) and build the site, so QuoteBlock exists.
  4. 4
    Register the tag helpersIn Views/_ViewImports.cshtml:
    razor
    1@addTagHelper *, ClientCraft.VisualBuilder.Umbraco
  5. 5
    Write the block viewCreate Views/Partials/blockgrid/Components/quoteBlock.cshtml:
    razor
    1@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockGridItem<QuoteBlock>>
    2@{
    3 var quote = Model.Content;
    4}
    5<ccvb-block block="Model" tag="figure">
    6 <blockquote class="quote@(quote.Accent ? " quote--accent" : "")" data-property="accent">
    7 <p>@quote.Quote</p>
    8 </blockquote>
    9 @if (!string.IsNullOrWhiteSpace(quote.Author))
    10 {
    11 <figcaption class="quote-author">@quote.Author</figcaption>
    12 }
    13</ccvb-block>
    • <ccvb-block> renders the figure with the styles editors set.
    • data-property="accent" lets editors click the blockquote to get the Accent toggle on the block's bar.
    • The text fields need no hint: the builder finds them on the page.
  6. 6
    Render the styles in the layoutIn your layout's <head>, once:
    razor
    1<ccvb-assets />
  7. 7
    Try itBuild and run the site, open a page in the builder and insert Quote block. Type the quote in place, select the blockquote to flip Accent, then open Style and set padding and a background colour. Switch to Phone and reduce the padding there.
  8. 8
    PublishSelect Save and publish and check the public page: the figure carries the styles and your markup inside is unchanged.

Result #

Editors place, edit and style the Quote block like any built-in block, per device, and the accent toggle is one click away on the canvas. Your view stays focused on content.

TipWant the block in the design library too? Allow the Component type in Page Grid, then use Make component on a styled quote. See Make a component and reuse it on many pages.
Was this article helpful?