mantle-lit 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +753 -740
- package/dist/index.cjs +450 -214
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +49 -176
- package/dist/index.d.ts +49 -176
- package/dist/index.js +445 -214
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,740 +1,753 @@
|
|
|
1
|
-
# Mantle Lit
|
|
2
|
-
|
|
3
|
-
A lightweight library for building
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install mantle-lit lit mobx
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
Requires Lit 3+ and MobX 6+.
|
|
12
|
-
|
|
13
|
-
## Basic Example
|
|
14
|
-
|
|
15
|
-
```ts
|
|
16
|
-
import { View, createView } from 'mantle-lit';
|
|
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
|
-
```json
|
|
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
|
-
this.input =
|
|
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
|
-
this.
|
|
593
|
-
this.
|
|
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
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
1
|
+
# Mantle Lit
|
|
2
|
+
|
|
3
|
+
A lightweight library for building web components with MobX reactivity and lit-html templating. Automatic observable state, computed getters, and bound actions.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install mantle-lit lit mobx
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires Lit 3+ and MobX 6+.
|
|
12
|
+
|
|
13
|
+
## Basic Example
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { View, createView, property, html } from 'mantle-lit';
|
|
17
|
+
|
|
18
|
+
class CounterView extends View {
|
|
19
|
+
// Props
|
|
20
|
+
@property() initialCount = 0;
|
|
21
|
+
|
|
22
|
+
// Internal state - auto-observable
|
|
23
|
+
count = 0;
|
|
24
|
+
|
|
25
|
+
onCreate() {
|
|
26
|
+
this.count = this.initialCount;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
increment() {
|
|
30
|
+
this.count++;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
render() {
|
|
34
|
+
return html`
|
|
35
|
+
<button @click=${this.increment}>
|
|
36
|
+
Count: ${this.count}
|
|
37
|
+
</button>
|
|
38
|
+
`;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const Counter = createView(CounterView, { tag: 'x-counter' });
|
|
43
|
+
|
|
44
|
+
// Register type for IDE autocomplete in templates
|
|
45
|
+
declare global {
|
|
46
|
+
interface HTMLElementTagNameMap {
|
|
47
|
+
'x-counter': CounterView;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
**Usage in HTML (property binding with `.`):**
|
|
53
|
+
```html
|
|
54
|
+
<x-counter .initialCount=${5}></x-counter>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**Everything is reactive by default.** Internal state becomes observable, getters become computed, and methods become auto-bound actions. Props use the `@property()` decorator for IDE autocomplete.
|
|
58
|
+
|
|
59
|
+
## Defining Props
|
|
60
|
+
|
|
61
|
+
Use `@property()` for props:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { View, createView, property, html } from 'mantle-lit';
|
|
65
|
+
|
|
66
|
+
interface TodoItem {
|
|
67
|
+
id: number;
|
|
68
|
+
text: string;
|
|
69
|
+
done: boolean;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
class TodoView extends View {
|
|
73
|
+
@property() title = '';
|
|
74
|
+
@property() initialTodos: TodoItem[] = [];
|
|
75
|
+
@property() onComplete?: (count: number) => void;
|
|
76
|
+
|
|
77
|
+
// Internal state (auto-observable, no decorator needed)
|
|
78
|
+
todos: TodoItem[] = [];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const Todo = createView(TodoView, { tag: 'x-todo' });
|
|
82
|
+
|
|
83
|
+
declare global {
|
|
84
|
+
interface HTMLElementTagNameMap {
|
|
85
|
+
'x-todo': TodoView;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Use property binding (`.prop=${value}`) to pass props in templates.
|
|
91
|
+
|
|
92
|
+
**No props?** Just extend `View` directly without any `@property()` decorators.
|
|
93
|
+
|
|
94
|
+
## Scoped Styles
|
|
95
|
+
|
|
96
|
+
Use `static styles` for component-scoped CSS:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { View, createView, html, css } from 'mantle-lit';
|
|
100
|
+
|
|
101
|
+
class MyView extends View {
|
|
102
|
+
static styles = css`
|
|
103
|
+
:host {
|
|
104
|
+
display: block;
|
|
105
|
+
padding: 1rem;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
button {
|
|
109
|
+
background: #6366f1;
|
|
110
|
+
color: white;
|
|
111
|
+
}
|
|
112
|
+
`;
|
|
113
|
+
|
|
114
|
+
render() {
|
|
115
|
+
return html`<button>Click me</button>`;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
For larger components, extract styles to a separate file:
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
// MyView.styles.ts
|
|
124
|
+
import { css } from 'mantle-lit';
|
|
125
|
+
export const styles = css`...`;
|
|
126
|
+
|
|
127
|
+
// MyView.ts
|
|
128
|
+
import { styles } from './MyView.styles';
|
|
129
|
+
|
|
130
|
+
class MyView extends View {
|
|
131
|
+
static styles = styles;
|
|
132
|
+
// ...
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## What You Get
|
|
137
|
+
|
|
138
|
+
**Direct mutation:**
|
|
139
|
+
```ts
|
|
140
|
+
this.items.push(item); // not [...items, item]
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
**Computed values via getters:**
|
|
144
|
+
```ts
|
|
145
|
+
get completed() { // automatically memoized
|
|
146
|
+
return this.items.filter(i => i.done);
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**Stable methods (auto-bound):**
|
|
151
|
+
```ts
|
|
152
|
+
toggle(id: number) { // automatically bound to this
|
|
153
|
+
const item = this.items.find(i => i.id === id);
|
|
154
|
+
if (item) item.done = !item.done;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// use directly, no wrapper needed
|
|
158
|
+
render() {
|
|
159
|
+
return html`<button @click=${this.toggle}>Toggle</button>`;
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
**React to changes explicitly:**
|
|
164
|
+
```ts
|
|
165
|
+
onCreate() {
|
|
166
|
+
this.watch(
|
|
167
|
+
() => this.filter,
|
|
168
|
+
(filter) => this.applyFilter(filter)
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Lifecycle
|
|
174
|
+
|
|
175
|
+
| Method | When |
|
|
176
|
+
|--------|------|
|
|
177
|
+
| `onCreate()` | Instance created, props available |
|
|
178
|
+
| `onMount()` | Component connected to DOM. Return a cleanup function (optional). |
|
|
179
|
+
| `onUnmount()` | Component disconnected from DOM. Called after cleanups (optional). |
|
|
180
|
+
| `render()` | On mount and updates. Return Lit `TemplateResult`. |
|
|
181
|
+
|
|
182
|
+
### Watching State
|
|
183
|
+
|
|
184
|
+
Use `this.watch` to react to state changes. Watchers are automatically disposed on unmount.
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
this.watch(
|
|
188
|
+
() => expr, // reactive expression (getter)
|
|
189
|
+
(value, prev) => {}, // callback when expression result changes
|
|
190
|
+
options? // optional: { delay, fireImmediately }
|
|
191
|
+
)
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
**Options:**
|
|
195
|
+
|
|
196
|
+
| Option | Type | Default | Description |
|
|
197
|
+
|--------|------|---------|-------------|
|
|
198
|
+
| `delay` | `number` | — | Debounce the callback by N milliseconds |
|
|
199
|
+
| `fireImmediately` | `boolean` | `false` | Run callback immediately with current value |
|
|
200
|
+
|
|
201
|
+
**Basic example:**
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
class SearchView extends View {
|
|
205
|
+
@property() placeholder = '';
|
|
206
|
+
|
|
207
|
+
query = '';
|
|
208
|
+
results: string[] = [];
|
|
209
|
+
|
|
210
|
+
onCreate() {
|
|
211
|
+
this.watch(
|
|
212
|
+
() => this.query,
|
|
213
|
+
async (query) => {
|
|
214
|
+
if (query.length > 2) {
|
|
215
|
+
this.results = await searchApi(query);
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
{ delay: 300 }
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
**Multiple watchers:**
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
onCreate() {
|
|
228
|
+
this.watch(() => this.filter, (filter) => this.applyFilter(filter));
|
|
229
|
+
this.watch(() => this.sort, (sort) => this.applySort(sort));
|
|
230
|
+
this.watch(() => this.page, (page) => this.fetchPage(page));
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
**Early disposal:**
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
onCreate() {
|
|
238
|
+
const stop = this.watch(() => this.token, (token) => {
|
|
239
|
+
this.authenticate(token);
|
|
240
|
+
stop(); // only needed once
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
`this.watch` wraps MobX's `reaction` with automatic lifecycle disposal. For advanced MobX patterns (`autorun`, `when`, custom schedulers), use `reaction` directly and return a dispose function from `onMount`.
|
|
246
|
+
|
|
247
|
+
## Mounting Components
|
|
248
|
+
|
|
249
|
+
Use the `mount` helper to imperatively create and mount components:
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
import { mount } from 'mantle-lit';
|
|
253
|
+
import './MyComponent';
|
|
254
|
+
|
|
255
|
+
// Mount with props
|
|
256
|
+
mount('x-my-component', {
|
|
257
|
+
title: 'Hello',
|
|
258
|
+
items: [1, 2, 3],
|
|
259
|
+
onSelect: (item) => console.log(item),
|
|
260
|
+
}, document.body);
|
|
261
|
+
|
|
262
|
+
// Returns the created element
|
|
263
|
+
const el = mount('x-counter', { initialCount: 5 }, container);
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## IDE Autocomplete
|
|
267
|
+
|
|
268
|
+
For IDE autocomplete in Lit templates, add `HTMLElementTagNameMap` declarations:
|
|
269
|
+
|
|
270
|
+
```ts
|
|
271
|
+
declare global {
|
|
272
|
+
interface HTMLElementTagNameMap {
|
|
273
|
+
'x-my-component': MyComponentView;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
Install the [lit-plugin](https://marketplace.visualstudio.com/items?itemName=runem.lit-plugin) VS Code extension for template type checking.
|
|
279
|
+
|
|
280
|
+
**CLI validation** (works reliably):
|
|
281
|
+
```bash
|
|
282
|
+
npx lit-analyzer "src/**/*.ts" --strict
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Add to your `package.json`:
|
|
286
|
+
```json
|
|
287
|
+
{
|
|
288
|
+
"scripts": {
|
|
289
|
+
"lint:lit": "lit-analyzer \"src/**/*.ts\" --strict"
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## TypeScript Configuration
|
|
295
|
+
|
|
296
|
+
Enable experimental decorators:
|
|
297
|
+
|
|
298
|
+
```json
|
|
299
|
+
{
|
|
300
|
+
"compilerOptions": {
|
|
301
|
+
"experimentalDecorators": true
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## Patterns
|
|
307
|
+
|
|
308
|
+
### Combined (default)
|
|
309
|
+
|
|
310
|
+
State, logic, and template in one class with a `render()` method:
|
|
311
|
+
|
|
312
|
+
```ts
|
|
313
|
+
import { View, createView, property, html } from 'mantle-lit';
|
|
314
|
+
|
|
315
|
+
interface TodoItem {
|
|
316
|
+
id: number;
|
|
317
|
+
text: string;
|
|
318
|
+
done: boolean;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
class TodoView extends View {
|
|
322
|
+
@property() initialTodos: TodoItem[] = [];
|
|
323
|
+
|
|
324
|
+
todos: TodoItem[] = [];
|
|
325
|
+
input = '';
|
|
326
|
+
|
|
327
|
+
onCreate() {
|
|
328
|
+
this.todos = this.initialTodos;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
add() {
|
|
332
|
+
this.todos.push({ id: Date.now(), text: this.input, done: false });
|
|
333
|
+
this.input = '';
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
setInput(e: Event) {
|
|
337
|
+
this.input = (e.target as HTMLInputElement).value;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
render() {
|
|
341
|
+
return html`
|
|
342
|
+
<div>
|
|
343
|
+
<input .value=${this.input} @input=${this.setInput} />
|
|
344
|
+
<button @click=${this.add}>Add</button>
|
|
345
|
+
<ul>${this.todos.map(t => html`<li>${t.text}</li>`)}</ul>
|
|
346
|
+
</div>
|
|
347
|
+
`;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export const Todo = createView(TodoView, { tag: 'x-todo' });
|
|
352
|
+
|
|
353
|
+
declare global {
|
|
354
|
+
interface HTMLElementTagNameMap {
|
|
355
|
+
'x-todo': TodoView;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### Separated
|
|
361
|
+
|
|
362
|
+
ViewModel (state/logic) and template as separate concerns. Pass the template to `createView`:
|
|
363
|
+
|
|
364
|
+
```ts
|
|
365
|
+
import { ViewModel, createView, property, html, css } from 'mantle-lit';
|
|
366
|
+
|
|
367
|
+
interface TodoItem {
|
|
368
|
+
id: number;
|
|
369
|
+
text: string;
|
|
370
|
+
done: boolean;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// ViewModel: pure state and logic (no render method)
|
|
374
|
+
class TodoVM extends ViewModel {
|
|
375
|
+
@property() initialTodos: TodoItem[] = [];
|
|
376
|
+
|
|
377
|
+
todos: TodoItem[] = [];
|
|
378
|
+
input = '';
|
|
379
|
+
|
|
380
|
+
onCreate() {
|
|
381
|
+
this.todos = this.initialTodos;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
add() {
|
|
385
|
+
this.todos.push({ id: Date.now(), text: this.input, done: false });
|
|
386
|
+
this.input = '';
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
setInput(e: Event) {
|
|
390
|
+
this.input = (e.target as HTMLInputElement).value;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Template: pure presentation
|
|
395
|
+
const template = (vm: TodoVM) => html`
|
|
396
|
+
<div>
|
|
397
|
+
<input .value=${vm.input} @input=${vm.setInput} />
|
|
398
|
+
<button @click=${vm.add}>Add</button>
|
|
399
|
+
<ul>${vm.todos.map(t => html`<li>${t.text}</li>`)}</ul>
|
|
400
|
+
</div>
|
|
401
|
+
`;
|
|
402
|
+
|
|
403
|
+
const styles = css`
|
|
404
|
+
button { background: #6366f1; color: white; }
|
|
405
|
+
`;
|
|
406
|
+
|
|
407
|
+
// createView wires them together
|
|
408
|
+
export const Todo = createView(TodoVM, {
|
|
409
|
+
tag: 'x-todo',
|
|
410
|
+
template,
|
|
411
|
+
styles,
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
declare global {
|
|
415
|
+
interface HTMLElementTagNameMap {
|
|
416
|
+
'x-todo': TodoVM;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
**Benefits of separation:**
|
|
422
|
+
- **Testable**: ViewModel is pure JS, unit test without DOM
|
|
423
|
+
- **Portable**: Same ViewModel could render to React, Vue, etc.
|
|
424
|
+
- **Cleaner**: State logic separate from presentation
|
|
425
|
+
|
|
426
|
+
## Decorators
|
|
427
|
+
|
|
428
|
+
For teams that prefer explicit annotations over auto-observable, Mantle provides its own decorators. These are lightweight metadata collectors. No `accessor` keyword required.
|
|
429
|
+
|
|
430
|
+
```ts
|
|
431
|
+
import { View, createView, property, observable, action, computed, html } from 'mantle-lit';
|
|
432
|
+
|
|
433
|
+
class TodoView extends View {
|
|
434
|
+
@property() title = '';
|
|
435
|
+
|
|
436
|
+
@observable todos: TodoItem[] = [];
|
|
437
|
+
@observable input = '';
|
|
438
|
+
|
|
439
|
+
@computed get remaining() {
|
|
440
|
+
return this.todos.filter(t => !t.done).length;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
@action add() {
|
|
444
|
+
this.todos.push({ id: Date.now(), text: this.input, done: false });
|
|
445
|
+
this.input = '';
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
render() {
|
|
449
|
+
return html`<!-- ... -->`;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
export const Todo = createView(TodoView, { tag: 'x-todo' });
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
**Key differences from auto-observable mode:**
|
|
457
|
+
- Only decorated fields are reactive (undecorated fields are inert)
|
|
458
|
+
- Methods are still auto-bound for stable `this` references
|
|
459
|
+
|
|
460
|
+
### Available Decorators
|
|
461
|
+
|
|
462
|
+
| Decorator | Purpose |
|
|
463
|
+
|-----------|---------|
|
|
464
|
+
| `@observable` | Deep observable field |
|
|
465
|
+
| `@observable.ref` | Reference-only observation |
|
|
466
|
+
| `@observable.shallow` | Shallow observation (add/remove only) |
|
|
467
|
+
| `@observable.struct` | Structural equality comparison |
|
|
468
|
+
| `@action` | Action method (auto-bound) |
|
|
469
|
+
| `@computed` | Computed getter (optional; getters are computed by default) |
|
|
470
|
+
|
|
471
|
+
### MobX Decorators (Legacy)
|
|
472
|
+
|
|
473
|
+
If you prefer using MobX's own decorators (requires `accessor` keyword for TC39):
|
|
474
|
+
|
|
475
|
+
```ts
|
|
476
|
+
import { observable, action } from 'mobx';
|
|
477
|
+
import { configure } from 'mantle-lit';
|
|
478
|
+
|
|
479
|
+
// Disable auto-observable globally
|
|
480
|
+
configure({ autoObservable: false });
|
|
481
|
+
|
|
482
|
+
class TodoView extends View {
|
|
483
|
+
@observable accessor todos: TodoItem[] = []; // note: accessor required
|
|
484
|
+
@action add() { /* ... */ }
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export const Todo = createView(TodoView, { tag: 'x-todo' });
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
## Error Handling
|
|
491
|
+
|
|
492
|
+
Render errors propagate to the browser as usual. Lifecycle errors (`onMount`, `onUnmount`, `watch`) in both Views and Behaviors are caught and routed through a configurable handler.
|
|
493
|
+
|
|
494
|
+
By default, errors are logged to `console.error`. Configure a global handler to integrate with your error reporting:
|
|
495
|
+
|
|
496
|
+
```ts
|
|
497
|
+
import { configure } from 'mantle-lit';
|
|
498
|
+
|
|
499
|
+
configure({
|
|
500
|
+
onError: (error, context) => {
|
|
501
|
+
// context.phase: 'onCreate' | 'onMount' | 'onUnmount' | 'watch'
|
|
502
|
+
// context.name: class name of the View or Behavior
|
|
503
|
+
// context.isBehavior: true if the error came from a Behavior
|
|
504
|
+
Sentry.captureException(error, {
|
|
505
|
+
tags: { phase: context.phase, component: context.name },
|
|
506
|
+
});
|
|
507
|
+
},
|
|
508
|
+
});
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
Behavior errors are isolated. A failing Behavior won't prevent sibling Behaviors or the parent View from mounting.
|
|
512
|
+
|
|
513
|
+
## Behaviors (Experimental)
|
|
514
|
+
|
|
515
|
+
> ⚠️ **Experimental:** The Behaviors API is still evolving and may change in future releases.
|
|
516
|
+
|
|
517
|
+
Behaviors are reusable pieces of state and logic that can be shared across views. Define them as classes, wrap with `createBehavior()`, and use the resulting factory function in your Views.
|
|
518
|
+
|
|
519
|
+
### Defining a Behavior
|
|
520
|
+
|
|
521
|
+
```ts
|
|
522
|
+
import { Behavior, createBehavior } from 'mantle-lit';
|
|
523
|
+
|
|
524
|
+
class WindowSizeBehavior extends Behavior {
|
|
525
|
+
width = window.innerWidth;
|
|
526
|
+
height = window.innerHeight;
|
|
527
|
+
breakpoint!: number;
|
|
528
|
+
|
|
529
|
+
onCreate(breakpoint = 768) {
|
|
530
|
+
this.breakpoint = breakpoint;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
get isMobile() {
|
|
534
|
+
return this.width < this.breakpoint;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
handleResize() {
|
|
538
|
+
this.width = window.innerWidth;
|
|
539
|
+
this.height = window.innerHeight;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
onMount() {
|
|
543
|
+
window.addEventListener('resize', this.handleResize);
|
|
544
|
+
return () => window.removeEventListener('resize', this.handleResize);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
export const withWindowSize = createBehavior(WindowSizeBehavior);
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
The naming convention:
|
|
552
|
+
- **Class**: PascalCase (`WindowSizeBehavior`)
|
|
553
|
+
- **Factory**: camelCase with `with` prefix (`withWindowSize`)
|
|
554
|
+
|
|
555
|
+
### Using Behaviors
|
|
556
|
+
|
|
557
|
+
Call the factory function (no `new` keyword) in your View. The `with` prefix signals that the View manages this behavior's lifecycle:
|
|
558
|
+
|
|
559
|
+
```ts
|
|
560
|
+
import { View, createView, html } from 'mantle-lit';
|
|
561
|
+
import { withWindowSize } from './withWindowSize';
|
|
562
|
+
|
|
563
|
+
class ResponsiveView extends View {
|
|
564
|
+
windowSize = withWindowSize(768);
|
|
565
|
+
|
|
566
|
+
render() {
|
|
567
|
+
return html`
|
|
568
|
+
<div>
|
|
569
|
+
${this.windowSize.isMobile
|
|
570
|
+
? html`<mobile-layout></mobile-layout>`
|
|
571
|
+
: html`<desktop-layout></desktop-layout>`}
|
|
572
|
+
<p>Window: ${this.windowSize.width}x${this.windowSize.height}</p>
|
|
573
|
+
</div>
|
|
574
|
+
`;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
export const Responsive = createView(ResponsiveView, { tag: 'x-responsive' });
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
### Watching in Behaviors
|
|
582
|
+
|
|
583
|
+
Behaviors can use `this.watch` just like Views:
|
|
584
|
+
|
|
585
|
+
```ts
|
|
586
|
+
class FetchBehavior extends Behavior {
|
|
587
|
+
url!: string;
|
|
588
|
+
data: any[] = [];
|
|
589
|
+
loading = false;
|
|
590
|
+
|
|
591
|
+
onCreate(url: string) {
|
|
592
|
+
this.url = url;
|
|
593
|
+
this.watch(() => this.url, () => this.fetchData(), { fireImmediately: true });
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
async fetchData() {
|
|
597
|
+
this.loading = true;
|
|
598
|
+
this.data = await fetch(this.url).then(r => r.json());
|
|
599
|
+
this.loading = false;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
export const withFetch = createBehavior(FetchBehavior);
|
|
604
|
+
```
|
|
605
|
+
|
|
606
|
+
### Multiple Behaviors
|
|
607
|
+
|
|
608
|
+
Behaviors compose naturally:
|
|
609
|
+
|
|
610
|
+
```ts
|
|
611
|
+
import { View, createView, html } from 'mantle-lit';
|
|
612
|
+
import { withFetch } from './FetchBehavior';
|
|
613
|
+
import { withWindowSize } from './WindowSizeBehavior';
|
|
614
|
+
|
|
615
|
+
class DashboardView extends View {
|
|
616
|
+
users = withFetch('/api/users');
|
|
617
|
+
posts = withFetch('/api/posts');
|
|
618
|
+
windowSize = withWindowSize(768);
|
|
619
|
+
|
|
620
|
+
render() {
|
|
621
|
+
return html`
|
|
622
|
+
<div>
|
|
623
|
+
${this.users.loading ? 'Loading...' : `${this.users.data.length} users`}
|
|
624
|
+
${this.windowSize.isMobile ? html`<mobile-nav></mobile-nav>` : ''}
|
|
625
|
+
</div>
|
|
626
|
+
`;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
export const Dashboard = createView(DashboardView, { tag: 'x-dashboard' });
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
### Behavior Lifecycle
|
|
634
|
+
|
|
635
|
+
Behaviors support the same lifecycle methods as Views:
|
|
636
|
+
|
|
637
|
+
| Method | When |
|
|
638
|
+
|--------|------|
|
|
639
|
+
| `onCreate(...args)` | Called during construction with the factory arguments |
|
|
640
|
+
| `onMount()` | Called when parent View connects to DOM. Return cleanup (optional). |
|
|
641
|
+
| `onUnmount()` | Called when parent View disconnects from DOM. |
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
## API
|
|
645
|
+
|
|
646
|
+
### `configure(config)`
|
|
647
|
+
|
|
648
|
+
Set global defaults for all views. Settings can still be overridden per-view in `createView` options.
|
|
649
|
+
|
|
650
|
+
```ts
|
|
651
|
+
import { configure } from 'mantle-lit';
|
|
652
|
+
|
|
653
|
+
// Disable auto-observable globally (for decorator users)
|
|
654
|
+
configure({ autoObservable: false });
|
|
655
|
+
```
|
|
656
|
+
|
|
657
|
+
| Option | Default | Description |
|
|
658
|
+
|--------|---------|-------------|
|
|
659
|
+
| `autoObservable` | `true` | Whether to automatically make View instances observable |
|
|
660
|
+
| `onError` | `console.error` | Global error handler for lifecycle errors (see [Error Handling](#error-handling)) |
|
|
661
|
+
|
|
662
|
+
### `View` / `ViewModel`
|
|
663
|
+
|
|
664
|
+
Base class for view components. Pure MobX state container—`createView()` generates the HTMLElement wrapper.
|
|
665
|
+
|
|
666
|
+
`View` and `ViewModel` are aliases. Use `View` for combined pattern (with `render()`), `ViewModel` for separated pattern (with external template).
|
|
667
|
+
|
|
668
|
+
| Property/Method | Description |
|
|
669
|
+
|-----------------|-------------|
|
|
670
|
+
| `onCreate()` | Called when instance created |
|
|
671
|
+
| `onMount()` | Called when connected to DOM, return cleanup (optional) |
|
|
672
|
+
| `onUnmount()` | Called when disconnected from DOM (optional) |
|
|
673
|
+
| `render()` | Optional. Return `TemplateResult`. If omitted, pass `template` to `createView()`. |
|
|
674
|
+
| `watch(expr, callback, options?)` | Watch reactive expression, auto-disposed on unmount |
|
|
675
|
+
|
|
676
|
+
### `mount(tag, props, container)`
|
|
677
|
+
|
|
678
|
+
Imperatively create and mount a custom element:
|
|
679
|
+
|
|
680
|
+
```ts
|
|
681
|
+
import { mount } from 'mantle-lit';
|
|
682
|
+
|
|
683
|
+
const element = mount('x-my-component', { title: 'Hello' }, document.body);
|
|
684
|
+
```
|
|
685
|
+
|
|
686
|
+
| Argument | Type | Description |
|
|
687
|
+
|----------|------|-------------|
|
|
688
|
+
| `tag` | `string` | Custom element tag name |
|
|
689
|
+
| `props` | `object` | Properties to set on the element |
|
|
690
|
+
| `container` | `Element \| string` | Container element or selector |
|
|
691
|
+
|
|
692
|
+
Returns the created element.
|
|
693
|
+
|
|
694
|
+
### `Behavior`
|
|
695
|
+
|
|
696
|
+
Base class for behaviors. Extend it and wrap with `createBehavior()`.
|
|
697
|
+
|
|
698
|
+
| Method | Description |
|
|
699
|
+
|--------|-------------|
|
|
700
|
+
| `onCreate(...args)` | Called during construction with constructor args |
|
|
701
|
+
| `onMount()` | Called when parent View mounts, return cleanup (optional) |
|
|
702
|
+
| `onUnmount()` | Called when parent View unmounts |
|
|
703
|
+
| `watch(expr, callback, options?)` | Watch reactive expression, auto-disposed on unmount |
|
|
704
|
+
|
|
705
|
+
### `createBehavior(Class)`
|
|
706
|
+
|
|
707
|
+
Creates a factory function from a behavior class. Returns a callable (no `new` needed).
|
|
708
|
+
|
|
709
|
+
```ts
|
|
710
|
+
class MyBehavior extends Behavior {
|
|
711
|
+
onCreate(value: string) { /* ... */ }
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
export const withMyBehavior = createBehavior(MyBehavior);
|
|
715
|
+
|
|
716
|
+
// Usage: withMyBehavior('hello')
|
|
717
|
+
```
|
|
718
|
+
|
|
719
|
+
### `createView(ViewClass, options)`
|
|
720
|
+
|
|
721
|
+
Creates a custom element from a ViewModel class.
|
|
722
|
+
|
|
723
|
+
```ts
|
|
724
|
+
// Combined pattern (ViewModel has render method)
|
|
725
|
+
createView(MyView, { tag: 'x-my-view' })
|
|
726
|
+
|
|
727
|
+
// Separated pattern (external template)
|
|
728
|
+
createView(MyVM, {
|
|
729
|
+
tag: 'x-my-view',
|
|
730
|
+
template: (vm) => html`...`,
|
|
731
|
+
styles: css`...`,
|
|
732
|
+
})
|
|
733
|
+
```
|
|
734
|
+
|
|
735
|
+
| Option | Default | Description |
|
|
736
|
+
|--------|---------|-------------|
|
|
737
|
+
| `tag` | (required) | Custom element tag name (must contain a hyphen) |
|
|
738
|
+
| `template` | — | Template function `(vm) => TemplateResult`. Required if ViewModel has no `render()`. |
|
|
739
|
+
| `styles` | — | CSS styles (can also be defined on `ViewModel.styles`) |
|
|
740
|
+
| `autoObservable` | `true` | Make all fields observable. Set to `false` when using decorators. |
|
|
741
|
+
| `shadow` | `true` | Use Shadow DOM. Set to `false` to render in light DOM (allows external CSS). |
|
|
742
|
+
|
|
743
|
+
## Who This Is For
|
|
744
|
+
|
|
745
|
+
- Teams using MobX for state management
|
|
746
|
+
- Developers who prefer class-based components
|
|
747
|
+
- Projects building standards-compliant web components
|
|
748
|
+
- Anyone integrating vanilla JS libraries
|
|
749
|
+
- Teams wanting to share components across frameworks
|
|
750
|
+
|
|
751
|
+
## License
|
|
752
|
+
|
|
753
|
+
MIT
|