UNPKG

vue-hooks-plus

Version:
34 lines (33 loc) 1.28 kB
"use strict"; const Cookies = require("js-cookie"); const vue = require("vue"); const isFunction = require("../utils/isFunction"); const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e }; const Cookies__default = /* @__PURE__ */ _interopDefaultLegacy(Cookies); function useCookieState(cookieKey, options = {}) { const defaultValue = () => { const cookieValue = Cookies__default.default.get(cookieKey); if (typeof cookieValue === "string") return cookieValue; if (isFunction.isFunction(options.defaultValue)) { return options.defaultValue(); } return options.defaultValue; }; const state = vue.ref(defaultValue()); const updateState = (newValue, newOptions = {}) => { const { defaultValue: defaultValue2, ...restOptions } = { ...options, ...newOptions }; const getValue = () => { const value = isFunction.isFunction(newValue) ? newValue(state.value) : newValue; if (value === void 0) { Cookies__default.default.remove(cookieKey); } else { Cookies__default.default.set(cookieKey, value, restOptions); } return value; }; state.value = getValue(); }; return [vue.readonly(state), updateState]; } module.exports = useCookieState;