When building a cart component, you often need to render multiple items with custom layouts. The intuitive approach might look like this:
<my-cart>
<div slot="line-item">Custom line item layout</div>
</my-cart>
However, this doesn't work as expected because named slots can only be used once in a shadow DOM.
We want to allow users to:
- Define a custom template for line items
- Access item-specific data within each line
- Maintain a clean, intuitive API
<!-- Feels disconnected -->
<template id="line-item">
<my-line-item>
<b><my-line-item-title></my-line-item-title></b>
</my-line-item>
</template>
<my-cart line-item-template="line-item"></my-cart>
<my-cart>
<!-- Regular slots work normally -->
<div slot="header">Cart Header</div>
<!-- Template for repeated items -->
<template slot="line-item-template">
<my-line-item>
<b><my-line-item-title></my-line-item-title></b>
</my-line-item>
</template>
<div slot="footer">Cart Footer</div>
</my-cart>
<my-cart>
<!-- Regular slots work normally -->
<div slot="header">Cart Header</div>
<script type="application/json" slot="line-item-renderer">
(item) => `<my-line-item>${item.title}</my-line-item>`
</script>
</my-cart>