Skip to content

Instantly share code, notes, and snippets.

@jonbrennecke
Created December 16, 2021 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbrennecke/33617ae770418fc42b135f09a812bd8b to your computer and use it in GitHub Desktop.
Save jonbrennecke/33617ae770418fc42b135f09a812bd8b to your computer and use it in GitHub Desktop.
Curative UI Context Experiment
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