Skip to content

Commit

Permalink
<!snake_case> components (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejhirsz authored Mar 28, 2024
1 parent 1863f2c commit 0f650f0
Show file tree
Hide file tree
Showing 47 changed files with 402 additions and 398 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"crates/*",
"examples/*",
]
resolver = "2"

[profile.release]
lto = "fat"
Expand Down
37 changes: 18 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ Components in **Kobold** are created by annotating a _render function_ with a `#
use kobold::prelude::*;

#[component]
fn Hello(name: &str) -> impl View + '_ {
fn hello(name: &str) -> impl View + '_ {
view! {
<h1>"Hello "{ name }"!"</h1>
}
}

fn main() {
kobold::start(view! {
<Hello name="Kobold" />
<!hello name="Kobold">
});
}
```
Expand All @@ -64,8 +64,8 @@ token stream, so the Rust compiler can tell you when you've made a mistake:
error[E0560]: struct `Hello` has no field named `nam`
--> examples/hello_world/src/main.rs:12:16
|
12 | <Hello nam="Kobold" />
| ^^^ help: a field with a similar name exists: `name`
12 | <!hello nam="Kobold">
| ^^^ help: there is a method with a similar name: `name`
```

You can even use [rust-analyzer](https://rust-analyzer.github.io/) to refactor component or field names,
Expand All @@ -80,7 +80,7 @@ their state:
use kobold::prelude::*;

#[component]
fn Counter(init: u32) -> impl View {
fn counter(init: u32) -> impl View {
stateful(init, |count| {
bind! { count:
// Create an event handler with access to `&mut u32`
Expand All @@ -100,7 +100,7 @@ fn Counter(init: u32) -> impl View {

fn main() {
kobold::start(view! {
<Counter init={0} />
<!counter init={0}>
});
}
```
Expand All @@ -109,7 +109,7 @@ The `stateful` function takes two parameters:

* State constructor that implements the `IntoState` trait. **Kobold** comes with default
implementations for most primitive types, so we can use `u32` here.
* The anonymous render function that uses the constructed state, in our case its argument is `&Hook<u32>`.
* The anonymous render closure that uses the constructed state, in our case its argument is `&Hook<u32>`.

The `Hook` here is a smart pointer to the state itself that allows non-mutable access to the
state. The `bind!` macro can be invoked for any `Hook` to create closures with `&mut` references to the
Expand All @@ -124,17 +124,17 @@ Use `#[component(<param>?)]` syntax to set a component parameter as default:
```rust
// `code` will default to `200` if omitted
#[component(code?: 200)]
fn Status(code: u32) -> impl View {
fn status(code: u32) -> impl View {
view! {
<p> "Status code was "{ code }
}
}

view! {
// Status code was 200
<Status />
<!status>
// Status code was 404
<Status code={404} />
<!status code={404}>
}
```

Expand All @@ -151,7 +151,7 @@ inside an `if` or `match` expression, and wrap them in an enum making them the s

```rust
#[component(auto_branch)]
fn Conditional(illuminatus: bool) -> impl View {
fn conditional(illuminatus: bool) -> impl View {
if illuminatus {
view! { <p> "It was the year when they finally immanentized the Eschaton." }
} else {
Expand All @@ -167,11 +167,10 @@ For more details visit the [`branching` module documentation](https://docs.rs/ko
To render an iterator use the `for` keyword:

```rust
// `ListIteratorExt` is included in the prelude
use kobold::prelude::*;

#[component]
fn IterateNumbers(count: u32) -> impl View {
fn iterate_numbers(count: u32) -> impl View {
view! {
<ul>
{
Expand All @@ -195,7 +194,7 @@ state without unnecessary clones:

```rust
#[component]
fn Users<'a>(names: &'a [&'a str]) -> impl View + 'a {
fn users<'a>(names: &'a [&'a str]) -> impl View + 'a {
view! {
<ul>
{
Expand All @@ -214,15 +213,15 @@ If you wish to capture children from parent `view!` invocation, simply change
use kobold::prelude::*;

#[component(children)]
fn Header(children: impl View) -> impl View {
fn header(children: impl View) -> impl View {
view! {
<header><h1>{ children }</h1></header>
}
}

fn main() {
kobold::start(view! {
<Header>"Hello Kobold"</Header>
<!header>"Hello Kobold"</!header>
});
}
```
Expand All @@ -234,7 +233,7 @@ use kobold::prelude::*;

// Capture children into the argument `n`
#[component(children: n)]
fn AddTen(n: i32) -> i32 {
fn add_ten(n: i32) -> i32 {
// integers implement `View` so they can be passed by value
n + 10
}
Expand All @@ -243,15 +242,15 @@ fn main() {
kobold::start(view! {
<p>
"Meaning of life is "
<AddTen>{ 32 }</AddTen>
<!add_ten>{ 32 }</!add_ten>
</p>
});
}
```

## More Examples

To run **Kobold** you'll need to install [`trunk`](https://trunkrs.dev/) (check the [full instructions](https://trunkrs.dev/#install) if you have problems):
To run **Kobold** you'll need to install [`trunk`](https://trunkrs.dev/):
```sh
cargo install --locked trunk
```
Expand Down
4 changes: 2 additions & 2 deletions crates/kobold/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kobold"
version = "0.9.1"
version = "0.10.0"
authors = ["Maciej Hirsz <[email protected]>"]
edition = "2021"
license = "MPL-2.0"
Expand All @@ -19,7 +19,7 @@ stateful = []
wasm-bindgen = "0.2.84"
wasm-bindgen-futures = "0.4.34"
itoa = "1.0.6"
kobold_macros = { version = "0.9.0", path = "../kobold_macros" }
kobold_macros = { version = "0.10.0", path = "../kobold_macros" }
console_error_panic_hook = "0.1.7"
rlsf = { version = "0.2.1", optional = true }
serde = { version = "1", optional = true }
Expand Down
12 changes: 5 additions & 7 deletions crates/kobold/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ export function removeClass(n,v) { n.classList.remove(v); }
export function replaceClass(n,o,v) { n.classList.replace(o,v); }
export function toggleClass(n,c,v) { n.classList.toggle(c,v); }

export function makeEventHandler(c,f) { return (e) => koboldCallback(e,c,f); }
export function checkEventHandler() { if (typeof koboldCallback !== "function") console.error(
`Missing \`koboldCallback\` in global scope.
Add the following to your Trunk.toml:
[build]
pattern_script = "<script type=\\"module\\">import init, { koboldCallback } from '{base}{js}';init('{base}{wasm}');window.koboldCallback = koboldCallback;</script>"
export function makeEventHandler(c,f) { return (e) => wasmBindings.koboldCallback(e,c,f); }
export function checkEventHandler() { if (typeof wasmBindings !== "object") console.error(
`Missing \`wasmBindings\` in global scope.
As of Kobold v0.10 and Trunk v0.17.16 you no longer need to export bindings manually, \
please remove the custom \`pattern_script\' from your \`Trunk.toml\` file.
`) }
13 changes: 8 additions & 5 deletions crates/kobold/src/branching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! ```compile_fail
//! # use kobold::prelude::*;
//! #[component]
//! fn Conditional(illuminatus: bool) -> impl View {
//! fn conditional(illuminatus: bool) -> impl View {
//! if illuminatus {
//! view! { <p>"It was the year when they finally immanentized the Eschaton."</p> }
//! } else {
Expand Down Expand Up @@ -38,13 +38,14 @@
//! ```
//! # use kobold::prelude::*;
//! #[component(auto_branch)]
//! fn Conditional(illuminatus: bool) -> impl View {
//! fn conditional(illuminatus: bool) -> impl View {
//! if illuminatus {
//! view! { <p>"It was the year when they finally immanentized the Eschaton."</p> }
//! } else {
//! view! { <blockquote>"It was love at first sight."</blockquote> }
//! }
//! }
//! # fn main() {}
//! ```
//!
//! This flag is not enabled by default, yet, as there might be situations [`auto_branch`](crate::component#componentauto_branch)
Expand All @@ -59,7 +60,7 @@
//! use kobold::branching::Branch2;
//!
//! #[component]
//! fn Conditional(illuminatus: bool) -> impl View {
//! fn conditional(illuminatus: bool) -> impl View {
//! if illuminatus {
//! Branch2::A(view! {
//! <p>"It was the year when they finally immanentized the Eschaton."</p>
Expand All @@ -70,16 +71,17 @@
//! })
//! }
//! }
//! # fn main() {}
//! ```
//!
//! This is in fact all that the [`auto_branch`](crate::component#componentauto_branch) flag does for you automatically.
//!
//! For simple optional renders you can always use the standard library [`Option`](Option):
//! For simple optional renders you can always use the standard library [`Option`]:
//!
//! ```
//! # use kobold::prelude::*;
//! #[component]
//! fn Conditional(illuminatus: bool) -> impl View {
//! fn conditional(illuminatus: bool) -> impl View {
//! if illuminatus {
//! Some(view! {
//! <p>"It was the year when they finally immanentized the Eschaton."</p>
Expand All @@ -88,6 +90,7 @@
//! None
//! }
//! }
//! # fn main() {}
//! ```
use std::mem::MaybeUninit;
Expand Down
11 changes: 6 additions & 5 deletions crates/kobold/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub use vstring::VString;
/// }
///
/// #[component]
/// fn UserRow(user: &User) -> impl View + '_ {
/// fn user_row(user: &User) -> impl View + '_ {
/// fence(user.id, || view! {
/// // This row is only re-rendered if `user.id` has changed
/// <tr>
Expand All @@ -48,6 +48,7 @@ pub use vstring::VString;
/// </tr>
/// })
/// }
/// # fn main() {}
/// ```
pub const fn fence<D, V, F>(guard: D, render: F) -> Fence<D, F>
where
Expand All @@ -61,7 +62,7 @@ where
}
}

/// Smart [`View`](View) that guards against unnecessary renders, see [`fence`](fence).
/// Smart [`View`] that guards against unnecessary renders, see [`fence`].
pub struct Fence<D, F> {
guard: D,
inner: F,
Expand Down Expand Up @@ -165,7 +166,7 @@ macro_rules! impl_diff {
impl_diff_str!(&str, &String);
impl_diff!(bool, u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64);

/// Smart [`View`](View) that only updates its content when the reference to T has changed.
/// Smart [`View`] that only updates its content when the reference to T has changed.
/// See [`ref`](crate::keywords::ref).
#[repr(transparent)]
pub struct Ref<T: ?Sized>(T);
Expand Down Expand Up @@ -203,15 +204,15 @@ impl<T: ?Sized> Diff for &Ref<T> {
}
}

/// Smart [`View`](View) that never performs diffing and instead always triggers
/// Smart [`View`] that never performs diffing and instead always triggers
/// updates.
///
/// See [`use`](crate::keywords::use)
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Eager<T>(pub(crate) T);

/// Smart [`View`](View) that never performs diffing and instead never triggers
/// Smart [`View`] that never performs diffing and instead never triggers
/// updates.
///
/// See [`static`](crate::keywords::static)
Expand Down
2 changes: 1 addition & 1 deletion crates/kobold/src/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub trait Mountable: 'static {
fn replace_with(&self, new: &JsValue);
}

/// A light-weight [`Deref`](Deref)-like trait that
/// A light-weight [`Deref`]-like trait that
/// auto-implements `Mountable` by proxying it to another type.
pub trait Anchor {
type Js: JsCast;
Expand Down
6 changes: 3 additions & 3 deletions crates/kobold/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use crate::View;

/// Uninitialized stable pointer to `T`.
///
/// Used for the initialize-in-place strategy employed by the [`View::build`](View::build) method.
/// Used for the initialize-in-place strategy employed by the [`View::build`] method.
#[must_use]
#[repr(transparent)]
pub struct In<'a, T>(&'a mut MaybeUninit<T>);

/// Initialized stable pointer to `T`.
///
/// Used for the initialize-in-place strategy employed by the [`View::build`](View::build) method.
/// Used for the initialize-in-place strategy employed by the [`View::build`] method.
#[repr(transparent)]
pub struct Out<'a, T>(&'a mut T);

Expand Down Expand Up @@ -174,7 +174,7 @@ macro_rules! init {
};
}

/// Wrapper that turns `extern` precompiled JavaScript functions into [`View`](View)s.
/// Wrapper that turns `extern` precompiled JavaScript functions into [`View`]s.
#[repr(transparent)]
pub struct Precompiled<F>(pub F);

Expand Down
Loading

0 comments on commit 0f650f0

Please sign in to comment.