add collapse group
This commit is contained in:
parent
4663698923
commit
a89dc2c3de
14
app.tsx
14
app.tsx
@ -1,6 +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, CollapseMenu} from "$/collapse.tsx";
|
||||||
|
|
||||||
const Comp = React.lazy(()=>import("./deep/component.tsx"));
|
const Comp = React.lazy(()=>import("./deep/component.tsx"));
|
||||||
|
|
||||||
@ -16,13 +16,17 @@ export default ()=>
|
|||||||
<a href="/details">Details</a>
|
<a href="/details">Details</a>
|
||||||
<a href="/data">Data Fetching</a>
|
<a href="/data">Data Fetching</a>
|
||||||
</nav>
|
</nav>
|
||||||
<Collapse open={false}>
|
|
||||||
<p>Hello!</p>
|
|
||||||
</Collapse>
|
|
||||||
</header>
|
</header>
|
||||||
<div>
|
<div class="self-start">
|
||||||
<CollapseGroup>
|
<CollapseGroup>
|
||||||
<CollapseButton></CollapseButton>
|
<CollapseButton></CollapseButton>
|
||||||
|
<CollapseMenu>
|
||||||
|
<p>collapsedable!</p>
|
||||||
|
<p>collapsedable!</p>
|
||||||
|
<p>collapsedable!</p>
|
||||||
|
<p>collapsedable!</p>
|
||||||
|
<p>collapsedable!</p>
|
||||||
|
</CollapseMenu>
|
||||||
</CollapseGroup>
|
</CollapseGroup>
|
||||||
</div>
|
</div>
|
||||||
<main class="layout-middle">
|
<main class="layout-middle">
|
||||||
|
@ -1,42 +1,5 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
export default (props:{open:boolean, children:React.JSX.Element|React.JSX.Element[]})=>
|
|
||||||
{
|
|
||||||
const [openGet, openSet] = React.useState(props.open);
|
|
||||||
const [doneGet, doneSet] = React.useState(true);
|
|
||||||
const refEl:React.RefObject<HTMLElement> = React.useRef(null);
|
|
||||||
const refCollapse:React.RefObject<CollapseControls> = React.useRef(null);
|
|
||||||
|
|
||||||
React.useEffect(()=>
|
|
||||||
{
|
|
||||||
if(refEl.current)
|
|
||||||
{
|
|
||||||
refCollapse.current = Collapser(refEl.current, openGet);
|
|
||||||
}
|
|
||||||
return ()=> refCollapse.current && refCollapse.current();
|
|
||||||
}
|
|
||||||
, []);
|
|
||||||
const Handler =()=>
|
|
||||||
{
|
|
||||||
openSet((old)=>
|
|
||||||
{
|
|
||||||
if(refCollapse.current)
|
|
||||||
{
|
|
||||||
doneSet(false);
|
|
||||||
refCollapse.current(!old, 1000, ()=>doneSet(true));
|
|
||||||
}
|
|
||||||
return !old;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return <div>
|
|
||||||
<button onClick={Handler}>{openGet ? "Close this" : "Open it"}{doneGet ? "." : "..."}</button>
|
|
||||||
<div ref={refEl as React.RefObject<HTMLDivElement>}>
|
|
||||||
{!(!openGet && doneGet) && props.children}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
};
|
|
||||||
|
|
||||||
export function useAway(inRef:React.Ref<HTMLElement>, inHandler:(inAway:boolean)=>void)
|
export function useAway(inRef:React.Ref<HTMLElement>, inHandler:(inAway:boolean)=>void)
|
||||||
{
|
{
|
||||||
const handlerRef = React.useRef(inHandler);
|
const handlerRef = React.useRef(inHandler);
|
||||||
@ -67,20 +30,66 @@ export const CollapseButton =()=>
|
|||||||
{
|
{
|
||||||
const [stateGet, stateSet] = React.useContext(CollapseContext);
|
const [stateGet, stateSet] = React.useContext(CollapseContext);
|
||||||
const buttonRef:React.Ref<HTMLElement> = React.useRef(null);
|
const buttonRef:React.Ref<HTMLElement> = React.useRef(null);
|
||||||
useAway(buttonRef, (Away)=>stateSet({Away}));
|
useAway(buttonRef, (Away)=>
|
||||||
return <button onClick={e=>stateSet({Open:!stateGet.Open})} ref={buttonRef as React.Ref<HTMLButtonElement>}>
|
{
|
||||||
|
if(Away)
|
||||||
|
{
|
||||||
|
stateGet.Open && stateSet({Open:!stateGet.Open, Done:false});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stateSet({Open:!stateGet.Open, Done:false})
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
return <button ref={buttonRef as React.Ref<HTMLButtonElement>}>
|
||||||
click it! {JSON.stringify(stateGet)}
|
click it! {JSON.stringify(stateGet)}
|
||||||
</button>
|
</button>
|
||||||
}
|
}
|
||||||
|
|
||||||
type DoneCallback =(openState:boolean)=>void;
|
export const CollapseMenu =(props:{children:React.JSX.Element|React.JSX.Element[]})=>
|
||||||
|
{
|
||||||
|
const [stateGet, stateSet] = React.useContext(CollapseContext);
|
||||||
|
const refEl:React.RefObject<HTMLElement> = React.useRef(null);
|
||||||
|
const refCollapse:React.RefObject<CollapseControls> = React.useRef(null);
|
||||||
|
|
||||||
|
React.useEffect(()=>
|
||||||
|
{
|
||||||
|
if(refEl.current)
|
||||||
|
{
|
||||||
|
refCollapse.current = Collapser(refEl.current, stateGet.Open);
|
||||||
|
}
|
||||||
|
return ()=> refCollapse.current && refCollapse.current();
|
||||||
|
}
|
||||||
|
, []);
|
||||||
|
|
||||||
|
React.useEffect(()=>
|
||||||
|
{
|
||||||
|
console.log(`menu animation start:${stateGet.Open}`);
|
||||||
|
refCollapse.current && refCollapse.current(stateGet.Open, 500, (state, id)=>
|
||||||
|
{
|
||||||
|
console.log("menu animation done", state, id);
|
||||||
|
stateSet({Done:true});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}, [stateGet.Open, stateGet.Done]);
|
||||||
|
|
||||||
|
|
||||||
|
console.log(`menu re-render: open:${stateGet.Open} done:${stateGet.Done} away:${stateGet.Away}`)
|
||||||
|
return <div ref={refEl as React.RefObject<HTMLDivElement>}>
|
||||||
|
{ props.children}
|
||||||
|
</div>
|
||||||
|
};
|
||||||
|
|
||||||
|
type DoneCallback =(openState:boolean, id:number)=>void;
|
||||||
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) => {};
|
||||||
let userMode = initialState;
|
let userMode = initialState;
|
||||||
let frameRequest = 0;
|
let frameRequest = 0;
|
||||||
|
let id = Math.random();
|
||||||
|
|
||||||
const done = (inEvent:TransitionEvent)=>
|
const done = (inEvent:TransitionEvent)=>
|
||||||
{
|
{
|
||||||
if (inEvent.propertyName == "height" && inEvent.target == inElement)
|
if (inEvent.propertyName == "height" && inEvent.target == inElement)
|
||||||
@ -91,19 +100,21 @@ export function Collapser(inElement:HTMLElement, initialState = false)
|
|||||||
inElement.style.height = "auto";
|
inElement.style.height = "auto";
|
||||||
inElement.style.overflow = "visible";
|
inElement.style.overflow = "visible";
|
||||||
}
|
}
|
||||||
userDone(userMode);
|
userDone(userMode, id);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
inElement.addEventListener("transitionend", done);
|
inElement.addEventListener("transitionend", done);
|
||||||
|
|
||||||
return function(inOpen?:boolean, inMs?:number, inDone?:DoneCallback)
|
return function(inOpen?:boolean, inMs?:number, inDone?:DoneCallback)
|
||||||
{
|
{
|
||||||
|
console.log("collapser sees", inOpen);
|
||||||
|
|
||||||
cancelAnimationFrame(frameRequest);
|
cancelAnimationFrame(frameRequest);
|
||||||
|
|
||||||
if(arguments.length)
|
if(arguments.length)
|
||||||
{
|
{
|
||||||
userDone = inDone|| ((m)=>{}) as DoneCallback;
|
userDone = inDone|| ((m)=>{}) as DoneCallback;
|
||||||
userMode = inOpen||!userMode;
|
userMode = inOpen === true;
|
||||||
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";
|
||||||
|
Loading…
Reference in New Issue
Block a user