@contentful/react-apps-toolkit 1.1.0 → 1.2.2
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/README.md +25 -1
- package/dist/SDKProvider.js +1 -1
- package/dist/SDKProvider.spec.js +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -5
- package/dist/useAutoResizer.d.ts +9 -0
- package/dist/useAutoResizer.js +28 -0
- package/dist/useAutoResizer.spec.d.ts +1 -0
- package/dist/useAutoResizer.spec.js +49 -0
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -82,7 +82,7 @@ function ComponentUsingCMA() {
|
|
|
82
82
|
const [entries, setEntries] = useState();
|
|
83
83
|
|
|
84
84
|
useEffect(() => {
|
|
85
|
-
cma.
|
|
85
|
+
cma.entry.getMany().then((data) => setEntries(data.items));
|
|
86
86
|
}, [cma]);
|
|
87
87
|
|
|
88
88
|
return <>{entries?.length}</>;
|
|
@@ -125,6 +125,30 @@ function App() {
|
|
|
125
125
|
}
|
|
126
126
|
```
|
|
127
127
|
|
|
128
|
+
### useAutoResizer
|
|
129
|
+
|
|
130
|
+
`useAutoResizer` listens for DOM changes and updates the app's height when the size changes.
|
|
131
|
+
|
|
132
|
+
Usage:
|
|
133
|
+
|
|
134
|
+
```tsx
|
|
135
|
+
import { SDKProvider, useAutoResizer } from '@contentful/react-apps-toolkit';
|
|
136
|
+
|
|
137
|
+
function ComponentUsingAutoResizer() {
|
|
138
|
+
useAutoResizer();
|
|
139
|
+
|
|
140
|
+
return <div>Component will be auto-resized</div>;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function App() {
|
|
144
|
+
return (
|
|
145
|
+
<SDKProvider>
|
|
146
|
+
<ComponentUsingAutoResizer />
|
|
147
|
+
</SDKProvider>
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
128
152
|
### Resources
|
|
129
153
|
|
|
130
154
|
- [create-contentful-app](https://www.npmjs.com/package/create-contentful-app): A starter that makes it easy to bootstrap apps for Contentful.
|
package/dist/SDKProvider.js
CHANGED
|
@@ -39,6 +39,6 @@ var SDKProvider = function (props) {
|
|
|
39
39
|
}
|
|
40
40
|
return null;
|
|
41
41
|
}
|
|
42
|
-
return (0, jsx_runtime_1.jsx)(exports.SDKContext.Provider, __assign({ value: { sdk: sdk } }, { children: props.children }));
|
|
42
|
+
return (0, jsx_runtime_1.jsx)(exports.SDKContext.Provider, __assign({ value: { sdk: sdk } }, { children: props.children }), void 0);
|
|
43
43
|
};
|
|
44
44
|
exports.SDKProvider = SDKProvider;
|
package/dist/SDKProvider.spec.js
CHANGED
|
@@ -21,13 +21,13 @@ describe('SDKProvider', function () {
|
|
|
21
21
|
consoleWarnMock === null || consoleWarnMock === void 0 ? void 0 : consoleWarnMock.mockRestore();
|
|
22
22
|
});
|
|
23
23
|
it('renders its children when sdk the init returns the sdk', function () {
|
|
24
|
-
var getByText = (0, react_1.render)((0, jsx_runtime_1.jsx)(SDKProvider_1.SDKProvider, { children: (0, jsx_runtime_1.jsx)("div", { children: "children" }) })).getByText;
|
|
24
|
+
var getByText = (0, react_1.render)((0, jsx_runtime_1.jsx)(SDKProvider_1.SDKProvider, { children: (0, jsx_runtime_1.jsx)("div", { children: "children" }, void 0) }, void 0)).getByText;
|
|
25
25
|
expect(getByText('children')).toBeTruthy();
|
|
26
26
|
});
|
|
27
27
|
it('calls console warn after timeout if callback is not returning ', function () {
|
|
28
28
|
// @ts-ignore
|
|
29
29
|
app_sdk_1.init.mockImplementation(function () { });
|
|
30
|
-
(0, react_1.render)((0, jsx_runtime_1.jsx)(SDKProvider_1.SDKProvider, { children: (0, jsx_runtime_1.jsx)("div", { children: "children" }) }));
|
|
30
|
+
(0, react_1.render)((0, jsx_runtime_1.jsx)(SDKProvider_1.SDKProvider, { children: (0, jsx_runtime_1.jsx)("div", { children: "children" }, void 0) }, void 0));
|
|
31
31
|
jest.runAllTimers();
|
|
32
32
|
expect(consoleWarnMock).toHaveBeenCalled();
|
|
33
33
|
});
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
9
5
|
}) : (function(o, m, k, k2) {
|
|
10
6
|
if (k2 === undefined) k2 = k;
|
|
11
7
|
o[k2] = m[k];
|
|
@@ -15,6 +11,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
11
|
};
|
|
16
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
13
|
__exportStar(require("./SDKProvider"), exports);
|
|
14
|
+
__exportStar(require("./useAutoResizer"), exports);
|
|
18
15
|
__exportStar(require("./useCMA"), exports);
|
|
19
16
|
__exportStar(require("./useFieldValue"), exports);
|
|
20
17
|
__exportStar(require("./useSDK"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { WindowAPI } from '@contentful/app-sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Starts/stops the auto resizer when the component is mounted/unmounted.
|
|
4
|
+
* Must be wrapped by SDKProvider.
|
|
5
|
+
* Can only be used when app is rendered in field, sidebar or dialog locations.
|
|
6
|
+
* @param {Object} params - Options to be passed to auto resizer
|
|
7
|
+
* @param {Boolean} params.absoluteElements - Defines if auto resizer should consider absolute elements
|
|
8
|
+
*/
|
|
9
|
+
export declare function useAutoResizer(...params: Parameters<WindowAPI['startAutoResizer']>): void;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useAutoResizer = void 0;
|
|
4
|
+
var react_1 = require("react");
|
|
5
|
+
var useSDK_1 = require("./useSDK");
|
|
6
|
+
/**
|
|
7
|
+
* Starts/stops the auto resizer when the component is mounted/unmounted.
|
|
8
|
+
* Must be wrapped by SDKProvider.
|
|
9
|
+
* Can only be used when app is rendered in field, sidebar or dialog locations.
|
|
10
|
+
* @param {Object} params - Options to be passed to auto resizer
|
|
11
|
+
* @param {Boolean} params.absoluteElements - Defines if auto resizer should consider absolute elements
|
|
12
|
+
*/
|
|
13
|
+
function useAutoResizer() {
|
|
14
|
+
var params = [];
|
|
15
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
16
|
+
params[_i] = arguments[_i];
|
|
17
|
+
}
|
|
18
|
+
var sdk = (0, useSDK_1.useSDK)();
|
|
19
|
+
(0, react_1.useEffect)(function () {
|
|
20
|
+
var _a;
|
|
21
|
+
if (typeof sdk.window === 'undefined') {
|
|
22
|
+
throw new Error('useAutoResizer is unavailable at this location, it can only be rendered in field, sidebar and dialog locations.');
|
|
23
|
+
}
|
|
24
|
+
(_a = sdk.window).startAutoResizer.apply(_a, params);
|
|
25
|
+
return sdk.window.stopAutoResizer;
|
|
26
|
+
}, [sdk.window]);
|
|
27
|
+
}
|
|
28
|
+
exports.useAutoResizer = useAutoResizer;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
var react_hooks_1 = require("@testing-library/react-hooks");
|
|
15
|
+
var useAutoResizer_1 = require("./useAutoResizer");
|
|
16
|
+
var useSDK_1 = require("./useSDK");
|
|
17
|
+
var mockSDK = {
|
|
18
|
+
window: {
|
|
19
|
+
startAutoResizer: jest.fn(),
|
|
20
|
+
stopAutoResizer: jest.fn(),
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
jest.mock('./useSDK', function () { return (__assign(__assign({}, jest.requireActual('./useSDK')), { useSDK: jest.fn() })); });
|
|
24
|
+
var useSDKMock = useSDK_1.useSDK;
|
|
25
|
+
beforeEach(function () {
|
|
26
|
+
jest.resetAllMocks();
|
|
27
|
+
mockSDK.window.startAutoResizer.mockImplementationOnce(function () { return function () { }; });
|
|
28
|
+
mockSDK.window.stopAutoResizer.mockImplementationOnce(function () { return function () { }; });
|
|
29
|
+
});
|
|
30
|
+
describe('useAutoResizer', function () {
|
|
31
|
+
beforeEach(function () {
|
|
32
|
+
// @ts-expect-error
|
|
33
|
+
useSDKMock.mockImplementation(function () { return mockSDK; });
|
|
34
|
+
});
|
|
35
|
+
it('should invoke the field SDK autoResizer start and stop methods', function () {
|
|
36
|
+
var hook = (0, react_hooks_1.renderHook)(useAutoResizer_1.useAutoResizer);
|
|
37
|
+
expect(mockSDK.window.startAutoResizer).toBeCalledTimes(1);
|
|
38
|
+
expect(mockSDK.window.stopAutoResizer).toBeCalledTimes(0);
|
|
39
|
+
hook.unmount();
|
|
40
|
+
expect(mockSDK.window.startAutoResizer).toBeCalledTimes(1);
|
|
41
|
+
expect(mockSDK.window.stopAutoResizer).toBeCalledTimes(1);
|
|
42
|
+
});
|
|
43
|
+
it('should accept sdk.window.startAutoResizer parameters', function () {
|
|
44
|
+
var params = { absoluteElements: true };
|
|
45
|
+
var hook = (0, react_hooks_1.renderHook)(function () { return (0, useAutoResizer_1.useAutoResizer)(params); });
|
|
46
|
+
expect(mockSDK.window.startAutoResizer).toHaveBeenCalledWith(params);
|
|
47
|
+
hook.unmount();
|
|
48
|
+
});
|
|
49
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/react-apps-toolkit",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "Toolkit for building a Contentful app in React",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "Contentful GmbH",
|
|
@@ -34,15 +34,15 @@
|
|
|
34
34
|
"react": ">=16.8.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@testing-library/react": "
|
|
38
|
-
"@testing-library/react-hooks": "
|
|
39
|
-
"@types/jest": "
|
|
40
|
-
"@types/react": "
|
|
41
|
-
"jest": "
|
|
42
|
-
"react": "
|
|
37
|
+
"@testing-library/react": "12.1.3",
|
|
38
|
+
"@testing-library/react-hooks": "8.0.0",
|
|
39
|
+
"@types/jest": "27.4.0",
|
|
40
|
+
"@types/react": "18.0.2",
|
|
41
|
+
"jest": "27.5.0",
|
|
42
|
+
"react": "17.0.2",
|
|
43
43
|
"react-dom": "17.0.2",
|
|
44
|
-
"ts-jest": "
|
|
45
|
-
"typescript": "
|
|
44
|
+
"ts-jest": "27.1.3",
|
|
45
|
+
"typescript": "4.5.5"
|
|
46
46
|
},
|
|
47
47
|
"publishConfig": {
|
|
48
48
|
"access": "public"
|
|
@@ -51,5 +51,5 @@
|
|
|
51
51
|
"@contentful/app-sdk": "^4.0.0",
|
|
52
52
|
"contentful-management": ">=7.30.0"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "4df44c41cb830360fc514b99d59a7aaa8796e15e"
|
|
55
55
|
}
|