Compare commits
No commits in common. "80bcb1f05e5b15ae28d6dd8bcbeb78587114fa5f" and "4663698923b9cf30bd57a09358b950cb768b0280" have entirely different histories.
80bcb1f05e
...
4663698923
9
app.tsx
9
app.tsx
@ -1,7 +1,6 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import * as Iso from "@eno/iso";
|
import * as Iso from "@eno/iso";
|
||||||
import Collapse, {CollapseButton, CollapseGroup} from "$/collapse.tsx";
|
import Collapse, {CollapseButton, CollapseGroup} from "$/collapse.tsx";
|
||||||
import C from "./context.tsx";
|
|
||||||
|
|
||||||
const Comp = React.lazy(()=>import("./deep/component.tsx"));
|
const Comp = React.lazy(()=>import("./deep/component.tsx"));
|
||||||
|
|
||||||
@ -21,9 +20,11 @@ export default ()=>
|
|||||||
<p>Hello!</p>
|
<p>Hello!</p>
|
||||||
</Collapse>
|
</Collapse>
|
||||||
</header>
|
</header>
|
||||||
|
<div>
|
||||||
<C/>
|
<CollapseGroup>
|
||||||
|
<CollapseButton></CollapseButton>
|
||||||
|
</CollapseGroup>
|
||||||
|
</div>
|
||||||
<main class="layout-middle">
|
<main class="layout-middle">
|
||||||
<Iso.Switch>
|
<Iso.Switch>
|
||||||
<Iso.Case value="/">
|
<Iso.Case value="/">
|
||||||
|
@ -74,7 +74,7 @@ export const CollapseButton =()=>
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DoneCallback =(openState:boolean)=>void;
|
type DoneCallback =(openState:boolean)=>void;
|
||||||
export type CollapseControls =(inOpen?:boolean, inMs?:number, inDone?:DoneCallback)=>void;
|
type CollapseControls =(inOpen?:boolean, inMs?:number, inDone?:DoneCallback)=>void;
|
||||||
export function Collapser(inElement:HTMLElement, initialState = false)
|
export function Collapser(inElement:HTMLElement, initialState = false)
|
||||||
{
|
{
|
||||||
let userDone:DoneCallback = (openState) => {};
|
let userDone:DoneCallback = (openState) => {};
|
||||||
@ -96,26 +96,21 @@ export function Collapser(inElement:HTMLElement, initialState = false)
|
|||||||
};
|
};
|
||||||
inElement.addEventListener("transitionend", done);
|
inElement.addEventListener("transitionend", done);
|
||||||
|
|
||||||
return function(inOpen, inMs, inDone)
|
return function(inOpen?:boolean, inMs?:number, inDone?:DoneCallback)
|
||||||
{
|
{
|
||||||
cancelAnimationFrame(frameRequest);
|
cancelAnimationFrame(frameRequest);
|
||||||
|
|
||||||
if(arguments.length)
|
if(arguments.length)
|
||||||
{
|
{
|
||||||
userDone = inDone|| ((m)=>{}) as DoneCallback;
|
userDone = inDone|| ((m)=>{}) as DoneCallback;
|
||||||
userMode = inOpen === true;
|
userMode = inOpen||!userMode;
|
||||||
|
|
||||||
inElement.style.height = inElement.clientHeight + "px";
|
inElement.style.height = inElement.clientHeight + "px";
|
||||||
inElement.style.overflow = "hidden";
|
inElement.style.overflow = "hidden";
|
||||||
inElement.style.transition = "none";
|
inElement.style.transition = "none";
|
||||||
frameRequest = requestAnimationFrame(()=>
|
frameRequest = requestAnimationFrame(()=>
|
||||||
{
|
|
||||||
inElement.style.transition = `height ${(inMs||1000) / 1000}s`;
|
|
||||||
frameRequest = requestAnimationFrame(()=>
|
|
||||||
{
|
{
|
||||||
inElement.style.height = `${inOpen ? inElement.scrollHeight : 0}px`;
|
inElement.style.height = `${inOpen ? inElement.scrollHeight : 0}px`;
|
||||||
});
|
inElement.style.transition = `height ${(inMs||1000) / 1000}s`;
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
58
context.tsx
58
context.tsx
@ -1,58 +0,0 @@
|
|||||||
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);
|
|
||||||
const refElement:React.MutableRefObject<HTMLElement|null> = React.useRef( null );
|
|
||||||
const refControl:React.MutableRefObject<CollapseControls|null> = React.useRef( null );
|
|
||||||
|
|
||||||
React.useEffect(()=>
|
|
||||||
{
|
|
||||||
refControl.current = Collapser(refElement.current as HTMLElement, true);
|
|
||||||
}
|
|
||||||
, []);
|
|
||||||
|
|
||||||
React.useEffect(()=>
|
|
||||||
{
|
|
||||||
console.log("open changed to:", stateGet.open);
|
|
||||||
refControl.current && refControl.current(stateGet.open, 1000, ()=>stateSet({done:true}));
|
|
||||||
|
|
||||||
}, [stateGet.open])
|
|
||||||
|
|
||||||
React.useEffect(()=>{
|
|
||||||
console.log("done changed to:", stateGet.done);
|
|
||||||
}, [stateGet.done])
|
|
||||||
|
|
||||||
return <div class="p-4">
|
|
||||||
<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>
|
|
||||||
<div ref={refElement as React.Ref<HTMLDivElement>}>
|
|
||||||
<p>hello</p>
|
|
||||||
<p>hello</p>
|
|
||||||
<p>hello</p>
|
|
||||||
<p>hello</p>
|
|
||||||
<p>hello</p>
|
|
||||||
<p>hello</p>
|
|
||||||
<p>hello</p>
|
|
||||||
</div>
|
|
||||||
</div>;
|
|
||||||
|
|
||||||
};
|
|
Loading…
Reference in New Issue
Block a user