-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathjsx-basic.js
More file actions
53 lines (45 loc) · 1.39 KB
/
Copy pathjsx-basic.js
File metadata and controls
53 lines (45 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env node
/**
* Basic JSX rendering example
*
* This example demonstrates how to use cli-html as a library
* to render React/JSX to the terminal via `renderJSX`.
*
* Note: renderJSX is async because react/react-dom are loaded lazily.
* This file uses React.createElement so it runs without a JSX build step;
* with a bundler/babel you can write actual JSX (see bin: `jsx <file.jsx>`).
*/
import { createElement } from 'react';
import { renderJSX } from '../../index.js';
const List = () => createElement(
'ul',
null,
createElement('li', null, 'one'),
createElement('li', null, 'two'),
createElement('li', null, 'three'),
);
const main = async () => {
// Example 1: A simple element tree
console.log('=== Example 1: Element tree ===\n');
const tree = createElement(
'div',
null,
createElement('h1', null, 'Welcome to cli-html'),
createElement(
'p',
null,
'Render ',
createElement('strong', null, 'JSX'),
' straight to the terminal.',
),
);
console.log(await renderJSX(tree));
// Example 2: A function component
console.log('\n=== Example 2: Component ===\n');
console.log(await renderJSX(List));
// Example 3: With a custom theme
console.log('\n=== Example 3: Custom theme ===\n');
const heading = createElement('h1', null, 'Themed heading');
console.log(await renderJSX(heading, { h1: 'magenta bold' }));
};
main();