48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
export {}
|
|
declare global
|
|
{
|
|
namespace Van {
|
|
|
|
interface State<T> {
|
|
val: T
|
|
readonly oldVal: T
|
|
readonly rawVal: T
|
|
}
|
|
|
|
// Defining readonly view of State<T> for covariance.
|
|
// Basically we want StateView<string> to implement StateView<string | number>
|
|
type StateView<T> = Readonly<State<T>>
|
|
|
|
type Val<T> = State<T> | T
|
|
|
|
type Primitive = string | number | boolean | bigint
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
type PropValue = Primitive | ((e: any) => void) | null
|
|
|
|
type PropValueOrDerived = PropValue | StateView<PropValue> | (() => PropValue)
|
|
|
|
type Props = Record<string, PropValueOrDerived> & { class?: PropValueOrDerived; is?: string }
|
|
|
|
type PropsWithKnownKeys<ElementType> = Partial<{[K in keyof ElementType]: PropValueOrDerived}>
|
|
|
|
type ValidChildDomValue = Primitive | Node | null | undefined
|
|
|
|
type BindingFunc = ((dom?: Node) => ValidChildDomValue) | ((dom?: Element) => Element)
|
|
|
|
type ChildDom = ValidChildDomValue | StateView<Primitive | null | undefined> | BindingFunc | readonly ChildDom[]
|
|
|
|
type TagFunc<Result> = (first?: Props & PropsWithKnownKeys<Result> | ChildDom, ...rest: readonly ChildDom[]) => Result
|
|
|
|
type Tags = Readonly<Record<string, TagFunc<Element>>> & {
|
|
[K in keyof HTMLElementTagNameMap]: TagFunc<HTMLElementTagNameMap[K]>
|
|
}
|
|
}
|
|
const van:{
|
|
readonly state: <T>(initVal: T, HMRKey?:string)=> Van.State<T>
|
|
readonly derive: <T>(f: () => T) => Van.State<T>
|
|
readonly add: (dom: Element, ...children: readonly Van.ChildDom[]) => Element
|
|
readonly tags: Van.Tags & ((namespaceURI: string) => Readonly<Record<string, Van.TagFunc<Element>>>)
|
|
readonly hydrate: <T extends Node>(dom: T, f: (dom: T) => T | null | undefined) => T
|
|
};
|
|
} |