docs/md/api.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
## API The core of the H3 API is comprised of the following six methods and two properties. ### h3(selector: string, attributes: object, children: array) The `h3` object is also used as a constructor for Virtual DOM Nodes (VNodes). It can actually take from one to three arguments used to configure the resulting node. The best way to understand how it works is by providing a few different examples. Please note that in each example the corresponding *HTML* markup is provided, although such markup will actually be generated when the Virtual Node is rendered/redrawn. #### Create an element, with an ID, classes, attributes and children This is a complete example showing how to create a link with an `href` attribute, an ID, two classes, and three child nodes. ```js h3("a#test-link.btn.primary", { href: "#/test" }, ["This is a ", h3("em", "test"), "link."]); ``` ↓ ```html <a id="test-link" class="btn primary" href="#/test"> This is a <em>test</em> link. </a> ``` #### Create an empty element ```js h3("div"); ``` ↓ ```html <div></div> ``` #### Create an element with a textual child node ```js h3("span", "This is a test"); ``` ↓ ```html <span>This is a test</span> ``` #### Create an element with child nodes ```js h3("ol", [ h3("li", "Do this first."), h3("li", "Then this."), h3("li", "And finally this.") ]); ``` ↓ ```html <ol> <li>Do this first.</li> <li>Then this.</li> <li>And finally this.</li> </ol> ``` #### Render a component ```js const TestComponent = () => { return h3("button.test", { onclick: () => alert("Hello!") }, "Show Alert"); }; h3(TestComponent); ``` ↓ ```html <button class="test">Show Alert</button> ``` Note: The event listener will not be added to the markup. #### Render child components ```js const TestLi = (text) => h3("li.test", text); h3("ul", ["A", "B", "C"].map(TestLi)); ``` ↓ ```html <ul> <li class="test">A</li> <li class="test">B</li> <li class="test">C</li> </ul> ``` #### Special attributes * Any attribute starting with *on* (e.g. onclick, onkeydown, etc.) will be treated as an event listener. * The `classList` attribute can be set to a list of classes to apply to the element (as an alternative to using the element selector shorthand). * The `data` attribute can be set to a simple object containing [data attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes). * The special `$key` attribute can be used to guarantee the uniqueness of two VNodes and it will not be translated into an HTML attribute. * The special `$html` attribute can be used to set the `innerHTML` property of the resulting HTML element. Use only if you know what you are doing! ### h3.dispatch(message: string, data: any) Dispatches a message and optionally some data. Messages are typically handled centrally by modules. ```js h3.dispatch("settings/set", { logging: true }); ``` A message name can be anything, but keep in mind that the following names (and typically any name starting with `$`) are reserved for framework use: * `$init` — Dispatched when the application is initialized. Useful to initialize application state. * `$redraw` — Dispatched after an application redraw is triggered. * `$navigation` — Dispatched after a navigation occurs. * `$log` — Dispatched after *any* message (except `$log` iself) is dispatched. Very useful for debugging. ### h3.init(config: object) The initialization method of every H3 application. You _must_ call this method once to initialize your application by providing a component to render or configuration object with the following properties: * **element** (Element) — The DOM Element to which the Application will be attached (default: `document.body`). * **modules** (Array) — An array of functions used to handle the application state that will be executed once before starting the application. * **routes** (Object) — An object containing routing definitions, using paths as keys and components as values. Routing paths can contain named parts like `:name` or `:id` which will populate the `parts` property of the current route (`h3.route`). * **preStart** (Function) — An optional function to be executed before the application is first rendered. * **postStart** (Function) — An optional function to be executed after the application is first rendered. This is an example of a simple routing configuration: ```js const routes = { "/posts/:id": Post, "/pages/:id": Page, "/": HomePage, }; ``` For more a complete example of initialization, see [this](https://h3.js.org/example/assets/js/app.js). ### h3.navigateTo(path: string, params: object) Navigates to the specified path. Optionally, it is possibile to specify query string parameters as an object. The following call causes the application to switch to the following URL: `#/posts/?orderBy=date&direction=desc`. ```js h3.navigateTo("/posts/", {orderBy: 'date', direction: 'desc'}); ``` ### h3.on(message: string, handler: function) Subscribes to the specified message and executes the specified handler function whenever the message is dispatches. Returns a function that can be used to delete the subscription. Subscriptions should be typically managed in modules rather than in components: a component gets rendered several times and subscriptions *must* be properly cleaned up to avoid memory leaks. Example: ```js const pages = (store) => { store.on("$init", () => ({ pagesize: 10, page: 1 })); store.on("pages/set", (state, page) => ({ page })); }; ``` ### h3.redraw() Triggers an application redraw. Unlike most frameworks, in H3 redraws *must* be triggered explicitly. Just call this method whenever you want something to change and components to re-render. ### h3.route An read-only property containing current route (Route object). A Route object has the following properties: * **path** — The current path (fragment without #) without query string parameters, e.g. `/posts/134` * **def** — The matching route definition, e.g. `/posts/:id` * **query** — The query string, if present, e.g. `?comments=yes` * **part** — An object containing the values of the parts defined in the route, e.g. `{id: "134"}` * **params** — An object containing the query string parameters, e.g. `{comments: "yet"}` ### h3.state A read-only property containing the current application state. The state is a plain object, but its properties should only be modified using message subscription handlers. |