Files
grafana/public/app/core/utils/ConfigProvider.tsx
Jack Westbrook d282b7a6f8 Grafana UI: Make it possible to bundle package with plugins (#76191)
* moved themecontext to data

* chore(grafana-ui): re-export ThemeContext from grafana/data for backwards compatibility

* Moved icon bundling to core.

* feat(plugins): share react-inlinesvg with plugins for grafana/ui bundling

* chore(codeowners): add generate-icon-bundle.js to file

* chore(storybook): update path to iconBundle file for theme

* feat(plugins): share i18n dependency via systemjs

* Make sure that icon bundle is initialized for tests.

* Removed comment.

* added tests for icon root.

* Removing the need of having an init variable.

* feat(grafana-ui): add icon svgs to bundle for projects that don't rely on grafana

---------

Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>
2023-10-13 14:11:41 +02:00

41 lines
1.3 KiB
TypeScript

import React, { useEffect, useState } from 'react';
import { SkeletonTheme } from 'react-loading-skeleton';
import { GrafanaTheme2, ThemeContext } from '@grafana/data';
import { ThemeChangedEvent, config } from '@grafana/runtime';
import { appEvents } from '../core';
import 'react-loading-skeleton/dist/skeleton.css';
export const ThemeProvider = ({ children, value }: { children: React.ReactNode; value: GrafanaTheme2 }) => {
const [theme, setTheme] = useState(value);
useEffect(() => {
const sub = appEvents.subscribe(ThemeChangedEvent, (event) => {
config.theme2 = event.payload;
setTheme(event.payload);
});
return () => sub.unsubscribe();
}, []);
return (
<ThemeContext.Provider value={theme}>
<SkeletonTheme
baseColor={theme.colors.background.secondary}
highlightColor={theme.colors.emphasize(theme.colors.background.secondary)}
borderRadius={theme.shape.radius.default}
>
{children}
</SkeletonTheme>
</ThemeContext.Provider>
);
};
export const provideTheme = (component: React.ComponentType<any>, theme: GrafanaTheme2) => {
return function ThemeProviderWrapper(props: any) {
return <ThemeProvider value={theme}>{React.createElement(component, { ...props })}</ThemeProvider>;
};
};