mirror of
https://github.com/zaphar/wasm-web-components.git
synced 2025-07-21 19:40:30 -04:00
Cleanup naming of crates and adding docs
This commit is contained in:
parent
b6a80d56a1
commit
3520d7b12c
@ -1,2 +1,2 @@
|
||||
[workspace]
|
||||
members = ["web-component", "derive"]
|
||||
members = ["wasm-web-component", "macros"]
|
@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "web-component-derive"
|
||||
name = "wasm-web-component-macros"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
@ -11,7 +11,6 @@
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use inflector::Inflector;
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::{Literal, Span};
|
||||
@ -80,7 +79,7 @@ fn expand_component_def(
|
||||
class_name: &Literal,
|
||||
element_name: &Literal,
|
||||
) -> syn::ItemImpl {
|
||||
let trait_path = expand_crate_ref("web-component-rs", parse_quote!(WebComponentDef));
|
||||
let trait_path = expand_crate_ref("wasm-web-component", parse_quote!(WebComponentDef));
|
||||
parse_quote! {
|
||||
impl #trait_path for #struct_name {
|
||||
fn element_name() -> &'static str {
|
||||
@ -96,8 +95,8 @@ fn expand_component_def(
|
||||
}
|
||||
|
||||
fn expand_struct_trait_shim(struct_name: &Ident, observed_attrs: Literal) -> syn::ItemImpl {
|
||||
let trait_path = expand_crate_ref("web-component-rs", parse_quote!(WebComponentDef));
|
||||
let handle_path = expand_crate_ref("web-component-rs", parse_quote!(WebComponentHandle));
|
||||
let trait_path = expand_crate_ref("wasm-web-component", parse_quote!(WebComponentDef));
|
||||
let handle_path = expand_crate_ref("wasm-web-component", parse_quote!(WebComponentHandle));
|
||||
parse_quote! {
|
||||
impl #struct_name {
|
||||
pub fn element_name() -> &'static str {
|
||||
@ -175,7 +174,7 @@ return element;",
|
||||
}
|
||||
|
||||
fn expand_wasm_shim(struct_name: &Ident) -> syn::ItemImpl {
|
||||
let trait_path = expand_crate_ref("web-component-rs", parse_quote!(WebComponentBinding));
|
||||
let trait_path = expand_crate_ref("wasm-web-component", parse_quote!(WebComponentBinding));
|
||||
parse_quote! {
|
||||
#[wasm_bindgen::prelude::wasm_bindgen]
|
||||
impl #struct_name {
|
||||
@ -229,7 +228,7 @@ fn expand_wasm_shim(struct_name: &Ident) -> syn::ItemImpl {
|
||||
}
|
||||
|
||||
fn expand_binding(struct_name: &Ident) -> syn::ItemImpl {
|
||||
let trait_path = expand_crate_ref("web-component-rs", parse_quote!(WebComponent));
|
||||
let trait_path = expand_crate_ref("wasm-web-component", parse_quote!(WebComponent));
|
||||
parse_quote!(
|
||||
impl #trait_path for #struct_name {}
|
||||
)
|
||||
@ -259,6 +258,7 @@ fn expand_struct(
|
||||
TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
/// Creates the necessary Rust and Javascript shims for a Web Component.
|
||||
#[proc_macro_attribute]
|
||||
pub fn web_component(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
// TODO(jwall): Attrs for class name and element name
|
@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "web-component-rs"
|
||||
name = "wasm-web-component"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
author = "Jeremy Wall <jeremy@marzhillstudios.com>"
|
||||
@ -10,7 +10,7 @@ crate-type = ["cdylib", "rlib"]
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
web-component-derive = {path = "../derive"}
|
||||
wasm-web-component-macros = { path = "../macros" }
|
||||
|
||||
[dependencies.wasm-bindgen-test]
|
||||
version = "0.3"
|
@ -2,8 +2,25 @@ use js_sys::Function;
|
||||
use wasm_bindgen::{convert::IntoWasmAbi, prelude::Closure, JsValue};
|
||||
use web_sys::{window, Element, HtmlElement};
|
||||
|
||||
pub mod macros;
|
||||
/// This attribute proc-macro will generate the following trait implementations
|
||||
/// * [WebComponentDef](trait@WebComponentDef)
|
||||
/// * [WebComponent](trait@WebComponent)
|
||||
///
|
||||
/// It will also generate a wasm_bindgen compatible impl block for your struct.
|
||||
///
|
||||
/// It expects you to implement the [WebComponentBinding](trait@WebComponentBinding)
|
||||
/// trait in order to implement the callbacks.
|
||||
///
|
||||
/// It supports three attribute `name = value` parameters.
|
||||
/// * `class_name = "ClassName"` - Required. The class name to use for the javascript shim.
|
||||
/// * `element_name = "class-name"` - Optional. A valid custom element name to use for the element.
|
||||
/// * `observed_attrs = "['attr1', attr2']"` - Optional. A javascript array with a list of observed attributes for this compoment.
|
||||
///
|
||||
/// Reference [MDN Web Components Guide](https://developer.mozilla.org/en-US/docs/Web/Web_Components)
|
||||
pub use wasm_web_component_macros::web_component;
|
||||
|
||||
/// Helper trait for Rust Web Components. This is autogenerated
|
||||
/// by the [`#[web_component]`](wasm_web_component_macros::web_component) attribute.
|
||||
pub trait WebComponentDef: IntoWasmAbi + Default {
|
||||
fn new() -> Self {
|
||||
Self::default()
|
||||
@ -22,6 +39,9 @@ pub trait WebComponentDef: IntoWasmAbi + Default {
|
||||
fn class_name() -> &'static str;
|
||||
}
|
||||
|
||||
/// Trait defining the lifecycle callbacks for a Custom Element.
|
||||
/// Each method is optional. You only need to implement the ones
|
||||
/// you want to specify behavior for.
|
||||
pub trait WebComponentBinding: WebComponentDef {
|
||||
fn connected(&self, _element: &HtmlElement) {
|
||||
// noop
|
||||
@ -46,11 +66,18 @@ pub trait WebComponentBinding: WebComponentDef {
|
||||
}
|
||||
}
|
||||
|
||||
/// Marker trait used in the generated shims to assert that their are Rust implemtntations
|
||||
/// of the callback functions for the component.
|
||||
pub trait WebComponent: WebComponentBinding {}
|
||||
|
||||
// TODO(jwall): Trait methods can't be exported out to js yet so we'll need a wrapper object or we'll need to `Derive` this api in a prop-macro.
|
||||
/// A handle for your WebComponent Definition. It is important that this
|
||||
/// handle is live for as long as your Web-Component might be used.
|
||||
pub struct WebComponentHandle<T> {
|
||||
/// The handle for the closure that is used to construct your Rust instance
|
||||
/// in the Javascript shim constructor. If this is dropped then your web component
|
||||
/// will not be able to be constructed properly.
|
||||
pub impl_handle: Closure<dyn FnMut() -> T>,
|
||||
/// A javascript function that can construct your element.
|
||||
pub element_constructor: Function,
|
||||
}
|
||||
|
||||
@ -63,7 +90,7 @@ mod tests {
|
||||
use web_sys::Text;
|
||||
use web_sys::{window, HtmlElement};
|
||||
|
||||
use web_component_derive::web_component;
|
||||
use wasm_web_component_macros::web_component;
|
||||
|
||||
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
|
||||
|
@ -1,13 +0,0 @@
|
||||
// Copyright 2022 Jeremy Wall (Jeremy@marzhilsltudios.com)
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
Loading…
x
Reference in New Issue
Block a user