/
query-all.ts
51 lines (48 loc) · 1.27 KB
/
query-all.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
/**
* @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';
/**
* A property decorator that converts a class property into a getter
* that executes a querySelectorAll on the element's renderRoot.
*
* @param selector A DOMString containing one or more selectors to match.
*
* See:
* https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
*
* ```ts
* class MyElement {
* @queryAll('div')
* divs: NodeListOf<HTMLDivElement>;
*
* render() {
* return html`
* <div id="first"></div>
* <div id="second"></div>
* `;
* }
* }
* ```
* @category Decorator
*/
export function queryAll(selector: string) {
return decorateProperty({
descriptor: (_name: PropertyKey) => ({
get(this: ReactiveElement) {
return this.renderRoot?.querySelectorAll(selector) ?? [];
},
enumerable: true,
configurable: true,
}),
});
}