@fluentui/react-context-selector 0.0.0-nightly-20230502-0418.1 → 0.0.0-nightly-20230504-0417.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/CHANGELOG.json +5 -5
- package/CHANGELOG.md +5 -5
- package/dist/index.d.ts +61 -0
- package/lib/createContext.js +57 -0
- package/lib/createContext.js.map +1 -0
- package/lib/index.js +4 -0
- package/lib/index.js.map +1 -0
- package/lib/types.js +2 -0
- package/lib/types.js.map +1 -0
- package/lib/useContextSelector.js +77 -0
- package/lib/useContextSelector.js.map +1 -0
- package/lib/useHasParentContext.js +17 -0
- package/lib/useHasParentContext.js.map +1 -0
- package/package.json +2 -2
- package/.swcrc +0 -30
package/CHANGELOG.json
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"name": "@fluentui/react-context-selector",
|
|
3
3
|
"entries": [
|
|
4
4
|
{
|
|
5
|
-
"date": "
|
|
6
|
-
"tag": "@fluentui/react-context-selector_v0.0.0-nightly-
|
|
7
|
-
"version": "0.0.0-nightly-
|
|
5
|
+
"date": "Thu, 04 May 2023 04:25:01 GMT",
|
|
6
|
+
"tag": "@fluentui/react-context-selector_v0.0.0-nightly-20230504-0417.1",
|
|
7
|
+
"version": "0.0.0-nightly-20230504-0417.1",
|
|
8
8
|
"comments": {
|
|
9
9
|
"prerelease": [
|
|
10
10
|
{
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
{
|
|
17
17
|
"author": "beachball",
|
|
18
18
|
"package": "@fluentui/react-context-selector",
|
|
19
|
-
"comment": "Bump @fluentui/react-utilities to v0.0.0-nightly-
|
|
20
|
-
"commit": "
|
|
19
|
+
"comment": "Bump @fluentui/react-utilities to v0.0.0-nightly-20230504-0417.1",
|
|
20
|
+
"commit": "433bf105e0be3c4383e246e823c6c1a56a6aea0f"
|
|
21
21
|
}
|
|
22
22
|
]
|
|
23
23
|
}
|
package/CHANGELOG.md
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
# Change Log - @fluentui/react-context-selector
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Thu, 04 May 2023 04:25:01 GMT and should not be manually modified.
|
|
4
4
|
|
|
5
5
|
<!-- Start content -->
|
|
6
6
|
|
|
7
|
-
## [0.0.0-nightly-
|
|
7
|
+
## [0.0.0-nightly-20230504-0417.1](https://github.com/microsoft/fluentui/tree/@fluentui/react-context-selector_v0.0.0-nightly-20230504-0417.1)
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-context-selector_v9.1.18..@fluentui/react-context-selector_v0.0.0-nightly-
|
|
9
|
+
Thu, 04 May 2023 04:25:01 GMT
|
|
10
|
+
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-context-selector_v9.1.18..@fluentui/react-context-selector_v0.0.0-nightly-20230504-0417.1)
|
|
11
11
|
|
|
12
12
|
### Changes
|
|
13
13
|
|
|
14
14
|
- Release nightly v9 ([commit](https://github.com/microsoft/fluentui/commit/not available) by fluentui-internal@service.microsoft.com)
|
|
15
|
-
- Bump @fluentui/react-utilities to v0.0.0-nightly-
|
|
15
|
+
- Bump @fluentui/react-utilities to v0.0.0-nightly-20230504-0417.1 ([commit](https://github.com/microsoft/fluentui/commit/433bf105e0be3c4383e246e823c6c1a56a6aea0f) by beachball)
|
|
16
16
|
|
|
17
17
|
## [9.1.18](https://github.com/microsoft/fluentui/tree/@fluentui/react-context-selector_v9.1.18)
|
|
18
18
|
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import * as React_2 from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare type Context<Value> = React_2.Context<Value> & {
|
|
7
|
+
Provider: React_2.FC<React_2.ProviderProps<Value>>;
|
|
8
|
+
Consumer: never;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export declare type ContextSelector<Value, SelectedValue> = (value: Value) => SelectedValue;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare type ContextValue<Value> = {
|
|
17
|
+
/** Holds a set of subscribers from components. */
|
|
18
|
+
listeners: ((payload: readonly [ContextVersion, Value]) => void)[];
|
|
19
|
+
/** Holds an actual value of React's context that will be propagated down for computations. */
|
|
20
|
+
value: React_2.MutableRefObject<Value>;
|
|
21
|
+
/** A version field is used to sync a context value and consumers. */
|
|
22
|
+
version: React_2.MutableRefObject<ContextVersion>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
export declare type ContextValues<Value> = ContextValue<Value> & {
|
|
29
|
+
/** List of listners to publish changes */
|
|
30
|
+
listeners: ((payload: readonly [ContextVersion, Record<string, Value>]) => void)[];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @internal
|
|
35
|
+
*/
|
|
36
|
+
export declare type ContextVersion = number;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
export declare const createContext: <Value>(defaultValue: Value) => Context<Value>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @internal
|
|
45
|
+
* This hook returns context selected value by selector.
|
|
46
|
+
* It will only accept context created by `createContext`.
|
|
47
|
+
* It will trigger re-render if only the selected value is referentially changed.
|
|
48
|
+
*/
|
|
49
|
+
export declare const useContextSelector: <Value, SelectedValue>(context: Context<Value>, selector: ContextSelector<Value, SelectedValue>) => SelectedValue;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @internal
|
|
53
|
+
* Utility hook for contexts created by react-context-selector to determine if a parent context exists
|
|
54
|
+
* WARNING: This hook will not work for native React contexts
|
|
55
|
+
*
|
|
56
|
+
* @param context - context created by react-context-selector
|
|
57
|
+
* @returns whether the hook is wrapped by a parent context
|
|
58
|
+
*/
|
|
59
|
+
export declare function useHasParentContext<Value>(context: Context<Value>): boolean;
|
|
60
|
+
|
|
61
|
+
export { }
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { unstable_NormalPriority as NormalPriority, unstable_runWithPriority as runWithPriority } from 'scheduler';
|
|
4
|
+
const createProvider = Original => {
|
|
5
|
+
const Provider = props => {
|
|
6
|
+
// Holds an actual "props.value"
|
|
7
|
+
const valueRef = React.useRef(props.value);
|
|
8
|
+
// Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.
|
|
9
|
+
const versionRef = React.useRef(0);
|
|
10
|
+
// A stable object, is used to avoid context updates via mutation of its values.
|
|
11
|
+
const contextValue = React.useRef();
|
|
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(listener => {
|
|
24
|
+
listener([versionRef.current, props.value]);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}, [props.value]);
|
|
28
|
+
return /*#__PURE__*/React.createElement(Original, {
|
|
29
|
+
value: contextValue.current
|
|
30
|
+
}, props.children);
|
|
31
|
+
};
|
|
32
|
+
/* istanbul ignore else */
|
|
33
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
34
|
+
Provider.displayName = 'ContextSelector.Provider';
|
|
35
|
+
}
|
|
36
|
+
return Provider;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
export const createContext = defaultValue => {
|
|
42
|
+
// eslint-disable-next-line @fluentui/no-context-default-value
|
|
43
|
+
const context = /*#__PURE__*/React.createContext({
|
|
44
|
+
value: {
|
|
45
|
+
current: defaultValue
|
|
46
|
+
},
|
|
47
|
+
version: {
|
|
48
|
+
current: -1
|
|
49
|
+
},
|
|
50
|
+
listeners: []
|
|
51
|
+
});
|
|
52
|
+
context.Provider = createProvider(context.Provider);
|
|
53
|
+
// We don't support Consumer API
|
|
54
|
+
delete context.Consumer;
|
|
55
|
+
return context;
|
|
56
|
+
};
|
|
57
|
+
//# sourceMappingURL=createContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useIsomorphicLayoutEffect","React","unstable_NormalPriority","NormalPriority","unstable_runWithPriority","runWithPriority","createProvider","Original","Provider","props","valueRef","useRef","value","versionRef","contextValue","current","version","listeners","forEach","listener","createElement","children","process","env","NODE_ENV","displayName","createContext","defaultValue","context","Consumer"],"sources":["../src/createContext.ts"],"sourcesContent":["import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';\nimport * as React from 'react';\nimport { unstable_NormalPriority as NormalPriority, unstable_runWithPriority as runWithPriority } from 'scheduler';\n\nimport { Context, ContextValue } from './types';\n\nconst createProvider = <Value>(Original: React.Provider<ContextValue<Value>>) => {\n const Provider: React.FC<React.ProviderProps<Value>> = props => {\n // Holds an actual \"props.value\"\n const valueRef = React.useRef(props.value);\n // Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.\n const versionRef = React.useRef(0);\n\n // A stable object, is used to avoid context updates via mutation of its values.\n const contextValue = React.useRef<ContextValue<Value>>();\n\n if (!contextValue.current) {\n contextValue.current = {\n value: valueRef,\n version: versionRef,\n listeners: [],\n };\n }\n\n useIsomorphicLayoutEffect(() => {\n valueRef.current = props.value;\n versionRef.current += 1;\n\n runWithPriority(NormalPriority, () => {\n (contextValue.current as ContextValue<Value>).listeners.forEach(listener => {\n listener([versionRef.current, props.value]);\n });\n });\n }, [props.value]);\n\n return React.createElement(Original, { value: contextValue.current }, props.children);\n };\n\n /* istanbul ignore else */\n if (process.env.NODE_ENV !== 'production') {\n Provider.displayName = 'ContextSelector.Provider';\n }\n\n return Provider as unknown as React.Provider<ContextValue<Value>>;\n};\n\n/**\n * @internal\n */\nexport const createContext = <Value>(defaultValue: Value): Context<Value> => {\n // eslint-disable-next-line @fluentui/no-context-default-value\n const context = React.createContext<ContextValue<Value>>({\n value: { current: defaultValue },\n version: { current: -1 },\n listeners: [],\n });\n\n context.Provider = createProvider<Value>(context.Provider);\n\n // We don't support Consumer API\n delete (context as unknown as Context<Value>).Consumer;\n\n return context as unknown as Context<Value>;\n};\n"],"mappings":"AAAA,SAASA,yBAAyB,QAAQ;AAC1C,YAAYC,KAAA,MAAW;AACvB,SAASC,uBAAA,IAA2BC,cAAc,EAAEC,wBAAA,IAA4BC,eAAe,QAAQ;AAIvG,MAAMC,cAAA,GAAyBC,QAAA,IAAkD;EAC/E,MAAMC,QAAA,GAAiDC,KAAA,IAAS;IAC9D;IACA,MAAMC,QAAA,GAAWT,KAAA,CAAMU,MAAM,CAACF,KAAA,CAAMG,KAAK;IACzC;IACA,MAAMC,UAAA,GAAaZ,KAAA,CAAMU,MAAM,CAAC;IAEhC;IACA,MAAMG,YAAA,GAAeb,KAAA,CAAMU,MAAM;IAEjC,IAAI,CAACG,YAAA,CAAaC,OAAO,EAAE;MACzBD,YAAA,CAAaC,OAAO,GAAG;QACrBH,KAAA,EAAOF,QAAA;QACPM,OAAA,EAASH,UAAA;QACTI,SAAA,EAAW;MACb;IACF;IAEAjB,yBAAA,CAA0B,MAAM;MAC9BU,QAAA,CAASK,OAAO,GAAGN,KAAA,CAAMG,KAAK;MAC9BC,UAAA,CAAWE,OAAO,IAAI;MAEtBV,eAAA,CAAgBF,cAAA,EAAgB,MAAM;QACnCW,YAAA,CAAaC,OAAO,CAAyBE,SAAS,CAACC,OAAO,CAACC,QAAA,IAAY;UAC1EA,QAAA,CAAS,CAACN,UAAA,CAAWE,OAAO,EAAEN,KAAA,CAAMG,KAAK,CAAC;QAC5C;MACF;IACF,GAAG,CAACH,KAAA,CAAMG,KAAK,CAAC;IAEhB,oBAAOX,KAAA,CAAMmB,aAAa,CAACb,QAAA,EAAU;MAAEK,KAAA,EAAOE,YAAA,CAAaC;IAAQ,GAAGN,KAAA,CAAMY,QAAQ;EACtF;EAEA;EACA,IAAIC,OAAA,CAAQC,GAAG,CAACC,QAAQ,KAAK,cAAc;IACzChB,QAAA,CAASiB,WAAW,GAAG;EACzB;EAEA,OAAOjB,QAAA;AACT;AAEA;;;AAGA,OAAO,MAAMkB,aAAA,GAAwBC,YAAA,IAAwC;EAC3E;EACA,MAAMC,OAAA,gBAAU3B,KAAA,CAAMyB,aAAa,CAAsB;IACvDd,KAAA,EAAO;MAAEG,OAAA,EAASY;IAAa;IAC/BX,OAAA,EAAS;MAAED,OAAA,EAAS,CAAC;IAAE;IACvBE,SAAA,EAAW;EACb;EAEAW,OAAA,CAAQpB,QAAQ,GAAGF,cAAA,CAAsBsB,OAAA,CAAQpB,QAAQ;EAEzD;EACA,OAAOoB,OAAC,CAAsCC,QAAQ;EAEtD,OAAOD,OAAA;AACT"}
|
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createContext","useContextSelector","useHasParentContext"],"sources":["../src/index.ts"],"sourcesContent":["export { createContext } from './createContext';\nexport { useContextSelector } from './useContextSelector';\nexport { useHasParentContext } from './useHasParentContext';\n// eslint-disable-next-line @fluentui/ban-context-export\nexport type { Context, ContextSelector, ContextValue, ContextValues, ContextVersion } from './types';\n"],"mappings":"AAAA,SAASA,aAAa,QAAQ;AAC9B,SAASC,kBAAkB,QAAQ;AACnC,SAASC,mBAAmB,QAAQ"}
|
package/lib/types.js
ADDED
package/lib/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React"],"sources":["../src/types.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * @internal\n */\nexport type Context<Value> = React.Context<Value> & {\n Provider: React.FC<React.ProviderProps<Value>>;\n Consumer: never;\n};\n\nexport type ContextSelector<Value, SelectedValue> = (value: Value) => SelectedValue;\n\n/**\n * @internal\n */\nexport type ContextVersion = number;\n\n/**\n * @internal\n */\nexport type ContextValue<Value> = {\n /** Holds a set of subscribers from components. */\n listeners: ((payload: readonly [ContextVersion, Value]) => void)[];\n\n /** Holds an actual value of React's context that will be propagated down for computations. */\n value: React.MutableRefObject<Value>;\n\n /** A version field is used to sync a context value and consumers. */\n version: React.MutableRefObject<ContextVersion>;\n};\n\n/**\n * @internal\n */\nexport type ContextValues<Value> = ContextValue<Value> & {\n /** List of listners to publish changes */\n listeners: ((payload: readonly [ContextVersion, Record<string, Value>]) => void)[];\n};\n"],"mappings":"AAAA,YAAYA,KAAA,MAAW"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
* This hook returns context selected value by selector.
|
|
6
|
+
* It will only accept context created by `createContext`.
|
|
7
|
+
* It will trigger re-render if only the selected value is referentially changed.
|
|
8
|
+
*/
|
|
9
|
+
export const useContextSelector = (context, selector) => {
|
|
10
|
+
const contextValue = React.useContext(context);
|
|
11
|
+
const {
|
|
12
|
+
value: {
|
|
13
|
+
current: value
|
|
14
|
+
},
|
|
15
|
+
version: {
|
|
16
|
+
current: version
|
|
17
|
+
},
|
|
18
|
+
listeners
|
|
19
|
+
} = contextValue;
|
|
20
|
+
const selected = selector(value);
|
|
21
|
+
const [state, dispatch] = React.useReducer((prevState, payload) => {
|
|
22
|
+
if (!payload) {
|
|
23
|
+
// early bail out when is dispatched during render
|
|
24
|
+
return [value, selected];
|
|
25
|
+
}
|
|
26
|
+
if (payload[0] <= version) {
|
|
27
|
+
if (objectIs(prevState[1], selected)) {
|
|
28
|
+
return prevState; // bail out
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return [value, selected];
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
if (objectIs(prevState[0], payload[1])) {
|
|
35
|
+
return prevState; // do not update
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const nextSelected = selector(payload[1]);
|
|
39
|
+
if (objectIs(prevState[1], nextSelected)) {
|
|
40
|
+
return prevState; // do not update
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return [payload[1], nextSelected];
|
|
44
|
+
} catch (e) {
|
|
45
|
+
// ignored (stale props or some other reason)
|
|
46
|
+
}
|
|
47
|
+
// explicitly spread to enforce typing
|
|
48
|
+
return [prevState[0], prevState[1]]; // schedule update
|
|
49
|
+
}, [value, selected]);
|
|
50
|
+
if (!objectIs(state[1], selected)) {
|
|
51
|
+
// schedule re-render
|
|
52
|
+
// this is safe because it's self contained
|
|
53
|
+
dispatch(undefined);
|
|
54
|
+
}
|
|
55
|
+
useIsomorphicLayoutEffect(() => {
|
|
56
|
+
listeners.push(dispatch);
|
|
57
|
+
return () => {
|
|
58
|
+
const index = listeners.indexOf(dispatch);
|
|
59
|
+
listeners.splice(index, 1);
|
|
60
|
+
};
|
|
61
|
+
}, [listeners]);
|
|
62
|
+
return state[1];
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* inlined Object.is polyfill to avoid requiring consumers ship their own
|
|
66
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
|
67
|
+
*/ // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
68
|
+
function is(x, y) {
|
|
69
|
+
return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
|
|
70
|
+
;
|
|
71
|
+
}
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
|
+
const objectIs =
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
75
|
+
// @ts-ignore fallback to native if it exists (not in IE11)
|
|
76
|
+
typeof Object.is === 'function' ? Object.is : is;
|
|
77
|
+
//# sourceMappingURL=useContextSelector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useIsomorphicLayoutEffect","React","useContextSelector","context","selector","contextValue","useContext","value","current","version","listeners","selected","state","dispatch","useReducer","prevState","payload","objectIs","nextSelected","e","undefined","push","index","indexOf","splice","is","x","y","Object"],"sources":["../src/useContextSelector.ts"],"sourcesContent":["import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';\nimport * as React from 'react';\n\nimport { Context, ContextSelector, ContextValue, ContextVersion } from './types';\n\n/**\n * Narrowing React.Reducer type to be more easily usable below.\n * No need to export this as it's for internal reducer usage.\n */\ntype ContextReducer<Value, SelectedValue> = React.Reducer<\n readonly [Value, SelectedValue],\n undefined | readonly [ContextVersion, Value]\n>;\n\n/**\n * @internal\n * This hook returns context selected value by selector.\n * It will only accept context created by `createContext`.\n * It will trigger re-render if only the selected value is referentially changed.\n */\nexport const useContextSelector = <Value, SelectedValue>(\n context: Context<Value>,\n selector: ContextSelector<Value, SelectedValue>,\n): SelectedValue => {\n const contextValue = React.useContext(context as unknown as Context<ContextValue<Value>>);\n\n const {\n value: { current: value },\n version: { current: version },\n listeners,\n } = contextValue;\n const selected = selector(value);\n\n const [state, dispatch] = React.useReducer<ContextReducer<Value, SelectedValue>>(\n (\n prevState: readonly [Value /* contextValue */, SelectedValue /* selector(value) */],\n payload:\n | undefined // undefined from render below\n | readonly [ContextVersion, Value], // from provider effect\n ): readonly [Value, SelectedValue] => {\n if (!payload) {\n // early bail out when is dispatched during render\n return [value, selected] as const;\n }\n\n if (payload[0] <= version) {\n if (objectIs(prevState[1], selected)) {\n return prevState; // bail out\n }\n\n return [value, selected] as const;\n }\n\n try {\n if (objectIs(prevState[0], payload[1])) {\n return prevState; // do not update\n }\n\n const nextSelected = selector(payload[1]);\n\n if (objectIs(prevState[1], nextSelected)) {\n return prevState; // do not update\n }\n\n return [payload[1], nextSelected] as const;\n } catch (e) {\n // ignored (stale props or some other reason)\n }\n\n // explicitly spread to enforce typing\n return [prevState[0], prevState[1]] as const; // schedule update\n },\n [value, selected] as const,\n );\n\n if (!objectIs(state[1], selected)) {\n // schedule re-render\n // this is safe because it's self contained\n dispatch(undefined);\n }\n\n useIsomorphicLayoutEffect(() => {\n listeners.push(dispatch);\n\n return () => {\n const index = listeners.indexOf(dispatch);\n listeners.splice(index, 1);\n };\n }, [listeners]);\n\n return state[1] as SelectedValue;\n};\n\n/**\n * inlined Object.is polyfill to avoid requiring consumers ship their own\n * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction is(x: any, y: any) {\n return (\n (x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y) // eslint-disable-line no-self-compare\n );\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst objectIs: (x: any, y: any) => boolean =\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore fallback to native if it exists (not in IE11)\n typeof Object.is === 'function' ? Object.is : is;\n"],"mappings":"AAAA,SAASA,yBAAyB,QAAQ;AAC1C,YAAYC,KAAA,MAAW;AAavB;;;;;;AAMA,OAAO,MAAMC,kBAAA,GAAqBA,CAChCC,OAAA,EACAC,QAAA,KACkB;EAClB,MAAMC,YAAA,GAAeJ,KAAA,CAAMK,UAAU,CAACH,OAAA;EAEtC,MAAM;IACJI,KAAA,EAAO;MAAEC,OAAA,EAASD;IAAK,CAAE;IACzBE,OAAA,EAAS;MAAED,OAAA,EAASC;IAAO,CAAE;IAC7BC;EAAS,CACV,GAAGL,YAAA;EACJ,MAAMM,QAAA,GAAWP,QAAA,CAASG,KAAA;EAE1B,MAAM,CAACK,KAAA,EAAOC,QAAA,CAAS,GAAGZ,KAAA,CAAMa,UAAU,CACxC,CACEC,SAAA,EACAC,OAAA,KAGoC;IACpC,IAAI,CAACA,OAAA,EAAS;MACZ;MACA,OAAO,CAACT,KAAA,EAAOI,QAAA,CAAS;IAC1B;IAEA,IAAIK,OAAO,CAAC,EAAE,IAAIP,OAAA,EAAS;MACzB,IAAIQ,QAAA,CAASF,SAAS,CAAC,EAAE,EAAEJ,QAAA,GAAW;QACpC,OAAOI,SAAA,EAAW;MACpB;;MAEA,OAAO,CAACR,KAAA,EAAOI,QAAA,CAAS;IAC1B;IAEA,IAAI;MACF,IAAIM,QAAA,CAASF,SAAS,CAAC,EAAE,EAAEC,OAAO,CAAC,EAAE,GAAG;QACtC,OAAOD,SAAA,EAAW;MACpB;;MAEA,MAAMG,YAAA,GAAed,QAAA,CAASY,OAAO,CAAC,EAAE;MAExC,IAAIC,QAAA,CAASF,SAAS,CAAC,EAAE,EAAEG,YAAA,GAAe;QACxC,OAAOH,SAAA,EAAW;MACpB;;MAEA,OAAO,CAACC,OAAO,CAAC,EAAE,EAAEE,YAAA,CAAa;IACnC,EAAE,OAAOC,CAAA,EAAG;MACV;IAAA;IAGF;IACA,OAAO,CAACJ,SAAS,CAAC,EAAE,EAAEA,SAAS,CAAC,EAAE,CAAC,EAAW;EAChD,GACA,CAACR,KAAA,EAAOI,QAAA,CAAS;EAGnB,IAAI,CAACM,QAAA,CAASL,KAAK,CAAC,EAAE,EAAED,QAAA,GAAW;IACjC;IACA;IACAE,QAAA,CAASO,SAAA;EACX;EAEApB,yBAAA,CAA0B,MAAM;IAC9BU,SAAA,CAAUW,IAAI,CAACR,QAAA;IAEf,OAAO,MAAM;MACX,MAAMS,KAAA,GAAQZ,SAAA,CAAUa,OAAO,CAACV,QAAA;MAChCH,SAAA,CAAUc,MAAM,CAACF,KAAA,EAAO;IAC1B;EACF,GAAG,CAACZ,SAAA,CAAU;EAEd,OAAOE,KAAK,CAAC,EAAE;AACjB;AAEA;;;GAAA,CAIA;AACA,SAASa,GAAGC,CAAM,EAAEC,CAAM,EAAE;EAC1B,OACED,CAAC,KAAMC,CAAA,KAAMD,CAAA,KAAM,KAAK,IAAIA,CAAA,KAAM,IAAIC,CAAA,KAAQD,CAAA,KAAMA,CAAA,IAAKC,CAAA,KAAMA,CAAA,CAAG;EAAA;AAEtE;AAEA;AACA,MAAMV,QAAA;AACJ;AACA;AACA,OAAOW,MAAA,CAAOH,EAAE,KAAK,aAAaG,MAAA,CAAOH,EAAE,GAAGA,EAAE"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* @internal
|
|
4
|
+
* Utility hook for contexts created by react-context-selector to determine if a parent context exists
|
|
5
|
+
* WARNING: This hook will not work for native React contexts
|
|
6
|
+
*
|
|
7
|
+
* @param context - context created by react-context-selector
|
|
8
|
+
* @returns whether the hook is wrapped by a parent context
|
|
9
|
+
*/
|
|
10
|
+
export function useHasParentContext(context) {
|
|
11
|
+
const contextValue = React.useContext(context);
|
|
12
|
+
if (contextValue.version) {
|
|
13
|
+
return contextValue.version.current !== -1;
|
|
14
|
+
}
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=useHasParentContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","useHasParentContext","context","contextValue","useContext","version","current"],"sources":["../src/useHasParentContext.ts"],"sourcesContent":["import * as React from 'react';\nimport { Context, ContextValue } from './types';\n\n/**\n * @internal\n * Utility hook for contexts created by react-context-selector to determine if a parent context exists\n * WARNING: This hook will not work for native React contexts\n *\n * @param context - context created by react-context-selector\n * @returns whether the hook is wrapped by a parent context\n */\nexport function useHasParentContext<Value>(context: Context<Value>) {\n const contextValue = React.useContext(context as unknown as Context<ContextValue<Value>>);\n\n if (contextValue.version) {\n return contextValue.version.current !== -1;\n }\n\n return false;\n}\n"],"mappings":"AAAA,YAAYA,KAAA,MAAW;AAGvB;;;;;;;;AAQA,OAAO,SAASC,oBAA2BC,OAAuB,EAAE;EAClE,MAAMC,YAAA,GAAeH,KAAA,CAAMI,UAAU,CAACF,OAAA;EAEtC,IAAIC,YAAA,CAAaE,OAAO,EAAE;IACxB,OAAOF,YAAA,CAAaE,OAAO,CAACC,OAAO,KAAK,CAAC;EAC3C;EAEA,OAAO,KAAK;AACd"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluentui/react-context-selector",
|
|
3
|
-
"version": "0.0.0-nightly-
|
|
3
|
+
"version": "0.0.0-nightly-20230504-0417.1",
|
|
4
4
|
"description": "React useContextSelector hook in userland",
|
|
5
5
|
"main": "lib-commonjs/index.js",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"@fluentui/scripts-tasks": "*"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@fluentui/react-utilities": "0.0.0-nightly-
|
|
30
|
+
"@fluentui/react-utilities": "0.0.0-nightly-20230504-0417.1",
|
|
31
31
|
"@swc/helpers": "^0.4.14"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
package/.swcrc
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://json.schemastore.org/swcrc",
|
|
3
|
-
"exclude": [
|
|
4
|
-
"/testing",
|
|
5
|
-
"/**/*.cy.ts",
|
|
6
|
-
"/**/*.cy.tsx",
|
|
7
|
-
"/**/*.spec.ts",
|
|
8
|
-
"/**/*.spec.tsx",
|
|
9
|
-
"/**/*.test.ts",
|
|
10
|
-
"/**/*.test.tsx"
|
|
11
|
-
],
|
|
12
|
-
"jsc": {
|
|
13
|
-
"parser": {
|
|
14
|
-
"syntax": "typescript",
|
|
15
|
-
"tsx": true,
|
|
16
|
-
"decorators": false,
|
|
17
|
-
"dynamicImport": false
|
|
18
|
-
},
|
|
19
|
-
"externalHelpers": true,
|
|
20
|
-
"transform": {
|
|
21
|
-
"react": {
|
|
22
|
-
"runtime": "classic",
|
|
23
|
-
"useSpread": true
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
"target": "es2019"
|
|
27
|
-
},
|
|
28
|
-
"minify": false,
|
|
29
|
-
"sourceMaps": true
|
|
30
|
-
}
|