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

Add IS_BROWSER constant to GenericNode #274

Merged
merged 8 commits into from
Nov 6, 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
4 changes: 2 additions & 2 deletions packages/sycamore-macro/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn component_impl(
let component_name_str = component_name.to_string();
let generic_node_ty = generic_node_ty.type_params().next().unwrap();
let generic_node: GenericParam = syn::parse_quote! {
#generic_node_ty: ::sycamore::generic_node::GenericNode
#generic_node_ty: ::sycamore::generic_node::Html
};

let ComponentFunction {
Expand All @@ -187,7 +187,7 @@ pub fn component_impl(
FnArg::Typed(pat_ty) => &pat_ty.ty,
};

// Add the GenericNode type param to generics.
// Add the Html type param to generics.
let first_generic_param_index = generics
.params
.iter()
Expand Down
2 changes: 1 addition & 1 deletion packages/sycamore-macro/src/template/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl ToTokens for Component {
syn::PathArguments::None => quote! {},
syn::PathArguments::AngleBracketed(mut generics) => {
if !generics.args.is_empty() {
// Add the GenericNode type param to generics.
// Add the Html type param to generics.
let first_generic_param_index = generics
.args
.iter()
Expand Down
2 changes: 1 addition & 1 deletion packages/sycamore-macro/tests/template/component-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn c() -> Template<G> {
}
}

fn compile_fail<G: GenericNode>() {
fn compile_fail<G: Html>() {
let _: Template<G> = template! { UnknownComponent() };

let _: Template<G> = template! { C };
Expand Down
2 changes: 1 addition & 1 deletion packages/sycamore-macro/tests/template/component-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn component() -> Template<G> {
}
}

fn compile_pass<G: GenericNode>() {
fn compile_pass<G: Html>() {
let _: Template<G> = template! { Component() };
}

Expand Down
8 changes: 4 additions & 4 deletions packages/sycamore/src/builder/agnostic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The renderer-agnostic API.

use crate::component::Component;
use crate::generic_node::GenericNode;
use crate::generic_node::{GenericNode, Html};
use crate::noderef::NodeRef;
use crate::template::Template;
use crate::utils::render;
Expand Down Expand Up @@ -51,13 +51,13 @@ where
/// }
///
/// // Elsewhere in another component.
/// # fn view<G: GenericNode>() -> Template<G> {
/// # fn view<G: Html>() -> Template<G> {
/// component::<_, MyComponent<_>>(())
/// # }
/// ```
pub fn component<G, C>(props: C::Props) -> Template<G>
where
G: GenericNode,
G: GenericNode + Html,
C: Component<G>,
{
C::__create_component(props)
Expand Down Expand Up @@ -197,7 +197,7 @@ where
/// h1().text("My component").build()
/// }
///
/// # fn _test<G: GenericNode>() -> Template<G> {
/// # fn _test<G: Html>() -> Template<G> {
/// div().component::<MyComponent<_>>(()).build()
/// }
/// ```
Expand Down
9 changes: 9 additions & 0 deletions packages/sycamore/src/generic_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,12 @@ pub trait GenericNode: fmt::Debug + Clone + PartialEq + Eq + Hash + 'static {
/// Create a deep clone of the node.
fn clone_node(&self) -> Self;
}

/// Trait that is implemented by all [`GenericNode`] backends that render to HTML.
pub trait Html: GenericNode {
/// A boolean indicating whether this node is rendered in a browser context.
///
/// A value of `false` does not necessarily mean that it is not being rendered in WASM or even
/// in the browser. It only means that it does not create DOM nodes.
const IS_BROWSER: bool;
}
6 changes: 5 additions & 1 deletion packages/sycamore/src/generic_node/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use wasm_bindgen::prelude::*;
use wasm_bindgen::{intern, JsCast};
use web_sys::{Comment, Element, Node, Text};

use crate::generic_node::{EventHandler, GenericNode};
use crate::generic_node::{EventHandler, GenericNode, Html};
use crate::reactive::{create_root, on_cleanup, ReactiveScope};
use crate::template::Template;
use crate::utils::render::insert;
Expand Down Expand Up @@ -272,6 +272,10 @@ impl GenericNode for DomNode {
}
}

impl Html for DomNode {
const IS_BROWSER: bool = true;
}

/// Render a [`Template`] into the DOM.
/// Alias for [`render_to`] with `parent` being the `<body>` tag.
///
Expand Down
6 changes: 5 additions & 1 deletion packages/sycamore/src/generic_node/ssr_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ahash::AHashMap;
use once_cell::sync::Lazy;
use wasm_bindgen::prelude::*;

use crate::generic_node::{EventHandler, GenericNode};
use crate::generic_node::{EventHandler, GenericNode, Html};
use crate::reactive::create_root;
use crate::template::Template;

Expand Down Expand Up @@ -326,6 +326,10 @@ impl GenericNode for SsrNode {
}
}

impl Html for SsrNode {
const IS_BROWSER: bool = false;
}

trait WriteToString {
fn write_to_string(&self, s: &mut String);
}
Expand Down
1 change: 1 addition & 0 deletions packages/sycamore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub mod prelude {
#[cfg(feature = "dom")]
pub use crate::generic_node::DomNode;
pub use crate::generic_node::GenericNode;
pub use crate::generic_node::Html;
#[cfg(feature = "ssr")]
pub use crate::generic_node::SsrNode;
pub use crate::noderef::NodeRef;
Expand Down
4 changes: 2 additions & 2 deletions packages/sycamore/src/portal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Portal API.

use std::any::{Any, TypeId};
use std::any::{Any};

use wasm_bindgen::prelude::*;

Expand All @@ -20,7 +20,7 @@ where
pub fn portal(props: PortalProps<G>) -> Template<G> {
let PortalProps { children, selector } = props;

if TypeId::of::<G>() == TypeId::of::<DomNode>() {
if G::IS_BROWSER {
let window = web_sys::window().unwrap_throw();
let document = window.document().unwrap_throw();
let container = document
Expand Down
2 changes: 1 addition & 1 deletion website/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async fn get_sidebar(version: Option<&str>) -> SidebarData {
}
}

fn switch<G: GenericNode>(route: StateHandle<Routes>) -> Template<G> {
fn switch<G: Html>(route: StateHandle<Routes>) -> Template<G> {
let template = Signal::new(Template::empty());
let cached_sidebar_data: Signal<Option<(Option<String>, SidebarData)>> = Signal::new(None);
create_effect(cloned!((template) => move || {
Expand Down