Skip to content

Commit 3b4b7e6

Browse files
authored
Add TSX documentation (#64)
1 parent b680ad9 commit 3b4b7e6

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

docs/advanced/language-extensions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { SideBySide } from "@site/src/components/SideBySide";
66

77
TypeScriptToLua provides several extensions to the TypeScript language in the form of types and helper functions. To use these language extensions, add the types to your `tsconfig.json`:
88

9-
```json
9+
```json title=tsconfig.json
1010
{
1111
"compilerOptions": {
1212
...

docs/tsx.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
title: TSX
3+
---
4+
5+
As of version `0.42.0`, TypeScriptToLua supports the use of TSX. To enable it, add `"jsx": "react"` to your tsconfig - other values are not supported. It is your job to make sure the required factory functions are available in the Lua environment.
6+
7+
```json title=tsconfig.json
8+
{
9+
"compilerOptions": {
10+
...
11+
"jsx": "react",
12+
...
13+
},
14+
}
15+
```
16+
17+
TSX will be translated to lua as it would be translated to JS:
18+
19+
```typescript
20+
const element = <div a={b}>Inner text!</div>;
21+
```
22+
23+
Will become:
24+
25+
```lua
26+
local element = React.createElement("div", {a = b}, "Inner text!");
27+
```
28+
29+
## Custom factory functions
30+
31+
It is possible to supply custom factory functions using the `jsxFactory` tsconfig setting or `/** @jsx */` annotation.
32+
33+
### jsxFactory tsconfig example
34+
35+
```json title=tsconfig.json
36+
{
37+
"compilerOptions": {
38+
...
39+
"jsx": "react",
40+
"jsxFactory": "MyNamespace.MyCreate"
41+
...
42+
},
43+
}
44+
```
45+
46+
```typescript
47+
const element = <div a={b}>Inner text!</div>;
48+
```
49+
50+
Will translate to:
51+
52+
```lua
53+
local element = MyNamespace.MyCreate("div", {a = b}, "Inner text!");
54+
```
55+
56+
### @jsx annotation example
57+
58+
Note: the annotation MUST be at the top of the file!
59+
60+
```typescript
61+
/** @jsx MyNamespace.MyCreate */
62+
const element = <div a={b}>Inner text!</div>;
63+
```
64+
65+
Will translate to:
66+
67+
```lua
68+
local element = MyNamespace.MyCreate("div", {a = b}, "Inner text!");
69+
```
70+
71+
## jsxFragmentFactory
72+
73+
You can also provide a customfragment using the `jsxFragmentFactory` tsconfig setting or `/** @jsxFrag */` annotation.
74+
75+
### jsxFragmentFactory tsconfig example
76+
77+
```json title=tsconfig.json
78+
{
79+
"compilerOptions": {
80+
...
81+
"jsx": "react",
82+
"jsxFactory": "MyNamespace.MyCreate",
83+
"jsxFragmentFactory": "MyNamespace.MyFragment"
84+
...
85+
},
86+
}
87+
```
88+
89+
```typescript
90+
const element = <></>;
91+
```
92+
93+
Will translate to:
94+
95+
```lua
96+
local element = MyNamespace.MyCreate(MyNamespace.MyFragment, {});
97+
```
98+
99+
### @jsxFrag annotation example
100+
101+
Note: the annotation MUST be at the top of the file!
102+
103+
```typescript
104+
/** @jsx MyNamespace.MyCreate */
105+
/** @jsxFrag MyNamespace.MyCreate */
106+
const element = <></>;
107+
```
108+
109+
Will translate to:
110+
111+
```lua
112+
local element = MyNamespace.MyCreate(MyNamespace.MyFragment, {});
113+
```

sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"advanced/writing-declarations",
1111
"advanced/compiler-annotations",
1212
"advanced/language-extensions",
13+
"tsx",
1314
{
1415
"type": "category",
1516
"label": "API",

0 commit comments

Comments
 (0)