@matrix-widget-toolkit/react 2.0.3 → 2.0.5

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.
Files changed (30) hide show
  1. package/README.md +41 -0
  2. package/build/cjs/components/CapabilitiesGuard/CapabilitiesGuard.d.cts +31 -0
  3. package/build/cjs/components/CapabilitiesGuard/CapabilitiesGuard.test.d.cts +1 -0
  4. package/build/cjs/components/CapabilitiesGuard/index.d.cts +2 -0
  5. package/build/cjs/components/ThemeSelectionProvider/ThemeSelectionProvider.d.cts +43 -0
  6. package/build/cjs/components/ThemeSelectionProvider/ThemeSelectionProvider.test.d.cts +1 -0
  7. package/build/cjs/components/ThemeSelectionProvider/index.d.cts +2 -0
  8. package/build/cjs/components/WidgetApiProvider/WidgetApiProvider.d.cts +55 -0
  9. package/build/cjs/components/WidgetApiProvider/WidgetApiProvider.test.d.cts +1 -0
  10. package/build/cjs/components/WidgetApiProvider/context.d.cts +17 -0
  11. package/build/cjs/components/WidgetApiProvider/index.d.cts +3 -0
  12. package/build/cjs/components/index.d.cts +3 -0
  13. package/build/cjs/{index.js → index.cjs} +18 -6
  14. package/build/cjs/index.d.cts +5 -0
  15. package/build/cjs/setupTests.d.cts +1 -0
  16. package/build/esm/components/CapabilitiesGuard/CapabilitiesGuard.d.ts +31 -0
  17. package/build/esm/components/CapabilitiesGuard/CapabilitiesGuard.test.d.ts +1 -0
  18. package/build/esm/components/CapabilitiesGuard/index.d.ts +2 -0
  19. package/build/esm/components/ThemeSelectionProvider/ThemeSelectionProvider.d.ts +43 -0
  20. package/build/esm/components/ThemeSelectionProvider/ThemeSelectionProvider.test.d.ts +1 -0
  21. package/build/esm/components/ThemeSelectionProvider/index.d.ts +2 -0
  22. package/build/esm/components/WidgetApiProvider/WidgetApiProvider.d.ts +55 -0
  23. package/build/esm/components/WidgetApiProvider/WidgetApiProvider.test.d.ts +1 -0
  24. package/build/esm/components/WidgetApiProvider/context.d.ts +17 -0
  25. package/build/esm/components/WidgetApiProvider/index.d.ts +3 -0
  26. package/build/esm/components/index.d.ts +3 -0
  27. package/build/esm/index.d.ts +5 -0
  28. package/build/esm/index.js +19 -7
  29. package/build/esm/setupTests.d.ts +1 -0
  30. package/package.json +35 -21
package/README.md CHANGED
@@ -17,6 +17,47 @@ yarn add @matrix-widget-toolkit/react
17
17
  While this package contains a `<WidgetApiProvider>` you probably don't want to use this package most of the time.
18
18
  Prefer using [`@matrix-widget-toolkit/mui`](../mui/) which internally uses this package to share functionality.
19
19
 
20
+ ### Customizing Widget Registration
21
+
22
+ You can customise the Widget registration with a name and type. This will be used to show the widget name in the
23
+ `Extensions` page of Element Web:
24
+
25
+ ```typescript
26
+ <WidgetApiProvider
27
+ widgetApiPromise={widgetApiPromise}
28
+ widgetRegistration={{
29
+ name: 'Example Widget',
30
+ type: 'com.example.clock',
31
+ data: { title: 'Learn more…' },
32
+ }}
33
+ >
34
+ ```
35
+
36
+ ### Checking for specific Widget Parameters
37
+
38
+ Widgets must be registered within the room state with a set of parameters that the hosting client will provide,
39
+ such as the `matrix_user_id`, `matrix_room_id` and others that are essential for the Widget to operate under
40
+ the correct context.
41
+
42
+ As new parameters have been introduced and exposed in the Widget API and hosting clients, by default they will
43
+ not be checked during registration and might not be available for the widget to use.
44
+
45
+ To check for specific parameters and require a re-registration of the widget if they are absent, you can create
46
+ the provider component with a list of required `WidgetParameter`'s:
47
+
48
+ ```typescript
49
+ <WidgetApiProvider
50
+ widgetApiPromise={widgetApiPromise}
51
+ widgetRegistration={{
52
+ name: 'Example Widget',
53
+ type: 'com.example.clock',
54
+ data: { title: 'Learn more…' },
55
+ // Device ID must be available upon registration
56
+ requiredParameters: [WidgetParameter.DeviceId],
57
+ }}
58
+ >
59
+ ```
60
+
20
61
  ### Acessing the Widget API
21
62
 
22
63
  Once the Widget API is provided to React components, use the `useWidgetApi` hook to access it:
@@ -0,0 +1,31 @@
1
+ import { Capability, WidgetEventCapability } from 'matrix-widget-api';
2
+ import { ComponentType, DispatchWithoutAction, PropsWithChildren, ReactElement } from 'react';
3
+ /**
4
+ * Props for the {@link CapabilitiesGuard} component.
5
+ */
6
+ export type CapabilitiesGuardProps = PropsWithChildren<{
7
+ /**
8
+ * Required capabilities to display the `children`.
9
+ */
10
+ capabilities: Array<WidgetEventCapability | Capability>;
11
+ /**
12
+ * Component to display if the required capabilities are missing. The
13
+ * `onRetry` callback can be used to re-request them from the user.
14
+ */
15
+ missingCapabilitiesComponent: ComponentType<{
16
+ onRetry: DispatchWithoutAction;
17
+ }>;
18
+ /**
19
+ * Component to display while the capabilities are evaluated or requested from
20
+ * the user.
21
+ */
22
+ loadingComponent: ComponentType;
23
+ }>;
24
+ /**
25
+ * A guard that ask the user for capabilities and only shows the `children`
26
+ * if all capabilities were accepted.
27
+ * If capabilities are denined, a message and a button to retry is displayed
28
+ * instead.
29
+ * @param param0 - {@link CapabilitiesGuardProps}
30
+ */
31
+ export declare function CapabilitiesGuard({ capabilities, children, missingCapabilitiesComponent: MissingCapabilitiesComponent, loadingComponent: LoadingComponent, }: CapabilitiesGuardProps): ReactElement;
@@ -0,0 +1,2 @@
1
+ export { CapabilitiesGuard } from './CapabilitiesGuard';
2
+ export type { CapabilitiesGuardProps } from './CapabilitiesGuard';
@@ -0,0 +1,43 @@
1
+ import { PropsWithChildren, ReactElement } from 'react';
2
+ /**
3
+ * Themes with different color schemes, either `light` or `dark`.
4
+ */
5
+ export type Theme = 'light' | 'dark' | string;
6
+ /**
7
+ * Return value of the {@link useThemeSelection} hook.
8
+ */
9
+ export type ThemeSelectionContextType = {
10
+ /**
11
+ * The current color scheme.
12
+ */
13
+ theme: Theme;
14
+ /**
15
+ * Whether the widget is displayed in a modal.
16
+ *
17
+ * @remarks Modals have different background colors which the theme needs to
18
+ * take into account.
19
+ */
20
+ isModal: boolean;
21
+ /**
22
+ * Select the current color scheme.
23
+ *
24
+ * @param theme - The new color scheme.
25
+ */
26
+ setTheme: (theme: Theme) => void;
27
+ };
28
+ export declare const ThemeSelectionContext: import("react").Context<ThemeSelectionContextType | undefined>;
29
+ /**
30
+ * Hook for accessing the current theme selection.
31
+ * @returns The current theme selection.
32
+ */
33
+ export declare const useThemeSelection: () => ThemeSelectionContextType;
34
+ /**
35
+ * Props for the {@link ThemeSelectionProvider} component.
36
+ */
37
+ export type ThemeSelectionProviderProps = PropsWithChildren;
38
+ /**
39
+ * Provides the current theme selection to child components.
40
+ * Use the {@link useThemeSelection} hook to access it.
41
+ * @param param0 - {@link ThemeSelectionProviderProps}
42
+ */
43
+ export declare function ThemeSelectionProvider({ children, }: ThemeSelectionProviderProps): ReactElement;
@@ -0,0 +1,2 @@
1
+ export { ThemeSelectionProvider, useThemeSelection, } from './ThemeSelectionProvider';
2
+ export type { Theme, ThemeSelectionContextType, ThemeSelectionProviderProps, } from './ThemeSelectionProvider';
@@ -0,0 +1,55 @@
1
+ import { WidgetApi, WidgetRegistration } from '@matrix-widget-toolkit/api';
2
+ import { ComponentType, DispatchWithoutAction, PropsWithChildren, ReactElement } from 'react';
3
+ import { FallbackProps } from 'react-error-boundary';
4
+ /**
5
+ * Props for the {@link (WidgetApiProvider:function)} component.
6
+ */
7
+ export type WidgetApiProviderProps = PropsWithChildren<{
8
+ /**
9
+ * Configuration to set during Widget registration.
10
+ */
11
+ widgetRegistration?: WidgetRegistration;
12
+ /**
13
+ * Result from a call to `WidgetApiImpl.create`.
14
+ */
15
+ widgetApiPromise: Promise<WidgetApi>;
16
+ /**
17
+ * Component to display while the widget API communication is established or
18
+ * while capabilities are evaluated or requested from the user.
19
+ */
20
+ loadingViewComponent: ComponentType;
21
+ /**
22
+ * Component to display if the widget is opened in an unsupported mobile
23
+ * client.
24
+ */
25
+ mobileClientErrorComponent: ComponentType;
26
+ /**
27
+ * Component to display if the widget is opened outside a Matrix client.
28
+ */
29
+ outsideClientErrorComponent: ComponentType;
30
+ /**
31
+ * Component to display when a child component fails to render.
32
+ */
33
+ childErrorComponent: ComponentType<FallbackProps>;
34
+ /**
35
+ * Component to display if the required capabilities are missing. The
36
+ * `onRetry` callback can be used to re-request them from the user.
37
+ */
38
+ missingCapabilitiesComponent: ComponentType<{
39
+ onRetry: DispatchWithoutAction;
40
+ }>;
41
+ /**
42
+ * Component to display when the widget is not properly configured in the
43
+ * room. Takes the expected `widgetRegistration` as a parameter.
44
+ */
45
+ missingParametersErrorComponent: ComponentType<{
46
+ widgetRegistration?: WidgetRegistration;
47
+ }>;
48
+ }>;
49
+ /**
50
+ * Provides the `WidgetApi` in the React context once it's fully
51
+ * initialized without errors.
52
+ * Use {@link useWidgetApi} to access it.
53
+ * @param param0 - {@link WidgetApiProviderProps}
54
+ */
55
+ export declare function WidgetApiProvider({ children, widgetApiPromise, widgetRegistration, loadingViewComponent: LoadingViewComponent, mobileClientErrorComponent: MobileClientErrorComponent, outsideClientErrorComponent: OutsideClientErrorComponent, childErrorComponent: ChildErrorComponent, missingCapabilitiesComponent: MissingCapabilitiesComponent, missingParametersErrorComponent: MissingParametersErrorComponent, }: WidgetApiProviderProps): ReactElement;
@@ -0,0 +1,17 @@
1
+ import { WidgetApi } from '@matrix-widget-toolkit/api';
2
+ export declare const WidgetApiContext: import("react").Context<WidgetApi | undefined>;
3
+ /**
4
+ * Hook for accessing the widget API.
5
+ *
6
+ * @remarks Can only be called inside a `WidgetApiProvider`
7
+ * (or WidgetApiMockProvider in tests).
8
+ *
9
+ * @returns A fully initialized widget API.
10
+ */
11
+ export declare const useWidgetApi: () => WidgetApi;
12
+ /**
13
+ * Provides a custom instance of the `WidgetApi` to the context.
14
+ *
15
+ * @remarks Should only be used in tests.
16
+ */
17
+ export declare const WidgetApiMockProvider: import("react").Provider<WidgetApi | undefined>;
@@ -0,0 +1,3 @@
1
+ export { WidgetApiMockProvider, useWidgetApi } from './context';
2
+ export { WidgetApiProvider } from './WidgetApiProvider';
3
+ export type { WidgetApiProviderProps } from './WidgetApiProvider';
@@ -0,0 +1,3 @@
1
+ export * from './CapabilitiesGuard';
2
+ export * from './ThemeSelectionProvider';
3
+ export * from './WidgetApiProvider';
@@ -88,10 +88,11 @@ var __generator = (undefined && undefined.__generator) || function (thisArg, bod
88
88
  */
89
89
  function WidgetApiProvider(_a) {
90
90
  var _this = this;
91
+ var _b;
91
92
  var children = _a.children, widgetApiPromise = _a.widgetApiPromise, widgetRegistration = _a.widgetRegistration, LoadingViewComponent = _a.loadingViewComponent, MobileClientErrorComponent = _a.mobileClientErrorComponent, OutsideClientErrorComponent = _a.outsideClientErrorComponent, ChildErrorComponent = _a.childErrorComponent, MissingCapabilitiesComponent = _a.missingCapabilitiesComponent, MissingParametersErrorComponent = _a.missingParametersErrorComponent;
92
93
  var isOpenedByClient = react.useMemo(function () { return api.extractWidgetParameters(); }, []).isOpenedByClient;
93
- var _b = react.useState(), hasInitalCapabilitiesGranted = _b[0], setInitialCapabilitiesGranted = _b[1];
94
- var _c = reactUse.useAsyncFn(function (widgetApi) { return __awaiter(_this, void 0, void 0, function () {
94
+ var _c = react.useState(), hasInitalCapabilitiesGranted = _c[0], setInitialCapabilitiesGranted = _c[1];
95
+ var _d = reactUse.useAsyncFn(function (widgetApi) { return __awaiter(_this, void 0, void 0, function () {
95
96
  return __generator(this, function (_b) {
96
97
  switch (_b.label) {
97
98
  case 0:
@@ -108,8 +109,8 @@ function WidgetApiProvider(_a) {
108
109
  case 3: return [2 /*return*/];
109
110
  }
110
111
  });
111
- }); }, []), rerequestInitialCapabilities = _c[1];
112
- var _d = reactUse.useAsync(function () { return __awaiter(_this, void 0, void 0, function () {
112
+ }); }, []), rerequestInitialCapabilities = _d[1];
113
+ var _e = reactUse.useAsync(function () { return __awaiter(_this, void 0, void 0, function () {
113
114
  var widgetApi;
114
115
  return __generator(this, function (_a) {
115
116
  switch (_a.label) {
@@ -120,7 +121,7 @@ function WidgetApiProvider(_a) {
120
121
  return [2 /*return*/, widgetApi];
121
122
  }
122
123
  });
123
- }); }, [widgetApiPromise]), widgetApi = _d.value, error = _d.error, loading = _d.loading;
124
+ }); }, [widgetApiPromise]), widgetApi = _e.value, error = _e.error, loading = _e.loading;
124
125
  if (loading) {
125
126
  return jsxRuntime.jsx(LoadingViewComponent, {});
126
127
  }
@@ -135,7 +136,18 @@ function WidgetApiProvider(_a) {
135
136
  if (!hasInitalCapabilitiesGranted) {
136
137
  return (jsxRuntime.jsx(MissingCapabilitiesComponent, { onRetry: function () { return rerequestInitialCapabilities(widgetApi); } }));
137
138
  }
138
- var hasParameters = api.hasRequiredWidgetParameters(widgetApi);
139
+ var hasParameters = api.hasWidgetParameters(widgetApi);
140
+ // Check for custom required parameters that are not part of the default setup
141
+ // and fail registration if they are missing.
142
+ var customRequiredParameters = (_b = widgetRegistration === null || widgetRegistration === void 0 ? void 0 : widgetRegistration.requiredParameters) !== null && _b !== void 0 ? _b : [];
143
+ if (customRequiredParameters.length > 0) {
144
+ hasParameters =
145
+ hasParameters &&
146
+ customRequiredParameters.every(function (param) {
147
+ return (param in widgetApi.widgetParameters &&
148
+ typeof widgetApi.widgetParameters[param] === 'string');
149
+ });
150
+ }
139
151
  return (jsxRuntime.jsx(WidgetApiContext.Provider, { value: widgetApi, children: hasParameters ? (jsxRuntime.jsx(reactErrorBoundary.ErrorBoundary, { FallbackComponent: ChildErrorComponent, children: children })) : (jsxRuntime.jsx(MissingParametersErrorComponent, { widgetRegistration: widgetRegistration })) }));
140
152
  }
141
153
 
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @packageDocumentation This is package that provides a Widget API
3
+ * intergration for React apps.
4
+ */
5
+ export * from './components';
@@ -0,0 +1 @@
1
+ import '@testing-library/jest-dom/vitest';
@@ -0,0 +1,31 @@
1
+ import { Capability, WidgetEventCapability } from 'matrix-widget-api';
2
+ import { ComponentType, DispatchWithoutAction, PropsWithChildren, ReactElement } from 'react';
3
+ /**
4
+ * Props for the {@link CapabilitiesGuard} component.
5
+ */
6
+ export type CapabilitiesGuardProps = PropsWithChildren<{
7
+ /**
8
+ * Required capabilities to display the `children`.
9
+ */
10
+ capabilities: Array<WidgetEventCapability | Capability>;
11
+ /**
12
+ * Component to display if the required capabilities are missing. The
13
+ * `onRetry` callback can be used to re-request them from the user.
14
+ */
15
+ missingCapabilitiesComponent: ComponentType<{
16
+ onRetry: DispatchWithoutAction;
17
+ }>;
18
+ /**
19
+ * Component to display while the capabilities are evaluated or requested from
20
+ * the user.
21
+ */
22
+ loadingComponent: ComponentType;
23
+ }>;
24
+ /**
25
+ * A guard that ask the user for capabilities and only shows the `children`
26
+ * if all capabilities were accepted.
27
+ * If capabilities are denined, a message and a button to retry is displayed
28
+ * instead.
29
+ * @param param0 - {@link CapabilitiesGuardProps}
30
+ */
31
+ export declare function CapabilitiesGuard({ capabilities, children, missingCapabilitiesComponent: MissingCapabilitiesComponent, loadingComponent: LoadingComponent, }: CapabilitiesGuardProps): ReactElement;
@@ -0,0 +1,2 @@
1
+ export { CapabilitiesGuard } from './CapabilitiesGuard';
2
+ export type { CapabilitiesGuardProps } from './CapabilitiesGuard';
@@ -0,0 +1,43 @@
1
+ import { PropsWithChildren, ReactElement } from 'react';
2
+ /**
3
+ * Themes with different color schemes, either `light` or `dark`.
4
+ */
5
+ export type Theme = 'light' | 'dark' | string;
6
+ /**
7
+ * Return value of the {@link useThemeSelection} hook.
8
+ */
9
+ export type ThemeSelectionContextType = {
10
+ /**
11
+ * The current color scheme.
12
+ */
13
+ theme: Theme;
14
+ /**
15
+ * Whether the widget is displayed in a modal.
16
+ *
17
+ * @remarks Modals have different background colors which the theme needs to
18
+ * take into account.
19
+ */
20
+ isModal: boolean;
21
+ /**
22
+ * Select the current color scheme.
23
+ *
24
+ * @param theme - The new color scheme.
25
+ */
26
+ setTheme: (theme: Theme) => void;
27
+ };
28
+ export declare const ThemeSelectionContext: import("react").Context<ThemeSelectionContextType | undefined>;
29
+ /**
30
+ * Hook for accessing the current theme selection.
31
+ * @returns The current theme selection.
32
+ */
33
+ export declare const useThemeSelection: () => ThemeSelectionContextType;
34
+ /**
35
+ * Props for the {@link ThemeSelectionProvider} component.
36
+ */
37
+ export type ThemeSelectionProviderProps = PropsWithChildren;
38
+ /**
39
+ * Provides the current theme selection to child components.
40
+ * Use the {@link useThemeSelection} hook to access it.
41
+ * @param param0 - {@link ThemeSelectionProviderProps}
42
+ */
43
+ export declare function ThemeSelectionProvider({ children, }: ThemeSelectionProviderProps): ReactElement;
@@ -0,0 +1,2 @@
1
+ export { ThemeSelectionProvider, useThemeSelection, } from './ThemeSelectionProvider';
2
+ export type { Theme, ThemeSelectionContextType, ThemeSelectionProviderProps, } from './ThemeSelectionProvider';
@@ -0,0 +1,55 @@
1
+ import { WidgetApi, WidgetRegistration } from '@matrix-widget-toolkit/api';
2
+ import { ComponentType, DispatchWithoutAction, PropsWithChildren, ReactElement } from 'react';
3
+ import { FallbackProps } from 'react-error-boundary';
4
+ /**
5
+ * Props for the {@link (WidgetApiProvider:function)} component.
6
+ */
7
+ export type WidgetApiProviderProps = PropsWithChildren<{
8
+ /**
9
+ * Configuration to set during Widget registration.
10
+ */
11
+ widgetRegistration?: WidgetRegistration;
12
+ /**
13
+ * Result from a call to `WidgetApiImpl.create`.
14
+ */
15
+ widgetApiPromise: Promise<WidgetApi>;
16
+ /**
17
+ * Component to display while the widget API communication is established or
18
+ * while capabilities are evaluated or requested from the user.
19
+ */
20
+ loadingViewComponent: ComponentType;
21
+ /**
22
+ * Component to display if the widget is opened in an unsupported mobile
23
+ * client.
24
+ */
25
+ mobileClientErrorComponent: ComponentType;
26
+ /**
27
+ * Component to display if the widget is opened outside a Matrix client.
28
+ */
29
+ outsideClientErrorComponent: ComponentType;
30
+ /**
31
+ * Component to display when a child component fails to render.
32
+ */
33
+ childErrorComponent: ComponentType<FallbackProps>;
34
+ /**
35
+ * Component to display if the required capabilities are missing. The
36
+ * `onRetry` callback can be used to re-request them from the user.
37
+ */
38
+ missingCapabilitiesComponent: ComponentType<{
39
+ onRetry: DispatchWithoutAction;
40
+ }>;
41
+ /**
42
+ * Component to display when the widget is not properly configured in the
43
+ * room. Takes the expected `widgetRegistration` as a parameter.
44
+ */
45
+ missingParametersErrorComponent: ComponentType<{
46
+ widgetRegistration?: WidgetRegistration;
47
+ }>;
48
+ }>;
49
+ /**
50
+ * Provides the `WidgetApi` in the React context once it's fully
51
+ * initialized without errors.
52
+ * Use {@link useWidgetApi} to access it.
53
+ * @param param0 - {@link WidgetApiProviderProps}
54
+ */
55
+ export declare function WidgetApiProvider({ children, widgetApiPromise, widgetRegistration, loadingViewComponent: LoadingViewComponent, mobileClientErrorComponent: MobileClientErrorComponent, outsideClientErrorComponent: OutsideClientErrorComponent, childErrorComponent: ChildErrorComponent, missingCapabilitiesComponent: MissingCapabilitiesComponent, missingParametersErrorComponent: MissingParametersErrorComponent, }: WidgetApiProviderProps): ReactElement;
@@ -0,0 +1,17 @@
1
+ import { WidgetApi } from '@matrix-widget-toolkit/api';
2
+ export declare const WidgetApiContext: import("react").Context<WidgetApi | undefined>;
3
+ /**
4
+ * Hook for accessing the widget API.
5
+ *
6
+ * @remarks Can only be called inside a `WidgetApiProvider`
7
+ * (or WidgetApiMockProvider in tests).
8
+ *
9
+ * @returns A fully initialized widget API.
10
+ */
11
+ export declare const useWidgetApi: () => WidgetApi;
12
+ /**
13
+ * Provides a custom instance of the `WidgetApi` to the context.
14
+ *
15
+ * @remarks Should only be used in tests.
16
+ */
17
+ export declare const WidgetApiMockProvider: import("react").Provider<WidgetApi | undefined>;
@@ -0,0 +1,3 @@
1
+ export { WidgetApiMockProvider, useWidgetApi } from './context';
2
+ export { WidgetApiProvider } from './WidgetApiProvider';
3
+ export type { WidgetApiProviderProps } from './WidgetApiProvider';
@@ -0,0 +1,3 @@
1
+ export * from './CapabilitiesGuard';
2
+ export * from './ThemeSelectionProvider';
3
+ export * from './WidgetApiProvider';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @packageDocumentation This is package that provides a Widget API
3
+ * intergration for React apps.
4
+ */
5
+ export * from './components';
@@ -1,7 +1,7 @@
1
1
  import { jsx, Fragment } from 'react/jsx-runtime';
2
2
  import { useAsyncFn, useAsync, useAsyncRetry } from 'react-use';
3
3
  import { createContext, useContext, useMemo, useState, useCallback } from 'react';
4
- import { extractWidgetParameters, hasRequiredWidgetParameters, extractWidgetApiParameters, parseWidgetId } from '@matrix-widget-toolkit/api';
4
+ import { extractWidgetParameters, hasWidgetParameters, extractWidgetApiParameters, parseWidgetId } from '@matrix-widget-toolkit/api';
5
5
  import { ErrorBoundary } from 'react-error-boundary';
6
6
 
7
7
  /*
@@ -86,10 +86,11 @@ var __generator = (undefined && undefined.__generator) || function (thisArg, bod
86
86
  */
87
87
  function WidgetApiProvider(_a) {
88
88
  var _this = this;
89
+ var _b;
89
90
  var children = _a.children, widgetApiPromise = _a.widgetApiPromise, widgetRegistration = _a.widgetRegistration, LoadingViewComponent = _a.loadingViewComponent, MobileClientErrorComponent = _a.mobileClientErrorComponent, OutsideClientErrorComponent = _a.outsideClientErrorComponent, ChildErrorComponent = _a.childErrorComponent, MissingCapabilitiesComponent = _a.missingCapabilitiesComponent, MissingParametersErrorComponent = _a.missingParametersErrorComponent;
90
91
  var isOpenedByClient = useMemo(function () { return extractWidgetParameters(); }, []).isOpenedByClient;
91
- var _b = useState(), hasInitalCapabilitiesGranted = _b[0], setInitialCapabilitiesGranted = _b[1];
92
- var _c = useAsyncFn(function (widgetApi) { return __awaiter(_this, void 0, void 0, function () {
92
+ var _c = useState(), hasInitalCapabilitiesGranted = _c[0], setInitialCapabilitiesGranted = _c[1];
93
+ var _d = useAsyncFn(function (widgetApi) { return __awaiter(_this, void 0, void 0, function () {
93
94
  return __generator(this, function (_b) {
94
95
  switch (_b.label) {
95
96
  case 0:
@@ -106,8 +107,8 @@ function WidgetApiProvider(_a) {
106
107
  case 3: return [2 /*return*/];
107
108
  }
108
109
  });
109
- }); }, []), rerequestInitialCapabilities = _c[1];
110
- var _d = useAsync(function () { return __awaiter(_this, void 0, void 0, function () {
110
+ }); }, []), rerequestInitialCapabilities = _d[1];
111
+ var _e = useAsync(function () { return __awaiter(_this, void 0, void 0, function () {
111
112
  var widgetApi;
112
113
  return __generator(this, function (_a) {
113
114
  switch (_a.label) {
@@ -118,7 +119,7 @@ function WidgetApiProvider(_a) {
118
119
  return [2 /*return*/, widgetApi];
119
120
  }
120
121
  });
121
- }); }, [widgetApiPromise]), widgetApi = _d.value, error = _d.error, loading = _d.loading;
122
+ }); }, [widgetApiPromise]), widgetApi = _e.value, error = _e.error, loading = _e.loading;
122
123
  if (loading) {
123
124
  return jsx(LoadingViewComponent, {});
124
125
  }
@@ -133,7 +134,18 @@ function WidgetApiProvider(_a) {
133
134
  if (!hasInitalCapabilitiesGranted) {
134
135
  return (jsx(MissingCapabilitiesComponent, { onRetry: function () { return rerequestInitialCapabilities(widgetApi); } }));
135
136
  }
136
- var hasParameters = hasRequiredWidgetParameters(widgetApi);
137
+ var hasParameters = hasWidgetParameters(widgetApi);
138
+ // Check for custom required parameters that are not part of the default setup
139
+ // and fail registration if they are missing.
140
+ var customRequiredParameters = (_b = widgetRegistration === null || widgetRegistration === void 0 ? void 0 : widgetRegistration.requiredParameters) !== null && _b !== void 0 ? _b : [];
141
+ if (customRequiredParameters.length > 0) {
142
+ hasParameters =
143
+ hasParameters &&
144
+ customRequiredParameters.every(function (param) {
145
+ return (param in widgetApi.widgetParameters &&
146
+ typeof widgetApi.widgetParameters[param] === 'string');
147
+ });
148
+ }
137
149
  return (jsx(WidgetApiContext.Provider, { value: widgetApi, children: hasParameters ? (jsx(ErrorBoundary, { FallbackComponent: ChildErrorComponent, children: children })) : (jsx(MissingParametersErrorComponent, { widgetRegistration: widgetRegistration })) }));
138
150
  }
139
151
 
@@ -0,0 +1 @@
1
+ import '@testing-library/jest-dom/vitest';
package/package.json CHANGED
@@ -1,27 +1,36 @@
1
1
  {
2
2
  "name": "@matrix-widget-toolkit/react",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "A simplified layer on top of @matrix-widget-toolkit/api to use it in a React based widget.",
5
5
  "author": "Nordeck IT + Consulting GmbH",
6
6
  "license": "Apache-2.0",
7
7
  "source": "./src/index.ts",
8
- "types": "./build/index.d.ts",
8
+ "types": "./build/esm/index.d.ts",
9
9
  "exports": {
10
- "import": "./build/esm/index.js",
11
- "require": "./build/cjs/index.js",
12
- "types": "./build/index.d.ts"
10
+ ".": {
11
+ "module": "./build/esm/index.js",
12
+ "import": {
13
+ "types": "./build/esm/index.d.ts",
14
+ "default": "./build/esm/index.js"
15
+ },
16
+ "require": {
17
+ "types": "./build/cjs/index.d.cts",
18
+ "default": "./build/cjs/index.cjs"
19
+ }
20
+ },
21
+ "./package.json": "./package.json"
13
22
  },
14
23
  "type": "module",
15
24
  "devDependencies": {
16
- "@testing-library/jest-dom": "^6.5.0",
17
- "@testing-library/react": "^16.0.1",
18
- "@testing-library/user-event": "^14.5.2",
19
- "@types/node": "^22.7.5",
20
- "@types/react": "^18.2.74",
21
- "@vitest/coverage-v8": "^2.1.3",
22
- "typescript": "^5.6.3",
23
- "vite": "^5.4.8",
24
- "vitest": "^2.1.3"
25
+ "@testing-library/jest-dom": "6.6.3",
26
+ "@testing-library/react": "16.2.0",
27
+ "@testing-library/user-event": "14.6.1",
28
+ "@types/node": "22.13.5",
29
+ "@types/react": "18.3.18",
30
+ "@vitest/coverage-v8": "3.0.7",
31
+ "typescript": "5.7.3",
32
+ "vite": "6.2.0",
33
+ "vitest": "3.0.7"
25
34
  },
26
35
  "scripts": {
27
36
  "build": "tsc && rollup --config ../../rollup.config.mjs",
@@ -33,14 +42,19 @@
33
42
  "postpack": "node ../../scripts/postpack.js",
34
43
  "translate": "echo \"Nothing to translate\"",
35
44
  "check-api-report": "api-extractor run --verbose",
36
- "generate-api-report": "tsc && api-extractor run --verbose --local"
45
+ "generate-api-report": "tsc && api-extractor run --verbose --local",
46
+ "clean": "yarn run clean:build",
47
+ "clean:build": "rm -rf lib",
48
+ "clean:cache": "echo 'script not implemented package'"
37
49
  },
38
50
  "dependencies": {
39
- "@matrix-widget-toolkit/api": "^3.4.2",
40
- "matrix-widget-api": "^1.9.0",
41
- "react": "^18.2.0",
42
- "react-error-boundary": "^3.1.4",
43
- "react-use": "^17.5.1"
51
+ "@matrix-widget-toolkit/api": "^4.1.0",
52
+ "matrix-widget-api": "1.13.1",
53
+ "react-error-boundary": "5.0.0",
54
+ "react-use": "17.6.0"
55
+ },
56
+ "peerDependencies": {
57
+ "react": "18.3.1"
44
58
  },
45
59
  "repository": {
46
60
  "type": "git",
@@ -56,5 +70,5 @@
56
70
  "matrix-widget-api"
57
71
  ],
58
72
  "module": "./build/esm/index.js",
59
- "main": "./build/cjs/index.js"
73
+ "main": "./build/cjs/index.cjs"
60
74
  }