Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Peter Laurie 54 posts 148 karma points
    1 week ago
    Peter Laurie
    0

    Umbraco 15 - Block Grid - custom Block grid previews with slots

    Hi, I am in the process of converting Block Grids from Umbraco 13 to Umbraco 15.

    I have followed the instructions to create a separate project to create custom preview block grids for the CMS using Typescript, Vite and Lit.

    So far so good, using 'npm run build' to compile in to /wwwroot/Plug_ins.

    The big issue I have come up against, and so far I have come up with no solutions is adding slots in the new typescript code that allowed me to agg content in layouts, in left and right areas. My code so far is simple not rendering "areas"/"slots" that I can add further content in to like, rich text or other block grids. This is becoming quite frustrating. I have been reading up on slots in web components, but to no avail.

    In Umbraco 13 - the code to render blocks was this:

    <div ng-controller="twoColumnSectionController">
      <section class="cmt-section-wrap {{containerHPadding}} {{alignItems}} {{bgcolor}}">
        <div class="{{theContainer}}">
          <umb-block-grid-render-area-slots></umb-block-grid-render-area-slots>
        </div>
      </section> 
    </div>
    

    or this, using slot instead:

    <div ng-controller="twoColumnSectionController">
      <section class="cmt-section-wrap {{containerHPadding}} {{alignItems}} {{bgcolor}}">
        <div class="{{theContainer}}">
          <slot name="left"></slot>
        </div>
      </section> 
    </div>
    

    Turning to This example code for Umbraco 15 - for a custom block grid cms preview,, the slots do not render, or allow me to add further content within the layout custom block, with two columns/areas:

    import { html, customElement, property, css } from '@umbraco-cms/backoffice/external/lit';
    import { UmbElementMixin } from '@umbraco-cms/backoffice/element-api';
    import type { UmbBlockDataType } from '@umbraco-cms/backoffice/block';
    import type { UmbBlockEditorCustomViewElement } from '@umbraco-cms/backoffice/block-custom-view';
    
    const elementName = "columns-66-custom-view";
    
    // eslint-disable-next-line local-rules/enforce-umb-prefix-on-element-name
    @customElement(elementName)
    // eslint-disable-next-line local-rules/umb-class-prefix
    export class Columns66
        extends UmbElementMixin(UmbLitElement) 
        implements UmbBlockEditorCustomViewElement
    {
        @property({ attribute: false })
        content?: UmbBlockDataType;
    
        @property({ attribute: false })
        settings?: UmbBlockDataType;
    
        override render()
        {
            return html`
                <div class="${this.settings?.align ? 'align-' + this.settings?.align : undefined}">           
                    <div slot="left">
                            Needs content left
                        </div>
                        <umb-block-grid-render-area-slots></umb-block-grid-render-area-slots>
                </div>
                <div class="${this.settings?.align ? 'align-' + this.settings?.align : undefined}">
                    <div slot="right">
                            Needs content right
                        </div>
                </div>
            `;
        }
    
        static override styles = [
            css`
                :host {
                    display: block;
                    height: 100%;
                    box-sizing: border-box;
                    background-color: #dddddd;
                    border-radius: 9px;
                    padding: 12px;
                }
    
                .align-center {
                    text-align: center;
                }
                .align-right {
                    text-align: right;
                }
            `,
        ];
    }
    
    export default Columns66;
    
    declare global
    {
        interface HTMLElementTagNameMap
        {
            [elementName]: Columns66;
        }
    }
    

    This is the display that is rendered in the cms:

    rendered block grid in the cms

    Please can someone assist, who has successfully rendered slots and content within block grid items in the cms, and advise on the correct code to use.

    Thank you.

    Kind regards,

    Pete

  • Afreed 64 posts 277 karma points
    1 week ago
    Afreed
    2

    Hi Peter,

    In web components, slots are designed to accept content dynamically passed from the parent component, not to define areas within the component itself as your current code suggests.

    You could use something like this for your case:

    import { UmbBlockGridTypeModel } from '@umbraco-cms/backoffice/block-grid';
    
     @state()
        blockType?: UmbBlockGridTypeModel;
    
        override render() {
            return html`
                <h5>My Custom View</h5>
                <umb-block-grid-areas-container
                .areas="${this.blockType?.areas ?? []}"
                .areaGridColumns="${this.blockType?.areaGridColumns}">
                </umb-block-grid-areas-container>
    
            `;
        }
    

    you can also pass in

    _styleElement?: HTMLLinkElement;
    

    Hope this helps

  • Peter Laurie 54 posts 148 karma points
    1 week ago
    Peter Laurie
    0

    Hi Afreed,

    Thank you for your feedback. Yes, this does indeed generate the areas so you can edit within them, thank you. Just also wondered, if I am able to target a single area, for example an area with an alias of left or right, can this be done as well?

    Thanks again,

    Kind regards,

    Pete

  • Afreed 64 posts 277 karma points
    1 week ago
    Afreed
    1

    Yes, you could use something like this:

    <umb-block-grid-entries
                    part="area"
                    class="umb-block-grid__area"
                    .areaKey=${this.blockType?.areas[0].key}
                    .layoutColumns=${this.blockType?.areas[0].columnSpan}></umb-block-grid-entries>
    
  • Peter Laurie 54 posts 148 karma points
    1 week ago
    Peter Laurie
    100

    Hi again Afreed,

    That works, thank you. For my starter custom block grid with just two columns, I can get the left and right areas using 0 and 1 like so:

    <umb-block-grid-entries
        part="area"
        class="umb-block-grid__area"
        .areaKey=${this.blockType?.areas[0].key}
        .layoutColumns=${this.blockType?.areas[0].columnSpan}></umb-block-grid-entries>
     <umb-block-grid-entries
        part="area"
        class="umb-block-grid__area"
        .areaKey=${this.blockType?.areas[1].key}
        .layoutColumns=${this.blockType?.areas[1].columnSpan}></umb-block-grid-entries>
    

    The custom views I am converting form in my Umbraco 13 build are in some instances are the surrounding columns for banners, and have different CSS applied left and right around the areas, and though it would be good to use those styles and the preview appearing as close as possible to what they will look like when published. Like I have been able to do in Umbraco 13 with Block grid. I also will be having arrangements in the rows for three or four columns. This will help greatly and is a big hurdle surmounted in understanding Typescript and Web Components in Umbraco.

    Kind regards,

    Pete

Please Sign in or register to post replies

Write your reply to:

Draft