Components
A component is a Rust function that returns an element, and a part is a component with its own signals and actions that several pages embed.
A function that returns an element
A component is a Rust function that returns an El<S>. Make it generic over S and take the signals it reads as arguments, and it works on any page. Its signature says what it reads, as fn field<S>(text: &str, value: Sig<S, String>) -> El<S> reads one String signal of whichever page calls it.
use hypersig::html::{El, input, label, span};
use hypersig::{Sig, Signals};
use serde::{Deserialize, Serialize};
/// A labeled text field bound to `value`, on any page.
fn field<S>(text: &str, value: Sig<S, String>) -> El<S> {
label()
.child(span().text(text))
.child(input().bind(value))
}
#[derive(Signals, Serialize, Deserialize)]
struct Signup {
name: String,
email: String,
}
let form = hypersig::html::form::<Signup>()
.child(field("Name", Signup::name()))
.child(field("Email", Signup::email()));The compiler takes S from the signal passed in, so a component given another page's signal returns an element of that page, which child refuses as Elements and attributes shows. A signal of the wrong type, such as a Sig<Signup, bool> for value, is a compile error too. A component that keeps its own state, such as a disclosure that is open or closed, takes a signal to a nested struct, so each place it sits on a page has its own state, as Signals inside signals shows.
hypersig_ui is a set of such functions styled after shadcn/ui. The demos on this site use its buttons and text fields.
Parts shared across pages
A piece that sits on several pages, such as a sign-out button in an app's shell or a newsletter form in a footer, is a Part: its own signal struct, an element built by Part::render, and actions over that struct. The trait is trait Part: Signals { fn render() -> El<Self>; }. A page whose struct has a field of the part's type embeds it with html::part(Home::footer()), and the part writes its paths under that field. Each call made inside the part tells the server the field, so the one handler registered for the part's action serves every page that embeds it, and the signals it patches and the fragments it renders again land under the field the call came from.
use axum::Router;
use hypersig::html::{El, button, form, input, p, part, attr};
use hypersig::{Action, Page, Part, Patches, RouterExt, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize, Default)]
struct Newsletter {
email: String,
joined: bool,
}
// The part's action is a line of the application's block, which registers it once.
hypersig::routes! {
fn pages(Router<()>);
action JOIN: Action<Newsletter, String> = post "/join" => join;
}
impl Part for Newsletter {
fn render() -> El<Self> {
let email = Newsletter::email();
form()
.on_submit(JOIN.call_with(email))
.child(input().bind(email))
.child(button().attr(attr::TYPE, "submit").text("Join"))
.child(p().show(Newsletter::joined()).text("Thanks for joining."))
}
}
async fn join((): (), email: String) -> Patches<Newsletter> {
Newsletter::email()
.patch(email.trim().to_owned())
.and(Newsletter::joined().patch(true))
.into()
}
// Two pages, each with the newsletter at its field `footer`.
#[derive(Signals, Serialize, Deserialize)]
struct Home {
footer: Newsletter,
}
#[derive(Signals, Serialize, Deserialize)]
struct About {
footer: Newsletter,
}
let home = Page::new("Home", Home { footer: Newsletter::default() })
.child(part(Home::footer()));
let about = Page::new("About", About { footer: Newsletter::default() })
.child(part(About::footer()));
// One handler answers the form on both pages.
let app: Router = pages(Router::new()).hypersig_runtime();render takes no arguments, so nothing built outside it reaches the part, and what the part shows comes from its own signals, which the page fills in as the value of its field. part returns a Node<S>, so nothing of the page can be added inside the part's element.
What a part cannot do
Calling the part's action outside the part is a compile error, because a call made inside the part is what tells the server the field its patches land under. The action belongs to the part's struct, so the button that calls it is an element of the part, which the page's element refuses:
#[derive(Signals, Serialize, Deserialize)]
struct Account {
signing_out: bool,
}
hypersig::routes! {
fn pages(Router<()>);
action SIGN_OUT: Action<Account> = post "/sign-out" => sign_out;
}
impl Part for Account {
fn render() -> El<Self> {
button().on_click(SIGN_OUT.call()).text("Sign out")
}
}
#[derive(Signals, Serialize, Deserialize)]
struct Inbox {
account: Account,
}
// error[E0277]: `El<Account>` is not a child of an element on the page whose
// signals are `Inbox`
let shell = div::<Inbox>()
.child(part(Inbox::account()))
.child(button().on_click(SIGN_OUT.call()).text("Sign out"));Embedding the part at a field of a list item is a compile error too, because an item of a list has no path the server can patch. part takes a Sig<S, P> that starts at the page's struct, and an item's field is a Sig<S, P, Local>:
#[derive(Signals, Serialize, Deserialize)]
struct Badge {
count: i32,
}
impl Part for Badge {
fn render() -> El<Self> {
span().bind_text(Badge::count().to_text())
}
}
#[derive(Signals, Serialize, Deserialize)]
struct Shelf {
badges: Vec<Badge>,
}
// error[E0308]: mismatched types: expected `Sig<_, _>`, found
// `Sig<Shelf, Badge, Local>`
let list = ul::<Shelf>().child(each(Shelf::badges(), |badge| li().child(part(badge.value()))));Two fields of one page may hold the same part as long as the part names no element id, node ref, or file input. Otherwise Page::render panics on the two elements that carry one of those.
The result of one call
Call::on_error runs a statement with the error text when that one call fails, and Call::on_success runs one after its patches are applied. Both run where the call was made, and let_in_stmt names a value when the call is sent, so a page that adds several items before the server answers marks only the item whose save failed. The storefront example puts one newsletter part on two pages and saves a wish list this way. Busy and failed calls covers the state of a call.
Next, Styling gives components their look with typed CSS. Actions covers the action a part calls.