Allow to pass generic variables to callable curtains / subapps and improve type check.

This commit is contained in:
Madeorsk 2024-07-14 16:01:50 +02:00
parent 2b4965a2ee
commit f949f3e191
Signed by: Madeorsk
SSH key fingerprint: SHA256:J9G0ofIOLKf7kyS2IfrMqtMaPdfsk1W02+oGueZzDDU

View file

@ -50,21 +50,38 @@ export function useCurtains(): CurtainsContextState
return useContext(CurtainsContext);
}
/**
* Curtain callable.
* A generic callable curtain element.
*/
export type CallableCurtainElement = (...args: any) => React.ReactNode;
/**
* A callable curtain function with typed parameters.
*/
export type CallableCurtain<F extends CallableCurtainElement> = (...args: Parameters<F>) => CurtainUuidType;
/**
* Callable curtain function generator.
* @param curtains The curtains context state.
* @param curtainElement The curtain element to open when called.
*/
export function callableCurtain(curtains: CurtainsContextState, curtainElement: React.ReactNode): () => CurtainUuidType
export function callableCurtain<F extends CallableCurtainElement>(curtains: CurtainsContextState, curtainElement: React.ReactNode|F): CallableCurtain<F>
{
return () => curtains.open(curtainElement);
if (typeof curtainElement == "function")
// It's a callable curtain element, the callable curtain should be called with the same parameters.
return (...args: Parameters<F>) => curtains.open(curtainElement(...args));
else
// It's a simple element, just open it.
return () => curtains.open(curtainElement);
}
/**
* Hook to create a simple curtain.
* @param curtainElement Content of the curtain to open.
*/
export function useCallableCurtain(curtainElement: React.ReactNode): () => CurtainUuidType
export function useCallableCurtain<F extends CallableCurtainElement>(curtainElement: React.ReactNode|F): CallableCurtain<F>
{
// Get curtains context state.
const curtains = useCurtains();