1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
//! Submillisecond LiveView provides rich, real-time user experiences with
//! server-rendered HTML.
//!
//! ### Prerequisites
//!
//! [Lunatic runtime] is required, along with the wasm32-wasi target.
//!
//! See [README.md#prerequisites] on how to install Lunatic.
//!
//! [Lunatic runtime]: https://github.com/lunatic-solutions/lunatic-rs#setup
//! [README.md#prerequisites]: https://github.com/lunatic-solutions/submillisecond-live-view#prerequisites
//!
//! ### Quick Start
//!
//! To get started, add `submillisecond`, `submillisecond-live-view`, and
//! `serde` to your Cargo.toml.
//!
//! ```text
//! [dependencies]
//! submillisecond = "*"
//! submillisecond-live-view = "*"
//! serde = { version = "*", features = ["derive"] }
//! ```
//!
//! We'll also need an index.html file next to our Cargo.toml file to act as a
//! template for our LiveView. The LiveView will be injected in the #app div.
//!
//! ```html
//! <html>
//! <head>
//! <title>My LiveView App</title>
//! </head>
//! <body>
//! <div id="app"></div>
//! </body>
//! </html>
//! ```
//!
//! Next, implement [`LiveView`] on a new type, and define the
//! [`LiveView::Events`] tuple, [`LiveView::mount`] and [`LiveView::render`]
//! methods.
//!
//! ```
//! use submillisecond_live_view::prelude::*;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Clone, Serialize, Deserialize)]
//! struct Counter {
//! count: u32,
//! }
//!
//! impl LiveView for Counter {
//! type Events = (Increment, Decrement);
//!
//! fn mount(_uri: Uri, _socket: Option<Socket>) -> Self {
//! Counter { count: 0 }
//! }
//!
//! fn render(&self) -> Rendered {
//! html! {
//! p { "Count is " (self.count) }
//! button @click=(Increment) { "Increment" }
//! button @click=(Decrement) { "Decrement" }
//! }
//! }
//! }
//!
//! #[derive(Serialize, Deserialize)]
//! struct Increment {}
//!
//! impl LiveViewEvent<Increment> for Counter {
//! fn handle(state: &mut Self, _event: Increment) {
//! state.count += 1;
//! }
//! }
//!
//! #[derive(Serialize, Deserialize)]
//! struct Decrement {}
//!
//! impl LiveViewEvent<Decrement> for Counter {
//! fn handle(state: &mut Self, _event: Decrement) {
//! state.count -= 1;
//! }
//! }
//! ```
//!
//! Finally, serve your submillisecond app with the `Counter`.
//!
//! ```
//! use submillisecond::{router, Application};
//!
//! fn main() -> std::io::Result<()> {
//! Application::new(router! {
//! GET "/" => Counter::handler("index.html", "#app")
//! })
//! .serve("127.0.0.1:3000")
//! }
//! ```
//!
//! ### Html Macro
//!
//! The `html!` macro is an extended version of the [maud] macro,
//! which is available under [`submillisecond_live_view::html!`](html!).
//!
//! Docs for the syntax of the `html!` macro are available on the maud website,
//! but this section documents some syntax features which are specific to
//! Submillisecond LiveView.
//!
//! [maud]: https://maud.lambda.xyz/
//!
//! #### Events
//!
//! Events can be defined with the `@click=(Increment)` syntax.
//! Where `click` is the event name, and `Increment` is the event to be sent
//! back to the server.
//!
//! This is syntax sugar for `phx-click=(std::any::type_name::<Increment>())`.
//!
//! **Example**
//!
//! ```rust
//! html! {
//! button @click=(Greet) { "Greet" }
//! }
//! ```
//!
//! See <https://hexdocs.pm/phoenix_live_view/bindings.html#click-events>.
//!
//! #### Values
//!
//! Values can be added to events with the `:name=(value)` syntax.
//! Where `name` is the name of the variable, and `value` is the value.
//! It is typically used along side events to pass data back with the event.
//!
//! This is syntax sugar for `phx-value-name=(value)`.
//!
//! **Example**
//!
//! ```rust
//! html! {
//! button :username=(user.name) @click=(Register) { "Register" }
//! }
//! ```
//!
//! See <https://hexdocs.pm/phoenix_live_view/bindings.html#click-events>.
//!
//! #### Nesting Html
//!
//! Maud supports [partials], but there is a different syntax for nesting
//! renders when using Submillisecond LiveView.
//!
//! Nested renders should use the `@(nested)` syntax.
//! If HTML created with the `html!` macro is nested without the `@` prefix,
//! then it will be rendered as a static string on the page and the content will
//! not be dynamic.
//!
//! **Example**
//!
//! ```rust
//! fn render_header(&self) -> Rendered {
//! html! {
//! h1 { "Header" }
//! }
//! }
//!
//! fn render(&self) -> Rendered {
//! html! {
//! @(self.render_header())
//! }
//! }
//! ```
//!
//! [partials]: https://maud.lambda.xyz/partials.html
#![warn(missing_docs)]
pub mod handler;
pub mod rendered;
pub mod socket;
mod csrf;
mod event_handler;
mod live_view;
mod manager;
mod maud;
mod template;
#[doc(hidden)]
pub use maud_live_view;
pub use maud_live_view::html;
pub use crate::live_view::*;
/// Prelude
pub mod prelude {
pub use submillisecond::http::Uri;
pub use crate::handler::LiveViewRouter;
pub use crate::rendered::Rendered;
pub use crate::socket::Socket;
pub use crate::*;
}