@fluentui/react-context-selector 0.0.0-nightly0025eeb82020211108.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.
@@ -0,0 +1,47 @@
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 React.createElement(Original, { value: contextValue.current }, props.children);
29
+ };
30
+ /* istanbul ignore else */
31
+ if (process.env.NODE_ENV !== 'production') {
32
+ Provider.displayName = 'ContextSelector.Provider';
33
+ }
34
+ return Provider;
35
+ };
36
+ export const createContext = (defaultValue) => {
37
+ const context = React.createContext({
38
+ value: { current: defaultValue },
39
+ version: { current: -1 },
40
+ listeners: [],
41
+ });
42
+ context.Provider = createProvider(context.Provider);
43
+ // We don't support Consumer API
44
+ delete context.Consumer;
45
+ return context;
46
+ };
47
+ //# sourceMappingURL=createContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createContext.js","sourceRoot":"","sources":["../src/createContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,O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0BAA0B;IAC1B,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;QACzC,QAAQ,CAAC,WAAW,GAAG,0BAA0B,CAAC;KACnD;IAED,OAAQ,QAA2D,CAAC;AACtE,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,CAAQ,YAAmB,EAAkB,EAAE;IAC1E,MAAM,OAAO,GAAG,KAAK,CAAC,aAAa,CAAsB;QACvD,KAAK,EAAE,EAAE,OAAO,EAAE,YAAY,EAAE;QAChC,OAAO,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE;QACxB,SAAS,EAAE,EAAE;KACd,CAAC,CAAC;IAEH,OAAO,CAAC,QAAQ,GAAG,cAAc,CAAQ,OAAO,CAAC,QAAQ,CAAC,CAAC;IAE3D,gCAAgC;IAChC,OAAS,OAAsC,CAAC,QAAQ,CAAC;IAEzD,OAAQ,OAAqC,CAAC;AAChD,CAAC,CAAC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { createContext } from './createContext';
2
+ export { useContextSelector } from './useContextSelector';
3
+ export { useHasParentContext } from './useHasParentContext';
4
+ export * from './types';
package/lib/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export { createContext } from './createContext';
2
+ export { useContextSelector } from './useContextSelector';
3
+ export { useHasParentContext } from './useHasParentContext';
4
+ export * from './types';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,cAAc,SAAS,CAAC"}
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.18.1"
9
+ }
10
+ ]
11
+ }
package/lib/types.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import * as React from 'react';
2
+ export declare type Context<Value> = React.Context<Value> & {
3
+ Provider: React.FC<React.ProviderProps<Value>>;
4
+ Consumer: never;
5
+ };
6
+ export declare type ContextSelector<Value, SelectedValue> = (value: Value) => SelectedValue;
7
+ export declare type ContextVersion = number;
8
+ export declare type ContextValue<Value> = {
9
+ /** Holds a set of subscribers from components. */
10
+ listeners: ((payload: readonly [ContextVersion, Value]) => void)[];
11
+ /** Holds an actual value of React's context that will be propagated down for computations. */
12
+ value: React.MutableRefObject<Value>;
13
+ /** A version field is used to sync a context value and consumers. */
14
+ version: React.MutableRefObject<ContextVersion>;
15
+ };
16
+ export declare type ContextValues<Value> = ContextValue<Value> & {
17
+ /** List of listners to publish changes */
18
+ listeners: ((payload: readonly [ContextVersion, Record<string, Value>]) => void)[];
19
+ };
package/lib/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
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`.
5
+ * It will trigger re-render if only the selected value is referencially changed.
6
+ */
7
+ export declare const useContextSelector: <Value, SelectedValue>(context: Context<Value>, selector: ContextSelector<Value, SelectedValue>) => SelectedValue;
@@ -0,0 +1,67 @@
1
+ import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
2
+ import * as React from 'react';
3
+ /**
4
+ * This hook returns context selected value by selector.
5
+ * It will only accept context created by `createContext`.
6
+ * It will trigger re-render if only the selected value is referencially changed.
7
+ */
8
+ export const useContextSelector = (context, selector) => {
9
+ const contextValue = React.useContext(context);
10
+ const { value: { current: value }, version: { current: version }, listeners, } = contextValue;
11
+ const selected = selector(value);
12
+ const [state, dispatch] = React.useReducer((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
+ const 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]);
39
+ if (!objectIs(state[1], selected)) {
40
+ // schedule re-render
41
+ // this is safe because it's self contained
42
+ dispatch(undefined);
43
+ }
44
+ useIsomorphicLayoutEffect(() => {
45
+ listeners.push(dispatch);
46
+ return () => {
47
+ const index = listeners.indexOf(dispatch);
48
+ listeners.splice(index, 1);
49
+ };
50
+ }, [listeners]);
51
+ return state[1];
52
+ };
53
+ /**
54
+ * inlined Object.is polyfill to avoid requiring consumers ship their own
55
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
56
+ */
57
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
+ 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
+ const objectIs =
64
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
65
+ // @ts-ignore fallback to native if it exists (not in IE11)
66
+ typeof Object.is === 'function' ? Object.is : is;
67
+ //# sourceMappingURL=useCont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MAAM,QAAQ;AACZ,6DAA6D;AAC7D,2DAA2D;AAC3D,OAAO,MAAM,CAAC,EAAE,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { Context } from './types';
2
+ /**
3
+ * Utility hook for contexts created by react-context-selector to determine if a parent context exists
4
+ * WARNING: This hook will not work for native React contexts
5
+ *
6
+ * @param context - context created by react-context-selector
7
+ * @returns whether the hook is wrapped by a parent context
8
+ */
9
+ export declare function useHasParentContext<Value>(context: Context<Value>): boolean;
@@ -0,0 +1,16 @@
1
+ import * as React from 'react';
2
+ /**
3
+ * Utility hook for contexts created by react-context-selector to determine if a parent context exists
4
+ * WARNING: This hook will not work for native React contexts
5
+ *
6
+ * @param context - context created by react-context-selector
7
+ * @returns whether the hook is wrapped by a parent context
8
+ */
9
+ export function useHasParentContext(context) {
10
+ const contextValue = React.useContext(context);
11
+ if (contextValue.version) {
12
+ return contextValue.version.current !== -1;
13
+ }
14
+ return false;
15
+ }
16
+ //# sourceMappingURL=useHasParentContext.js.map
@@ -0,0 +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,MAAM,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"}
@@ -0,0 +1,2 @@
1
+ import { Context } from './types';
2
+ export declare const createContext: <Value>(defaultValue: Value) => Context<Value>;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createContext = void 0;
4
+ const react_utilities_1 = require("@fluentui/react-utilities");
5
+ const React = require("react");
6
+ const scheduler_1 = require("scheduler");
7
+ const createProvider = (Original) => {
8
+ const Provider = props => {
9
+ // Holds an actual "props.value"
10
+ const 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
+ const versionRef = React.useRef(0);
13
+ // A stable object, is used to avoid context updates via mutation of its values.
14
+ const 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(() => {
23
+ valueRef.current = props.value;
24
+ versionRef.current += 1;
25
+ scheduler_1.unstable_runWithPriority(scheduler_1.unstable_NormalPriority, () => {
26
+ contextValue.current.listeners.forEach(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';
36
+ }
37
+ return Provider;
38
+ };
39
+ const createContext = (defaultValue) => {
40
+ const 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;
49
+ };
50
+ exports.createContext = createContext;
51
+ //# sourceMappingURL=createContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createContext.js","sourceRoot":"","sources":["../src/createContext.ts"],"names":[],"mappings":";;;AAAA,+DAAsE;AACtE,+BAA+B;AAC/B,yCAAmH;AAInH,MAAM,cAAc,GAAG,CAAQ,QAA6C,EAAE,EAAE;IAC9E,MAAM,QAAQ,GAAyC,KAAK,CAAC,EAAE;QAC7D,gCAAgC;QAChC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AG,KAAK,CAAC,aAAa,CAAsB;QACvD,KAAK,EAAE,EAAE,OAAO,EAAE,YAAY,EAAE;QAChC,OAAO,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE;QACxB,SAAS,EAAE,EAAE;KACd,CAAC,CAAC;IAEH,OAAO,CAAC,QAAQ,GAAG,cAAc,CAAQ,OAAO,CAAC,QAAQ,CAAC,CAAC;IAE3D,gCAAgC;IAChC,OAAS,OAAsC,CAAC,QAAQ,CAAC;IAEzD,OAAQ,OAAqC,CAAC;AAChD,CAAC,CAAC;AAbW,QAAA,aAAa,iBAaxB"}
@@ -0,0 +1,4 @@
1
+ export { createContext } from './createContext';
2
+ export { useContextSelector } from './useContextSelector';
3
+ export { useHasParentContext } from './useHasParentContext';
4
+ export * from './types';
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useHasParentContext = exports.useContextSelector = exports.createContext = void 0;
4
+ const 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);
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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"}
@@ -0,0 +1,19 @@
1
+ import * as React from 'react';
2
+ export declare type Context<Value> = React.Context<Value> & {
3
+ Provider: React.FC<React.ProviderProps<Value>>;
4
+ Consumer: never;
5
+ };
6
+ export declare type ContextSelector<Value, SelectedValue> = (value: Value) => SelectedValue;
7
+ export declare type ContextVersion = number;
8
+ export declare type ContextValue<Value> = {
9
+ /** Holds a set of subscribers from components. */
10
+ listeners: ((payload: readonly [ContextVersion, Value]) => void)[];
11
+ /** Holds an actual value of React's context that will be propagated down for computations. */
12
+ value: React.MutableRefObject<Value>;
13
+ /** A version field is used to sync a context value and consumers. */
14
+ version: React.MutableRefObject<ContextVersion>;
15
+ };
16
+ export declare type ContextValues<Value> = ContextValue<Value> & {
17
+ /** List of listners to publish changes */
18
+ listeners: ((payload: readonly [ContextVersion, Record<string, Value>]) => void)[];
19
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
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`.
5
+ * It will trigger re-render if only the selected value is referencially changed.
6
+ */
7
+ export declare const useContextSelector: <Value, SelectedValue>(context: Context<Value>, selector: ContextSelector<Value, SelectedValue>) => SelectedValue;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useContextSelector = void 0;
4
+ const react_utilities_1 = require("@fluentui/react-utilities");
5
+ const React = require("react");
6
+ /**
7
+ * This hook returns context selected value by selector.
8
+ * It will only accept context created by `createContext`.
9
+ * It will trigger re-render if only the selected value is referencially changed.
10
+ */
11
+ const useContextSelector = (context, selector) => {
12
+ const contextValue = React.useContext(context);
13
+ const { value: { current: value }, version: { current: version }, listeners, } = contextValue;
14
+ const selected = selector(value);
15
+ const [state, dispatch] = React.useReducer((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
+ const 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]);
42
+ if (!objectIs(state[1], selected)) {
43
+ // schedule re-render
44
+ // this is safe because it's self contained
45
+ dispatch(undefined);
46
+ }
47
+ react_utilities_1.useIsomorphicLayoutEffect(() => {
48
+ listeners.push(dispatch);
49
+ return () => {
50
+ const index = listeners.indexOf(dispatch);
51
+ listeners.splice(index, 1);
52
+ };
53
+ }, [listeners]);
54
+ return state[1];
55
+ };
56
+ exports.useContextSelector = useContextSelector;
57
+ /**
58
+ * inlined Object.is polyfill to avoid requiring consumers ship their own
59
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
60
+ */
61
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
62
+ 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
+ const objectIs =
68
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
69
+ // @ts-ignore fallback to native if it exists (not in IE11)
70
+ typeof Object.is === 'function' ? Object.is : is;
71
+ //# sourceMappingURL=useContextSelector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useContextSelector.js","sourceRoot":"","sources":["../src/useContextSel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AC,CAAC,CAAC,EAAE,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { Context } from './types';
2
+ /**
3
+ * Utility hook for contexts created by react-context-selector to determine if a parent context exists
4
+ * WARNING: This hook will not work for native React contexts
5
+ *
6
+ * @param context - context created by react-context-selector
7
+ * @returns whether the hook is wrapped by a parent context
8
+ */
9
+ export declare function useHasParentContext<Value>(context: Context<Value>): boolean;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useHasParentContext = void 0;
4
+ const React = require("react");
5
+ /**
6
+ * Utility hook for contexts created by react-context-selector to determine if a parent context exists
7
+ * WARNING: This hook will not work for native React contexts
8
+ *
9
+ * @param context - context created by react-context-selector
10
+ * @returns whether the hook is wrapped by a parent context
11
+ */
12
+ function useHasParentContext(context) {
13
+ const contextValue = React.useContext(context);
14
+ if (contextValue.version) {
15
+ return contextValue.version.current !== -1;
16
+ }
17
+ return false;
18
+ }
19
+ exports.useHasParentContext = useHasParentContext;
20
+ //# sourceMappingURL=useHasParentContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useHasParentContext.js","sourceRoot":"","sources":["../src/useHasParentContext.ts"],"names":[],"mappings":";;;AAAA,+BAA+B;AAG/B;;;;;;GAMG;AACH,SAAgB,mBAAmB,CAAQ,OAAuB;IAChE,MAAM,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"}
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@fluentui/react-context-selector",
3
+ "version": "0.0.0-nightly0025eeb82020211108.1",
4
+ "description": "React useContextSelector hook in userland",
5
+ "main": "lib-commonjs/index.js",
6
+ "module": "lib/index.js",
7
+ "typings": "lib/index.d.ts",
8
+ "sideEffects": false,
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/microsoft/fluentui"
12
+ },
13
+ "license": "MIT",
14
+ "scripts": {
15
+ "build": "just-scripts build",
16
+ "clean": "just-scripts clean",
17
+ "code-style": "just-scripts code-style",
18
+ "just": "just-scripts",
19
+ "lint": "just-scripts lint",
20
+ "test": "jest",
21
+ "docs": "api-extractor run --config=config/api-extractor.local.json --local",
22
+ "build:local": "tsc -p . --module esnext --emitDeclarationOnly && node ../../scripts/typescript/normalize-import --output dist/react-context-selector/src && yarn docs"
23
+ },
24
+ "devDependencies": {
25
+ "@fluentui/eslint-plugin": "*",
26
+ "@fluentui/scripts": "^1.0.0",
27
+ "@types/scheduler": "^0.16.1",
28
+ "@types/react": "16.9.42",
29
+ "@types/react-dom": "16.9.10",
30
+ "@types/react-test-renderer": "^16.0.0",
31
+ "react": "16.8.6",
32
+ "react-dom": "16.8.6",
33
+ "react-is": "^16.6.3",
34
+ "react-test-renderer": "^16.3.0"
35
+ },
36
+ "dependencies": {
37
+ "@fluentui/react-utilities": "0.0.0-nightly0025eeb82020211108.1",
38
+ "scheduler": "^0.20.1",
39
+ "tslib": "^2.1.0"
40
+ },
41
+ "peerDependencies": {
42
+ "@types/react": ">=16.8.0 <17.0.0",
43
+ "@types/react-dom": ">=16.8.0 <17.0.0",
44
+ "react": ">=16.8.0 <17.0.0",
45
+ "react-dom": ">=16.8.0 <17.0.0"
46
+ },
47
+ "beachball": {
48
+ "tag": "beta",
49
+ "disallowedChangeTypes": [
50
+ "major",
51
+ "minor",
52
+ "patch"
53
+ ]
54
+ }
55
+ }