Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Aug 17, 2024
1 parent 279bfe1 commit f5c71bb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 73 deletions.
1 change: 1 addition & 0 deletions packages/yew-attrs-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use syn::parse_macro_input;

use crate::attrs::Attrs;

/// Macro to generate dynamic attributes.
#[proc_macro_error::proc_macro_error]
#[proc_macro]
pub fn attrs(input: TokenStream) -> TokenStream {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,6 @@ pub struct HtmlDashedName {
}

impl HtmlDashedName {
/// Checks if this name is equal to the provided item (which can be anything implementing
/// `Into<String>`).
pub fn eq_ignore_ascii_case<S>(&self, other: S) -> bool
where
S: Into<String>,
{
let mut s = other.into();
s.make_ascii_lowercase();
s == self.to_ascii_lowercase_string()
}

pub fn to_ascii_lowercase_string(&self) -> String {
let mut s = self.to_string();
s.make_ascii_lowercase();
s
}

pub fn to_lit_str(&self) -> LitStr {
LitStr::new(&self.to_string(), self.span())
}
Expand Down
48 changes: 1 addition & 47 deletions packages/yew-attrs-macro/src/yew_macro/props/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
use std::convert::TryFrom;
use std::ops::{Deref, DerefMut};

use proc_macro2::{Spacing, Span, TokenStream, TokenTree};
use quote::{quote, quote_spanned};
use proc_macro2::{Spacing, TokenTree};
use syn::parse::{Parse, ParseBuffer, ParseStream};
use syn::spanned::Spanned;
use syn::token::Brace;
use syn::{braced, Block, Expr, ExprBlock, ExprMacro, ExprPath, ExprRange, Stmt, Token};

use crate::yew_macro::html_tree::HtmlDashedName;
use crate::yew_macro::stringify::Stringify;

#[derive(Copy, Clone)]
pub enum PropDirective {
Expand Down Expand Up @@ -281,12 +278,6 @@ impl PropList {
Self(drained)
}

/// Run the given function for all props and aggregate the errors.
/// If there's at least one error, the result will be `Result::Err`.
pub fn check_all(&self, f: impl FnMut(&Prop) -> syn::Result<()>) -> syn::Result<()> {
crate::yew_macro::join_errors(self.0.iter().map(f).filter_map(Result::err))
}

/// Return an error for all duplicate props.
pub fn check_no_duplicates(&self) -> syn::Result<()> {
crate::yew_macro::join_errors(self.iter_duplicates().map(|prop| {
Expand Down Expand Up @@ -333,43 +324,6 @@ impl SpecialProps {
let key = props.pop_unique(Self::KEY_LABEL)?;
Ok(Self { node_ref, key })
}

fn iter(&self) -> impl Iterator<Item = &Prop> {
self.node_ref.as_ref().into_iter().chain(self.key.as_ref())
}

/// Run the given function for all props and aggregate the errors.
/// If there's at least one error, the result will be `Result::Err`.
pub fn check_all(&self, f: impl FnMut(&Prop) -> syn::Result<()>) -> syn::Result<()> {
crate::yew_macro::join_errors(self.iter().map(f).filter_map(Result::err))
}

pub fn wrap_node_ref_attr(&self) -> TokenStream {
self.node_ref
.as_ref()
.map(|attr| {
let value = &attr.value;
quote_spanned! {value.span().resolved_at(Span::call_site())=>
::yew::html::IntoPropValue::<::yew::html::NodeRef>
::into_prop_value(#value)
}
})
.unwrap_or(quote! { ::std::default::Default::default() })
}

pub fn wrap_key_attr(&self) -> TokenStream {
self.key
.as_ref()
.map(|attr| {
let value = attr.value.optimize_literals();
quote_spanned! {value.span().resolved_at(Span::call_site())=>
::std::option::Option::Some(
::std::convert::Into::<::yew::virtual_dom::Key>::into(#value)
)
}
})
.unwrap_or(quote! { ::std::option::Option::None })
}
}

pub struct Props {
Expand Down
8 changes: 0 additions & 8 deletions packages/yew-attrs-macro/src/yew_macro/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ pub trait Stringify {
/// Create `AttrValue` however possible.
fn stringify(&self) -> TokenStream;

/// Optimize literals to `&'static str`, otherwise keep the value as is.
fn optimize_literals(&self) -> TokenStream
where
Self: ToTokens,
{
self.optimize_literals_tagged().to_token_stream()
}

/// Like `optimize_literals` but tags static or dynamic strings with [Value]
fn optimize_literals_tagged(&self) -> Value
where
Expand Down
4 changes: 3 additions & 1 deletion packages/yew-attrs-macro/tests/attrs_component_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ fn Button(props: &ButtonProps) -> Html {

#[function_component]
fn App() -> Html {
let on_click = |_| {};

html! {
<Button attrs={attrs! {class="text-red" disabled=false}}>
<Button attrs={attrs! {class="text-red" disabled=false onclick={on_click}}}>
{"Click"}
</Button>
}
Expand Down
43 changes: 43 additions & 0 deletions packages/yew-attrs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
//! Dynamic attributes for Yew.
//!
//! # Example
//! ```
//! use yew::{prelude::*, virtual_dom::VTag};
//! use yew_attrs::{attrs, Attrs};
//!
//! #[derive(PartialEq, Properties)]
//! struct ButtonProps {
//! #[prop_or_default]
//! pub attrs: Attrs,
//! #[prop_or_default]
//! pub children: Html,
//! }
//!
//! #[function_component]
//! fn Button(props: &ButtonProps) -> Html {
//! VTag::__new_other(
//! "button".into(),
//! Default::default(),
//! Default::default(),
//! props.attrs.attributes.clone(),
//! props.attrs.listeners.clone(),
//! props.children.clone(),
//! )
//! .into()
//! }
//!
//! #[function_component]
//! fn App() -> Html {
//! let on_click = |_| {};
//!
//! html! {
//! <Button attrs={attrs! {class="text-red" disabled=false onclick={on_click}}}>
//! {"Click"}
//! </Button>
//! }
//! }
//! ```

pub use yew_attrs_macro::attrs;

use yew::virtual_dom::{Attributes, Listeners};

/// Container for dynamic attributes and listeners.
#[derive(Debug, Default, PartialEq)]
pub struct Attrs {
/// Dynamic attributes.
pub attributes: Attributes,
/// Dynamic listeners.
pub listeners: Listeners,
}

Expand Down

0 comments on commit f5c71bb

Please sign in to comment.