@appsemble/preact
TypeScript icon, indicating that this package has built-in type declarations

0.28.3 • Public • Published

Appsemble Preact SDK

Build your own blocks using Preact

npm GitLab CI Prettier

Table of Contents

Installation

npm install @appsemble/preact preact

Usage

This package integrates Preact with @appsemble/sdk. Please read the documentation for a better understanding of how to use it.

bootstrap()

The bootstrap function takes a Preact component as an argument and renders. It takes the Appsemble block context as its props, extended with the ready() function. The ready() function needs to be called when the block is done rendering. Actions won’t be finalized for any blocks on the page, until all blocks are ready.

import { bootstrap } from '@appsemble/preact';
import { useEffect } from 'preact/hooks';

bootstrap(({ actions }) => {
  useEffect(() => {
    ready();
  }, []);

  return (
    <button onClick={() => actions.onClick()} type="button">
      Hello world!
    </button>
  );
});

Note: The script will be loaded only once. The component is then bootstrapped for every instance of your block type that is loaded by the app creator.

useBlock()

Get the block context. This is available for components that are rendered within a subtree of a component rendered by bootstrap

import { useBlock } from '@appsemble/preact';
import { type VNode } from 'preact';

export function MyButton(): VNode {
  const { actions } = useBlock();

  return (
    <button onClick={() => actions.onClick()} type="button">
      Hello world!
    </button>
  );
}

<FormattedMessage />

This helper component renders a translated message ID.

import { FormattedMessage } from '@appsemble/preact';
import { type VNode } from 'preact';

interface MyButtonProps {
  readonly name: string;
}

export function MyButton({ name }: MyButtonProps): VNode {
  return (
    <button type="button">
      <FormattedMessage id="myButtonLabel" values={{ name }} />
    </button>
  );
}

License

LGPL-3.0-only © Appsemble

Package Sidebar

Install

npm i @appsemble/preact

Weekly Downloads

225

Version

0.28.3

License

LGPL-3.0-only

Unpacked Size

14.7 kB

Total Files

5

Last publish

Collaborators

  • ekhorn
  • remcohaszing
  • kvdb
  • appsemble-bot