Skip to content

Commit 4942940

Browse files
committed
feat: use nextcloud-theme-dark var for dark theme computation
Signed-off-by: John Molakvoæ <skjnldsv@users.noreply.github.com>
1 parent 93485d9 commit 4942940

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/functions/isDarkTheme/index.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@
99
* @return {boolean} - Whether the dark theme is enabled via Nextcloud theme
1010
*/
1111
export function checkIfDarkTheme(el: HTMLElement = document.body): boolean {
12-
// Nextcloud uses --background-invert-if-dark for dark theme filters in CSS
13-
// Values:
14-
// - 'invert(100%)' for dark theme
15-
// - 'no' for light theme
16-
// This is the most reliable way to check for dark theme, including custom themes
17-
const backgroundInvertIfDark = window.getComputedStyle(el).getPropertyValue('--background-invert-if-dark')
18-
if (backgroundInvertIfDark !== undefined) {
19-
return backgroundInvertIfDark === 'invert(100%)'
12+
// Check if the new clean nextcloud-theme-dark variable is set
13+
const isNextcloudThemeDark = window.getComputedStyle(el).getPropertyValue('--nextcloud-theme-dark')
14+
if (isNextcloudThemeDark !== undefined) {
15+
return isNextcloudThemeDark === '1'
2016
}
2117

22-
// There is no theme? Fallback to the light theme
23-
return false
18+
// Else we check the old ways
19+
// 1. first priority is the data-theme-dark attribute on a parent element
20+
// 2. second priority is the data-themes attribute on the body, aka the user setting
21+
// 3. third priority is the system preference
22+
// 4. lastly is the default light theme
23+
const darkModeSystemPreference = window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches
24+
const darkModeAccountSetting = document.body.getAttribute('data-themes')?.includes('dark')
25+
const darkModeElementOverride = el.closest('[data-theme-dark]') !== null
26+
return darkModeElementOverride || darkModeAccountSetting || darkModeSystemPreference || false
2427
}
2528

2629
/**

0 commit comments

Comments
 (0)