2023-05-16 10:26:00 -04:00
|
|
|
import React from "react";
|
|
|
|
import { CollapseControls, Collapser } from "./components/collapse.tsx";
|
|
|
|
|
|
|
|
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 default ()=>
|
|
|
|
{
|
|
|
|
const [stateGet, stateSet] = React.useState({done:true, open:false} as StateObj);
|
|
|
|
const setter =(args:StateArgs)=> stateSet({...stateGet, ...args});
|
|
|
|
|
|
|
|
return <CTX.Provider value={[stateGet, setter]}>
|
|
|
|
<Inner/>
|
|
|
|
</CTX.Provider>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Inner =()=>
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
|
|
|
|
React.useEffect(()=>
|
|
|
|
{
|
2023-05-16 13:44:37 -04:00
|
|
|
refControl.current = Collapser(refElement.current as HTMLElement, true);
|
2023-05-16 10:26:00 -04:00
|
|
|
}
|
|
|
|
, []);
|
|
|
|
|
|
|
|
React.useEffect(()=>
|
|
|
|
{
|
|
|
|
console.log("open changed to:", stateGet.open);
|
|
|
|
refControl.current && refControl.current(stateGet.open, 1000, ()=>stateSet({done:true}));
|
2023-05-16 13:44:37 -04:00
|
|
|
|
2023-05-16 10:26:00 -04:00
|
|
|
}, [stateGet.open])
|
|
|
|
|
|
|
|
React.useEffect(()=>{
|
|
|
|
console.log("done changed to:", stateGet.done);
|
|
|
|
}, [stateGet.done])
|
|
|
|
|
|
|
|
return <div class="p-4">
|
|
|
|
<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>
|
|
|
|
<div ref={refElement as React.Ref<HTMLDivElement>}>
|
2023-05-16 10:26:00 -04:00
|
|
|
<p>hello</p>
|
|
|
|
<p>hello</p>
|
|
|
|
<p>hello</p>
|
|
|
|
<p>hello</p>
|
|
|
|
<p>hello</p>
|
|
|
|
<p>hello</p>
|
|
|
|
<p>hello</p>
|
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
|
|
|
|
};
|