Skip to content

Commit

Permalink
the when doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ECorreia45 committed Oct 19, 2024
1 parent d530f64 commit 8a1501d
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/documentation/utilities/when.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,35 @@ layout: document
---

## When Utility

The `when` helper is Markup out of the box utility to do conditional rendering in or outside templates.

It mimics an if-and-else statement with the else being conditional.

```javascript
const visible = true

html` <p>${when(visible, `visible`, `hidden`)}</p> `
```

### Condition

The condition is the first argument and it can be a static value or a function for something that will change like a [StateGetter](../state/index.md#stategetter).

```javascript
const [visible, updateVisible] = state(true)

html` ${when(visible, html`<p>visible</p>`, html`<p>hidden</p>`)} `
```

The `when` helper will re-evaluate whenever the condition changes for an accurate render.

### Then & else

The second argument is required and represent the "then" value while the third argument is optional since it represent the "else" body.

```javascript
const [visible, updateVisible] = state(true)

html`${when(visible, html`<p>visible</p>`)}`
```

0 comments on commit 8a1501d

Please sign in to comment.