11import type { ElementEventHandlers } from "./event-handlers.ts" ;
22
3- /*
4- Examples of React jsdoc comment types that clearly states what the API does
5- /**
6- * Created by {@link createRef }, or {@link useRef} when passed `null`.
7- *
8- * @template T The type of the ref's value.
9- *
10- * @example
11- *
12- * ```tsx
13- * const ref = createRef<HTMLDivElement>();
14- *
15- * ref.current = document.createElement('div'); // Error
16- * ```
17- */
18- // interface RefObject<T> {
19- // /**
20- // * The current value of the ref.
21- //
22- // current: T;
23- // }
24- // */
25- //
26- //
27- // // This will technically work if you give a Consumer<T> or Provider<T> but it's deprecated and warns
28- // /**
29- // * Accepts a context object (the value returned from `React.createContext`) and returns the current
30- // * context value, as given by the nearest context provider for the given context.
31- // *
32- // * @version 16.8.0
33- // * @see {@link https://react.dev/reference/react/useContext }
34- // */
35- // function useContext<T>(context: Context<T> /*, (not public API) observedBits?: number|boolean */): T;
36- // /**
37- // * Returns a stateful value, and a function to update it.
38- // *
39- // * @version 16.8.0
40- // * @see {@link https://react.dev/reference/react/useState }
41- // */
42- // function useState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>];
43- //
44-
453export type {
464 ElementEventHandlers ,
475 ReactiveEventHandler ,
@@ -56,34 +14,29 @@ export type {
5614 *
5715 * ```ts
5816 * const count = van.state(0);
59- *
60- * count.val; // read — tracked as dependency
61- * count.val = 5; // write — triggers reactive updates
62- * count.oldVal; // previous value before last update
63- * count.rawVal; // raw value, no dependency tracking
17+ * count.val = 5; // triggers reactive updates
6418 * ```
6519 */
6620export interface State < T > {
6721 /**
68- * The current value of the state. Reading this property inside a binding or derive
69- * tracks it as a dependency. Writing to this property triggers reactive updates.
22+ * The current value of the state. Reading tracks it as a dependency, writing triggers updates.
7023 */
7124 val : T ;
7225
7326 /**
74- * The previous value before the last update. Reading this property tracks it as a dependency.
27+ * The previous value before the last update.
7528 */
7629 readonly oldVal : T ;
7730
7831 /**
79- * The raw value without dependency tracking. Use this to read the value without
80- * creating a reactive dependency.
32+ * The raw value without dependency tracking.
8133 */
8234 readonly rawVal : T ;
8335}
8436
85- // Defining readonly view of State<T> for covariance.
86- // Basically we want StateView<string> to implement StateView<string | number>
37+ /**
38+ * Readonly view of State<T> for covariance.
39+ */
8740export type StateView < T > = Readonly < State < T > > ;
8841
8942export type Val < T > = State < T > | T ;
@@ -110,6 +63,11 @@ export type PropsWithKnownKeys<ElementType> = Partial<{
11063 : K ] : PropValueOrDerived ;
11164} > ;
11265
66+ /**
67+ * A mutable ref object that holds a current value.
68+ *
69+ * @template T The type of the ref's value.
70+ */
11371export type Ref < T > = { current : T | null } ;
11472
11573export type RefProp < T > = { ref ?: Ref < T > } ;
@@ -137,43 +95,84 @@ export type TagFunc<Result extends Element> = (
13795 ...rest : readonly ChildDom [ ]
13896) => Result ;
13997
140- // HTML Tags - typed for all known HTML elements
98+ /**
99+ * HTML tag functions typed for all known HTML elements.
100+ */
141101type HTMLTags = Readonly < Record < string , TagFunc < Element > > > & {
142102 [ K in keyof HTMLElementTagNameMap ] : TagFunc < HTMLElementTagNameMap [ K ] > ;
143103} ;
144104
145- // SVG Tags - typed for all known SVG elements
105+ /**
106+ * SVG tag functions typed for all known SVG elements.
107+ */
146108type SVGTags = Readonly < Record < string , TagFunc < SVGElement > > > & {
147109 [ K in keyof SVGElementTagNameMap ] : TagFunc < SVGElementTagNameMap [ K ] > ;
148110} ;
149111
150- // MathML Tags - typed for all known MathML elements
112+ /**
113+ * MathML tag functions typed for all known MathML elements.
114+ */
151115type MathMLTags = Readonly < Record < string , TagFunc < MathMLElement > > > & {
152116 [ K in keyof MathMLElementTagNameMap ] : TagFunc < MathMLElementTagNameMap [ K ] > ;
153117} ;
154118
155- // Namespace URIs
156119type SVGNamespaceURI = "http://www.w3.org/2000/svg" ;
157120type MathMLNamespaceURI = "http://www.w3.org/1998/Math/MathML" ;
158121
159- // Namespace function overloads
122+ /**
123+ * Creates tag functions for elements in a specific namespace.
124+ */
160125type NamespacedTags = {
161126 ( namespaceURI : SVGNamespaceURI ) : SVGTags ;
162127 ( namespaceURI : MathMLNamespaceURI ) : MathMLTags ;
163128 ( namespaceURI : string ) : Readonly < Record < string , TagFunc < Element > > > ;
164129} ;
165130
131+ /**
132+ * Creates a reactive state object.
133+ *
134+ * @template T The type of the state's value.
135+ *
136+ * @example
137+ *
138+ * ```ts
139+ * const count = van.state(0);
140+ * const name = van.state("Alice");
141+ * ```
142+ */
166143declare function state < T > ( ) : State < T > ;
167144declare function state < T > ( initVal : T ) : State < T > ;
168145
146+ /**
147+ * The main VanJS interface.
148+ */
169149export interface Van {
150+ /**
151+ * Creates a reactive state object.
152+ */
170153 readonly state : typeof state ;
154+
155+ /**
156+ * Creates derived state from a function.
157+ */
171158 readonly derive : < T > ( f : ( ) => T ) => State < T > ;
159+
160+ /**
161+ * Adds child elements to a DOM node.
162+ */
172163 readonly add : (
173164 dom : Element | DocumentFragment ,
174165 ...children : readonly ChildDom [ ]
175166 ) => Element ;
167+
168+ /**
169+ * Tag functions for creating HTML, SVG, and MathML elements.
170+ */
176171 readonly tags : HTMLTags & NamespacedTags ;
172+
173+ /**
174+ * Hydrates existing DOM with VanJS reactivity.
175+ */
177176 readonly hydrate : < T extends Node > (
178177 dom : T ,
179178 f : ( dom : T ) => T | null | undefined
0 commit comments