aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-04-19 02:40:25 +0200
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-05-16 00:55:01 +0200
commite95c17947ab36d82e7c401577912e9ddffadd0c6 (patch)
tree9ef5b18def985656388ee91a8afdce1153e6d1eb
parentchore(deps): Bump yarn version (diff)
downloadsophie-e95c17947ab36d82e7c401577912e9ddffadd0c6.tar.gz
sophie-e95c17947ab36d82e7c401577912e9ddffadd0c6.tar.zst
sophie-e95c17947ab36d82e7c401577912e9ddffadd0c6.zip
fix(renderer): Make RTL flipping more resilient to hot reloading
It seems moving the emotion cache into a separate file makes vite less likely to try to hot-reload @emotion/cache (which would fail due to creating the default cache twice). We still have some vite hot reload problems, possibly connecte to loading versions of react components with a different number of hook calls. Signed-off-by: Kristóf Marussy <kristof@marussy.com>
-rw-r--r--packages/renderer/src/i18n/RtlCacheProvider.tsx17
-rw-r--r--packages/renderer/src/i18n/createRtlCache.ts30
-rw-r--r--packages/renderer/src/index.tsx5
3 files changed, 42 insertions, 10 deletions
diff --git a/packages/renderer/src/i18n/RtlCacheProvider.tsx b/packages/renderer/src/i18n/RtlCacheProvider.tsx
index d54308b..be770b8 100644
--- a/packages/renderer/src/i18n/RtlCacheProvider.tsx
+++ b/packages/renderer/src/i18n/RtlCacheProvider.tsx
@@ -18,21 +18,20 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import createCache from '@emotion/cache'; 21import type { EmotionCache } from '@emotion/cache';
22import { CacheProvider } from '@emotion/react'; 22import { CacheProvider } from '@emotion/react';
23import { observer } from 'mobx-react-lite'; 23import { observer } from 'mobx-react-lite';
24import React, { ReactNode } from 'react'; 24import React, { ReactNode } from 'react';
25import { prefixer } from 'stylis';
26import rtlPlugin from 'stylis-plugin-rtl';
27 25
28import { useStore } from '../components/StoreProvider'; 26import { useStore } from '../components/StoreProvider';
29 27
30const rtlCache = createCache({ 28function RtlCacheProvider({
31 key: 'muirtl', 29 children,
32 stylisPlugins: [prefixer, rtlPlugin], 30 rtlCache,
33}); 31}: {
34 32 children?: ReactNode;
35function RtlCacheProvider({ children }: { children?: ReactNode }): JSX.Element { 33 rtlCache: EmotionCache;
34}): JSX.Element {
36 const { 35 const {
37 shared: { writingDirection }, 36 shared: { writingDirection },
38 } = useStore(); 37 } = useStore();
diff --git a/packages/renderer/src/i18n/createRtlCache.ts b/packages/renderer/src/i18n/createRtlCache.ts
new file mode 100644
index 0000000..3c0f923
--- /dev/null
+++ b/packages/renderer/src/i18n/createRtlCache.ts
@@ -0,0 +1,30 @@
1/*
2 * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import createCache, { type EmotionCache } from '@emotion/cache';
22import { prefixer } from 'stylis';
23import rtlPlugin from 'stylis-plugin-rtl';
24
25export default function createRtlCache(): EmotionCache {
26 return createCache({
27 key: 'muirtl',
28 stylisPlugins: [prefixer, rtlPlugin],
29 });
30}
diff --git a/packages/renderer/src/index.tsx b/packages/renderer/src/index.tsx
index 2722d3b..72a9ffd 100644
--- a/packages/renderer/src/index.tsx
+++ b/packages/renderer/src/index.tsx
@@ -33,6 +33,7 @@ import StoreProvider from './components/StoreProvider';
33import ThemeProvider from './components/ThemeProvider'; 33import ThemeProvider from './components/ThemeProvider';
34import { exposeToReduxDevtools, hotReload } from './devTools'; 34import { exposeToReduxDevtools, hotReload } from './devTools';
35import RtlCacheProvider from './i18n/RtlCacheProvider'; 35import RtlCacheProvider from './i18n/RtlCacheProvider';
36import createRtlCache from './i18n/createRtlCache';
36import loadRendererLocalization from './i18n/loadRendererLoalization'; 37import loadRendererLocalization from './i18n/loadRendererLoalization';
37import { createAndConnectRendererStore } from './stores/RendererStore'; 38import { createAndConnectRendererStore } from './stores/RendererStore';
38import { getLogger } from './utils/log'; 39import { getLogger } from './utils/log';
@@ -66,13 +67,15 @@ const disposeSetHtmlLang = autorun(() => {
66}); 67});
67addDisposer(store, disposeSetHtmlLang); 68addDisposer(store, disposeSetHtmlLang);
68 69
70const rtlCache = createRtlCache();
71
69const App = lazy(() => import('./components/App')); 72const App = lazy(() => import('./components/App'));
70 73
71function Root(): JSX.Element { 74function Root(): JSX.Element {
72 return ( 75 return (
73 <React.StrictMode> 76 <React.StrictMode>
74 <StoreProvider store={store}> 77 <StoreProvider store={store}>
75 <RtlCacheProvider> 78 <RtlCacheProvider rtlCache={rtlCache}>
76 <ThemeProvider> 79 <ThemeProvider>
77 <CssBaseline enableColorScheme /> 80 <CssBaseline enableColorScheme />
78 <Suspense fallback={<Loading />}> 81 <Suspense fallback={<Loading />}>