定義

LitコンポーネントはLitElementを拡張したクラスを生成して、そのクラスをブラウザに登録することで定義します。

@customElement('simple-greeting')
export class SimpleGreeting extends LitElement { /* ... */ }

@customElementデコレータはcustomElements.defineを実行する処理の省略した表現です。 これはCustom Element classをブラウザに登録します。そして、要素名(この場合はsimple-greeting)をそれに関連付けます。

JavaScriptを使っていたりデコレータを使わない場合は直接defined()を実行します。

export class SimpleGreeting extends LitElement { /* ... */  }
customElements.define('simple-greeting', SimpleGreeting);

LitコンポーネントはHTML要素です

Litコンポーネントを定義するとcustom elementsが定義されます。 つまり、その定義された要素はbuilt-in要素のように使うことができます。

<simple-greeting name="Markup"></simple-greeting>
const greeting = document.createElement('simple-greeting');

LitElementクラスはHTMLElementのサブクラスです。 だから、LitコンポーネントはWeb標準のHTMLElementのプロパティとメソッドをすべて継承します。 正確には、LitElementReactiveElementを継承しています。 ReactiveElementHTMLElementを継承してリアクティブプロパティを実装しています。

lit-element-inheritance.png

TypeScriptの型を提供する

TypeScriptはタグ名に基づいて特定のDOM APIが返すクラスを推論します。 例えば、document.createElement('img')src: stringプロパティを持つHTMLImageElementを返します。 Custom elementsは下記のようにHTMLElementTagNameMapに加えることによってbuilt-in要素と同じように取り扱われます。

@customElement('my-element')
export class MyElement extends LitElement {
  @property({type: Number})
  aNumber: number = 5;
  /* ... */
}

declare global {
  interface HTMLElementTagNameMap {
    "my-element": MyElement;
  }
}

これによって、以下のコードは適切に型チェックされます。

const myElement = document.createElement('my-element');
myElement.aNumber = 10;

TypeScriptで定義したすべての要素をHTMLElementTagNameMapに追加することや npmにパッケージ公開する際は必ず.d.tsでそれらを提供することを推奨します。


License

Japanese part

Creative Commons Attribution-NonCommercial 4.0 International Public License

Copyright (c) 2022 38elements

Other

Creative Commons Attribution 3.0 Unported

Copyright (c) 2020 Google LLC. All rights reserved.

BSD 3-Clause License

Copyright (c) 2020 Google LLC. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.