@fluentui/react-context-selector 9.0.0-nightly.d730088d7f.0 → 9.0.0-rc.10

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.
@@ -5,63 +5,80 @@ import * as React from 'react';
5
5
  * It will only accept context created by `createContext`.
6
6
  * It will trigger re-render if only the selected value is referencially changed.
7
7
  */
8
- export var useContextSelector = function (context, selector) {
9
- var contextValue = React.useContext(context);
10
- var value = contextValue.value.current, version = contextValue.version.current, listeners = contextValue.listeners;
11
- var selected = selector(value);
12
- var _a = React.useReducer(function (prevState, payload) {
13
- if (!payload) {
14
- // early bail out when is dispatched during render
15
- return [value, selected];
16
- }
17
- if (payload[0] <= version) {
18
- if (objectIs(prevState[1], selected)) {
19
- return prevState; // bail out
20
- }
21
- return [value, selected];
22
- }
23
- try {
24
- if (objectIs(prevState[0], payload[1])) {
25
- return prevState; // do not update
26
- }
27
- var nextSelected = selector(payload[1]);
28
- if (objectIs(prevState[1], nextSelected)) {
29
- return prevState; // do not update
30
- }
31
- return [payload[1], nextSelected];
32
- }
33
- catch (e) {
34
- // ignored (stale props or some other reason)
35
- }
36
- // explicitly spread to enforce typing
37
- return [prevState[0], prevState[1]]; // schedule update
38
- }, [value, selected]), state = _a[0], dispatch = _a[1];
39
- if (!objectIs(state[1], selected)) {
40
- // schedule re-render
41
- // this is safe because it's self contained
42
- dispatch(undefined);
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];
43
25
  }
44
- useIsomorphicLayoutEffect(function () {
45
- listeners.push(dispatch);
46
- return function () {
47
- var index = listeners.indexOf(dispatch);
48
- listeners.splice(index, 1);
49
- };
50
- }, [listeners]);
51
- return state[1];
26
+
27
+ if (payload[0] <= version) {
28
+ if (objectIs(prevState[1], selected)) {
29
+ return prevState; // bail out
30
+ }
31
+
32
+ return [value, selected];
33
+ }
34
+
35
+ try {
36
+ if (objectIs(prevState[0], payload[1])) {
37
+ return prevState; // do not update
38
+ }
39
+
40
+ const nextSelected = selector(payload[1]);
41
+
42
+ if (objectIs(prevState[1], nextSelected)) {
43
+ return prevState; // do not update
44
+ }
45
+
46
+ return [payload[1], nextSelected];
47
+ } catch (e) {// ignored (stale props or some other reason)
48
+ } // explicitly spread to enforce typing
49
+
50
+
51
+ return [prevState[0], prevState[1]]; // schedule update
52
+ }, [value, selected]);
53
+
54
+ if (!objectIs(state[1], selected)) {
55
+ // schedule re-render
56
+ // this is safe because it's self contained
57
+ dispatch(undefined);
58
+ }
59
+
60
+ useIsomorphicLayoutEffect(() => {
61
+ listeners.push(dispatch);
62
+ return () => {
63
+ const index = listeners.indexOf(dispatch);
64
+ listeners.splice(index, 1);
65
+ };
66
+ }, [listeners]);
67
+ return state[1];
52
68
  };
53
69
  /**
54
70
  * inlined Object.is polyfill to avoid requiring consumers ship their own
55
71
  * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
56
72
  */
57
73
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
+
58
75
  function is(x, y) {
59
- return ((x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y) // eslint-disable-line no-self-compare
60
- );
61
- }
62
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
63
- var objectIs =
64
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
76
+ return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
77
+ ;
78
+ } // eslint-disable-next-line @typescript-eslint/no-explicit-any
79
+
80
+
81
+ const objectIs = // eslint-disable-next-line @typescript-eslint/ban-ts-comment
65
82
  // @ts-ignore fallback to native if it exists (not in IE11)
66
83
  typeof Object.is === 'function' ? Object.is : is;
67
84
  //# sourceMappingURL=useContextSelector.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useContextSelector.js","sourceRoot":"","sources":["../src/useContextSelector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAa/B;;;;GAIG;AACH,MAAM,CAAC,IAAM,kBAAkB,GAAG,UAChC,OAAuB,EACvB,QAA+C;IAE/C,IAAM,YAAY,GAAG,KAAK,CAAC,UAAU,CAAE,OAAmD,CAAC,CAAC;IAGjF,IAAS,KAAK,GAGrB,YAAY,cAHS,EACH,OAAO,GAEzB,YAAY,gBAFa,EAC3B,SAAS,GACP,YAAY,UADL,CACM;IACjB,IAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAE3B,IAAA,KAAoB,KAAK,CAAC,UAAU,CACxC,UACE,SAAmF,EACnF,OAEoC;QAEpC,IAAI,CAAC,OAAO,EAAE;YACZ,kDAAkD;YAClD,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAU,CAAC;SACnC;QAED,IAAI,OAAO,CAAC,CAAC,CAAC,IAAI,OAAO,EAAE;YACzB,IAAI,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,EAAE;gBACpC,OAAO,SAAS,CAAC,CAAC,WAAW;aAC9B;YAED,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAU,CAAC;SACnC;QAED,IAAI;YACF,IAAI,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE;gBACtC,OAAO,SAAS,CAAC,CAAC,gBAAgB;aACnC;YAED,IAAM,YAAY,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;YAE1C,IAAI,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,E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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 * 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 referencially 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"],"sourceRoot":"../src/"}
@@ -6,11 +6,14 @@ import * as React from 'react';
6
6
  * @param context - context created by react-context-selector
7
7
  * @returns whether the hook is wrapped by a parent context
8
8
  */
9
+
9
10
  export function useHasParentContext(context) {
10
- var contextValue = React.useContext(context);
11
- if (contextValue.version) {
12
- return contextValue.version.current !== -1;
13
- }
14
- return false;
11
+ const contextValue = React.useContext(context);
12
+
13
+ if (contextValue.version) {
14
+ return contextValue.version.current !== -1;
15
+ }
16
+
17
+ return false;
15
18
  }
16
19
  //# sourceMappingURL=useHasParentContext.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useHasParentContext.js","sourceRoot":"","sources":["../src/useHasParentContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CAAQ,OAAuB;IAChE,IAAM,YAAY,GAAG,KAAK,CAAC,UAAU,CAAE,OAAmD,CAAC,CAAC;IAE5F,IAAI,YAAY,CAAC,OAAO,EAAE;QACxB,OAAO,YAAY,CAAC,OAAO,CAAC,OAAO,KAAK,CAAC,CAAC,CAAC;KAC5C;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
1
+ {"version":3,"sources":["useHasParentContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AAGA;;;;;;AAMG;;AACH,OAAM,SAAU,mBAAV,CAAqC,OAArC,EAA4D;AAChE,QAAM,YAAY,GAAG,KAAK,CAAC,UAAN,CAAkB,OAAlB,CAArB;;AAEA,MAAI,YAAY,CAAC,OAAjB,EAA0B;AACxB,WAAO,YAAY,CAAC,OAAb,CAAqB,OAArB,KAAiC,CAAC,CAAzC;AACD;;AAED,SAAO,KAAP;AACD","sourcesContent":["import * as React from 'react';\nimport { Context, ContextValue } from './types';\n\n/**\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"],"sourceRoot":"../src/"}
@@ -1,51 +1,71 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
3
6
  exports.createContext = void 0;
4
- var react_utilities_1 = require("@fluentui/react-utilities");
5
- var React = require("react");
6
- var scheduler_1 = require("scheduler");
7
- var createProvider = function (Original) {
8
- var Provider = function (props) {
9
- // Holds an actual "props.value"
10
- var valueRef = React.useRef(props.value);
11
- // Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.
12
- var versionRef = React.useRef(0);
13
- // A stable object, is used to avoid context updates via mutation of its values.
14
- var contextValue = React.useRef();
15
- if (!contextValue.current) {
16
- contextValue.current = {
17
- value: valueRef,
18
- version: versionRef,
19
- listeners: [],
20
- };
21
- }
22
- react_utilities_1.useIsomorphicLayoutEffect(function () {
23
- valueRef.current = props.value;
24
- versionRef.current += 1;
25
- scheduler_1.unstable_runWithPriority(scheduler_1.unstable_NormalPriority, function () {
26
- contextValue.current.listeners.forEach(function (listener) {
27
- listener([versionRef.current, props.value]);
28
- });
29
- });
30
- }, [props.value]);
31
- return React.createElement(Original, { value: contextValue.current }, props.children);
32
- };
33
- /* istanbul ignore else */
34
- if (process.env.NODE_ENV !== 'production') {
35
- Provider.displayName = 'ContextSelector.Provider';
7
+
8
+ const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
9
+
10
+ const React = /*#__PURE__*/require("react");
11
+
12
+ const scheduler_1 = /*#__PURE__*/require("scheduler");
13
+
14
+ const createProvider = Original => {
15
+ const Provider = props => {
16
+ // Holds an actual "props.value"
17
+ const valueRef = React.useRef(props.value); // Used to sync context updates and avoid stale values, can be considered as render/effect counter of Provider.
18
+
19
+ const versionRef = React.useRef(0); // A stable object, is used to avoid context updates via mutation of its values.
20
+
21
+ const contextValue = React.useRef();
22
+
23
+ if (!contextValue.current) {
24
+ contextValue.current = {
25
+ value: valueRef,
26
+ version: versionRef,
27
+ listeners: []
28
+ };
36
29
  }
37
- return Provider;
30
+
31
+ react_utilities_1.useIsomorphicLayoutEffect(() => {
32
+ valueRef.current = props.value;
33
+ versionRef.current += 1;
34
+ scheduler_1.unstable_runWithPriority(scheduler_1.unstable_NormalPriority, () => {
35
+ contextValue.current.listeners.forEach(listener => {
36
+ listener([versionRef.current, props.value]);
37
+ });
38
+ });
39
+ }, [props.value]);
40
+ return React.createElement(Original, {
41
+ value: contextValue.current
42
+ }, props.children);
43
+ };
44
+ /* istanbul ignore else */
45
+
46
+
47
+ if (process.env.NODE_ENV !== 'production') {
48
+ Provider.displayName = 'ContextSelector.Provider';
49
+ }
50
+
51
+ return Provider;
38
52
  };
39
- var createContext = function (defaultValue) {
40
- var context = React.createContext({
41
- value: { current: defaultValue },
42
- version: { current: -1 },
43
- listeners: [],
44
- });
45
- context.Provider = createProvider(context.Provider);
46
- // We don't support Consumer API
47
- delete context.Consumer;
48
- return context;
53
+
54
+ const createContext = defaultValue => {
55
+ const context = React.createContext({
56
+ value: {
57
+ current: defaultValue
58
+ },
59
+ version: {
60
+ current: -1
61
+ },
62
+ listeners: []
63
+ });
64
+ context.Provider = createProvider(context.Provider); // We don't support Consumer API
65
+
66
+ delete context.Consumer;
67
+ return context;
49
68
  };
69
+
50
70
  exports.createContext = createContext;
51
71
  //# sourceMappingURL=createContext.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"createContext.js","sourceRoot":"","sources":["../src/createContext.ts"],"names":[],"mappings":";;;AAAA,6DAAsE;AACtE,6BAA+B;AAC/B,uCAAmH;AAInH,IAAM,cAAc,GAAG,UAAQ,QAA6C;IAC1E,IAAM,QAAQ,GAAyC,UAAA,KAAK;QAC1D,gCAAgC;QAChC,IAAM,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC3C,+GAA+G;QAC/G,IAAM,UAAU,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QAEnC,gFAAgF;QAChF,IAAM,YAAY,GAAG,KAAK,CAAC,MAAM,EAAuB,CAAC;QAEzD,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE;YACzB,YAAY,CAAC,OAAO,GAAG;gBACrB,KAAK,EAAE,QAAQ;gBACf,OAAO,EAAE,UAAU;gBACnB,SAAS,EAAE,EAAE;aACd,CAAC;SACH;QAED,2CAAyB,CAAC;YACxB,QAAQ,CAAC,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC;YAC/B,UAAU,CAAC,OAAO,IAAI,CAAC,CAAC;YAExB,oCAAe,CAAC,mCAAc,EAAE;gBAC7B,YAAY,CAAC,OAA+B,CAAC,SAAS,CAAC,OAAO,CAAC,UAAA,QAAQ;oBACtE,QAAQ,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;gBAC9C,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;QAElB,OAAO,KAAK,CAAC,aAAa,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,YAAY,C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,CAP0E,CAS1E;;AACA,SAAS,OAAsC,CAAC,QAAhD;AAEA,SAAQ,OAAR;AACD,CAbM;;AAAM,OAAA,CAAA,aAAA,GAAa,aAAb","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\nexport const createContext = <Value>(defaultValue: Value): Context<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"],"sourceRoot":"../src/"}
@@ -1,12 +1,34 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
3
6
  exports.useHasParentContext = exports.useContextSelector = exports.createContext = void 0;
4
- var tslib_1 = require("tslib");
5
- var createContext_1 = require("./createContext");
6
- Object.defineProperty(exports, "createContext", { enumerable: true, get: function () { return createContext_1.createContext; } });
7
- var useContextSelector_1 = require("./useContextSelector");
8
- Object.defineProperty(exports, "useContextSelector", { enumerable: true, get: function () { return useContextSelector_1.useContextSelector; } });
9
- var useHasParentContext_1 = require("./useHasParentContext");
10
- Object.defineProperty(exports, "useHasParentContext", { enumerable: true, get: function () { return useHasParentContext_1.useHasParentContext; } });
11
- tslib_1.__exportStar(require("./types"), exports);
7
+
8
+ var createContext_1 = /*#__PURE__*/require("./createContext");
9
+
10
+ Object.defineProperty(exports, "createContext", {
11
+ enumerable: true,
12
+ get: function () {
13
+ return createContext_1.createContext;
14
+ }
15
+ });
16
+
17
+ var useContextSelector_1 = /*#__PURE__*/require("./useContextSelector");
18
+
19
+ Object.defineProperty(exports, "useContextSelector", {
20
+ enumerable: true,
21
+ get: function () {
22
+ return useContextSelector_1.useContextSelector;
23
+ }
24
+ });
25
+
26
+ var useHasParentContext_1 = /*#__PURE__*/require("./useHasParentContext");
27
+
28
+ Object.defineProperty(exports, "useHasParentContext", {
29
+ enumerable: true,
30
+ get: function () {
31
+ return useHasParentContext_1.useHasParentContext;
32
+ }
33
+ });
12
34
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,iDAAgD;AAAvC,8GAAA,aAAa,OAAA;AACtB,2DAA0D;AAAjD,wHAAA,kBAAkB,OAAA;AAC3B,6DAA4D;AAAnD,0HAAA,mBAAmB,OAAA;AAC5B,kDAAwB"}
1
+ {"version":3,"sources":["index.ts"],"names":[],"mappings":";;;;;;;AAAA,IAAA,eAAA,gBAAA,OAAA,CAAA,iBAAA,CAAA;;AAAS,MAAA,CAAA,cAAA,CAAA,OAAA,EAAA,eAAA,EAAA;AAAA,EAAA,UAAA,EAAA,IAAA;AAAA,EAAA,GAAA,EAAA,YAAA;AAAA,WAAA,eAAA,CAAA,aAAA;AAAa;AAAb,CAAA;;AACT,IAAA,oBAAA,gBAAA,OAAA,CAAA,sBAAA,CAAA;;AAAS,MAAA,CAAA,cAAA,CAAA,OAAA,EAAA,oBAAA,EAAA;AAAA,EAAA,UAAA,EAAA,IAAA;AAAA,EAAA,GAAA,EAAA,YAAA;AAAA,WAAA,oBAAA,CAAA,kBAAA;AAAkB;AAAlB,CAAA;;AACT,IAAA,qBAAA,gBAAA,OAAA,CAAA,uBAAA,CAAA;;AAAS,MAAA,CAAA,cAAA,CAAA,OAAA,EAAA,qBAAA,EAAA;AAAA,EAAA,UAAA,EAAA,IAAA;AAAA,EAAA,GAAA,EAAA,YAAA;AAAA,WAAA,qBAAA,CAAA,mBAAA;AAAmB;AAAnB,CAAA","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"],"sourceRoot":"../src/"}
@@ -1,3 +1,6 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
3
6
  //# sourceMappingURL=types.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
1
+ {"version":3,"sources":[],"names":[],"mappings":"","sourceRoot":"../src/"}
@@ -1,71 +1,95 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
3
6
  exports.useContextSelector = void 0;
4
- var react_utilities_1 = require("@fluentui/react-utilities");
5
- var React = require("react");
7
+
8
+ const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
9
+
10
+ const React = /*#__PURE__*/require("react");
6
11
  /**
7
12
  * This hook returns context selected value by selector.
8
13
  * It will only accept context created by `createContext`.
9
14
  * It will trigger re-render if only the selected value is referencially changed.
10
15
  */
11
- var useContextSelector = function (context, selector) {
12
- var contextValue = React.useContext(context);
13
- var value = contextValue.value.current, version = contextValue.version.current, listeners = contextValue.listeners;
14
- var selected = selector(value);
15
- var _a = React.useReducer(function (prevState, payload) {
16
- if (!payload) {
17
- // early bail out when is dispatched during render
18
- return [value, selected];
19
- }
20
- if (payload[0] <= version) {
21
- if (objectIs(prevState[1], selected)) {
22
- return prevState; // bail out
23
- }
24
- return [value, selected];
25
- }
26
- try {
27
- if (objectIs(prevState[0], payload[1])) {
28
- return prevState; // do not update
29
- }
30
- var nextSelected = selector(payload[1]);
31
- if (objectIs(prevState[1], nextSelected)) {
32
- return prevState; // do not update
33
- }
34
- return [payload[1], nextSelected];
35
- }
36
- catch (e) {
37
- // ignored (stale props or some other reason)
38
- }
39
- // explicitly spread to enforce typing
40
- return [prevState[0], prevState[1]]; // schedule update
41
- }, [value, selected]), state = _a[0], dispatch = _a[1];
42
- if (!objectIs(state[1], selected)) {
43
- // schedule re-render
44
- // this is safe because it's self contained
45
- dispatch(undefined);
16
+
17
+
18
+ const useContextSelector = (context, selector) => {
19
+ const contextValue = React.useContext(context);
20
+ const {
21
+ value: {
22
+ current: value
23
+ },
24
+ version: {
25
+ current: version
26
+ },
27
+ listeners
28
+ } = contextValue;
29
+ const selected = selector(value);
30
+ const [state, dispatch] = React.useReducer((prevState, payload) => {
31
+ if (!payload) {
32
+ // early bail out when is dispatched during render
33
+ return [value, selected];
46
34
  }
47
- react_utilities_1.useIsomorphicLayoutEffect(function () {
48
- listeners.push(dispatch);
49
- return function () {
50
- var index = listeners.indexOf(dispatch);
51
- listeners.splice(index, 1);
52
- };
53
- }, [listeners]);
54
- return state[1];
35
+
36
+ if (payload[0] <= version) {
37
+ if (objectIs(prevState[1], selected)) {
38
+ return prevState; // bail out
39
+ }
40
+
41
+ return [value, selected];
42
+ }
43
+
44
+ try {
45
+ if (objectIs(prevState[0], payload[1])) {
46
+ return prevState; // do not update
47
+ }
48
+
49
+ const nextSelected = selector(payload[1]);
50
+
51
+ if (objectIs(prevState[1], nextSelected)) {
52
+ return prevState; // do not update
53
+ }
54
+
55
+ return [payload[1], nextSelected];
56
+ } catch (e) {// ignored (stale props or some other reason)
57
+ } // explicitly spread to enforce typing
58
+
59
+
60
+ return [prevState[0], prevState[1]]; // schedule update
61
+ }, [value, selected]);
62
+
63
+ if (!objectIs(state[1], selected)) {
64
+ // schedule re-render
65
+ // this is safe because it's self contained
66
+ dispatch(undefined);
67
+ }
68
+
69
+ react_utilities_1.useIsomorphicLayoutEffect(() => {
70
+ listeners.push(dispatch);
71
+ return () => {
72
+ const index = listeners.indexOf(dispatch);
73
+ listeners.splice(index, 1);
74
+ };
75
+ }, [listeners]);
76
+ return state[1];
55
77
  };
78
+
56
79
  exports.useContextSelector = useContextSelector;
57
80
  /**
58
81
  * inlined Object.is polyfill to avoid requiring consumers ship their own
59
82
  * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
60
83
  */
61
84
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
85
+
62
86
  function is(x, y) {
63
- return ((x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y) // eslint-disable-line no-self-compare
64
- );
65
- }
66
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
67
- var objectIs =
68
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
87
+ return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
88
+ ;
89
+ } // eslint-disable-next-line @typescript-eslint/no-explicit-any
90
+
91
+
92
+ const objectIs = // eslint-disable-next-line @typescript-eslint/ban-ts-comment
69
93
  // @ts-ignore fallback to native if it exists (not in IE11)
70
94
  typeof Object.is === 'function' ? Object.is : is;
71
95
  //# sourceMappingURL=useCont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{ 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 * 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 referencially 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"],"sourceRoot":"../src/"}
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
3
6
  exports.useHasParentContext = void 0;
4
- var React = require("react");
7
+
8
+ const React = /*#__PURE__*/require("react");
5
9
  /**
6
10
  * Utility hook for contexts created by react-context-selector to determine if a parent context exists
7
11
  * WARNING: This hook will not work for native React contexts
@@ -9,12 +13,17 @@ var React = require("react");
9
13
  * @param context - context created by react-context-selector
10
14
  * @returns whether the hook is wrapped by a parent context
11
15
  */
16
+
17
+
12
18
  function useHasParentContext(context) {
13
- var contextValue = React.useContext(context);
14
- if (contextValue.version) {
15
- return contextValue.version.current !== -1;
16
- }
17
- return false;
19
+ const contextValue = React.useContext(context);
20
+
21
+ if (contextValue.version) {
22
+ return contextValue.version.current !== -1;
23
+ }
24
+
25
+ return false;
18
26
  }
27
+
19
28
  exports.useHasParentContext = useHasParentContext;
20
29
  //# sourceMappingURL=useHasParentContext.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useHasParentContext.js","sourceRoot":"","sources":["../src/useHasParentContext.ts"],"names":[],"mappings":";;;AAAA,6BAA+B;AAG/B;;;;;;GAMG;AACH,SAAgB,mBAAmB,CAAQ,OAAuB;IAChE,IAAM,YAAY,GAAG,KAAK,CAAC,UAAU,CAAE,OAAmD,CAAC,CAAC;IAE5F,IAAI,YAAY,CAAC,OAAO,EAAE;QACxB,OAAO,YAAY,CAAC,OAAO,CAAC,OAAO,KAAK,CAAC,CAAC,CAAC;KAC5C;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AARD,kDAQC"}
1
+ {"version":3,"sources":["useHasParentContext.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,KAAA,gBAAA,OAAA,CAAA,OAAA,CAAA;AAGA;;;;;;AAMG;;;AACH,SAAgB,mBAAhB,CAA2C,OAA3C,EAAkE;AAChE,QAAM,YAAY,GAAG,KAAK,CAAC,UAAN,CAAkB,OAAlB,CAArB;;AAEA,MAAI,YAAY,CAAC,OAAjB,EAA0B;AACxB,WAAO,YAAY,CAAC,OAAb,CAAqB,OAArB,KAAiC,CAAC,CAAzC;AACD;;AAED,SAAO,KAAP;AACD;;AARD,OAAA,CAAA,mBAAA,GAAA,mBAAA","sourcesContent":["import * as React from 'react';\nimport { Context, ContextValue } from './types';\n\n/**\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"],"sourceRoot":"../src/"}