import { DomCollection } from './collection.ts'; import { create } from './dom.ts'; import { TemplateStamper } from './template.ts'; import type { SelectOptions, CreateOptions, TemplateConfig } from './types.ts'; /** * Query the DOM and return a {@link DomCollection} wrapping matched elements. * * @example * const buttons = $('.btn'); * const scoped = $('.item', { container: sidebar }); * const managed = $('.chat', { signal: controller.signal }); * const both = $('.btn', { container: sidebar, signal: ctrl.signal }); * const wrapped = $(element); * const wrapped = $([el1, el2], { signal: ctrl.signal }); */ export function $( selector: string | T | T[], opts?: SelectOptions ): DomCollection { if (Array.isArray(selector)) { return new DomCollection(selector, opts); } if (typeof selector !== 'string') { return new DomCollection([selector], opts); } const context = opts?.container ?? document.documentElement; const elements = Array.from(context.querySelectorAll(selector)); return new DomCollection(elements, opts); } /** * Create a DOM element and return it wrapped in a {@link DomCollection}. * * @example * const card = $.create('div', { text: 'Hello', class: ['card'] }); */ $.create = function createEl( tag: K, opts?: CreateOptions ): DomCollection { const el = create(tag, opts); return new DomCollection([el], opts); } as ( tag: K, opts?: CreateOptions ) => DomCollection; /** * Create a reusable {@link TemplateStamper} for an HTML `