💉 vNode Syringe
Add attributes and event-listeners to <slot>
content.
Supports key
, class
/style
, attrs
, props
& listeners
!
⬅ The class and event-listener gets added to every element passed in
🙋♂️ Why?
- 🔥 Set or overwrite attributes & event-listeners on content received from the
<slot>
! - 🧠 Smart merging strategies Pick between merging, overwriting, or falling-back!
- 🐥 Tiny
1.05 KB
minzipped!
🚀 Install
npm i vue-vnode-syringe
💠 Merging strategies
Fallback
This is the default behavior. The class new-class
and event-listener newOnClick
only gets added if there isn't one added yet.
For example, given the following <slot>
content, only the event-listener newOnClick
will be added:
> some content
!
Overwrite Add !
at the end of the attribute or event-listener to overwrite what exists.
For example, given the following <slot>
content, both the class and event-listener will overwrite the existing class and event-listener.
@click="existing" <!-- becomes "newOnClick" -->> some content
&
Merge Add &
at the end of the attribute or event-listener to merge with what exists.
For example, given the following <slot>
content, both the class and event-listener will merge with the existing class and event-listener. When merging event-listeners, both event-listeners will be called.
@click="existing" <!-- becomes "existing(); newOnClick()" -->> some content
👨🏻🏫 Examples
Demo 1: Passing down attributes
In this demo, the class="button-group__button"
attribute is passed down to all of its <slot>
content.
ButtonGroup.vue
Usage.vue
Button 1 <!-- Will render with the `button-group__button` class --> Button 2 <!-- Will render with the `button-group__button` class --> Button 3 <!-- Will render with the `button-group__button` class -->
Demo 2: Merging and Overwriting classes
By default, vNode Syringe only adds the attribute/event-listener if it doesn't already exist. To merge with or overwrite the existing one, use the &
(merge) or !
(overwrite) suffix.
ButtonGroup.vue
class&="button-group__button" <!-- Force all buttons to have type="button" --> type!="button" <!-- Only gets added if child doesn't specify `disabled` --> :disabled="disabled" >
Usage.vue
class="button" <!-- Gets overwritten to type="button" --> type="submit" <!-- Will be inherit parent's disabled state --> > Button 1 class="button" <!-- Won't inherit parent's disabled state --> :disabled="false" > Button 2
💁♀️ FAQ
<slot>
?
How can I add attributes/event-listeners to a specific element in the You can use Subslot to pick out specific elements in the slot.
For example, if you only want to accept <button>
s in your slot:
<template> <div class="button-group"> <vnode-syringe class&="button-group-item" @click="onClick" > <subslot element="button" /> </vnode-syringe> </div></template> <script>import Subslot from 'vue-subslot';import vnodeSyringe from 'vue-vnode-syringe'; export default { components: { Subslot, vnodeSyringe }, ..., methods: { onClick() { ... } }};</script>
👨👩👧 Related
- vue-proxi - 💠 Tiny proxy component
- vue-subslot - 💍 Pick 'n choose what you want from a slot passed into your Vue component
- vue-pseudo-window - 🖼 Declaratively interface window/document in your Vue template
- vue-vnode-syringe - render vNodes via component template
- vue-frag - 🤲 Directive to return multiple root elements