Updated the docs

This commit is contained in:
2024-12-24 14:40:48 +01:00
parent 90c308e949
commit 60fb444bc1
4 changed files with 73 additions and 74 deletions
+6 -4
View File
@@ -14,10 +14,11 @@ cargo add osui
use osui::prelude::*; use osui::prelude::*;
fn main() { fn main() {
launch!(app()); launch!(App);
} }
fn app() -> Element { #[component]
fn App() -> Element {
rsx! { rsx! {
text { "Hello, World!" } text { "Hello, World!" }
} }
@@ -30,10 +31,11 @@ This app uses state management and a function using the state as a dependent
use osui::prelude::*; use osui::prelude::*;
fn main() { fn main() {
launch!(app()); launch!(App);
} }
fn app() -> Element { #[component]
fn App() -> Element {
let count = State::new(0); let count = State::new(0);
rsx! { rsx! {
+13 -17
View File
@@ -4,7 +4,8 @@ title: RSX
This macro is the recommended way to describe a ui, it is a div that holds multiple elements This macro is the recommended way to describe a ui, it is a div that holds multiple elements
```rust ```rust
fn app() -> Element { #[component]
fn App() -> Element {
rsx! { rsx! {
button { "click me" } button { "click me" }
text { "This is some text" } text { "This is some text" }
@@ -25,24 +26,22 @@ fn app() -> Element {
## Child values ## Child values
If you want to add more children to values then just simply express it like a `rsx!` If you want to add more children to values then just simply express it like a `rsx!`
```rust ```rust
fn app() -> Element {
let x = 69; let x = 69;
rsx! { rsx! {
div { div {
text { "the number is {x}, Nice!" } text { "the number is {x}, Nice!" }
} }
} }
}
``` ```
## Formatted text ## Formatted text
```rust ```rust
fn app() -> Element {
let x = 69; let x = 69;
rsx! { rsx! {
text { "the number is {x}, Nice!" } text { "the number is {x}, Nice!" }
} }
}
``` ```
> :::info You can also do expressions after the string > :::info You can also do expressions after the string
```rust ```rust
@@ -51,29 +50,24 @@ text { "the number is {}, Nice!", x }
## For loops ## For loops
```rust ```rust
fn app() -> Element {
rsx! { rsx! {
for (i in 0..5) { // () are required for (i in 0..5) { // () are required
text { "{i}" } text { "{i}" }
} }
} }
}
``` ```
## Attribute ## Attribute
You can change the element's struct fields by just providing some attributes before the children You can change the element's struct fields by just providing some attributes before the children
```rust ```rust
fn app() -> Element {
rsx! { rsx! {
text { class: "my_text" } text { class: "my_text" }
} }
}
``` ```
## Handler functions ## Handler functions
Instead of closures which are tricky to work with on structs, We use `Handler`, It's basically a closure wrapped with a `Arc<Mutex<T>>` Instead of closures which are tricky to work with on structs, We use `Handler`, It's basically a closure wrapped with a `Arc<Mutex<T>>`
```rust ```rust
fn app() -> Element {
rsx! { rsx! {
button { button {
on_click: fn(btn: &mut Button, event, document) { on_click: fn(btn: &mut Button, event, document) {
@@ -81,13 +75,11 @@ fn app() -> Element {
} }
} }
} }
}
``` ```
## Handler functions (with dependents) ## Handler functions (with dependents)
If you have a `State<T>` and want to use it, you have to put a @ before the code block like this If you have a `State<T>` and want to use it, you have to put a @ before the code block like this
```rust ```rust
fn app() -> Element {
let count = State::new(); let count = State::new();
rsx! { rsx! {
@@ -98,14 +90,12 @@ fn app() -> Element {
"Current count: {count}" "Current count: {count}"
} }
} }
}
``` ```
> :::info you can add multiple decedents by separating them via `,` like: @var1, var2, var3 > :::info you can add multiple decedents by separating them via `,` like: @var1, var2, var3
## Static text ## Static text
If your text doesn't use variables that will change you can put `static` right before the text to signal that it's static. If your text doesn't use variables that will change you can put `static` right before the text to signal that it's static.
```rust ```rust
fn app() -> Element {
let count = 69; // count won't change since it's not mutable let count = 69; // count won't change since it's not mutable
rsx! { rsx! {
@@ -113,13 +103,11 @@ fn app() -> Element {
static "Current count: {count}" static "Current count: {count}"
} }
} }
}
``` ```
## Instructions ## Instructions
Add an instruction by putting a `@` before it, like this: Add an instruction by putting a `@` before it, like this:
```rust ```rust
pub fn app() -> Element {
rsx! { rsx! {
@SetStyle(css! { // the SetStyle instruction @SetStyle(css! { // the SetStyle instruction
"title": { "title": {
@@ -129,5 +117,13 @@ pub fn app() -> Element {
text { class: "title", static "Hello world!" } text { class: "title", static "Hello world!" }
} }
}
``` ```
# Ghost elements
With a `div` you can use tab (or shift+tab) to go through components, But sometimes an element doesn't really do anything, So to skip over the element you can use the ghost syntax `%`. This makes it so that the element is not selectable.
```rust
rsx! {
%text { "This is a ghost title!!!!" }
button { "Click me!" }
}
+2 -1
View File
@@ -24,7 +24,8 @@ rsx! {
### With dependents ### With dependents
If you have a `State<T>` and want to use it in a `fn()`, Pub a @ before the code block like this If you have a `State<T>` and want to use it in a `fn()`, Pub a @ before the code block like this
```rust ```rust
pub fn app() -> Element { #[component]
pub fn App() -> Element {
let count = State::new(0); let count = State::new(0);
rsx! { rsx! {
button { button {
+13 -13
View File
@@ -7,25 +7,25 @@ Along with the `Css` type, This macro provides styling for multiple elements by
## Example ## Example
In this case we will make a button with the classname `my_btn`. And set `styling` In this case we will make a button with the classname `my_btn`. And set `styling`
```rust ```rust
fn app() -> Element { #[component]
fn App() -> Element {
rsx! { rsx! {
button { class: "my_btn", "Click me!" } @SetStyle(css! {
blue-outline {
outline: true,
outline_color: Blue,
} }
}
```
## Style by classname red {
```rust
fn styles() -> Css {
css! {
"my_btn" { // style by classname
color: Red, color: Red,
} }
// Style by state green-hover: "hover" {
"my_btn": "clicked" { // When the element state is clicked color: Green,
color: Blue,
} }
})
button { class: "red blue-outline green-hover", "Click me!" }
} }
} }
``` ```
@@ -33,7 +33,7 @@ fn styles() -> Css {
## `hover` ## `hover`
The `hover` state is on every element but only if it's focused/hovered. The `hover` state is on every element but only if it's focused/hovered.
```rust ```rust
"my_btn": "clicked" { "my_btn": "hover" {
color: Green, color: Green,
} }
``` ```