2021-02-20 02:02:06 -06:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2023-06-01 05:06:28 -05:00
|
|
|
import { SkeletonTheme } from 'react-loading-skeleton';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-07-14 13:34:00 -05:00
|
|
|
import { GrafanaTheme2 } from '@grafana/data';
|
2023-05-10 08:37:04 -05:00
|
|
|
import { ThemeChangedEvent, config } from '@grafana/runtime';
|
2021-02-20 02:02:06 -06:00
|
|
|
import { ThemeContext } from '@grafana/ui';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2021-02-20 02:02:06 -06:00
|
|
|
import { appEvents } from '../core';
|
2019-01-18 02:50:29 -06:00
|
|
|
|
2023-06-01 05:06:28 -05:00
|
|
|
import 'react-loading-skeleton/dist/skeleton.css';
|
|
|
|
|
2022-07-14 13:34:00 -05:00
|
|
|
export const ThemeProvider = ({ children, value }: { children: React.ReactNode; value: GrafanaTheme2 }) => {
|
|
|
|
const [theme, setTheme] = useState(value);
|
2019-01-18 02:50:29 -06:00
|
|
|
|
2021-02-20 02:02:06 -06:00
|
|
|
useEffect(() => {
|
|
|
|
const sub = appEvents.subscribe(ThemeChangedEvent, (event) => {
|
2023-05-10 08:37:04 -05:00
|
|
|
config.theme2 = event.payload;
|
2021-02-20 02:02:06 -06:00
|
|
|
setTheme(event.payload);
|
|
|
|
});
|
2019-01-18 02:50:29 -06:00
|
|
|
|
2021-02-20 02:02:06 -06:00
|
|
|
return () => sub.unsubscribe();
|
|
|
|
}, []);
|
|
|
|
|
2023-06-01 05:06:28 -05:00
|
|
|
return (
|
|
|
|
<ThemeContext.Provider value={theme}>
|
|
|
|
<SkeletonTheme
|
|
|
|
baseColor={theme.colors.background.secondary}
|
|
|
|
highlightColor={theme.colors.emphasize(theme.colors.background.secondary)}
|
2023-08-01 08:46:07 -05:00
|
|
|
borderRadius={theme.shape.radius.default}
|
2023-06-01 05:06:28 -05:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</SkeletonTheme>
|
|
|
|
</ThemeContext.Provider>
|
|
|
|
);
|
2019-01-18 02:50:29 -06:00
|
|
|
};
|
2019-02-05 10:04:48 -06:00
|
|
|
|
2022-07-14 13:34:00 -05:00
|
|
|
export const provideTheme = (component: React.ComponentType<any>, theme: GrafanaTheme2) => {
|
2022-07-23 10:09:03 -05:00
|
|
|
return function ThemeProviderWrapper(props: any) {
|
|
|
|
return <ThemeProvider value={theme}>{React.createElement(component, { ...props })}</ThemeProvider>;
|
|
|
|
};
|
2019-02-05 10:04:48 -06:00
|
|
|
};
|