@bifrostui/utils 2.0.0-alpha.2 → 2.0.0-alpha.20
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/dist/context-selector/createContext.d.ts +26 -0
- package/dist/context-selector/createContext.js +83 -0
- package/dist/context-selector/index.d.ts +4 -0
- package/dist/context-selector/index.js +33 -0
- package/dist/context-selector/types.d.ts +24 -0
- package/dist/context-selector/types.js +15 -0
- package/dist/context-selector/useContextSelector.d.ts +29 -0
- package/dist/context-selector/useContextSelector.js +92 -0
- package/dist/context-selector/useHasParentContext.d.ts +26 -0
- package/dist/context-selector/useHasParentContext.js +46 -0
- package/dist/directionLocationUtil.d.ts +1 -0
- package/dist/getBoundingClientRect/index.js +1 -11
- package/dist/hooks/useDomReady/index.js +2 -2
- package/dist/hooks/useEventCallback.d.ts +12 -3
- package/dist/hooks/useEventCallback.js +14 -5
- package/dist/hooks/useIsomorphicLayoutEffect.d.ts +11 -0
- package/dist/hooks/useIsomorphicLayoutEffect.js +39 -0
- package/dist/hooks/useMemoizedFn.js +1 -1
- package/dist/hooks/useSize.js +2 -2
- package/dist/hooks/useTouchEmulator.js +3 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/isMini.js +1 -1
- package/dist/render.d.ts +2 -2
- package/dist/setRef.d.ts +1 -1
- package/dist/ssr/canUseDOM.d.ts +4 -0
- package/dist/ssr/canUseDOM.js +29 -0
- package/dist/ssr/index.d.ts +1 -0
- package/dist/ssr/index.js +27 -0
- package/dist/themeCreator/cssVarToValue.d.ts +16 -0
- package/dist/themeCreator/cssVarToValue.js +32 -4
- package/dist/toArray.d.ts +1 -1
- package/dist/toArray.js +3 -3
- package/dist/transitions.d.ts +1 -1
- package/dist/transitions.js +1 -1
- package/es/context-selector/createContext.d.ts +26 -0
- package/es/context-selector/createContext.js +53 -0
- package/es/context-selector/index.d.ts +4 -0
- package/es/context-selector/index.js +8 -0
- package/es/context-selector/types.d.ts +24 -0
- package/es/context-selector/types.js +0 -0
- package/es/context-selector/useContextSelector.d.ts +29 -0
- package/es/context-selector/useContextSelector.js +59 -0
- package/es/context-selector/useHasParentContext.d.ts +26 -0
- package/es/context-selector/useHasParentContext.js +13 -0
- package/es/directionLocationUtil.d.ts +1 -0
- package/es/getBoundingClientRect/index.js +1 -11
- package/es/hooks/useDomReady/index.js +1 -1
- package/es/hooks/useEventCallback.d.ts +12 -3
- package/es/hooks/useEventCallback.js +14 -5
- package/es/hooks/useIsomorphicLayoutEffect.d.ts +11 -0
- package/es/hooks/useIsomorphicLayoutEffect.js +6 -0
- package/es/hooks/useMemoizedFn.js +1 -1
- package/es/hooks/useSize.js +1 -1
- package/es/hooks/useTouchEmulator.js +3 -3
- package/es/index.d.ts +1 -0
- package/es/index.js +1 -0
- package/es/isMini.js +1 -1
- package/es/render.d.ts +2 -2
- package/es/setRef.d.ts +1 -1
- package/es/ssr/canUseDOM.d.ts +4 -0
- package/es/ssr/canUseDOM.js +6 -0
- package/es/ssr/index.d.ts +1 -0
- package/es/ssr/index.js +4 -0
- package/es/themeCreator/cssVarToValue.d.ts +16 -0
- package/es/themeCreator/cssVarToValue.js +31 -3
- package/es/toArray.d.ts +1 -1
- package/es/toArray.js +2 -2
- package/es/transitions.d.ts +1 -1
- package/es/transitions.js +1 -1
- package/package.json +5 -4
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Context } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a context that supports selective subscriptions.
|
|
4
|
+
* This allows components to subscribe to only specific parts of the context value
|
|
5
|
+
* and re-render only when those parts change.
|
|
6
|
+
*
|
|
7
|
+
* @param defaultValue - The default value for the context
|
|
8
|
+
* @returns A context object that can be used with useContextSelector
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* interface MyContextValue {
|
|
13
|
+
* count: number;
|
|
14
|
+
* name: string;
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* const MyContext = createContext<MyContextValue>({ count: 0, name: '' });
|
|
18
|
+
*
|
|
19
|
+
* function MyComponent() {
|
|
20
|
+
* // Only re-renders when count changes, not when name changes
|
|
21
|
+
* const count = useContextSelector(MyContext, ctx => ctx.count);
|
|
22
|
+
* return <div>{count}</div>;
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare const createContext: <Value>(defaultValue: Value) => Context<Value>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var createContext_exports = {};
|
|
29
|
+
__export(createContext_exports, {
|
|
30
|
+
createContext: () => createContext
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(createContext_exports);
|
|
33
|
+
var React = __toESM(require("react"));
|
|
34
|
+
var import_scheduler = require("scheduler");
|
|
35
|
+
var import_useIsomorphicLayoutEffect = require("../hooks/useIsomorphicLayoutEffect");
|
|
36
|
+
const createProvider = (Original) => {
|
|
37
|
+
const Provider = (props) => {
|
|
38
|
+
const valueRef = React.useRef(props.value);
|
|
39
|
+
const versionRef = React.useRef(0);
|
|
40
|
+
const contextValue = React.useRef(null);
|
|
41
|
+
if (!contextValue.current) {
|
|
42
|
+
contextValue.current = {
|
|
43
|
+
value: valueRef,
|
|
44
|
+
version: versionRef,
|
|
45
|
+
listeners: []
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
(0, import_useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => {
|
|
49
|
+
valueRef.current = props.value;
|
|
50
|
+
versionRef.current += 1;
|
|
51
|
+
(0, import_scheduler.unstable_runWithPriority)(import_scheduler.unstable_NormalPriority, () => {
|
|
52
|
+
contextValue.current.listeners.forEach(
|
|
53
|
+
(listener) => {
|
|
54
|
+
listener([versionRef.current, props.value]);
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
}, [props.value]);
|
|
59
|
+
return React.createElement(
|
|
60
|
+
Original,
|
|
61
|
+
{ value: contextValue.current },
|
|
62
|
+
props.children
|
|
63
|
+
);
|
|
64
|
+
};
|
|
65
|
+
if (process.env.NODE_ENV !== "production") {
|
|
66
|
+
Provider.displayName = "ContextSelector.Provider";
|
|
67
|
+
}
|
|
68
|
+
return Provider;
|
|
69
|
+
};
|
|
70
|
+
const createContext = (defaultValue) => {
|
|
71
|
+
const context = React.createContext({
|
|
72
|
+
value: { current: defaultValue },
|
|
73
|
+
version: { current: -1 },
|
|
74
|
+
listeners: []
|
|
75
|
+
});
|
|
76
|
+
context.Provider = createProvider(context.Provider);
|
|
77
|
+
delete context.Consumer;
|
|
78
|
+
return context;
|
|
79
|
+
};
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
createContext
|
|
83
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var context_selector_exports = {};
|
|
19
|
+
__export(context_selector_exports, {
|
|
20
|
+
createContext: () => import_createContext.createContext,
|
|
21
|
+
useContextSelector: () => import_useContextSelector.useContextSelector,
|
|
22
|
+
useHasParentContext: () => import_useHasParentContext.useHasParentContext
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(context_selector_exports);
|
|
25
|
+
var import_createContext = require("./createContext");
|
|
26
|
+
var import_useContextSelector = require("./useContextSelector");
|
|
27
|
+
var import_useHasParentContext = require("./useHasParentContext");
|
|
28
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
29
|
+
0 && (module.exports = {
|
|
30
|
+
createContext,
|
|
31
|
+
useContextSelector,
|
|
32
|
+
useHasParentContext
|
|
33
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
export type Context<Value> = React.Context<Value> & {
|
|
6
|
+
Provider: React.FC<React.ProviderProps<Value>>;
|
|
7
|
+
Consumer: never;
|
|
8
|
+
};
|
|
9
|
+
export type ContextSelector<Value, SelectedValue> = (value: Value) => SelectedValue;
|
|
10
|
+
/**
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export type ContextVersion = number;
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export type ContextValue<Value> = {
|
|
18
|
+
/** Holds a set of subscribers from components. */
|
|
19
|
+
listeners: ((payload: readonly [ContextVersion, Value]) => void)[];
|
|
20
|
+
/** Holds an actual value of React's context that will be propagated down for computations. */
|
|
21
|
+
value: React.MutableRefObject<Value>;
|
|
22
|
+
/** A version field is used to sync a context value and consumers. */
|
|
23
|
+
version: React.MutableRefObject<ContextVersion>;
|
|
24
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var types_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(types_exports);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Context, ContextSelector } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* This hook returns context selected value by selector.
|
|
4
|
+
* It will only accept context created by `createContext` from this package.
|
|
5
|
+
* It will trigger re-render only if the selected value is referentially changed.
|
|
6
|
+
*
|
|
7
|
+
* @param context - Context created by createContext from this package
|
|
8
|
+
* @param selector - Function that selects a value from the context
|
|
9
|
+
* @returns The selected value from the context
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* interface MyContextValue {
|
|
14
|
+
* count: number;
|
|
15
|
+
* name: string;
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* const MyContext = createContext<MyContextValue>({ count: 0, name: '' });
|
|
19
|
+
*
|
|
20
|
+
* function Counter() {
|
|
21
|
+
* // Only re-renders when count changes, not when name changes
|
|
22
|
+
* const count = useContextSelector(MyContext, ctx => ctx.count);
|
|
23
|
+
* const increment = useContextSelector(MyContext, ctx => ctx.increment);
|
|
24
|
+
*
|
|
25
|
+
* return <button onClick={increment}>{count}</button>;
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare const useContextSelector: <Value, SelectedValue>(context: Context<Value>, selector: ContextSelector<Value, SelectedValue>) => SelectedValue;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var useContextSelector_exports = {};
|
|
29
|
+
__export(useContextSelector_exports, {
|
|
30
|
+
useContextSelector: () => useContextSelector
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(useContextSelector_exports);
|
|
33
|
+
var React = __toESM(require("react"));
|
|
34
|
+
var import_useEventCallback = __toESM(require("../hooks/useEventCallback"));
|
|
35
|
+
var import_useIsomorphicLayoutEffect = require("../hooks/useIsomorphicLayoutEffect");
|
|
36
|
+
const useContextSelector = (context, selector) => {
|
|
37
|
+
const contextValue = React.useContext(
|
|
38
|
+
context
|
|
39
|
+
);
|
|
40
|
+
const {
|
|
41
|
+
value: { current: value },
|
|
42
|
+
version: { current: version },
|
|
43
|
+
listeners
|
|
44
|
+
} = contextValue;
|
|
45
|
+
const selected = selector(value);
|
|
46
|
+
const [state, setState] = React.useState([
|
|
47
|
+
value,
|
|
48
|
+
selected
|
|
49
|
+
]);
|
|
50
|
+
const dispatch = (payload) => {
|
|
51
|
+
setState((prevState) => {
|
|
52
|
+
if (!payload) {
|
|
53
|
+
return [value, selected];
|
|
54
|
+
}
|
|
55
|
+
if (payload[0] <= version) {
|
|
56
|
+
if (Object.is(prevState[1], selected)) {
|
|
57
|
+
return prevState;
|
|
58
|
+
}
|
|
59
|
+
return [value, selected];
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
if (Object.is(prevState[0], payload[1])) {
|
|
63
|
+
return prevState;
|
|
64
|
+
}
|
|
65
|
+
const nextSelected = selector(payload[1]);
|
|
66
|
+
if (Object.is(prevState[1], nextSelected)) {
|
|
67
|
+
return prevState;
|
|
68
|
+
}
|
|
69
|
+
return [payload[1], nextSelected];
|
|
70
|
+
} catch (e) {
|
|
71
|
+
console.error(e);
|
|
72
|
+
}
|
|
73
|
+
return [prevState[0], prevState[1]];
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
if (!Object.is(state[1], selected)) {
|
|
77
|
+
dispatch(void 0);
|
|
78
|
+
}
|
|
79
|
+
const stableDispatch = (0, import_useEventCallback.default)(dispatch);
|
|
80
|
+
(0, import_useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => {
|
|
81
|
+
listeners.push(stableDispatch);
|
|
82
|
+
return () => {
|
|
83
|
+
const index = listeners.indexOf(stableDispatch);
|
|
84
|
+
listeners.splice(index, 1);
|
|
85
|
+
};
|
|
86
|
+
}, [stableDispatch, listeners]);
|
|
87
|
+
return state[1];
|
|
88
|
+
};
|
|
89
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
90
|
+
0 && (module.exports = {
|
|
91
|
+
useContextSelector
|
|
92
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Context } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Utility hook for contexts created by createContext from this package
|
|
4
|
+
* to determine if a parent context exists.
|
|
5
|
+
*
|
|
6
|
+
* WARNING: This hook will not work for native React contexts
|
|
7
|
+
*
|
|
8
|
+
* @param context - Context created by createContext from this package
|
|
9
|
+
* @returns Whether the hook is wrapped by a parent context provider
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* const MyContext = createContext({ value: 'default' });
|
|
14
|
+
*
|
|
15
|
+
* function MyComponent() {
|
|
16
|
+
* const hasContext = useHasParentContext(MyContext);
|
|
17
|
+
*
|
|
18
|
+
* if (hasContext) {
|
|
19
|
+
* return <div>I am inside context provider</div>;
|
|
20
|
+
* } else {
|
|
21
|
+
* return <div>I can only use default context value</div>;
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function useHasParentContext<Value>(context: Context<Value>): boolean;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var useHasParentContext_exports = {};
|
|
29
|
+
__export(useHasParentContext_exports, {
|
|
30
|
+
useHasParentContext: () => useHasParentContext
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(useHasParentContext_exports);
|
|
33
|
+
var React = __toESM(require("react"));
|
|
34
|
+
function useHasParentContext(context) {
|
|
35
|
+
const contextValue = React.useContext(
|
|
36
|
+
context
|
|
37
|
+
);
|
|
38
|
+
if (contextValue.version) {
|
|
39
|
+
return contextValue.version.current !== -1;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
44
|
+
0 && (module.exports = {
|
|
45
|
+
useHasParentContext
|
|
46
|
+
});
|
|
@@ -22,17 +22,7 @@ __export(getBoundingClientRect_exports, {
|
|
|
22
22
|
module.exports = __toCommonJS(getBoundingClientRect_exports);
|
|
23
23
|
function getBoundingClientRect(ele) {
|
|
24
24
|
if (!ele) {
|
|
25
|
-
return Promise.resolve(
|
|
26
|
-
width: 0,
|
|
27
|
-
height: 0,
|
|
28
|
-
top: 0,
|
|
29
|
-
right: 0,
|
|
30
|
-
bottom: 0,
|
|
31
|
-
left: 0,
|
|
32
|
-
x: 0,
|
|
33
|
-
y: 0,
|
|
34
|
-
toJSON: () => ({})
|
|
35
|
-
});
|
|
25
|
+
return Promise.resolve(null);
|
|
36
26
|
}
|
|
37
27
|
return Promise.resolve(ele.getBoundingClientRect());
|
|
38
28
|
}
|
|
@@ -30,9 +30,9 @@ __export(useDomReady_exports, {
|
|
|
30
30
|
default: () => useDomReady_default
|
|
31
31
|
});
|
|
32
32
|
module.exports = __toCommonJS(useDomReady_exports);
|
|
33
|
-
var
|
|
33
|
+
var React = __toESM(require("react"));
|
|
34
34
|
function useDomReady(cb) {
|
|
35
|
-
|
|
35
|
+
React.useEffect(() => {
|
|
36
36
|
cb == null ? void 0 : cb();
|
|
37
37
|
}, []);
|
|
38
38
|
}
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Modified `useCallback` that can be used when dependencies change too frequently.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Can occur when:
|
|
5
|
+
* - User props are dependencies which could change on every render
|
|
6
|
+
* - Volatile values (i.e. useState/useDispatch) are dependencies which could change frequently
|
|
7
|
+
*
|
|
8
|
+
* This should not be used often, but can be a useful re-render optimization since the callback
|
|
9
|
+
* is a ref and will not be invalidated between re-renders.
|
|
10
|
+
*
|
|
11
|
+
* @param fn - The callback function that will be used
|
|
12
|
+
* @see https://github.com/facebook/react/issues/14099#issuecomment-440013892
|
|
13
|
+
* @see https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback
|
|
5
14
|
*/
|
|
6
|
-
export default function useEventCallback(fn:
|
|
15
|
+
export default function useEventCallback<Args extends unknown[], Return>(fn: (...args: Args) => Return): (...args: Args) => Return;
|
|
@@ -30,11 +30,20 @@ __export(useEventCallback_exports, {
|
|
|
30
30
|
default: () => useEventCallback
|
|
31
31
|
});
|
|
32
32
|
module.exports = __toCommonJS(useEventCallback_exports);
|
|
33
|
-
var
|
|
33
|
+
var React = __toESM(require("react"));
|
|
34
|
+
var import_useIsomorphicLayoutEffect = require("./useIsomorphicLayoutEffect");
|
|
34
35
|
function useEventCallback(fn) {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
ref.current = fn;
|
|
36
|
+
const callbackRef = React.useRef(() => {
|
|
37
|
+
throw new Error("Cannot call an event handler while rendering");
|
|
38
38
|
});
|
|
39
|
-
|
|
39
|
+
(0, import_useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => {
|
|
40
|
+
callbackRef.current = fn;
|
|
41
|
+
}, [fn]);
|
|
42
|
+
return React.useCallback(
|
|
43
|
+
(...args) => {
|
|
44
|
+
const callback = callbackRef.current;
|
|
45
|
+
return callback(...args);
|
|
46
|
+
},
|
|
47
|
+
[callbackRef]
|
|
48
|
+
);
|
|
40
49
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* React currently throws a warning when using useLayoutEffect on the server. To get around it, we can conditionally
|
|
4
|
+
* useEffect on the server (no-op) and useLayoutEffect in the browser. We occasionally need useLayoutEffect to
|
|
5
|
+
* ensure we don't get a render flash for certain operations, but we may also need affected components to render on
|
|
6
|
+
* the server.
|
|
7
|
+
*
|
|
8
|
+
* https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85
|
|
9
|
+
* https://github.com/reduxjs/react-redux/blob/master/src/utils/useIsomorphicLayoutEffect.js
|
|
10
|
+
*/
|
|
11
|
+
export declare const useIsomorphicLayoutEffect: typeof React.useEffect;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var useIsomorphicLayoutEffect_exports = {};
|
|
29
|
+
__export(useIsomorphicLayoutEffect_exports, {
|
|
30
|
+
useIsomorphicLayoutEffect: () => useIsomorphicLayoutEffect
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(useIsomorphicLayoutEffect_exports);
|
|
33
|
+
var React = __toESM(require("react"));
|
|
34
|
+
var import_ssr = require("../ssr");
|
|
35
|
+
const useIsomorphicLayoutEffect = (0, import_ssr.canUseDOM)() ? React.useLayoutEffect : React.useEffect;
|
|
36
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
37
|
+
0 && (module.exports = {
|
|
38
|
+
useIsomorphicLayoutEffect
|
|
39
|
+
});
|
|
@@ -45,7 +45,7 @@ function useMemoizedFn(fn) {
|
|
|
45
45
|
fnRef.current = (0, import_react.useMemo)(() => fn, [fn]);
|
|
46
46
|
const memoizedFn = (0, import_react.useRef)();
|
|
47
47
|
if (!memoizedFn.current) {
|
|
48
|
-
memoizedFn.current = function(...args) {
|
|
48
|
+
memoizedFn.current = function memoized(...args) {
|
|
49
49
|
return fnRef.current.apply(this, args);
|
|
50
50
|
};
|
|
51
51
|
}
|
package/dist/hooks/useSize.js
CHANGED
|
@@ -30,12 +30,12 @@ __export(useSize_exports, {
|
|
|
30
30
|
default: () => useSize_default
|
|
31
31
|
});
|
|
32
32
|
module.exports = __toCommonJS(useSize_exports);
|
|
33
|
-
var
|
|
33
|
+
var React = __toESM(require("react"));
|
|
34
34
|
var import_useDomReady = __toESM(require("./useDomReady"));
|
|
35
35
|
var import_getBoundingClientRect = __toESM(require("../getBoundingClientRect"));
|
|
36
36
|
var import_domTarget = require("../domTarget");
|
|
37
37
|
function useSize(target) {
|
|
38
|
-
const [state, setState] =
|
|
38
|
+
const [state, setState] = React.useState({});
|
|
39
39
|
(0, import_useDomReady.default)(() => {
|
|
40
40
|
const el = (0, import_domTarget.getTargetElement)(target);
|
|
41
41
|
if (el) {
|
|
@@ -26,9 +26,9 @@ __export(useTouchEmulator_exports, {
|
|
|
26
26
|
module.exports = __toCommonJS(useTouchEmulator_exports);
|
|
27
27
|
var import_react = require("react");
|
|
28
28
|
let eventTarget;
|
|
29
|
-
const Touch = function Touch2(target, identifier, pos,
|
|
30
|
-
deltaX =
|
|
31
|
-
deltaY =
|
|
29
|
+
const Touch = function Touch2(target, identifier, pos, dx = 0, dy = 0) {
|
|
30
|
+
const deltaX = dx || 0;
|
|
31
|
+
const deltaY = dy || 0;
|
|
32
32
|
this.identifier = identifier;
|
|
33
33
|
this.target = target;
|
|
34
34
|
this.clientX = pos.clientX + deltaX;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -82,6 +82,7 @@ __reExport(src_exports, require("./domUtils"), module.exports);
|
|
|
82
82
|
__reExport(src_exports, require("./isType"), module.exports);
|
|
83
83
|
__reExport(src_exports, require("./render"), module.exports);
|
|
84
84
|
__reExport(src_exports, require("./themeCreator"), module.exports);
|
|
85
|
+
__reExport(src_exports, require("./context-selector"), module.exports);
|
|
85
86
|
// Annotate the CommonJS export names for ESM import in node:
|
|
86
87
|
0 && (module.exports = {
|
|
87
88
|
blockTouch,
|
|
@@ -122,5 +123,6 @@ __reExport(src_exports, require("./themeCreator"), module.exports);
|
|
|
122
123
|
...require("./domUtils"),
|
|
123
124
|
...require("./isType"),
|
|
124
125
|
...require("./render"),
|
|
125
|
-
...require("./themeCreator")
|
|
126
|
+
...require("./themeCreator"),
|
|
127
|
+
...require("./context-selector")
|
|
126
128
|
});
|
package/dist/isMini.js
CHANGED
|
@@ -24,7 +24,7 @@ __export(isMini_exports, {
|
|
|
24
24
|
isWeapp: () => isWeapp
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(isMini_exports);
|
|
27
|
-
const isMini = typeof process.env.TARO_ENV === "string";
|
|
27
|
+
const isMini = typeof process.env.TARO_ENV === "string" && ["weapp", "swan", "alipay", "tt", "qq", "jd"].includes(process.env.TARO_ENV);
|
|
28
28
|
const isWeapp = process.env.TARO_ENV === "weapp";
|
|
29
29
|
const isAlipay = process.env.TARO_ENV === "alipay";
|
|
30
30
|
const isTt = process.env.TARO_ENV === "tt";
|
package/dist/render.d.ts
CHANGED
|
@@ -8,6 +8,6 @@ type ContainerType = (Element | DocumentFragment) & {
|
|
|
8
8
|
export declare function testModernRender(node: ReactElement, container: ContainerType, isTest: boolean): void;
|
|
9
9
|
export declare function render(node: ReactElement, container: ContainerType): void;
|
|
10
10
|
/** @private Test usage. Not work in prod */
|
|
11
|
-
export declare function testLegacyUnmount(container: ContainerType, isTest: boolean):
|
|
12
|
-
export declare function unmount(container: ContainerType):
|
|
11
|
+
export declare function testLegacyUnmount(container: ContainerType, isTest: boolean): boolean;
|
|
12
|
+
export declare function unmount(container: ContainerType): boolean | Promise<void>;
|
|
13
13
|
export {};
|
package/dist/setRef.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import * as React from 'react';
|
|
2
2
|
export default function setRef<T>(ref: React.MutableRefObject<T | null> | ((instance: T | null) => void) | null | undefined, value: T | null): void;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var canUseDOM_exports = {};
|
|
19
|
+
__export(canUseDOM_exports, {
|
|
20
|
+
canUseDOM: () => canUseDOM
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(canUseDOM_exports);
|
|
23
|
+
function canUseDOM() {
|
|
24
|
+
return typeof window !== "undefined" && !!(window.document && window.document.createElement);
|
|
25
|
+
}
|
|
26
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
27
|
+
0 && (module.exports = {
|
|
28
|
+
canUseDOM
|
|
29
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { canUseDOM } from './canUseDOM';
|