|
5 | 5 | - [Conditional VDOM](#conditional-vdom) |
6 | 6 | - [Collections](#collections) |
7 | 7 | - [Custom VDOM](#custom-vdom) |
| 8 | +- [Types](#types) |
8 | 9 | - [Cheatsheet](#cheatsheet) |
9 | 10 |
|
10 | 11 | ## Basics |
11 | 12 |
|
12 | | -It's important to understand the [VDOM types](TYPES.md#vdom) too so have a read. |
13 | | - |
14 | 13 | There are two ways of creating virtual-DOM. |
15 | 14 |
|
16 | 15 | 1. **Prefixed (recommended)** - Importing DOM tags and attributes under prefixes is recommended. |
@@ -245,6 +244,38 @@ customTag(customAttr := "hello", customStyle := "123", "bye") |
245 | 244 | In addition to `HtmlTag(…)`, there is also `SvgTag(…)`, `HtmlTagTo[N](…)`, `SvgTagTo[N](…)`. |
246 | 245 |
|
247 | 246 |
|
| 247 | +## Types |
| 248 | + |
| 249 | +The most important types are probably `TagMod` and `VdomElement`. |
| 250 | + |
| 251 | +| Type | Explaination | |
| 252 | +| ---- | ---- | |
| 253 | +| `VdomElement` | A single VDOM tag. <br> This can be a tag like `<div>`, or a rendered component. This is also the result of components' `.render` methods. | |
| 254 | +| `VdomNode` | A single piece of VDOM. <br> Can be a `VdomElement`, or a piece of text, a number, etc. | |
| 255 | +| `VdomArray` | An array of VDOM nodes. <br> This is passed to React as an array which helps Reacts diff'ing mechanism. React also requires that each array element have a key. | |
| 256 | +| `VdomAttr` | A tag attribute (including styles). <br> Examples: `href`, `value`, `onClick`, `margin`. | |
| 257 | +| `VdomTagOf[Node]` | A HTML or SVG tag of type `Node`. | |
| 258 | +| `VdomTag` | A HTML or SVG tag. | |
| 259 | +| `HtmlTagOf[Node]` | A HTML tag of type `Node`. | |
| 260 | +| `HtmlTag` | A HTML tag. | |
| 261 | +| `SvgTagOf[Node]` | An SVG tag of type `Node`. | |
| 262 | +| `SvgTag` | An SVG tag. | |
| 263 | +| `TagMod` | Tag-Modifier. A modification to a `VdomTag`. <br> All of the types here can be a `TagMod` because they can all be used to modify a `VdomTag`. <br> This is ***very useful*** for reuse and abstraction in practice, very useful for separating DOM functionality, asthetics and content. <br> For example, it allows a function to return a child tag, a style and some event handlers which the function caller can then apply to some external tag. | |
| 264 | + |
| 265 | +Examples: |
| 266 | +```scala |
| 267 | +import japgolly.scalajs.react.vdom.all._ |
| 268 | + |
| 269 | +val tag1 : VdomTag = input(className := "name") |
| 270 | +val mod1 : TagMod = value := "Bob" |
| 271 | +val mod2 : TagMod = TagMod(mod1, `type` := "text", title := "hello!") |
| 272 | +val tag2 : VdomTag = tag1(mod2, readOnly := true) |
| 273 | +val element: VdomElement = tag2 |
| 274 | +// equivalent to |
| 275 | +// <input class="name" value="Bob" type="text", title := "hello!" readonly=true /> |
| 276 | +``` |
| 277 | + |
| 278 | + |
248 | 279 | ## Cheatsheet |
249 | 280 |
|
250 | 281 | | Category | Expressions | Result Type | |
|
0 commit comments