@ContentChildren()

This decorator is inspired by Angular’s @ContentChildren decorator and allows you to observe changes in the content children elements of a Stencil component and set the value of a decorated property with the latest emitted value from the observable created by the contentChildrenObservable() operator.

Property may be decorated with @State() decorator to trigger re-render of the component when the value of the property changes.

Warning

Performances of this decorator will have same caveats as the used underlying observable. Read the documentation of the contentChildrenObservable() operator for more information about performance considerations.

ContentChildren(selector, options)

Decorates a property with an observable emitting the content children elements of a Stencil component that matches the given selector and sets the value of the decorated property with the latest emitted value from the observable created by the contentChildrenObservable() operator.

function ContentChildren<E extends Element = Element>(selector: string, options?: ContentChildrenOptions): PropertyDecorator
Arguments:
  • selector (string) – The CSS selector to match the content children elements.

  • options (ContentChildrenOptions) – Optional configuration for the observable.

Returns:

Property decorator that sets the value of the decorated property with the latest emitted value from the observable created by the contentChildrenObservable() operator.

Return type:

PropertyDecorator

class ContentChildrenOptions()

Configuration options for observing a content children elements.

export type ContentChildrenOptions = {
    cached?: boolean;
    filter?: ElementsFilterFn | boolean;
    whileConnected?: boolean;
    distinct?: ElementComparatorFn | boolean;
    attributes?: boolean;
    attributeFilter?: string[];
};
ContentChildrenOptions.cached
Type:

boolean

Optional: Yes

Default: false

If true, property will be set only once, with the value of the content children elements, if found. However, with filter option set, property will be set only once with the value of the filtered elements.

ContentChildrenOptions.filter
Type:

ElementsFilterFn | boolean

Optional: Yes

Default: false

Controls whether the property will be set only with the value of the filtered elements. If true, the property will be set only when the content children elements are found. If false, the property will be set with the value of the content children elements, if found, or empty array if not found. If a function is provided, it will be used to filter the content children elements and the property will be set only with the value of the filtered elements.

ContentChildrenOptions.whileConnected
Type:

boolean

Optional: Yes

Default: false

If true, the property will be set only when the component is connected to the DOM. When component is disconnected, the property will not be set with any values. However, when component is re-connected, the property will be set with the current value, if it has changed in the meantime. You may use this option to avoid unnecessary recalculations or side effects when the component is not in the DOM, preserving last set value.

ContentChildrenOptions.distinct

Underlying implementation of the decorator uses contentChildrenObservable() operator. See the documentation of the operator for more information about this option.

ContentChildrenOptions.attributes

Underlying implementation of the decorator uses contentChildrenObservable() operator. See the documentation of the operator for more information about this option.

ContentChildrenOptions.attributeFilter

Underlying implementation of the decorator uses contentChildrenObservable() operator. See the documentation of the operator for more information about this option.

Example

Observe content children elements and execute logic when they change.
 1import { Component, h, State, Watch } from '@stencil/core';
 2import { ContentChildren } from '@runopenode/rxjs-stencil';
 3
 4@Component({
 5    tag: 'my-component',
 6    shadow: true,
 7})
 8export class MyComponent {
 9
10    @State()
11    @ContentChildren('.item', { whileConnected: true })
12    private _items!: Element[] = [];
13
14    @Watch('_items')
15    private _onItemsChange(): void {
16        // Collection of items has changed, execute logic here.
17    }
18
19    public render(): any {
20        return (
21            <slot/>
22        );
23    }
24}