eno-example/context.tsx

87 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-05-16 10:26:00 -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);
2023-05-16 20:23:06 -04:00
export const Group =(props:{children:React.JSX.Element|React.JSX.Element[]})=>
2023-05-16 10:26:00 -04:00
{
const [stateGet, stateSet] = React.useState({done:true, open:false} as StateObj);
2023-05-16 20:23:06 -04:00
return <CTX.Provider value={[stateGet, (args:StateArgs)=> stateSet({...stateGet, ...args})]}>{props.children}</CTX.Provider>;
};
2023-05-16 10:26:00 -04:00
2023-05-16 20:23:06 -04:00
export const Menu =(props:{children:React.JSX.Element|React.JSX.Element[]})=>
2023-05-16 10:26:00 -04:00
{
const [stateGet, stateSet] = React.useContext(CTX);
2023-05-16 13:44:37 -04:00
const refElement:React.MutableRefObject<HTMLElement|null> = React.useRef( null );
2023-05-16 10:26:00 -04:00
const refControl:React.MutableRefObject<CollapseControls|null> = React.useRef( null );
2023-05-16 20:23:06 -04:00
React.useEffect(()=>refControl.current = Collapser(refElement.current as HTMLElement, true), []);
React.useEffect(()=> {refControl.current && refControl.current(stateGet.open, 1000, ()=>stateSet({done:true}))}, [stateGet.open])
2023-05-16 13:44:37 -04:00
2023-05-16 20:23:06 -04:00
return <div ref={refElement as React.Ref<HTMLDivElement>}>{props.children}</div>;
};
2023-05-16 10:26:00 -04:00
2023-05-16 20:23:06 -04:00
export const Button =()=>
{
const [stateGet, stateSet] = React.useContext(CTX);
return <>
2023-05-16 10:26:00 -04:00
<p>{JSON.stringify(stateGet)}</p>
2023-05-16 13:44:37 -04:00
<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-16 20:23:06 -04:00
</>;
2023-05-16 10:26:00 -04:00
};
2023-05-16 20:23:06 -04:00
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;
const done = (inEvent:TransitionEvent)=>
{
if (inEvent.propertyName == "height" && inEvent.target == inElement)
{
inEvent.stopPropagation();
if (userMode)
{
inElement.style.height = "auto";
inElement.style.overflow = "visible";
}
userDone(userMode);
}
};
inElement.addEventListener("transitionend", done);
return function(inOpen, inMs, inDone)
{
cancelAnimationFrame(frameRequest);
if(arguments.length)
{
userDone = inDone|| ((m)=>{}) as DoneCallback;
userMode = inOpen === true;
inElement.style.height = inElement.clientHeight + "px";
inElement.style.overflow = "hidden";
inElement.style.transition = "none";
frameRequest = requestAnimationFrame(()=>
{
inElement.style.transition = `height ${(inMs||1000) / 1000}s`;
frameRequest = requestAnimationFrame(()=>
{
inElement.style.height = `${inOpen ? inElement.scrollHeight : 0}px`;
});
});
}
else
{
inElement.removeEventListener("transitionend", done);
}
} as CollapseControls;
}