Return to site

Web Components Nested Slots

broken image


With Shadow DOM, we can create Web Components that are a composition between the component's internals and elements provided at author-time by the user of the custom element. Nested slot examples. GitHub Gist: instantly share code, notes, and snippets. Now, in this article, we will learn about Angular 2 components and we will also see how to create a nested component and how parent and child components communicate with each other. Angular 2 Components. We all know that Angular 2 is a component based framework because everything is a component in Angular 2.

  1. Web Components Nested Slots Software
  2. Web Components Nested Slots Folders
  3. Web Components Nested Slots C++
  4. Web Components Nested Slots Machines

Slots for Content Projection Many Web Components - or to be more precise, many Custom Elements - need to be adaptable with some markup passed as the element's content. This is also called content projected because the passed content is projected to different positions of the element's template. I made a simple example using Web Components with two custom elements (v1) where one is nested in another. Index.html:

Web Components provide a component model to the Web. Web Components, instead of being a single spec, is a collection of several stand-alone Web technologies. Often Web Components will leverage Shadow DOM features. Shadow DOM is commonly used for CSS encapsulation. However, Shadow DOM has another useful feature called Slots.

The Slot API is a content projection API that allows HTML content from the host application to be rendered into your component template. Common examples of this are things like cards and modals.

Web

Web Components Nested Slots Software

Machines

Web Components Nested Slots Software

Here is a minimal example of a Custom Element using the Slot API.

The tags' content can be rendered into our template we defined. The browser render the content wherever the element is placed. If we look at what the browser renders, we will see something like this:

The content is projected and rendered within the template of our component. Often there are use cases, whereas the component author we would like to know about any updates to the content provided by the slot element. We can achieve this by adding an event listener in our component for the slotchange event.

Web Components Nested Slots Folders

This event will fire whenever any content has changed within the slot. To test this, we can use our component and dynamically update the content to see the event update.

In this example, every one second, we can set the textContent or the innerHTML of the component and see the slotchange event fire within the x-component. How to say roulette in french.

We can easily render content into our component templates and listen for content updates. But there is one interesting exception to this rule. While the event will happen whenever textContent or innerHTML are set, the event will not occur if a textNode reference is updated dynamically. Let's take a look at an example.

Instead of directly setting the textContent or innerHTML of our element we create a text node. While not an HTML element, the text node allows us to hold a reference in memory we can update at a later point. Casino halifax poker room. So if we go back to our interval, we will see the text change, but the event is no longer triggered.

This behavior can be a bit unexpected at first. Many JavaScript frameworks will leverage text nodes to optimize for performance. The short rule to remember is slotchange only fires when the HTML DOM has changed either by a DOM/Text Node from being added or removed. Check out the full working example below!

App.vue

Web Components Nested Slots C++

<template>
<manage-listsv-model='items'>
<templatescope='{ item: user }'>
{{ user.firstName }} {{ user.lastName }}
template>
manage-lists>
template>
<script>
exportdefault {
components: {
ManageLists
},
data() {
return {
items: [
{ firstName:'John', lastName:'Doe' },
{ firstName:'Renae', lastName:'McGillicuddy' }
]
};
}
}
script>

Web Components Nested Slots Machines

ManageLists.vue
<template>
<div>
<token-list :value='items' @input='forwardInput'>
<templatescope='props'>
<slotv-bind='props'>slot>
template>
token-list>
<formclass='form' @submit.prevent='handleAdd'>
<inputtype='text'placeholder='New Item'v-model='newItem'ref='newItem' />
<buttontype='submit'>Add Itembutton>
form>
div>
template>
<script>
importTokenListfrom'./TokenList';
exportdefault {
props: {
value: {
type:Array,
default() {
return [];
}
}
},
components: {
TokenList
},
methods: {
handleAdd() {
this.$emit( 'input', this.value.concat( this.newItem ) );
this.newItem='';
this.$refs.newItem.focus();
},
forwardInput( payload ) {
this.$emit( 'input', payload );
}
}
}
script>
TokenList.vue
<template>
<divclass='token-list clearfix'>
<divv-for='( item, index ) in value'class='token-item'>
<slot :item='item' :index='index'>
<span>{{ item }}span>
<buttontype='button' @click='remove( index )'>×button>
slot>
div>
div>
template>
<script>
exportdefault {
props: {
value: {
required:true
}
}
methods: {
remove( index ) {
this.$emit( 'input', [
..this.value.slice( 0, index ),
..this.value.slice( index +1 )
] );
}
}
}
script>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment




broken image