/vuex-multi-tab-state

πŸ’ΎπŸ”—πŸ–₯️ Share, synchronize and persist state between multiple tabs with this plugin for Vuex. TypeScript types included.

Primary LanguageTypeScriptMIT LicenseMIT

vuex-multi-tab-state

Build Status npm codecov codebeat badge npm npm bundle size npm type definitions code style: prettier demo

This Vuex plugin allows you to sync and share the status of your Vue application across multiple tabs or windows using the local storage.

This repository has a gitter chat where you can ask questions and propose new features:

Gitter

Installation

vuex-multi-tab-state is available in npm and can be installed with the following command:

npm i vuex-multi-tab-state

Usage

Just import vuex-multi-tab-state and add it in the plugins list of your Vuex Store object.

import Vue from 'vue';
import Vuex from 'vuex';
import createMultiTabState from 'vuex-multi-tab-state';

Vue.use(Vuex);

export default new Vuex.Store({
  state: { ... },
  mutations: { ... },
  actions: { ... },
  getters: { ... },
  plugins: [
    createMultiTabState(),
  ],
});

You can check the example provided here

NuxtJS

Integrating the plugin in NuxtJS requires a little more effort than in Vue. First of all, we have to create a file inside the plugins directory.

// ~/plugins/multiTabState.client.js
import createMultiTabState from 'vuex-multi-tab-state';

export default ({ store }) => {
  createMultiTabState()(store);
};

Note that the file name must have the following format *.client.js. The next step is to add this plugin to NuxtJS in nuxt.config.js:

// nuxt.config.js
export default {
  ...
  plugins: [{ src: '~/plugins/multiTabState.client.js' }],
  ...
}

If you didn't named the file following the format specified you can add this plugin this way:

// nuxt.config.js
export default {
  ...
  plugins: [{ src: '~/plugins/multiTabState.client.js', mode: 'client' }],
  ...
}

Both ways tell NuxtJS that the plugin should only be run on the client side (because the plugins uses window which is not available in server side).

API

createMultiTabState({options})

Creates a new instance of the plugin with the given options. The possible options are as follows:

  • statesPaths Array<String>: contains the name of the states to be synchronized with dot notation. If the param is not provided, the whole state of your app will be sync. Defaults to [].

    Example: Only the oranges will be synchronized.

    export default new Vuex.Store({
      state: {
        fruits: {
          oranges: 0,
          apples: 0,
        },
      },
      plugins: [createMultiTabState({
        statesPaths: ['fruits.oranges'],
      })],
    });
  • key <String>: key of the local storage in which the state will be stored. Defaults to 'vuex-multi-tab'.

Test

The test have been written with mocha and chai.

npm install
npm run test

Collaborate

npm collaborators

If you feel that something can be improved, go on, create a pull request! Please follow the programming style and document your changes correctly.

License

NPM

This project is under the MIT license. More information here.