Web Components Nested Slots

Posted on

Tutorial

Slot- a defined location in a shadow tree. Represented by the slot element. Slot name- the name of a slot. Default slot- a slot for assigning nodes without a slot name. Slotting Algorithm. The slotting algorithm assigns nodes of a shadow tree host into slots of that tree. Input HOST- a shadow tree host Output All child nodes of HOST are slotted. I made a simple example using Web Components with two custom elements (v1) where one is nested in another. Index.html: <!DOCTYPE html> <html> <head> <meta charset='utf-8'>.

  • Previous Web Components. Hierarchical Menu button. Nested Web Components. Slotting; Subproperties; Template Slots; Wrapping JET Slot Components; Responsive.
  • Web components nested slots. Ask Question Asked 2 years, 9 months ago. Active 2 years, 7 months ago. Viewed 2k times 0. Is there a better way to pass slots to deeply.
  • This article explains how you can use the template and slot elements to create a flexible template that can then be used to populate the shadow DOM of a web component. MDN will be in maintenance mode, Monday December 14, from 7:00 AM until no later than 5:00 PM Pacific Time (in UTC, Monday December 14, 3:00 PM until Tuesday December 15, 1:00 AM).
Web

While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the 'report an issue' button at the bottom of the tutorial.

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. This is similar with using either option or optgroup elements inside a native select element. This composition is made possible using a mechanism known as slots and here we’ll go over how to use slots and named slots in your custom elements to allow for interesting compositions.

Your First Slotted Content

Allowing users of your custom elements to add their own markup and elements as part of your element is as simple as using the slot element in your component’s template.

Here’s an example of a dumb custom element that only acts as a styled shell for content that’s added when the element is used:

Wondering about the use of :host for our element’s style? Read about styling custom elements.

This element can then be used like this:

The markup added inside a custom element is called the light DOM and it stays outside of the Shadow DOM’s reach, as you can see from this screenshot of the element as seen from Chrome’s DevTools:

Elements in the light DOM are instead accessible directly as children of the element, so you could do something like this to grab an element, as you would normally to access an element in the DOM:

A light DOM element that’s being used in a slot is known as a distributed node.

Default Content

You can provide default content for a slot, in case none is provided when the element is used:

my-info-box.js (partial)

And this default content will be used if the element is used like this:

Web Components Nested Slots

Named Slots

Web

Web Components Nested Slots Machines

Creating more complex elements that can take various pieces of content from the element’s user can be done easily using named slots. You can see a simple example here where a named slot is used alongside a regular slot:

The element can then be used like this:

Thanks to named slots, you can easily create a complex custom elements that compose multiple pieces together. For example, a nav bar that takes a title, a logo, left navigation items and right navigation items.

Styling Slotted Content With ::slotted

We can style slotted content using the ::slotted() selector. Here’s a very simple example:

And, with this, the title in the following example will have a pink background and the main content will be in a monospace font:

Note that the selector used with ::slotted should select a top-level element, as it can’t match a slot using a nested element.

App.vue
<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 C++

ManageLists.vue

Web Components Nested Slots Folders

Web Components Nested Slots

Web Components Nested Slots Software

<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 Item</button>
</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 )'>&times;</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>
Nested
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment