Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap HtmlElement inside TemplateResult #10

Merged
merged 1 commit into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use maple_core::prelude::*;

pub fn MyComponent(num: StateHandle<i32>) -> HtmlElement {
pub fn MyComponent(num: StateHandle<i32>) -> TemplateResult {
template! {
div(class="my-component") {
# "My component"
Expand Down
2 changes: 1 addition & 1 deletion maple-core-macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl ToTokens for Component {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Component { expr_call } = self;

let quoted = quote! { #expr_call };
let quoted = quote! { ::maple_core::TemplateResult::inner_element(&#expr_call) };

tokens.extend(quoted);
}
Expand Down
2 changes: 1 addition & 1 deletion maple-core-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn template(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as element::Element);

let quoted = quote! {
{ #input }
::maple_core::TemplateResult::new(#input)
};

TokenStream::from(quoted)
Expand Down
2 changes: 1 addition & 1 deletion maple-core-macro/tests/ui/component-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use maple_core::prelude::*;

fn Component() -> HtmlElement {
fn Component() -> TemplateResult {
template! {
div
}
Expand Down
2 changes: 1 addition & 1 deletion maple-core-macro/tests/ui/component-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use maple_core::prelude::*;

pub fn Component() -> HtmlElement {
pub fn Component() -> TemplateResult {
template! {
div
}
Expand Down
32 changes: 26 additions & 6 deletions maple-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,40 @@ pub mod reactive;
use web_sys::HtmlElement;

/// Render an [`HtmlElement`] into the DOM.
pub fn render(element: HtmlElement) {
pub fn render(template_result: TemplateResult) {
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
document.body().unwrap().append_child(&element).unwrap();
document
.body()
.unwrap()
.append_child(&template_result.element)
.unwrap();
}

/// The result of the `template!` macro. Should not be used directly.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TemplateResult {
element: HtmlElement,
}

impl TemplateResult {
/// Create a new `TemplateResult` from an [`HtmlElement`].
pub fn new(element: HtmlElement) -> Self {
Self { element }
}

pub fn inner_element(&self) -> HtmlElement {
self.element.clone()
}
}

/// The maple prelude.
pub mod prelude {
pub use crate::reactive::{
create_effect, create_memo, create_signal, untracked, SetStateHandle, StateHandle,
create_effect, create_memo, create_selector, create_signal, untracked, SetStateHandle,
StateHandle,
};
pub use crate::render;
pub use crate::{render, TemplateResult};

pub use maple_core_macro::template;

pub use web_sys::HtmlElement;
}
2 changes: 1 addition & 1 deletion maple-core/src/reactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ where
/// assert_eq!(*double(), 2);
///
/// set_state(2);
/// assert_eq!(*double(), 2); // double value should still be true because state() was inside untracked
/// assert_eq!(*double(), 2); // double value should still be old value because state() was inside untracked
/// ```
pub fn untracked<F, Out>(f: F) -> Out
where
Expand Down