• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Plugin/Mixin wrapping Vue's static 'provide/inject' feature allowing to easily pass reactive data to children

vue-reactive-provide

GitHub CircleCI branch Coverage Status npm

What this is and what it does

This library is a Vue plugin and mixin that wraps Vue's own provide API and makes the object that is provided to children reactive.

This makes it much easier to pass reactive updates down from the parent component down to the children and grandchildren that consume the provided object via inject.

Installation

npm install -D vue-reactive-provide
# or
yarn add -D vue-reactive-provide

You can use this library as a pure mixin or use it via an options interface injected with a Plugin.

To Install the Plugin:

import Vue from 'vue'
import ReactiveProvide from 'vue-reactive-provide'

// not necessary when used as a mixin, see below for details
Vue.use(ReactiveProvide)

// overwrite the option's name:
Vue.use(ReactiveProvide, {
  name: 'reactiveProvide', // default value
})

Basic Usage

You can use this library in two ways:

  • through an option defined in a component (requires installation as a plugin with Vue.use() as described above)
  • as a mixin (Vue.use() not required)

A. Options config

Provide:

// in the parent component
export default {
  name: 'Parent',

  reactiveProvide: {
    name: 'nameOfInject',
    include: ['items', 'filteredItems'],
  }

  data() {
    return {
      items [ /* .... */ ]
    }
  },
  computed: {
    numItems() {
      return this.items.length
    }
  }
}

B. Mixin

import { ReactiveProvideMixin } from 'vue-reactive-provide'
export default {
  name: 'Parent',

  mixins: [
    ReactiveProvideMixin({
      name: 'nameOfInject',
      include: ['items', 'filteredItems'],
    })
  ],
  data() {
    return {
      items [ /* .... */ ]
    }
  },
  computed: {
    numItems() {
      return this.items.length
    }
  }
}

Using it in the Child:

export default {
  name: 'Child',
  inject: ['nameOfInject'],
  /**
   * {
   *   items: [ ...],
   *   numItems: [ ...],
   * }
   */
}

Why Is that better than the native provide option?

While Vue's native provide implemention is very useful on its own, if you're like me you quickly found that the lack of reactivity limits it in many situations.

While we an provide a reactive object as a property on the provided object quite easily, this doesn't work for a reactive property containing a string, for example:

data: () => ({
  items: ['item 1', 'item 2],
  message: 'A reactive message'
})
provide() {
  return {
    items: this.items, // changes to the array will be reactive in the consuming component
    message: this.message // this will not be reactive
  }
}

We could pass down $data, but the we possibly pass down more than we want. And we also can't pass down computed props in a reactive way, either.

This small lib aims to fix these problems. You just tell it which properties of your component you want to provide to its children, and they will be put on a reactive object for you.

Options

Option Required Type (Default) Description
name yes string (undefined) The name under which to provide the object
include no string[] (false) An array of strings, each a property name of the component
props no boolean (false) Provide all $props properties on the object.
attrs no boolean (false) Works like the props option, but for $attrs
listeners no boolean (false) Works like the props option, but for $listeners
nameForComputed no `string false (false`)
inheritAs no `string false (false`)

Advanced Usage

Passing down all...

  • Props:
reactiveProvide: {
  name: 'nameOfInject',
  props: true,
}
inject: ['nameOfInject'],
// {
//   "prop1": <value>
//   "prop3": <value>
//   "prop2": <value>
//   "propX": <value>
// }
  • Attributes:
reactiveProvide: {
  name: 'nameOfInject',
  attrs: true,
}
  • Props:
reactiveProvide: {
  name: 'nameOfInject',
  listeners: true,
}

Using the computed property

  • With scoped slots:

The object that is provided to the children is also available as a computed prop of the same name in the component that defined it.

This works great with scoped slots:

<script>
export default {
  reactiveProvide: {
    name: 'nameOfInject',
    include: ['message', 'items']
  },
  computed: {
    // a computed property with the name 'nameOfInject' is automatically added
  }
}
</script>
<template>
  <div>
    <!-- This slot will receive `items` and `message` as props -->
    <slot v-bind="nameOfInject">
  </div>
</template>

Inheriting an injection from the parent

When building a complicated tree of components, you may find yourself in a situation where you want to essentially pass an injection from the parent further down to the children, but also add or overwrite something. This option makes it possible.

// defined in Parent.vue
data: () => ({
  static: 'this doesnt get changed',
  message: 'the original message'
}),
reactiveProvide: {
  name: 'nameOfInject',
  include: ['message', 'items']
},
// Middleman.vue
data: () => ({
  message: 'a better message',
  plus: 'something else'
}),
reactiveProvide: {
  name: 'nameOfInject',
  include: ['message', 'plus'],
  inheritAs: 'injectFromParent'
},

Your Middleman.vue, you will now have two props (the computed one, this.nameOfInject and the originall injected one, this.injectFromParent) and the object that's provided to the children will look like this:

{
  static: 'this doesnt get changed',
  message: 'a better message',
  plus: 'something else'
}

Working on this

The following instructions are only relevant to people working on this repository:

Development

Compiles and hot-reloads for development

yarn run serve

Build for production

yarn run build

Lints and fixes files

yarn run lint

Tests

This project runs unit tests with jest.

yarn run test:unit

# for watch mode:
yarn run test:unit:w

More Repositories

1

portal-vue

A feature-rich Portal Plugin for Vue 3, for rendering DOM outside of a component, anywhere in your app or the entire document. (Vue 2 version: v2.portal-vue.linusb.org)
Vue
3,772
star
2

composition-api-demos

A Vue.js app demonstarting various use cases for the new composition API
JavaScript
517
star
3

vue-mixable

Turn Vue Mixins into Composables with a simple wrapper function
TypeScript
282
star
4

vue-simple-portal

A simpler Portal implementation focussed on moving slot content to the end of the body element
JavaScript
217
star
5

vue-lib-template

a personal template to boostrap the boilerplate for developing Vue libraries in a pnpm monorepo
TypeScript
70
star
6

vue-cli-plugin-test-attrs

A small plugin vor vue-cli 3 that adds a compiler plugin to remove data-test attributes when desired
JavaScript
65
star
7

slidev-theme-vuetiful

A Slidev Theme for my talks about Vue.js
Vue
63
star
8

talks

Collection of talks I gave
HTML
44
star
9

varia

Vuejs 3 baseline components and composables fo building WAI-ARIA compatible UI components
TypeScript
33
star
10

vueday-enterjs-vue3

Talk slides for Enter.js Vue Day (15.06.2021)
Vue
24
star
11

vue-filedrop

[WIP, NOT READY] A set of composable Vue components to create rich Dropzone-like UI elments and behaviour
JavaScript
14
star
12

talks_2018-11_VuejsRoadtrip-Berlin

Slides of the talk I gave at Vuejs Roadtrip Berlin, 2018-11-23
11
star
13

vue-directive-clickout

a directive to catch clicks outside of an element, compatible with Vue 2.0 only
10
star
14

vue-ko-fi-button

the Ko-fi Buttton (www.ko-fi.com) implemented as a Vue component (no affiliation)
Vue
6
star
15

vue-teleport-plus

A superior Teleport component experience for Vue 3
TypeScript
5
star
16

tailwindui-listbox-vue3-demo

TypeScript
5
star
17

eslint-config

JavaScript
4
star
18

create-vue-ts-eslint-airbnb-example

Example Repo demonstrating how to add the Airbnb config to a Typescript-Vue Project created with create-vue
Vue
4
star
19

demo-project-building-portals-in-vue

Created with CodeSandbox
Vue
3
star
20

vue-inject-helpers

[WIP] Wrapper components for Vue.js to pass injected properties as props to a child component
JavaScript
3
star
21

talk-thinking-in-script-setup

My talk for Vuejs Germany 2022 in Berlin
Vue
1
star
22

talk-cross-compatible-vue-components

Slides for my talk at Vuejs Amsterdam 2022
Vue
1
star