Created
December 16, 2021 18:36
-
-
Save jonbrennecke/33617ae770418fc42b135f09a812bd8b to your computer and use it in GitHub Desktop.
Curative UI Context Experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useContext, createContext, PropsWithChildren, ComponentType } from 'react'; | |
const CUIContext = createContext(false); | |
function CUIProvider({children}: PropsWithChildren<{}>) { | |
const isWithinCuiContext = useContext(CUIContext); | |
if (!isWithinCuiContext) { | |
return ( | |
<CUIContext.Provider value={true}> | |
<div className="cui"> | |
{children} | |
</div> | |
</CUIContext.Provider> | |
); | |
} | |
return ( | |
<> | |
{children} | |
</> | |
); | |
} | |
function withCUIContext<Props>(WrappedComponent: ComponentType<Props>) { | |
return function ComponentWrappedInCUIContext(props: Props) { | |
return ( | |
<CUIProvider> | |
<WrappedComponent {...props} /> | |
</CUIProvider> | |
); | |
} | |
} | |
// -- Component implementation (automatically wrapped in a div with the `.cui` className) | |
const MyCUIComponent = withCUIContext(() => <a href="https://google.com">Link</a>); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment