Skip to content

Commit

Permalink
Clarify application of code in Dynamic Classes section (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
gihrig authored Dec 19, 2023
1 parent 76ad52e commit 464773c
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/view/02_dynamic_attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,17 @@ reactively update when the signal changes.
Now every time I click the button, the text should toggle between red and black as
the number switches between even and odd.

Some CSS class names can’t be directly parsed by the `view` macro, especially if they include a mix of dashes and numbers or other characters. In that case, you can use a tuple syntax: `class=("name", value)` still directly updates a single class.

```rust
class=("button-20", move || count() % 2 == 1)
<button
on:click=move |_| {
set_count.update(|n| *n += 1);
}
// the class: syntax reactively updates a single class
// here, we'll set the `red` class when `count` is odd
class:red=move || count() % 2 == 1
>
"Click me"
</button>
```

> If you’re following along, make sure you go into your `index.html` and add something like this:
Expand All @@ -68,6 +75,12 @@ class=("button-20", move || count() % 2 == 1)
> </style>
> ```
Some CSS class names can’t be directly parsed by the `view` macro, especially if they include a mix of dashes and numbers or other characters. In that case, you can use a tuple syntax: `class=("name", value)` still directly updates a single class.
```rust
class=("button-20", move || count() % 2 == 1)
```
## Dynamic Styles

Individual CSS properties can be directly updated with a similar `style:` syntax.
Expand Down Expand Up @@ -172,7 +185,7 @@ for expensive calculations.
<iframe src="https://codesandbox.io/p/sandbox/2-dynamic-attributes-0-5-lwdrpm?file=%2Fsrc%2Fmain.rs%3A1%2C1" width="100%" height="1000px" style="max-height: 100vh"></iframe>
<details>
<summary>Code Sandbox Source</summary>
<summary>CodeSandbox Source</summary>
```rust
use leptos::*;
Expand Down

0 comments on commit 464773c

Please sign in to comment.