forked from FormidableLabs/radium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.js
43 lines (37 loc) · 1.28 KB
/
context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* @flow */
import React, {type Context, type ComponentType, useContext} from 'react';
import hoistStatics from 'hoist-non-react-statics';
import StyleKeeper from './style-keeper';
import type {Config} from './config';
export const StyleKeeperContext: Context<StyleKeeper | void> = React.createContext(
undefined
);
export const RadiumConfigContext: Context<Config | void> = React.createContext(
undefined
);
export type WithRadiumContextsProps = {
styleKeeperContext?: StyleKeeper,
radiumConfigContext?: Config
};
export function withRadiumContexts<P: {}>(
WrappedComponent: ComponentType<P>
): ComponentType<$Diff<P, WithRadiumContextsProps>> {
const WithRadiumContexts = React.forwardRef(
(props: $Diff<P, WithRadiumContextsProps>, ref) => {
const radiumConfigContext = useContext(RadiumConfigContext);
const styleKeeperContext = useContext(StyleKeeperContext);
return (
<WrappedComponent
ref={ref}
{...props}
radiumConfigContext={radiumConfigContext}
styleKeeperContext={styleKeeperContext}
/>
);
}
);
WithRadiumContexts.displayName = `withRadiumContexts(${WrappedComponent.displayName ||
WrappedComponent.name ||
'Component'})`;
return hoistStatics(WithRadiumContexts, WrappedComponent);
}