mirror of https://github.com/grafana/grafana
parent
7626ce9922
commit
1bc007e29c
@ -1,4 +1,5 @@ |
|||||||
export * from './components'; |
export * from './components'; |
||||||
export * from './types'; |
export * from './types'; |
||||||
export * from './utils'; |
export * from './utils'; |
||||||
export * from './theme'; |
export * from './themes'; |
||||||
|
export * from './themes/ThemeContext'; |
||||||
|
|||||||
@ -0,0 +1,20 @@ |
|||||||
|
import React from 'react'; |
||||||
|
import { GrafanaThemeType, Themeable } from '../types'; |
||||||
|
import { getTheme } from './index'; |
||||||
|
|
||||||
|
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>; |
||||||
|
type Subtract<T, K> = Omit<T, keyof K>; |
||||||
|
|
||||||
|
// Use Grafana Dark theme by default
|
||||||
|
export const ThemeContext = React.createContext(getTheme(GrafanaThemeType.Dark)); |
||||||
|
|
||||||
|
export const withTheme = <P extends Themeable>(Component: React.ComponentType<P>) => { |
||||||
|
const WithTheme: React.FunctionComponent<Subtract<P, Themeable>> = props => { |
||||||
|
// @ts-ignore
|
||||||
|
return <ThemeContext.Consumer>{theme => <Component {...props} theme={theme} />}</ThemeContext.Consumer>; |
||||||
|
}; |
||||||
|
|
||||||
|
WithTheme.displayName = `WithTheme(${Component.displayName})`; |
||||||
|
|
||||||
|
return WithTheme; |
||||||
|
}; |
||||||
@ -0,0 +1,4 @@ |
|||||||
|
import { GrafanaTheme } from "../types"; |
||||||
|
|
||||||
|
export function getTheme(themeName?: string): GrafanaTheme |
||||||
|
export function mockTheme(themeMock: Partial<GrafanaTheme>): () => void |
||||||
@ -1,14 +1,7 @@ |
|||||||
|
|
||||||
export * from './data'; |
export * from './data'; |
||||||
export * from './time'; |
export * from './time'; |
||||||
export * from './panel'; |
export * from './panel'; |
||||||
export * from './plugin'; |
export * from './plugin'; |
||||||
export * from './datasource'; |
export * from './datasource'; |
||||||
|
export * from './theme'; |
||||||
export enum GrafanaTheme { |
|
||||||
Light = 'light', |
|
||||||
Dark = 'dark', |
|
||||||
} |
|
||||||
|
|
||||||
export interface Themeable { |
|
||||||
theme?: GrafanaTheme; |
|
||||||
} |
|
||||||
|
|||||||
@ -0,0 +1,40 @@ |
|||||||
|
import React from 'react'; |
||||||
|
import { RenderFunction } from '@storybook/react'; |
||||||
|
import { ThemeContext } from '../../themes/ThemeContext'; |
||||||
|
import { select } from '@storybook/addon-knobs'; |
||||||
|
import { getTheme } from '../../themes'; |
||||||
|
import { GrafanaThemeType } from '../../types'; |
||||||
|
|
||||||
|
const ThemableStory: React.FunctionComponent<{}> = ({ children }) => { |
||||||
|
const themeKnob = select( |
||||||
|
'Theme', |
||||||
|
{ |
||||||
|
Default: GrafanaThemeType.Dark, |
||||||
|
Light: GrafanaThemeType.Light, |
||||||
|
Dark: GrafanaThemeType.Dark, |
||||||
|
}, |
||||||
|
GrafanaThemeType.Dark |
||||||
|
); |
||||||
|
|
||||||
|
return ( |
||||||
|
<ThemeContext.Provider value={getTheme(themeKnob)}> |
||||||
|
{children} |
||||||
|
</ThemeContext.Provider> |
||||||
|
|
||||||
|
); |
||||||
|
}; |
||||||
|
|
||||||
|
export const renderComponentWithTheme = (component: React.ComponentType<any>, props: any) => { |
||||||
|
return ( |
||||||
|
<ThemeContext.Consumer> |
||||||
|
{theme => { |
||||||
|
return React.createElement(component, { |
||||||
|
...props, |
||||||
|
theme, |
||||||
|
}); |
||||||
|
}} |
||||||
|
</ThemeContext.Consumer> |
||||||
|
); |
||||||
|
}; |
||||||
|
|
||||||
|
export const withTheme = (story: RenderFunction) => <ThemableStory>{story()}</ThemableStory>; |
||||||
Loading…
Reference in new issue