2023-05-17 22:30:36 -04:00
|
|
|
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 );
|
|
|
|
|
2023-05-21 22:12:47 -04:00
|
|
|
type MenuClassStates = {Keep:string, Open:string, Shut:string, Move:string, Exit:string};
|
2023-05-24 15:40:03 -04:00
|
|
|
const base = `relative transition-all border(8 black) overflow-hidden`;
|
2023-05-21 22:12:47 -04:00
|
|
|
const Classes:MenuClassStates =
|
|
|
|
{
|
2023-05-27 07:28:50 -04:00
|
|
|
Shut: `${base} h-0 top-0 w-1/2 duration-300`,
|
2023-05-28 07:34:45 -04:00
|
|
|
Open: `${base} h-auto top-8 w-full duration-1000`,
|
2023-05-27 07:28:50 -04:00
|
|
|
lol: `${base} h-auto top-36 bg-yellow-500 w-2/3 duration-700`,
|
2023-05-21 22:12:47 -04:00
|
|
|
};
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
2023-05-17 22:30:36 -04:00
|
|
|
|
2023-05-28 07:34:45 -04:00
|
|
|
const initialKey = stateGet.open ? "Open" : "Shut";
|
2023-05-17 22:30:36 -04:00
|
|
|
|
2023-05-28 07:34:45 -04:00
|
|
|
React.useEffect(()=> {
|
|
|
|
refElement.current?.setAttribute("style", "");
|
|
|
|
refControl.current = refElement.current && Collapser(refElement.current, initialKey, Classes);
|
|
|
|
}, []);
|
|
|
|
React.useEffect(()=> {refControl.current && refControl.current(initialKey, ()=>stateSet({done:true}))}, [stateGet.open])
|
|
|
|
|
|
|
|
return <div ref={refElement as React.Ref<HTMLDivElement>} class={Classes.Shut} style="transition:none;">
|
2023-05-21 22:12:47 -04:00
|
|
|
{ (!stateGet.open && stateGet.done) ? null : props.children}
|
|
|
|
</div>;
|
2023-05-17 22:30:36 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
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>
|
|
|
|
</>;
|
|
|
|
};
|
|
|
|
|
2023-05-24 15:40:03 -04:00
|
|
|
type StyleSize = [classes:string, width:number, height:number];
|
|
|
|
type StylePack = Record<string, string>;
|
|
|
|
type StyleCalc = Record<string, StyleSize>;
|
|
|
|
const StyleCalc =(inElement:HTMLElement, inClasses:StylePack)=>
|
|
|
|
{
|
|
|
|
const initialStyle = inElement.getAttribute("style")||"";
|
|
|
|
const initialClass = inElement.getAttribute("class")||"";
|
|
|
|
const output = {} as StyleCalc;
|
|
|
|
|
|
|
|
inElement.setAttribute("style", `transition: none; overflow: hidden;`);
|
|
|
|
Object.entries(inClasses).forEach(([key, value])=>
|
|
|
|
{
|
|
|
|
inElement.setAttribute("class", value);
|
|
|
|
output[key] = [value, inElement.offsetWidth, inElement.offsetHeight];
|
|
|
|
});
|
|
|
|
inElement.setAttribute("class", initialClass);
|
2023-05-27 07:28:50 -04:00
|
|
|
inElement.offsetHeight;
|
|
|
|
inElement.setAttribute("style", initialStyle);
|
2023-05-24 15:40:03 -04:00
|
|
|
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
2023-05-17 22:30:36 -04:00
|
|
|
type DoneCallback =(openState:boolean)=>void;
|
2023-05-24 15:40:03 -04:00
|
|
|
export type CollapseControls =(inOpen?:string, inDone?:DoneCallback)=>void;
|
|
|
|
export function Collapser(inElement:HTMLElement, initialState:string, library:Record<string, string>)
|
2023-05-17 22:30:36 -04:00
|
|
|
{
|
|
|
|
let userDone:DoneCallback = (openState) => {};
|
|
|
|
let userMode = initialState;
|
|
|
|
let frameRequest = 0;
|
2023-05-21 22:32:16 -04:00
|
|
|
let inTransition = false;
|
2023-05-24 15:40:03 -04:00
|
|
|
let measurements:StyleCalc;
|
|
|
|
const transitions:Set<string> = new Set();
|
2023-05-21 22:12:47 -04:00
|
|
|
|
|
|
|
const run = (inEvent:TransitionEvent)=>
|
2023-05-17 22:30:36 -04:00
|
|
|
{
|
2023-05-24 15:40:03 -04:00
|
|
|
console.log("+", inEvent.propertyName);
|
|
|
|
(inEvent.target == inElement) && transitions.add(inEvent.propertyName);
|
2023-05-21 22:12:47 -04:00
|
|
|
};
|
|
|
|
const end = (inEvent:TransitionEvent)=>
|
|
|
|
{
|
2023-05-24 15:40:03 -04:00
|
|
|
console.log("-", inEvent.propertyName);
|
2023-05-21 22:12:47 -04:00
|
|
|
if (inEvent.target == inElement)
|
|
|
|
{
|
|
|
|
transitions.delete(inEvent.propertyName);
|
|
|
|
if(transitions.size === 0)
|
2023-05-17 22:30:36 -04:00
|
|
|
{
|
2023-05-24 15:40:03 -04:00
|
|
|
console.log("--done--", userMode);
|
|
|
|
inElement.setAttribute("style", "transition:none;");
|
|
|
|
inElement.clientHeight;
|
|
|
|
frameRequest = requestAnimationFrame(()=>
|
2023-05-21 22:12:47 -04:00
|
|
|
{
|
2023-05-24 15:40:03 -04:00
|
|
|
inElement.setAttribute("style", "");
|
|
|
|
inTransition = false;
|
|
|
|
userDone(userMode);
|
|
|
|
});
|
2023-05-17 22:30:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-05-28 07:34:45 -04:00
|
|
|
|
|
|
|
const child =(e:CustomEvent)=>
|
|
|
|
{
|
|
|
|
if(e.target == inElement || !inTransition){ return; }
|
|
|
|
console.log("resize", e.detail);
|
|
|
|
const oldWidth = inElement.offsetWidth;
|
|
|
|
const oldHeight = inElement.offsetHeight;
|
|
|
|
//inElement.style.overflow = "hidden";
|
|
|
|
inElement.style.width = oldWidth + e.detail[0] + "px";
|
|
|
|
inElement.style.height = oldHeight + e.detail[1] + "px";
|
|
|
|
}
|
|
|
|
|
2023-05-21 22:12:47 -04:00
|
|
|
inElement.addEventListener("transitionend", end);
|
|
|
|
inElement.addEventListener("transitionrun", run);
|
2023-05-28 07:34:45 -04:00
|
|
|
inElement.addEventListener("childresize", child);
|
2023-05-17 22:30:36 -04:00
|
|
|
|
2023-05-24 15:40:03 -04:00
|
|
|
return function(inState:string, inDone)
|
2023-05-17 22:30:36 -04:00
|
|
|
{
|
|
|
|
cancelAnimationFrame(frameRequest);
|
|
|
|
|
|
|
|
if(arguments.length)
|
|
|
|
{
|
2023-05-24 15:40:03 -04:00
|
|
|
if(!library[inState]){ return; }
|
|
|
|
|
2023-05-17 22:30:36 -04:00
|
|
|
userDone = inDone|| ((m)=>{}) as DoneCallback;
|
2023-05-24 15:40:03 -04:00
|
|
|
userMode = inState;
|
2023-05-17 22:30:36 -04:00
|
|
|
|
2023-05-24 15:40:03 -04:00
|
|
|
if(!inTransition)
|
2023-05-21 22:32:16 -04:00
|
|
|
{
|
2023-05-24 15:40:03 -04:00
|
|
|
measurements = StyleCalc(inElement, library);
|
|
|
|
console.log("measurements taken", measurements)
|
2023-05-21 22:32:16 -04:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:40:03 -04:00
|
|
|
if(measurements)
|
2023-05-17 22:30:36 -04:00
|
|
|
{
|
2023-05-27 07:28:50 -04:00
|
|
|
const [classes, width, height] = measurements[inState] as StyleSize;
|
2023-05-28 07:34:45 -04:00
|
|
|
const oldWidth = inElement.offsetWidth;
|
|
|
|
const oldHeight = inElement.offsetHeight;
|
2023-05-27 07:28:50 -04:00
|
|
|
//inElement.style.overflow = "hidden";
|
2023-05-28 07:34:45 -04:00
|
|
|
inElement.style.width = oldWidth + "px";
|
|
|
|
inElement.style.height = oldHeight + "px";
|
2023-05-27 07:28:50 -04:00
|
|
|
inTransition = true;
|
|
|
|
console.log(`from: {${inElement.offsetWidth} ${inElement.offsetHeight}}`);
|
|
|
|
|
2023-05-28 07:34:45 -04:00
|
|
|
inElement.dispatchEvent(new CustomEvent("childresize", {bubbles:true, detail:[width-oldWidth, height-oldHeight]}))
|
|
|
|
|
2023-05-27 07:28:50 -04:00
|
|
|
frameRequest = requestAnimationFrame(()=>
|
|
|
|
{
|
|
|
|
inElement.style.height = height + "px";
|
|
|
|
inElement.style.width = width + "px";
|
|
|
|
inElement.className = classes;
|
|
|
|
console.log(` to: {${width} ${height}}`)
|
|
|
|
});
|
2023-05-24 15:40:03 -04:00
|
|
|
}
|
2023-05-17 22:30:36 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-21 22:12:47 -04:00
|
|
|
inElement.removeEventListener("transitionrun", run);
|
|
|
|
inElement.removeEventListener("transitionend", end);
|
2023-05-17 22:30:36 -04:00
|
|
|
}
|
|
|
|
} as CollapseControls;
|
2023-05-24 15:40:03 -04:00
|
|
|
|
2023-05-17 22:30:36 -04:00
|
|
|
}
|