JSX
Formatter supports JSX markup. it uses preact by default.
Hot to use JSX
- install
@tlgr/fmt
module. - install preact.
- modify
tsconfig.json
file (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.reply
function.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
,button
etc. Since telegram has a lot of limitations. - Use components only for your reply context. For
reply
use components fromreact
folder. - Each reply context should be call
render
function. As for previous limitationrender
function only for your using context. render
function indefault
function should be called withrest/spread
operator, sincereply
function accepts text modifications(like bold, underline, etc.)- not all components can be combined with children(e.g.
Bold
andItalic
can be combined) - do not crate component with many hierarchies. Better to use 1 hierary without depths.