import { MapArrayKeyFn } from "./internals/map-array.js"; import { Expression } from "./signals.js"; import type { Component, Content, Falsy } from "./types.js"; /** * Render arbitrary content. * * Supported content types are: * + Null and undefined (not displayed). * + Arbitrarily nested arrays/fragments of content. * + DOM nodes. Children will be removed from document fragments. * + {@link View Views}. * + Anything created with rvx's jsx runtime. * + Anything else is displayed as text. * * @param content The content to render. * @returns A view instance or the `content` parameter itself if it's already a view. * * @example * ```tsx * import { $, render } from "rvx"; * * // Not displayed: * render(null); * render(undefined); * * // Arbitrarily nested arrays/fragments of content: * render([["Hello"], " World!"]); * render(<>{<>"Hello"}{" World!"}); * * // DOM nodes: * render(

Hello World!

); * render(document.createElement("input")); * render(document.createTextNode("Hello World!")); * render(someTemplate.content.cloneNode(true)); * * // Views: * render(render("Hello World!")); * render(when(true, () => "Hello World!")); * render({() => "Hello World!"}); * * // Text: * render("Hello World!"); * render(() => "Hello World!"); * render(42); * render($(42)); * ``` */ export declare function render(content: Content): View; /** * Render arbitrary content and append it to the specified parent until the current lifecycle is disposed. * * @param parent The parent node. * @param content The content to render. See {@link render} for supported types. * @returns The view instance. * * @example * ```tsx * import { mount } from "rvx"; * * mount( * document.body, *

Hello World!

* ); * ``` * * Since the content is removed when the current lifecycle is disposed, this can also be used to temporarily append * content to different elements while some component is rendered: * ```tsx * import { mount, Content } from "rvx"; * * function Popover(props: { text: Content, children: Content }) { * const visible = $(false); * * mount( * document.body, * * {props.children} * * ); * * return ; * } * * mount( * document.body, * * Hello World! * * ); * ``` */ export declare function mount(parent: Node, content: Content): View; /** * A function that is called when the view boundary may have been changed. */ export interface ViewBoundaryOwner { /** * @param first The current first node. * @param last The current last node. */ (first: Node, last: Node): void; } /** * A function that must be called after the view boundary has been changed. */ export interface ViewSetBoundaryFn { /** * @param first The first node if changed. * @param last The last node if changed. */ (first: Node | undefined, last: Node | undefined): void; } interface UninitViewProps { /** * The current first node of this view. * * + This may be undefined while the view is not fully initialized. * + This property is not reactive. */ get first(): Node | undefined; /** * The current last node of this view. * * + This may be undefined while the view is not fully initialized. * + This property is not reactive. */ get last(): Node | undefined; } export type UninitView = UninitViewProps & Omit; /** * A function that is called once to initialize a view instance. * * View creation will fail if no first or last node has been set during initialization. */ export interface ViewInitFn { /** * @param setBoundary A function that must be called after the view boundary has been changed. * @param self The current view itself. This can be used to keep track of the current boundary and parent nodes. */ (setBoundary: ViewSetBoundaryFn, self: UninitView): void; } /** * Represents a sequence of at least one DOM node. * * Consumers of the view API need to guarantee that: * + The sequence of nodes is not modified from the outside. * + If there are multiple nodes, all nodes must have a common parent node at all time. */ export declare class View { #private; /** * Create a new view. * * View implementations need to guarantee that: * + The view doesn't break when the parent node is replaced or when a view consisting of only a single node is detached from its parent. * + The boundary is updated immediately after the first or last node has been updated. * + If there are multiple nodes, all nodes remain in the current parent. * + If there are multiple nodes, the initial nodes must have a common parent. */ constructor(init: ViewInitFn); /** * The current first node of this view. * * Note, that this property is not reactive. */ get first(): Node; /** * The current last node of this view. * * Note, that this property is not reactive. */ get last(): Node; /** * The current parent node or undefined if there is none. * * Note, that this property is not reactive. */ get parent(): Node | undefined; /** * Set the boundary owner for this view until the current lifecycle is disposed. * * @throws An error if there currently is a boundary owner. */ setBoundaryOwner(owner: ViewBoundaryOwner): void; /** * Append all nodes of this view to the specified parent. * * @param parent The parent to append to. * @deprecated Use {@link move} instead. */ appendTo(parent: Node): void; /** * Insert all nodes of this view before a reference child of the specified parent. * * @param parent The parent to insert into. * @param ref The reference child to insert before. If this is null, the nodes are appended to the parent. * @deprecated Use {@link move} instead. */ insertBefore(parent: Node, ref: Node | null): void; /** * Move all nodes of this view into the specified parent. * * @param parent The parent to move into. * @param before A child of the specified parent to insert before, or null or undefined to append. */ move(parent: Node, before?: Node | null): void; /** * Detach all nodes of this view from the current parent if there is one. * * If there are multiple nodes, they are moved into a new document fragment to allow the view implementation to stay alive. * * @returns The single removed node or the document fragment they have been moved into. */ detach(): Node | DocumentFragment; } /** * Get an iterator over all current top level nodes of a view. * * @param view The view. * @returns The iterator. * * @example * ```tsx * import { render, viewNodes } from "rvx"; * * const view = render(<> *

Hello World!

* ); * * for (const node of viewNodes(view)) { * console.log(node); * } * ``` */ export declare function viewNodes(view: View): IterableIterator; /** * Watch an expression and render content from its result. * * + If an error is thrown during initialization, the error is re-thrown. * + If an error is thrown during a signal update, the previously rendered content is kept in place and the error is re-thrown. * + Content returned from the component can be directly reused within the same `nest` instance. * * See {@link Nest ``} when using JSX. * * @param expr The expression to watch. * @param component The component to render with the expression result. If the expression returns a component, null or undefined, this can be omitted. * * @example * ```tsx * import { $, nest, e } from "rvx"; * * const count = $(0); * * // Using the expression result in a component: * nest(count, count => { * switch (count) { * case 0: return e("h1").append("Hello World!"); * case 1: return "Something else..."; * } * }) * * // Or directly returning a component from the expression: * nest(() => { * switch (count.value) { * case 0: return () => e("h1").append("Hello World!"); * case 1: return () => "Something else..."; * } * }) * ``` */ export declare function nest(expr: Expression): View; export declare function nest(expr: Expression, component: Component>): View; /** * Render conditional content. * * + Content is only re-rendered if the expression result is not strictly equal to the previous one. If this behavior is undesired, use {@link nest} instead. * + If an error is thrown by the expression or component during initialization, the error is re-thrown. * + If an error is thrown by the expression or component during a signal update, the previously rendered content is kept and the error is re-thrown. * * See {@link Show ``} when using JSX. * * @param condition The condition to watch. * @param truthy The component to render when the condition result is truthy. * @param falsy An optional component to render when the condition is falsy. * * @example * ```tsx * import { $, when, e } from "rvx"; * * const message = $("Hello World!"); * * when(message, value => e("h1").append(value), () => "No message...") * ``` */ export declare function when(condition: Expression, truthy: Component>, falsy?: Component): View; export interface ForContentFn { /** * @param value The value. * @param index An expression to get the current index. * @returns The content. */ (value: T, index: () => number): Content; } /** * Render content for each entry in an iterable. * * Errors thrown by the component or while updating an index result in undefined behavior. * * See {@link For ``} for use with JSX. * * @param each The expression to watch. Note, that observables accessed during iteration will also trigger updates. * @param component The component to render for each entry. * @param keyFn A function to get the key for a given value. If not specified, the value is used as key. * * @example * ```tsx * import { $, forEach, e } from "rvx"; * * const items = $([1, 2, 3]); * * forEach(items, value => e("li").append(value)) * ``` */ export declare function forEach(each: Expression>, component: ForContentFn>, keyFn?: MapArrayKeyFn>): View; export interface IndexContentFn { /** * @param value An expression to get the current value. * @param index The index. * @returns The content. */ (value: () => T, index: number): Content; } /** * Render content for each index in an iterable. * * Errors thrown by the component or while updating a value result in undefined behavior. * * See {@link Index ``} when using JSX. * * @param each The expression to watch. Note, that observables accessed during iteration will also trigger updates. * @param component The component to render for each index. * * @example * ```tsx * import { $, indexEach, e } from "rvx"; * * const items = $([1, 2, 3]); * * indexEach(items, value => e("li").append(value)) * ``` */ export declare function indexEach(each: Expression>, component: IndexContentFn>): View; /** * Create a component for safely moving or reusing already rendered content. * * @example * ```tsx * const content = movable(

Hello World!

); * * * {content} * * * {content} * * ``` */ export declare function movable(content: Content): Component; /** * Attach or detach content depending on an expression. * * Content is kept alive when detached. * * See {@link Attach ``} when using JSX. * * @param condition The condition to watch. * @param content The content to attach when the condition result is truthy. * * @example * ```tsx * import { $, attachWhen } from "rvx"; * * const showMessage = $(true); * * attachWhen(showMessage, e("h1").append("Hello World!")) * ``` */ export declare function attachWhen(condition: Expression, content: Content): View; export {}; //# sourceMappingURL=view.d.ts.map