click away
This commit is contained in:
parent
f78e7c8107
commit
c78b1fafba
@ -41,6 +41,8 @@ export const Menu =(props:{children:React.JSX.Element|React.JSX.Element[]})=>
|
|||||||
}, []);
|
}, []);
|
||||||
React.useEffect(()=> {refControl.current && refControl.current(stateGet.open ? "Open" : "Shut", ()=>stateSet({done:true}))}, [stateGet.open])
|
React.useEffect(()=> {refControl.current && refControl.current(stateGet.open ? "Open" : "Shut", ()=>stateSet({done:true}))}, [stateGet.open])
|
||||||
|
|
||||||
|
useAway(refElement, ()=>stateSet({open:false, done:false}));
|
||||||
|
|
||||||
return <div ref={refElement as React.Ref<HTMLDivElement>} class={Classes.Shut} style="transition:none;">
|
return <div ref={refElement as React.Ref<HTMLDivElement>} class={Classes.Shut} style="transition:none;">
|
||||||
{ (!stateGet.open && stateGet.done) ? null : props.children}
|
{ (!stateGet.open && stateGet.done) ? null : props.children}
|
||||||
</div>;
|
</div>;
|
||||||
@ -56,6 +58,28 @@ export const Button =()=>
|
|||||||
</>;
|
</>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type Handler = (e:MouseEvent)=>void
|
||||||
|
const Refs:Map<HTMLElement, React.Ref<Handler>> = new Map();
|
||||||
|
window.innerWidth && document.addEventListener("click", e=>
|
||||||
|
{
|
||||||
|
const path = e.composedPath();
|
||||||
|
Refs.forEach( (handlerRef, element)=> handlerRef.current && (path.includes(element) ? null : handlerRef.current(e)) );
|
||||||
|
}
|
||||||
|
, true);
|
||||||
|
const useAway =(inRef:React.Ref<HTMLElement>, handleAway:Handler)=>
|
||||||
|
{
|
||||||
|
const refHandler:React.MutableRefObject<Handler> = React.useRef(handleAway);
|
||||||
|
refHandler.current = handleAway;
|
||||||
|
|
||||||
|
React.useEffect(()=>
|
||||||
|
{
|
||||||
|
inRef.current && Refs.set(inRef.current, refHandler);
|
||||||
|
return ()=> inRef.current && Refs.delete(inRef.current);
|
||||||
|
}
|
||||||
|
, []);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
type StyleSize = [classes:string, width:number, height:number];
|
type StyleSize = [classes:string, width:number, height:number];
|
||||||
type StylePack = Record<string, string>;
|
type StylePack = Record<string, string>;
|
||||||
type StyleCalc = Record<string, StyleSize>;
|
type StyleCalc = Record<string, StyleSize>;
|
||||||
|
Loading…
Reference in New Issue
Block a user