@reause/integrations 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.iife.js +1621 -0
- package/dist/index.iife.min.js +1 -0
- package/dist/index.js +13 -0
- package/dist/useAsyncValidator.d.ts +83 -0
- package/dist/useAsyncValidator.iife.js +192 -0
- package/dist/useAsyncValidator.iife.min.js +1 -0
- package/dist/useAsyncValidator.js +168 -0
- package/dist/useAxios.d.ts +125 -0
- package/dist/useAxios.iife.js +288 -0
- package/dist/useAxios.iife.min.js +1 -0
- package/dist/useAxios.js +265 -0
- package/dist/useChangeCase.d.ts +57 -0
- package/dist/useChangeCase.iife.js +91 -0
- package/dist/useChangeCase.iife.min.js +1 -0
- package/dist/useChangeCase.js +68 -0
- package/dist/useCookies.d.ts +117 -0
- package/dist/useCookies.iife.js +142 -0
- package/dist/useCookies.iife.min.js +1 -0
- package/dist/useCookies.js +117 -0
- package/dist/useDrauu.d.ts +152 -0
- package/dist/useDrauu.iife.js +221 -0
- package/dist/useDrauu.iife.min.js +1 -0
- package/dist/useDrauu.js +221 -0
- package/dist/useFocusTrap.d.ts +116 -0
- package/dist/useFocusTrap.iife.js +125 -0
- package/dist/useFocusTrap.iife.min.js +1 -0
- package/dist/useFocusTrap.js +125 -0
- package/dist/useFuse.d.ts +86 -0
- package/dist/useFuse.iife.js +95 -0
- package/dist/useFuse.iife.min.js +1 -0
- package/dist/useFuse.js +72 -0
- package/dist/useIDBKeyval.d.ts +139 -0
- package/dist/useIDBKeyval.iife.js +175 -0
- package/dist/useIDBKeyval.iife.min.js +1 -0
- package/dist/useIDBKeyval.js +174 -0
- package/dist/useJwt.d.ts +52 -0
- package/dist/useJwt.iife.js +54 -0
- package/dist/useJwt.iife.min.js +1 -0
- package/dist/useJwt.js +53 -0
- package/dist/useNProgress.d.ts +115 -0
- package/dist/useNProgress.iife.js +148 -0
- package/dist/useNProgress.iife.min.js +1 -0
- package/dist/useNProgress.js +125 -0
- package/dist/useQRCode.d.ts +38 -0
- package/dist/useQRCode.iife.js +78 -0
- package/dist/useQRCode.iife.min.js +1 -0
- package/dist/useQRCode.js +55 -0
- package/dist/useSortable.d.ts +143 -0
- package/dist/useSortable.iife.js +199 -0
- package/dist/useSortable.iife.min.js +1 -0
- package/dist/useSortable.js +173 -0
- package/package.json +92 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
(function(exports, _reause_shared, change_case, react) {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
//#region \0rolldown/runtime.js
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
21
|
+
value: mod,
|
|
22
|
+
enumerable: true
|
|
23
|
+
}) : target, mod));
|
|
24
|
+
//#endregion
|
|
25
|
+
change_case = __toESM(change_case, 1);
|
|
26
|
+
//#region useChangeCase/index.tsx
|
|
27
|
+
const changeCaseTransforms = /* @__PURE__ */ Object.entries(change_case).filter((entry) => {
|
|
28
|
+
const [name, fn] = entry;
|
|
29
|
+
return typeof fn === "function" && name.endsWith("Case");
|
|
30
|
+
}).reduce((acc, [name, fn]) => {
|
|
31
|
+
acc[name] = fn;
|
|
32
|
+
return acc;
|
|
33
|
+
}, {});
|
|
34
|
+
/**
|
|
35
|
+
* React port of VueUse's `useChangeCase`.
|
|
36
|
+
*
|
|
37
|
+
* Map from @vueuse/integrations `useChangeCase`
|
|
38
|
+
* (`source/vueuse/packages/integrations/useChangeCase/`), a reactive wrapper
|
|
39
|
+
* around the `change-case` package. Upstream returns a writable
|
|
40
|
+
* `WritableComputedRef<string>`; here the writable computed ref maps to a
|
|
41
|
+
* `[value, setValue]` tuple: `value` is the transformed string (`change-case`
|
|
42
|
+
* applied to the internal input state with the current `type`), and
|
|
43
|
+
* `setValue` updates that internal input state like a controlled `useState`.
|
|
44
|
+
* `input` is the hook's **read-only value source** and takes a plain `string`
|
|
45
|
+
* (upstream: `MaybeRef<string>` / `MaybeRefOrGetter<string>`); `type` and
|
|
46
|
+
* `options` stay `RefOrValue` (format knobs, upstream `MaybeRefOrGetter`) and
|
|
47
|
+
* are resolved with `toValue` from `@reause/shared`.
|
|
48
|
+
*
|
|
49
|
+
* Adjustment for React:
|
|
50
|
+
* - upstream's writable computed captures a plain `input` once at setup. Here
|
|
51
|
+
* a changed `input` prop re-syncs the internal state on the next render, so
|
|
52
|
+
* a parent re-render with a new string is reflected; a `setValue` write is
|
|
53
|
+
* never clobbered while the `input` prop is unchanged (the baseline records
|
|
54
|
+
* the last externally synced value);
|
|
55
|
+
* - writes are **not** propagated back to the caller: `setValue` updates the
|
|
56
|
+
* internal input state only (upstream's writable computed writes through to
|
|
57
|
+
* a ref input). A changed `input` prop always wins over the internal state;
|
|
58
|
+
* - `setValue` writes the RAW string; the transform is re-applied on the next
|
|
59
|
+
* render (the upstream computed's `get` re-derives from the ref on access).
|
|
60
|
+
*
|
|
61
|
+
* @__NO_SIDE_EFFECTS__
|
|
62
|
+
* @example
|
|
63
|
+
* const [changeCase, setChangeCase] = useChangeCase('hello world', 'camelCase')
|
|
64
|
+
* changeCase // 'helloWorld'
|
|
65
|
+
* setChangeCase('vue use')
|
|
66
|
+
* changeCase // 'vueUse'
|
|
67
|
+
*/
|
|
68
|
+
function useChangeCase(input, type, options) {
|
|
69
|
+
const [text, setText] = (0, react.useState)(input);
|
|
70
|
+
const lastExternalRef = (0, react.useRef)(input);
|
|
71
|
+
(0, react.useEffect)(() => {
|
|
72
|
+
if (!Object.is(input, lastExternalRef.current)) {
|
|
73
|
+
lastExternalRef.current = input;
|
|
74
|
+
setText(input);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
const typeName = (0, _reause_shared.toValue)(type);
|
|
78
|
+
const resolvedOptions = (0, _reause_shared.toValue)(options);
|
|
79
|
+
return [(0, react.useMemo)(() => {
|
|
80
|
+
const transform = changeCaseTransforms[typeName];
|
|
81
|
+
if (!transform) throw new Error(`Invalid change case type "${typeName}"`);
|
|
82
|
+
return transform(text, resolvedOptions);
|
|
83
|
+
}, [
|
|
84
|
+
text,
|
|
85
|
+
typeName,
|
|
86
|
+
resolvedOptions
|
|
87
|
+
]), setText];
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
exports.useChangeCase = useChangeCase;
|
|
91
|
+
})(this.reause = this.reause || {}, reause, changeCase, React);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var i=Object.create,a=Object.defineProperty,o=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,c=Object.getPrototypeOf,l=Object.prototype.hasOwnProperty,u=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var i=s(t),c=0,u=i.length,d;c<u;c++)d=i[c],!l.call(e,d)&&d!==n&&a(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(r=o(t,d))||r.enumerable});return e};n=((e,t,n)=>(n=e==null?{}:i(c(e)),u(t||!e||!e.__esModule||!l.call(e,`default`)?a(n,`default`,{value:e,enumerable:!0}):n,e)))(n,1);let d=Object.entries(n).filter(e=>{let[t,n]=e;return typeof n==`function`&&t.endsWith(`Case`)}).reduce((e,[t,n])=>(e[t]=n,e),{});function f(e,n,i){let[a,o]=(0,r.useState)(e),s=(0,r.useRef)(e);(0,r.useEffect)(()=>{Object.is(e,s.current)||(s.current=e,o(e))});let c=(0,t.toValue)(n),l=(0,t.toValue)(i);return[(0,r.useMemo)(()=>{let e=d[c];if(!e)throw Error(`Invalid change case type "${c}"`);return e(a,l)},[a,c,l]),o]}e.useChangeCase=f})(this.reause=this.reause||{},reause,changeCase,React);
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { toValue } from "@reause/shared";
|
|
3
|
+
import * as changeCase from "change-case";
|
|
4
|
+
//#region useChangeCase/index.tsx
|
|
5
|
+
const changeCaseTransforms = /* @__PURE__ */ Object.entries(changeCase).filter((entry) => {
|
|
6
|
+
const [name, fn] = entry;
|
|
7
|
+
return typeof fn === "function" && name.endsWith("Case");
|
|
8
|
+
}).reduce((acc, [name, fn]) => {
|
|
9
|
+
acc[name] = fn;
|
|
10
|
+
return acc;
|
|
11
|
+
}, {});
|
|
12
|
+
/**
|
|
13
|
+
* React port of VueUse's `useChangeCase`.
|
|
14
|
+
*
|
|
15
|
+
* Map from @vueuse/integrations `useChangeCase`
|
|
16
|
+
* (`source/vueuse/packages/integrations/useChangeCase/`), a reactive wrapper
|
|
17
|
+
* around the `change-case` package. Upstream returns a writable
|
|
18
|
+
* `WritableComputedRef<string>`; here the writable computed ref maps to a
|
|
19
|
+
* `[value, setValue]` tuple: `value` is the transformed string (`change-case`
|
|
20
|
+
* applied to the internal input state with the current `type`), and
|
|
21
|
+
* `setValue` updates that internal input state like a controlled `useState`.
|
|
22
|
+
* `input` is the hook's **read-only value source** and takes a plain `string`
|
|
23
|
+
* (upstream: `MaybeRef<string>` / `MaybeRefOrGetter<string>`); `type` and
|
|
24
|
+
* `options` stay `RefOrValue` (format knobs, upstream `MaybeRefOrGetter`) and
|
|
25
|
+
* are resolved with `toValue` from `@reause/shared`.
|
|
26
|
+
*
|
|
27
|
+
* Adjustment for React:
|
|
28
|
+
* - upstream's writable computed captures a plain `input` once at setup. Here
|
|
29
|
+
* a changed `input` prop re-syncs the internal state on the next render, so
|
|
30
|
+
* a parent re-render with a new string is reflected; a `setValue` write is
|
|
31
|
+
* never clobbered while the `input` prop is unchanged (the baseline records
|
|
32
|
+
* the last externally synced value);
|
|
33
|
+
* - writes are **not** propagated back to the caller: `setValue` updates the
|
|
34
|
+
* internal input state only (upstream's writable computed writes through to
|
|
35
|
+
* a ref input). A changed `input` prop always wins over the internal state;
|
|
36
|
+
* - `setValue` writes the RAW string; the transform is re-applied on the next
|
|
37
|
+
* render (the upstream computed's `get` re-derives from the ref on access).
|
|
38
|
+
*
|
|
39
|
+
* @__NO_SIDE_EFFECTS__
|
|
40
|
+
* @example
|
|
41
|
+
* const [changeCase, setChangeCase] = useChangeCase('hello world', 'camelCase')
|
|
42
|
+
* changeCase // 'helloWorld'
|
|
43
|
+
* setChangeCase('vue use')
|
|
44
|
+
* changeCase // 'vueUse'
|
|
45
|
+
*/
|
|
46
|
+
function useChangeCase(input, type, options) {
|
|
47
|
+
const [text, setText] = useState(input);
|
|
48
|
+
const lastExternalRef = useRef(input);
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (!Object.is(input, lastExternalRef.current)) {
|
|
51
|
+
lastExternalRef.current = input;
|
|
52
|
+
setText(input);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
const typeName = toValue(type);
|
|
56
|
+
const resolvedOptions = toValue(options);
|
|
57
|
+
return [useMemo(() => {
|
|
58
|
+
const transform = changeCaseTransforms[typeName];
|
|
59
|
+
if (!transform) throw new Error(`Invalid change case type "${typeName}"`);
|
|
60
|
+
return transform(text, resolvedOptions);
|
|
61
|
+
}, [
|
|
62
|
+
text,
|
|
63
|
+
typeName,
|
|
64
|
+
resolvedOptions
|
|
65
|
+
]), setText];
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
export { useChangeCase };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import Cookie from "universal-cookie";
|
|
2
|
+
//#region useCookies/index.d.ts
|
|
3
|
+
type CookieGetOptions = NonNullable<Parameters<Cookie['get']>[1]>;
|
|
4
|
+
type CookieSetOptions = NonNullable<Parameters<Cookie['set']>[2]>;
|
|
5
|
+
type CookieValue = Parameters<Cookie['set']>[1];
|
|
6
|
+
type CookieChangeListener = Parameters<Cookie['addChangeListener']>[0];
|
|
7
|
+
export interface UseCookiesOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Do not convert the cookie into an object no matter what
|
|
10
|
+
*
|
|
11
|
+
* @default false
|
|
12
|
+
*/
|
|
13
|
+
doNotParse?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Automatically add cookie names ever provided to `get` method
|
|
16
|
+
*
|
|
17
|
+
* @default false
|
|
18
|
+
*/
|
|
19
|
+
autoUpdateDependencies?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface UseCookiesReturn {
|
|
22
|
+
/**
|
|
23
|
+
* Reactive get cookie by name. If **autoUpdateDependencies = true** then it
|
|
24
|
+
* will update watching dependencies
|
|
25
|
+
*/
|
|
26
|
+
get: <T = any>(name: string, options?: CookieGetOptions) => T;
|
|
27
|
+
/**
|
|
28
|
+
* Reactive get all cookies
|
|
29
|
+
*/
|
|
30
|
+
getAll: <T = any>(options?: CookieGetOptions) => T;
|
|
31
|
+
/**
|
|
32
|
+
* Set cookie
|
|
33
|
+
*
|
|
34
|
+
* @see https://www.npmjs.com/package/universal-cookie#setname-value-options
|
|
35
|
+
*/
|
|
36
|
+
set: (name: string, value: CookieValue, options?: CookieSetOptions) => void;
|
|
37
|
+
/**
|
|
38
|
+
* Remove cookie
|
|
39
|
+
*
|
|
40
|
+
* @see https://www.npmjs.com/package/universal-cookie#removename-options
|
|
41
|
+
*/
|
|
42
|
+
remove: (name: string, options?: CookieSetOptions) => void;
|
|
43
|
+
/**
|
|
44
|
+
* Add a listener fired on every cookie change
|
|
45
|
+
*/
|
|
46
|
+
addChangeListener: (callback: CookieChangeListener) => void;
|
|
47
|
+
/**
|
|
48
|
+
* Remove a previously added change listener
|
|
49
|
+
*/
|
|
50
|
+
removeChangeListener: (callback: CookieChangeListener) => void;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Minimal structural request object accepted by {@link createCookies} for SSR.
|
|
54
|
+
*
|
|
55
|
+
* Deviation from upstream: VueUse types this parameter as Node's
|
|
56
|
+
* `IncomingMessage` (`import type { IncomingMessage } from 'node:http'`), which
|
|
57
|
+
* would pull the Node built-in into a browser bundle. Only `headers.cookie` is
|
|
58
|
+
* actually read, so the port declares that shape itself and keeps the browser
|
|
59
|
+
* build free of any `node:` import.
|
|
60
|
+
*/
|
|
61
|
+
export interface CreateCookiesRequest {
|
|
62
|
+
headers?: {
|
|
63
|
+
cookie?: string | null;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* React port of VueUse's `createCookies` — creates a `universal-cookie`
|
|
68
|
+
* instance from a request (default is `window.document.cookie`) and returns a
|
|
69
|
+
* {@link useCookies} bound to that instance.
|
|
70
|
+
*
|
|
71
|
+
* Map from @vueuse/integrations `createCookies`
|
|
72
|
+
* (`source/vueuse/packages/integrations/useCookies/index.ts`).
|
|
73
|
+
*
|
|
74
|
+
* @param req - incoming request (for SSR); a plain `cookie` header string is
|
|
75
|
+
* also accepted for convenience
|
|
76
|
+
* @see https://github.com/reactivestack/cookies/tree/master/packages/universal-cookie universal-cookie
|
|
77
|
+
*
|
|
78
|
+
* @__NO_SIDE_EFFECTS__
|
|
79
|
+
* @example
|
|
80
|
+
* const useSsrCookies = createCookies({ headers: { cookie: 'locale=en-US' } })
|
|
81
|
+
* const { get } = useSsrCookies(['locale'])
|
|
82
|
+
* get('locale') // 'en-US'
|
|
83
|
+
*/
|
|
84
|
+
export declare function createCookies(req?: CreateCookiesRequest | string): (dependencies?: string[] | null, options?: UseCookiesOptions) => UseCookiesReturn;
|
|
85
|
+
/**
|
|
86
|
+
* React port of VueUse's `useCookies` — reactive methods to work with cookies
|
|
87
|
+
* (use {@link createCookies} instead if you are using SSR).
|
|
88
|
+
*
|
|
89
|
+
* Map from @vueuse/integrations `useCookies`
|
|
90
|
+
* (`source/vueuse/packages/integrations/useCookies/index.ts`).
|
|
91
|
+
*
|
|
92
|
+
* Adjustment for React:
|
|
93
|
+
* - upstream returns a **method object** (`get`, `getAll`, `set`, `remove`,
|
|
94
|
+
* `addChangeListener`, `removeChangeListener`), not a piece of writable
|
|
95
|
+
* state, so the port returns the same object instead of a tuple (§2B);
|
|
96
|
+
* - upstream's `shallowRef(0)` "touches" counter is a `useState` counter read
|
|
97
|
+
* in the render body, so a watched cookie change re-renders the component
|
|
98
|
+
* and the `get`/`getAll` closures from that render observe the new value;
|
|
99
|
+
* - `previousCookies` and the mutable watch list live in refs so they persist
|
|
100
|
+
* across renders exactly as upstream's local variables do;
|
|
101
|
+
* - upstream's `tryOnScopeDispose` maps to a `useEffect` cleanup that removes
|
|
102
|
+
* the change listener.
|
|
103
|
+
*
|
|
104
|
+
* @param dependencies - array of watching cookie's names. Pass empty array if don't want to watch cookies changes.
|
|
105
|
+
* @param options
|
|
106
|
+
* @param options.doNotParse - don't try parse value as JSON
|
|
107
|
+
* @param options.autoUpdateDependencies - automatically update watching dependencies
|
|
108
|
+
* @param cookies - universal-cookie instance
|
|
109
|
+
*
|
|
110
|
+
* @__NO_SIDE_EFFECTS__
|
|
111
|
+
* @example
|
|
112
|
+
* const { get, set } = useCookies(['locale'])
|
|
113
|
+
* get('locale') // reads reactively
|
|
114
|
+
* set('locale', 'en-US')
|
|
115
|
+
*/
|
|
116
|
+
export declare function useCookies(dependencies?: string[] | null, { doNotParse, autoUpdateDependencies }?: UseCookiesOptions, cookies?: Cookie): UseCookiesReturn;
|
|
117
|
+
//#endregion
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
(function(exports, react, universal_cookie) {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
//#region \0rolldown/runtime.js
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
21
|
+
value: mod,
|
|
22
|
+
enumerable: true
|
|
23
|
+
}) : target, mod));
|
|
24
|
+
//#endregion
|
|
25
|
+
universal_cookie = __toESM(universal_cookie, 1);
|
|
26
|
+
//#region useCookies/index.tsx
|
|
27
|
+
/**
|
|
28
|
+
* React port of VueUse's `createCookies` — creates a `universal-cookie`
|
|
29
|
+
* instance from a request (default is `window.document.cookie`) and returns a
|
|
30
|
+
* {@link useCookies} bound to that instance.
|
|
31
|
+
*
|
|
32
|
+
* Map from @vueuse/integrations `createCookies`
|
|
33
|
+
* (`source/vueuse/packages/integrations/useCookies/index.ts`).
|
|
34
|
+
*
|
|
35
|
+
* @param req - incoming request (for SSR); a plain `cookie` header string is
|
|
36
|
+
* also accepted for convenience
|
|
37
|
+
* @see https://github.com/reactivestack/cookies/tree/master/packages/universal-cookie universal-cookie
|
|
38
|
+
*
|
|
39
|
+
* @__NO_SIDE_EFFECTS__
|
|
40
|
+
* @example
|
|
41
|
+
* const useSsrCookies = createCookies({ headers: { cookie: 'locale=en-US' } })
|
|
42
|
+
* const { get } = useSsrCookies(['locale'])
|
|
43
|
+
* get('locale') // 'en-US'
|
|
44
|
+
*/
|
|
45
|
+
function createCookies(req) {
|
|
46
|
+
var _req$headers$cookie, _req$headers;
|
|
47
|
+
const universalCookie = new universal_cookie.default(typeof req === "string" ? req : (_req$headers$cookie = req === null || req === void 0 || (_req$headers = req.headers) === null || _req$headers === void 0 ? void 0 : _req$headers.cookie) !== null && _req$headers$cookie !== void 0 ? _req$headers$cookie : null);
|
|
48
|
+
return (dependencies, { doNotParse = false, autoUpdateDependencies = false } = {}) => useCookies(dependencies, {
|
|
49
|
+
doNotParse,
|
|
50
|
+
autoUpdateDependencies
|
|
51
|
+
}, universalCookie);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* React port of VueUse's `useCookies` — reactive methods to work with cookies
|
|
55
|
+
* (use {@link createCookies} instead if you are using SSR).
|
|
56
|
+
*
|
|
57
|
+
* Map from @vueuse/integrations `useCookies`
|
|
58
|
+
* (`source/vueuse/packages/integrations/useCookies/index.ts`).
|
|
59
|
+
*
|
|
60
|
+
* Adjustment for React:
|
|
61
|
+
* - upstream returns a **method object** (`get`, `getAll`, `set`, `remove`,
|
|
62
|
+
* `addChangeListener`, `removeChangeListener`), not a piece of writable
|
|
63
|
+
* state, so the port returns the same object instead of a tuple (§2B);
|
|
64
|
+
* - upstream's `shallowRef(0)` "touches" counter is a `useState` counter read
|
|
65
|
+
* in the render body, so a watched cookie change re-renders the component
|
|
66
|
+
* and the `get`/`getAll` closures from that render observe the new value;
|
|
67
|
+
* - `previousCookies` and the mutable watch list live in refs so they persist
|
|
68
|
+
* across renders exactly as upstream's local variables do;
|
|
69
|
+
* - upstream's `tryOnScopeDispose` maps to a `useEffect` cleanup that removes
|
|
70
|
+
* the change listener.
|
|
71
|
+
*
|
|
72
|
+
* @param dependencies - array of watching cookie's names. Pass empty array if don't want to watch cookies changes.
|
|
73
|
+
* @param options
|
|
74
|
+
* @param options.doNotParse - don't try parse value as JSON
|
|
75
|
+
* @param options.autoUpdateDependencies - automatically update watching dependencies
|
|
76
|
+
* @param cookies - universal-cookie instance
|
|
77
|
+
*
|
|
78
|
+
* @__NO_SIDE_EFFECTS__
|
|
79
|
+
* @example
|
|
80
|
+
* const { get, set } = useCookies(['locale'])
|
|
81
|
+
* get('locale') // reads reactively
|
|
82
|
+
* set('locale', 'en-US')
|
|
83
|
+
*/
|
|
84
|
+
function useCookies(dependencies, { doNotParse = false, autoUpdateDependencies = false } = {}, cookies = new universal_cookie.default()) {
|
|
85
|
+
const watchingDependenciesRef = (0, react.useRef)(autoUpdateDependencies ? [...dependencies || []] : dependencies);
|
|
86
|
+
const previousCookiesRef = (0, react.useRef)(null);
|
|
87
|
+
if (previousCookiesRef.current === null) previousCookiesRef.current = cookies.getAll({ doNotParse: true });
|
|
88
|
+
const [touches, setTouches] = (0, react.useState)(0);
|
|
89
|
+
const previousDependenciesRef = (0, react.useRef)(dependencies);
|
|
90
|
+
(0, react.useEffect)(() => {
|
|
91
|
+
if (previousDependenciesRef.current === dependencies) return;
|
|
92
|
+
previousDependenciesRef.current = dependencies;
|
|
93
|
+
if (!autoUpdateDependencies) watchingDependenciesRef.current = dependencies;
|
|
94
|
+
}, [dependencies, autoUpdateDependencies]);
|
|
95
|
+
const onChange = (0, react.useCallback)(() => {
|
|
96
|
+
var _watchingDependencies;
|
|
97
|
+
const newCookies = cookies.getAll({ doNotParse: true });
|
|
98
|
+
const previous = previousCookiesRef.current;
|
|
99
|
+
if (previous === null) {
|
|
100
|
+
previousCookiesRef.current = newCookies;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (shouldUpdate((_watchingDependencies = watchingDependenciesRef.current) !== null && _watchingDependencies !== void 0 ? _watchingDependencies : null, newCookies, previous)) setTouches((n) => n + 1);
|
|
104
|
+
previousCookiesRef.current = newCookies;
|
|
105
|
+
}, [cookies]);
|
|
106
|
+
(0, react.useEffect)(() => {
|
|
107
|
+
cookies.addChangeListener(onChange);
|
|
108
|
+
return () => {
|
|
109
|
+
cookies.removeChangeListener(onChange);
|
|
110
|
+
};
|
|
111
|
+
}, [cookies, onChange]);
|
|
112
|
+
return {
|
|
113
|
+
get: (name, options) => {
|
|
114
|
+
const watchingDependencies = watchingDependenciesRef.current;
|
|
115
|
+
/**
|
|
116
|
+
* Auto update watching dependencies if needed
|
|
117
|
+
*/
|
|
118
|
+
if (autoUpdateDependencies && watchingDependencies && !watchingDependencies.includes(name)) watchingDependencies.push(name);
|
|
119
|
+
return cookies.get(name, {
|
|
120
|
+
doNotParse,
|
|
121
|
+
...options
|
|
122
|
+
});
|
|
123
|
+
},
|
|
124
|
+
getAll: (options) => cookies.getAll({
|
|
125
|
+
doNotParse,
|
|
126
|
+
...options
|
|
127
|
+
}),
|
|
128
|
+
set: (name, value, options) => cookies.set(name, value, options),
|
|
129
|
+
remove: (name, options) => cookies.remove(name, options),
|
|
130
|
+
addChangeListener: (callback) => cookies.addChangeListener(callback),
|
|
131
|
+
removeChangeListener: (callback) => cookies.removeChangeListener(callback)
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function shouldUpdate(dependencies, newCookies, oldCookies) {
|
|
135
|
+
if (!dependencies) return true;
|
|
136
|
+
for (const dependency of dependencies) if (newCookies[dependency] !== oldCookies[dependency]) return true;
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
//#endregion
|
|
140
|
+
exports.createCookies = createCookies;
|
|
141
|
+
exports.useCookies = useCookies;
|
|
142
|
+
})(this.reause = this.reause || {}, React, UniversalCookie);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t,n){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var r=Object.create,i=Object.defineProperty,a=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.getPrototypeOf,c=Object.prototype.hasOwnProperty,l=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var s=o(t),l=0,u=s.length,d;l<u;l++)d=s[l],!c.call(e,d)&&d!==n&&i(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(r=a(t,d))||r.enumerable});return e};n=((e,t,n)=>(n=e==null?{}:r(s(e)),l(t||!e||!e.__esModule||!c.call(e,`default`)?i(n,`default`,{value:e,enumerable:!0}):n,e)))(n,1);function u(e){var t,r;let i=new n.default(typeof e==`string`?e:(t=e==null||(r=e.headers)==null?void 0:r.cookie)==null?null:t);return(e,{doNotParse:t=!1,autoUpdateDependencies:n=!1}={})=>d(e,{doNotParse:t,autoUpdateDependencies:n},i)}function d(e,{doNotParse:r=!1,autoUpdateDependencies:i=!1}={},a=new n.default){let o=(0,t.useRef)(i?[...e||[]]:e),s=(0,t.useRef)(null);s.current===null&&(s.current=a.getAll({doNotParse:!0}));let[c,l]=(0,t.useState)(0),u=(0,t.useRef)(e);(0,t.useEffect)(()=>{u.current!==e&&(u.current=e,i||(o.current=e))},[e,i]);let d=(0,t.useCallback)(()=>{var e;let t=a.getAll({doNotParse:!0}),n=s.current;if(n===null){s.current=t;return}f((e=o.current)==null?null:e,t,n)&&l(e=>e+1),s.current=t},[a]);return(0,t.useEffect)(()=>(a.addChangeListener(d),()=>{a.removeChangeListener(d)}),[a,d]),{get:(e,t)=>{let n=o.current;return i&&n&&!n.includes(e)&&n.push(e),a.get(e,{doNotParse:r,...t})},getAll:e=>a.getAll({doNotParse:r,...e}),set:(e,t,n)=>a.set(e,t,n),remove:(e,t)=>a.remove(e,t),addChangeListener:e=>a.addChangeListener(e),removeChangeListener:e=>a.removeChangeListener(e)}}function f(e,t,n){if(!e)return!0;for(let r of e)if(t[r]!==n[r])return!0;return!1}e.createCookies=u,e.useCookies=d})(this.reause=this.reause||{},React,UniversalCookie);
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
import Cookie from "universal-cookie";
|
|
3
|
+
//#region useCookies/index.tsx
|
|
4
|
+
/**
|
|
5
|
+
* React port of VueUse's `createCookies` — creates a `universal-cookie`
|
|
6
|
+
* instance from a request (default is `window.document.cookie`) and returns a
|
|
7
|
+
* {@link useCookies} bound to that instance.
|
|
8
|
+
*
|
|
9
|
+
* Map from @vueuse/integrations `createCookies`
|
|
10
|
+
* (`source/vueuse/packages/integrations/useCookies/index.ts`).
|
|
11
|
+
*
|
|
12
|
+
* @param req - incoming request (for SSR); a plain `cookie` header string is
|
|
13
|
+
* also accepted for convenience
|
|
14
|
+
* @see https://github.com/reactivestack/cookies/tree/master/packages/universal-cookie universal-cookie
|
|
15
|
+
*
|
|
16
|
+
* @__NO_SIDE_EFFECTS__
|
|
17
|
+
* @example
|
|
18
|
+
* const useSsrCookies = createCookies({ headers: { cookie: 'locale=en-US' } })
|
|
19
|
+
* const { get } = useSsrCookies(['locale'])
|
|
20
|
+
* get('locale') // 'en-US'
|
|
21
|
+
*/
|
|
22
|
+
function createCookies(req) {
|
|
23
|
+
var _req$headers$cookie, _req$headers;
|
|
24
|
+
const universalCookie = new Cookie(typeof req === "string" ? req : (_req$headers$cookie = req === null || req === void 0 || (_req$headers = req.headers) === null || _req$headers === void 0 ? void 0 : _req$headers.cookie) !== null && _req$headers$cookie !== void 0 ? _req$headers$cookie : null);
|
|
25
|
+
return (dependencies, { doNotParse = false, autoUpdateDependencies = false } = {}) => useCookies(dependencies, {
|
|
26
|
+
doNotParse,
|
|
27
|
+
autoUpdateDependencies
|
|
28
|
+
}, universalCookie);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* React port of VueUse's `useCookies` — reactive methods to work with cookies
|
|
32
|
+
* (use {@link createCookies} instead if you are using SSR).
|
|
33
|
+
*
|
|
34
|
+
* Map from @vueuse/integrations `useCookies`
|
|
35
|
+
* (`source/vueuse/packages/integrations/useCookies/index.ts`).
|
|
36
|
+
*
|
|
37
|
+
* Adjustment for React:
|
|
38
|
+
* - upstream returns a **method object** (`get`, `getAll`, `set`, `remove`,
|
|
39
|
+
* `addChangeListener`, `removeChangeListener`), not a piece of writable
|
|
40
|
+
* state, so the port returns the same object instead of a tuple (§2B);
|
|
41
|
+
* - upstream's `shallowRef(0)` "touches" counter is a `useState` counter read
|
|
42
|
+
* in the render body, so a watched cookie change re-renders the component
|
|
43
|
+
* and the `get`/`getAll` closures from that render observe the new value;
|
|
44
|
+
* - `previousCookies` and the mutable watch list live in refs so they persist
|
|
45
|
+
* across renders exactly as upstream's local variables do;
|
|
46
|
+
* - upstream's `tryOnScopeDispose` maps to a `useEffect` cleanup that removes
|
|
47
|
+
* the change listener.
|
|
48
|
+
*
|
|
49
|
+
* @param dependencies - array of watching cookie's names. Pass empty array if don't want to watch cookies changes.
|
|
50
|
+
* @param options
|
|
51
|
+
* @param options.doNotParse - don't try parse value as JSON
|
|
52
|
+
* @param options.autoUpdateDependencies - automatically update watching dependencies
|
|
53
|
+
* @param cookies - universal-cookie instance
|
|
54
|
+
*
|
|
55
|
+
* @__NO_SIDE_EFFECTS__
|
|
56
|
+
* @example
|
|
57
|
+
* const { get, set } = useCookies(['locale'])
|
|
58
|
+
* get('locale') // reads reactively
|
|
59
|
+
* set('locale', 'en-US')
|
|
60
|
+
*/
|
|
61
|
+
function useCookies(dependencies, { doNotParse = false, autoUpdateDependencies = false } = {}, cookies = new Cookie()) {
|
|
62
|
+
const watchingDependenciesRef = useRef(autoUpdateDependencies ? [...dependencies || []] : dependencies);
|
|
63
|
+
const previousCookiesRef = useRef(null);
|
|
64
|
+
if (previousCookiesRef.current === null) previousCookiesRef.current = cookies.getAll({ doNotParse: true });
|
|
65
|
+
const [touches, setTouches] = useState(0);
|
|
66
|
+
const previousDependenciesRef = useRef(dependencies);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (previousDependenciesRef.current === dependencies) return;
|
|
69
|
+
previousDependenciesRef.current = dependencies;
|
|
70
|
+
if (!autoUpdateDependencies) watchingDependenciesRef.current = dependencies;
|
|
71
|
+
}, [dependencies, autoUpdateDependencies]);
|
|
72
|
+
const onChange = useCallback(() => {
|
|
73
|
+
var _watchingDependencies;
|
|
74
|
+
const newCookies = cookies.getAll({ doNotParse: true });
|
|
75
|
+
const previous = previousCookiesRef.current;
|
|
76
|
+
if (previous === null) {
|
|
77
|
+
previousCookiesRef.current = newCookies;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (shouldUpdate((_watchingDependencies = watchingDependenciesRef.current) !== null && _watchingDependencies !== void 0 ? _watchingDependencies : null, newCookies, previous)) setTouches((n) => n + 1);
|
|
81
|
+
previousCookiesRef.current = newCookies;
|
|
82
|
+
}, [cookies]);
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
cookies.addChangeListener(onChange);
|
|
85
|
+
return () => {
|
|
86
|
+
cookies.removeChangeListener(onChange);
|
|
87
|
+
};
|
|
88
|
+
}, [cookies, onChange]);
|
|
89
|
+
return {
|
|
90
|
+
get: (name, options) => {
|
|
91
|
+
const watchingDependencies = watchingDependenciesRef.current;
|
|
92
|
+
/**
|
|
93
|
+
* Auto update watching dependencies if needed
|
|
94
|
+
*/
|
|
95
|
+
if (autoUpdateDependencies && watchingDependencies && !watchingDependencies.includes(name)) watchingDependencies.push(name);
|
|
96
|
+
return cookies.get(name, {
|
|
97
|
+
doNotParse,
|
|
98
|
+
...options
|
|
99
|
+
});
|
|
100
|
+
},
|
|
101
|
+
getAll: (options) => cookies.getAll({
|
|
102
|
+
doNotParse,
|
|
103
|
+
...options
|
|
104
|
+
}),
|
|
105
|
+
set: (name, value, options) => cookies.set(name, value, options),
|
|
106
|
+
remove: (name, options) => cookies.remove(name, options),
|
|
107
|
+
addChangeListener: (callback) => cookies.addChangeListener(callback),
|
|
108
|
+
removeChangeListener: (callback) => cookies.removeChangeListener(callback)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function shouldUpdate(dependencies, newCookies, oldCookies) {
|
|
112
|
+
if (!dependencies) return true;
|
|
113
|
+
for (const dependency of dependencies) if (newCookies[dependency] !== oldCookies[dependency]) return true;
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
//#endregion
|
|
117
|
+
export { createCookies, useCookies };
|