import { Attributes } from 'graphology-types'; import { createContext, useContext } from 'react'; import Sigma from 'sigma'; import { PrimitivesDeclaration } from 'sigma/types'; export interface SigmaContextInterface< N extends Attributes = Attributes, E extends Attributes = Attributes, G extends Attributes = Attributes, NS = unknown, ES = unknown, GS = unknown, P extends PrimitivesDeclaration = PrimitivesDeclaration, > { sigma: Sigma; container: HTMLElement; } /** * @hidden */ export const SigmaContext = createContext(null); /** * @hidden */ export const SigmaProvider = SigmaContext.Provider; /** * React hook that store the sigma and html container reference. * * ```typescript * const {sigma, container} = useSigmaContext(); *``` * * See {@link SigmaContextInterface} for more information. * * @category Hook */ export function useSigmaContext< N extends Attributes = Attributes, E extends Attributes = Attributes, G extends Attributes = Attributes, NS = unknown, ES = unknown, GS = unknown, P extends PrimitivesDeclaration = PrimitivesDeclaration, >(): SigmaContextInterface { const context = useContext(SigmaContext); if (context == null) { throw new Error('No context provided: useSigmaContext() can only be used in a descendant of '); } // cast context to the one with good generics return context as unknown as SigmaContextInterface; }