Skip to content

Commit

Permalink
Builder API v2 (#373)
Browse files Browse the repository at this point in the history
* Reworked builder API

* Reorganize module structure

* BREAKING CHANGE: Remove unused feature flag

* Make child and dynamic child directly accept a ElementBuilder

* Add a dyn_if helper method

* Add doc tests for new builder API

* Remove old builder API

* Test all features in CI

* Fix doctests

* Fix clippy lint

* Use new format strings syntax

* Do not test all features in miri

* Fix clippy lints
  • Loading branch information
lukechu10 authored Mar 6, 2022
1 parent 24a1c15 commit 8b2beb5
Show file tree
Hide file tree
Showing 16 changed files with 800 additions and 887 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ jobs:
if: matrix.rust == '1.58.0'
env:
RUN_UI_TESTS: true
run: cargo test
run: cargo test --all-features

- name: Run tests (excluding UI)
run: cargo test
run: cargo test --all-features
if: matrix.rust != '1.58.0'

- name: Run headless browser tests
Expand Down Expand Up @@ -124,6 +124,7 @@ jobs:
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Run tests
# Note that we don't have --all-features for packages/sycamore because of https://github.com/rust-lang/miri/issues/1038
run: |
cd packages/sycamore
cargo miri test
Expand Down
7 changes: 2 additions & 5 deletions docs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn parse(path: &Path) -> Result<MarkdownPage, Box<dyn Error>> {
Some(event)
} else {
let tmp = tmp.take().unwrap();
let anchor = tmp.name.trim().to_lowercase().replace(" ", "-");
let anchor = tmp.name.trim().to_lowercase().replace(' ', "-");
let name = tmp.name.clone();
if level == HeadingLevel::H2 {
outline_tmp.push(tmp);
Expand All @@ -72,10 +72,7 @@ fn parse(path: &Path) -> Result<MarkdownPage, Box<dyn Error>> {
l.children.push(tmp);
}
Some(Event::Html(CowStr::from(format!(
"<{level} id=\"{anchor}\">{name}</{level}>",
level = level,
anchor = anchor,
name = name
"<{level} id=\"{anchor}\">{name}</{level}>"
))))
}
}
Expand Down
9 changes: 4 additions & 5 deletions examples/hello-builder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
[package]
authors = ["Luke Chu <37006668+lukechu10@users.noreply.github.com>"]
edition = "2021"
name = "hello-builder"
publish = false
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
console_error_panic_hook = "0.1.7"
console_log = "0.2.0"
log = "0.4.14"
sycamore = { path = "../../packages/sycamore", features = ["experimental-builder-html"] }
wasm-bindgen = "0.2.78"
sycamore = { path = "../../packages/sycamore", features = [
"experimental-builder-agnostic",
] }
37 changes: 18 additions & 19 deletions examples/hello-builder/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
use sycamore::builder::html::*;
//! Look ma, No `view!`!
//!
//! This example demonstrates the basics of the builder API for constructing views, as an
//! alternative to using the `view!` macro.
use sycamore::builder::prelude::*;
use sycamore::prelude::*;

#[component]
fn App<G: Html>(ctx: ScopeRef) -> View<G> {
let name = ctx.create_signal(String::new());

div(ctx)
.child(
h1(ctx)
.text("Hello ")
.dyn_child(move || {
if *ctx.create_selector(move || !name.get().is_empty()).get() {
span(ctx).dyn_text(move || name.get().to_string()).build()
} else {
span(ctx).text("World").build()
}
})
.text("!")
.build(),
)
.child(input(ctx).bind_value(name).build())
.build()
h(div)
.c(h(h1)
.t("Hello ")
.dyn_if(
|| !name.get().is_empty(),
|| h(span).dyn_t(|| name.get().to_string()),
|| h(span).t("World").view(ctx),
)
.t("!"))
.c(h(input).bind_value(name))
.view(ctx)
}

fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

sycamore::render(|ctx| view! {ctx, App() });
sycamore::render(|ctx| component(|| App(ctx, ())));
}
2 changes: 1 addition & 1 deletion packages/sycamore-macro/src/view/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Codegen {
let quote_tag = match tag {
ElementTag::Builtin(id) => quote! {
let __el = ::sycamore::generic_node::GenericNode::element(
<::sycamore::html::#id as ::sycamore::html::SycamoreElement>::TAG_NAME
<::sycamore::html::#id as ::sycamore::generic_node::SycamoreElement>::TAG_NAME
);
},
ElementTag::Custom(tag_s) => quote! {
Expand Down
1 change: 0 additions & 1 deletion packages/sycamore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ wasm-bindgen-test = "0.3.29"
default = ["dom", "wasm-bindgen-interning"]
dom = []
experimental-builder-agnostic = []
experimental-builder-html = ["experimental-builder-agnostic"]
experimental-hydrate = ["sycamore-macro/experimental-hydrate"]
ssr = ["html-escape", "once_cell", "experimental-hydrate", "sycamore-macro/ssr"]
suspense = ["futures", "wasm-bindgen-futures", "sycamore-futures"]
Expand Down
Loading

0 comments on commit 8b2beb5

Please sign in to comment.