0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-22 13:43:03 -05:00
penpot-exporter-figma-plugin/ui-src/context/createGenericContext.ts
2024-06-04 15:33:55 +02:00

19 lines
569 B
TypeScript

import { Provider } from 'preact';
// @TODO: Try to use react
import { createContext, useContext } from 'react';
export const createGenericContext = <T>(): [<K extends T>() => K, Provider<T | undefined>] => {
const genericContext = createContext<T | undefined>(undefined);
const useGenericContext = <K extends T>(): K => {
const context = useContext(genericContext);
if (!context) {
throw new Error('useGenericContext must be used within a Provider');
}
return context as K;
};
return [useGenericContext, genericContext.Provider];
};