@webiny/react-composition 5.28.0-beta.0

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/Compose.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import React from "react";
2
+ import { HigherOrderComponent } from "./Context";
3
+ export interface ComposableFC<TProps> extends React.FC<TProps> {
4
+ original: React.FC<TProps>;
5
+ originalName: string;
6
+ }
7
+ export interface ComposeProps {
8
+ /**
9
+ * Component to compose.
10
+ * NOTE: ComposableFC<TProps> use `any` because the type of the component props is irrelevant.
11
+ */
12
+ component: ComposableFC<any>;
13
+ with: HigherOrderComponent | HigherOrderComponent[];
14
+ }
15
+ export declare const Compose: React.FC<ComposeProps>;
package/Compose.js ADDED
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Compose = void 0;
7
+
8
+ var _react = require("react");
9
+
10
+ var _Context = require("./Context");
11
+
12
+ var Compose = function Compose(props) {
13
+ var _useComposition = (0, _Context.useComposition)(),
14
+ composeComponent = _useComposition.composeComponent;
15
+
16
+ (0, _react.useEffect)(function () {
17
+ if (typeof props.component.original === "undefined") {
18
+ console.warn("You must make your component \"<".concat(props.component.displayName, ">\" composable, by using the makeComposable() function!"));
19
+ return;
20
+ }
21
+
22
+ var hocs = Array.isArray(props.with) ? props.with : [props.with];
23
+ return composeComponent(props.component.original, hocs);
24
+ }, [props.with]);
25
+ return null;
26
+ };
27
+
28
+ exports.Compose = Compose;
package/Compose.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":["Compose","props","useComposition","composeComponent","useEffect","component","original","console","warn","displayName","hocs","Array","isArray","with"],"sources":["Compose.tsx"],"sourcesContent":["import React, { useEffect } from \"react\";\nimport { HigherOrderComponent, useComposition } from \"./Context\";\n\nexport interface ComposableFC<TProps> extends React.FC<TProps> {\n original: React.FC<TProps>;\n originalName: string;\n}\n\nexport interface ComposeProps {\n /**\n * Component to compose.\n * NOTE: ComposableFC<TProps> use `any` because the type of the component props is irrelevant.\n */\n component: ComposableFC<any>;\n with: HigherOrderComponent | HigherOrderComponent[];\n}\n\nexport const Compose: React.FC<ComposeProps> = props => {\n const { composeComponent } = useComposition();\n\n useEffect(() => {\n if (typeof props.component.original === \"undefined\") {\n console.warn(\n `You must make your component \"<${props.component.displayName}>\" composable, by using the makeComposable() function!`\n );\n\n return;\n }\n\n const hocs = Array.isArray(props.with) ? props.with : [props.with];\n return composeComponent(props.component.original, hocs);\n }, [props.with]);\n\n return null;\n};\n"],"mappings":";;;;;;;AAAA;;AACA;;AAgBO,IAAMA,OAA+B,GAAG,SAAlCA,OAAkC,CAAAC,KAAK,EAAI;EACpD,sBAA6B,IAAAC,uBAAA,GAA7B;EAAA,IAAQC,gBAAR,mBAAQA,gBAAR;;EAEA,IAAAC,gBAAA,EAAU,YAAM;IACZ,IAAI,OAAOH,KAAK,CAACI,SAAN,CAAgBC,QAAvB,KAAoC,WAAxC,EAAqD;MACjDC,OAAO,CAACC,IAAR,2CACsCP,KAAK,CAACI,SAAN,CAAgBI,WADtD;MAIA;IACH;;IAED,IAAMC,IAAI,GAAGC,KAAK,CAACC,OAAN,CAAcX,KAAK,CAACY,IAApB,IAA4BZ,KAAK,CAACY,IAAlC,GAAyC,CAACZ,KAAK,CAACY,IAAP,CAAtD;IACA,OAAOV,gBAAgB,CAACF,KAAK,CAACI,SAAN,CAAgBC,QAAjB,EAA2BI,IAA3B,CAAvB;EACH,CAXD,EAWG,CAACT,KAAK,CAACY,IAAP,CAXH;EAaA,OAAO,IAAP;AACH,CAjBM"}
package/Context.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ import React, { FC, ComponentType } from "react";
2
+ export declare function compose(...fns: HigherOrderComponent[]): (Base: FC<unknown>) => FC<unknown>;
3
+ interface ComposedComponent {
4
+ /**
5
+ * Ready to use React component.
6
+ */
7
+ component: ComponentType<unknown>;
8
+ /**
9
+ * HOCs used to compose the original component.
10
+ */
11
+ hocs: HigherOrderComponent[];
12
+ }
13
+ /**
14
+ * IMPORTANT: TInputProps default type is `any` because this interface is use as a prop type in the `Compose` component.
15
+ * You can pass any HigherOrderComponent as a prop, regardless of its TInputProps type. The only way to allow that is
16
+ * to let it be `any` in this interface.
17
+ */
18
+ export interface HigherOrderComponent<TInputProps = any, TOutputProps = TInputProps> {
19
+ (Component: FC<TInputProps>): FC<TOutputProps>;
20
+ }
21
+ declare type ComposedComponents = Map<ComponentType<unknown>, ComposedComponent>;
22
+ interface CompositionContext {
23
+ components: ComposedComponents;
24
+ getComponent(component: ComponentType<unknown>): ComponentType<unknown> | undefined;
25
+ composeComponent(component: ComponentType<unknown>, hocs: HigherOrderComponent[]): void;
26
+ }
27
+ declare const CompositionContext: React.Context<CompositionContext | undefined>;
28
+ export declare const CompositionProvider: React.FC;
29
+ export declare function useComponent(Component: ComponentType<any>): React.ComponentClass<unknown, any> | React.FunctionComponent<unknown>;
30
+ /**
31
+ * This hook will throw an error if composition context doesn't exist.
32
+ */
33
+ export declare function useComposition(): CompositionContext;
34
+ /**
35
+ * This hook will not throw an error if composition context doesn't exist.
36
+ */
37
+ export declare function useOptionalComposition(): CompositionContext | undefined;
38
+ export {};
package/Context.js ADDED
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+
3
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
+
5
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
6
+
7
+ Object.defineProperty(exports, "__esModule", {
8
+ value: true
9
+ });
10
+ exports.CompositionProvider = void 0;
11
+ exports.compose = compose;
12
+ exports.useComponent = useComponent;
13
+ exports.useComposition = useComposition;
14
+ exports.useOptionalComposition = useOptionalComposition;
15
+
16
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
17
+
18
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
19
+
20
+ var _react = _interopRequireWildcard(require("react"));
21
+
22
+ function compose() {
23
+ for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
24
+ fns[_key] = arguments[_key];
25
+ }
26
+
27
+ return function ComposedComponent(Base) {
28
+ return fns.reduceRight(function (Component, hoc) {
29
+ return hoc(Component);
30
+ }, Base);
31
+ };
32
+ }
33
+
34
+ var CompositionContext = /*#__PURE__*/(0, _react.createContext)(undefined);
35
+
36
+ var CompositionProvider = function CompositionProvider(_ref) {
37
+ var children = _ref.children;
38
+
39
+ var _useState = (0, _react.useState)(new Map()),
40
+ _useState2 = (0, _slicedToArray2.default)(_useState, 2),
41
+ components = _useState2[0],
42
+ setComponents = _useState2[1];
43
+
44
+ var composeComponent = (0, _react.useCallback)(function (component, hocs) {
45
+ setComponents(function (prevComponents) {
46
+ var components = new Map(prevComponents);
47
+ var recipe = components.get(component) || {
48
+ component: null,
49
+ hocs: []
50
+ };
51
+ var newHocs = [].concat((0, _toConsumableArray2.default)(recipe.hocs || []), (0, _toConsumableArray2.default)(hocs));
52
+ components.set(component, {
53
+ component: compose.apply(void 0, (0, _toConsumableArray2.default)((0, _toConsumableArray2.default)(newHocs).reverse()))(component),
54
+ hocs: newHocs
55
+ });
56
+ return components;
57
+ }); // Return a function that will remove the added HOCs.
58
+
59
+ return function () {
60
+ setComponents(function (prevComponents) {
61
+ var components = new Map(prevComponents);
62
+ var recipe = components.get(component) || {
63
+ component: null,
64
+ hocs: []
65
+ };
66
+ var newHOCs = (0, _toConsumableArray2.default)(recipe.hocs).filter(function (hoc) {
67
+ return !hocs.includes(hoc);
68
+ });
69
+ var NewComponent = compose.apply(void 0, (0, _toConsumableArray2.default)((0, _toConsumableArray2.default)(newHOCs).reverse()))(component);
70
+ components.set(component, {
71
+ component: NewComponent,
72
+ hocs: newHOCs
73
+ });
74
+ return components;
75
+ });
76
+ };
77
+ }, [setComponents]);
78
+ var getComponent = (0, _react.useCallback)(function (Component) {
79
+ var composedComponent = components.get(Component);
80
+ return composedComponent ? composedComponent.component : undefined;
81
+ }, [components]);
82
+ var context = (0, _react.useMemo)(function () {
83
+ return {
84
+ getComponent: getComponent,
85
+ composeComponent: composeComponent,
86
+ components: components
87
+ };
88
+ }, [components, composeComponent]);
89
+ return /*#__PURE__*/_react.default.createElement(CompositionContext.Provider, {
90
+ value: context
91
+ }, children);
92
+ };
93
+
94
+ exports.CompositionProvider = CompositionProvider;
95
+
96
+ function useComponent(Component) {
97
+ var context = useOptionalComposition();
98
+
99
+ if (!context) {
100
+ return Component;
101
+ }
102
+
103
+ return context.getComponent(Component) || Component;
104
+ }
105
+ /**
106
+ * This hook will throw an error if composition context doesn't exist.
107
+ */
108
+
109
+
110
+ function useComposition() {
111
+ var context = (0, _react.useContext)(CompositionContext);
112
+
113
+ if (!context) {
114
+ throw new Error("You're missing a <CompositionProvider> higher up in your component hierarchy!");
115
+ }
116
+
117
+ return context;
118
+ }
119
+ /**
120
+ * This hook will not throw an error if composition context doesn't exist.
121
+ */
122
+
123
+
124
+ function useOptionalComposition() {
125
+ return (0, _react.useContext)(CompositionContext);
126
+ }
package/Context.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":["compose","fns","ComposedComponent","Base","reduceRight","Component","hoc","CompositionContext","createContext","undefined","CompositionProvider","children","useState","Map","components","setComponents","composeComponent","useCallback","component","hocs","prevComponents","recipe","get","newHocs","set","reverse","newHOCs","filter","includes","NewComponent","getComponent","composedComponent","context","useMemo","useComponent","useOptionalComposition","useComposition","useContext","Error"],"sources":["Context.tsx"],"sourcesContent":["import React, {\n FC,\n ComponentType,\n useState,\n useCallback,\n createContext,\n useContext,\n useMemo\n} from \"react\";\n\nexport function compose(...fns: HigherOrderComponent[]) {\n return function ComposedComponent(Base: FC<unknown>): FC<unknown> {\n return fns.reduceRight((Component, hoc) => hoc(Component), Base);\n };\n}\n\ninterface ComposedComponent {\n /**\n * Ready to use React component.\n */\n component: ComponentType<unknown>;\n /**\n * HOCs used to compose the original component.\n */\n hocs: HigherOrderComponent[];\n}\n\n/**\n * IMPORTANT: TInputProps default type is `any` because this interface is use as a prop type in the `Compose` component.\n * You can pass any HigherOrderComponent as a prop, regardless of its TInputProps type. The only way to allow that is\n * to let it be `any` in this interface.\n */\nexport interface HigherOrderComponent<TInputProps = any, TOutputProps = TInputProps> {\n (Component: FC<TInputProps>): FC<TOutputProps>;\n}\n\ntype ComposedComponents = Map<ComponentType<unknown>, ComposedComponent>;\n\ninterface CompositionContext {\n components: ComposedComponents;\n getComponent(component: ComponentType<unknown>): ComponentType<unknown> | undefined;\n composeComponent(component: ComponentType<unknown>, hocs: HigherOrderComponent[]): void;\n}\n\nconst CompositionContext = createContext<CompositionContext | undefined>(undefined);\n\nexport const CompositionProvider: React.FC = ({ children }) => {\n const [components, setComponents] = useState<ComposedComponents>(new Map());\n\n const composeComponent = useCallback(\n (component, hocs) => {\n setComponents(prevComponents => {\n const components = new Map(prevComponents);\n const recipe = components.get(component) || { component: null, hocs: [] };\n\n const newHocs = [...(recipe.hocs || []), ...hocs];\n\n components.set(component, {\n component: compose(...[...newHocs].reverse())(component),\n hocs: newHocs\n });\n\n return components;\n });\n\n // Return a function that will remove the added HOCs.\n return () => {\n setComponents(prevComponents => {\n const components = new Map(prevComponents);\n const recipe = components.get(component) || {\n component: null,\n hocs: []\n };\n\n const newHOCs = [...recipe.hocs].filter(hoc => !hocs.includes(hoc));\n const NewComponent = compose(...[...newHOCs].reverse())(component);\n\n components.set(component, {\n component: NewComponent,\n hocs: newHOCs\n });\n\n return components;\n });\n };\n },\n [setComponents]\n );\n\n const getComponent: CompositionContext[\"getComponent\"] = useCallback(\n Component => {\n const composedComponent = components.get(Component);\n return composedComponent ? composedComponent.component : undefined;\n },\n [components]\n );\n\n const context: CompositionContext = useMemo(\n () => ({\n getComponent,\n composeComponent,\n components\n }),\n [components, composeComponent]\n );\n\n return <CompositionContext.Provider value={context}>{children}</CompositionContext.Provider>;\n};\n\nexport function useComponent(Component: ComponentType<any>) {\n const context = useOptionalComposition();\n\n if (!context) {\n return Component;\n }\n\n return context.getComponent(Component) || Component;\n}\n\n/**\n * This hook will throw an error if composition context doesn't exist.\n */\nexport function useComposition() {\n const context = useContext(CompositionContext);\n if (!context) {\n throw new Error(\n `You're missing a <CompositionProvider> higher up in your component hierarchy!`\n );\n }\n\n return context;\n}\n\n/**\n * This hook will not throw an error if composition context doesn't exist.\n */\nexport function useOptionalComposition() {\n return useContext(CompositionContext);\n}\n"],"mappings":"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"}
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # Validation
2
+ [![](https://img.shields.io/npm/dw/@webiny/react-composition.svg)](https://www.npmjs.com/package/@webiny/react-composition)
3
+ [![](https://img.shields.io/npm/v/@webiny/react-composition.svg)](https://www.npmjs.com/package/@webiny/react-composition)
4
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
+
7
+ A tiny composition framework for React components which makes it possible to compose components anywhere in your application.
8
+
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./Context";
2
+ export * from "./Compose";
3
+ export * from "./makeComposable";
package/index.js ADDED
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ var _Context = require("./Context");
8
+
9
+ Object.keys(_Context).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (key in exports && exports[key] === _Context[key]) return;
12
+ Object.defineProperty(exports, key, {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _Context[key];
16
+ }
17
+ });
18
+ });
19
+
20
+ var _Compose = require("./Compose");
21
+
22
+ Object.keys(_Compose).forEach(function (key) {
23
+ if (key === "default" || key === "__esModule") return;
24
+ if (key in exports && exports[key] === _Compose[key]) return;
25
+ Object.defineProperty(exports, key, {
26
+ enumerable: true,
27
+ get: function get() {
28
+ return _Compose[key];
29
+ }
30
+ });
31
+ });
32
+
33
+ var _makeComposable = require("./makeComposable");
34
+
35
+ Object.keys(_makeComposable).forEach(function (key) {
36
+ if (key === "default" || key === "__esModule") return;
37
+ if (key in exports && exports[key] === _makeComposable[key]) return;
38
+ Object.defineProperty(exports, key, {
39
+ enumerable: true,
40
+ get: function get() {
41
+ return _makeComposable[key];
42
+ }
43
+ });
44
+ });
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./Context\";\nexport * from \"./Compose\";\nexport * from \"./makeComposable\";\n"],"mappings":";;;;;;AAAA;;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA"}
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+ import { ComposableFC } from "./Compose";
3
+ export declare function makeComposable<TProps>(name: string, Component?: React.FC<TProps>): ComposableFC<TProps>;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+
3
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
+
5
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
6
+
7
+ Object.defineProperty(exports, "__esModule", {
8
+ value: true
9
+ });
10
+ exports.makeComposable = makeComposable;
11
+
12
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
+
14
+ var _react = _interopRequireWildcard(require("react"));
15
+
16
+ var _lodash = _interopRequireDefault(require("lodash.debounce"));
17
+
18
+ var _Context = require("./Context");
19
+
20
+ var ComposableContext = /*#__PURE__*/(0, _react.createContext)([]);
21
+ ComposableContext.displayName = "ComposableContext";
22
+
23
+ function useComposableParents() {
24
+ var context = (0, _react.useContext)(ComposableContext);
25
+
26
+ if (!context) {
27
+ return [];
28
+ }
29
+
30
+ return context;
31
+ }
32
+
33
+ var createEmptyRenderer = function createEmptyRenderer(name) {
34
+ return function EmptyRenderer() {
35
+ (0, _react.useEffect)(function () {
36
+ // We need to debounce the log, as it sometimes only requires a single tick to get the new
37
+ // composed component to render, and we don't want to scare developers for no reason.
38
+ var debounced = (0, _lodash.default)(function () {
39
+ console.info("<".concat(name, "/> is not implemented! To provide an implementation, use the <Compose/> component."));
40
+ }, 100);
41
+ return function () {
42
+ debounced.cancel();
43
+ };
44
+ }, []);
45
+ return null;
46
+ };
47
+ };
48
+
49
+ function makeComposable(name, Component) {
50
+ if (!Component) {
51
+ Component = createEmptyRenderer(name);
52
+ }
53
+
54
+ var Composable = function Composable(props) {
55
+ var parents = useComposableParents();
56
+ var ComposedComponent = (0, _Context.useComponent)(Component);
57
+ var context = (0, _react.useMemo)(function () {
58
+ return [].concat((0, _toConsumableArray2.default)(parents), [name]);
59
+ }, [parents, name]);
60
+ return /*#__PURE__*/_react.default.createElement(ComposableContext.Provider, {
61
+ value: context
62
+ }, /*#__PURE__*/_react.default.createElement(ComposedComponent, props, props.children));
63
+ };
64
+
65
+ Component.displayName = name;
66
+ Composable.original = Component;
67
+ Composable.originalName = name;
68
+ Composable.displayName = "Composable<".concat(name, ">");
69
+ return Composable;
70
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"names":["ComposableContext","createContext","displayName","useComposableParents","context","useContext","createEmptyRenderer","name","EmptyRenderer","useEffect","debounced","debounce","console","info","cancel","makeComposable","Component","Composable","props","parents","ComposedComponent","useComponent","useMemo","children","original","originalName"],"sources":["makeComposable.tsx"],"sourcesContent":["import React, { createContext, useContext, useEffect, useMemo } from \"react\";\nimport debounce from \"lodash.debounce\";\nimport { ComposableFC } from \"./Compose\";\nimport { useComponent } from \"./Context\";\n\nconst ComposableContext = createContext<string[]>([]);\nComposableContext.displayName = \"ComposableContext\";\n\nfunction useComposableParents() {\n const context = useContext(ComposableContext);\n if (!context) {\n return [];\n }\n\n return context;\n}\n\nconst createEmptyRenderer = (name: string): React.FC => {\n return function EmptyRenderer(): null {\n useEffect(() => {\n // We need to debounce the log, as it sometimes only requires a single tick to get the new\n // composed component to render, and we don't want to scare developers for no reason.\n const debounced = debounce(() => {\n console.info(\n `<${name}/> is not implemented! To provide an implementation, use the <Compose/> component.`\n );\n }, 100);\n\n return () => {\n debounced.cancel();\n };\n }, []);\n\n return null;\n };\n};\n\nexport function makeComposable<TProps>(name: string, Component?: React.FC<TProps>) {\n if (!Component) {\n Component = createEmptyRenderer(name);\n }\n\n const Composable: ComposableFC<TProps> = props => {\n const parents = useComposableParents();\n const ComposedComponent = useComponent(Component as React.FC<TProps>);\n\n const context = useMemo(() => [...parents, name], [parents, name]);\n\n return (\n <ComposableContext.Provider value={context}>\n <ComposedComponent {...props}>{props.children}</ComposedComponent>\n </ComposableContext.Provider>\n );\n };\n\n Component.displayName = name;\n\n Composable.original = Component;\n Composable.originalName = name;\n Composable.displayName = `Composable<${name}>`;\n\n return Composable;\n}\n"],"mappings":";;;;;;;;;;;;;AAAA;;AACA;;AAEA;;AAEA,IAAMA,iBAAiB,gBAAG,IAAAC,oBAAA,EAAwB,EAAxB,CAA1B;AACAD,iBAAiB,CAACE,WAAlB,GAAgC,mBAAhC;;AAEA,SAASC,oBAAT,GAAgC;EAC5B,IAAMC,OAAO,GAAG,IAAAC,iBAAA,EAAWL,iBAAX,CAAhB;;EACA,IAAI,CAACI,OAAL,EAAc;IACV,OAAO,EAAP;EACH;;EAED,OAAOA,OAAP;AACH;;AAED,IAAME,mBAAmB,GAAG,SAAtBA,mBAAsB,CAACC,IAAD,EAA4B;EACpD,OAAO,SAASC,aAAT,GAA+B;IAClC,IAAAC,gBAAA,EAAU,YAAM;MACZ;MACA;MACA,IAAMC,SAAS,GAAG,IAAAC,eAAA,EAAS,YAAM;QAC7BC,OAAO,CAACC,IAAR,YACQN,IADR;MAGH,CAJiB,EAIf,GAJe,CAAlB;MAMA,OAAO,YAAM;QACTG,SAAS,CAACI,MAAV;MACH,CAFD;IAGH,CAZD,EAYG,EAZH;IAcA,OAAO,IAAP;EACH,CAhBD;AAiBH,CAlBD;;AAoBO,SAASC,cAAT,CAAgCR,IAAhC,EAA8CS,SAA9C,EAA4E;EAC/E,IAAI,CAACA,SAAL,EAAgB;IACZA,SAAS,GAAGV,mBAAmB,CAACC,IAAD,CAA/B;EACH;;EAED,IAAMU,UAAgC,GAAG,SAAnCA,UAAmC,CAAAC,KAAK,EAAI;IAC9C,IAAMC,OAAO,GAAGhB,oBAAoB,EAApC;IACA,IAAMiB,iBAAiB,GAAG,IAAAC,qBAAA,EAAaL,SAAb,CAA1B;IAEA,IAAMZ,OAAO,GAAG,IAAAkB,cAAA,EAAQ;MAAA,kDAAUH,OAAV,IAAmBZ,IAAnB;IAAA,CAAR,EAAkC,CAACY,OAAD,EAAUZ,IAAV,CAAlC,CAAhB;IAEA,oBACI,6BAAC,iBAAD,CAAmB,QAAnB;MAA4B,KAAK,EAAEH;IAAnC,gBACI,6BAAC,iBAAD,EAAuBc,KAAvB,EAA+BA,KAAK,CAACK,QAArC,CADJ,CADJ;EAKH,CAXD;;EAaAP,SAAS,CAACd,WAAV,GAAwBK,IAAxB;EAEAU,UAAU,CAACO,QAAX,GAAsBR,SAAtB;EACAC,UAAU,CAACQ,YAAX,GAA0BlB,IAA1B;EACAU,UAAU,CAACf,WAAX,wBAAuCK,IAAvC;EAEA,OAAOU,UAAP;AACH"}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@webiny/react-composition",
3
+ "version": "5.28.0-beta.0",
4
+ "main": "index.js",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/webiny/webiny-js.git"
8
+ },
9
+ "description": "A tiny composition framework for React components.",
10
+ "contributors": [
11
+ "Pavel Denisjuk <pavel@webiny.com>",
12
+ "Sven Al Hamad <sven@webiny.com>",
13
+ "Adrian Smijulj <adrian@webiny.com>"
14
+ ],
15
+ "license": "MIT",
16
+ "dependencies": {
17
+ "@babel/runtime": "7.18.3",
18
+ "@types/react": "16.14.2",
19
+ "lodash.debounce": "4.0.8",
20
+ "react": "16.14.0",
21
+ "react-dom": "16.14.0"
22
+ },
23
+ "devDependencies": {
24
+ "@babel/cli": "^7.16.0",
25
+ "@babel/core": "^7.16.0",
26
+ "@babel/preset-env": "^7.16.4",
27
+ "@babel/preset-typescript": "^7.16.0",
28
+ "@webiny/cli": "^5.28.0-beta.0",
29
+ "@webiny/project-utils": "^5.28.0-beta.0",
30
+ "ttypescript": "^1.5.13",
31
+ "typescript": "4.5.5"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public",
35
+ "directory": "dist"
36
+ },
37
+ "scripts": {
38
+ "build": "yarn webiny run build",
39
+ "watch": "yarn webiny run watch"
40
+ },
41
+ "gitHead": "6503c7f86cfc90660f005acf72049505bfd4fb76"
42
+ }