import { MapArrayKeyFn } from "./map-array.js"; import { Expression } from "./signals.js"; import { Component, Content, Falsy } from "./types.js"; import { attachWhen, ForContentFn, forEach, IndexContentFn, indexEach, nest, View, when } from "./view.js"; /** * 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 `` instance. * * See {@link nest} when not using JSX. * * @example * ```tsx * import { $, Nest } from "rvx"; * * const count = $(0); * * // Using the expression result in a component: * * {count => { * switch (count) { * case 0: return

Hello World!

; * case 1: return "Something else..."; * } * }} *
* * // Or directly returning a component from the expression: * { * switch (count.value) { * case 0: return () =>

Hello World!

; * case 1: return () => "Something else..."; * } * }} /> * ``` */ export function Nest(props: { /** * The expression to watch. */ watch: Expression; /** * The component to render with the expression result. * * If the expression returns a component, null or undefined, this can be omitted. */ children: Component>; } | { /** * The expression to watch. */ watch: Expression; }): View { return nest(props.watch as Expression, (props as any).children); } /** * 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 when} when not using JSX. * * @example * ```tsx * import { $, Show } from "rvx"; * * const message = $("Hello World!"); * * <>No message...}> * {value =>

{value}

} *
* ``` */ export function Show(props: { /** * The condition to watch. */ when: Expression; /** * The component to render when the condition result is truthy. */ children: Component>; /** * An optional component to render when the condition result is falsy. */ else?: Component; }): View { return when(props.when, props.children, props.else); } /** * Render content for each entry in an iterable. * * Errors thrown by the component or while updating an index result in undefined behavior. * * See {@link forEach} when not using JSX. * * @example * ```tsx * import { $, For } from "rvx"; * * const items = $([1, 2, 3]); * * * {value =>
  • {value}
  • } *
    * ``` */ export function For(props: { /** * The expression to watch. * * Note that observables accessed during iteration will also trigger updates. */ each: Expression>; /** * The component to render for each entry. */ children: ForContentFn>; /** * A function to get the key for a given value. If not specified, the value is used as key. */ key?: MapArrayKeyFn>; }): View { return forEach(props.each, props.children, props.key); } /** * Render content for each index in an iterable. * * Errors thrown by the component or while updating a value result in undefined behavior. * * See {@link indexEach} when not using JSX. * * @example * ```tsx * import { $, Index } from "rvx"; * * const items = $([1, 2, 3]); * * * {value =>
  • {value}
  • } *
    * ``` */ export function Index(props: { /** * The expression to watch. * * Note that observables accessed during iteration will also trigger updates. */ each: Expression>; /** * The component to render for each index pair. */ children: IndexContentFn>; }): View { return indexEach(props.each, props.children); } /** * Attach or detach content depending on an expression. * * Content is kept alive when detached. * * See {@link attachWhen} when not using JSX. * * @example * ```tsx * import { $, Attach } from "rvx"; * * const showMessage = $(true); * * *

    Hello World!

    *
    * ``` */ export function Attach(props: { /** * The condition to watch. */ when: Expression; /** * The content to attach when the condition result is truthy. */ children?: Content; }): View { return attachWhen(props.when, props.children); }