Skip to content

Commit

Permalink
Demonstrate wrapper component
Browse files Browse the repository at this point in the history
  • Loading branch information
kmicklas committed Jun 17, 2024
1 parent bbc1101 commit 67cd570
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export RUST_BACKTRACE=1
export RUSTC_BOOTSTRAP=1
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:

env:
CARGO_TERM_COLOR: always
RUSTC_BOOTSTRAP: 1

jobs:
build:
Expand Down
59 changes: 38 additions & 21 deletions examples/tutorial/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(type_alias_impl_trait)]
use std::collections::BTreeMap;

use ravel::with;
Expand All @@ -9,7 +10,7 @@ use ravel_web::{
format_text,
run::spawn_body,
text::{display, text},
View,
State, View, ViewMarker,
};
use web_sys::{
wasm_bindgen::{JsCast as _, UnwrapThrowExt},
Expand Down Expand Up @@ -52,31 +53,47 @@ fn basic_html() -> View!(Model) {
))
}

type SectionState<S: ViewMarker + State<Model>> =
impl ViewMarker + State<Model>;

fn section<S, Body>(
title: &'static str,
body: Body,
) -> impl View<ViewState = SectionState<S>>
where
S: ViewMarker + State<Model>,
Body: View<ViewState = S>,
{
el::section((el::h2(title), body))
}

/// Components can take data in parameters, which can be borrowed from shared
/// state such as our [`Model`]. When using borrowed data, we need to add an
/// appropriate bound to the return type for the captured lifetime (here `'_`).
fn state(model: &Model) -> View!(Model, '_) {
(
el::h2("State"),
el::p(
// To generate strings dynamically, we can use standard format
// strings using [`format_text`].
format_text!("Count: {}", model.count),
section(
"State",
(
el::p(
// To generate strings dynamically, we can use standard format
// strings using [`format_text`].
format_text!("Count: {}", model.count),
),
el::p((
"Also count: ",
// In the very common case of just displaying a scalar value like a
// number, it is easier and more efficient to use [`display`].
display(model.count),
)),
el::p((
"Message: ",
// Previously, we only genereted static strings, which can be used
// directly. This is also possible for a by-value [`String`].
//
// However, for any other string-like type, we need to use [`text`].
text(&model.message),
)),
),
el::p((
"Also count: ",
// In the very common case of just displaying a scalar value like a
// number, it is easier and more efficient to use [`display`].
display(model.count),
)),
el::p((
"Message: ",
// Previously, we only genereted static strings, which can be used
// directly. This is also possible for a by-value [`String`].
//
// However, for any other string-like type, we need to use [`text`].
text(&model.message),
)),
)
}

Expand Down

0 comments on commit 67cd570

Please sign in to comment.