-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
736 lines (648 loc) · 27.2 KB
/
index.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>smart-component</title>
<link rel="apple-touch-icon" sizes="180x180" href="./resources/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="./resources/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="./resources/favicon-16x16.png">
<link rel="manifest" href="./resources/site.webmanifest">
<link href="https://fonts.googleapis.com/css?family=Maven+Pro:400,700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/themes/prism-okaidia.min.css">
<link rel="stylesheet" href="./style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/components/prism-javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.16.0/plugins/normalize-whitespace/prism-normalize-whitespace.min.js"></script>
</head>
<body>
<div class="s-header">
<svg class="s-header__logo" viewBox="0 0 514 514" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M246.15 208.03a8.63 8.63 0 0 0-7.38-2.27l-52.38 8.76a8.57 8.57 0 0 0-4.81 2.55L27.7 378.25a8.63 8.63 0 0 0 .29 12.2l46.06 43.96a8.6 8.6 0 0 0 12.19-.28l153.87-161.18 6.04-64.92zm0 0a8.58 8.58 0 0 1 2.6 7.26m-2.6-7.26l2.6 7.26m0 0l-6.31 52.73m6.3-52.73l-6.3 52.73m0 0a8.65 8.65 0 0 1-2.33 4.93l2.33-4.93z" fill="#CCD1D9" stroke="#434A54" stroke-width="4"/><path d="M182.43 268.22a8.63 8.63 0 1 1-8.82-8.43 8.62 8.62 0 0 1 8.82 8.43zM174.71 307.07a8.63 8.63 0 1 1-.4-17.25 8.63 8.63 0 0 1 .4 17.25zM153.09 298.95a8.62 8.62 0 1 1-17.24.39 8.62 8.62 0 1 1 17.24-.39zm-19.24.44zM153.79 329a8.62 8.62 0 1 1-8.83-8.43 8.64 8.64 0 0 1 8.83 8.42zM123.74 329.7a8.62 8.62 0 1 1-17.24.4 8.63 8.63 0 1 1 17.24-.4zm-19.24.45zM124.44 359.73a8.62 8.62 0 1 1-8.82-8.41 8.61 8.61 0 0 1 8.82 8.41z" fill="#AAB2BC" stroke="#434A54" stroke-width="4"/><path d="M383.34 97.9c24.8 48.28 53.43 123.74 105.72 278.7.8 2.41.5 5.05-.81 7.23a8.56 8.56 0 0 1-6.04 4.06l-130.7 20.43h-.02a8.65 8.65 0 0 1-9.77-6.67l-15.25-69.25a8.64 8.64 0 0 1 5.05-9.8l15.05-6.38 1.8-.76-.72-1.81c-11.03-28.09-43.85-111.19-65.16-160.08-12.38-28.4-14.7-60.8-6.58-93.73 5.99-24.42 15.61-40.68 16.2-41.66v-.01a8.62 8.62 0 0 1 10.36-3.64c14.53 5.35 27.63 12.4 40.63 24.95 13.03 12.57 26.04 30.73 40.24 58.42z" fill="#E6E9ED" stroke="#434A54" stroke-width="4"/><path d="M76.39 353.98H437.6c39.92 0 72.4 32.48 72.4 72.38s-32.47 72.38-72.4 72.38H76.4C36.47 498.74 4 466.27 4 426.36c0-39.9 32.47-72.38 72.39-72.38z" fill="#ED5564" stroke="#434A54" stroke-width="4"/><path opacity=".1" d="M437.6 490.11H76.4c-39.24 0-71.46-30.52-74.2-69.07-.13 1.76-.19 3.53-.19 5.32 0 41.01 33.37 74.38 74.39 74.38H437.6c41.03 0 74.4-33.37 74.4-74.38 0-1.79-.06-3.56-.19-5.32-2.74 38.55-34.95 69.07-74.2 69.07z" fill="#080808"/><path d="M437.7 435.07h-42.66a8.62 8.62 0 1 1 0-17.25h42.66a8.62 8.62 0 1 1 0 17.25z" fill="#CCD1D9" stroke="#434A54" stroke-width="4"/><path d="M416.37 456.4a8.62 8.62 0 0 1-8.63-8.62v-42.66a8.62 8.62 0 1 1 17.25 0v42.66a8.63 8.63 0 0 1-8.62 8.63z" fill="#E6E9ED" stroke="#434A54" stroke-width="4"/><path d="M93.1 420.26a8.64 8.64 0 0 1-12.2 12.2 8.64 8.64 0 0 1 12.2-12.2z" fill="#434A54" stroke="#434A54" stroke-width="4"/><path d="M410.29 235.64l-28.75-74.75v-.01a8.62 8.62 0 1 1 16.1-6.18l28.74 74.75a8.62 8.62 0 1 1-16.1 6.2z" fill="#CCD1D9" stroke="#434A54" stroke-width="4"/></svg>
<div class="s-header__text">smart-component</div>
</div>
<div class="s-container">
<div class="s-content">
<h2>What is smart-component?</h2>
<p>Smart-component is a simple extension of the base HTMLElement class. It helps developers to easily create feature rich web components.</p>
<p>Features:</p>
<ul>
<li>Unified attribute and property handling</li>
<li>State value transforming</li>
<li>Auto-bind property to state</li>
<li>Communication between parent and child components</li>
<li>Lighterhtml based templating with multi-template system</li>
<li>Local and global state management</li>
<li>Extra callbacks to never miss a moment</li>
<li>And much more...</li>
</ul>
</div>
</div>
<div class="s-docs">
<h2 class="s-docs__title">Documentation</h2>
<h3 class="s-docs__title">Attributes, properties and basic templating</h3>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">Add <b>observedAttributes</b> getter to the component to listen on a list of attributes or properties.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
static get observedAttributes() {
return ['value'];
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">Every component has built in state management and you can easily make a connection between properties and state by defining <b>boundProperties</b> getter.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
static get boundProperties() {
return ['value'];
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">Templating has never been easier than it is now with smart-component; just define a <b>template</b> getter on the component.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
get template() {
return html => html`
The value is: <b>${component._state.get('value')}</b>
`;
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">The component can add classes to itself with the <b>className</b> option. The parameter is a string and multiple classes can be append with a space between classes.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
super.init({
className: 'my-example-1 my-example-1--dark'
});
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<div class="s-example">
<h3>Test the final component</h3>
<div>
<my-example-1 value="0"></my-example-1>
</div>
<button id="example-1-button-property">Set random value on component</button>
<script>
document.querySelector('#example-1-button-property').addEventListener('click', () => {
document.querySelector('my-example-1').value = Math.random();
});
</script>
</div>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
class MyExample1 extends SmartComponent {
static get observedAttributes() {
return ['value'];
}
static get boundProperties() {
return ['value'];
}
get template() {
return html => html`
The value is: <b>${this._state.get('value')}</b>
`;
}
}
</code></pre>
</div>
</div>
</div>
<div class="s-docs">
<h3 class="s-docs__title">Advanced templating</h3>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">As you could see above a <b>simple template</b> looks like this.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
get template() {
return html => html`<div>Hello World!</div>`;
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text"><b>Multiple templates</b> could be defined by using an object in the template getter. You can set various options here.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
get template() {
return [
{
name: 'hello',
markup: html => html`<div>Hello</div>`,
container: this._templater.parseHTML('<div class="hello-container"></div>'),
autoAppendContainer: true
},
{
name: 'world',
markup: html => html`<div>World!</div>`,
container: this._templater.parseHTML('<div class="world-container"></div>'),
autoAppendContainer: true
}
];
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">Templates can easily connect to eachother using the <b>_templater</b>'s render method.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
get template() {
return [
{
name: 'hello',
markup: html => html`<div>Hello ${this._templater.render('world')}!</div>`,
container: this.constructor._parseHTML('<div class="hello-container"></div>'),
autoAppendContainer: true
},
{
name: 'world',
markup: html => html`<strong>World</strong>`
}
];
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">By default, templates are append to the component. It can be changed to <b>prepend</b> instead.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
get template() {
return [
{
name: 'hello',
markup: html => html`<div>Hello</div>`,
container: this._templater.parseHTML('<div class="hello-container"></div>'),
autoAppendContainer: true,
prepend: true
}
];
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">Another interesting and useful part of the templating system is the ability to <b>define templates in the DOM</b>. State variables can be used as well. In this case, be sure to <b>define a container</b> too.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-html">
<my-component>
<template>Hello ${value}!</template>
</my-component>
</code></pre>
<pre><code class="language-javascript">
get template() {
return [
{
name: 'hello',
markup: this.querySelector('template'),
container: this._templater.parseHTML('<div></div>'),
autoAppendContainer: true
}
];
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">Markup option can be a <b>selector</b> too. This way your components can use the same template defined outside.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-html">
<template id="my-template">Hello ${value}!</template>
<my-component></my-component>
</code></pre>
<pre><code class="language-javascript">
get template() {
return [
{
name: 'hello',
markup: '#my-template'
}
];
}
</code></pre>
</div>
</div>
</div>
<div class="s-docs">
<h3 class="s-docs__title">Communication between parent and child</h3>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">First of all, in the parent component's constructor you have to set <b>listenChildren</b> option to <i>true</i>. It will prepares the parent to be able to receive events from children.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
super.init({
listenChildren: true
});
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">You have to do almost the same thing with the child component, except the option called <b>notifyParent</b>. This will make the component send events on <i>connect, disconnect and state changes</i>.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
super.init({
notifyParent: true
});
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">In the parent component, a new callback can be used for handling the events, <b>childrenChangedCallback</b>. This callback gives back the whole list of connected children. What you doing with this data is up to you. In this example we will use it to store the child's value from it's state.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
childrenChangedCallback(collection) {
const childrenList = collection.get();
const items = childrenList.map(child => {
this._state.set('childValue', child.state.value);
});
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">A parent can have multiple different kind of children, and because of that the <b>collection</b> can give back them separately, if you want. Only thing you have to do is defining a <b>first parameter</b> with a tag name.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
const childrenList = collection.get('my-example-2-child');
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<div class="s-example">
<h3>Test the final component</h3>
<div>
<my-example-2>
<template>Value of the child component is: <b>${childValue}</b></template>
<my-example-2-child value="0"></my-example-2-child>
</my-example-2>
</div>
<button id="example-2-button-property">Set random value on child</button>
<script>
document.querySelector('#example-2-button-property').addEventListener('click', () => {
document.querySelector('my-example-2-child').value = Math.random();
});
</script>
</div>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
// Parent definition
class MyExample2 extends SmartComponent {
init() {
super.init({ listenChildren: true });
}
get template() {
return [{
name: 'main',
markup: html => html`
Value of the child component is:
<b>${this._state.get('childValue')}</b>
`,
container: this._templater.parseHTML(`
<div class="my-example-2-value"></div>
`),
autoAppendContainer: true
}
childrenChangedCallback(collection) {
const childrenList = collection.get();
const items = childrenList.map(child => {
this._state.set('childValue', child.state.value);
});
}
}
/*------------------------------------------------------------------*/
// Child definition
class MyExample2Child extends SmartComponent {
init() {
super.init({ notifyParent: true });
}
static get observedAttributes() {
return ['value'];
}
static get boundProperties() {
return ['value'];
}
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">If you don't want to notify the parent on every state change you can define it in the <b>boundProperties</b>. It's useful when the parent sets this value on the component, so it won't report back the change.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
static get boundProperties() {
return [
{
name: 'value',
options: {
triggerCallback: false
}
}
];
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">Another interesting option in the <b>boundProperties</b> is that you can tell the component to store the value with a different name in state.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
static get boundProperties() {
return [
{
name: 'value',
as: 'stateValue'
}
];
}
</code></pre>
</div>
</div>
</div>
<div class="s-docs">
<h3 class="s-docs__title">Event handling</h3>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">Event handling is always something, what causing problems and makes our code much complex. With smart-component, this problem becomes much less painful. You can define an <b>eventHandlers</b> getter.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
static get eventHandlers() {
return {
':click': '_onClick'
}
}
_onClick(event) {
// Do something here...
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<div class="s-example">
<h3>Test the final component</h3>
<div>
<my-example-3></my-example-3>
</div>
</div>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
import SmartComponent from '../../libs/smartcomponent';
export default class MyExample3 extends SmartComponent {
static get defaultState() {
return { clicks: 0 };
}
static get eventHandlers() {
return { ':click': '_onClick' }
}
get template() {
return html => html`
<button>Clicked <b>${this._state.get('clicks')}</b> times.</button>
`;
}
_onClick() {
this._state.set('clicks', value => ++value);
}
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">Event handlers can be created through templates too using <b>data-handler</b> attribute and the event binding to the context.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
static get eventHandlers() {
return {
'button:click': '_onButtonClick'
}
}
get template() {
return html => html`
<button data-handler="button" onclick="${this}" >Click Me!</button>
`;
}
_onButtonClick(event) {
// Do something here...
}
</code></pre>
</div>
</div>
</div>
<div class="s-docs">
<h3 class="s-docs__title">State mangement</h3>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">Every component has it's own <b>state</b>.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
this._state.set('value', 1); // Sets value on local state
this._state.get('value'); // Gets value from local state
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">You can pass <b>options to the state</b> on set.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
this._state.set('value', 1, {
triggerCallback: false, // Won't call the attached callbacks
triggerRender: false // Won't call render function
});
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">A function can be also passed as value. Perfect for incrementing/decrementing values.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
this._state.set('value', value => value + 2);
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">With the awesome <b>subscription</b> system, you will always know, what's happening.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
this._state.subscribe('value', console.log);
this._state.set('value', 1); // Logs '1, "value"'
// Defining multiple at once with an array
this._state.subscribe(['firstName', 'lastName'], console.log);
this._state.set('firstName', 1); // Logs '1, "value"'
this._state.set('lastName', 2); // Logs '2, lastName"'
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">Every subscription comes with an <b>unsubscribe</b> method.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
const subscription = this._state.subscribe('value', console.log);
this._state.set('value', 1); // Logs '1, "value"'
subscription.unsubscribe();
this._state.set('value', 1); // Logs nothing
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">It has the ability to <b>nest values</b>.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
this._state.set('group.value', 1);
this._state.get('group.value'); // Gets 1
this._state.get('group'); // Gets { value: 1 }
this._state.get(); // Ultimately gets { group: { value: 1 } }
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">State values can be formatted. Basically, there is two ways of defining it. You can use the <b>stateOptions</b> getter or calling the <b>setOptions()</b> function on state.<br>Transforming has some predefined options, these are <i>number, integer, float, boolean, json</i>. When using one of these, <b>transformFunction</b> won't be called.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
static get stateOptions() {
return {
value: {
type: 'custom',
transformFunction: value => Number(value).toFixed(1)
}
}
}
/* OR */
this._state.setOptions('value', {
type: 'custom',
transformFunction: value => Number(value).toFixed(1)
});
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">If you defined a default value for the state variable, you can access it with calling <b>getDefaultValue()</b> function.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
this._state.setOptions('value', {
defaultValue: 1
});
...
this._state.getDefaultValue('value'); // Gets 1
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">State options can be defined through a static method too, called <b>stateOptions</b>.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
static get stateOptions() {
return {
value: {
defaultValue: 'Default Value'
}
};
}
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text"><b>Global state</b> is available in every instance and work exactly like the default component state.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
this._globalState.set('value', 1); // Sets 1
this._globalState.get('value'); // Gets 1
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text"><b>Prefixed global state</b> is useful, if you want to render only when a subset of state changed.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
init() {
super.init({
watchGlobalState: 'subset' // or an array ['subset1, subset2']
});
}
...
this._globalState.set('subset.value'); // Triggers rendering in all component uses this prefix
</code></pre>
</div>
</div>
<div class="s-docs__block">
<div class="s-docs__left">
<p class="s-docs__text">Global state couldn't be true global, if you can't access it from anywhere. The whole object is available on the <b>window</b>.</p>
</div>
<div class="s-docs__right">
<pre><code class="language-javascript">
window.smartComponent.globalState.value = 5; // Calls rendering of course
window.smartComponent.globalState.value; // 5
</code></pre>
</div>
</div>
</div>
<script src="https://polyfill.io/v3/polyfill.min.js?features=Symbol%2CArray.prototype.find"></script>
<script src="./bundle.js"></script>
</body>
</html>