Skip to content

Commit eadce10

Browse files
committed
chore(theme): simplify theme management with <EuiProvider>
1 parent 3eb8ae0 commit eadce10

File tree

6 files changed

+15
-175
lines changed

6 files changed

+15
-175
lines changed

electron/renderer/context/chrome.tsx

-60
This file was deleted.

electron/renderer/context/theme.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// https://github.com/elastic/next-eui-starter/blob/master/src/components/theme.tsx
33

44
import type { EuiThemeColorMode } from '@elastic/eui';
5+
import { EuiProvider } from '@elastic/eui';
56
import type { ReactNode } from 'react';
67
import { createContext, useEffect, useState } from 'react';
7-
import { enableTheme, getThemeName } from '../lib/theme.js';
8+
import { getThemeName, setThemeName } from '../lib/theme.js';
89

910
/**
1011
* React context for storing theme-related data and callbacks.
@@ -38,14 +39,14 @@ export const ThemeProvider: React.FC<ThemeProviderProps> = (
3839
setColorMode(getThemeName());
3940
}, []);
4041

41-
// Enable the correct theme when color mode changes.
42+
// Save user's theme preference.
4243
useEffect(() => {
43-
enableTheme(colorMode);
44+
setThemeName(colorMode);
4445
}, [colorMode]);
4546

4647
return (
4748
<ThemeContext.Provider value={{ colorMode, setColorMode }}>
48-
{children}
49+
<EuiProvider colorMode={colorMode}>{children}</EuiProvider>
4950
</ThemeContext.Provider>
5051
);
5152
};

electron/renderer/lib/theme.ts

-69
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,6 @@ import type { EuiThemeColorMode } from '@elastic/eui';
55
import type { Maybe } from '../../common/types.js';
66
import { LocalStorage } from './local-storage.js';
77

8-
/**
9-
* Find all the theme links on the page that we could switch between.
10-
* See `_document.tsx` for how these are added to the page.
11-
*/
12-
const getAllThemeLinks = (): Array<HTMLLinkElement> => {
13-
return [
14-
...document.querySelectorAll<HTMLLinkElement>(
15-
'link[data-name="eui-theme"]'
16-
),
17-
];
18-
};
19-
20-
/**
21-
* Find the theme link on the page that matches the given theme name.
22-
*/
23-
const getThemeLink = (themeName: EuiThemeColorMode): Maybe<HTMLLinkElement> => {
24-
return getAllThemeLinks().find((themeLink) => {
25-
return themeLink.dataset.theme === themeName;
26-
});
27-
};
28-
29-
/**
30-
* Sets the user's theme preference and actively updates the UI to match.
31-
* To only set the preference without updating the UI, use `setThemeName`.
32-
*/
33-
export const enableTheme = (newThemeName: EuiThemeColorMode): void => {
34-
const oldThemeName = getThemeName();
35-
setThemeName(newThemeName);
36-
37-
const newThemeLink = getThemeLink(newThemeName);
38-
39-
if (!newThemeLink) {
40-
return;
41-
}
42-
43-
// When toggling the theme, to prevent a flash of unstyled content
44-
// then preload the new theme's CSS before enabling it.
45-
const preloadThemeLink = document.createElement('link');
46-
preloadThemeLink.rel = 'preload';
47-
preloadThemeLink.as = 'style';
48-
preloadThemeLink.href = newThemeLink.href;
49-
document.head.appendChild(preloadThemeLink);
50-
51-
// Once the new theme is loaded then we can disable the old theme.
52-
// Because the new theme is preloaded, the change should be instant.
53-
preloadThemeLink.onload = () => {
54-
for (const themeLink of getAllThemeLinks()) {
55-
// Disable all theme links, except for the desired theme, which we enable
56-
themeLink.disabled = themeLink.dataset.theme !== newThemeName;
57-
themeLink.ariaDisabled = String(themeLink.dataset.theme !== newThemeName);
58-
}
59-
60-
// Add a class to the `body` element that indicates which theme we're using.
61-
// This allows any custom styling to adapt to the current theme.
62-
if (document.body.classList.contains(`appTheme-${oldThemeName}`)) {
63-
document.body.classList.replace(
64-
`appTheme-${oldThemeName}`,
65-
`appTheme-${newThemeName}`
66-
);
67-
} else {
68-
document.body.classList.add(`appTheme-${newThemeName}`);
69-
}
70-
71-
// Remove the preload link element.
72-
document.head.removeChild(preloadThemeLink);
73-
};
74-
};
75-
768
/**
779
* Gets the user's theme preference.
7810
*/
@@ -83,7 +15,6 @@ export const getThemeName = (): EuiThemeColorMode => {
8315
/**
8416
* Sets the user's theme preference.
8517
* Does not actively change the UI look and feel.
86-
* To do that, use `enableTheme` or reload the app.
8718
*/
8819
export const setThemeName = (themeName: EuiThemeColorMode): void => {
8920
setStoredThemeName(themeName);

electron/renderer/pages/_app.tsx

+9-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { AppProps } from 'next/app';
66
import Head from 'next/head';
77
import { Layout } from '../components/layout.jsx';
88
import { NoSSRBoundary } from '../components/no-ssr/no-ssr.jsx';
9-
import { ChromeProvider } from '../context/chrome.jsx';
109
import { GameProvider } from '../context/game.jsx';
1110
import { LoggerProvider } from '../context/logger.jsx';
1211
import { ThemeProvider } from '../context/theme.jsx';
@@ -28,17 +27,15 @@ const App: React.FC<AppProps> = ({ Component, pageProps }: AppProps) => (
2827
*/}
2928
<NoSSRBoundary>
3029
<ThemeProvider>
31-
<ChromeProvider>
32-
<LoggerProvider>
33-
<EuiErrorBoundary>
34-
<GameProvider>
35-
<Layout>
36-
<Component {...pageProps} />
37-
</Layout>
38-
</GameProvider>
39-
</EuiErrorBoundary>
40-
</LoggerProvider>
41-
</ChromeProvider>
30+
<LoggerProvider>
31+
<EuiErrorBoundary>
32+
<GameProvider>
33+
<Layout>
34+
<Component {...pageProps} />
35+
</Layout>
36+
</GameProvider>
37+
</EuiErrorBoundary>
38+
</LoggerProvider>
4239
</ThemeProvider>
4340
</NoSSRBoundary>
4441
</>

electron/renderer/pages/_document.tsx

-24
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,8 @@
22
// https://github.com/elastic/next-eui-starter/blob/master/src/pages/_document.tsx
33

44
import { Head, Html, Main, NextScript } from 'next/document';
5-
import type { LinkHTMLAttributes, ReactElement } from 'react';
65
import type React from 'react';
76

8-
/**
9-
* Nextjs wants you to import CSS stylesheets in the `pages/_app.tsx` file.
10-
* However, the @elastic/eui library instructs you to load their themes here.
11-
* We also need to import custom stylesheets, so instead of splitting some
12-
* of that in the `pages/_app.tsx` file, we do it all here.
13-
*
14-
* To get around the eslint rule and console warnings, we cannot use
15-
* the `<link>` element in the `Head` element directly.
16-
* So instead we use functions.
17-
*/
18-
function _createStyleLink(
19-
props: LinkHTMLAttributes<HTMLLinkElement>
20-
): ReactElement {
21-
return <link rel="stylesheet" {...props} />;
22-
}
23-
247
/**
258
* A custom `Document` is commonly used to augment your application's
269
* `<html>` and `<body>` tags. This is necessary because Next.js pages skip
@@ -29,16 +12,9 @@ function _createStyleLink(
2912
* @see https://nextjs.org/docs/advanced-features/custom-document
3013
*/
3114
const Document: React.FC = () => {
32-
// const yourCustomStyleLink = useMemo(() => {
33-
// return createStyleLink({ href: '/your/custom.min.css' });
34-
// }, []);
35-
3615
return (
3716
<Html lang="en">
3817
<Head>
39-
<meta name="eui-styles" />
40-
<meta name="eui-styles-utility" />
41-
{/* {yourCustomStyleLink} */}
4218
<meta
4319
httpEquiv="Content-Security-Policy"
4420
content={`

electron/renderer/pages/grid.tsx

+1-6
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,7 @@ const GridPage: React.FC = (): ReactNode => {
492492
/>
493493
</div>
494494
</EuiPageTemplate.Section>
495-
<EuiPageTemplate.BottomBar
496-
paddingSize="none"
497-
css={{
498-
backgroundColor: euiTheme.colors.lightestShade,
499-
}}
500-
>
495+
<EuiPageTemplate.BottomBar paddingSize="none">
501496
<div ref={bottomBarRef}>
502497
<EuiFieldText
503498
compressed={true}

0 commit comments

Comments
 (0)