mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Added config provider to be able to access config easily from react components
This commit is contained in:
28
public/app/core/utils/ConfigProvider.tsx
Normal file
28
public/app/core/utils/ConfigProvider.tsx
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import config, { Settings } from 'app/core/config';
|
||||||
|
import { GrafanaTheme } from '@grafana/ui';
|
||||||
|
|
||||||
|
export const ConfigContext = React.createContext<Settings>(config);
|
||||||
|
export const ConfigConsumer = ConfigContext.Consumer;
|
||||||
|
|
||||||
|
export const provideConfig = (component: React.ComponentType<any>) => {
|
||||||
|
const ConfigProvider = (props: any) => (
|
||||||
|
<ConfigContext.Provider value={config}>{React.createElement(component, { ...props })}</ConfigContext.Provider>
|
||||||
|
);
|
||||||
|
|
||||||
|
return ConfigProvider;
|
||||||
|
};
|
||||||
|
|
||||||
|
interface ThemeProviderProps {
|
||||||
|
children: (theme: GrafanaTheme) => JSX.Element;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ThemeProvider = ({ children }: ThemeProviderProps) => {
|
||||||
|
return (
|
||||||
|
<ConfigConsumer>
|
||||||
|
{({ bootData }) => {
|
||||||
|
return children(bootData.user.lightTheme ? GrafanaTheme.Light : GrafanaTheme.Dark);
|
||||||
|
}}
|
||||||
|
</ConfigConsumer>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
import coreModule from 'app/core/core_module';
|
import coreModule from 'app/core/core_module';
|
||||||
|
import { provideConfig } from 'app/core/utils/ConfigProvider';
|
||||||
|
|
||||||
export function react2AngularDirective(name: string, component: any, options: any) {
|
export function react2AngularDirective(name: string, component: any, options: any) {
|
||||||
coreModule.directive(name, [
|
coreModule.directive(name, [
|
||||||
'reactDirective',
|
'reactDirective',
|
||||||
reactDirective => {
|
reactDirective => {
|
||||||
return reactDirective(component, options);
|
return reactDirective(provideConfig(component), options);
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user