Compare commits

..

2 Commits

Author SHA1 Message Date
fafdeffeeb metas reorgnaization started 2023-04-23 22:17:39 -04:00
4613ff38e2 improved click handler 2023-04-23 08:31:42 -04:00
4 changed files with 53 additions and 34 deletions

View File

@ -8,7 +8,7 @@ const Comp = React.lazy(()=>import("./deep/component.tsx"));
export default ()=> export default ()=>
{ {
return <div class="p-4 font-sans"> return <div class="p-4 font-sans">
<Iso.Metas title="Main Page!"/> <Iso.Meta.Metas title="Main Page!"/>
<nav class="p-4"> <nav class="p-4">
<a class="text-red-500" href="/">Home</a> <a class="text-red-500" href="/">Home</a>
<a href="/about">About</a> <a href="/about">About</a>

View File

@ -13,7 +13,7 @@ export default ()=>
console.log("render!!") console.log("render!!")
return <div class="p-4 text-red-500"> return <div class="p-4 text-red-500">
<Iso.Metas title="Component!"/> <Iso.Meta.Metas title="Component!"/>
Component Route is: {routeGet.Path.toString()} Component Route is: {routeGet.Path.toString()}
<button className="p-4 bg-green-500 text-white" onClick={e=>{countSet(countGet+1); routeSet(["lol", "idk"], {count:countGet+1});}}>{countGet}</button> <button className="p-4 bg-green-500 text-white" onClick={e=>{countSet(countGet+1); routeSet(["lol", "idk"], {count:countGet+1});}}>{countGet}</button>
<a href="/page/about-us" className="p-2 text(lg blue-500) font-bold">a link</a> <a href="/page/about-us" className="p-2 text(lg blue-500) font-bold">a link</a>

View File

@ -1,33 +1,42 @@
import React from "react"; import React from "react";
type MetasInputs = { [Property in MetaKeys]?: string };
type Meta = {title:string, description:string, keywords:string, image:string, canonical:string } type Meta = {title:string, description:string, keywords:string, image:string, canonical:string }
type MetaKeys = keyof Meta; type MetaKeys = keyof Meta;
export const Meta:Meta = {
title:"",
description:"",
keywords:"",
image:"",
canonical:""
};
type MetasInputs = { [Property in MetaKeys]?: string }; export const Meta =
export const Metas =(props:{concatListed?:boolean; dropUnlisted?:boolean}&MetasInputs):null=>
{ {
const additive = props.concatListed ? (key:MetaKeys, value:string)=> Meta[key] += value : (key:MetaKeys, value:string)=> Meta[key] = value; Meta: {
const subtractive = props.dropUnlisted ? (key:MetaKeys)=> Meta[key] = "" : (key:MetaKeys)=> {}; title:"",
description:"",
Object.keys(Meta).forEach((key)=>{ keywords:"",
const metaKey = key as MetaKeys; image:"",
const propValue = props[metaKey]||""; canonical:""
propValue ? additive(metaKey, propValue) : subtractive(metaKey); } as Meta,
}) Context: React.createContext([{} as Meta, ()=>{}] as [Get:Meta, Set:React.StateUpdater<Meta>]),
if(window.innerWidth) Provider({children}:{children:Children})
{ {
document.title = Meta.title; const binding = React.useState(Meta.Meta);
} return <Meta.Context.Provider value={binding}>{children}</Meta.Context.Provider>;
},
Metas(props:{concatListed?:boolean; dropUnlisted?:boolean}&MetasInputs):null
{
const additive = props.concatListed ? (key:MetaKeys, value:string)=> Meta.Meta[key] += value : (key:MetaKeys, value:string)=> Meta.Meta[key] = value;
const subtractive = props.dropUnlisted ? (key:MetaKeys)=> Meta.Meta[key] = "" : (key:MetaKeys)=> {};
return null; Object.keys(Meta).forEach((key)=>{
} const metaKey = key as MetaKeys;
const propValue = props[metaKey]||"";
propValue ? additive(metaKey, propValue) : subtractive(metaKey);
})
if(window.innerWidth)
{
document.title = Meta.Meta.title;
}
return null;
}
};
export type Children = string | number | React.JSX.Element | React.JSX.Element[]; export type Children = string | number | React.JSX.Element | React.JSX.Element[];
@ -78,14 +87,19 @@ export const Router = {
document.addEventListener("click", e=> document.addEventListener("click", e=>
{ {
const t = e.target as HTMLAnchorElement; const t = e.target as HTMLAnchorElement;
if(t.href) const path = e.composedPath() as HTMLAnchorElement[];
for(let i=0; i<path.length; i++)
{ {
const u = new URL(t.href); if(path[i].href)
if(u.origin == document.location.origin)
{ {
e.preventDefault(); const u = new URL(t.href);
const parts = Router.Parse(u); if(u.origin == document.location.origin)
routeUpdate(parts.Path, parts.Params, parts.Anchor); {
e.preventDefault();
const parts = Router.Parse(u);
routeUpdate(parts.Path, parts.Params, parts.Anchor);
}
return;
} }
} }
}) })

View File

@ -226,7 +226,7 @@ FileListen("${url.pathname}", reloadHandler);`;
Iso.Fetch.ServerBlocking = []; Iso.Fetch.ServerBlocking = [];
Iso.Fetch.ServerTouched = new Set(); Iso.Fetch.ServerTouched = new Set();
Iso.Fetch.ServerRemove = new Set(); Iso.Fetch.ServerRemove = new Set();
let app = <Iso.Router.Provider url={url}><App/></Iso.Router.Provider>; let app = <Iso.Router.Provider url={url}><Iso.Meta.Provider><App/></Iso.Meta.Provider></Iso.Router.Provider>;
await Prepass(app) await Prepass(app)
let bake = SSR(app); let bake = SSR(app);
while(Iso.Fetch.ServerBlocking.length) while(Iso.Fetch.ServerBlocking.length)
@ -235,7 +235,7 @@ FileListen("${url.pathname}", reloadHandler);`;
Iso.Fetch.ServerBlocking = []; Iso.Fetch.ServerBlocking = [];
// at this point, anything that was requested that was not cached, has now been loaded and cached // at this point, anything that was requested that was not cached, has now been loaded and cached
// this next render will use cached resources. using a cached resource (if its "Seed" is true) adds it to the "touched" set. // this next render will use cached resources. using a cached resource (if its "Seed" is true) adds it to the "touched" set.
app = <Iso.Router.Provider url={url}><App/></Iso.Router.Provider>; app = <Iso.Router.Provider url={url}><Iso.Meta.Provider><App/></Iso.Meta.Provider></Iso.Router.Provider>;
await Prepass(app) await Prepass(app)
bake = SSR(app); bake = SSR(app);
} }
@ -255,7 +255,7 @@ FileListen("${url.pathname}", reloadHandler);`;
`<!doctype html> `<!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<title>${Iso.Meta.title}</title> <title>${Iso.Meta.Meta.title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8"/> <meta charset="utf-8"/>
<style data-twind>${results.css}</style> <style data-twind>${results.css}</style>
@ -271,7 +271,12 @@ FileListen("${url.pathname}", reloadHandler);`;
Twind.install(CSS); Twind.install(CSS);
Fetch.Seed(${JSON.stringify(seed)}); Fetch.Seed(${JSON.stringify(seed)});
const hmrWrap = H( ()=>H(App) ); const hmrWrap = H( ()=>H(App) );
hydrate( H(Router.Provider, null, hmrWrap), document.querySelector("#app")); hydrate(
H(Router.Provider, null,
H(Meta.Provider, null, hmrWrap)
),
document.querySelector("#app")
);
</script> </script>
</body> </body>
</html>`; </html>`;