/
event-options.ts
57 lines (54 loc) · 1.53 KB
/
event-options.ts
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
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
/*
* IMPORTANT: For compatibility with tsickle and the Closure JS compiler, all
* property decorators (but not class decorators) in this file that have
* an @ExportDecoratedItems annotation must be defined as a regular function,
* not an arrow function.
*/
import {ReactiveElement} from '../reactive-element.js';
import {decorateProperty} from './base.js';
/**
* Adds event listener options to a method used as an event listener in a
* lit-html template.
*
* @param options An object that specifies event listener options as accepted by
* `EventTarget#addEventListener` and `EventTarget#removeEventListener`.
*
* Current browsers support the `capture`, `passive`, and `once` options. See:
* https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Parameters
*
* ```ts
* class MyElement {
* clicked = false;
*
* render() {
* return html`
* <div @click=${this._onClick}>
* <button></button>
* </div>
* `;
* }
*
* @eventOptions({capture: true})
* _onClick(e) {
* this.clicked = true;
* }
* }
* ```
* @category Decorator
*/
export function eventOptions(options: AddEventListenerOptions) {
return decorateProperty({
finisher: (ctor: typeof ReactiveElement, name: PropertyKey) => {
Object.assign(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ctor.prototype[name as keyof ReactiveElement] as any,
options
);
},
});
}