Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vue-composable

pikax17.5kMIT1.0.0-beta.24TypeScript support: included

vue-composable

vue, composition-api, vue-composable, composable

readme

vue-composable

vue-composable logo

CircleCI Coverage Status npm version bundle size

Out-of-the-box ready to use composables

  • 🌴 TreeShakable
  • 🧙‍♂️ Fully Typescript
  • 💚 Vue 3 and 2 support
  • 🔨 Vue Devtools support

Introduction

This library aim is to be one stop shop for many real-world composable functions, with aggressive tree-shaking to keep it light on your end code.

Installing

# @vue/composition-api

# install with yarn
yarn add @vue/composition-api vue-composable

# install with npm
npm install @vue/composition-api vue-composable

# vue 3

# install with yarn
yarn add vue-composable

# install with npm
npm install vue-composable

Documentation

Check our documentation

Composable

Event

  • Event - Attach event listener to a DOM element
  • Mouse Move - Attach mousemove listener to a DOM element
  • Resize - Attach resize listener to a DOM element
  • Scroll - Attach scroll listener to a DOM element
  • onOutsidePress - Execute callback when click is outside of element

Dom

Date

  • useNow : Return reactive custom timer with specified refresh rate
  • useDateNow : Returns reactive Date.now() with custom refresh rate
  • usePerformanceNow : Returns reactive performance.now() with custom refresh rate

Format

  • format - Reactive string format
  • path - Retrieve object value based on string path

Breakpoint

MISC

Storage

  • WebStorage - Reactive access to Storage API, useLocalStorage and useSessionStorage use this
  • storage - uses localStorage or on safari private it uses sessionStorage
  • localStorage - Reactive access to a localStorage
  • sessionStorage - Reactive access to a sessionStorage

Pagination

Validation

i18n

  • i18n - Simple i18n implementation with API inspired by vue-i18n

Intl

Promise

Meta

  • Title - reactive document.title

State

  • Timeline - Tracks variable history
  • Undo - Tracks variable history, to allow undo and redo
  • valueSync - syncs variables value, across multiple refs

Web

External

New packages needed

Information

This is a monorepo project, please check packages

Devtools

There's some experimental devtools support starting from 1.0.0-beta.6, only available for vue-next and devtools beta 6.

Install plugin

To use devtools you need to install the plugin first:

import { createApp } from "vue";
import { VueComposableDevtools } from "vue-composable";
import App from "./App.vue";

const app = createApp(App);
app.use(VueComposableDevtools);
// or
app.use(VueComposableDevtools, {
  id: "vue-composable",
  label: "devtool composables"
});

app.mount("#app");

Component State

To add properties to the component inspector tab ComponentState

const bar = "bar";
useDevtoolsComponentState(
  {
    bar
  },
  {
    type: "custom composable" // change group
  }
);

const baz = () => "baz";
useDevtoolsComponentState({ baz });
// no duplicates added by default
useDevtoolsComponentState({ baz });

const the = 42;
useDevtoolsComponentState({ the });
// to allow multiple same key
useDevtoolsComponentState({ the }, { duplicate: true });

// use a devtools api list directly
interface StateBase {
  key: string;
  value: any;
  editable: boolean;
  objectType?: "ref" | "reactive" | "computed" | "other";
  raw?: string;
  type?: string;
}
useDevtoolsComponentState([
  {
    key: "_bar",
    type: "direct",
    value: "bar",
    editable: true
  },
  {
    key: "_baz",
    type: "direct",
    value: "baz",
    editable: false
  }
]);

// raw change
useDevtoolsComponentState((payload, ctx) => {
  payload.state.push(
    ...[
      {
        key: "_bar",
        type: "raw",
        value: "bar",
        editable: true
      },
      {
        key: "_baz",
        type: "raw",
        value: "baz",
        editable: false
      }
    ]
  );
});

Timeline events

To add timeline events:

const id = "vue-composable";
const label = "Test events";
const color = 0x92a2bf;

const { addEvent, pushEvent } = useDevtoolsTimelineLayer(
  id,
  description,
  color
);

// adds event to a specific point in the timeline
addEvent({
  time: Date.now(),
  data: {
    // data object
  },
  meta: {
    // meta object
  }
});

// adds event with `time: Date.now()`
pushEvent({
  data: {
    // data object
  },
  meta: {
    // meta object
  }
});

Inspector

Allows to create a new inspector for your data.

I'm still experimenting on how to expose this API on a composable, this will likely to change in the future, suggestions are welcome.

useDevtoolsInspector(
  {
    id: "vue-composable",
    label: "test vue-composable"
  },
  // list of nodes, this can be reactive
  [
    {
      id: "test",
      label: "test - vue-composable",
      depth: 0,
      state: {
        composable: [
          {
            editable: false,
            key: "count",
            objectType: "Ref",
            type: "setup",
            value: myRefValue
          }
        ]
      }
    }
  ]
);

Typescript

`Typescript@3.xis not supported, the supported version can be checked on [package.json](./package.json), usually is the latest version or the same major asvue-3`

Contributing

You can contribute raising issues and by helping out with code.

Tests and Documentation are the most important things for me, because good documentation is really useful!

I really appreciate some tweaks or changes on how the documentation is displayed and how to make it easier to read.

Twitter: @pikax_dev

Build

# install packages
yarn

# build and test for v2
yarn build --version=2
yarn test:vue2

# build and test for v3
yarn build
yarn test

New composable

  1. Fork it!
  2. Create your feature branch: git checkout -b feat/new-composable
  3. Commit your changes: git commit -am 'feat(composable): add a new composable'
  4. Push to the branch: git push origin feat/new-composable
  5. Submit a pull request

License

MIT

changelog

1.0.0-beta.24 (2021-10-17)

Bug Fixes

1.0.0-beta.24 (2021-10-17)

Bug Fixes

1.0.0-beta.23 (2021-04-24)

Bug Fixes

  • use Vue instead the VueRuntimeCore (6f31250), closes #852

1.0.0-beta.23 (2021-04-24)

Bug Fixes

  • use Vue instead the VueRuntimeCore (6f31250), closes #852

1.0.0-beta.22 (2021-04-10)

Bug Fixes

  • validation: create dirty watcher after $reset (#843) (f476114)

1.0.0-beta.21 (2021-03-27)

Bug Fixes

  • usePromise: last promise would never be mark as complete (bd03e00)
  • import.meta SSR (aafc3e1)

Features

  • useVModel: Set to throw error if current instance is undefined (#819) (f352cf5)

1.0.0-beta.20 (2021-03-06)

Bug Fixes

  • ssr: SSR check changed to also taking process.env into consideration (8ef1ad0)

1.0.0-beta.19 (2021-03-06)

Bug Fixes

  • ssr: changing SSR check from process.env.SSR to import.meta.env.SSR (957fab3)

1.0.0-beta.18 (2021-02-21)

Features

  • storage: support for ref key for localStorage and sessionStorage (f873ca0), closes #776

1.0.0-beta.17 (2021-02-20)

Features

  • validation: add $touch/$reset methods and some return changes (7b3b83d), closes #753
  • add useCookie composable (#745) (03e6633)

1.0.0-beta.16 (2021-01-23)

Features

1.0.0-beta.15 (2021-01-16)

Bug Fixes

  • i18n: do not import default when importing a json as a module (#732) (1ef0912)

1.0.0-beta.14 (2021-01-06)

Features

  • refDebounced: support passing a ref as a value (f8a26b9)
  • useValidation: add toObject (#723) (28e9fd3)

1.0.0-beta.13 (2021-01-01)

Bug Fixes

  • cancellablePromise: cancelled not set to false when executing (d1b9ef9), closes #704

Features

  • useClipboard: added a new composable (#709) (e2d12c9)

1.0.0-beta.12 (2020-10-31)

Features

  • add useRefDebounced (#579) (42c027a)
  • injectFactory: use inject with treatDefaultAsFactory: true (2b378c0)
  • onScroll: add scrollBy and scrollIntoView methods (3f6e559), closes #640

1.0.0-beta.11 (2020-10-11)

Features

  • add useMouseDistanceFromElement (#578) (a09cb69)
  • onMouseMove: add pageX and pageY properties (ed4788d)
  • remove devtools code from production build (916187e)

1.0.0-beta.10 (2020-09-15)

Features

  • devtools: add useDevtoolsComponentState (#570) (aff632e)

1.0.0-beta.8 (2020-09-14)

Bug Fixes

1.0.0-beta.7 (2020-09-13)

Bug Fixes

  • devtools: return the new created callback (9ef375a)

1.0.0-beta.6 (2020-09-13)

Bug Fixes

  • valueSync: vue3 prevent recursive error when assigning a new ref to a list (3350183)

Features

1.0.0-beta.5 (2020-08-28)

Bug Fixes

  • axios: add postinstall script for @vue-composable/axios (8f7dab4)

1.0.0-beta.4 (2020-08-15)

Features

  • cancellablePromise: automatically cancel promise on unmount (#517) (b18b21d)
  • fetch: automatically cancel on going request on unmount (#516) (a030179)
  • validation: add to $errors if the validator has $message (#521) (7e79920)

Reverts

  • build(deps-dev): bump @vue/* (6dc3650)

1.0.0-beta.3 (2020-07-27)

Features

  • useInterval: add useInterval composable (#343) (499908e)
  • useOnOutsidePress: add useOnOutsidePress composable (#383) (b8fa41f)
  • useScrollLock: add useScrollLock composable (#330) (0da2f44)
  • useTimeline: add useTimeline composable (#342) (618842b)
  • useValueSync: add useValueSync composable (#348) (04f4bbf)

1.0.0-beta.2 (2020-07-24)

Features

  • add current when tailwindcss config contains only number bp (#475) (202f53a)

1.0.0-beta.1 (2020-07-18)

Features

1.0.0-dev.37 (2020-07-18)

Bug Fixes

  • breakpointTailwindCSS: generate correct matchMedia when using tailwindconfig (#455) (c2d3549)

Features

  • useIntlDateTimeFormat: add useIntlDateTimeFormat composable (#329) (8ab953f)

1.0.0-dev.36 (2020-06-14)

Bug Fixes

  • useOnResize: allow to retrieve the size on mount if ref is passed (e52dadb), closes #364

1.0.0-dev.35 (2020-06-13)

Features

  • @vue/composition-api: update to 0.6.2 (0a24556)
  • @vue/composition-api: updated to 0.6.0 (c554c55)
  • useUndo: add useUndo composable (#347) (63aa3f0)

1.0.0-dev.34 (2020-06-03)

Features

  • useBreakpointTailwind: use injectFactory instead of inject (e8d0e9c)
  • vmodel: add useVModel composable (#337) (f703908)
  • add injectFactory composable (#324) (f571463)
  • useHydration: add useHydration composable (5af0bc7)

1.0.0-dev.33 (2020-05-31)

Features

  • useTitle: useTitle composable (#331) (42670d3)
  • add __SSR__ from process.env.SSR === 'true' (ebe2f43)
  • improve return types and update deps, fixing outstanding PRs (#316) (c2e0afc)

1.0.0-dev.32 (2020-04-22)

Bug Fixes

  • Build wrongly outputting __VUE_2__ env variable instead of replacing

Features

  • Export COMMIT, VERSION and VUE_VERSION in the index.ts

1.0.0-dev.29 (2020-04-22)

Bug Fixes

  • 3.0.0-dev.26 fix build (6e9380b)
  • typings and publish script (ac9e78f)

Features

1.0.0-dev.21 (2020-04-13)

Features

  • intl added numberformat and currencyFormat (#191) (5e5ff20)

1.0.0-dev.20 (2020-04-06)

Types

1.0.0-dev.18 (2020-04-06)

Features

1.0.0-dev.17 (2020-04-05)

Changes

Added

  • i18n - Added $tc, same as $t but returns a string, sugar for usage in the template.

Fixes

  • [useValidation] - Fix tracking of $value when is not ref
  • TailwindCSS - improve typings and fix bug when sending custom breakpoints

1.0.0-dev.16 (2020-03-27)

Bug Fixes

Features

  • execute promise immediately BREAKING CHANGE (#197) (57c24d1), closes #194
  • Only update usePromise.result when the promise is complete (#190) (dcfbd9c)

1.0.0-dev.15 (2020-03-09)

Bug Fixes

  • i18n: bug loading the default locale correctly (8c2ee07), closes #167

1.0.0-dev.14 (2020-03-05)

Features

1.0.0-dev.13 (2020-02-27)

Breaking change

1.0.0-dev.12 (2020-02-27)

Features

  • onScroll: add scrollTopTo and scrollLeftTo functions and watch scrollTop and ScrollLeft refs (c003669)

1.0.0-dev.11 (2020-02-24)

Features

1.0.0-dev.10 (2020-02-23)

Features

1.0.0-dev.9 (2020-02-07)

Features

  • fetch: add abortController and allow execute when called with options (#130) (275df33)
  • web: allow onScroll to default to window, if no argument is passed and expose scrollTO (27bd03b)

1.0.0-dev.8 (2020-02-04)

Bug Fixes

  • docs: remove setInterval when bulding docs (23ed997)

Features

  • add geolocation API (#86) (7ebf12d)
  • add nuxtJS and SSR support (#109) (4d73885)
  • useBroadcastChannel, refShared and useSharedRef (#104) (ebf33e1)
  • axios: allow calling useAxios.exec with string (58b11ba)

1.0.0-dev.7 (2020-01-19)

Bug Fixes

  • Fix readme storage links

1.0.0-dev.6 (2020-01-19)

Features

1.0.0-dev.4 (2020-01-13)

Features

1.0.0-dev.2 (2019-12-28)

Features

0.2.2 (2019-12-10)

Bug Fixes

Reverts

  • Revert "chore: use optional chaining" (e96ca76)

0.2.0 (2019-11-03)

Bug Fixes

Features

0.1.0 (2019-10-23)

Bug Fixes

  • cicle ci and removing lodash.debounce depency (9172dd5)
  • circle.ci config test (2f3568e)

Features

  • add examples, TODO tests and fix readme (da3411b)
  • added licence and readme (6585b14)
  • coveralls setup (b2d2a0a)
  • rolllup config improvements, adding tests, circle ci (e5a190d)