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