Skip to content

Commit cd33b4b

Browse files
committed
Refactor
1 parent 82da253 commit cd33b4b

File tree

7 files changed

+55
-72
lines changed

7 files changed

+55
-72
lines changed

src/components/ThemeManager.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useEffect } from 'react';
2+
import { setTheme } from '@ui5/webcomponents-base/dist/config/Theme.js';
3+
import { useTheme } from '../hooks/useTheme.ts';
4+
5+
export function ThemeManager() {
6+
const { theme } = useTheme();
7+
8+
useEffect(() => {
9+
void setTheme(theme);
10+
}, [theme]);
11+
12+
return null;
13+
}

src/components/ThemeManager/ThemeManager.spec.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/components/ThemeManager/ThemeManager.tsx

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/components/Yaml/YamlViewer.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import { Button, FlexBox } from '@ui5/webcomponents-react';
66
import styles from './YamlViewer.module.css';
77
import { useToast } from '../../context/ToastContext.tsx';
88
import { useTranslation } from 'react-i18next';
9-
import { useIsDarkModePreferred } from '../../hooks/useIsDarkModePreferred.ts';
9+
import { useTheme } from '../../hooks/useTheme.ts';
1010
type YamlViewerProps = { yamlString: string; filename: string };
1111
const YamlViewer: FC<YamlViewerProps> = ({ yamlString, filename }) => {
1212
const toast = useToast();
1313
const { t } = useTranslation();
14-
const isDarkModePreferred = useIsDarkModePreferred();
14+
const { isDarkTheme } = useTheme();
1515
const copyToClipboard = () => {
1616
navigator.clipboard.writeText(yamlString);
1717
toast.show(t('yaml.copiedToClipboard'));
@@ -40,7 +40,7 @@ const YamlViewer: FC<YamlViewerProps> = ({ yamlString, filename }) => {
4040
</FlexBox>
4141
<SyntaxHighlighter
4242
language="yaml"
43-
style={isDarkModePreferred ? materialDark : materialLight}
43+
style={isDarkTheme ? materialDark : materialLight}
4444
showLineNumbers
4545
lineNumberStyle={{
4646
paddingRight: '20px',

src/hooks/useIsDarkModePreferred.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/hooks/useTheme.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { useSyncExternalStore } from 'react';
2+
import { Theme } from '@ui5/webcomponents-react';
3+
4+
const DEFAULT_THEME_LIGHT = Theme.sap_horizon;
5+
const DEFAULT_THEME_DARK = Theme.sap_horizon_dark;
6+
const DARK_SAP_THEMES = new Set<string>([
7+
Theme.sap_fiori_3_dark,
8+
Theme.sap_fiori_3_hcb,
9+
Theme.sap_horizon_dark,
10+
Theme.sap_horizon_hcb,
11+
]);
12+
13+
function useSystemDarkModePreference() {
14+
return useSyncExternalStore(
15+
(callback) => {
16+
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
17+
mediaQuery.addEventListener('change', callback);
18+
return () => mediaQuery.removeEventListener('change', callback);
19+
},
20+
() => window.matchMedia('(prefers-color-scheme: dark)').matches,
21+
);
22+
}
23+
24+
export function useTheme() {
25+
const systemPrefersDark = useSystemDarkModePreference();
26+
const themeFromUrl = new URL(window.location.href).searchParams.get('sap-theme');
27+
28+
// Theme from URL takes precedence over system settings
29+
const theme = themeFromUrl || (systemPrefersDark ? DEFAULT_THEME_DARK : DEFAULT_THEME_LIGHT);
30+
31+
// For well-defined SAP themes, return if they are light or dark – unknown themes will fall back to light
32+
const isDarkTheme = DARK_SAP_THEMES.has(theme);
33+
34+
return {
35+
theme,
36+
isDarkTheme,
37+
};
38+
}

src/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ToastProvider } from './context/ToastContext.tsx';
77
import { CopyButtonProvider } from './context/CopyButtonContext.tsx';
88
import { FrontendConfigProvider } from './context/FrontendConfigContext.tsx';
99
import '@ui5/webcomponents-react/dist/Assets'; //used for loading themes
10-
import { ThemeManager } from './components/ThemeManager/ThemeManager.tsx';
10+
import { ThemeManager } from './components/ThemeManager.tsx';
1111
import '.././i18n.ts';
1212
import './utils/i18n/timeAgo';
1313
import { ErrorBoundary, FallbackProps } from 'react-error-boundary';

0 commit comments

Comments
 (0)