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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
docs / webui_using_lit.md [blame]
# Using Lit in Chromium WebUI Development
[TOC]
## Background
This documentation focuses on using Lit in the context of Chromium WebUI,
and on compatibility between Lit and Polymer elements, since much of Chromium
WebUI is currently written in Polymer. It assumes familiarity with the
following:
* Web Components: The basic foundation on which Lit is built. See
[Introduction to Web Components from MDN](https://developer.mozilla.org/en-US/docs/Web/API/Web_components) and the related
[MDN guide for custom elements](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements).
* Lit framework, see [official docs](https://lit.dev/docs/), and especially
the [Reactive properties, Lifecycle](https://lit.dev/docs/components/lifecycle/)
and [Expressions](https://lit.dev/docs/templates/expressions/) sections.
* Lit vs Polymer differences. See
[Lit for Polymer users](https://lit.dev/articles/lit-for-polymer-users/),
covering some general Lit/Polymer compatibility and migration issues
***promo
For developers unfamiliar with these, the external sources linked above are
recommended before continuing with this documentation.
***
## Chromium WebUI Infrastructure: CrLitElement base class
<code>[CrLitElement](https://source.chromium.org/chromium/chromium/src/+/main:third_party/lit/v3_0/cr_lit_element.ts;l=1?q=cr_lit_element&sq=&ss=chromium%2Fchromium%2Fsrc)</code> is provided as a base class for Chromium WebUI development. It contains code to
1. Reduce the amount of boilerplate code necessary for individual elements.
2. Improve compatibility with elements using Polymer (necessary in a codebase
that is using both Polymer and Lit).
3. Make Polymer -> Lit migrations easier
***promo
Lit custom elements in Chromium should inherit from the `CrLitElement` class.
***
Specific features of `CrLitElement` include:
1. Forces initial rendering to be synchronous when
1. The element’s connectedCallback runs
2. The element is focused before the connectedCallback has run
3. Child elements are accessed with `this.$` before the connectedCallback
has run
This means that there is no need to call `await element.updateComplete`
before accessing the element’s DOM in certain cases:
1. Immediately after attaching it
2. When doing something like focusing the element or accessing its children
with `element.$` in a Polymer parent’s `connectedCallback()`, which may
run before the Lit child’s `connectedCallback()`.
For more detail on this, see the inline documentation in `CrLitElement`.
2. “$” proxy to allow accessing child elements by ID with `this.$.id`, to match
the behavior of Polymer, which does the same thing.
3. Implementation of `notify: true` for properties. Setting this will cause
`CrLitElement` to fire `foo-changed` events in the `updated()` lifecycle
callback, whenever some property `foo` with `notify: true` set is changed.
This allows compatibility with Polymer parent element two-way bindings, but
is not exactly the same; see more details below.
4. Changes Lit’s default property/attribute mapping to match the mapping used
in Polymer - i.e. property `fooBar` will be mapped to attribute `foo-bar`,
not the Lit default of `foobar`
## Lit Data Bindings and handling `-changed` events
As noted above, `CrLitElement` forces an initial synchronous render in
`connectedCallback()`. This means child elements may initialize properties
with `notify: true` and then fire `-changed` events for these properties in
`updated()` as soon as they are connected, which may occur before the parent
element has finished its first update.
One consequence of this is that `-changed` event handlers cannot assume that
the element has completed its first update when the `-changed` event is
received, and should not make any changes to the element's DOM until after
waiting for the element's `updateComplete` promise. This means such handlers
must either (1) be async and `await this.updateComplete;` before running any
code that updates the element's DOM, or (2) only update properties on the
parent in response to the child's property change, and perform resulting UI
updates in the `updated()` lifecycle method instead.
Note that if the parent property being updated is protected or private, a cast
will be necessary to check for changes to the property in `changedProperties`.
This is also demonstrated in the example below.
Suppose the Lit child has a property with `notify: true` as follows:
```
static override get properties() {
return {
foo: {
type: Boolean,
notify: true,
},
};
}
```
This property is also bound to a parent element that listens for the
`-changed` event as follows:
```
<foo-child ?foo="${this.foo_}" on-foo-changed="${this.onFooChanged_}">
</foo-child>
<demo-child id="demo"></demo-child>
```
The parent TypeScript code could look like this:
```
static override get properties() {
return {
foo_: {type: Boolean},
};
}
protected foo_: boolean = true;
onFooChanged_(e: CustomEvent<{value: boolean}>) {
// Updates the parent's property that is bound to the child.
this.foo_ = e.detail.value;
}
override updated(changedProperties: PropertyValues<this>) {
super.updated(changedProperties);
// Cast necessary to check for changes to protected/private properties.
const changedPrivateProperties =
changedProperties as Map<PropertyKey, unknown>;
// Updates the DOM when |foo_| changes.
if (changedPrivateProperties.has('foo_')) {
if (this.foo_) {
this.$.demo.show();
} else {
this.$.demo.hide();
}
}
}
```
## Lit data binding issue with select elements
The `<select>` element has an ordering requirement that sometimes causes a
bug when using Lit data bindings on both the `value` property of the
`<select>` and the `value` attribute of its child `<option>` elements.
Specifically, when the `<select>`'s `value` property is set, there must
already be an existing `<option>` with that value, or the `<select>` will
be rendered as blank. If Lit bindings are used for the `<option>` values,
these values will not be populated in time, and the `<select>` will be
empty at startup. The following example would reproduce this bug and
have an empty `<select>` displayed at startup.
`.html.ts` file with `<select>` bug:
```
<select .value="${this.mySelectValue}" @change="${this.onSelectChange_}">
<option value="${MyEnum.FIRST}">Option 1</option>
<option value="${MyEnum.SECOND}">Option 2</option>
</select>
```
Corresponding `.ts`. Note that the bug manifests even though `mySelectValue`
is being initialized to a valid option.
```
static get properties() {
return {
mySelectValue: {type: String},
};
}
mySelectValue: MyEnum = MyEnum.SECOND;
onSelectChange_(e: Event) {
this.mySelectValue = (e.target as HTMLSelectElement).value;
}
```
The current recommended workaround is to instead bind to the `selected`
attribute on each `<option>`, i.e.:
`.html.ts` file:
```
<select @change="${this.onSelectChange_}">
<option value="${MyEnum.FIRST}"
?selected="${this.isSelected_(MyEnum.FIRST)}">
Option 1
</option>
<option value="${MyEnum.SECOND}"
?selected="${this.isSelected_(MyEnum.SECOND)}">
Option 2
</option>
</select>
```
Corresponding `.ts` file:
```
static get properties() {
return {
mySelectValue: {type: String},
};
}
mySelectValue: MyEnum = MyEnum.SECOND;
onSelectChange_(e: Event) {
this.mySelectValue = (e.target as HTMLSelectElement).value;
}
isSelected_(value: MyEnum): boolean {
return value === this.mySelectValue;
}
```
Note: This bug can also be worked around by using Lit's `live` directive in
the data binding and requesting an extra update any time the `<select>` is
rendered. Including `live` in the Chromium Lit bundle is still under
consideration. Reach out to the WebUI team if you have a `<select>` where the
workaround above is problematic or impractical (e.g. due to a huge list of
`<option>` elements).
## Lit and Polymer Data Bindings Compatibility
Two-way bindings are not natively supported in Lit. As mentioned above,
basic compatibility is provided by the `CrLitElement` base class’s
implementation of `notify: true`. However, these events differ from the Polymer
two-way binding behavior.
* In Polymer two-way bindings, the child element only fires a `-changed`
event if the property is modified from the child. The child element does not
fire the event if the property value is set from the parent.
* The equivalent code in `CrLitElement` can’t differentiate whether the
property was set from the parent or the child itself and always fires the
`-changed` event when the value changes.
*** promo
When migrating parent or child elements to Lit, any code directly handling
`-changed` events in the parent should either be agnostic to whether the
corresponding property was changed from the parent or the child, or should check
that the new value in the event differs from the current parent value.
***
Example: a `-changed` event handler that logs a metric indicating something was
changed from the child (e.g. due to user input) should add a check before
logging that the new value is in fact new, and was not set by the parent via the
data binding.
The behavior difference for two-way bindings can be seen by playing with this [Lit playground example](https://lit.dev/playground/#project=W3sibmFtZSI6InRvcC1lbGVtZW50LnRzIiwiY29udGVudCI6ImltcG9ydCB7UG9seW1lckVsZW1lbnQsIGh0bWx9IGZyb20gJ0Bwb2x5bWVyL3BvbHltZXInO1xuaW1wb3J0IHtjdXN0b21FbGVtZW50LCBwcm9wZXJ0eX0gZnJvbSAnQHBvbHltZXIvZGVjb3JhdG9ycyc7XG5pbXBvcnQge0NyRHVtbXlQb2x5bWVyRWxlbWVudH0gZnJvbSAnLi9jcl9kdW1teV9wb2x5bWVyLmpzJztcbmltcG9ydCB7Q3JEdW1teUxpdEVsZW1lbnR9IGZyb20gJy4vY3JfZHVtbXlfbGl0LmpzJztcbmltcG9ydCAnLi9jcl9kdW1teV9saXQuanMnO1xuaW1wb3J0ICcuL2NyX2R1bW15X3BvbHltZXIuanMnO1xuXG5AY3VzdG9tRWxlbWVudCgndG9wLWVsZW1lbnQnKVxuY2xhc3MgVG9wRWxlbWVudCBleHRlbmRzIFBvbHltZXJFbGVtZW50IHtcbiAgc3RhdGljIGdldCB0ZW1wbGF0ZSgpIHtcbiAgICByZXR1cm4gaHRtbGBcbiAgICAgIDxzdHlsZT5cbiAgICAgICAgOmhvc3Qge1xuICAgICAgICAgIGNvbG9yOiBibHVlO1xuICAgICAgICAgIGRpc3BsYXk6YmxvY2s7XG4gICAgICAgICAgYm9yZGVyOiAycHggc29saWQgYmx1ZTtcbiAgICAgICAgfVxuICAgICAgICBcbiAgICAgICAgI2NoaWxkcmVuIHtcbiAgICAgICAgICBkaXNwbGF5OiBmbGV4O1xuICAgICAgICAgIGdhcDogMTBweDtcbiAgICAgICA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ICB2YWx1ZToge3R5cGU6IE51bWJlcn0sXG4gICAgfTtcbiAgfVxuICBcbiAgb3ZlcnJpZGUgY29ubmVjdGVkQ2FsbGJhY2soKSB7XG4gICAgc3VwZXIuY29ubmVjdGVkQ2FsbGJhY2soKTtcbiAgICBcbiAgICB0aGlzLnBlcmZvcm1VcGRhdGUoKTtcbiAgfVxuIFxuICB2YWx1ZTogbnVtYmVyID0gMTtcbiAgXG4gIC8vIEltcGxlbWVudGF0aW9uIG9mIG5vdGlmeTogdHJ1ZSBmcm9tIENyTGl0RWxlbWVudFxuICBvdmVycmlkZSB1cGRhdGVkKGNoYW5nZWRQcm9wZXJ0aWVzOiBQcm9wZXJ0eVZhbHVlczx0aGlzPikge1xuICAgIGlmIChjaGFuZ2VkUHJvcGVydGllcy5oYXMoJ3ZhbHVlJykpIHtcbiAgICAgIHRoaXMuZGlzcGF0Y2hFdmVudChuZXcgQ3VzdG9tRXZlbnQoJ3ZhbHVlLWNoYW5nZWQnLCB7IGJ1YmJsZXM6IHRydWUsIGNvbXBvc2VkOiB0cnVlLCBkZXRhaWw6IHsgdmFsdWU6IHRoaXMudmFsdWUgfX0pKTtcbiAgICB9XG4gIH1cbn0ifV0).
Modifying initialization of the properties in the different parent and child
elements in this playground example reveals that there are also differences
in behavior on initialization. These differences are also documented in the
following table:
| |Property initialized in both parent and child|Property initialized in parent only|Property initialized in child only|Property uninitialized in both parent adnd child|
|---|---|---|---|---|
|**Polymer parent hosting Polymer child**|Parent value propagates to child. No `-changed` event fired.|Parent value propagates to child. No `-changed` event fired.|Child value propagates to parent. `-changed` event fired.|Property is left undefined. No events fired.|
|**Polymer parent hosting Lit child**|Parent value propagates to child. `-changed` event fired.|Parent value propagates to child. `-changed` event fired.|Child value propagates to parent. `-changed` event fired.|Property is left undefined. No events fired.|
|**Lit parent hosting Polymer child**|Parent value propagates to child. `-changed` event fired.|Parent value propagates to child. `-changed` event fired.|Child value propagates to parent if binding is using attribute syntax\*. If using property syntax\*\*, parent `undefined` value takes precedence and `-changed` event fires with `undefined`.|Property is left undefined. No events fired.|
|**Lit parent hosting Lit child**|Parent value propagates to child. `-changed` event fired.|Parent value propagates to child. `-changed` event fired.| Child value propagates to parent if binding is using attribute syntax\*. If using property syntax\*\*, parent `undefined` value takes precedence and `-changed` event fires with `undefined`.|Property is left undefined. No events fired.|
***aside
\* attribute syntax: `value="${this.childValue || nothing}"`
\*\* property syntax: `.value="${this.value}" `Note that this syntax must be used for anything that is not a boolean, string, or number.
***
## Polymer iron/paper elements alternatives
Previously, code in Chromium WebUI relied heavily on the Polymer library of
elements in addition to the Polymer framework itself. The following table
captures the list of elements that were still being used in Desktop WebUI code
when the WebUI team started exploring Lit as an alternative to Polymer, and
discusses the recommended future approach for each. Some of these
elements have subsequently been removed from Desktop (non-CrOS) builds, and more
will be removed over time.
*** note
Note: `iron-` and `paper-` elements are
[no longer recommended even for Polymer UIs.](https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/web/web.md#Polymer)
***
|POLYMER LIBRARY ELEMENT|RECOMMENDED APPROACH|
|-----------------------|--------------------|
|`iron-list`|Use `cr-infinite-list` or, if the list is not very large, use the `map()` directive. See additional detail on migrating `iron-list` clients below.|
|`iron-icon`|Use `cr-icon`.|
|`iron-collapse`|Use `cr-collapse`.|
|`paper-spinner`|Use `cr-spinner-style` CSS, or for more customization style `throbber.svg` as needed.|
|`paper-styles`|Do not use, these styles are pre-2023 refresh and have been removed on non-CrOS builds.|
|`iron-flex-layout`|Do not use, use standard CSS to style elements.|
|`iron-a11y-announcer`|Use `cr-a11y-announcer`|
|`iron-location`/`iron-query-params`|Use `CrRouter` or custom code.|
|`iron-scroll-target-behavior`|Do not use.|
|`paper-progress`|Use either `cr-progress` or the native `<progress>` element with CSS styling.|
|`iron-iconset-svg`/`iron-meta`|Use `cr-iconset` and `IconsetMap`.|
|`iron-media-query`|Do not use, use `window.matchMedia()`.|
|`iron-pages`|Do not use, replace with equivalent conditional rendering or use `cr-page-selector`.|
|`iron-scroll-threshold`|Do not use.|
|`iron-resizable-behavior`|Do not use. `ResizeObserver`s can be used to trigger changes in items that need to be modified when something is resized.|
|`paper-tooltip`|Use `cr-tooltip`.|
|`iron-a11y-keys`|Do not use.|
|`iron-selector`/`iron-selectable-behavior`|Use `CrSelectableMixin`.|
## Anatomy of a Lit-based element
In Chromium WebUI, Lit based custom elements are defined using 3 files:
1. A \*`.ts` file defining the element, which imports the template from the
\*`.html.js` file and the style from the \*`.css.js` file.
2. A \*`.css` file containing the element’s styling. This file is run through
`css_to_wrapper` at build time to create a \*`.css.js` file that the
element’s \*`.ts` file can import the styles from.
3. A \*`.html.ts` file containing the element’s HTML template. This file can be
either
* auto-generated from a checked-in `*.html` file via `html_to_wrapper`
(preferred approach for Polymer->Lit migrations), OR
* directly be checked in to the repository (preferred approach for new Lit
code, or post migration cleanups)
***note
Note: This differs from Polymer based custom elements in Chromium, which
typically use only 2 files: a `.html` file containing both the element’s
template and its styling, and a `.ts` file containing the element definition.
***
Example `.ts` file:
```
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '//resources/cr_elements/cr_input/cr_input.js';
import {CrLitElement} from '//resources/lit/v3_0/lit.rollup.js';
import {getCss} from './my_example.css.js';
import {getHtml} from './my_example.html.js';
export interface MyExampleElement {
$: {
input: HTMLElement,
};
}
export class MyExampleElement extends CrLitElement {
static get is() {
return 'my-example';
}
static override get styles() {
return getCss();
}
override render() {
return getHtml.bind(this)();
}
static override get properties() {
return {
disabled: {
type: Boolean,
reflect: true,
},
myValue: {type: String},
};
}
disabled: boolean = false;
myValue: string = 'hello world';
// Referenced from the template, so must be protected (not private).
protected onInputValueChanged_(e: CustomEvent<string>) {
this.myValue = e.detail.value;
}
}
declare global {
interface HTMLElementTagNameMap {
'my-example': MyExampleElement;
}
}
customElements.define(MyExampleElement.is, MyExampleElement);
```
Example CSS file:
```
/* Copyright 2024 The Chromium Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. */
/* #css_wrapper_metadata_start
* #type=style-lit
* #import=//resources/cr_elements/cr_shared_vars.css.js
* #scheme=relative
* #css_wrapper_metadata_end */
#input {
background-color: blue;
--cr-input-error-display: none;
}
:host([disabled]) #input {
background-color: gray;
}
```
***note
CSS files holding Lit element styles should begin with metadata comments,
between 2 lines marked with `#css_wrapper_metadata_start` and
`#css_wrapper_metadata_end`. These comments tell `css_to_wrapper()` how to
generate the wrapper `.css.ts` file.
* `style-lit` indicates this is a Lit style file
* imports are specified with `#import=//import/path/for/file`
* includes (not used in this particular example) are specified with
`#include="style-name-1 style-name-2"`
* `scheme=relative` indicates imports should be scheme-relative
(i.e. use “`//resources`”)
***
Example `.html.ts `file:
```
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {html} from '//resources/lit/v3_0/lit.rollup.js';
import type {MyExampleElement} from './my_example.js';
export function getHtml(this: MyExampleElement) {
return html`
<div>Input something</div>
<cr-input id="input" .value="${this.myValue}"
?disabled="${this.disabled}"
@value-changed="${this.onInputValueChanged_}">
</cr-input>`;
}
```
`BUILD.gn` file configuration:
```
build_webui("build") {
…
# Use non_web_component_files since the .html.ts file is checked in.
non_web_component_files = [
"my_example.html.ts",
"my_example.ts",
]
# Unlike Polymer, when using Lit non-shared CSS code resides in dedicated
# CSS files passed to css_to_wrapper.
css_files = [
"my_example.css",
]
# Other TS Compiler related arguments…
ts_deps = [
"//ui/webui/resources/cr_elements:build_ts",
"//third_party/lit/v3_0:build_ts",
]
}
```
***note
Note that unlike for Polymer custom elements,
both `.ts` and `.html.ts` files are passed as `non_web_component_files`. This
indicates to `build_webui()` that they do not have a corresponding `.html` file
that needs to be passed to `html_to_wrapper()` (since in the case of Lit
elements, `.html.ts` files are checked in directly).
***
## Polymer to Lit migrations
Many of the boilerplate steps for migrating from Polymer to Lit are documented
in the [readme](https://chromium.googlesource.com/chromium/src/+/main/ui/webui/resources/tools/codemods/lit_migration.md)
in the Lit migration [script](https://source.chromium.org/chromium/chromium/src/+/main:ui/webui/resources/tools/codemods/lit_migration.py)
folder.
***promo
Most of the basic boilerplate migration steps can be automated using the
migration script.
***
Prior to running the script, [jscodeshift](https://github.com/facebook/jscodeshift#readme)
needs to be downloaded and installed as follows:
```
npm install --prefix ui/webui/resources/tools/codemods jscodeshift
```
The script can be invoked to begin migrating an element from Polymer to Lit as
follows (replace the `most_visited.ts` file path with the file being migrated):
```
python3 ui/webui/resources/tools/codemods/lit_migration.py \
--file ui/webui/resources/cr_components/most_visited/most_visited.ts
```
The rest of this section describes migration steps that cannot be automated
using the script.
### Computed properties
Computed properties in Polymer may not be needed after migrating Lit, if the
properties are simply used to populate some part of the element’s template and
are not reflected as attributes or double-bound to a Polymer parent. Lit
automatically re-renders any changed parts of the template without needing to
have the individual properties listed as parameters in the HTML template, so
in these cases the computation method can be used directly in the template
without specifying parameters. An example of this follows.
Polymer HTML template snippet:
```
<cr-button hidden="[[hideButton_]]">Click Me</cr-button>
```
In the Polymer element definition:
```
static get properties() {
return {
loading: Boolean,
showingDialog: Boolean,
hideButton_: {
type: Boolean,
computed: 'computeHideButton_(loading, showingDialog)',
},
};
}
// Other code goes here
private computeHideButton_(): boolean {
return !this.loading && !this.showingDialog;
}
```
This could be rewritten in Lit, omitting the `hideButton_` property entirely.
Equivalent Lit HTML template snippet:
```
<cr-button ?hidden="${this.computeHideButton_()}">Click Me</cr-button>
```
Equivalent Lit element definition:
```
static get properties() {
return {
loading: {type: Boolean},
showingDialog: {type: Boolean},
};
}
// Other code goes here
// Anything referenced in the HTML template needs to be protected, not
// private.
protected computeHideButton_(): boolean {
return !this.loading && !this.showingDialog;
}
```
In other cases, where computed properties are bound to other elements, used as
attributes, or are needed for other internal logic, they can be computed in the
`willUpdate()` lifecycle callback when the properties that they depend on change
as in the following example:
```
override willUpdate(changedProperties: PropertyValues<this>) {
super.willUpdate(changedProperties);
if (changedProperties.has('value')) {
const values = (this.value || '').split(',');
this.multipleValues_ = values.length > 1;
}
}
```
### Observers
Observer code should be triggered in either the `willUpdate()` lifecycle
callback or the `updated()` lifecycle callback, depending on whether it is
internal logic or requires accessing the element’s DOM:
* Any code that measures or queries the element’s DOM belongs in `updated()`
to avoid measuring or querying before rendering has actually completed for
the current cycle.
* Otherwise, updates to other reactive properties should generally be put in
`willUpdate() `so that any resulting template changes from these property
updates can be batched with the other changes in a single update, rather
than triggering a second round of updates.
Consider the following Polymer code, with a complex observer:
```
static get properties() {
return {
max: Number,
min: Number,
value: Number,
};
}
static get observers() {
return [ 'onValueSet_(min, max, value)' ];
}
private onValueSet_() {
this.value = Math.min(Math.max(this.value, this.min), this.max);
const demo = this.shadowRoot!.querySelector('#demo');
if (demo) {
demo.style.height = `${this.value}px`;
}
}
```
The Lit migrated code would look as follows, with the observer code split
into `willUpdate()` and `updated()` based on whether it accesses the DOM:
```
static override get properties() {
return {
max: {type: Number},
min: {type: Number},
value: {type: Number},
};
}
override willUpdate(changedProperties: PropertyValues<this>) {
super.willUpdate(changedProperties);
// Clamp value in willUpdate() so we don't trigger a second update
// cycle for the same changes.
if (changedProperties.has('min') || changedProperties.has('max') ||
changedProperties.has('value')) {
this.value = Math.min(Math.max(this.value, this.min), this.max);
}
}
override updated(changedProperties: PropertyValues<this>) {
super.updated(changedProperties);
// Querying and modifying the DOM should happen in updated().
if (changedProperties.has('value')) {
const demo = this.shadowRoot!.querySelector('#demo');
if (demo) {
demo.style.height = `${this.value}px`;
}
}
}
```
### dom-if
Polymer `<template is="dom-if">` should generally be replaced by ternary
statements in the `.html.ts` file of the form
`${condition ? html`<some-html>` : ''}`. Example simplified from
`cr-toolbar`:
Polymer `cr_toolbar.html`:
```
<div id="content">
<template is="dom-if" if="[[showMenu]]" restamp>
<cr-icon-button id="menuButton" class="no-overlap"
iron-icon="cr20:menu" on-click="onMenuClick_">
</cr-icon-button>
</template>
<h1>[[pageName]]</h1>
</div>
```
Lit `cr_toolbar.html.ts`:
```
<div id="content">
${this.showMenu ? html`
<cr-icon-button id="menuButton" class="no-overlap"
iron-icon="cr20:menu" @click="${this.onMenuClick_}">
</cr-icon-button>` : ''}
<h1>${this.pageName}</h1>
</div>
```
***note
Lit conditional rendering is specifically similar to a dom-if template
that uses `restamp` (like the example above). This represents the vast majority
of cases in Chromium WebUI code.
***
### dom-repeat
Polymer `<template is="dom-repeat">` should generally be replaced by a `map()`
call, of the form
`${this.myItems.map((item, index) => html`<div>item.name</div>`)}`.
Unlike in Polymer where events triggered from elements in the template are
augmented with data about the item and index they are associated with (i.e. the
`DomRepeatEvent` data), event handlers connected to elements in a repeated
subtree in Lit receive the original event without any additional data.
*** promo
When migrating to Lit, event handlers that use the `DomRepeatEvent'`s `item`
and/or `index` need to use a different method to get this information.
***
One possibility is to set the index or item as data attributes on elements that
fire events, as seen in the example that follows.
From the Polymer element template:
```
<template is="dom-repeat" items="[[listItems]]">
<div class="item-container [[getSelectedClass_(item, selectedItem)]]">
<cr-button id="[[getItemId_(index)]]" on-click="onItemClick_">
[[item.name]]
</cr-button>
</div>
</template>
```
From the Polymer element definition:
```
private getItemId_(index: number): string {
return 'listItemId' + index;
}
private getSelectedClass_(item: ListItemType): string {
return (item === this.selectedItem) ? 'selected' : '';
}
private onItemClick_(e: DomRepeatEvent<ListItemType>) {
this.selectedItem = e.model.item;
// Autoscroll to selected item if it is not completely visible.
const list =
this.shadowRoot!.querySelectorAll<HTMLElement>('.item-container');
const selectedElement = list[e.model.index];
assert(selectedElement!.classList.contains('selected'));
selectedElement!.scrollIntoViewIfNeeded();
}
```
Lit template:
```
${this.listItems.map((item, index) => html`
<div class="item-container ${this.getSelectedClass_(item)}">
<cr-button id="${this.getItemId_(index)}"
data-index="${index}" @click="${this.onItemClick_}">
${item.name}
</cr-button>
</div>
`)}
```
***note
Note the `data-index` setting the `data` attribute on the
`cr-button` that triggers the click handler.
***
From the Lit element definition file:
```
protected getItemId_(index: number): string {
return 'listItemId' + index;
}
protected getSelectedClass_(item: ListItemType): string {
return item === this.selectedItem ? 'selected' : '';
}
protected onItemClick_(e: Event) {
const currentTarget = e.currentTarget as HTMLElement;
// Use dataset to get the index set in the .html.ts template.
const index = Number(currentTarget.dataset['index']);
this.selectedItem = this.listItems[index];
// Autoscroll to selected item if it is not completely visible.
const list =
this.shadowRoot!.querySelectorAll<HTMLElement>('.item-container');
const selectedElement = list[index];
selectedElement!.scrollIntoViewIfNeeded();
}
```
### Using composition for more complex dom-if/dom-repeat cases
In more complex cases, composition in the Lit `.html.ts` file may be more
readable and easier to maintain than directly replacing dom-ifs and dom-repeats
as described above. Composition involves the use of helper functions called from
the main `getHtml()` function to define portions of the element’s HTML template.
Cases where this has proven useful include:
1. Nested `<template is="dom-if">` and/or `<template is="dom-repeat">`
2. dom-repeats using the `filter` option
An example based on a simplified form of `cr-url-list-item`, which uses
composition, follows.
From the Polymer `.html` template:
```
<div class="folder-and-count">
<template is="dom-if" if="[[shouldShowFolderImages_(size)]]" restamp>
<template is="dom-repeat" items="[[imageUrls]]"
filter="shouldShowImageUrl_">
<div class="image-container" hidden$="[[!firstImageLoaded_]]">
<img is="cr-auto-img" auto-src="[[item]]" draggable="false">
</div>
</template>
</template>
<div class="count">[[getDisplayedCount_(count)]]</div>
</div>
```
From the Polymer element definition:
```
private shouldShowImageUrl_(_url: string, index: number) {
return index <= 1;
}
private shouldShowFolderImages_(): boolean {
return this.size !== CrUrlListItemSize.COMPACT;
}
private getDisplayedCount_() {
if (this.count && this.count > 999) {
// The square to display the count only fits 3 characters.
return '99+';
}
return this.count;
}
```
From the Lit `.html.ts` template file:
```
import {html} from '//resources/lit/v3_0/lit.rollup.js';
import type {CrUrlListItemElement} from './cr_url_list_item.js';
function getImageHtml(this: CrUrlListItemElement,
item: string, index: number) {
// Replaces dom-repeat's |filter| property by returning empty if the
// filter function returns false for this item and index.
if (!this.shouldShowImageUrl_(item, index)) {
return '';
}
return html`
<div class="image-container" ?hidden="${!this.firstImageLoaded_}">
<img is="cr-auto-img" auto-src="${item}" draggable="false">
</div>`;
}
function getFolderImagesHtml(this: CrUrlListItemElement) {
// Replaces dom-if by returning empty string if condition is false.
if (!this.shouldShowFolderImages_()) {
return '';
}
// Replaces dom-repeat with map()
return html`${
this.imageUrls.map(
(item, index) => getImageHtml.bind(this)(item, index))}`;
}
export function getHtml(this: CrUrlListItemElement) {
return html`
/* other content here */
<div class="folder-and-count">
${getFolderImagesHtml.bind(this)()}
<div class="count">${this.getDisplayedCount_()}</div>
</div>
/* other content */
`;
}
```
From the Lit element definition:
```
protected getDisplayedCount_(): string {
if (this.count && this.count > 999) {
// The square to display the count only fits 3 characters.
return '99+';
}
return this.count === undefined ? '' : this.count.toString();
}
protected shouldShowImageUrl_(_url: string, index: number): boolean {
return index <= 1;
}
protected shouldShowFolderImages_(): boolean {
return this.size !== CrUrlListItemSize.COMPACT;
}
```
### Migrating iron-list clients
There are a few considerations when migrating `iron-list` clients.
First, many existing `iron-list` clients don't require virtualization
as the lists they render are bounded in size and not particularly large (e.g.
only ~100 items). Such clients should use Lit's `map()` directive.
If the `iron-list` client is actually rendering a very large number of items,
some lazy rendering may be necessary. `cr-infinite-list` replicates the
focus and navigation behavior of `iron-list`. It uses `cr-lazy-list`
internally to render items.
`cr-lazy-list` adds list items to the DOM lazily as the user scrolls to them.
It also leverages CSS `content-visibility` to avoid rendering work for items
not in the viewport. If custom navigation or focus behavior (i.e. different
from `iron-list`) is desired, `cr-lazy-list` can be used directly as it is in
the Tab Search Page's `selectable-lazy-list`.
If you do not think any of the 3 options above are suitable for a list you
are migrating or adding to a WebUI, reach out to the WebUI team.
For incremental migrations, it may be useful to migrate `iron-list` children
prior to migrating the `iron-list` client itself. This can be somewhat
complicated by `iron-list` manually positioning its items, meaning it must
always know when its children change size. When migrating `iron-list`
children, the child elements must manually fire an `iron-resize` event from
their `updated()` lifecycle callback whenever any property that may impact
their height has changed. See example below:
From the `list_parent.html` template (`iron-list` client so must be Polymer)
```
<iron-list id="list" items="[[listItems_]]" as="item">
<template>
<custom-item description="[[item.description]]" name="[[item.name]]"
on-click="onListItemClick_">
</custom-item>
</template>
</iron-list>
```
From the child `custom_item.html.ts` template:
```
<div class="name">${this.name}</div>
<div class="description" ?hidden="${!this.description}">
${this.description}
</div>
```
In this case, the value of `description` impacts the height of the child
item. If `iron-list` is not notified of when the child is done with rendering
a change to this property, it may compute the child's height incorrectly, and
display gaps or overlap in the list. To prevent this, the child item should
fire `iron-resize` in `updated()` if its `description` property changes.
From `custom_item.ts`:
```
override updated(changedProperties: PropertyValues<this>) {
super.updated(changedProperties);
if (changedProperties.has('description')) {
this.fire('iron-resize');
}
}
```
## Additional Lit and Polymer differences
### Testing
A large number of unit tests do something like the following:
```
// Validate that the input is disabled when invalid is set.
myTestElement.invalid = true;
assertTrue(myTestElement.$.input.disabled);
```
This assumes that setting `invalid` synchronously updates the DOM of the test
element. If the test element is a Lit-based element, this is no longer the case,
and we need to wait for a render cycle to complete. There are a couple of ways
to do this:
1. Preferred: Use `await microtasksFinished()` test helper method from
chrome://webui-test/test\_util.js. This method awaits a setTimeout of 0 which
allows any render cycles to complete (useful if there may be multiple Lit
elements that need to finish updating before assertions).
2. Directly `await myTestElement.updateComplete` (waits for the test element’s
render cycle).
Updated example:
```
// Validate that the input is disabled when invalid is set.
myTestElement.invalid = true;
await microtasksFinished();
assertTrue(myTestElement.$.input.disabled);
```
*** note
Note: for many test cases, it is less fragile to directly wait on an event
or BrowserProxy call that should be triggered by an action in a test, instead
of either assuming everything is synchronous or waiting on framework-dependent
test helpers like `microtasksFinished()` or the Polymer
`waitAfterNextRender()/flushTasks()` that do not actually guarantee that
anything specific has happened.
***
### Use of the `hidden` attribute
As documented in the [styleguide](https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/web/web.md#Polymer),
in Polymer the `hidden` attribute was recommended over `<template is="dom-if">`
for cases of showing and hiding small amounts of HTML or a single element. In
Lit, since conditional rendering does not rely on adding a custom element like
dom-if, there is not the same potential performance downside to using
conditional rendering instead of the `hidden` attribute.
***promo
In Lit, conditional rendering can be used instead of the
`hidden` attribute in most cases, and should always be used anywhere
`<template is="dom-if">` would previously have been used in Polymer code.
***