|
| 1 | +export interface State<T> extends StateView<T> { |
| 2 | + val: T |
| 3 | +} |
| 4 | + |
| 5 | +// Defining readonly view of State<T> for covariance. |
| 6 | +// Basically we want StateView<string> to implement StateView<string | number> |
| 7 | +export interface StateView<T> { |
| 8 | + readonly val: T |
| 9 | + readonly oldVal: T |
| 10 | +} |
| 11 | + |
| 12 | +export type Primitive = string | number | boolean | bigint |
| 13 | + |
| 14 | +export type PropValue = Primitive | ((e: any) => void) | null |
| 15 | + |
| 16 | +export interface Props { |
| 17 | + readonly [key: string]: PropValue | StateView<PropValue> | (() => PropValue) |
| 18 | +} |
| 19 | + |
| 20 | +export type ValidChildDomValue = Primitive | Node | null | undefined |
| 21 | + |
| 22 | +export type BindingFunc = (dom: Node) => ValidChildDomValue |
| 23 | + |
| 24 | +export type ChildDom = ValidChildDomValue | StateView<ValidChildDomValue> | BindingFunc | readonly ChildDom[] |
| 25 | + |
| 26 | +export type TagFunc<Result> = (first?: Props | ChildDom, ...rest: readonly ChildDom[]) => Result |
| 27 | + |
| 28 | +interface TagsBase { |
| 29 | + readonly [key: string]: TagFunc<Element> |
| 30 | +} |
| 31 | + |
| 32 | +interface Tags extends TagsBase { |
| 33 | + // Register known element types |
| 34 | + // Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element |
| 35 | + |
| 36 | + // Main root |
| 37 | + readonly html: TagFunc<HTMLHtmlElement> |
| 38 | + |
| 39 | + // Document metadata |
| 40 | + readonly base: TagFunc<HTMLBaseElement> |
| 41 | + readonly head: TagFunc<HTMLHeadElement> |
| 42 | + readonly link: TagFunc<HTMLLinkElement> |
| 43 | + readonly meta: TagFunc<HTMLMetaElement> |
| 44 | + readonly style: TagFunc<HTMLStyleElement> |
| 45 | + readonly title: TagFunc<HTMLTitleElement> |
| 46 | + |
| 47 | + // Sectioning root |
| 48 | + readonly body: TagFunc<HTMLBodyElement> |
| 49 | + |
| 50 | + // Content sectioning |
| 51 | + readonly h1: TagFunc<HTMLHeadingElement> |
| 52 | + readonly h2: TagFunc<HTMLHeadingElement> |
| 53 | + readonly h3: TagFunc<HTMLHeadingElement> |
| 54 | + readonly h4: TagFunc<HTMLHeadingElement> |
| 55 | + readonly h5: TagFunc<HTMLHeadingElement> |
| 56 | + readonly h6: TagFunc<HTMLHeadingElement> |
| 57 | + |
| 58 | + // Text content |
| 59 | + readonly blockquote: TagFunc<HTMLQuoteElement> |
| 60 | + readonly div: TagFunc<HTMLDivElement> |
| 61 | + readonly dl: TagFunc<HTMLDListElement> |
| 62 | + readonly hr: TagFunc<HTMLHRElement> |
| 63 | + readonly li: TagFunc<HTMLLIElement> |
| 64 | + readonly menu: TagFunc<HTMLMenuElement> |
| 65 | + readonly ol: TagFunc<HTMLOListElement> |
| 66 | + readonly p: TagFunc<HTMLParagraphElement> |
| 67 | + readonly pre: TagFunc<HTMLPreElement> |
| 68 | + readonly ul: TagFunc<HTMLUListElement> |
| 69 | + |
| 70 | + // Inline text semantics |
| 71 | + readonly a: TagFunc<HTMLAnchorElement> |
| 72 | + readonly br: TagFunc<HTMLBRElement> |
| 73 | + readonly data: TagFunc<HTMLDataElement> |
| 74 | + readonly q: TagFunc<HTMLQuoteElement> |
| 75 | + readonly span: TagFunc<HTMLSpanElement> |
| 76 | + readonly time: TagFunc<HTMLTimeElement> |
| 77 | + |
| 78 | + // Image and multimedia |
| 79 | + readonly area: TagFunc<HTMLAreaElement> |
| 80 | + readonly audio: TagFunc<HTMLAudioElement> |
| 81 | + readonly img: TagFunc<HTMLImageElement> |
| 82 | + readonly map: TagFunc<HTMLMapElement> |
| 83 | + readonly track: TagFunc<HTMLTrackElement> |
| 84 | + readonly video: TagFunc<HTMLVideoElement> |
| 85 | + |
| 86 | + // Embedded content |
| 87 | + readonly embed: TagFunc<HTMLEmbedElement> |
| 88 | + readonly iframe: TagFunc<HTMLIFrameElement> |
| 89 | + readonly object: TagFunc<HTMLObjectElement> |
| 90 | + readonly picture: TagFunc<HTMLPictureElement> |
| 91 | + readonly source: TagFunc<HTMLSourceElement> |
| 92 | + |
| 93 | + // Scripting |
| 94 | + readonly canvas: TagFunc<HTMLCanvasElement> |
| 95 | + readonly script: TagFunc<HTMLScriptElement> |
| 96 | + |
| 97 | + // Demarcating edits |
| 98 | + readonly del: TagFunc<HTMLModElement> |
| 99 | + readonly ins: TagFunc<HTMLModElement> |
| 100 | + |
| 101 | + // Table content |
| 102 | + readonly caption: TagFunc<HTMLTableCaptionElement> |
| 103 | + readonly col: TagFunc<HTMLTableColElement> |
| 104 | + readonly colgroup: TagFunc<HTMLTableColElement> |
| 105 | + readonly table: TagFunc<HTMLTableElement> |
| 106 | + readonly tbody: TagFunc<HTMLTableSectionElement> |
| 107 | + readonly td: TagFunc<HTMLTableCellElement> |
| 108 | + readonly tfoot: TagFunc<HTMLTableSectionElement> |
| 109 | + readonly th: TagFunc<HTMLTableCellElement> |
| 110 | + readonly thead: TagFunc<HTMLTableSectionElement> |
| 111 | + readonly tr: TagFunc<HTMLTableRowElement> |
| 112 | + |
| 113 | + // Forms |
| 114 | + readonly button: TagFunc<HTMLButtonElement> |
| 115 | + readonly datalist: TagFunc<HTMLDataListElement> |
| 116 | + readonly fieldset: TagFunc<HTMLFieldSetElement> |
| 117 | + readonly form: TagFunc<HTMLFormElement> |
| 118 | + readonly input: TagFunc<HTMLInputElement> |
| 119 | + readonly label: TagFunc<HTMLLabelElement> |
| 120 | + readonly legend: TagFunc<HTMLLegendElement> |
| 121 | + readonly meter: TagFunc<HTMLMeterElement> |
| 122 | + readonly optgroup: TagFunc<HTMLOptGroupElement> |
| 123 | + readonly option: TagFunc<HTMLOptionElement> |
| 124 | + readonly output: TagFunc<HTMLOutputElement> |
| 125 | + readonly progress: TagFunc<HTMLProgressElement> |
| 126 | + readonly select: TagFunc<HTMLSelectElement> |
| 127 | + readonly textarea: TagFunc<HTMLTextAreaElement> |
| 128 | + |
| 129 | + // Interactive elements |
| 130 | + readonly details: TagFunc<HTMLDetailsElement> |
| 131 | + readonly dialog: TagFunc<HTMLDialogElement> |
| 132 | + |
| 133 | + // Web Components |
| 134 | + readonly slot: TagFunc<HTMLSlotElement> |
| 135 | + readonly template: TagFunc<HTMLTemplateElement> |
| 136 | +} |
| 137 | + |
| 138 | +export interface Van { |
| 139 | + readonly state: <T>(initVal: T) => State<T> |
| 140 | + readonly val: <T>(s: T | StateView<T>) => T |
| 141 | + readonly oldVal: <T>(s: T | StateView<T>) => T |
| 142 | + readonly derive: <T>(f: () => T) => State<T> |
| 143 | + readonly add: (dom: Element, ...children: readonly ChildDom[]) => Element |
| 144 | + readonly _: (f: () => PropValue) => () => PropValue |
| 145 | + readonly tags: Tags |
| 146 | + readonly tagsNS: (namespaceURI: string) => TagsBase |
| 147 | +} |
| 148 | + |
| 149 | +declare const van: Van |
| 150 | + |
| 151 | +export default van |
0 commit comments