From 69bccb0445ce3c73cc6e39a21921eca06285c05f Mon Sep 17 00:00:00 2001 From: Amir Ekbatanifard Date: Tue, 29 Oct 2024 10:32:19 +0330 Subject: [PATCH] fix: theme change sync issue (#1616) --- .../src/components/View.tsx | 53 ++++++++++++------- .../src/components/chooseTheme.ts | 3 +- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/packages/extension-polkagate/src/components/View.tsx b/packages/extension-polkagate/src/components/View.tsx index 58289177f..5fd6fd8ca 100644 --- a/packages/extension-polkagate/src/components/View.tsx +++ b/packages/extension-polkagate/src/components/View.tsx @@ -1,10 +1,11 @@ // Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors // SPDX-License-Identifier: Apache-2.0 -// @ts-nocheck -import { CssBaseline, PaletteMode, Theme } from '@mui/material'; +import type { PaletteMode, Theme } from '@mui/material'; + +import { CssBaseline } from '@mui/material'; import { createTheme, ThemeProvider } from '@mui/material/styles'; -import React from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; import { createGlobalStyle } from 'styled-components'; import { darkTheme as dark } from '../themes/dark'; @@ -16,25 +17,37 @@ interface Props { className?: string; } -function View({ children, className }: Props): React.ReactElement { - const [mode, setMode] = React.useState(chooseTheme()); - - const colorMode = React.useMemo( - () => ({ - toggleColorMode: () => { - const toMode = mode === 'light' ? 'dark' : 'light'; +function View ({ children, className }: Props): React.ReactElement { + const [mode, setMode] = useState(chooseTheme()); - localStorage.setItem('theme', toMode); - setMode(toMode); + useEffect(() => { + // Handler for storage events + const handleStorageChange = (event: StorageEvent) => { + if (event.key === 'theme') { + // Type assertion since we know theme can only be 'light' or 'dark' + setMode(event.newValue as PaletteMode); } - }), - [mode] - ); + }; - const theme = React.useMemo( - () => createTheme(mode === 'light' ? light : dark), - [mode] - ); + // Add event listener + window.addEventListener('storage', handleStorageChange); + + // Cleanup + return () => { + window.removeEventListener('storage', handleStorageChange); + }; + }, []); + + const colorMode = useMemo(() => ({ + toggleColorMode: () => { + const toMode = mode === 'light' ? 'dark' : 'light'; + + localStorage.setItem('theme', toMode); + setMode(toMode); + } + }), [mode]); + + const theme = useMemo(() => createTheme(mode === 'light' ? light : dark), [mode]); return ( @@ -49,7 +62,7 @@ function View({ children, className }: Props): React.ReactElement { ); } -const BodyTheme = createGlobalStyle` +const BodyTheme = createGlobalStyle<{theme: Theme}>` body { background-color: ${(props) => props.theme.palette.background.paper}; } diff --git a/packages/extension-polkagate/src/components/chooseTheme.ts b/packages/extension-polkagate/src/components/chooseTheme.ts index 492c26b36..a1cebe5ee 100644 --- a/packages/extension-polkagate/src/components/chooseTheme.ts +++ b/packages/extension-polkagate/src/components/chooseTheme.ts @@ -1,8 +1,7 @@ // Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors // SPDX-License-Identifier: Apache-2.0 -// @ts-nocheck -export function chooseTheme(): 'dark' | 'light' { +export function chooseTheme (): 'dark' | 'light' { const preferredTheme = localStorage.getItem('theme'); if (preferredTheme) {