v 0.2.0

@query

The @query decorator provides a convenient way to query and cache references to elements within your custom element's DOM. It creates a getter that performs the query on first access and caches the results by default.

Usage

import { RadiantElement, customElement, query } from '@ecopages/radiant';

@customElement('user-profile')
export class UserProfile extends RadiantElement {
	// Query by data-ref (recommended)
	@query({ ref: 'avatar' }) avatar!: HTMLImageElement;
	
	// Query by CSS selector
	@query({ selector: 'button.action' }) submitButton!: HTMLButtonElement;
	
	// Query all matching elements
	@query({ ref: 'item', all: true }) items!: HTMLElement[];
}

Parameters

ParameterTypeRequiredDescription
selectorstringOne of selector/refCSS selector to match elements.
refstringOne of selector/refValue of data-ref attribute to match.
allbooleanNoQuery for all matching elements (default: false).
cachebooleanNoCache the query result (default: true).

Feature Highlight

Caching Behavior

By default, queries are cached for performance. For dynamic content that changes frequently, you can disable caching to re-query on every access.

@query({ ref: 'dynamic-item', all: true, cache: false }) 
items!: HTMLElement[];

Type Safety

Use TypeScript's non-null assertion operator (!) for required elements, or the optional chaining operator (?) if the element might not be present.

@query({ ref: 'required-el' }) element!: HTMLElement;
@query({ ref: 'optional-el' }) optional?: HTMLElement;

Learn More