UNPKG

2.58 kBTypeScriptView Raw
1export as namespace hyperapp
2
3/** @namespace [VDOM] */
4
5/** The VDOM representation of an Element.
6 *
7 * @memberOf [VDOM]
8 */
9export interface VNode<Attributes = {}> {
10 nodeName: string
11 attributes?: Attributes
12 children: Array<VNode | string>
13 key: string
14}
15
16/** A Component is a function that returns a custom VNode or View.
17 *
18 * @memberOf [VDOM]
19 */
20export interface Component<Attributes = {}, State = {}, Actions = {}> {
21 (attributes: Attributes, children: Array<VNode | string>):
22 | VNode<Attributes>
23 | View<State, Actions>
24}
25
26/**
27 * Possibles children types
28 */
29export type Children = VNode | string | number | null
30
31/** The soft way to create a VNode.
32 * @param name An element name or a Component function
33 * @param attributes Any valid HTML atributes, events, styles, and meta data
34 * @param children The children of the VNode
35 * @returns A VNode tree.
36 *
37 * @memberOf [VDOM]
38 */
39export function h<Attributes>(
40 nodeName: Component<Attributes, any, any> | string,
41 attributes?: Attributes,
42 ...children: Array<Children | Children[]>
43): VNode<Attributes>
44
45/** @namespace [App] */
46
47/** The result of an action.
48 *
49 * @memberOf [App]
50 */
51export type ActionResult<State> = Partial<State> | Promise<any> | null | void
52
53/** The interface for a single action implementation.
54 *
55 * @memberOf [App]
56 */
57export type ActionType<State, Actions> = (
58 data?: any
59) =>
60 | ((state: State, actions: Actions) => ActionResult<State>)
61 | ActionResult<State>
62
63/** The interface for the actions tree implementation.
64 *
65 * @memberOf [App]
66 */
67export type ActionsType<State, Actions> = {
68 [P in keyof Actions]:
69 | ActionType<State, Actions>
70 | ActionsType<any, Actions[P]>
71}
72
73/** The view function describes the application UI as a tree of VNodes.
74 * @returns A VNode tree.
75 * @memberOf [App]
76 */
77export interface View<State, Actions> {
78 (state: State, actions: Actions): VNode<object>
79}
80
81/** The app() call creates and renders a new application.
82 *
83 * @param state The state object.
84 * @param actions The actions object implementation.
85 * @param view The view function.
86 * @param container The DOM element where the app will be rendered to.
87 * @returns The actions wired to the application.
88 * @memberOf [App]
89 */
90export function app<State, Actions>(
91 state: State,
92 actions: ActionsType<State, Actions>,
93 view: View<State, Actions>,
94 container: Element | null
95): Actions
96
97/** @namespace [JSX] */
98
99declare global {
100 namespace JSX {
101 interface Element extends VNode<any> {}
102 interface IntrinsicElements {
103 [elemName: string]: any
104 }
105 }
106}