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