mirror of
https://github.com/penpot/penpot-exporter-figma-plugin.git
synced 2024-12-22 21:53:27 -05:00
20 lines
569 B
TypeScript
20 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];
|
||
|
};
|