Skip to content

Commit

Permalink
✨ Add trait fn that can inject custom header or footer.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Dec 20, 2023
1 parent d757945 commit 0d5c2e4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
16 changes: 9 additions & 7 deletions packages/boot/tests/register_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ mod test {
impl DeclType for App {
type Routes = Routes;
type AppStates = AppStates;

fn render_outside(props: &hikari_macro_types::RoutesOutsideProps) -> yew::Html {
yew::html! {
<>
<h1>{"Hikari DEMO"}</h1>
{props.children.clone()}
</>
}
}
}

#[tokio::test]
Expand All @@ -72,12 +81,5 @@ mod test {
#[wasm_bindgen_test::wasm_bindgen_test]
fn render_on_client() {
let html = App.App();

assert_eq!(
html,
yew::html! {
<div>{"Portal"}</div>
}
);
}
}
13 changes: 13 additions & 0 deletions packages/macro-types/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ where
pub states: T,
}

#[derive(Debug, PartialEq, Clone, ::yew::Properties)]
pub struct RoutesOutsideProps {
pub children: ::yew::Html,
}

#[async_trait::async_trait]
pub trait Application: DeclType {
async fn render_to_string(url: String, status: <Self as DeclType>::AppStates) -> String;
Expand All @@ -26,4 +31,12 @@ where
{
type Routes;
type AppStates;

fn render_outside(props: &RoutesOutsideProps) -> ::yew::Html {
::yew::html! {
<>
{props.children.clone()}
</>
}
}
}
25 changes: 16 additions & 9 deletions packages/macro/src/utils/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,29 @@ pub fn root(input: DeriveApp) -> TokenStream {

html! {
<ContextProvider<AppStatesContextProviderType> context={ctx.clone()}>
<HikariContent />
{
<#ident as ::hikari_boot::DeclType>::render_outside(&::hikari_boot::RoutesOutsideProps {
children: ::yew::html! {
<HikariRoutesContent />
}
})
}
</ContextProvider<AppStatesContextProviderType>>
}
}

#[::stylist::yew::styled_component]
pub fn HikariContent() -> yew::Html {
#[::yew::function_component]
pub fn HikariRoutesContent() -> yew::Html {
use yew::prelude::*;
use yew_router::prelude::*;

html! {
<>
<Switch<<#ident as ::hikari_boot::DeclType>::Routes>
render={|r| <<#ident as ::hikari_boot::DeclType>::Routes as ::hikari_boot::DeclRoutes>::switch(&r)}
/>
</>
<Switch<<#ident as ::hikari_boot::DeclType>::Routes>
render={
|r| {
<<#ident as ::hikari_boot::DeclType>::Routes as ::hikari_boot::DeclRoutes>::switch(&r)
}
}
/>
}
}

Expand Down
12 changes: 7 additions & 5 deletions packages/macro/src/utils/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use quote::quote;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{Data, DeriveInput, Fields, Ident, Path, Variant};
use syn::{Data, DeriveInput, Fields, Ident, Path, Type, Variant};

const COMPONENT_ATTR_IDENT: &str = "component";

pub struct DeriveRoutes {
ident: Ident,
components: HashMap<Ident, (Vec<Ident>, Path)>,
components: HashMap<Ident, (Vec<(Ident, Type)>, Path)>,
}

impl Parse for DeriveRoutes {
Expand Down Expand Up @@ -42,8 +42,8 @@ impl Parse for DeriveRoutes {

fn parse_variants_attributes(
variants: &Punctuated<Variant, syn::token::Comma>,
) -> syn::Result<HashMap<Ident, (Vec<Ident>, Path)>> {
let mut components: HashMap<Ident, (Vec<Ident>, Path)> = Default::default();
) -> syn::Result<HashMap<Ident, (Vec<(Ident, Type)>, Path)>> {
let mut components: HashMap<Ident, (Vec<(Ident, Type)>, Path)> = Default::default();

for variant in variants.iter() {
if let Fields::Unnamed(ref field) = variant.fields {
Expand All @@ -56,7 +56,7 @@ fn parse_variants_attributes(
let args = variant
.fields
.iter()
.map(|field| field.ident.clone().unwrap())
.map(|field| (field.ident.clone().unwrap(), field.ty.clone()))
.collect::<Vec<_>>();

let attrs = &variant.attrs;
Expand Down Expand Up @@ -92,6 +92,7 @@ pub fn root(input: DeriveRoutes) -> TokenStream {
let DeriveRoutes {
components, ident, ..
} = &input;

let components = components
.iter()
.map(|(key, (fields, path))| {
Expand All @@ -102,6 +103,7 @@ pub fn root(input: DeriveRoutes) -> TokenStream {
}
}
} else {
let (fields, _) = fields.iter().cloned().unzip::<_, _, Vec<_>, Vec<_>>();
quote! {
#ident::#key { #(#fields),* } => ::yew::html! {
<#path
Expand Down

0 comments on commit 0d5c2e4

Please sign in to comment.