Compare commits

...

10 Commits

3 changed files with 173 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import React from "react";
import * as Iso from "@eno/iso";
import * as C from "./tree-menu.tsx";
/*
const delay =(inMS:number)=>
@ -33,6 +34,20 @@ export default ()=>
<a href="/about">About</a>
</nav>
<C.Group>
<C.Button></C.Button>
<C.Menu>
<p>hello!</p>
<p>hello!</p>
<p>hello!</p>
<p>hello!</p>
<p>hello!</p>
<p>hello!</p>
<p>hello!</p>
</C.Menu>
</C.Group>
<h1 class="my-2 font(bold serif) text(3xl)">Title!!</h1>
<h2>suspended:</h2>
<React.Suspense fallback={<div>Loading!</div>}>

154
example/tree-menu.tsx Normal file
View File

@ -0,0 +1,154 @@
import React from "react";
type StateArgs = {done?:boolean, open?:boolean};
type StateObj = {done:boolean, open:boolean};
type StateBinding = [state:StateObj, update:(args:StateArgs)=>void];
const CTX = React.createContext([{done:true, open:false}, (args)=>{}] as StateBinding);
export const Group =(props:{children:React.JSX.Element|React.JSX.Element[]})=>
{
const [stateGet, stateSet] = React.useState({done:true, open:false} as StateObj);
return <CTX.Provider value={[stateGet, (args:StateArgs)=> stateSet({...stateGet, ...args})]}>{props.children}</CTX.Provider>;
};
export const Menu =(props:{children:React.JSX.Element|React.JSX.Element[]})=>
{
const [stateGet, stateSet] = React.useContext(CTX);
const refElement:React.MutableRefObject<HTMLElement|null> = React.useRef( null );
const refControl:React.MutableRefObject<CollapseControls|null> = React.useRef( null );
type MenuClassStates = {Keep:string, Open:string, Shut:string, Move:string, Exit:string};
const Classes:MenuClassStates =
{
Keep: "relative transition-all border(8 black) ",
Open: "h-auto w-1/2 top-8 duration-700",
Shut: "h-0 w-[300px] top-0 duration-300",
Move: "",
Exit: "h-auto w-auto top-36 px-4 opacity-0"
};
const Window = window as {TwindInst?:(c:string)=>string};
if(Window.TwindInst)
{
for(let stateName in Classes)
{
Classes[stateName as keyof MenuClassStates] = Window.TwindInst(Classes[stateName as keyof MenuClassStates]);
}
}
React.useEffect(()=>refControl.current = Collapser(refElement.current as HTMLElement, true), []);
React.useEffect(()=> {refControl.current && refControl.current(stateGet.open, 1000, ()=>stateSet({done:true}))}, [stateGet.open])
return <div class={Classes.Keep} data-class-keep={Classes.Keep} data-class-open={Classes.Open} data-class-shut={Classes.Shut} ref={refElement as React.Ref<HTMLDivElement>}>
{ (!stateGet.open && stateGet.done) ? null : props.children}
</div>;
};
export const Button =()=>
{
const [stateGet, stateSet] = React.useContext(CTX);
return <>
<p>{JSON.stringify(stateGet)}</p>
<button class="px-10 py-2 bg-red-500 text-white" onClick={e=>stateSet({open:true, done:false})}>Open</button>
<button class="px-10 py-2 bg-red-500 text-white" onClick={e=>stateSet({open:false, done:false})}>Close</button>
</>;
};
type DoneCallback =(openState:boolean)=>void;
export type CollapseControls =(inOpen?:boolean, inMs?:number, inDone?:DoneCallback)=>void;
export function Collapser(inElement:HTMLElement, initialState = false)
{
let userDone:DoneCallback = (openState) => {};
let userMode = initialState;
let frameRequest = 0;
let inTransition = false;
let transitions = new Set();
let measurements:false|Record<string, { width: number; height: number; }>
const run = (inEvent:TransitionEvent)=>
{
if (inEvent.target == inElement)
{
transitions.add(inEvent.propertyName);
}
};
const end = (inEvent:TransitionEvent)=>
{
if (inEvent.target == inElement)
{
transitions.delete(inEvent.propertyName);
if(transitions.size === 0)
{
if (userMode)
{
inElement.setAttribute("style", "transition:none;");
inElement.clientHeight;
frameRequest = requestAnimationFrame(()=>{inElement.setAttribute("style", "")});
}
inTransition = false;
userDone(userMode);
}
}
};
const getMeasurements =(inClasses:Record<string, string>)=>
{
inElement.setAttribute("style", `transition: none;`);
const initialClasses = inElement.getAttribute("class")||"";
const output = {} as Record<string, {width:number, height:number}>;
Object.entries(inClasses).forEach(([key, value])=>
{
inElement.setAttribute("class", value);
output[key] = { width: inElement.offsetWidth, height: inElement.offsetHeight };
});
inElement.removeAttribute("style");
inElement.setAttribute("class", initialClasses);
return output;
};
inElement.addEventListener("transitionend", end);
inElement.addEventListener("transitionrun", run);
return function(inOpen, inMs, inDone)
{
cancelAnimationFrame(frameRequest);
if(arguments.length)
{
userDone = inDone|| ((m)=>{}) as DoneCallback;
userMode = inOpen === true;
if((inOpen || !measurements) && !inTransition)
{
const keep = inElement.getAttribute("data-class-keep")+" ";
measurements = getMeasurements({
Open: keep + inElement.getAttribute("data-class-open"),
Shut: keep + inElement.getAttribute("data-class-shut"),
});
}
inElement.style.overflow = "hidden";
inElement.style.width = inElement.offsetWidth + "px";
inElement.style.height = inElement.offsetHeight + "px";
inTransition = true;
frameRequest = requestAnimationFrame(()=>
{
const measure = inOpen ? measurements.Open : measurements.Shut;
inElement.style.width = measure.width + "px";
inElement.style.height = measure.height + "px";
inElement.className = inElement.getAttribute("data-class-keep") + " " + inElement.getAttribute(`data-class-${inOpen?"open":"shut"}`);
});
}
else
{
inElement.removeEventListener("transitionrun", run);
inElement.removeEventListener("transitionend", end);
}
} as CollapseControls;
}

View File

@ -304,6 +304,9 @@ console.log(`Args seen: `, Flags);
const {Transpileable, TranspileURL, SocketsHandler} = Transpiler(DevMode);
let {Imports, App, TwindInst, Error} = await Configure(DevMode, Path.LibDir);
//@ts-ignore
window.TwindInst = TwindInst;
if(Error)
{
console.log(Error);
@ -423,7 +426,7 @@ else if(App && TwindInst)
import * as Twind from "https://esm.sh/v115/@twind/core@1.1.3/es2022/core.mjs";
import * as App from "@eno/app";
import {Router, Fetch, CSS, Meta} from "@eno/iso";
Twind.install(App.CSS ? {...CSS, ...App.CSS} : CSS);
window.TwindInst = Twind.install(App.CSS ? {...CSS, ...App.CSS} : CSS);
Fetch.Seed(${JSON.stringify(seed)});
const hmrWrap = H( ()=>H(App.default) );
hydrate(