@bifrostui/utils 2.0.0-alpha.12 → 2.0.0-alpha.14
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/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/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/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/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/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/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
|
+
});
|
|
@@ -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/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';
|
|
@@ -0,0 +1,27 @@
|
|
|
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 ssr_exports = {};
|
|
19
|
+
__export(ssr_exports, {
|
|
20
|
+
canUseDOM: () => import_canUseDOM.canUseDOM
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(ssr_exports);
|
|
23
|
+
var import_canUseDOM = require("./canUseDOM");
|
|
24
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
25
|
+
0 && (module.exports = {
|
|
26
|
+
canUseDOM
|
|
27
|
+
});
|
package/dist/toArray.d.ts
CHANGED
package/dist/toArray.js
CHANGED
|
@@ -30,17 +30,17 @@ __export(toArray_exports, {
|
|
|
30
30
|
default: () => toArray
|
|
31
31
|
});
|
|
32
32
|
module.exports = __toCommonJS(toArray_exports);
|
|
33
|
-
var
|
|
33
|
+
var React = __toESM(require("react"));
|
|
34
34
|
var import_react_is = require("react-is");
|
|
35
35
|
function toArray(children, option = {}) {
|
|
36
36
|
let ret = [];
|
|
37
|
-
|
|
37
|
+
React.Children.forEach(children, (child) => {
|
|
38
38
|
if ((child === void 0 || child === null) && !option.keepEmpty) {
|
|
39
39
|
return;
|
|
40
40
|
}
|
|
41
41
|
if (Array.isArray(child)) {
|
|
42
42
|
ret = ret.concat(toArray(child));
|
|
43
|
-
} else if ((0, import_react_is.isFragment)(child) && child
|
|
43
|
+
} else if ((0, import_react_is.isFragment)(child) && React.isValidElement(child)) {
|
|
44
44
|
ret = ret.concat(toArray(child.props.children, option));
|
|
45
45
|
} else {
|
|
46
46
|
ret.push(child);
|
package/dist/transitions.d.ts
CHANGED
package/dist/transitions.js
CHANGED
|
@@ -75,7 +75,7 @@ function formatMs(milliseconds) {
|
|
|
75
75
|
function createTransitions(inputTransitions = {}) {
|
|
76
76
|
const mergedEasing = __spreadValues(__spreadValues({}, easing), inputTransitions.easing);
|
|
77
77
|
const mergedDuration = __spreadValues(__spreadValues({}, duration), inputTransitions.duration);
|
|
78
|
-
const create = (props = ["all"], options) => {
|
|
78
|
+
const create = (props = ["all"], options = {}) => {
|
|
79
79
|
const {
|
|
80
80
|
duration: durationOption = mergedDuration.standard,
|
|
81
81
|
easing: easingOption = mergedEasing.easeInOut,
|
|
@@ -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,53 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import {
|
|
3
|
+
unstable_NormalPriority as NormalPriority,
|
|
4
|
+
unstable_runWithPriority as runWithPriority
|
|
5
|
+
} from "scheduler";
|
|
6
|
+
import { useIsomorphicLayoutEffect } from "../hooks/useIsomorphicLayoutEffect";
|
|
7
|
+
const createProvider = (Original) => {
|
|
8
|
+
const Provider = (props) => {
|
|
9
|
+
const valueRef = React.useRef(props.value);
|
|
10
|
+
const versionRef = React.useRef(0);
|
|
11
|
+
const contextValue = React.useRef(null);
|
|
12
|
+
if (!contextValue.current) {
|
|
13
|
+
contextValue.current = {
|
|
14
|
+
value: valueRef,
|
|
15
|
+
version: versionRef,
|
|
16
|
+
listeners: []
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
useIsomorphicLayoutEffect(() => {
|
|
20
|
+
valueRef.current = props.value;
|
|
21
|
+
versionRef.current += 1;
|
|
22
|
+
runWithPriority(NormalPriority, () => {
|
|
23
|
+
contextValue.current.listeners.forEach(
|
|
24
|
+
(listener) => {
|
|
25
|
+
listener([versionRef.current, props.value]);
|
|
26
|
+
}
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
}, [props.value]);
|
|
30
|
+
return React.createElement(
|
|
31
|
+
Original,
|
|
32
|
+
{ value: contextValue.current },
|
|
33
|
+
props.children
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
if (process.env.NODE_ENV !== "production") {
|
|
37
|
+
Provider.displayName = "ContextSelector.Provider";
|
|
38
|
+
}
|
|
39
|
+
return Provider;
|
|
40
|
+
};
|
|
41
|
+
const createContext = (defaultValue) => {
|
|
42
|
+
const context = React.createContext({
|
|
43
|
+
value: { current: defaultValue },
|
|
44
|
+
version: { current: -1 },
|
|
45
|
+
listeners: []
|
|
46
|
+
});
|
|
47
|
+
context.Provider = createProvider(context.Provider);
|
|
48
|
+
delete context.Consumer;
|
|
49
|
+
return context;
|
|
50
|
+
};
|
|
51
|
+
export {
|
|
52
|
+
createContext
|
|
53
|
+
};
|
|
@@ -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
|
+
};
|
|
File without changes
|
|
@@ -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,59 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import useEventCallback from "../hooks/useEventCallback";
|
|
3
|
+
import { useIsomorphicLayoutEffect } from "../hooks/useIsomorphicLayoutEffect";
|
|
4
|
+
const useContextSelector = (context, selector) => {
|
|
5
|
+
const contextValue = React.useContext(
|
|
6
|
+
context
|
|
7
|
+
);
|
|
8
|
+
const {
|
|
9
|
+
value: { current: value },
|
|
10
|
+
version: { current: version },
|
|
11
|
+
listeners
|
|
12
|
+
} = contextValue;
|
|
13
|
+
const selected = selector(value);
|
|
14
|
+
const [state, setState] = React.useState([
|
|
15
|
+
value,
|
|
16
|
+
selected
|
|
17
|
+
]);
|
|
18
|
+
const dispatch = (payload) => {
|
|
19
|
+
setState((prevState) => {
|
|
20
|
+
if (!payload) {
|
|
21
|
+
return [value, selected];
|
|
22
|
+
}
|
|
23
|
+
if (payload[0] <= version) {
|
|
24
|
+
if (Object.is(prevState[1], selected)) {
|
|
25
|
+
return prevState;
|
|
26
|
+
}
|
|
27
|
+
return [value, selected];
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
if (Object.is(prevState[0], payload[1])) {
|
|
31
|
+
return prevState;
|
|
32
|
+
}
|
|
33
|
+
const nextSelected = selector(payload[1]);
|
|
34
|
+
if (Object.is(prevState[1], nextSelected)) {
|
|
35
|
+
return prevState;
|
|
36
|
+
}
|
|
37
|
+
return [payload[1], nextSelected];
|
|
38
|
+
} catch (e) {
|
|
39
|
+
console.error(e);
|
|
40
|
+
}
|
|
41
|
+
return [prevState[0], prevState[1]];
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
if (!Object.is(state[1], selected)) {
|
|
45
|
+
dispatch(void 0);
|
|
46
|
+
}
|
|
47
|
+
const stableDispatch = useEventCallback(dispatch);
|
|
48
|
+
useIsomorphicLayoutEffect(() => {
|
|
49
|
+
listeners.push(stableDispatch);
|
|
50
|
+
return () => {
|
|
51
|
+
const index = listeners.indexOf(stableDispatch);
|
|
52
|
+
listeners.splice(index, 1);
|
|
53
|
+
};
|
|
54
|
+
}, [stableDispatch, listeners]);
|
|
55
|
+
return state[1];
|
|
56
|
+
};
|
|
57
|
+
export {
|
|
58
|
+
useContextSelector
|
|
59
|
+
};
|
|
@@ -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,13 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
function useHasParentContext(context) {
|
|
3
|
+
const contextValue = React.useContext(
|
|
4
|
+
context
|
|
5
|
+
);
|
|
6
|
+
if (contextValue.version) {
|
|
7
|
+
return contextValue.version.current !== -1;
|
|
8
|
+
}
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
useHasParentContext
|
|
13
|
+
};
|
|
@@ -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;
|
|
@@ -1,10 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
|
|
2
3
|
function useEventCallback(fn) {
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
ref.current = fn;
|
|
4
|
+
const callbackRef = React.useRef(() => {
|
|
5
|
+
throw new Error("Cannot call an event handler while rendering");
|
|
6
6
|
});
|
|
7
|
-
|
|
7
|
+
useIsomorphicLayoutEffect(() => {
|
|
8
|
+
callbackRef.current = fn;
|
|
9
|
+
}, [fn]);
|
|
10
|
+
return React.useCallback(
|
|
11
|
+
(...args) => {
|
|
12
|
+
const callback = callbackRef.current;
|
|
13
|
+
return callback(...args);
|
|
14
|
+
},
|
|
15
|
+
[callbackRef]
|
|
16
|
+
);
|
|
8
17
|
}
|
|
9
18
|
export {
|
|
10
19
|
useEventCallback as default
|
|
@@ -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;
|
|
@@ -13,7 +13,7 @@ function useMemoizedFn(fn) {
|
|
|
13
13
|
fnRef.current = useMemo(() => fn, [fn]);
|
|
14
14
|
const memoizedFn = useRef();
|
|
15
15
|
if (!memoizedFn.current) {
|
|
16
|
-
memoizedFn.current = function(...args) {
|
|
16
|
+
memoizedFn.current = function memoized(...args) {
|
|
17
17
|
return fnRef.current.apply(this, args);
|
|
18
18
|
};
|
|
19
19
|
}
|
package/es/hooks/useSize.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { useEffect } from "react";
|
|
2
2
|
let eventTarget;
|
|
3
|
-
const Touch = function Touch2(target, identifier, pos,
|
|
4
|
-
deltaX =
|
|
5
|
-
deltaY =
|
|
3
|
+
const Touch = function Touch2(target, identifier, pos, dx = 0, dy = 0) {
|
|
4
|
+
const deltaX = dx || 0;
|
|
5
|
+
const deltaY = dy || 0;
|
|
6
6
|
this.identifier = identifier;
|
|
7
7
|
this.target = target;
|
|
8
8
|
this.clientX = pos.clientX + deltaX;
|
package/es/index.d.ts
CHANGED
package/es/index.js
CHANGED
package/es/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 @@
|
|
|
1
|
+
export { canUseDOM } from './canUseDOM';
|
package/es/ssr/index.js
ADDED
package/es/toArray.d.ts
CHANGED
package/es/toArray.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React from "react";
|
|
1
|
+
import * as React from "react";
|
|
2
2
|
import { isFragment } from "react-is";
|
|
3
3
|
function toArray(children, option = {}) {
|
|
4
4
|
let ret = [];
|
|
@@ -8,7 +8,7 @@ function toArray(children, option = {}) {
|
|
|
8
8
|
}
|
|
9
9
|
if (Array.isArray(child)) {
|
|
10
10
|
ret = ret.concat(toArray(child));
|
|
11
|
-
} else if (isFragment(child) && child
|
|
11
|
+
} else if (isFragment(child) && React.isValidElement(child)) {
|
|
12
12
|
ret = ret.concat(toArray(child.props.children, option));
|
|
13
13
|
} else {
|
|
14
14
|
ret.push(child);
|
package/es/transitions.d.ts
CHANGED
package/es/transitions.js
CHANGED
|
@@ -52,7 +52,7 @@ function formatMs(milliseconds) {
|
|
|
52
52
|
function createTransitions(inputTransitions = {}) {
|
|
53
53
|
const mergedEasing = __spreadValues(__spreadValues({}, easing), inputTransitions.easing);
|
|
54
54
|
const mergedDuration = __spreadValues(__spreadValues({}, duration), inputTransitions.duration);
|
|
55
|
-
const create = (props = ["all"], options) => {
|
|
55
|
+
const create = (props = ["all"], options = {}) => {
|
|
56
56
|
const {
|
|
57
57
|
duration: durationOption = mergedDuration.standard,
|
|
58
58
|
easing: easingOption = mergedEasing.easeInOut,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bifrostui/utils",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.14",
|
|
4
4
|
"description": "BUI React utilities for building components.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "es/index.js",
|
|
@@ -17,13 +17,14 @@
|
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"react-is": "^18.0.0",
|
|
20
|
-
"@bifrostui/styles": "2.0.0-alpha.
|
|
20
|
+
"@bifrostui/styles": "2.0.0-alpha.14"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"@tarojs/runtime": "^3.0.0",
|
|
24
24
|
"@tarojs/taro": "^3.0.0",
|
|
25
25
|
"react": "^17.0.0 || ^18.0.0",
|
|
26
|
-
"react-dom": "^17.0.0 || ^18.0.0"
|
|
26
|
+
"react-dom": "^17.0.0 || ^18.0.0",
|
|
27
|
+
"scheduler": ">=0.27.0"
|
|
27
28
|
},
|
|
28
29
|
"license": "MIT",
|
|
29
30
|
"publishConfig": {
|
|
@@ -32,6 +33,6 @@
|
|
|
32
33
|
},
|
|
33
34
|
"scripts": {
|
|
34
35
|
"build": "node esbuild.config.js",
|
|
35
|
-
"test": "
|
|
36
|
+
"test": "cd ../.. && vitest run --project bui-utils"
|
|
36
37
|
}
|
|
37
38
|
}
|