|
| 1 | +import { Expression, ExpressionResult } from "./signals.js"; |
| 2 | +import { Component, Content, Falsy } from "./types.js"; |
| 3 | +import { attachWhen, ForContentFn, forEach, IndexContentFn, indexEach, nest, View, when } from "./view.js"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Watch an expression and render content from it's result. |
| 7 | + * |
| 8 | + * + If an error is thrown during initialization, the error is re-thrown. |
| 9 | + * + If an error is thrown during a signal update, the previously rendered content is kept in place and the error is re-thrown. |
| 10 | + * + Content returned from the component can be directly reused within the same `<Nest>` instance. |
| 11 | + * |
| 12 | + * See {@link nest} when not using JSX. |
| 13 | + * |
| 14 | + * @example |
| 15 | + * ```tsx |
| 16 | + * import { $, Nest } from "rvx"; |
| 17 | + * |
| 18 | + * const count = $(0); |
| 19 | + * |
| 20 | + * <Nest watch={count}> |
| 21 | + * {count => { |
| 22 | + * switch (count) { |
| 23 | + * case 0: return <h1>Hello World!</h1>; |
| 24 | + * case 1: return "Something else..."; |
| 25 | + * } |
| 26 | + * }} |
| 27 | + * </Nest> |
| 28 | + * ``` |
| 29 | + */ |
| 30 | +export function Nest<T>(props: { |
| 31 | + /** |
| 32 | + * The expression to watch. |
| 33 | + */ |
| 34 | + watch: T; |
| 35 | + |
| 36 | + /** |
| 37 | + * The component to render with the expression result. |
| 38 | + * |
| 39 | + * If the expression returns a component, null or undefined, this can be omitted. |
| 40 | + */ |
| 41 | + children: Component<ExpressionResult<T>>; |
| 42 | +} | { |
| 43 | + /** |
| 44 | + * The expression to watch. |
| 45 | + */ |
| 46 | + watch: Expression<Component | null | undefined>; |
| 47 | +}): View { |
| 48 | + return nest(props.watch, (props as any).children); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Render conditional content. |
| 53 | + * |
| 54 | + * + 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. |
| 55 | + * + If an error is thrown by the expression or component during initialization, the error is re-thrown. |
| 56 | + * + 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. |
| 57 | + * |
| 58 | + * See {@link when} when not using JSX. |
| 59 | + * |
| 60 | + * @example |
| 61 | + * ```tsx |
| 62 | + * import { $, Show } from "rvx"; |
| 63 | + * |
| 64 | + * const message = $<null | string>("Hello World!"); |
| 65 | + * |
| 66 | + * <Show when={message} else={() => <>No message...</>}> |
| 67 | + * {value => <h1>{value}</h1>} |
| 68 | + * </Show> |
| 69 | + * ``` |
| 70 | + */ |
| 71 | +export function Show<T>(props: { |
| 72 | + /** |
| 73 | + * The condition to watch. |
| 74 | + */ |
| 75 | + when: Expression<T | Falsy>; |
| 76 | + |
| 77 | + /** |
| 78 | + * The component to render when the condition result is truthy. |
| 79 | + */ |
| 80 | + children: Component<T>; |
| 81 | + |
| 82 | + /** |
| 83 | + * An optional component to render when the condition result is falsy. |
| 84 | + */ |
| 85 | + else?: Component; |
| 86 | +}): View { |
| 87 | + return when(props.when, props.children, props.else); |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Render content for each unique value in an iterable. |
| 92 | + * |
| 93 | + * If an error is thrown while iterating or while rendering an item, the update is stopped as if the previous item was the last one and the error is re-thrown. |
| 94 | + * |
| 95 | + * See {@link forEach} when not using JSX. |
| 96 | + * |
| 97 | + * @example |
| 98 | + * ```tsx |
| 99 | + * import { $, For } from "rvx"; |
| 100 | + * |
| 101 | + * const items = $([1, 2, 3]); |
| 102 | + * |
| 103 | + * <For each={items}> |
| 104 | + * {value => <li>{value}</li>} |
| 105 | + * </For> |
| 106 | + * ``` |
| 107 | + */ |
| 108 | +export function For<T>(props: { |
| 109 | + /** |
| 110 | + * The expression to watch. Note, that signals accessed during iteration will also trigger updates. |
| 111 | + */ |
| 112 | + each: Expression<Iterable<T>>; |
| 113 | + |
| 114 | + /** |
| 115 | + * The component to render for each unique value. |
| 116 | + */ |
| 117 | + children: ForContentFn<T>; |
| 118 | +}): View { |
| 119 | + return forEach(props.each, props.children); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Render content for each value in an iterable, keyed by index and value. |
| 124 | + * |
| 125 | + * If an error is thrown by iterating or by rendering an item, the update is stopped as if the previous item was the last one and the error is re-thrown. |
| 126 | + * |
| 127 | + * See {@link indexEach} when not using JSX. |
| 128 | + * |
| 129 | + * @example |
| 130 | + * ```tsx |
| 131 | + * import { $, Index } from "rvx"; |
| 132 | + * |
| 133 | + * const items = $([1, 2, 3]); |
| 134 | + * |
| 135 | + * <Index each={items}> |
| 136 | + * {value => <li>{value}</li>} |
| 137 | + * </Index> |
| 138 | + * ``` |
| 139 | + */ |
| 140 | +export function Index<T>(props: { |
| 141 | + /** |
| 142 | + * The expression to watch.. |
| 143 | + * |
| 144 | + * Note, that signals accessed during iteration will also trigger updates. |
| 145 | + */ |
| 146 | + each: Expression<Iterable<T>>; |
| 147 | + |
| 148 | + /** |
| 149 | + * The component to render for each value/index pair. |
| 150 | + */ |
| 151 | + children: IndexContentFn<T>; |
| 152 | +}): View { |
| 153 | + return indexEach(props.each, props.children); |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * Attach or detach content depending on an expression. |
| 158 | + * |
| 159 | + * Content is kept alive when detached. |
| 160 | + * |
| 161 | + * See {@link attachWhen} when not using JSX. |
| 162 | + * |
| 163 | + * @example |
| 164 | + * ```tsx |
| 165 | + * import { $, Attach } from "rvx"; |
| 166 | + * |
| 167 | + * const showMessage = $(true); |
| 168 | + * |
| 169 | + * <Attach when={showMessage}> |
| 170 | + * <h1>Hello World!</h1> |
| 171 | + * </Attach> |
| 172 | + * ``` |
| 173 | + */ |
| 174 | +export function Attach(props: { |
| 175 | + /** |
| 176 | + * The condition to watch. |
| 177 | + */ |
| 178 | + when: Expression<boolean>; |
| 179 | + |
| 180 | + /** |
| 181 | + * The content to attach when the condition result is truthy. |
| 182 | + */ |
| 183 | + children?: Content; |
| 184 | +}): View { |
| 185 | + return attachWhen(props.when, props.children); |
| 186 | +} |
0 commit comments