|
| 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 | +``` |
0 commit comments