StyleCalc started
This commit is contained in:
parent
73ecde256b
commit
049e4c0f0e
@ -19,13 +19,11 @@ export const Menu =(props:{children:React.JSX.Element|React.JSX.Element[]})=>
|
|||||||
const refControl:React.MutableRefObject<CollapseControls|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};
|
type MenuClassStates = {Keep:string, Open:string, Shut:string, Move:string, Exit:string};
|
||||||
|
const base = `relative transition-all border(8 black) overflow-hidden`;
|
||||||
const Classes:MenuClassStates =
|
const Classes:MenuClassStates =
|
||||||
{
|
{
|
||||||
Keep: "relative transition-all border(8 black) ",
|
Open: `${base} h-auto top-8 duration-700`,
|
||||||
Open: "h-auto w-1/2 top-8 duration-700",
|
Shut: `${base} h-0 top-0 duration-300`,
|
||||||
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};
|
const Window = window as {TwindInst?:(c:string)=>string};
|
||||||
if(Window.TwindInst)
|
if(Window.TwindInst)
|
||||||
@ -36,8 +34,8 @@ export const Menu =(props:{children:React.JSX.Element|React.JSX.Element[]})=>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
React.useEffect(()=>refControl.current = Collapser(refElement.current as HTMLElement, true), []);
|
React.useEffect(()=>refControl.current = Collapser(refElement.current as HTMLElement, "Shut", Classes), []);
|
||||||
React.useEffect(()=> {refControl.current && refControl.current(stateGet.open, 1000, ()=>stateSet({done:true}))}, [stateGet.open])
|
React.useEffect(()=> {refControl.current && refControl.current(stateGet.open ? "Open" : "Shut", ()=>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>}>
|
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}
|
{ (!stateGet.open && stateGet.done) ? null : props.children}
|
||||||
@ -54,101 +52,111 @@ export const Button =()=>
|
|||||||
</>;
|
</>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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;`);
|
||||||
|
inElement.clientHeight;
|
||||||
|
Object.entries(inClasses).forEach(([key, value])=>
|
||||||
|
{
|
||||||
|
inElement.setAttribute("class", value);
|
||||||
|
inElement.clientHeight;
|
||||||
|
output[key] = [value, inElement.offsetWidth, inElement.offsetHeight];
|
||||||
|
});
|
||||||
|
inElement.setAttribute("style", initialStyle);
|
||||||
|
inElement.setAttribute("class", initialClass);
|
||||||
|
inElement.clientHeight;
|
||||||
|
|
||||||
|
return output;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
type DoneCallback =(openState:boolean)=>void;
|
type DoneCallback =(openState:boolean)=>void;
|
||||||
export type CollapseControls =(inOpen?:boolean, inMs?:number, inDone?:DoneCallback)=>void;
|
export type CollapseControls =(inOpen?:string, inDone?:DoneCallback)=>void;
|
||||||
export function Collapser(inElement:HTMLElement, initialState = false)
|
export function Collapser(inElement:HTMLElement, initialState:string, library:Record<string, string>)
|
||||||
{
|
{
|
||||||
let userDone:DoneCallback = (openState) => {};
|
let userDone:DoneCallback = (openState) => {};
|
||||||
let userMode = initialState;
|
let userMode = initialState;
|
||||||
let frameRequest = 0;
|
let frameRequest = 0;
|
||||||
let inTransition = false;
|
let inTransition = false;
|
||||||
|
let measurements:StyleCalc;
|
||||||
let transitions = new Set();
|
const transitions:Set<string> = new Set();
|
||||||
let measurements:false|Record<string, { width: number; height: number; }>
|
|
||||||
|
|
||||||
const run = (inEvent:TransitionEvent)=>
|
const run = (inEvent:TransitionEvent)=>
|
||||||
{
|
{
|
||||||
if (inEvent.target == inElement)
|
console.log("+", inEvent.propertyName);
|
||||||
{
|
(inEvent.target == inElement) && transitions.add(inEvent.propertyName);
|
||||||
transitions.add(inEvent.propertyName);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const end = (inEvent:TransitionEvent)=>
|
const end = (inEvent:TransitionEvent)=>
|
||||||
{
|
{
|
||||||
|
console.log("-", inEvent.propertyName);
|
||||||
if (inEvent.target == inElement)
|
if (inEvent.target == inElement)
|
||||||
{
|
{
|
||||||
transitions.delete(inEvent.propertyName);
|
transitions.delete(inEvent.propertyName);
|
||||||
if(transitions.size === 0)
|
if(transitions.size === 0)
|
||||||
{
|
{
|
||||||
if (userMode)
|
console.log("--done--", userMode);
|
||||||
{
|
|
||||||
inElement.setAttribute("style", "transition:none;");
|
inElement.setAttribute("style", "transition:none;");
|
||||||
inElement.clientHeight;
|
inElement.clientHeight;
|
||||||
frameRequest = requestAnimationFrame(()=>{inElement.setAttribute("style", "")});
|
frameRequest = requestAnimationFrame(()=>
|
||||||
}
|
{
|
||||||
|
inElement.setAttribute("style", "");
|
||||||
inTransition = false;
|
inTransition = false;
|
||||||
userDone(userMode);
|
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("transitionend", end);
|
||||||
inElement.addEventListener("transitionrun", run);
|
inElement.addEventListener("transitionrun", run);
|
||||||
|
|
||||||
return function(inOpen, inMs, inDone)
|
return function(inState:string, inDone)
|
||||||
{
|
{
|
||||||
cancelAnimationFrame(frameRequest);
|
cancelAnimationFrame(frameRequest);
|
||||||
|
|
||||||
if(arguments.length)
|
if(arguments.length)
|
||||||
{
|
{
|
||||||
userDone = inDone|| ((m)=>{}) as DoneCallback;
|
if(!library[inState]){ return; }
|
||||||
userMode = inOpen === true;
|
|
||||||
|
|
||||||
if((inOpen || !measurements) && !inTransition)
|
userDone = inDone|| ((m)=>{}) as DoneCallback;
|
||||||
|
userMode = inState;
|
||||||
|
|
||||||
|
if(!inTransition)
|
||||||
{
|
{
|
||||||
const keep = inElement.getAttribute("data-class-keep")+" ";
|
measurements = StyleCalc(inElement, library);
|
||||||
measurements = getMeasurements({
|
console.log("measurements taken", measurements)
|
||||||
Open: keep + inElement.getAttribute("data-class-open"),
|
|
||||||
Shut: keep + inElement.getAttribute("data-class-shut"),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(measurements)
|
||||||
|
{
|
||||||
|
const [classes, width, height] = measurements[inState] as StyleSize;
|
||||||
inElement.style.overflow = "hidden";
|
inElement.style.overflow = "hidden";
|
||||||
inElement.style.width = inElement.offsetWidth + "px";
|
inElement.style.width = inElement.offsetWidth + "px";
|
||||||
inElement.style.height = inElement.offsetHeight + "px";
|
inElement.style.height = inElement.offsetHeight + "px";
|
||||||
|
|
||||||
inTransition = true;
|
inTransition = true;
|
||||||
|
console.log(`from: {${inElement.offsetWidth} ${inElement.offsetHeight}}`)
|
||||||
frameRequest = requestAnimationFrame(()=>
|
frameRequest = requestAnimationFrame(()=>
|
||||||
{
|
{
|
||||||
const measure = inOpen ? measurements.Open : measurements.Shut;
|
inElement.style.width = width + "px";
|
||||||
inElement.style.width = measure.width + "px";
|
inElement.style.height = height + "px";
|
||||||
inElement.style.height = measure.height + "px";
|
inElement.className = classes;
|
||||||
inElement.className = inElement.getAttribute("data-class-keep") + " " + inElement.getAttribute(`data-class-${inOpen?"open":"shut"}`);
|
console.log(` to: {${width} ${height}}`)
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
inElement.removeEventListener("transitionrun", run);
|
inElement.removeEventListener("transitionrun", run);
|
||||||
inElement.removeEventListener("transitionend", end);
|
inElement.removeEventListener("transitionend", end);
|
||||||
}
|
}
|
||||||
} as CollapseControls;
|
} as CollapseControls;
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user