Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dark mode fixes: editable note and persistence (#10754)
Signed-off-by: Hubert Zub <hubert.zub@databricks.com>
- Loading branch information
1 parent
e5df626
commit 4be7f5f
Showing
3 changed files
with
46 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ | |
margin-left: 16px; | ||
} | ||
|
||
.mde-header { | ||
background: var(--secondary-background-color); | ||
} |
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,39 @@ | ||
import { Global } from '@emotion/react'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
const darkModePrefLocalStorageKey = '_mlflow_dark_mode_toggle_enabled'; | ||
const darkModeBodyClassName = 'dark-mode'; | ||
|
||
// CSS attributes to be applied when dark mode is enabled. Affects inputs and other form elements. | ||
const darkModeCSSStyles = { body: { [`&.${darkModeBodyClassName}`]: { colorScheme: 'dark' } } }; | ||
// This component is used to set the global CSS. | ||
const DarkModeStylesComponent = () => <Global styles={darkModeCSSStyles} />; | ||
|
||
/** | ||
* This hook is used to toggle the dark mode for the entire app. | ||
* Used in open source MLflow. | ||
* Returns a boolean value with the current state, setter function, and a component to be rendered in the root of the app. | ||
*/ | ||
export const useMLflowDarkTheme = (): [ | ||
boolean, | ||
React.Dispatch<React.SetStateAction<boolean>>, | ||
React.ComponentType, | ||
] => { | ||
const [isDarkTheme, setIsDarkTheme] = useState(() => { | ||
// If the user has explicitly set a preference, use that. | ||
if (localStorage.getItem(darkModePrefLocalStorageKey) === 'true') { | ||
return true; | ||
} | ||
// Otherwise, use the system preference as a default. | ||
return window.matchMedia('(prefers-color-scheme: dark)').matches || false; | ||
}); | ||
|
||
useEffect(() => { | ||
// Update the theme when the user changes their system preference. | ||
document.body.classList.toggle(darkModeBodyClassName, isDarkTheme); | ||
// Persist the user's preference in local storage. | ||
localStorage.setItem(darkModePrefLocalStorageKey, isDarkTheme ? 'true' : 'false'); | ||
}, [isDarkTheme]); | ||
|
||
return [isDarkTheme, setIsDarkTheme, DarkModeStylesComponent]; | ||
}; |