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-subslot

v1.2.3

Published

πŸ’ Pick 'n choose what you want from a slot passed into your Vue component

Downloads

39

Readme

Pick out specific elements from the component <slot>.

<template>
	<div class="header">
		<subslot element="h1" /> β¬… Pick only the `h1` element from the default slot
	</div>
</template>

πŸš€ Install

npm i vue-subslot

πŸ™‹β€β™‚οΈ Why?

  • πŸ”₯ Cleaner Slot API Give your users a cleaner and more readable API!
  • 🧠 Full Slot control Filter out and limit unwanted content from slots!
  • πŸ₯ Tiny 1.04 KB minzipped!

πŸ‘¨πŸ»β€πŸ« Examples

Have you ever developed a parent-child component set, and wanted to allow users to pass in the child-component without specifiying a slot but still have the same level of control as named-slots? With Subslot, you can!

Imagine being able to offer the following API with parent-child components Card and CardHeader.

<card>
	<!-- The Card Header will be positioned separetely from the content -->
	<card-header>
		My special card
	</card-header>

	My card content
</card>

Using Subslot, this is all the code you need to make this possible. This is what Card.vue looks like.

<template>
	<div class="card">
		<div class="card-header">
			<!-- Pick out the Card Header from the default slot -->
			<subslot element="@CardHeader" limit="1" />
		</div>

		<div class="card-content">
			<!-- Use the remainder -->
			<subslot not element="@CardHeader" />
		</div>
	</div>
</template>

<script>
import Subslot from 'vue-subslot';
import CardHeader './CardHeader.vue';

export default {
	name: 'Card',

	components: {
		Subslot,
		CardHeader,
	}
};
</script>

Alternatively to using inline filter attributes, you can define subslots on the component. With this approach, you can access subslots like you would normal slots but via $subslots. This is what Card.vue would look like.

<template>
	<div class="card">
		<div
			v-if="$subslots.cardHeader"
			class="card-header"
		 >
			<subslot name="cardHeader" />
		</div>

		<div class="card-content">
			<!-- Use the remainder -->
			<subslot />
		</div>
	</div>
</template>

<script>
import Subslot from 'vue-subslot';
import CardHeader './CardHeader.vue';

export default {
	name: 'Card',

	components: {
		Subslot,
		CardHeader,
	},

	mixins: [
		Subslot.define({
			// Use a string filter
			cardHeader: '@CardHeader:1', // Limit 1
			cardHeader: '@CardHeader[3:2]', // Offset 3, Limit 2

			// Or an object filter
			cardHeader: {
				element: '@CardHeader',
				limit: 1,
			},
		}),
	],
};
</script>

πŸ“– API

Filter by element tag

As a string, it filters the vnodes by tag (as opposed to component)

<subslot element="div" />

Filter the vnodes with tag child-component

<subslot element="ChildComponent" />

To match a specific component

Use the @ prefix to use the component from the components hash

<subslot element="@ChildComponent" />

Or, pass in the direct Component reference

<subslot :element="ChildComponent" />

To match multiple elements

Pass in an array

<subslot :element="[ChildComponentA, '@ChildComponentB', 'div']" />

To match any element

Use the asterisk to match any element. This is to match only elements and remove any text/white-space.

<subslot element="*" />

Offset the number of returned elements

<subslot
	element="ChildComponent"
	offset="1"
/>

Limit the number of returned elements

<subslot
	element="ChildComponent"
	offset="1"
	limit="1"
/>

Inverse the filter

Set the not boolean to inverse the filter and get everything that doesn't match.

<subslot not element="@ChildComponent" />

Slot fallback

Like normal slots, what you pass into the slot of subslot will be the fallback content of that subslot.

<subslot name="banner">
	<default-banner />
</subslot>

πŸ“¬ Events

  • @no-match: Emitted when there are no matching vnodes

⚑ Advanced usage

Pass in vnodes from a difference source

<subslot
	:vnodes="$slots.namedSlot"
	element="@ChildComponent"
/>

πŸ’β€β™€οΈ FAQ

Will this work for functional components passed into the slot?

Unfortunately not due to how functional components are implemented in Vue.js.

Functional components are stateless and are immediately invoked as a function that outputs vNodes. The outputted vNodes are passed into the slot in place of the functional component. Because Subslot doesn't actually receive the functional component, it's impossible to detect them.

πŸ‘¨β€πŸ‘©β€πŸ‘§ Related

  • vue-proxi - πŸ’  Tiny proxy component
  • vue-vnode-syringe - 🧬 Add attributes and event-listeners to <slot> content πŸ’‰
  • vue-pseudo-window - πŸ–Ό Declaratively interface window/document in your Vue template
  • vue-v - render vNodes via component template
  • vue-frag - 🀲 Directive to return multiple root elements