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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//! # Welcome!
//!
//! This is an introduction to writing user interfaces with the mogwai crate.
//!
//! <div align="center">
//! <h1>
//! <img src="https://raw.githubusercontent.com/schell/mogwai/master/img/gizmo.svg" />
//! <br />
//! mogwai
//! </h1>
//! </div>
//!
//! The following is a short introduction to the library's basic concepts.
//!
//! ## Preludes
//!
//! There are a _few_ prelude modules that you can glob-import to make development easier:
//!
//! The first is the common prelude, which contains cross-platform types and traits.
//!
//! ```rust
//! use mogwai::prelude::*;
//! ```
//!
//! Then there are more domain-specific preludes for web and server-side rendering,
//! both of which re-export the common prelude:
//!
//! ```rust
//! use mogwai::web::prelude::*;
//! use mogwai::ssr::prelude::*;
//! ```
//!
//! The [web prelude](crate::web::prelude) also re-exports a few of the most commonly
//! used WASM crates as a convenience, such as [`web_sys`], [`wasm_bindgen`] and
//! [`wasm_bindgen_futures`].
//!
//! ## View Construction
//!
//! View construction is accomplished using a novel [`rsx!`] macro that reduces
//! boilerplate and has special syntax for setting node attributes, text and nesting
//! views.
//!
//! ### RSX
//!
//! [`rsx!`] is a lot like react.js's JSX, except that it uses type checked Rust expressions.
//!
//! Let's start by writing a function that constructs a simple element:
//!
//! ```rust
//! use mogwai::prelude::*;
//!
//! struct Widget<V:View> {
//! root: V::Element
//! }
//!
//! impl<V:View> Widget<V> {
//! fn new() -> Self {
//! rsx! {
//! let root = div(class = "my-div") {
//! a(href = "http://zyghost.com") {
//! "Schellsan's website"
//! }
//! }
//! };
//!
//! Self{ root }
//! }
//! }
//! ```
//!
//! As you can see, the struct takes a type parameter `V:View` which has an
//! associated type `Element`. This allows us to write our views in a platform-agnostic
//! way, and then specialize at runtime:
//!
//! ```rust,no_run
//! # use mogwai::web::Web;
//! # use mogwai::prelude::*;
//! # struct Widget<V:View> {
//! # root: V::Element
//! # }
//! # impl<V:View> Widget<V> {
//! # fn new() -> Self {
//! # rsx! {
//! # let root = div(class = "my-div") {
//! # a(href = "http://zyghost.com") {
//! # "Schellsan's website"
//! # }
//! # }
//! # };
//! # Self{ root }
//! # }
//! # }
//! let web_element = Widget::<Web>::new();
//! ```
//!
//! [`Web`](crate::web::Web) is a type that implements [`View`]. It monomorphizes our struct
//! to produce a view using [`web_sys`] types.
//!
//! Also provided is the [`Ssr`](crate::ssr::Ssr) type, which implements [`View`] to produce
//! views that render to [`String`] for server-side rendering.
//!
//! ```rust
//! # use mogwai::ssr::Ssr;
//! # use mogwai::prelude::*;
//! # struct Widget<V:View> {
//! # root: V::Element
//! # }
//! # impl<V:View> Widget<V> {
//! # fn new() -> Self {
//! # rsx! {
//! # let root = div(class = "my-div") {
//! # a(href = "http://zyghost.com") {
//! # "Schellsan's website"
//! # }
//! # }
//! # };
//! # Self{ root }
//! # }
//! # }
//! let ssr_element = Widget::<Ssr>::new();
//! println!("{}", ssr_element.root.html_string());
//! ```
//!
//! In this way, server-side rendering is a separate view "platform" from the browser,
//! but we can build the view all the same using our `V:View` parameterization.
//!
//! ### Cross-platform
//!
//! [`View`] has a number of associated types:
//!
//! * **Element**
//! * **Text**
//! * **Node**
//! * **EventListener**
//! * **Event**
//!
//! They all work together with few [interlocking traits](crate::view#traits)
//! to make your views cross-platform. But you can also specialize certain
//! operations to specific platforms using the convenience functions
//! [`when_element`](crate::view::ViewElement::when_element)
//! and [`when_event`](crate::view::ViewEvent::when_event):
//!
//! ```rust
//! use mogwai::web::prelude::*;
//!
//! struct MyView<V: View> {
//! root: V::Element,
//! button: V::Element,
//! }
//!
//! impl<V: View> MyView<V> {
//! fn new() -> Self {
//! rsx! {
//! let root = div(class = "my-view") {
//! h1() { "Hello, Mogwai!" }
//! let button = button() {
//! "Click me"
//! }
//! }
//! }
//! Self { root, button }
//! }
//!
//! fn specialize_for_web(&self) {
//! self.button.when_element::<Web, _>(|el: &web_sys::Element| {
//! el.set_property("data-special", "web");
//! });
//! }
//! }
//! ```
//!
//! We can even go a step further when specializing for the web by using
//! the [`dyn_el`](crate::web::WebElement::dyn_el) and
//! [`dyn_ev`](crate::web::WebEvent::dyn_ev) extension methods, which cast your
//! elements and events to specific [`web_sys`] types that implement
//! [`JsCast`](wasm_bindgen::JsCast).
//!
//! ```rust
//! use mogwai::web::prelude::*;
//!
//! struct MyView<V: View> {
//! root: V::Element,
//! input: V::Element,
//! }
//!
//! impl<V: View> MyView<V> {
//! fn new() -> Self {
//! rsx! {
//! let root = div(class = "my-view") {
//! h1() { "Use the input:" }
//! let input = input(type_ = "text") {}
//! }
//! }
//! Self { root, input }
//! }
//!
//! fn specialize_for_web(&self) {
//! self.input.dyn_el(|input: &web_sys::HtmlInputElement| {
//! let value = input.value();
//! // do special stuff with the input value here...
//! });
//! }
//! }
//! ```
//!
//! ### Event handling
//!
//! The [`rsx!`] macro binds [event listeners](crate::view::ViewEventListener) in
//! attribute position to a name, which can then be used by platform-agnostic
//! logic:
//!
//! ```rust
//! use mogwai::prelude::*;
//!
//! struct Widget<V:View> {
//! root: V::Element,
//! text: V::Text,
//! /// A cross-platform event listener, which responds to `.next()` to await the
//! /// next event occurence.
//! on_click: V::EventListener,
//! }
//!
//! impl<V:View> Widget<V> {
//! fn new() -> Self {
//! rsx! {
//! let root = div(class = "my-div") {
//! a(
//! // Here an event listener is registered and then bound
//! // to the name `on_click`
//! on:click = on_click,
//! href = "http://zyghost.com"
//! ) {
//! let text = "Schellsan's website"
//! }
//! }
//! };
//!
//! Self{
//! root,
//! text,
//! on_click,
//! }
//! }
//!
//! async fn step(&self) {
//! let _ev: V::Event = self.on_click.next().await;
//! self.text.set_text("You clicked!");
//! }
//! }
//! ```
//!
//! As you can see, the view platform is kept agnostic, and after the click event,
//! updating the text is an obvious, intentional action on the part of the logic inside
//! the `step` function.
//!
//! ### Using [`Proxy`] for updates
//!
//! Views in mogwai are dynamic, but they are updated explicitly in response to events.
//! Sometimes, though, we'd like to hold little bits of state in our views, and when
//! that state changes we want multiple parts of the view to "react".
//!
//! This doesn't violate mogwai's goal of ensuring updates are explicit, in that the change must
//! be executed explicitly in logic, but the results of that change may occur in more
//! than one place, by use of the [`Proxy`] type.
//!
//! To use a [`Proxy`] we construct it outside of the [`rsx!`] macro and then use it
//! with some special notation inside the macro:
//!
//! ```rust
//! use mogwai::prelude::*;
//!
//! struct Widget<V:View> {
//! root: V::Element,
//! on_click: V::EventListener,
//! state: Proxy<u32>,
//! }
//!
//! impl<V:View> Widget<V> {
//! fn new() -> Self {
//! let mut state = Proxy::new(0);
//!
//! rsx! {
//! let root = div(class = "my-div") {
//! a(
//! // Here an event listener is registered and then bound to the name `on_click`
//! on:click = on_click,
//! href = "http://zyghost.com"
//! ) {
//! {state(n => match *n {
//! 0 => "Schellsan's website",
//! _ => "You clicked!",
//! }.to_string())}
//! span() {
//! {state(n => format!("Counted {n} clicks."))}
//! }
//! }
//! }
//! };
//!
//! Self{
//! root,
//! on_click,
//! state,
//! }
//! }
//!
//! async fn step(&mut self) {
//! let _ev: V::Event = self.on_click.next().await;
//! let current_clicks = *self.state;
//! self.state.set(current_clicks + 1);
//! }
//! }
//! ```
//!
//! In this example, clicking the button updates the state, which in turn updates
//! the paragraph text. The `Proxy` type is used to manage the state and trigger
//! updates to the view when the state changes.
//!
//! #### [`Proxy`] API notes
//!
//! Note that [`Proxy`] is not `Clone`, and that modifying a [`Proxy`] requires mutation.
//! This is a purposeful design choice to make tracking down data updates easy.
//!
//! # Getting started
//!
//! That's it! Time to get started. If you're looking for a project template you can use
//! the [`cargo-generate`](https://crates.io/crates/cargo-generate)
//! [`mogwai-template`](https://github.com/schell/mogwai-template).
use *;
use crate as mogwai;