-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport pull request #5589 from jellyfin-web/release-10.9.z
Add support for user themes for mui components Original-merge: 61976b8 Merged-by: thornbill <thornbill@users.noreply.github.com> Backported-by: Joshua M. Boniface <joshua@boniface.me>
- Loading branch information
1 parent
669784b
commit 1929ba8
Showing
16 changed files
with
298 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
src/apps/experimental/routes/user/display/hooks/useServerThemes.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { useWebConfig } from './useWebConfig'; | ||
|
||
export function useThemes() { | ||
const { themes } = useWebConfig(); | ||
|
||
const defaultTheme = useMemo(() => { | ||
return themes?.find(theme => theme.default); | ||
}, [ themes ]); | ||
|
||
return { | ||
themes: themes || [], | ||
defaultTheme | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
import { currentSettings as userSettings } from 'scripts/settings/userSettings'; | ||
import Events from 'utils/events'; | ||
|
||
import { useApi } from './useApi'; | ||
import { useThemes } from './useThemes'; | ||
|
||
const THEME_FIELD_NAMES = [ 'appTheme', 'dashboardTheme' ]; | ||
|
||
export function useUserTheme() { | ||
const [ theme, setTheme ] = useState<string>(); | ||
const [ dashboardTheme, setDashboardTheme ] = useState<string>(); | ||
|
||
const { user } = useApi(); | ||
const { defaultTheme } = useThemes(); | ||
|
||
useEffect(() => { | ||
if (defaultTheme) { | ||
if (!theme) setTheme(defaultTheme.id); | ||
if (!dashboardTheme) setDashboardTheme(defaultTheme.id); | ||
} | ||
}, [ dashboardTheme, defaultTheme, theme ]); | ||
|
||
// Update the current themes with values from user settings | ||
const updateThemesFromSettings = useCallback(() => { | ||
const userTheme = userSettings.theme(); | ||
if (userTheme) setTheme(userTheme); | ||
const userDashboardTheme = userSettings.dashboardTheme(); | ||
if (userDashboardTheme) setDashboardTheme(userDashboardTheme); | ||
}, []); | ||
|
||
const onUserSettingsChange = useCallback((_e, name?: string) => { | ||
if (name && THEME_FIELD_NAMES.includes(name)) { | ||
updateThemesFromSettings(); | ||
} | ||
}, [ updateThemesFromSettings ]); | ||
|
||
// Handle user settings changes | ||
useEffect(() => { | ||
Events.on(userSettings, 'change', onUserSettingsChange); | ||
|
||
return () => { | ||
Events.off(userSettings, 'change', onUserSettingsChange); | ||
}; | ||
}, [ onUserSettingsChange ]); | ||
|
||
// Update the theme if the user changes | ||
useEffect(() => { | ||
updateThemesFromSettings(); | ||
}, [ updateThemesFromSettings, user ]); | ||
|
||
return { | ||
theme, | ||
dashboardTheme | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ThemeProvider } from '@mui/material'; | ||
import React, { type FC, useState, useEffect } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
import { DASHBOARD_APP_PATHS } from 'apps/dashboard/routes/routes'; | ||
import { useUserTheme } from 'hooks/useUserTheme'; | ||
|
||
import { DEFAULT_THEME, getTheme } from './themes'; | ||
|
||
const isDashboardThemePage = (pathname: string) => [ | ||
// NOTE: The metadata manager doesn't seem to use the dashboard theme | ||
DASHBOARD_APP_PATHS.Dashboard, | ||
DASHBOARD_APP_PATHS.PluginConfig | ||
].some(path => pathname.startsWith(`/${path}`)); | ||
|
||
const UserThemeProvider: FC = ({ children }) => { | ||
const [ isDashboard, setIsDashboard ] = useState(false); | ||
const [ muiTheme, setMuiTheme ] = useState(DEFAULT_THEME); | ||
|
||
const location = useLocation(); | ||
const { theme, dashboardTheme } = useUserTheme(); | ||
|
||
// Check if we are on a dashboard page when the path changes | ||
useEffect(() => { | ||
setIsDashboard(isDashboardThemePage(location.pathname)); | ||
}, [ location.pathname ]); | ||
|
||
useEffect(() => { | ||
if (isDashboard) { | ||
setMuiTheme(getTheme(dashboardTheme)); | ||
} else { | ||
setMuiTheme(getTheme(theme)); | ||
} | ||
}, [ dashboardTheme, isDashboard, theme ]); | ||
|
||
return ( | ||
<ThemeProvider theme={muiTheme}> | ||
{children} | ||
</ThemeProvider> | ||
); | ||
}; | ||
|
||
export default UserThemeProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import createTheme, { type ThemeOptions } from '@mui/material/styles/createTheme'; | ||
import merge from 'lodash-es/merge'; | ||
|
||
import { DEFAULT_THEME_OPTIONS } from 'themes/defaults'; | ||
|
||
const themeOptions: ThemeOptions = { | ||
palette: { | ||
mode: 'light', | ||
background: { | ||
default: '#d5e9f2', | ||
paper: '#fff' | ||
} | ||
}, | ||
components: { | ||
MuiAppBar: { | ||
styleOverrides: { | ||
colorPrimary: { | ||
backgroundColor: '#bcbcbc' | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const theme = createTheme(merge({}, DEFAULT_THEME_OPTIONS, themeOptions)); | ||
|
||
export default theme; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import createTheme, { type ThemeOptions } from '@mui/material/styles/createTheme'; | ||
import merge from 'lodash-es/merge'; | ||
|
||
import { DEFAULT_THEME_OPTIONS } from 'themes/defaults'; | ||
|
||
const options: ThemeOptions = { | ||
palette: { | ||
background: { | ||
paper: '#011432' | ||
} | ||
} | ||
}; | ||
|
||
const theme = createTheme(merge({}, DEFAULT_THEME_OPTIONS, options)); | ||
|
||
export default theme; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import createTheme from '@mui/material/styles/createTheme'; | ||
|
||
import { DEFAULT_THEME_OPTIONS } from 'themes/defaults'; | ||
|
||
const theme = createTheme(DEFAULT_THEME_OPTIONS); | ||
|
||
export default theme; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import createTheme, { type ThemeOptions } from '@mui/material/styles/createTheme'; | ||
import merge from 'lodash-es/merge'; | ||
|
||
import { DEFAULT_THEME_OPTIONS } from 'themes/defaults'; | ||
|
||
const options: ThemeOptions = { | ||
palette: { | ||
mode: 'light', | ||
background: { | ||
default: '#f2f2f2', | ||
// NOTE: The original theme uses #303030 for the drawer and app bar but we would need the drawer to use | ||
// dark mode for a color that dark to work properly which would require a separate ThemeProvider just for | ||
// the drawer... which is not worth the trouble in my opinion | ||
paper: '#e8e8e8' | ||
} | ||
}, | ||
components: { | ||
MuiAppBar: { | ||
styleOverrides: { | ||
colorPrimary: { | ||
backgroundColor: '#e8e8e8' | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const theme = createTheme(merge({}, DEFAULT_THEME_OPTIONS, options)); | ||
|
||
export default theme; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import createTheme, { type ThemeOptions } from '@mui/material/styles/createTheme'; | ||
import merge from 'lodash-es/merge'; | ||
|
||
import { DEFAULT_THEME_OPTIONS } from 'themes/defaults'; | ||
|
||
const options: ThemeOptions = { | ||
palette: { | ||
background: { | ||
paper: '#000420' | ||
}, | ||
primary: { | ||
main: '#48c3c8' | ||
}, | ||
secondary: { | ||
main: '#ff77f1' | ||
} | ||
} | ||
}; | ||
|
||
const theme = createTheme(merge({}, DEFAULT_THEME_OPTIONS, options)); | ||
|
||
export default theme; |
Oops, something went wrong.