Skip to content

Commit

Permalink
feat(dark-mode): add dark mode theme support in the theme provider
Browse files Browse the repository at this point in the history
  • Loading branch information
clovisdasilvaneto committed Oct 9, 2024
1 parent 8f85da9 commit bc238d6
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/tx-builder/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ThemeProvider } from 'styled-components'
ReactDOM.render(
<>
<GlobalStyles />
<SafeThemeProvider mode="light">
<SafeThemeProvider>
{theme => (
<ThemeProvider theme={theme}>
<SafeProvider>
Expand Down
29 changes: 24 additions & 5 deletions apps/tx-builder/src/theme/SafeThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import { useMemo, type FC } from 'react'
import { type PaletteMode, type Theme } from '@mui/material'
import { useEffect, useMemo, useState, type FC } from 'react'
import { type Theme } from '@mui/material'
import { ThemeProvider } from '@material-ui/core'
import createSafeTheme from './safeTheme'
import { getSDKVersion } from '@safe-global/safe-apps-sdk'

type SafeThemeProviderProps = {
children: (theme: Theme) => React.ReactNode
mode: PaletteMode
}

const SafeThemeProvider: FC<SafeThemeProviderProps> = ({ children, mode }) => {
const theme = useMemo(() => createSafeTheme(mode), [mode])
const SafeThemeProvider: FC<SafeThemeProviderProps> = ({ children }) => {
const [isDarkMode, setDarkMode] = useState(false)

const theme = useMemo(() => createSafeTheme(isDarkMode ? 'dark' : 'light'), [isDarkMode])

useEffect(() => {
window.parent.postMessage(
{
id: 'tx-builder',
env: { sdkVersion: getSDKVersion() },
method: 'getCurrentTheme',
},
'*',
)

window.addEventListener('message', function (event) {
if (!event.data?.data.hasOwnProperty('darkMode')) return

setDarkMode(event.data?.data.darkMode)
})
}, [])

return <ThemeProvider theme={theme}>{children(theme)}</ThemeProvider>
}
Expand Down
68 changes: 68 additions & 0 deletions apps/tx-builder/src/theme/darkPalette.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const darkPalette = {
text: {
primary: '#FFFFFF',
secondary: '#636669',
disabled: '#636669',
},
primary: {
dark: '#0cb259',
main: '#12FF80',
light: '#A1A3A7',
},
secondary: {
dark: '#636669',
main: '#FFFFFF',
light: '#B0FFC9',
background: '#1B2A22',
},
border: {
main: '#636669',
light: '#303033',
background: '#121312',
},
error: {
dark: '#AC2C3B',
main: '#FF5F72',
light: '#FFB4BD',
background: '#2F2527',
},
success: {
dark: '#028D4C',
main: '#00B460',
light: '#81C784',
background: '#1F2920',
},
info: {
dark: '#52BFDC',
main: '#5FDDFF',
light: '#B7F0FF',
background: '#19252C',
},
warning: {
dark: '#C04C32',
main: '#FF8061',
light: '#FFBC9F',
background: '#2F2318',
},
background: {
default: '#121312',
main: '#121312',
paper: '#1C1C1C',
light: '#1B2A22',
},
backdrop: {
main: '#636669',
},
logo: {
main: '#FFFFFF',
background: '#303033',
},
static: {
main: '#121312',
},
code: {
main: 'transparent',
},
}

export default darkPalette
8 changes: 4 additions & 4 deletions apps/tx-builder/src/theme/safeTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Shadows } from '@mui/material/styles'
import { createTheme } from '@mui/material/styles'

import palette from './lightPalette'
import darkPalette from './darkPalette'
import typography from './typography'

export const base = 8
Expand Down Expand Up @@ -67,12 +68,13 @@ declare module '@mui/material/IconButton' {

const createSafeTheme = (mode: PaletteMode): Theme => {
const isDarkMode = mode === 'dark'
const shadowColor = palette.primary.light
const colors = isDarkMode ? darkPalette : palette
const shadowColor = colors.primary.light

return createTheme({
palette: {
mode: isDarkMode ? 'dark' : 'light',
...palette,
...colors,
},
spacing: base,
shape: {
Expand Down Expand Up @@ -515,6 +517,4 @@ const createSafeTheme = (mode: PaletteMode): Theme => {
})
}

export const defaultTheme = createSafeTheme('light')

export default createSafeTheme

0 comments on commit bc238d6

Please sign in to comment.