Added dynatos-html.

This commit is contained in:
Filipe Rodrigues 2024-02-04 10:01:41 +00:00
parent 6bdb983304
commit 4ac4946245
Signed by: zenithsiz
SSH Key Fingerprint: SHA256:Mb5ppb3Sh7IarBO/sBTXLHbYEOz37hJAlslLQPPAPaU
13 changed files with 87 additions and 45 deletions

8
Cargo.lock generated
View File

@ -55,6 +55,13 @@ dependencies = [
name = "dynatos-context"
version = "0.1.0"
[[package]]
name = "dynatos-html"
version = "0.1.0"
dependencies = [
"web-sys",
]
[[package]]
name = "dynatos-logger"
version = "0.1.0"
@ -79,6 +86,7 @@ dependencies = [
"anyhow",
"duplicate",
"dynatos-context",
"dynatos-html",
"dynatos-reactive",
"dynatos-util",
"extend",

View File

@ -3,6 +3,7 @@
members = [
"dynatos",
"dynatos-context",
"dynatos-html",
"dynatos-logger",
"dynatos-reactive",
"dynatos-router",
@ -15,6 +16,7 @@ resolver = "2"
# Workspace members
dynatos = { path = "dynatos" }
dynatos-context = { path = "dynatos-context" }
dynatos-html = { path = "dynatos-html" }
dynatos-logger = { path = "dynatos-logger" }
dynatos-reactive = { path = "dynatos-reactive" }
dynatos-router = { path = "dynatos-router" }

8
dynatos-html/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "dynatos-html"
version = "0.1.0"
edition = "2021"
[dependencies]
web-sys = { workspace = true }

28
dynatos-html/src/html.rs Normal file
View File

@ -0,0 +1,28 @@
//! HTML elements
// Imports
use web_sys::Element;
/// Declares all elements
macro decl_elements(
$( $fn_name:ident = $el_name:literal ),* $(,)?
) {
$(
pub fn $fn_name() -> Element {
// TODO: Cache the document in a thread local?
let window = web_sys::window().expect("Unable to get window");
let document = window.document().expect("Unable to get document");
document.create_element($el_name).expect(concat!("Unable to create element: `", $el_name, "`"))
}
)*
}
decl_elements! {
a = "a",
br = "br",
button = "button",
div = "div",
hr = "hr",
p = "p",
span = "span",
}

7
dynatos-html/src/lib.rs Normal file
View File

@ -0,0 +1,7 @@
//! Html wrappers for [`dynatos`]
// Features
#![feature(decl_macro)]
// Modules
pub mod html;

View File

@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
dynatos-context = { workspace = true }
dynatos-html = { workspace = true }
dynatos-reactive = { workspace = true }
dynatos-util = { workspace = true }

View File

@ -3,7 +3,7 @@
// Imports
use {
crate::Location,
anyhow::Context,
dynatos_html::html,
dynatos_util::{ev, ElementEventListener, ElementWithAttr, JsResultContext},
web_sys::{Element, PointerEvent},
};
@ -15,12 +15,7 @@ pub fn anchor<U>(new_location: U) -> Result<Element, anyhow::Error>
where
U: AsRef<str> + 'static,
{
let window = web_sys::window().context("Unable to get window")?;
let document = window.document().context("Unable to get document")?;
let el = document
.create_element("a")
.context("Unable to create a")?
let el = html::a()
.with_attr("href", new_location.as_ref())
.context("Unable to set attribute")?
.with_event_listener::<ev::Click, _>(move |ev: PointerEvent| {

10
examples/Cargo.lock generated
View File

@ -46,6 +46,7 @@ dependencies = [
"anyhow",
"console_error_panic_hook",
"dynatos",
"dynatos-html",
"dynatos-logger",
"dynatos-reactive",
"dynatos-util",
@ -81,6 +82,13 @@ dependencies = [
name = "dynatos-context"
version = "0.1.0"
[[package]]
name = "dynatos-html"
version = "0.1.0"
dependencies = [
"web-sys",
]
[[package]]
name = "dynatos-logger"
version = "0.1.0"
@ -105,6 +113,7 @@ dependencies = [
"anyhow",
"duplicate",
"dynatos-context",
"dynatos-html",
"dynatos-reactive",
"dynatos-util",
"extend",
@ -327,6 +336,7 @@ dependencies = [
"console_error_panic_hook",
"dynatos",
"dynatos-context",
"dynatos-html",
"dynatos-logger",
"dynatos-reactive",
"dynatos-router",

View File

@ -8,6 +8,7 @@ resolver = "2"
# Parent workspace members
dynatos = { path = "../dynatos" }
dynatos-context = { path = "../dynatos-context" }
dynatos-html = { path = "../dynatos-html" }
dynatos-logger = { path = "../dynatos-logger" }
dynatos-reactive = { path = "../dynatos-reactive" }
dynatos-router = { path = "../dynatos-router" }

View File

@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
dynatos = { workspace = true }
dynatos-html = { workspace = true }
dynatos-logger = { workspace = true }
dynatos-reactive = { workspace = true }
dynatos-util = { workspace = true }

View File

@ -4,9 +4,10 @@
use {
anyhow::Context,
dynatos::ElementDynText,
dynatos_html::html,
dynatos_reactive::Signal,
dynatos_util::{ev, ElementEventListener, ElementWithChildren, ElementWithTextContent, JsResultContext},
web_sys::{Document, Element},
web_sys::Element,
};
fn main() {
@ -24,21 +25,17 @@ fn run() -> Result<(), anyhow::Error> {
let document = window.document().context("Unable to get document")?;
let body = document.body().context("Unable to get document body")?;
let counter = self::counter(&document).context("Unable to build button")?;
let counter = self::counter().context("Unable to build button")?;
body.append_child(&counter).context("Unable to append counter")?;
Ok(())
}
fn counter(document: &Document) -> Result<Element, anyhow::Error> {
fn counter() -> Result<Element, anyhow::Error> {
let value = Signal::new(0);
document
.create_element("div")
.context("Unable to create div")?
html::div()
.with_children([
document
.create_element("button")
.context("Unable to create button")?
html::button()
.with_text_content("Clear")
.with_event_listener::<ev::Click, _>({
let value = value.clone();
@ -46,29 +43,22 @@ fn counter(document: &Document) -> Result<Element, anyhow::Error> {
value.set(0);
}
}),
document
.create_element("button")
.context("Unable to create button")?
html::button()
.with_text_content("+")
.with_event_listener::<ev::Click, _>({
let value = value.clone();
move |_ev| value.update(|value| *value += 1)
}),
document
.create_element("button")
.context("Unable to create button")?
html::button()
.with_text_content("-")
.with_event_listener::<ev::Click, _>({
let value = value.clone();
move |_ev| value.update(|value| *value -= 1)
}),
document
.create_element("span")
.context("Unable to create span")?
.with_dyn_text({
let value = value.clone();
move || Some(format!("Value: {}.", value.get()))
}),
html::span().with_dyn_text({
let value = value.clone();
move || Some(format!("Value: {}.", value.get()))
}),
])
.context("Unable to add children")
}

View File

@ -7,6 +7,7 @@ edition = "2021"
dynatos = { workspace = true }
dynatos-context = { workspace = true }
dynatos-html = { workspace = true }
dynatos-logger = { workspace = true }
dynatos-reactive = { workspace = true }
dynatos-router = { workspace = true }

View File

@ -8,6 +8,7 @@ use {
anyhow::Context,
dynatos::ElementDynChild,
dynatos_context::Handle,
dynatos_html::html,
dynatos_router::Location,
dynatos_util::{ElementWithChildren, ElementWithTextContent, JsResultContext, ObjectDefineProperty},
wasm_bindgen::prelude::wasm_bindgen,
@ -34,10 +35,7 @@ fn run() -> Result<(), anyhow::Error> {
body.dyn_child(move || match self::render_route() {
Ok(page) => page,
Err(err) => document
.create_element("p")
.expect("Unable to create element")
.with_text_content(format!("Error: {err:?}")),
Err(err) => html::p().with_text_content(format!("Error: {err:?}")),
});
#[wasm_bindgen]
@ -60,20 +58,12 @@ fn render_route() -> Result<Element, anyhow::Error> {
}
fn page(name: &str) -> Result<Element, anyhow::Error> {
let window = web_sys::window().context("Unable to get window")?;
let document = window.document().context("Unable to get document")?;
document
.create_element("div")
.context("Unable to create div")?
html::div()
.with_children([
document
.create_element("p")
.context("Unable to create p")?
.with_text_content(format!("Page {name}")),
document.create_element("hr").context("Unable to create hr")?,
html::p().with_text_content(format!("Page {name}")),
html::hr(),
dynatos_router::anchor("/a")?.with_text_content("A"),
document.create_element("br").context("Unable to create br")?,
html::br(),
dynatos_router::anchor("/b")?.with_text_content("B"),
])
.context("Unable to add children")