JSX
Formatter supports JSX markup. it uses preact by default.
Hot to use JSX
- install
@tlgr/fmtmodule. - install preact.
- modify
tsconfig.jsonfile (if neccesary)
In tsconfig.json file add next fields:
tsconfig.json
{
"compilerOptions": {
// rest options
"jsx": "react",
"jsxFactory": "h"
}
}
Usage
decide which replying context you would like to use. This tutorial shows working with
context.replyfunction.import Function
example.ts
import { h } from "preact";
import { render, Bold } from "@tlgr/fmt/react";
// rest code
- use JSX for building reply text
example.ts
import { h, Fragment } from "preact";
import { render, Bold, Break, Italic } from "@tlgr/fmt/react";
import { Telegraf } from "telegraf";
const bot = new Telegraf("<API TOKEN>");
bot.start((ctx) => {
ctx.reply(
...render(
<Fragment>
<Bold>This is bold text</Bold>
<Break />
<Italic>This is italic text</Italic>
</Fragment>
)
);
});
bot.launch();
tip
Better to wrap high-level component with Fragment component from preact library.
Limitations & Pitfalls
- Not use built-in componenets like
a,buttonetc. Since telegram has a lot of limitations. - Use components only for your reply context. For
replyuse components fromreactfolder. - Each reply context should be call
renderfunction. As for previous limitationrenderfunction only for your using context. renderfunction indefaultfunction should be called withrest/spreadoperator, sincereplyfunction accepts text modifications(like bold, underline, etc.)- not all components can be combined with children(e.g.
BoldandItaliccan be combined) - do not crate component with many hierarchies. Better to use 1 hierary without depths.