メインコンテンツまでスキップ
バージョン: v5 (Preview)

イベント

stencil eventsのようなものはありません。代わりに、StencilはDOMイベントの使用を推奨しています。 ただし、Stencilは、コンポーネントが発行できるイベントと、コンポーネントがリッスンするイベントを指定するためのAPIを提供します。 これは、 Event()および Listen()デコレータを使用して行われます。

イベントデコレータ​

コンポーネントは、イベントエミッタデコレータを使用してデータとイベントを発行できます。

カスタムDOMイベントをディスパッチして他のコンポーネントを処理するには、@Event()デコレータを使用します。

import { Event, EventEmitter } from '@stencil/core';

...
export class TodoList {

@Event() todoCompleted: EventEmitter<Todo>;

todoCompletedHandler(todo: Todo) {
this.todoCompleted.emit(todo);
}
}

上記のコードは、todoCompletedと呼ばれるカスタムDOMイベントをディスパッチします。

Event(opts: EventOptions)デコレータは、オプションでオプションオブジェクトを受け入れて、ディスパッチされたイベントの動作を形成します。 オプションとデフォルトについては、以下で説明します。

export interface EventOptions {
/**
* デフォルトをオーバーライドする文字列カスタムイベント名。
*/
eventName?: string;
/**
* イベントがDOMを介してバブルアップするかどうかを示すブール値。
*/
bubbles?: boolean;

/**
* イベントがキャンセル可能かどうかを示すブール値。
*/
cancelable?: boolean;

/**
* イベントがShadowDOMと通常のDOMの境界を越えてバブルできるかどうかを示すブール値。
*/
composed?: boolean;
}

例:

import { Event, EventEmitter } from '@stencil/core';

...
export class TodoList {

// Event called 'todoCompleted' that is "composed", "cancellable" and it will bubble up!
@Event({
eventName: 'todoCompleted',
composed: true,
cancelable: true,
bubbles: true,
}) todoCompleted: EventEmitter<Todo>;

todoCompletedHandler(todo: Todo) {
const event = this.todoCompleted.emit(todo);
if(!event.defaultPrevented) {
// 防止されていない場合は、デフォルトの処理コードを実行します
}
}
}
注記

In the case where the Stencil Event type conflicts with the native web Event type, there are two possible solutions:

  1. Import aliasing:
import { Event as StencilEvent, EventEmitter } from '@stencil/core';

@StencilEvent() myEvent: EventEmitter<{value: string, ev: Event}>;
  1. Namespace the native web Event type with globalThis:
@Event() myEvent: EventEmitter<{value: string, ev: globalThis.Event}>;

Listenデコレータ​

The Listen() decorator is for listening to DOM events, including the ones dispatched from @Events. The event listeners are automatically added and removed when the component gets added or removed from the DOM.

以下の例では、子コンポーネント TodoListがEventEmitterを使用して todoCompletedイベントを発行すると仮定します。

import { Listen } from '@stencil/core';

...
export class TodoApp {

@Listen('todoCompleted')
todoCompletedHandler(event: CustomEvent<Todo>) {
console.log('Received the custom todoCompleted event: ', event.detail);
}
}

リッスンのオプション​

@Listen(eventName,opts: ListenOptions)には、DOMイベントリスナーのアタッチ方法を構成するために使用できる2番目のオプションの引数が含まれています。

export interface ListenOptions {
target?: 'body' | 'document' | 'window';
capture?: boolean;
passive?: boolean;
}

The available options are target, capture and passive:

target​

ハンドラーは、ホスト自体以外のイベントに登録することもできます。 targetオプションを使用して、イベントリスナーが接続されている場所を変更できます。これは、アプリケーション全体のイベントをリッスンする場合に役立ちます。

以下の例では、 windowから発行されたスクロールイベントをリッスンします。

  @Listen('scroll', { target: 'window' })
handleScroll(ev) {
console.log('the body was scrolled', ev);
}

passive​

By default, Stencil uses several heuristics to determine if it must attach a passive event listener or not. The passive option can be used to change the default behavior.

https://developers.google.com/web/updates/2016/06/passive-event-listenersをチェックしてください -詳細については、リスナー)を参照してください。

capture​

@Listenでアタッチされたイベントリスナーは、デフォルトではcaptureしません。 イベントリスナーがcaptureに設定されている場合、それはcapture phase中にイベントがディスパッチされることを意味します。 詳細についてはhttps://www.quirksmode.org/js/events_order.htmlを確認してください。

  @Listen('click', { capture: true })
handleClick(ev) {
console.log('click');
}

キーボードイベント​

キーボードイベントの場合、 @Listen()で標準の keydownイベントを使用し、event.keyCodeまたは event.whichを使用してキーコードを取得するか、event.keyを使用しての文字列表現を取得できます。

@Listen('keydown')
handleKeyDown(ev: KeyboardEvent){
if (ev.key === 'ArrowDown'){
console.log('down arrow pressed')
}
}

イベントキー文字列の詳細についてはw3c specを参照してください.

JSXでイベントを使用する​

Stencilでコンパイルされたアプリケーションまたはコンポーネント内で、リスナーをJSXのイベントに直接バインドすることもできます。 これは、 onClickなどの通常のDOMイベントと非常によく似ています。

上からTodoListコンポーネントを使用してみましょう

import { Event, EventEmitter } from '@stencil/core';

...
export class TodoList {

@Event() todoCompleted: EventEmitter<Todo>;

todoCompletedHandler(todo: Todo) {
this.todoCompleted.emit(todo);
}
}

これで、次の構文を使用して、JSXのコンポーネントでこのイベントを直接聞くことができます。

<todo-list onTodoCompleted={ev => this.someMethod(ev)} />

This property is generated automatically and is prefixed with "on". For example, if the event emitted is called todoDeleted the property will be called onTodoDeleted:

<todo-list onTodoDeleted={ev => this.someOtherMethod(ev)} />

In this playground, todo-list emits todoCompleted when you click Done, and todo-app listens with onTodoCompleted:

todo-list.tsx
import { Component, Event, EventEmitter, State } from '@stencil/core';

export interface Todo {
title: string;
}

@Component({ tag: 'todo-list' })
export class TodoList {
@State() todos: Todo[] = [{ title: 'Write the docs' }, { title: 'Ship v5' }];

@Event() todoCompleted: EventEmitter<Todo>;

todoCompletedHandler(todo: Todo) {
this.todos = this.todos.filter((t) => t !== todo);
this.todoCompleted.emit(todo);
}

render() {
return (
<ul>
{this.todos.map((todo) => (
<li>
{todo.title} <button onClick={() => this.todoCompletedHandler(todo)}>Done</button>
</li>
))}
</ul>
);
}
}
todo-app.tsx
import { Component, State } from '@stencil/core';
import type { Todo } from './todo-list';

@Component({ tag: 'todo-app' })
export class TodoApp {
@State() completed: Todo[] = [];

render() {
return (
<div>
<todo-list onTodoCompleted={(ev) => (this.completed = [...this.completed, ev.detail])} />
<p>Completed: {this.completed.map((todo) => todo.title).join(', ') || 'nothing yet'}</p>
</div>
);
}
}

非JSX要素からのイベントをリッスンする​

<todo-list></todo-list>
<script>
const todoListElement = document.querySelector('todo-list');
todoListElement.addEventListener('todoCompleted', event => { /* your listener */ })
</script>