Feat: move the rsx macro around

This commit is contained in:
Jonathan Kelley committed 2021-06-03 10:42:28 -04:00
1 parent 1919f88f03
commit 50c8b93aad
23 files changed
+886 -617

No files matched your search

+88 -4
View File
@@ -1,13 +1,97 @@
#![allow(non_upper_case_globals)]
use dioxus_ssr::{
prelude::*,
prelude::{builder::IntoVNode, dioxus::events::on::MouseEvent},
TextRenderer,
};
mod utils;
fn main() {
TextRenderer::new(App);
let renderer = TextRenderer::new(App);
}
fn App(ctx: Context<()>) -> VNode {
todo!()
}
static App: FC<()> = |ctx| {
rsx! { in ctx,
div {
Home {}
Docs {}
Tutorial {}
Blog {}
Community {}
}
}
};
const HeroContent: [(&'static str, &'static str); 3] = [
("Declarative",
"React makes it painless to create interactive UIs. Design simple views for each state in your application, and React will efficiently update and render just the right components when your data changes.\nDeclarative views make your code more predictable and easier to debug."),
("Component-Based", "Build encapsulated components that manage their own state, then compose them to make complex UIs.\nSince component logic is written in JavaScript instead of templates, you can easily pass rich data through your app and keep state out of the DOM."),
("Learn Once, Write Anywhere", "We don’t make assumptions about the rest of your technology stack, so you can develop new features in React without rewriting existing code.\nReact can also render on the server using Node and power mobile apps using React Native."),
];
const SnippetHighlights: &'static str = include_str!("./snippets.md");
static Home: FC<()> = |ctx| {
let hero = HeroContent.iter().map(|(title, body)| {
rsx! {
div {
h3 { "{title}" }
div { {body.split("\n").map(|paragraph| rsx!( p{"{paragraph}"} ))} }
}
}
});
let snippets: Vec<VNode> = utils::markdown_to_snippet(ctx, SnippetHighlights);
rsx! { in ctx,
div {
header {
// Hero
section {
div { {hero} }
}
hr {}
// Highlighted Snippets
section {
{snippets}
}
}
div {}
section {}
}
}
};
static Docs: FC<()> = |ctx| {
rsx! { in ctx,
div {
}
}
};
static Tutorial: FC<()> = |ctx| {
rsx! { in ctx,
div {
}
}
};
static Blog: FC<()> = |ctx| {
rsx! { in ctx,
div {
}
}
};
static Community: FC<()> = |ctx| {
rsx! { in ctx,
div {
}
}
};
+56
View File
@@ -0,0 +1,56 @@
# A Simple Component
```rust
#[derive(PartialEq, Properties)]
struct Props { name: &'static str }
static HelloMessage: FC<Props> = |ctx| {
ctx.render(rsx!{
div { "Hello {ctx.props.name}" }
})
}
```
# Two syntaxes: html! and rsx!
Choose from a close-to-html syntax or the standard rsx! syntax
```rust
static HelloMessage: FC<()> = |ctx| {
ctx.render(html!{
<div> Hello World! </div>
})
}
```
# A Stateful Component
Store state with hooks!
```rust
enum LightState {
Green
Yellow,
Red,
}
static HelloMessage: FC<()> = |ctx| {
let (color, set_color) = use_state(ctx, || LightState::Green);
let title = match color {
Green => "Green means go",
Yellow => "Yellow means slow down",
Red => "Red means stop",
};
ctx.render(rsx!{
h1 { "{title}" }
button { "tick"
onclick: move |_| set_color(match color {
Green => Yellow,
Yellow => Red,
Red => Green,
})
}
})
}
```
+31
View File
@@ -0,0 +1,31 @@
use dioxus_ssr::prelude::{Context, VNode};
// Parse a snippet into
pub fn markdown_to_snippet<'a, P>(ctx: Context<'a, P>, text: &str) -> Vec<VNode<'a>> {
let snips = Vec::new();
use pulldown_cmark::{Options, Parser};
let mut options = Options::empty();
let mut parser = Parser::new_ext(text, options);
while let Some(evt) = parser.next() {
match evt {
pulldown_cmark::Event::Start(tag) => {
// take until the end
let r = parser.next();
}
// push a p{} tag with the contents
pulldown_cmark::Event::Text(text) => todo!(),
// Code delinates an end
pulldown_cmark::Event::Code(code) => todo!(),
// not supported
pulldown_cmark::Event::Html(ht) => {}
_ => {}
}
}
//
snips
}