components
This commit is contained in:
parent
80bcb1f05e
commit
85a65d97eb
27
app.tsx
27
app.tsx
@ -1,7 +1,7 @@
|
|||||||
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";
|
import * as C from "./context.tsx";
|
||||||
|
|
||||||
const Comp = React.lazy(()=>import("./deep/component.tsx"));
|
const Comp = React.lazy(()=>import("./deep/component.tsx"));
|
||||||
|
|
||||||
@ -22,7 +22,30 @@ export default ()=>
|
|||||||
</Collapse>
|
</Collapse>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<C/>
|
<C.Group>
|
||||||
|
<C.Button></C.Button>
|
||||||
|
<C.Menu>
|
||||||
|
<p>hello!</p>
|
||||||
|
<p>hello!</p>
|
||||||
|
<p>hello!</p>
|
||||||
|
<p>hello!</p>
|
||||||
|
<C.Group>
|
||||||
|
<C.Button></C.Button>
|
||||||
|
<C.Menu>
|
||||||
|
<p>hello!</p>
|
||||||
|
<p>hello!</p>
|
||||||
|
<p>hello!</p>
|
||||||
|
<p>hello!</p>
|
||||||
|
<p>hello!</p>
|
||||||
|
<p>hello!</p>
|
||||||
|
<p>hello!</p>
|
||||||
|
</C.Menu>
|
||||||
|
</C.Group>
|
||||||
|
<p>hello!</p>
|
||||||
|
<p>hello!</p>
|
||||||
|
<p>hello!</p>
|
||||||
|
</C.Menu>
|
||||||
|
</C.Group>
|
||||||
|
|
||||||
<main class="layout-middle">
|
<main class="layout-middle">
|
||||||
<Iso.Switch>
|
<Iso.Switch>
|
||||||
|
101
context.tsx
101
context.tsx
@ -1,5 +1,4 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { CollapseControls, Collapser } from "./components/collapse.tsx";
|
|
||||||
|
|
||||||
type StateArgs = {done?:boolean, open?:boolean};
|
type StateArgs = {done?:boolean, open?:boolean};
|
||||||
type StateObj = {done:boolean, open:boolean};
|
type StateObj = {done:boolean, open:boolean};
|
||||||
@ -7,52 +6,82 @@ type StateBinding = [state:StateObj, update:(args:StateArgs)=>void];
|
|||||||
|
|
||||||
const CTX = React.createContext([{done:true, open:false}, (args)=>{}] as StateBinding);
|
const CTX = React.createContext([{done:true, open:false}, (args)=>{}] as StateBinding);
|
||||||
|
|
||||||
export default ()=>
|
export const Group =(props:{children:React.JSX.Element|React.JSX.Element[]})=>
|
||||||
{
|
{
|
||||||
const [stateGet, stateSet] = React.useState({done:true, open:false} as StateObj);
|
const [stateGet, stateSet] = React.useState({done:true, open:false} as StateObj);
|
||||||
const setter =(args:StateArgs)=> stateSet({...stateGet, ...args});
|
return <CTX.Provider value={[stateGet, (args:StateArgs)=> stateSet({...stateGet, ...args})]}>{props.children}</CTX.Provider>;
|
||||||
|
};
|
||||||
|
|
||||||
return <CTX.Provider value={[stateGet, setter]}>
|
export const Menu =(props:{children:React.JSX.Element|React.JSX.Element[]})=>
|
||||||
<Inner/>
|
|
||||||
</CTX.Provider>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Inner =()=>
|
|
||||||
{
|
{
|
||||||
const [stateGet, stateSet] = React.useContext(CTX);
|
const [stateGet, stateSet] = React.useContext(CTX);
|
||||||
const refElement:React.MutableRefObject<HTMLElement|null> = React.useRef( null );
|
const refElement:React.MutableRefObject<HTMLElement|null> = React.useRef( null );
|
||||||
const refControl:React.MutableRefObject<CollapseControls|null> = React.useRef( null );
|
const refControl:React.MutableRefObject<CollapseControls|null> = React.useRef( null );
|
||||||
|
|
||||||
React.useEffect(()=>
|
React.useEffect(()=>refControl.current = Collapser(refElement.current as HTMLElement, true), []);
|
||||||
{
|
React.useEffect(()=> {refControl.current && refControl.current(stateGet.open, 1000, ()=>stateSet({done:true}))}, [stateGet.open])
|
||||||
refControl.current = Collapser(refElement.current as HTMLElement, true);
|
|
||||||
}
|
|
||||||
, []);
|
|
||||||
|
|
||||||
React.useEffect(()=>
|
return <div ref={refElement as React.Ref<HTMLDivElement>}>{props.children}</div>;
|
||||||
{
|
};
|
||||||
console.log("open changed to:", stateGet.open);
|
|
||||||
refControl.current && refControl.current(stateGet.open, 1000, ()=>stateSet({done:true}));
|
|
||||||
|
|
||||||
}, [stateGet.open])
|
export const Button =()=>
|
||||||
|
{
|
||||||
React.useEffect(()=>{
|
const [stateGet, stateSet] = React.useContext(CTX);
|
||||||
console.log("done changed to:", stateGet.done);
|
return <>
|
||||||
}, [stateGet.done])
|
|
||||||
|
|
||||||
return <div class="p-4">
|
|
||||||
<p>{JSON.stringify(stateGet)}</p>
|
<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: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>
|
<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>;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user