@reactuses/core 5.0.18 → 5.0.19-beta.1
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/README.md +13 -13
- package/dist/index.cjs +224 -10
- package/dist/index.d.cts +180 -1
- package/dist/index.d.mts +180 -1
- package/dist/index.d.ts +180 -1
- package/dist/index.mjs +221 -11
- package/dist/useQRCode.cjs +95 -0
- package/dist/useQRCode.d.cts +36 -0
- package/dist/useQRCode.d.mts +36 -0
- package/dist/useQRCode.mjs +88 -0
- package/package.json +19 -1
package/dist/index.mjs
CHANGED
|
@@ -199,7 +199,7 @@ const useClickOutside = (target, handler, enabled = true)=>{
|
|
|
199
199
|
useEventListener('touchstart', listener, defaultWindow, listerOptions);
|
|
200
200
|
};
|
|
201
201
|
|
|
202
|
-
function getInitialState$
|
|
202
|
+
function getInitialState$5(key, defaultValue) {
|
|
203
203
|
// Prevent a React hydration mismatch when a default value is provided.
|
|
204
204
|
if (defaultValue !== undefined) {
|
|
205
205
|
return defaultValue;
|
|
@@ -213,7 +213,7 @@ function getInitialState$4(key, defaultValue) {
|
|
|
213
213
|
return '';
|
|
214
214
|
}
|
|
215
215
|
const useCookie = (key, options = defaultOptions$1, defaultValue)=>{
|
|
216
|
-
const [cookieValue, setCookieValue] = useState(getInitialState$
|
|
216
|
+
const [cookieValue, setCookieValue] = useState(getInitialState$5(key, defaultValue));
|
|
217
217
|
useEffect(()=>{
|
|
218
218
|
const getStoredValue = ()=>{
|
|
219
219
|
const raw = Cookies.get(key);
|
|
@@ -434,7 +434,7 @@ const defaultOptions = {
|
|
|
434
434
|
observe: false
|
|
435
435
|
};
|
|
436
436
|
|
|
437
|
-
function getInitialState$
|
|
437
|
+
function getInitialState$4(defaultValue) {
|
|
438
438
|
// Prevent a React hydration mismatch when a default value is provided.
|
|
439
439
|
if (defaultValue !== undefined) {
|
|
440
440
|
return defaultValue;
|
|
@@ -449,7 +449,7 @@ function getInitialState$3(defaultValue) {
|
|
|
449
449
|
}
|
|
450
450
|
const useCssVar = (prop, target, defaultValue, options = defaultOptions)=>{
|
|
451
451
|
const { observe } = options;
|
|
452
|
-
const [variable, setVariable] = useState(getInitialState$
|
|
452
|
+
const [variable, setVariable] = useState(getInitialState$4(defaultValue));
|
|
453
453
|
const observerRef = useRef();
|
|
454
454
|
const set = useCallback((v)=>{
|
|
455
455
|
const element = getTargetElement(target);
|
|
@@ -571,7 +571,7 @@ const StorageSerializers = {
|
|
|
571
571
|
write: (v)=>v.toISOString()
|
|
572
572
|
}
|
|
573
573
|
};
|
|
574
|
-
function getInitialState$
|
|
574
|
+
function getInitialState$3(key, defaultValue, storage, serializer, onError) {
|
|
575
575
|
// Prevent a React hydration mismatch when a default value is provided.
|
|
576
576
|
if (defaultValue !== undefined) {
|
|
577
577
|
return defaultValue;
|
|
@@ -604,7 +604,7 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
|
|
|
604
604
|
const type = guessSerializerType(defaultValue);
|
|
605
605
|
var _options_serializer;
|
|
606
606
|
const serializer = (_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type];
|
|
607
|
-
const [state, setState] = useState(getInitialState$
|
|
607
|
+
const [state, setState] = useState(getInitialState$3(key, defaultValue, storage, serializer, onError));
|
|
608
608
|
useDeepCompareEffect(()=>{
|
|
609
609
|
var _ref;
|
|
610
610
|
const data = (_ref = effectStorageValue ? isFunction(effectStorageValue) ? effectStorageValue() : effectStorageValue : defaultValue) != null ? _ref : null;
|
|
@@ -747,7 +747,7 @@ const useDebounce = (value, wait, options)=>{
|
|
|
747
747
|
return debounced;
|
|
748
748
|
};
|
|
749
749
|
|
|
750
|
-
function getInitialState$
|
|
750
|
+
function getInitialState$2(defaultValue) {
|
|
751
751
|
// Prevent a React hydration mismatch when a default value is provided.
|
|
752
752
|
if (defaultValue !== undefined) {
|
|
753
753
|
return defaultValue;
|
|
@@ -761,7 +761,7 @@ function getInitialState$1(defaultValue) {
|
|
|
761
761
|
return 'visible';
|
|
762
762
|
}
|
|
763
763
|
function useDocumentVisibility(defaultValue) {
|
|
764
|
-
const [visible, setVisible] = useState(getInitialState$
|
|
764
|
+
const [visible, setVisible] = useState(getInitialState$2(defaultValue));
|
|
765
765
|
useEventListener('visibilitychange', ()=>{
|
|
766
766
|
setVisible(document.visibilityState);
|
|
767
767
|
}, ()=>document);
|
|
@@ -2027,7 +2027,7 @@ const useMediaDevices = (options = {})=>{
|
|
|
2027
2027
|
];
|
|
2028
2028
|
};
|
|
2029
2029
|
|
|
2030
|
-
function getInitialState(query, defaultState) {
|
|
2030
|
+
function getInitialState$1(query, defaultState) {
|
|
2031
2031
|
// Prevent a React hydration mismatch when a default value is provided by not defaulting to window.matchMedia(query).matches.
|
|
2032
2032
|
if (defaultState !== undefined) {
|
|
2033
2033
|
return defaultState;
|
|
@@ -2042,7 +2042,7 @@ function getInitialState(query, defaultState) {
|
|
|
2042
2042
|
return false;
|
|
2043
2043
|
}
|
|
2044
2044
|
const useMediaQuery = (query, defaultState)=>{
|
|
2045
|
-
const [state, setState] = useState(getInitialState(query, defaultState));
|
|
2045
|
+
const [state, setState] = useState(getInitialState$1(query, defaultState));
|
|
2046
2046
|
useEffect(()=>{
|
|
2047
2047
|
let mounted = true;
|
|
2048
2048
|
const mql = window.matchMedia(query);
|
|
@@ -3517,4 +3517,214 @@ const useEventSource = (url, events = [], options = defaultOptions$1)=>{
|
|
|
3517
3517
|
}
|
|
3518
3518
|
});
|
|
3519
3519
|
|
|
3520
|
-
|
|
3520
|
+
function getInitialState(defaultState) {
|
|
3521
|
+
// Prevent a React hydration mismatch when a default value is provided by not defaulting to window.matchMedia(query).matches.
|
|
3522
|
+
if (defaultState !== undefined) {
|
|
3523
|
+
return defaultState;
|
|
3524
|
+
}
|
|
3525
|
+
if (isBrowser) {
|
|
3526
|
+
const navigator1 = window.navigator;
|
|
3527
|
+
return navigator1.languages;
|
|
3528
|
+
}
|
|
3529
|
+
// A default value has not been provided, and you are rendering on the server, warn of a possible hydration mismatch when defaulting to false.
|
|
3530
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
3531
|
+
console.warn('`usePreferredLanguage` When server side rendering, defaultState should be defined to prevent a hydration mismatches.');
|
|
3532
|
+
}
|
|
3533
|
+
return [
|
|
3534
|
+
'en'
|
|
3535
|
+
];
|
|
3536
|
+
}
|
|
3537
|
+
const usePreferredLanguages = (defaultLanguages)=>{
|
|
3538
|
+
const [state, setState] = useState(getInitialState(defaultLanguages));
|
|
3539
|
+
useEventListener('languagechange', ()=>{
|
|
3540
|
+
setState(navigator.languages);
|
|
3541
|
+
});
|
|
3542
|
+
return state;
|
|
3543
|
+
};
|
|
3544
|
+
|
|
3545
|
+
const useBroadcastChannel = (options)=>{
|
|
3546
|
+
const { name } = options;
|
|
3547
|
+
const isSupported = useSupported(()=>window && 'BroadcastChannel' in window);
|
|
3548
|
+
const [isClosed, setIsClosed] = useState(false);
|
|
3549
|
+
const [data, setData] = useState();
|
|
3550
|
+
const [error, setError] = useState(null);
|
|
3551
|
+
const [timeStamp, setTimeStamp] = useState(0);
|
|
3552
|
+
const channelRef = useRef();
|
|
3553
|
+
const post = useCallback((data)=>{
|
|
3554
|
+
if (channelRef.current) {
|
|
3555
|
+
channelRef.current.postMessage(data);
|
|
3556
|
+
}
|
|
3557
|
+
}, []);
|
|
3558
|
+
const close = useCallback(()=>{
|
|
3559
|
+
if (channelRef.current) {
|
|
3560
|
+
channelRef.current.close();
|
|
3561
|
+
}
|
|
3562
|
+
setIsClosed(true);
|
|
3563
|
+
}, []);
|
|
3564
|
+
useEffect(()=>{
|
|
3565
|
+
if (isSupported) {
|
|
3566
|
+
channelRef.current = new BroadcastChannel(name);
|
|
3567
|
+
setError(null);
|
|
3568
|
+
const handleMessage = (e)=>{
|
|
3569
|
+
setData(e.data);
|
|
3570
|
+
// avoid data is same between two messages
|
|
3571
|
+
setTimeStamp(Date.now());
|
|
3572
|
+
};
|
|
3573
|
+
const handleError = (e)=>{
|
|
3574
|
+
setError(e);
|
|
3575
|
+
};
|
|
3576
|
+
const handleClose = ()=>{
|
|
3577
|
+
setIsClosed(true);
|
|
3578
|
+
};
|
|
3579
|
+
channelRef.current.addEventListener('message', handleMessage, {
|
|
3580
|
+
passive: true
|
|
3581
|
+
});
|
|
3582
|
+
channelRef.current.addEventListener('messageerror', handleError, {
|
|
3583
|
+
passive: true
|
|
3584
|
+
});
|
|
3585
|
+
channelRef.current.addEventListener('close', handleClose);
|
|
3586
|
+
return ()=>{
|
|
3587
|
+
if (channelRef.current) {
|
|
3588
|
+
channelRef.current.removeEventListener('message', handleMessage);
|
|
3589
|
+
channelRef.current.removeEventListener('messageerror', handleError);
|
|
3590
|
+
channelRef.current.removeEventListener('close', handleClose);
|
|
3591
|
+
close();
|
|
3592
|
+
}
|
|
3593
|
+
};
|
|
3594
|
+
}
|
|
3595
|
+
return close;
|
|
3596
|
+
}, [
|
|
3597
|
+
isSupported,
|
|
3598
|
+
name,
|
|
3599
|
+
close
|
|
3600
|
+
]);
|
|
3601
|
+
return {
|
|
3602
|
+
isSupported,
|
|
3603
|
+
channel: channelRef.current,
|
|
3604
|
+
data,
|
|
3605
|
+
post,
|
|
3606
|
+
close,
|
|
3607
|
+
error,
|
|
3608
|
+
isClosed,
|
|
3609
|
+
timeStamp
|
|
3610
|
+
};
|
|
3611
|
+
};
|
|
3612
|
+
|
|
3613
|
+
const useDevicePixelRatio = ()=>{
|
|
3614
|
+
const [pixelRatio, setPixelRatio] = useState(1);
|
|
3615
|
+
const observe = useCallback(()=>{
|
|
3616
|
+
if (!window) return;
|
|
3617
|
+
setPixelRatio(window.devicePixelRatio);
|
|
3618
|
+
const media = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
|
|
3619
|
+
const handleChange = ()=>{
|
|
3620
|
+
observe();
|
|
3621
|
+
};
|
|
3622
|
+
media.addEventListener('change', handleChange, {
|
|
3623
|
+
once: true
|
|
3624
|
+
});
|
|
3625
|
+
return ()=>{
|
|
3626
|
+
media.removeEventListener('change', handleChange);
|
|
3627
|
+
};
|
|
3628
|
+
}, []);
|
|
3629
|
+
useEffect(()=>{
|
|
3630
|
+
const cleanup = observe();
|
|
3631
|
+
return cleanup;
|
|
3632
|
+
}, [
|
|
3633
|
+
observe
|
|
3634
|
+
]);
|
|
3635
|
+
return {
|
|
3636
|
+
pixelRatio
|
|
3637
|
+
};
|
|
3638
|
+
};
|
|
3639
|
+
|
|
3640
|
+
const useElementByPoint = (options)=>{
|
|
3641
|
+
const { x, y, document: doc = typeof document !== 'undefined' ? document : null, multiple = false, interval = 'requestAnimationFrame', immediate = true } = options;
|
|
3642
|
+
const isSupported = useSupported(()=>{
|
|
3643
|
+
if (multiple) return doc && 'elementsFromPoint' in doc;
|
|
3644
|
+
return doc && 'elementFromPoint' in doc;
|
|
3645
|
+
});
|
|
3646
|
+
const [element, setElement] = useState(null);
|
|
3647
|
+
const [isActive, setIsActive] = useState(immediate);
|
|
3648
|
+
const rafIdRef = useRef(null);
|
|
3649
|
+
const intervalIdRef = useRef(null);
|
|
3650
|
+
const getXY = useCallback(()=>{
|
|
3651
|
+
// 需要判断 NaN
|
|
3652
|
+
const currentX = typeof x === 'function' ? x() : x;
|
|
3653
|
+
const currentY = typeof y === 'function' ? y() : y;
|
|
3654
|
+
return {
|
|
3655
|
+
x: Number.isNaN(currentX) ? 0 : currentX,
|
|
3656
|
+
y: Number.isNaN(currentY) ? 0 : currentY
|
|
3657
|
+
};
|
|
3658
|
+
}, [
|
|
3659
|
+
x,
|
|
3660
|
+
y
|
|
3661
|
+
]);
|
|
3662
|
+
const cb = useCallback(()=>{
|
|
3663
|
+
const { x: currentX, y: currentY } = getXY();
|
|
3664
|
+
var _doc_elementsFromPoint, _doc_elementFromPoint;
|
|
3665
|
+
setElement(multiple ? (_doc_elementsFromPoint = doc == null ? void 0 : doc.elementsFromPoint(currentX, currentY)) != null ? _doc_elementsFromPoint : [] : (_doc_elementFromPoint = doc == null ? void 0 : doc.elementFromPoint(currentX, currentY)) != null ? _doc_elementFromPoint : null);
|
|
3666
|
+
}, [
|
|
3667
|
+
doc,
|
|
3668
|
+
multiple,
|
|
3669
|
+
getXY
|
|
3670
|
+
]);
|
|
3671
|
+
const cleanup = useCallback(()=>{
|
|
3672
|
+
if (rafIdRef.current !== null) {
|
|
3673
|
+
cancelAnimationFrame(rafIdRef.current);
|
|
3674
|
+
rafIdRef.current = null;
|
|
3675
|
+
}
|
|
3676
|
+
if (intervalIdRef.current !== null) {
|
|
3677
|
+
clearInterval(intervalIdRef.current);
|
|
3678
|
+
intervalIdRef.current = null;
|
|
3679
|
+
}
|
|
3680
|
+
}, []);
|
|
3681
|
+
const pause = useCallback(()=>{
|
|
3682
|
+
setIsActive(false);
|
|
3683
|
+
cleanup();
|
|
3684
|
+
}, [
|
|
3685
|
+
cleanup
|
|
3686
|
+
]);
|
|
3687
|
+
const resume = useCallback(()=>{
|
|
3688
|
+
setIsActive(true);
|
|
3689
|
+
}, []);
|
|
3690
|
+
useEffect(()=>{
|
|
3691
|
+
if (!isActive) {
|
|
3692
|
+
return;
|
|
3693
|
+
}
|
|
3694
|
+
if (interval === 'requestAnimationFrame') {
|
|
3695
|
+
const runRaf = ()=>{
|
|
3696
|
+
cb();
|
|
3697
|
+
rafIdRef.current = requestAnimationFrame(runRaf);
|
|
3698
|
+
};
|
|
3699
|
+
runRaf();
|
|
3700
|
+
} else {
|
|
3701
|
+
cb();
|
|
3702
|
+
intervalIdRef.current = setInterval(cb, interval);
|
|
3703
|
+
}
|
|
3704
|
+
return cleanup;
|
|
3705
|
+
}, [
|
|
3706
|
+
isActive,
|
|
3707
|
+
interval,
|
|
3708
|
+
cb,
|
|
3709
|
+
cleanup
|
|
3710
|
+
]);
|
|
3711
|
+
useEffect(()=>{
|
|
3712
|
+
if (immediate) {
|
|
3713
|
+
resume();
|
|
3714
|
+
}
|
|
3715
|
+
return pause;
|
|
3716
|
+
}, [
|
|
3717
|
+
immediate,
|
|
3718
|
+
resume,
|
|
3719
|
+
pause
|
|
3720
|
+
]);
|
|
3721
|
+
return {
|
|
3722
|
+
isSupported,
|
|
3723
|
+
element,
|
|
3724
|
+
pause,
|
|
3725
|
+
resume,
|
|
3726
|
+
isActive
|
|
3727
|
+
};
|
|
3728
|
+
};
|
|
3729
|
+
|
|
3730
|
+
export { defaultOptions, use, useActiveElement, useAsyncEffect, useBroadcastChannel, useClickOutside, useClipboard, useControlled, useCookie, useCountDown, useCounter, useCssVar, useCustomCompareEffect, useCycleList, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDevicePixelRatio, useDisclosure, useDocumentVisibility, useDoubleClick, useDraggable, useDropZone, useElementBounding, useElementByPoint, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useEventSource, useEyeDropper, useFavicon, useFileDialog, useFirstMountState, useFocus, useFps, useFullscreen, useGeolocation, useHover, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLocationSelector, useLongPress, useMeasure, useMediaDevices, useMediaQuery, useMobileLandscape, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnceEffect, useOnceLayoutEffect, useOnline, useOrientation, usePageLeave, usePermission, usePlatform, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePreferredLanguages, usePrevious, useRafFn, useRafState, useReducedMotion, useResizeObserver, useScreenSafeArea, useScriptTag, useScroll, useScrollIntoView, useScrollLock, useSessionStorage, useSetState, useSticky, useSupported, useTextDirection, useTextSelection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, useUpdateEffect, useUpdateLayoutEffect, useWebNotification, useWindowScroll, useWindowSize, useWindowsFocus };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
|
|
3
|
+
var QRCode = require('qrcode');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var lodashEs = require('lodash-es');
|
|
6
|
+
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
|
|
9
|
+
var QRCode__default = /*#__PURE__*/_interopDefault(QRCode);
|
|
10
|
+
|
|
11
|
+
const useCustomCompareEffect = (effect, deps, depsEqual)=>{
|
|
12
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
13
|
+
if (!Array.isArray(deps) || !deps.length) {
|
|
14
|
+
console.warn('`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead.');
|
|
15
|
+
}
|
|
16
|
+
if (typeof depsEqual !== 'function') {
|
|
17
|
+
console.warn('`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const ref = react.useRef(undefined);
|
|
21
|
+
if (!ref.current || !depsEqual(deps, ref.current)) {
|
|
22
|
+
ref.current = deps;
|
|
23
|
+
}
|
|
24
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
25
|
+
react.useEffect(effect, ref.current);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const useDeepCompareEffect = (effect, deps)=>{
|
|
29
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
30
|
+
if (!Array.isArray(deps) || !deps.length) {
|
|
31
|
+
console.warn('`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead.');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
useCustomCompareEffect(effect, deps, lodashEs.isEqual);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const defaultOptions = {};
|
|
38
|
+
|
|
39
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
40
|
+
try {
|
|
41
|
+
var info = gen[key](arg);
|
|
42
|
+
var value = info.value;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
reject(error);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (info.done) {
|
|
48
|
+
resolve(value);
|
|
49
|
+
} else {
|
|
50
|
+
Promise.resolve(value).then(_next, _throw);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function _async_to_generator(fn) {
|
|
54
|
+
return function() {
|
|
55
|
+
var self = this, args = arguments;
|
|
56
|
+
return new Promise(function(resolve, reject) {
|
|
57
|
+
var gen = fn.apply(self, args);
|
|
58
|
+
function _next(value) {
|
|
59
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
60
|
+
}
|
|
61
|
+
function _throw(err) {
|
|
62
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
63
|
+
}
|
|
64
|
+
_next(undefined);
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function generateQRCode(text, options) {
|
|
69
|
+
return QRCode__default.default.toDataURL(text, options);
|
|
70
|
+
}
|
|
71
|
+
const useQRCode = (text, options = defaultOptions)=>{
|
|
72
|
+
const [qrCode, setQRCode] = react.useState('');
|
|
73
|
+
const [error, setError] = react.useState(null);
|
|
74
|
+
useDeepCompareEffect(()=>{
|
|
75
|
+
const generate = /*#__PURE__*/ _async_to_generator(function*() {
|
|
76
|
+
try {
|
|
77
|
+
const qrCode = yield generateQRCode(text, options);
|
|
78
|
+
setQRCode(qrCode);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
setError(error);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
generate();
|
|
84
|
+
}, [
|
|
85
|
+
text,
|
|
86
|
+
options
|
|
87
|
+
]);
|
|
88
|
+
return {
|
|
89
|
+
qrCode,
|
|
90
|
+
error
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
exports.generateQRCode = generateQRCode;
|
|
95
|
+
exports.useQRCode = useQRCode;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import QRCode from 'qrcode';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @title UseQRCode
|
|
5
|
+
*/
|
|
6
|
+
type UseQRCode = (
|
|
7
|
+
/**
|
|
8
|
+
* @zh 文本
|
|
9
|
+
* @en Text
|
|
10
|
+
*/
|
|
11
|
+
text: string,
|
|
12
|
+
/**
|
|
13
|
+
* @zh 传递给 `QRCode.toDataURL` 的选项
|
|
14
|
+
* @en Options passed to `QRCode.toDataURL`
|
|
15
|
+
*/
|
|
16
|
+
options?: QRCode.QRCodeToDataURLOptions) => UseQRCodeReturn;
|
|
17
|
+
/**
|
|
18
|
+
* @title UseQRCodeReturn
|
|
19
|
+
*/
|
|
20
|
+
interface UseQRCodeReturn {
|
|
21
|
+
/**
|
|
22
|
+
* @zh 生成的二维码
|
|
23
|
+
* @en Generated QR code
|
|
24
|
+
*/
|
|
25
|
+
qrCode: string;
|
|
26
|
+
/**
|
|
27
|
+
* @zh 错误
|
|
28
|
+
* @en Error
|
|
29
|
+
*/
|
|
30
|
+
error: unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
declare function generateQRCode(text: string, options?: QRCode.QRCodeToDataURLOptions): Promise<string>;
|
|
34
|
+
declare const useQRCode: UseQRCode;
|
|
35
|
+
|
|
36
|
+
export { generateQRCode, useQRCode };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import QRCode from 'qrcode';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @title UseQRCode
|
|
5
|
+
*/
|
|
6
|
+
type UseQRCode = (
|
|
7
|
+
/**
|
|
8
|
+
* @zh 文本
|
|
9
|
+
* @en Text
|
|
10
|
+
*/
|
|
11
|
+
text: string,
|
|
12
|
+
/**
|
|
13
|
+
* @zh 传递给 `QRCode.toDataURL` 的选项
|
|
14
|
+
* @en Options passed to `QRCode.toDataURL`
|
|
15
|
+
*/
|
|
16
|
+
options?: QRCode.QRCodeToDataURLOptions) => UseQRCodeReturn;
|
|
17
|
+
/**
|
|
18
|
+
* @title UseQRCodeReturn
|
|
19
|
+
*/
|
|
20
|
+
interface UseQRCodeReturn {
|
|
21
|
+
/**
|
|
22
|
+
* @zh 生成的二维码
|
|
23
|
+
* @en Generated QR code
|
|
24
|
+
*/
|
|
25
|
+
qrCode: string;
|
|
26
|
+
/**
|
|
27
|
+
* @zh 错误
|
|
28
|
+
* @en Error
|
|
29
|
+
*/
|
|
30
|
+
error: unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
declare function generateQRCode(text: string, options?: QRCode.QRCodeToDataURLOptions): Promise<string>;
|
|
34
|
+
declare const useQRCode: UseQRCode;
|
|
35
|
+
|
|
36
|
+
export { generateQRCode, useQRCode };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import QRCode from 'qrcode';
|
|
2
|
+
import { useRef, useEffect, useState } from 'react';
|
|
3
|
+
import { isEqual } from 'lodash-es';
|
|
4
|
+
|
|
5
|
+
const useCustomCompareEffect = (effect, deps, depsEqual)=>{
|
|
6
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
7
|
+
if (!Array.isArray(deps) || !deps.length) {
|
|
8
|
+
console.warn('`useCustomCompareEffect` should not be used with no dependencies. Use React.useEffect instead.');
|
|
9
|
+
}
|
|
10
|
+
if (typeof depsEqual !== 'function') {
|
|
11
|
+
console.warn('`useCustomCompareEffect` should be used with depsEqual callback for comparing deps list');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const ref = useRef(undefined);
|
|
15
|
+
if (!ref.current || !depsEqual(deps, ref.current)) {
|
|
16
|
+
ref.current = deps;
|
|
17
|
+
}
|
|
18
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
19
|
+
useEffect(effect, ref.current);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const useDeepCompareEffect = (effect, deps)=>{
|
|
23
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
24
|
+
if (!Array.isArray(deps) || !deps.length) {
|
|
25
|
+
console.warn('`useDeepCompareEffect` should not be used with no dependencies. Use React.useEffect instead.');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
useCustomCompareEffect(effect, deps, isEqual);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const defaultOptions = {};
|
|
32
|
+
|
|
33
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
34
|
+
try {
|
|
35
|
+
var info = gen[key](arg);
|
|
36
|
+
var value = info.value;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
reject(error);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (info.done) {
|
|
42
|
+
resolve(value);
|
|
43
|
+
} else {
|
|
44
|
+
Promise.resolve(value).then(_next, _throw);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function _async_to_generator(fn) {
|
|
48
|
+
return function() {
|
|
49
|
+
var self = this, args = arguments;
|
|
50
|
+
return new Promise(function(resolve, reject) {
|
|
51
|
+
var gen = fn.apply(self, args);
|
|
52
|
+
function _next(value) {
|
|
53
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
54
|
+
}
|
|
55
|
+
function _throw(err) {
|
|
56
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
57
|
+
}
|
|
58
|
+
_next(undefined);
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function generateQRCode(text, options) {
|
|
63
|
+
return QRCode.toDataURL(text, options);
|
|
64
|
+
}
|
|
65
|
+
const useQRCode = (text, options = defaultOptions)=>{
|
|
66
|
+
const [qrCode, setQRCode] = useState('');
|
|
67
|
+
const [error, setError] = useState(null);
|
|
68
|
+
useDeepCompareEffect(()=>{
|
|
69
|
+
const generate = /*#__PURE__*/ _async_to_generator(function*() {
|
|
70
|
+
try {
|
|
71
|
+
const qrCode = yield generateQRCode(text, options);
|
|
72
|
+
setQRCode(qrCode);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
setError(error);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
generate();
|
|
78
|
+
}, [
|
|
79
|
+
text,
|
|
80
|
+
options
|
|
81
|
+
]);
|
|
82
|
+
return {
|
|
83
|
+
qrCode,
|
|
84
|
+
error
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export { generateQRCode, useQRCode };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactuses/core",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.19-beta.1",
|
|
4
4
|
"license": "Unlicense",
|
|
5
5
|
"homepage": "https://www.reactuse.com/",
|
|
6
6
|
"repository": {
|
|
@@ -31,6 +31,16 @@
|
|
|
31
31
|
"types": "./dist/index.d.cts",
|
|
32
32
|
"default": "./dist/index.cjs"
|
|
33
33
|
}
|
|
34
|
+
},
|
|
35
|
+
"./useQRCode": {
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./dist/useQRCode.d.mts",
|
|
38
|
+
"default": "./dist/useQRCode.mjs"
|
|
39
|
+
},
|
|
40
|
+
"require": {
|
|
41
|
+
"types": "./dist/useQRCode.d.cts",
|
|
42
|
+
"default": "./dist/useQRCode.cjs"
|
|
43
|
+
}
|
|
34
44
|
}
|
|
35
45
|
},
|
|
36
46
|
"main": "./dist/index.cjs",
|
|
@@ -55,8 +65,14 @@
|
|
|
55
65
|
"test:coverage": "jest --coverage"
|
|
56
66
|
},
|
|
57
67
|
"peerDependencies": {
|
|
68
|
+
"qrcode": "^1.5",
|
|
58
69
|
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
|
|
59
70
|
},
|
|
71
|
+
"peerDependenciesMeta": {
|
|
72
|
+
"qrcode": {
|
|
73
|
+
"optional": true
|
|
74
|
+
}
|
|
75
|
+
},
|
|
60
76
|
"dependencies": {
|
|
61
77
|
"js-cookie": "^3.0.5",
|
|
62
78
|
"lodash-es": "^4.17.21",
|
|
@@ -71,6 +87,7 @@
|
|
|
71
87
|
"@types/js-cookie": "^3.0.3",
|
|
72
88
|
"@types/lodash": "^4.14.184",
|
|
73
89
|
"@types/lodash-es": "^4.17.7",
|
|
90
|
+
"@types/qrcode": "^1.5.5",
|
|
74
91
|
"@types/use-sync-external-store": "^0.0.6",
|
|
75
92
|
"babel-jest": "^29.0.2",
|
|
76
93
|
"consola": "^2.15.3",
|
|
@@ -80,6 +97,7 @@
|
|
|
80
97
|
"jest-environment-jsdom": "^29.0.2",
|
|
81
98
|
"jest-environment-node": "^29.7.0",
|
|
82
99
|
"lodash": "^4.17.21",
|
|
100
|
+
"qrcode": "^1.5.3",
|
|
83
101
|
"react": "^18.2.0",
|
|
84
102
|
"react-dom": "^18.2.0",
|
|
85
103
|
"typescript": "^5.3.3"
|