npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-forward-slots

v5.0.4

Published

Forward slots to child components

Downloads

1,084

Readme

Vue Forward Slots

Effortlessly forward slots to child components in Vue 3 applications.

Features

  • Easily forward all slots or specific slots to child components
  • Simple and declarative syntax

Why Vue Forward Slots?

In Vue applications, it's common to need to forward slots from a parent component to a child component. However, the default way of doing this can be verbose and repetitive. Consider the following example:

The Default Way

<template>
    <ChildComponent>
        <template v-for="(index, name) in $slots" v-slot:[name]="data">
            <slot :name="name" v-bind="data"/>
        </template>
    </ChildComponent>
    <AnotherChildComponent>
        <template v-for="(index, name) in $slots" v-slot:[name]="data">
            <slot :name="name" v-bind="data"/>
        </template>
    </AnotherChildComponent>
</template>

Verbose and hard to read!

With Vue Forward Slots

<template>
    <ForwardSlots :slots="$slots">
        <ChildComponent/>
        <AnotherChildComponent/>
    </ForwardSlots>
</template>

Simple and clean!

Installation

npm install vue-forward-slots

Importing

You can import it in the component where you want to use it.

<script>
    import {ForwardSlots} from "vue-forward-slots";

    ...
</script>

Usage

Example Usage

A classic example is that of a table component with multiple levels of nested components. We can easily define and forward slots to nested components using the ForwardSlots component.

Root Component

We define the slots in the root component.

<template>
    <TableComponent>
        <template #name-header>
            <p class="font-bold">
                Name
            </p>
        </template>

        // We still have access to the slot data like we would normally
        <template #status-cell="{ user }">
            <StatusBadge :status="user.status"/>
        </template>
    </TableComponent>
</template>

Table Component

We forward the slots to the child components.

<template>
    <table>
        // Notice that we can wrap multiple components in the ForwardSlots component
        <ForwardSlots :slots="$slots">
            <TableHeadComponent/>
            <TableBodyComponent/>
        </ForwardSlots>
    </table>
</template>

TableHead Component

The TableHeadComponent now has access to the slots defined in the root component. If no slot is provided, it will default to the text in the slot.

<template>
    <thead>
    <tr>
        <th>
            <slot name="name-header">
                Some default text
            </slot>
        </th>
        <th>
            <slot name="status-header">
                Some default text
            </slot>
        </th>
    </tr>
    </thead>
</template>

TableBody Component

The TableBodyComponent also has access to the slots defined in the root component. Notice how we also pass the user data.

<template>
    <tbody>
    <tr v-for="user in users">
        <td>
            <slot name="name-cell" :user="user">
                {{ user.name }}
            </slot>
        </td>
        <td>
            <slot name="status-cell" :user="user">
                {{ user.status }}
            </slot>
        </td>
    </tr>
    </tbody>
</template>

We could even go a step further and forward the slots to the next level of child components.

<template>
    <thead>
    <tr>
        <th v-for="header in headers">
            <ForwardSlots :slots="$slots">
                <TableHeaderCell :header="header"/>
            </ForwardSlots>
        </th>
    </tr>
    </thead>
</template>

In theory, we could keep forwarding slots to as many levels of child components as we need.

Forwarding Only Specific Slots

<template>
    // For a single slot
    <ForwardSlots :slots="$slots" only="header">
        <MyComponent/>
    </ForwardSlots>

    // For multiple slots
    <ForwardSlots :slots="$slots" :only="['header', 'footer']">
        <MyComponent/>
    </ForwardSlots>
</template>

Excluding Specific Slots

<template>
    // For a single slot
    <ForwardSlots :slots="$slots" except="sidebar">
        <MyComponent/>
    </ForwardSlots>

    // For multiple slots
    <ForwardSlots :slots="$slots" :except="['sidebar', 'footer']">
        <MyComponent/>
    </ForwardSlots>
</template>