-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joel Anton
committed
May 15, 2024
1 parent
40a3d8f
commit 8bc533d
Showing
4 changed files
with
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ColorScheme } from './ColorScheme'; | ||
import { getColorSchemeHtmlElement } from './getColorSchemeHtmlElement'; | ||
|
||
const DEFAULT_COLOR_SCHEME: ColorScheme = 'light'; | ||
|
||
/** | ||
* Gets the user's preferred color scheme according to their browser settings. | ||
* @returns ColorScheme | ||
*/ | ||
export const getBrowserColorScheme = (): ColorScheme => { | ||
return window?.matchMedia?.(`(prefers-color-scheme: dark)`)?.matches ? 'dark' : DEFAULT_COLOR_SCHEME; | ||
}; | ||
|
||
/** | ||
* Get the current color scheme of the application based on the application html. | ||
* @returns ColorScheme | ||
*/ | ||
export const getCurrentColorScheme = (): ColorScheme => { | ||
const htmlElem = getColorSchemeHtmlElement(); | ||
|
||
// fallback to browser preferences if there isn't an html element | ||
if (!htmlElem?.classList) { | ||
return getBrowserColorScheme(); | ||
} | ||
|
||
return htmlElem.classList.contains('dark') | ||
? 'dark' | ||
: htmlElem.classList.contains('light') | ||
? 'light' | ||
: DEFAULT_COLOR_SCHEME; | ||
}; |
14 changes: 14 additions & 0 deletions
14
libs/design-system/src/color-scheme/getColorSchemeHtmlElement.ts
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,14 @@ | ||
/** Get the innermost <html> element that serves as the basis for our theme */ | ||
export const getColorSchemeHtmlElement = (): HTMLHtmlElement | null => { | ||
/** | ||
* Avoid issues with multiple `html` elements (like in Storybook) by getting all html elements | ||
* and taking the innermost one | ||
*/ | ||
const htmlElements = document.querySelectorAll('html'); | ||
|
||
if (!htmlElements?.length) { | ||
return null; | ||
} | ||
|
||
return htmlElements.item(htmlElements.length - 1); | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './ColorScheme'; | ||
export * from './mapThemeStatusToColorScheme'; | ||
export * from './useColorScheme'; | ||
export * from './getColorScheme'; |
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