This repository was archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposi-without-jsx.html
337 lines (291 loc) · 20.3 KB
/
composi-without-jsx.html
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="description" content="Composi is a JavaScript library for creating component-based interfaces. It uses the virtual dom to make efficient updates to the DOM based on a component's data or state.">
<meta name="keywords" content="javascript, framework, performance, small, fast, UI, programming, code, component, composi, chocolatechipui, chocolatechip-ui, reactive, virtual dom">
<title>Composi - Tutorials</title>
<link rel="stylesheet" href="../css/styles.css">
<link rel="stylesheet" href="../css/styles.css">
<link rel="stylesheet" href="../css/prism-tomorrow.css">
</head>
<body class='tutorial-page'>
<nav>
<ul class='nav--menu'>
<li class='nav--menu__item'>
<a class='nav--menu__item__link' href="../index.html">
<svg id='composi-logo' viewBox="0 0 300 300" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Composi Logo</title>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Composi-Logo-Solid" fill="#fff">
<path d="M1.77635684e-15,0 L95,0 L95,38 L209,38 L209,0 L300,0 L300,94 L265,93.8571663 L265,209 L300,209 L300,300 L209,300 L209,265 L95,265 L95,300 L1.77635684e-15,300 L1.77635684e-15,209 L40,209 L40,94 L1.77635684e-15,93.8571663 L1.77635684e-15,0 Z M107,107 L107,192 L192,192 L192,107 L107,107 Z" id="Combined-Shape"></path>
</g>
</g>
</svg>
<span class='logo__link--text'>Composi</span></a>
</li>
<li class='nav--menu__item'>
<a class='nav--menu__item__link' href="../docs/installation.html">Docs</a>
</li>
<li class='nav--menu__item selected'>
<a class='nav--menu__item__link' href="./index.html">Tutorials</a>
</li>
<li class='nav--menu__item'>
<a class='nav--menu__item__link external' target='__blank' href="https://github.com/composor/awesome-composi">Resources
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="14px" height="14px" viewBox="0 0 511.626 511.627" style="enable-background:new 0 0 511.626 511.627;"
xml:space="preserve">
<path fill='#fff' d="M392.857,292.354h-18.274c-2.669,0-4.859,0.855-6.563,2.573c-1.718,1.708-2.573,3.897-2.573,6.563v91.361
c0,12.563-4.47,23.315-13.415,32.262c-8.945,8.945-19.701,13.414-32.264,13.414H82.224c-12.562,0-23.317-4.469-32.264-13.414
c-8.945-8.946-13.417-19.698-13.417-32.262V155.31c0-12.562,4.471-23.313,13.417-32.259c8.947-8.947,19.702-13.418,32.264-13.418
h200.994c2.669,0,4.859-0.859,6.57-2.57c1.711-1.713,2.566-3.9,2.566-6.567V82.221c0-2.662-0.855-4.853-2.566-6.563
c-1.711-1.713-3.901-2.568-6.57-2.568H82.224c-22.648,0-42.016,8.042-58.102,24.125C8.042,113.297,0,132.665,0,155.313v237.542
c0,22.647,8.042,42.018,24.123,58.095c16.086,16.084,35.454,24.13,58.102,24.13h237.543c22.647,0,42.017-8.046,58.101-24.13
c16.085-16.077,24.127-35.447,24.127-58.095v-91.358c0-2.669-0.856-4.859-2.574-6.57
C397.709,293.209,395.519,292.354,392.857,292.354z"/>
<path fill='#fff' d="M506.199,41.971c-3.617-3.617-7.905-5.424-12.85-5.424H347.171c-4.948,0-9.233,1.807-12.847,5.424
c-3.617,3.615-5.428,7.898-5.428,12.847s1.811,9.233,5.428,12.85l50.247,50.248L198.424,304.067
c-1.906,1.903-2.856,4.093-2.856,6.563c0,2.479,0.953,4.668,2.856,6.571l32.548,32.544c1.903,1.903,4.093,2.852,6.567,2.852
s4.665-0.948,6.567-2.852l186.148-186.148l50.251,50.248c3.614,3.617,7.898,5.426,12.847,5.426s9.233-1.809,12.851-5.426
c3.617-3.616,5.424-7.898,5.424-12.847V54.818C511.626,49.866,509.813,45.586,506.199,41.971z"/>
</svg></a>
</li>
</ul>
</nav>
<article class='tutorial__article'>
<section>
<div class='tutorial'>
<h1>Composi Without JSX</h1>
<p class="tutorial__intro">Although we love JSX because of its brevity and clarity, some people really dislike it.</p>
<p>You can use Composi without JSX. You have two options: the Composi <a target='__blank' href='../docs/h.html'>h</a> function or <a href='https://www.npmjs.com/package/hyperx-es6' target='__blank'>Hyperx-es6</a>.</p>
<h2>H</h2>
<p>The <code>h</code> function lets you use hyperscript syntax to designate the HTML you want to create. When working with Composi, you will always be importing the <code>h</code> function into your files to enable Composi's virtual dom. A Composi project is automatically configured to let <a target="__blank" href="http://babeljs.io">Babel</a> know know to use it when dealing with JSX. So, whether you use JSX or <code>h</code>, both will result in the creation of the same virtual node. Take the following for example:</p>
<p data-height="300" data-theme-id="6688" data-slug-hash="pdJzJa" data-default-tab="js,result" data-user="rbiggs" data-embed-version="2" data-pen-title="Composi Tuts - Hyperx-1" class="codepen">See the Pen <a href="https://codepen.io/rbiggs/pen/pdJzJa/">Composi Tuts - Hyperx-1</a> by Robert Biggs (<a href="https://codepen.io/rbiggs">@rbiggs</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
<p>Now we can create the same result using <code>h</code>:</p>
<p data-height="300" data-theme-id="6688" data-slug-hash="VrLZvw" data-default-tab="js,result" data-user="rbiggs" data-embed-version="2" data-pen-title="Composi Tuts - Hyperx-2" class="codepen">See the Pen <a href="https://codepen.io/rbiggs/pen/VrLZvw/">Composi Tuts - Hyperx-2</a> by Robert Biggs (<a href="https://codepen.io/rbiggs">@rbiggs</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
<p>Both of these produce the same virtual node. As you can see, the <code>h</code> code looks quite similar to the virtual node that it creates:</p>
<pre><code class="language-javascript">// The virtual node created is:
{
type: "h2",
props: {
class: "title",
title: "Hello, Ellen!"
},
children: ["Hello, ", "Ellen", "!"]
}</code></pre>
<p> </p>
<h2>Subcomponents With H</h2>
<p>You can also create subcomponents when using <code>h</code>. You write a function that returns the required hyperscript. Then you enclude it as an interpolation in the parent. Because this is hyperscript syntax, you'll need to use <code>${}</code> for interpolation:</p>
<p data-height="300" data-theme-id="6688" data-slug-hash="ooXvLv" data-default-tab="js,result" data-user="rbiggs" data-embed-version="2" data-pen-title="Composi Tuts - Hyperx-3" class="codepen">See the Pen <a href="https://codepen.io/rbiggs/pen/ooXvLv/">Composi Tuts - Hyperx-3</a> by Robert Biggs (<a href="https://codepen.io/rbiggs">@rbiggs</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
<p>If you prefer functional over classes, you will probably likeusing the <code>h</code> funtion.</p>
<h2>taggers</h2>
<p>Typing out tags with the <code>h</code> function can get verbose. We have created a hyperscript compatible NPM module called <code>h-html</code>. What this does is provide a more compact function named after each tag you want to create. You therefore only need to provide props and children.</p>
<h3>Installing:</h3>
<p> </p>
<pre><code class="language-bash">npm i -D taggers</code></pre>
<p> </p>
<p>Example usage:</p>
<pre><code class="language-javascript">import {h, mount} from 'composi'
import {taggers} from 'taggers'
const {nav, h1} = taggers(h)
// Define tags:
function title(message) {
return nav(
{},
h1(
{},
'Hello, ${message}!'
)
)
}
// Mount the component:
mount(title('World'), 'header')</code></pre>
<p>And here's a Codepen example:</p>
<p data-height="300" data-theme-id="6688" data-slug-hash="opWjZQ" data-default-tab="js,result" data-user="rbiggs" data-embed-version="2" data-pen-title="Composi with taggers-1" class="codepen">See the Pen <a href="https://codepen.io/rbiggs/pen/opWjZQ/">Composi with taggers-1</a> by Robert Biggs (<a href="https://codepen.io/rbiggs">@rbiggs</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
<p> </p>
<h2>h-html</h2>
<p>Like Taggers, there is also <code>h-html</code>, a module that allows you to use tag shortcuts. It's nearly double the size of <code>taggers</code>, but easier to use. No need to pass in the <code>h</code>, function, etc.</p>
<h3>Installing:</h3>
<p> </p>
<pre><code class="language-bash">npm i -D h-html</code></pre>
<p>After installing, you can import it into your project. There are two ways you can do so, importing indidual tags, or importing them all on a alias.</p>
<pre><code class="language-javascript">
import {h, mount} from 'composi'
import {nav, h1} from 'h-html'
// Define tags:
function title(message) {
return nav(
{},
h1(
{},
'Hello, ${message}!'
)
)
}
// Mount the tags:
mount(title('World'), 'header')</code></pre>
<p>And here's a Codepen example:</p>
<p data-height="400" data-theme-id="6688" data-slug-hash="xPJmoz" data-default-tab="js,result" data-user="rbiggs" data-embed-version="2" data-pen-title="Composi with h-html-1" class="codepen">See the Pen <a href="https://codepen.io/rbiggs/pen/xPJmoz/">Composi with h-html-1</a> by Robert Biggs (<a href="https://codepen.io/rbiggs">@rbiggs</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
<p>Here's an example importing all tags as an alias:</p>
<pre><code class="language-javascript">import {h, mount} from 'composi'
import * as t from 'h-html'
// Define tags:
function title(message) {
return t.nav(
{},
t.h1(
{},
'Hello, ${message}!'
)
)
}
// Mount the title:
mount(title('World'), 'header')
// Define list:
function list(data) {
// Define list Item:
function listItems(data) {
return data.map(item => t.li({}, `Item ${item}`))
}
return t.ul(
{class: 'list'},
listItems(data)
)
}
// Mount the list:
mount(list(['One', 'Two', 'Three']), 'section')</code></pre>
<p>It's totally up to you which way you want to import <code>h-html</code>. However, if you know you are going to use a lot of tags, it makes sense to import them on an alias.</p>
<p>And here's a Codepen example of tags being aliased:</p>
<p data-height="400" data-theme-id="6688" data-slug-hash="NwBoGr" data-default-tab="js,result" data-user="rbiggs" data-embed-version="2" data-pen-title="Composi with h-html-2" class="codepen">See the Pen <a href="https://codepen.io/rbiggs/pen/NwBoGr/">Composi with h-html-2</a> by Robert Biggs (<a href="https://codepen.io/rbiggs">@rbiggs</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
<p> </p>
<h2>Hyperx-es6</h2>
<p>Another alternative to JSX is <a target='__blank' href='https://www.npmjs.com/package/hyperx'>Hyperx</a>. The original version is in CommonJS format and cannot be easily imported into a Composi project. Therefore we've create a clone called <a href="https://www.npmjs.com/package/hyperx-es6">Hyperx-es6</a>. The only difference is that Hyperx-es6 gets exported as an ES6 module. This means you can use normal ES6 module import syntax and Rollup and bundle it properly. Hyperx-es6 allows you to ES6 template literal instead of JSX. The results are the same. The biggest difference in use will be in the text editor. JSX with get proper syntax highlighting, whereas Hyperx-es6 template literals will be treated as strings.</p>
<h2>Hyperx-es6 Converts Template Literals into Hyperscript</h2>
<p>Hyperx-es6 converts template literals into the same format as JSX and <code>h</code>. In this case, since we tell Hyperx-es6 to use the Composi <code>h</code> function, the result will be a virtual node that Composi can use.</p>
<h2>Installation and Use</h2>
<p>You'll need to install Hyperx-es6 in your project and import it into any file where you want to use it. You'll have to make an update to your project's <code>gulpfile.js</code> to know to include Hyperx-es6 when it bundles all the dependencies. And you'll also need to let Babel know to use Hyperx-es6 instead of Composi's <code>h</code> function for creating virtual nodes.</p>
<pre><code class="language-bash"># cd to your project and run:
npm i -D hyperx-es6</code></pre>
<p>In your project's <code>app.js</code> file add:</p>
<pre><code class="language-javascript">import {h, Component} from 'composi'
import {hyperx} from 'hyperx-es6'
// Tell Hyperx-es6 to use the Composi h function,
// by passing it to Hyperx-es6:
const html = hyperx(h)</code>]</pre>
<p>With this in your file, you can start using Hyperx-es6. You could make functional components like with JSX, or use it inside of a class component. Below are some functional components with composition. We need to use <code>${}</code> for interpolation. Note that when using Hyperx, you'll be using the tagged template literal <code>html</code> that you designated after importing Hyperx-es6. <strong>Note:</strong> in all the following Codepen examples, we're using the UMD version of Hyperx-es6 that we can load in the browser and access as a global variable.</p>
<p data-height="300" data-theme-id="6688" data-slug-hash="ooXvLv" data-default-tab="js,result" data-user="rbiggs" data-embed-version="2" data-pen-title="Composi Tuts - Hyperx-3" class="codepen">See the Pen <a href="https://codepen.io/rbiggs/pen/ooXvLv/">Composi Tuts - Hyperx-3</a> by Robert Biggs (<a href="https://codepen.io/rbiggs">@rbiggs</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
<p> </p>
<h2>Using Hyperx-es6 with a Class Component</h2>
<p>You can also use Hyperx-es6 with class-based components. In the example below we have subcomponents using Hyperx-es6, <code>listItem</code> and <code>disclosure</code>. We integrate them into the parent component <code>List</code>, which is an extension of Component. Because the subcomponents are functions, we need to invoke them with interpolation, so they get enclosed with <code>${}</code>. Our <code>List</code> component also implements events. You can click on a list item to get an alert and you can add a new list item as well.</p>
<p data-height="450" data-theme-id="6688" data-slug-hash="gXpYmg" data-default-tab="js,result" data-user="rbiggs" data-embed-version="2" data-pen-title="Composi Tuts - Hyperx-4" class="codepen">See the Pen <a href="https://codepen.io/rbiggs/pen/gXpYmg/">Composi Tuts - Hyperx-4</a> by Robert Biggs (<a href="https://codepen.io/rbiggs">@rbiggs</a>) on <a href="https://codepen.io">CodePen</a>.</p>
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
<h2>Load From a CDN</h2>
<p>You can also load Hyperx-es6 from a CDN:</p>
<pre><code class="language-html"><script src='https://unpkg.com/composi@0.9.1/dist/composi.js'></script>
<script src='https://unpkg.com/hyperx-es6@2.3.2/dist/hyperx-es6.js'></script></code></pre>
<p>When you load Hyperx-es6 from a CDN, you can access it like this:</p>
<pre><code class="language-javascript">const { h, render, Component } = composi
// When using CDN version, notice capitalized: Hyperx.
// ES6 import is lowercase: import {hyper} from 'hyper-es6'
const {hyperx} = Hyperx
// Tell Hyperx-es6 to use the Composi h function,
// by passing it to hyperx:
const html = hyperx(h)
// Start using html`` to define component markup.</code></pre>
<p> </p>
</div>
<aside>
<menu>
<ul class="tutorials__menu">
<li class="tutorials__menu__item">
<a href='./index.html'>Hello World</a>
</li>
<li class="tutorials__menu__item">
<a href='./introducing-jsx.html'>Introducing JSX</a>
</li>
<li class="tutorials__menu__item">
<a href='./rendering-elements.html'>Rendering Elements</a>
</li>
<li class="tutorials__menu__item">
<a href='./components-n-props.html'>Components and Props</a>
</li>
<li class="tutorials__menu__item">
<a href='./state-n-lifecycle.html'>State and Lifecycle</a>
</li>
<li class="tutorials__menu__item">
<a href='./conditional-rendering.html'>Conditional Rendering</a>
</li>
<li class="tutorials__menu__item">
<a href='./handling-events.html'>Handling Events</a>
</li>
<li class="tutorials__menu__item">
<a href='./lists-n-keys.html'>Lists and Keys</a>
</li>
<li class="tutorials__menu__item">
<a href='./forms.html'>Forms</a>
</li>
<li class="tutorials__menu__item">
<a href='./lifting-state-up.html'>Lifting State Up</a>
</li>
<li class="tutorials__menu__item">
<a href='./thinking-in-composi.html'>Thinking in Composi</a>
</li>
<li class="tutorials__menu__item">
<a href='./jsx-in-depth.html'>JSX In Depth</a>
</li>
<li class="tutorials__menu__item">
<a href='./advanced-state-management.html'>Advanced State Management</a>
</li>
<li class="tutorials__menu__item">
<a href='./composi-datastore.html'>Composi DataStore</a>
</li>
<li class="tutorials__menu__item">
<a href='./integrating-other-libs.html'>Integrating with Other Libraries</a>
</li>
<li class="tutorials__menu__item selected">
<a href='./composi-without-jsx.html'>Composi Without JSX</a>
</li>
<li class="tutorials__menu__item">
<a href='./immutable-data.html'>Immutable Data</a>
</li>
</ul>
</menu>
</aside>
</section>
</article>
<footer>
<section>
<svg id='composi-logo-footer' viewBox="0 0 300 300" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Composi Logo</title>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Composi-Logo-Solid" fill="rgba(255,255,255,0.5)">
<path d="M1.77635684e-15,0 L95,0 L95,38 L209,38 L209,0 L300,0 L300,94 L265,93.8571663 L265,209 L300,209 L300,300 L209,300 L209,265 L95,265 L95,300 L1.77635684e-15,300 L1.77635684e-15,209 L40,209 L40,94 L1.77635684e-15,93.8571663 L1.77635684e-15,0 Z M107,107 L107,192 L192,192 L192,107 L107,107 Z" id="Combined-Shape"></path>
</g>
</g>
</svg>
<h3>Composi is open source (MIT) and available on <a href='https://github.com/composor/composi' target='__blank'>Github</a> and <a href="https://www.npmjs.com/package/composi" target='__blank'>NPM</a>.</h3>
</section>
</footer>
<script src="../js/prism.min.js"></script>
<script src="../js/prism-jsx.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-115293685-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-115293685-1');
</script>
</body>
</html>