From b678641ab16a6648c5a6ac23f06fc08ac3453675 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 31 Dec 2022 14:16:31 +0100 Subject: [PATCH] theme: add multi theme provider and holder Signed-off-by: Patrik Oldsberg --- .../theme/src/compat/MultiThemeProvider.tsx | 58 +++++++++++++++++++ packages/theme/src/compat/types.ts | 19 ++++++ 2 files changed, 77 insertions(+) create mode 100644 packages/theme/src/compat/MultiThemeProvider.tsx create mode 100644 packages/theme/src/compat/types.ts diff --git a/packages/theme/src/compat/MultiThemeProvider.tsx b/packages/theme/src/compat/MultiThemeProvider.tsx new file mode 100644 index 0000000000..e707010dc9 --- /dev/null +++ b/packages/theme/src/compat/MultiThemeProvider.tsx @@ -0,0 +1,58 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React, { ReactNode } from 'react'; +import { + Theme as Mui4Theme, + ThemeProvider as Mui4Provider, +} from '@material-ui/core/styles'; +import { + Theme as Mui5Theme, + ThemeProvider as Mui5Provider, +} from '@mui/material/styles'; +import CssBaseline from '@mui/material/CssBaseline'; +import { MultiThemeHolder } from './types'; + +interface ThemeProviderProps { + children: ReactNode; + theme: MultiThemeHolder; + noCssBaseline?: boolean; +} + +export function MultiThemeProvider(props: ThemeProviderProps) { + const { children, theme, noCssBaseline } = props; + + let result = noCssBaseline ? ( + children + ) : ( + <> + + {children} + + ); + + const v4Theme = theme.getThemeForVersion('v4'); + if (v4Theme) { + result = {result}; + } + + const v5Theme = theme.getThemeForVersion('v5'); + if (v5Theme) { + result = {result}; + } + + return result; +} diff --git a/packages/theme/src/compat/types.ts b/packages/theme/src/compat/types.ts new file mode 100644 index 0000000000..dac6ef9492 --- /dev/null +++ b/packages/theme/src/compat/types.ts @@ -0,0 +1,19 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export interface MultiThemeHolder { + getThemeForVersion(version: string): unknown | undefined; +}