DEV Community

Kern Designs
Kern Designs

Posted on

3 1

Slot Patrol

Web components are great for their flexibility and versatility. One thing that makes that possible is their ability to have slots. An HTML element allows us to create separate DOM trees and code them together into one element. Below you can see how the element contains both an emoji and a message.

HTML DOM tree

In an element that I have been recently creating, called the learning-card, that has multiple slots. I have to scaffold a banner (header, sub header, icon), a body, an icon and the overall scaffold into one element. You can see a picture of it below.
Image description

In the banner render function you can see how the whole banner is defined in a div wrapper. Inside I call the icon and then scaffold in a header wrapper. There are two slots inside that to hold the header and sub header. This is a great example of how slots scaffold inside of a component.

render() {
    return html`
      <div class="banner-wrapper">
        <learning-icon type="${this.icon}"></learning-icon>
        <div class="header-wrapper">
          <slot name="header"></slot>
          <slot name="subheader"></slot>
        </div>
      </div>
    `;
  }
Enter fullscreen mode Exit fullscreen mode

This project is still growing but check out what I've done: https://github.com/TheKodingKrab/project-two. You can also learn about issues I've encountered in many of my other blogposts.

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️