UNPKG

@lit/reactive-element

Version:

A simple low level base class for creating fast, lightweight web components

70 lines 2.47 kB
/** * @license * Copyright 2021 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ var _a; /* * 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 { decorateProperty } from './base.js'; /** * A tiny module scoped polyfill for HTMLSlotElement.assignedElements. */ const slotAssignedElements = ((_a = window.HTMLSlotElement) === null || _a === void 0 ? void 0 : _a.prototype.assignedElements) != null ? (slot, opts) => slot.assignedElements(opts) : (slot, opts) => slot .assignedNodes(opts) .filter((node) => node.nodeType === Node.ELEMENT_NODE); /** * A property decorator that converts a class property into a getter that * returns the `assignedElements` of the given `slot`. Provides a declarative * way to use * [`HTMLSlotElement.assignedElements`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLSlotElement/assignedElements). * * Can be passed an optional {@linkcode QueryAssignedElementsOptions} object. * * Example usage: * ```ts * class MyElement { * @queryAssignedElements({ slot: 'list' }) * listItems!: Array<HTMLElement>; * @queryAssignedElements() * unnamedSlotEls!: Array<HTMLElement>; * * render() { * return html` * <slot name="list"></slot> * <slot></slot> * `; * } * } * ``` * * Note, the type of this property should be annotated as `Array<HTMLElement>`. * * @category Decorator */ export function queryAssignedElements(options) { const { slot, selector } = options !== null && options !== void 0 ? options : {}; return decorateProperty({ descriptor: (_name) => ({ get() { var _a; const slotSelector = `slot${slot ? `[name=${slot}]` : ':not([name])'}`; const slotEl = (_a = this.renderRoot) === null || _a === void 0 ? void 0 : _a.querySelector(slotSelector); const elements = slotEl != null ? slotAssignedElements(slotEl, options) : []; if (selector) { return elements.filter((node) => node.matches(selector)); } return elements; }, enumerable: true, configurable: true, }), }); } //# sourceMappingURL=query-assigned-elements.js.map