@appsemble/preact 0.20.8 → 0.20.11
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/.eslintrc.yaml +4 -0
- package/README.md +6 -3
- package/changed/added/.gitkeep +0 -0
- package/changed/changed/.gitkeep +0 -0
- package/changed/deprecated/.gitkeep +0 -0
- package/changed/fixed/.gitkeep +0 -0
- package/changed/removed/.gitkeep +0 -0
- package/changed/security/.gitkeep +0 -0
- package/{dist/index.d.ts → index.d.ts} +2 -2
- package/{dist/index.js → index.js} +2 -2
- package/index.js.map +1 -0
- package/index.tsx +94 -0
- package/jest.config.js +1 -0
- package/package.json +6 -5
- package/tsconfig.json +8 -0
- package/dist/index.js.map +0 -1
package/.eslintrc.yaml
ADDED
package/README.md
CHANGED
|
@@ -27,9 +27,10 @@ called when the block is done rendering. Actions won’t be finalized for any bl
|
|
|
27
27
|
until all blocks are ready.
|
|
28
28
|
|
|
29
29
|
```tsx
|
|
30
|
-
import { bootstrap } from '@appsemble/preact';
|
|
31
30
|
import { useEffect } from 'preact/hooks';
|
|
32
31
|
|
|
32
|
+
import { bootstrap } from '@appsemble/preact';
|
|
33
|
+
|
|
33
34
|
bootstrap(({ actions }) => {
|
|
34
35
|
useEffect(() => {
|
|
35
36
|
ready();
|
|
@@ -52,9 +53,10 @@ Get the block context. This is available for components that are rendered within
|
|
|
52
53
|
component rendered by [`bootstrap`](#bootstrap)
|
|
53
54
|
|
|
54
55
|
```tsx
|
|
55
|
-
import { useBlock } from '@appsemble/preact';
|
|
56
56
|
import { VNode } from 'preact';
|
|
57
57
|
|
|
58
|
+
import { useBlock } from '@appsemble/preact';
|
|
59
|
+
|
|
58
60
|
export function MyButton(): VNode {
|
|
59
61
|
const { actions } = useBlock();
|
|
60
62
|
|
|
@@ -71,9 +73,10 @@ export function MyButton(): VNode {
|
|
|
71
73
|
This helper component renders a translated message ID.
|
|
72
74
|
|
|
73
75
|
```tsx
|
|
74
|
-
import { FormattedMessage } from '@appsemble/preact';
|
|
75
76
|
import { VNode } from 'preact';
|
|
76
77
|
|
|
78
|
+
import { FormattedMessage } from '@appsemble/preact';
|
|
79
|
+
|
|
77
80
|
interface MyButtonProps {
|
|
78
81
|
name: string;
|
|
79
82
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -9,7 +9,7 @@ export interface BlockProps extends BootstrapParams {
|
|
|
9
9
|
/**
|
|
10
10
|
* Mount a Preact component returned by a bootstrap function in the shadow DOM of a block.
|
|
11
11
|
*
|
|
12
|
-
* @param Component
|
|
12
|
+
* @param Component The Preact component to mount.
|
|
13
13
|
* @returns A promise which gets resolved if the component calls `ready()`.
|
|
14
14
|
*/
|
|
15
15
|
export declare function mount(Component: ComponentType<BlockProps>): (params: BootstrapParams) => Promise<void>;
|
|
@@ -17,7 +17,7 @@ export declare function bootstrap(Component: ComponentType<BlockProps>): void;
|
|
|
17
17
|
/**
|
|
18
18
|
* A HOC which passes the Appsemble block values to he wrapped Preact component.
|
|
19
19
|
*
|
|
20
|
-
* @param Component
|
|
20
|
+
* @param Component The Preact componen to wrap.
|
|
21
21
|
* @returns The wrapper component.
|
|
22
22
|
* @deprecated Use `useBlock()` instead.
|
|
23
23
|
*/
|
|
@@ -6,7 +6,7 @@ const Context = createContext(null);
|
|
|
6
6
|
/**
|
|
7
7
|
* Mount a Preact component returned by a bootstrap function in the shadow DOM of a block.
|
|
8
8
|
*
|
|
9
|
-
* @param Component
|
|
9
|
+
* @param Component The Preact component to mount.
|
|
10
10
|
* @returns A promise which gets resolved if the component calls `ready()`.
|
|
11
11
|
*/
|
|
12
12
|
export function mount(Component) {
|
|
@@ -27,7 +27,7 @@ export function bootstrap(Component) {
|
|
|
27
27
|
/**
|
|
28
28
|
* A HOC which passes the Appsemble block values to he wrapped Preact component.
|
|
29
29
|
*
|
|
30
|
-
* @param Component
|
|
30
|
+
* @param Component The Preact componen to wrap.
|
|
31
31
|
* @returns The wrapper component.
|
|
32
32
|
* @deprecated Use `useBlock()` instead.
|
|
33
33
|
*/
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.tsx"],"names":[],"mappings":";AAAA,OAAO,EAA6B,SAAS,IAAI,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACtF,OAAO,EAAiC,aAAa,EAAE,MAAM,EAAS,MAAM,QAAQ,CAAC;AACrF,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAS1C,MAAM,OAAO,GAAG,aAAa,CAAa,IAAI,CAAC,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,UAAU,KAAK,CACnB,SAAoC;IAEpC,OAAO,CAAC,MAAM,EAAE,EAAE,CAChB,IAAI,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;QACpB,mEAAmE;QACnE,MAAM,KAAK,GAAG;YACZ,GAAG,MAAM;YACT,KAAK;SACN,CAAC;QACF,MAAM,SAAS,GAAG,CAChB,KAAC,OAAO,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,YAC5B,KAAC,SAAS,OAAK,KAAK,GAAI,GACP,CACpB,CAAC;QACF,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;QACrC,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;IACjE,CAAC,CAAC,CAAC;AACP,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,SAAoC;IAC5D,YAAY,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;AACjC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CACvB,SAAuD;IAEvD,OAAO,CAAC,KAAQ,EAAE,EAAE,CAAC,CACnB,KAAC,OAAO,CAAC,QAAQ,cAAE,CAAC,MAAM,EAAE,EAAE,CAAC,KAAC,SAAS,OAAK,KAAK,KAAM,MAAM,GAAI,GAAoB,CACxF,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,QAAQ;IACtB,OAAO,UAAU,CAAC,OAAO,CAAC,CAAC;AAC7B,CAAC;AAqBD;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAC9B,KAEsC;IAEtC,MAAM,EAAE,KAAK,EAAE,GAAG,QAAQ,EAAE,CAAC;IAE7B,mFAAmF;IACnF,OAAO,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,MAAM,CAA4B,CAAC;AAChF,CAAC"}
|
package/index.tsx
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { BootstrapParams, Messages, bootstrap as sdkBootstrap } from '@appsemble/sdk';
|
|
2
|
+
import { ComponentChild, ComponentType, createContext, render, VNode } from 'preact';
|
|
3
|
+
import { useContext } from 'preact/hooks';
|
|
4
|
+
|
|
5
|
+
export interface BlockProps extends BootstrapParams {
|
|
6
|
+
/**
|
|
7
|
+
* A function that must be called to indicate the block is ready to be rendered.
|
|
8
|
+
*/
|
|
9
|
+
ready: () => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const Context = createContext<BlockProps>(null);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Mount a Preact component returned by a bootstrap function in the shadow DOM of a block.
|
|
16
|
+
*
|
|
17
|
+
* @param Component The Preact component to mount.
|
|
18
|
+
* @returns A promise which gets resolved if the component calls `ready()`.
|
|
19
|
+
*/
|
|
20
|
+
export function mount(
|
|
21
|
+
Component: ComponentType<BlockProps>,
|
|
22
|
+
): (params: BootstrapParams) => Promise<void> {
|
|
23
|
+
return (params) =>
|
|
24
|
+
new Promise((ready) => {
|
|
25
|
+
// eslint-disable-next-line react/jsx-no-constructed-context-values
|
|
26
|
+
const props = {
|
|
27
|
+
...params,
|
|
28
|
+
ready,
|
|
29
|
+
};
|
|
30
|
+
const component = (
|
|
31
|
+
<Context.Provider value={props}>
|
|
32
|
+
<Component {...props} />
|
|
33
|
+
</Context.Provider>
|
|
34
|
+
);
|
|
35
|
+
render(component, params.shadowRoot);
|
|
36
|
+
params.utils.addCleanup(() => render(null, params.shadowRoot));
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function bootstrap(Component: ComponentType<BlockProps>): void {
|
|
41
|
+
sdkBootstrap(mount(Component));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A HOC which passes the Appsemble block values to he wrapped Preact component.
|
|
46
|
+
*
|
|
47
|
+
* @param Component The Preact componen to wrap.
|
|
48
|
+
* @returns The wrapper component.
|
|
49
|
+
* @deprecated Use `useBlock()` instead.
|
|
50
|
+
*/
|
|
51
|
+
export function withBlock<P extends {}>(
|
|
52
|
+
Component: ComponentType<Omit<BlockProps, keyof P> & P>,
|
|
53
|
+
): ComponentType<P> {
|
|
54
|
+
return (props: P) => (
|
|
55
|
+
<Context.Consumer>{(values) => <Component {...props} {...values} />}</Context.Consumer>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function useBlock(): BlockProps {
|
|
60
|
+
return useContext(Context);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface FormattedMessagePropsWithoutValues<M extends keyof Messages> {
|
|
64
|
+
/**
|
|
65
|
+
* The ID of the message to render.
|
|
66
|
+
*/
|
|
67
|
+
id: M;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface FormattedMessagePropsWithValues<M extends keyof Messages> {
|
|
71
|
+
/**
|
|
72
|
+
* The ID of the message to render.
|
|
73
|
+
*/
|
|
74
|
+
id: M;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Values to pass to the formatted message.
|
|
78
|
+
*/
|
|
79
|
+
values: Messages[M] extends never ? undefined : Messages[M];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Render an Appsemble message.
|
|
84
|
+
*/
|
|
85
|
+
export function FormattedMessage<M extends keyof Messages>(
|
|
86
|
+
props: Messages[M] extends never
|
|
87
|
+
? FormattedMessagePropsWithoutValues<M>
|
|
88
|
+
: FormattedMessagePropsWithValues<M>,
|
|
89
|
+
): VNode {
|
|
90
|
+
const { utils } = useBlock();
|
|
91
|
+
|
|
92
|
+
// @ts-expect-error The messages interface isn’t implemented within the Preact SDK.
|
|
93
|
+
return utils.formatMessage(props.id, props.values) as ComponentChild as VNode;
|
|
94
|
+
}
|
package/jest.config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../../config/jest/config')(__dirname);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appsemble/preact",
|
|
3
|
-
"version": "0.20.
|
|
3
|
+
"version": "0.20.11",
|
|
4
4
|
"description": "Build your own blocks using Preact",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"app",
|
|
@@ -24,16 +24,17 @@
|
|
|
24
24
|
"license": "LGPL-3.0-only",
|
|
25
25
|
"author": "Appsemble <info@appsemble.com> (https://appsemble.com)",
|
|
26
26
|
"sideEffects": false,
|
|
27
|
-
"main": "dist",
|
|
28
27
|
"files": [
|
|
29
|
-
"
|
|
28
|
+
"**/*.d.ts",
|
|
29
|
+
"**/*.js",
|
|
30
|
+
"!*.config.js"
|
|
30
31
|
],
|
|
31
32
|
"scripts": {
|
|
32
|
-
"prepack": "tsc
|
|
33
|
+
"prepack": "tsc --noEmit false",
|
|
33
34
|
"test": "jest"
|
|
34
35
|
},
|
|
35
36
|
"peerDependencies": {
|
|
36
|
-
"@appsemble/sdk": "0.20.
|
|
37
|
+
"@appsemble/sdk": "0.20.11",
|
|
37
38
|
"preact": "^10"
|
|
38
39
|
}
|
|
39
40
|
}
|
package/tsconfig.json
ADDED
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":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